BibleTime
btinstallmgr.cpp
Go to the documentation of this file.
1 /*********
2 *
3 * In the name of the Father, and of the Son, and of the Holy Spirit.
4 *
5 * This file is part of BibleTime's source code, https://bibletime.info/
6 *
7 * Copyright 1999-2021 by the BibleTime developers.
8 * The BibleTime source code is licensed under the GNU General Public License
9 * version 2.0.
10 *
11 **********/
12 
13 #include "btinstallmgr.h"
14 
15 #include <QByteArray>
16 #include <QtGlobal>
17 #include <type_traits>
18 #include <utility>
19 #include "../util/btassert.h"
20 #include "btinstallbackend.h"
21 
22 
23 // Sword includes:
24 #include <defs.h>
25 #include <installmgr.h>
26 
27 
28 namespace {
29 
30 template <typename T>
31 inline T normalizeCompletionPercentage(const T value) {
32  if (value < 0)
33  return 0;
34  if (value > 100)
35  return 100;
36  return value;
37 }
38 
39 template <typename T>
40 inline int calculateIntPercentage(T done, T total) {
41  BT_ASSERT(done >= 0);
42  BT_ASSERT(total >= 0);
43 
44  // Special care (see warning in BtInstallMgr::statusUpdate()).
45  if (done > total)
46  done = total;
47  if (total <= 0.0)
48  return 100;
49 
50  return normalizeCompletionPercentage<int>((done / total) * 100);
51 }
52 
53 template <typename T, class = void>
54 struct HasSetTimeoutMillis : std::false_type {};
55 
56 template <typename T>
58  std::void_t<decltype(std::declval<T &>().setTimeoutMillis(0))>>
59  : std::true_type
60 {};
61 
62 } // anonymous namespace
63 
64 using namespace sword;
65 
67  : QObject(parent)
68  , InstallMgr(BtInstallBackend::configPath().toLatin1(), this)
69  , m_totalBytes(1)
70  , m_completedBytes(0)
71  , m_firstCallOfPreStatus(true)
72 {
73  setFTPPassive(true);
74  if constexpr (HasSetTimeoutMillis<BtInstallMgr>::value)
75  setTimeoutMillis(0);
76 }
77 
79  //doesn't really help because it only sets a flag
80  this->terminate(); // make sure to close the connection
81 }
82 
84  //// \todo Check from config if it's been confirmed with "don't show this anymore" checked.
85  // Create a dialog with the message, checkbox and Continue/Cancel, Cancel as default.
86  return true;
87 }
88 
89 void BtInstallMgr::statusUpdate(double dltotal, double dlnow) {
90  /**
91  \warning Note that these *might be* rough measures due to the double data
92  type being used by Sword to store the number of bytes. Special
93  care must be taken to work around this, since the arguments may
94  contain weird values which would otherwise break this logic.
95  */
96 
97  if (dltotal < 0.0) // Special care (see warning above)
98  dltotal = 0.0;
99  if (dlnow < 0.0) // Special care (see warning above)
100  dlnow = 0.0;
101 
102  const int totalPercent = calculateIntPercentage<double>(dlnow + m_completedBytes,
103  m_totalBytes);
104  const int filePercent = calculateIntPercentage(dlnow, dltotal);
105 
106  //qApp->processEvents();
107  Q_EMIT percentCompleted(totalPercent, filePercent);
108 }
109 
110 
111 void BtInstallMgr::preStatus(long totalBytes,
112  long completedBytes,
113  const char * message)
114 {
115  Q_UNUSED(message)
116  BT_ASSERT(completedBytes <= totalBytes);
118  m_firstCallOfPreStatus = false;
119  Q_EMIT downloadStarted();
120  }
121  m_completedBytes = completedBytes;
122  m_totalBytes = totalBytes;
123 }
#define BT_ASSERT(...)
Definition: btassert.h:17
long m_totalBytes
Definition: btinstallmgr.h:74
void percentCompleted(const int total, const int file)
BtInstallMgr(QObject *parent=nullptr)
bool m_firstCallOfPreStatus
Definition: btinstallmgr.h:76
void statusUpdate(double dltotal, double dlnow) override
void downloadStarted()
bool isUserDisclaimerConfirmed() const override
~BtInstallMgr() override
long m_completedBytes
Definition: btinstallmgr.h:75
void preStatus(long totalBytes, long completedBytes, const char *message) override
#define T(f)
int calculateIntPercentage(T done, T total)
T normalizeCompletionPercentage(const T value)