Qt Utilities: setup/updater.h Source File

Qt Utilities 6.21.1 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
7#include "../settingsdialog/optionpage.h"
8#include "../settingsdialog/settingsdialog.h"
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;
38struct UpdateNotifierPrivate;
39struct UpdaterPrivate;
40struct UpdateHandlerPrivate;
41struct UpdateOptionPagePrivate;
42struct UpdateDialogPrivate;
44
45 enum class UpdateCheckFlags : quint64 {
46 None = 0,
47 IncludePreReleases = (1 << 0),
48 IncludeDrafts = (1 << 1),
49 Default = None,
50};
51
52} // namespace QtUtilities
53
54CPP_UTILITIES_MARK_FLAG_ENUM_CLASS(QtUtilities, QtUtilities::UpdateCheckFlags);
55
56namespace QtUtilities {
57
61 class 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)
73 Q_PROPERTY(QUrl previousVersionDownloadUrl READ previousVersionDownloadUrl)
74 Q_PROPERTY(QUrl previousVersionSignatureUrl READ previousVersionSignatureUrl)
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;
84 void setFlags(UpdateCheckFlags flags);
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:
111 void inProgressChanged(bool inProgress);
112 void checkedForUpdate();
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
131 class 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:
168 void inProgressChanged(bool inProgress);
169 void updateFailed(const QString &error);
170 void updateStored();
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
189 class 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:
196 struct CheckInterval {
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
209 UpdateNotifier *notifier();
210 Updater *updater();
211 const CheckInterval &checkInterval() const;
212 void setCheckInterval(CheckInterval checkInterval);
213 bool isConsideringSeparateSignature() const;
214 void setConsideringSeparateSignature(bool consideringSeparateSignature);
215 QString preCheck() const;
216 static UpdateHandler *mainInstance();
217 static void setMainInstance(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
239 inline UpdateHandler *UpdateHandler::mainInstance()
240{
241 return s_mainInstance;
242}
243
244 inline void UpdateHandler::setMainInstance(UpdateHandler *mainInstance)
245{
246 s_mainInstance = mainInstance;
247}
248
252 class QT_UTILITIES_EXPORT RestartHandler {
253public:
254 explicit RestartHandler()
255 : m_restartRequested(false)
256 {
257 }
258 bool isRestartRequested()
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
281 BEGIN_DECLARE_UI_FILE_BASED_OPTION_PAGE_CUSTOM_CTOR(UpdateOptionPage)
282public:
283explicit UpdateOptionPage(UpdateHandler *updateHandler, QWidget *parentWidget = nullptr);
284
285void setRestartHandler(std::function<void()> &&handler);
286
287private:
288 DECLARE_SETUP_WIDGETS
289void updateLatestVersion(bool inProgress = false);
290std::unique_ptr<UpdateOptionPagePrivate> m_p;
291 END_DECLARE_OPTION_PAGE
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)
331 DECLARE_EXTERN_UI_FILE_BASED_OPTION_PAGE(UpdateOptionPage)
332#endif
QtUtilities::RestartHandler::RestartHandler
RestartHandler()
Definition updater.h:254
QtUtilities::RestartHandler::requester
std::function< void()> requester()
Definition updater.h:268
QtUtilities::RestartHandler::requestRestart
void requestRestart()
Definition updater.cpp:1312
QtUtilities::RestartHandler::isRestartRequested
bool isRestartRequested()
Definition updater.h:258
QtUtilities::RestartHandler::reset
void reset()
Definition updater.h:263
QtUtilities::SettingsDialog
The SettingsDialog class provides a framework for creating settings dialogs with different categories...
Definition settingsdialog.h:21
QtUtilities::UpdateHandler::isConsideringSeparateSignature
bool isConsideringSeparateSignature() const
Definition updater.cpp:1227
QtUtilities::UpdateHandler::updater
Updater * updater()
QtUtilities::UpdateHandler::mainInstance
static UpdateHandler * mainInstance()
Definition updater.h:239
QtUtilities::UpdateHandler::performUpdate
void performUpdate()
Definition updater.cpp:1272
QtUtilities::UpdateHandler::setConsideringSeparateSignature
void setConsideringSeparateSignature(bool consideringSeparateSignature)
Definition updater.cpp:1232
QtUtilities::UpdateHandler::setMainInstance
static void setMainInstance(UpdateHandler *mainInstance)
Definition updater.h:244
QtUtilities::UpdateHandler::updater
Updater * updater
Definition updater.h:192
QtUtilities::UpdateHandler::UpdateHandler
UpdateHandler(QSettings *settings, QNetworkAccessManager *nm, QObject *parent=nullptr)
Handles checking for updates and performing an update of the application if available.
Definition updater.cpp:1166
QtUtilities::UpdateHandler::checkInterval
const CheckInterval & checkInterval() const
Definition updater.cpp:1202
QtUtilities::UpdateHandler::saveNotifierState
void saveNotifierState()
Definition updater.cpp:1279
QtUtilities::UpdateHandler::notifier
UpdateNotifier * notifier
Definition updater.h:191
QtUtilities::UpdateHandler::preCheck
QString preCheck() const
Definition updater.cpp:1237
QtUtilities::UpdateHandler::setCheckInterval
void setCheckInterval(CheckInterval checkInterval)
Definition updater.cpp:1215
QtUtilities::UpdateHandler::notifier
UpdateNotifier * notifier()
QtUtilities::UpdateHandler::applySettings
void applySettings()
Definition updater.cpp:1264
QtUtilities::UpdateNotifier
The UpdateNotifier class allows checking for new updates.
Definition updater.h:61
QtUtilities::UpdateNotifier::setFlags
void setFlags(UpdateCheckFlags flags)
Definition updater.cpp:270
QtUtilities::UpdateNotifier::executableName
QString executableName
Definition updater.h:66
QtUtilities::UpdateNotifier::setNetworkAccessManager
void setNetworkAccessManager(QNetworkAccessManager *nm)
Definition updater.cpp:442
QtUtilities::UpdateNotifier::UpdateNotifier
UpdateNotifier(QObject *parent=nullptr)
Definition updater.cpp:189
QtUtilities::UpdateNotifier::downloadUrl
QUrl downloadUrl
Definition updater.h:71
QtUtilities::UpdateNotifier::newVersion
QString newVersion
Definition updater.h:67
QtUtilities::UpdateNotifier::checkedForUpdate
void checkedForUpdate()
QtUtilities::UpdateNotifier::status
QString status() const
Definition updater.cpp:421
QtUtilities::UpdateNotifier::updateAvailable
void updateAvailable(const QString &version, const QString &additionalInfo)
QtUtilities::UpdateNotifier::inProgress
bool inProgress
Definition updater.h:64
QtUtilities::UpdateNotifier::save
void save(QSettings *settings)
Definition updater.cpp:402
QtUtilities::UpdateNotifier::isInProgress
bool isInProgress() const
Definition updater.cpp:243
QtUtilities::UpdateNotifier::previousVersionSignatureUrl
QUrl previousVersionSignatureUrl
Definition updater.h:74
QtUtilities::UpdateNotifier::isSupported
bool isSupported() const
Definition updater.cpp:234
QtUtilities::UpdateNotifier::isUpdateAvailable
bool isUpdateAvailable() const
Definition updater.cpp:252
QtUtilities::UpdateNotifier::inProgressChanged
void inProgressChanged(bool inProgress)
QtUtilities::UpdateNotifier::updateAvailable
bool updateAvailable
Definition updater.h:65
QtUtilities::UpdateNotifier::previousVersionDownloadUrl
QUrl previousVersionDownloadUrl
Definition updater.h:73
QtUtilities::UpdateNotifier::additionalInfo
QString additionalInfo
Definition updater.h:68
QtUtilities::UpdateNotifier::signatureUrl
QUrl signatureUrl
Definition updater.h:72
QtUtilities::UpdateNotifier::error
QString error
Definition updater.h:70
QtUtilities::UpdateNotifier::supplyNewReleaseData
void supplyNewReleaseData(const QByteArray &data)
Definition updater.cpp:534
QtUtilities::UpdateNotifier::releaseNotes
QString releaseNotes
Definition updater.h:69
QtUtilities::UpdateNotifier::checkForUpdate
void checkForUpdate()
Definition updater.cpp:486
QtUtilities::UpdateNotifier::flags
UpdateCheckFlags flags() const
Definition updater.cpp:261
QtUtilities::UpdateNotifier::latestVersion
const QString & latestVersion() const
Definition updater.cpp:299
QtUtilities::UpdateNotifier::restore
void restore(QSettings *settings)
Definition updater.cpp:383
QtUtilities::UpdateNotifier::lastCheck
CppUtilities::DateTime lastCheck() const
Definition updater.cpp:374
QtUtilities::UpdateNotifier::supported
bool supported
Definition updater.h:63
QtUtilities::UpdateNotifier::resetUpdateInfo
void resetUpdateInfo()
Definition updater.cpp:504
QtUtilities::Updater
The Updater class allows downloading and applying an update.
Definition updater.h:131
QtUtilities::Updater::abortUpdate
void abortUpdate()
Definition updater.cpp:862
QtUtilities::Updater::Updater
Updater(const QString &executableName, QObject *parent=nullptr)
Definition updater.cpp:750
QtUtilities::Updater::inProgress
bool inProgress
Definition updater.h:133
QtUtilities::Updater::updateStored
void updateStored()
QtUtilities::Updater::updatePercentageChanged
void updatePercentageChanged(qint64 bytesReceived, qint64 bytesTotal)
QtUtilities::Updater::error
QString error
Definition updater.h:135
QtUtilities::Updater::updateStatusChanged
void updateStatusChanged(const QString &statusMessage)
QtUtilities::Updater::updateFailed
void updateFailed(const QString &error)
QtUtilities::Updater::storedPath
QString storedPath
Definition updater.h:137
QtUtilities::Updater::overallStatus
QString overallStatus
Definition updater.h:134
QtUtilities::Updater::VerifyFunction
std::function< QString(const Update &)> VerifyFunction
Definition updater.h:146
QtUtilities::Updater::statusMessage
QString statusMessage
Definition updater.h:136
QtUtilities::Updater::performUpdate
bool performUpdate(const QString &downloadUrl, const QString &signatureUrl)
Definition updater.cpp:846
QtUtilities::Updater::setVerifier
void setVerifier(VerifyFunction &&verifyFunction)
Definition updater.cpp:830
QtUtilities::Updater::inProgressChanged
void inProgressChanged(bool inProgress)
QtUtilities::Updater::setNetworkAccessManager
void setNetworkAccessManager(QNetworkAccessManager *nm)
Definition updater.cpp:821
QtUtilities::Updater::isInProgress
bool isInProgress() const
Definition updater.cpp:779
global.h
QT_UTILITIES_EXPORT
#define QT_UTILITIES_EXPORT
Marks the symbol to be exported by the qtutilities library.
Definition global.h:14
CppUtilities
The CppUtilities namespace contains addons to the c++utilities library provided by the qtutilities li...
Definition qtconfigarguments.h:38
QtUtilities
!
Definition trylocker.h:8
QtUtilities::UpdateCheckFlags
UpdateCheckFlags
Definition updater.h:45
QtUtilities::UpdateCheckFlags::IncludeDrafts
@ IncludeDrafts
Definition updater.h:48
QtUtilities::UpdateCheckFlags::None
@ None
Definition updater.h:46
QtUtilities::UpdateCheckFlags::Default
@ Default
Definition updater.h:49
QtUtilities::UpdateCheckFlags::IncludePreReleases
@ IncludePreReleases
Definition updater.h:47
optionpage.h
DECLARE_EXTERN_UI_FILE_BASED_OPTION_PAGE
#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
BEGIN_DECLARE_UI_FILE_BASED_OPTION_PAGE_CUSTOM_CTOR
#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
END_DECLARE_OPTION_PAGE
#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
DECLARE_SETUP_WIDGETS
#define DECLARE_SETUP_WIDGETS
Declares the method setupWidget() in a convenient way.
Definition optionpage.h:300
settingsdialog.h
QtUtilities::UpdateHandlerPrivate
Definition updater.cpp:1146
QtUtilities::UpdateHandler::CheckInterval
The CheckInterval struct specifies whether automatic checks for updates are enabled and of often they...
Definition updater.h:196
QtUtilities::UpdateHandler::CheckInterval::enabled
bool enabled
Whether automatic checks for updates are enabled at all.
Definition updater.h:201
QtUtilities::UpdateHandler::CheckInterval::duration
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
QtUtilities::UpdateNotifierPrivate
Definition updater.cpp:184
QtUtilities::UpdaterPrivate
Definition updater.cpp:745
QtUtilities::Updater::Update
Definition updater.h:140
QtUtilities::Updater::Update::executableName
std::string_view executableName
Definition updater.h:141
QtUtilities::Updater::Update::signatureName
std::string_view signatureName
Definition updater.h:142
QtUtilities::Updater::Update::data
std::string_view data
Definition updater.h:143
QtUtilities::Updater::Update::signature
std::string_view signature
Definition updater.h:144
CPP_UTILITIES_MARK_FLAG_ENUM_CLASS
CPP_UTILITIES_MARK_FLAG_ENUM_CLASS(QtUtilities, QtUtilities::UpdateCheckFlags)