Qt Utilities 6.21.0
Common Qt related C++ classes and routines used by my applications such as dialogs, widgets and models
Loading...
Searching...
No Matches
updater.h
Go to the documentation of this file.
1#ifndef QT_UTILITIES_SETUP_UPDATER_H
2#define QT_UTILITIES_SETUP_UPDATER_H
3
4#include "../global.h"
5
6#ifdef QT_UTILITIES_GUI_QTWIDGETS
9#endif
10
11#include <c++utilities/chrono/datetime.h>
12#include <c++utilities/misc/flagenumclass.h>
13
14#include <QObject>
15#include <QUrl>
16
17#ifdef QT_UTILITIES_SETUP_TOOLS_ENABLED
18#include <QNetworkRequest>
19#endif
20#ifdef QT_UTILITIES_GUI_QTWIDGETS
21#include <QMessageBox>
22#endif
23
24#include <atomic>
25#include <memory>
26
27QT_FORWARD_DECLARE_CLASS(QJsonParseError)
28QT_FORWARD_DECLARE_CLASS(QJsonArray)
29QT_FORWARD_DECLARE_CLASS(QNetworkAccessManager)
30QT_FORWARD_DECLARE_CLASS(QNetworkReply)
31QT_FORWARD_DECLARE_CLASS(QSettings)
32QT_FORWARD_DECLARE_CLASS(QVersionNumber)
33
34namespace QtUtilities {
35
37struct VersionAndSuffix;
39struct UpdaterPrivate;
41struct UpdateOptionPagePrivate;
42struct UpdateDialogPrivate;
44
45enum class UpdateCheckFlags : quint64 {
46 None = 0,
48 IncludeDrafts = (1 << 1),
50};
51
52} // namespace QtUtilities
53
55
56namespace QtUtilities {
57
61class QT_UTILITIES_EXPORT UpdateNotifier : public QObject {
62 Q_OBJECT
63 Q_PROPERTY(bool supported READ isSupported)
64 Q_PROPERTY(bool inProgress READ isInProgress NOTIFY inProgressChanged)
65 Q_PROPERTY(bool updateAvailable READ isUpdateAvailable)
66 Q_PROPERTY(QString executableName READ executableName CONSTANT)
67 Q_PROPERTY(QString newVersion READ newVersion)
68 Q_PROPERTY(QString additionalInfo READ additionalInfo)
69 Q_PROPERTY(QString releaseNotes READ releaseNotes)
70 Q_PROPERTY(QString error READ error)
71 Q_PROPERTY(QUrl downloadUrl READ downloadUrl)
72 Q_PROPERTY(QUrl signatureUrl READ signatureUrl)
75
76public:
77 explicit UpdateNotifier(QObject *parent = nullptr);
78 ~UpdateNotifier() override;
79
80 bool isSupported() const;
81 bool isInProgress() const;
82 bool isUpdateAvailable() const;
83 UpdateCheckFlags flags() const;
85 const QString &executableName() const;
86 const QString &newVersion() const;
87 const QString &latestVersion() const;
88 const QString &additionalInfo() const;
89 const QString &releaseNotes() const;
90 const QString &error() const;
91 const QUrl &downloadUrl() const;
92 const QUrl &signatureUrl() const;
93 const QUrl &previousVersionDownloadUrl() const;
94 const QUrl &previousVersionSignatureUrl() const;
95 QString status() const;
96 CppUtilities::DateTime lastCheck() const;
97 void restore(QSettings *settings);
98 void save(QSettings *settings);
99 void setNetworkAccessManager(QNetworkAccessManager *nm);
100#ifdef QT_UTILITIES_SETUP_TOOLS_ENABLED
101 void setCacheLoadControl(QNetworkRequest::CacheLoadControl cacheLoadControl);
102 static bool isVersionHigher(const QString &lhs, const QString &rhs);
103#endif
104
105public Q_SLOTS:
106 void checkForUpdate();
107 void resetUpdateInfo();
108 void supplyNewReleaseData(const QByteArray &data);
109
110Q_SIGNALS:
113 void updateAvailable(const QString &version, const QString &additionalInfo);
114
115private Q_SLOTS:
116 void lastCheckNow() const;
117 void setError(const QString &context, QNetworkReply *reply);
118 void setError(const QString &context, const QJsonParseError &jsonError, const QByteArray &response);
119 void readReleases();
120 void queryRelease(const QUrl &releaseUrl, bool forUpdate, bool forPreviousVersion);
121 void readRelease();
122 void processAssets(const QJsonArray &assets, bool forUpdate, bool forPreviousVersion);
123
124private:
125 std::unique_ptr<UpdateNotifierPrivate> m_p;
126};
127
131class QT_UTILITIES_EXPORT Updater : public QObject {
132 Q_OBJECT
133 Q_PROPERTY(bool inProgress READ isInProgress NOTIFY inProgressChanged)
134 Q_PROPERTY(QString overallStatus READ overallStatus NOTIFY inProgressChanged)
135 Q_PROPERTY(QString error READ error NOTIFY updateStatusChanged)
136 Q_PROPERTY(QString statusMessage READ statusMessage NOTIFY updateStatusChanged)
137 Q_PROPERTY(QString storedPath READ storedPath NOTIFY inProgressChanged)
138
139public:
140 struct Update {
141 std::string_view executableName;
142 std::string_view signatureName;
143 std::string_view data;
144 std::string_view signature;
145 };
146 using VerifyFunction = std::function<QString(const Update &)>;
147
148 explicit Updater(const QString &executableName, QObject *parent = nullptr);
149 explicit Updater(const QString &executableName, const QString &signatureExtension, QObject *parent = nullptr);
150 ~Updater() override;
151
152 bool isInProgress() const;
153 QString overallStatus() const;
154 const QString &error() const;
155 const QString &statusMessage() const;
156 const QString &storedPath() const;
157 void setNetworkAccessManager(QNetworkAccessManager *nm);
158 void setVerifier(VerifyFunction &&verifyFunction);
159#ifdef QT_UTILITIES_SETUP_TOOLS_ENABLED
160 void setCacheLoadControl(QNetworkRequest::CacheLoadControl cacheLoadControl);
161#endif
162
163public Q_SLOTS:
164 bool performUpdate(const QString &downloadUrl, const QString &signatureUrl);
165 void abortUpdate();
166
167Q_SIGNALS:
169 void updateFailed(const QString &error);
171 void updatePercentageChanged(qint64 bytesReceived, qint64 bytesTotal);
172 void updateStatusChanged(const QString &statusMessage);
173
174private Q_SLOTS:
175 void setError(const QString &error);
176 void startDownload(const QString &downloadUrl, const QString &signatureUrl);
177 void handleDownloadFinished();
178 void readSignature();
179 void storeExecutable();
180 void concludeUpdate();
181
182private:
183 std::unique_ptr<UpdaterPrivate> m_p;
184};
185
189class QT_UTILITIES_EXPORT UpdateHandler : public QObject {
190 Q_OBJECT
191 Q_PROPERTY(UpdateNotifier *notifier READ notifier CONSTANT)
192 Q_PROPERTY(Updater *updater READ updater CONSTANT)
193
194public:
199 CppUtilities::TimeSpan duration;
201 bool enabled = true;
202 };
203
204 explicit UpdateHandler(QSettings *settings, QNetworkAccessManager *nm, QObject *parent = nullptr);
205 explicit UpdateHandler(
206 const QString &executableName, const QString &signatureExtension, QSettings *settings, QNetworkAccessManager *nm, QObject *parent = nullptr);
207 ~UpdateHandler() override;
208
211 const CheckInterval &checkInterval() const;
214 void setConsideringSeparateSignature(bool consideringSeparateSignature);
215 QString preCheck() const;
216 static UpdateHandler *mainInstance();
218#ifdef QT_UTILITIES_SETUP_TOOLS_ENABLED
219 void setCacheLoadControl(QNetworkRequest::CacheLoadControl cacheLoadControl);
220#endif
221
222public Q_SLOTS:
223 void applySettings();
224 void performUpdate();
225 void saveNotifierState();
226
227private Q_SLOTS:
228 void handleUpdateCheckDone();
229
230private:
231#ifdef QT_UTILITIES_SETUP_TOOLS_ENABLED
232 void scheduleNextUpdateCheck();
233#endif
234
235 std::unique_ptr<UpdateHandlerPrivate> m_p;
236 static UpdateHandler *s_mainInstance;
237};
238
240{
241 return s_mainInstance;
242}
243
245{
246 s_mainInstance = mainInstance;
247}
248
253public:
254 explicit RestartHandler()
255 : m_restartRequested(false)
256 {
257 }
259 {
260 return m_restartRequested;
261 }
262 void requestRestart();
263 void reset()
264 {
265 m_restartRequested = false;
266 }
267 void respawnIfRestartRequested();
268 std::function<void()> requester()
269 {
270 return [this] { requestRestart(); };
271 }
272
273private:
274 std::atomic_bool m_restartRequested;
275};
276
277#ifdef QT_UTILITIES_GUI_QTWIDGETS
282public:
283explicit UpdateOptionPage(UpdateHandler *updateHandler, QWidget *parentWidget = nullptr);
284
285void setRestartHandler(std::function<void()> &&handler);
286
287private:
289void updateLatestVersion(bool inProgress = false);
290std::unique_ptr<UpdateOptionPagePrivate> m_p;
292
296class QT_UTILITIES_EXPORT VerificationErrorMessageBox : public QMessageBox {
297 Q_OBJECT
298
299public:
300 explicit VerificationErrorMessageBox();
301 ~VerificationErrorMessageBox() override;
302
303public Q_SLOTS:
304 int execForError(QString &errorMessage, const QString &explanation = QString());
305 void openForError(const QString &errorMessage, const QString &explanation = QString());
306};
307
311class QT_UTILITIES_EXPORT UpdateDialog : public SettingsDialog {
312 Q_OBJECT
313
314public:
315 explicit UpdateDialog(QWidget *parent = nullptr);
316 ~UpdateDialog() override;
317 UpdateOptionPage *page();
318 const UpdateOptionPage *page() const;
319
320private:
321 std::unique_ptr<UpdateDialogPrivate> m_p;
322};
323
324#endif
325
326} // namespace QtUtilities
327
328#endif // QT_UTILITIES_SETUP_UPDATER_H
329
330#if defined(QT_UTILITIES_GUI_QTWIDGETS)
332#endif
std::function< void()> requester()
Definition updater.h:268
The SettingsDialog class provides a framework for creating settings dialogs with different categories...
bool isConsideringSeparateSignature() const
Definition updater.cpp:1224
static UpdateHandler * mainInstance()
Definition updater.h:239
void setConsideringSeparateSignature(bool consideringSeparateSignature)
Definition updater.cpp:1229
static void setMainInstance(UpdateHandler *mainInstance)
Definition updater.h:244
UpdateHandler(QSettings *settings, QNetworkAccessManager *nm, QObject *parent=nullptr)
Handles checking for updates and performing an update of the application if available.
Definition updater.cpp:1163
const CheckInterval & checkInterval() const
Definition updater.cpp:1199
UpdateNotifier * notifier
Definition updater.h:191
QString preCheck() const
Definition updater.cpp:1234
void setCheckInterval(CheckInterval checkInterval)
Definition updater.cpp:1212
UpdateNotifier * notifier()
The UpdateNotifier class allows checking for new updates.
Definition updater.h:61
void setFlags(UpdateCheckFlags flags)
Definition updater.cpp:270
void setNetworkAccessManager(QNetworkAccessManager *nm)
Definition updater.cpp:442
UpdateNotifier(QObject *parent=nullptr)
Definition updater.cpp:189
void updateAvailable(const QString &version, const QString &additionalInfo)
void save(QSettings *settings)
Definition updater.cpp:402
bool isUpdateAvailable() const
Definition updater.cpp:252
void inProgressChanged(bool inProgress)
void supplyNewReleaseData(const QByteArray &data)
Definition updater.cpp:534
UpdateCheckFlags flags() const
Definition updater.cpp:261
const QString & latestVersion() const
Definition updater.cpp:299
void restore(QSettings *settings)
Definition updater.cpp:383
CppUtilities::DateTime lastCheck() const
Definition updater.cpp:374
The Updater class allows downloading and applying an update.
Definition updater.h:131
Updater(const QString &executableName, QObject *parent=nullptr)
Definition updater.cpp:750
void updatePercentageChanged(qint64 bytesReceived, qint64 bytesTotal)
void updateStatusChanged(const QString &statusMessage)
void updateFailed(const QString &error)
QString overallStatus
Definition updater.h:134
std::function< QString(const Update &)> VerifyFunction
Definition updater.h:146
QString statusMessage
Definition updater.h:136
bool performUpdate(const QString &downloadUrl, const QString &signatureUrl)
Definition updater.cpp:846
void setVerifier(VerifyFunction &&verifyFunction)
Definition updater.cpp:830
void inProgressChanged(bool inProgress)
void setNetworkAccessManager(QNetworkAccessManager *nm)
Definition updater.cpp:821
bool isInProgress() const
Definition updater.cpp:779
#define QT_UTILITIES_EXPORT
Marks the symbol to be exported by the qtutilities library.
Definition global.h:14
The CppUtilities namespace contains addons to the c++utilities library provided by the qtutilities li...
#define DECLARE_EXTERN_UI_FILE_BASED_OPTION_PAGE(SomeClass)
Declares external instantiation of class declared with BEGIN_DECLARE_UI_FILE_BASED_OPTION_PAGE in a c...
Definition optionpage.h:271
#define BEGIN_DECLARE_UI_FILE_BASED_OPTION_PAGE_CUSTOM_CTOR(SomeClass)
Declares a class inheriting from Dialogs::UiFileBasedOptionPage in a convenient way.
Definition optionpage.h:215
#define END_DECLARE_OPTION_PAGE
Must be used after BEGIN_DECLARE_OPTION_PAGE and BEGIN_DECLARE_UI_FILE_BASED_OPTION_PAGE.
Definition optionpage.h:241
#define DECLARE_SETUP_WIDGETS
Declares the method setupWidget() in a convenient way.
Definition optionpage.h:300
The CheckInterval struct specifies whether automatic checks for updates are enabled and of often they...
Definition updater.h:196
bool enabled
Whether automatic checks for updates are enabled at all.
Definition updater.h:201
CppUtilities::TimeSpan duration
The duration of the interval. Only durations up to around 24 days are supported. Only full-second acc...
Definition updater.h:199
std::string_view executableName
Definition updater.h:141
std::string_view signatureName
Definition updater.h:142
std::string_view data
Definition updater.h:143
std::string_view signature
Definition updater.h:144
CPP_UTILITIES_MARK_FLAG_ENUM_CLASS(QtUtilities, QtUtilities::UpdateCheckFlags)