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-2025 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
28namespace {
29
30template <typename T>
31inline T normalizeCompletionPercentage(const T value) {
32 if (value < 0)
33 return 0;
34 if (value > 100)
35 return 100;
36 return value;
37}
38
39template <typename T>
40inline 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
53template <typename T, class = void>
55 template <typename ... Args>
56 static void setTimeoutMillis(Args && ...) noexcept {}
57};
58
59template <typename T>
61 std::void_t<decltype(std::declval<T &>().setTimeoutMillis(0))>>
62{
63 template <typename ... Args>
64 static void setTimeoutMillis(T & c, Args && ... args)
65 { c.setTimeoutMillis(std::forward<Args>(args)...); }
66};
67
68} // anonymous namespace
69
70using namespace sword;
71
73 : QObject(parent)
74 , InstallMgr(BtInstallBackend::configPath().toLatin1(), this)
75 , m_totalBytes(1)
76 , m_completedBytes(0)
77 , m_firstCallOfPreStatus(true)
78{
79 setFTPPassive(true);
81}
82
84 //doesn't really help because it only sets a flag
85 this->terminate(); // make sure to close the connection
86}
87
89 //// \todo Check from config if it's been confirmed with "don't show this anymore" checked.
90 // Create a dialog with the message, checkbox and Continue/Cancel, Cancel as default.
91 return true;
92}
93
94void BtInstallMgr::statusUpdate(double dltotal, double dlnow) {
95 /**
96 \warning Note that these *might be* rough measures due to the double data
97 type being used by Sword to store the number of bytes. Special
98 care must be taken to work around this, since the arguments may
99 contain weird values which would otherwise break this logic.
100 */
101
102 if (dltotal < 0.0) // Special care (see warning above)
103 dltotal = 0.0;
104 if (dlnow < 0.0) // Special care (see warning above)
105 dlnow = 0.0;
106
107 const int totalPercent = calculateIntPercentage<double>(dlnow + m_completedBytes,
109 const int filePercent = calculateIntPercentage(dlnow, dltotal);
110
111 //qApp->processEvents();
112 Q_EMIT percentCompleted(totalPercent, filePercent);
113}
114
115
116void BtInstallMgr::preStatus(long totalBytes,
117 long completedBytes,
118 const char * message)
119{
120 Q_UNUSED(message)
121 BT_ASSERT(completedBytes <= totalBytes);
124 Q_EMIT downloadStarted();
125 }
126 m_completedBytes = completedBytes;
127 m_totalBytes = totalBytes;
128}
#define BT_ASSERT(...)
Definition btassert.h:17
void percentCompleted(const int total, const int file)
BtInstallMgr(QObject *parent=nullptr)
bool m_firstCallOfPreStatus
void statusUpdate(double dltotal, double dlnow) override
void downloadStarted()
bool isUserDisclaimerConfirmed() const override
~BtInstallMgr() override
long m_completedBytes
void preStatus(long totalBytes, long completedBytes, const char *message) override
#define T(f)
int calculateIntPercentage(T done, T total)