Qt Utilities: setup/updater.h Source File

Qt Utilities 6.19.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)
32
33namespace QtUtilities {
34
36struct UpdateNotifierPrivate;
37struct UpdaterPrivate;
38struct UpdateHandlerPrivate;
39struct UpdateOptionPagePrivate;
40struct UpdateDialogPrivate;
42
43 enum class UpdateCheckFlags : quint64 {
44 None = 0,
45 IncludePreReleases = (1 << 0),
46 IncludeDrafts = (1 << 1),
47 Default = None,
48};
49
50} // namespace QtUtilities
51
52CPP_UTILITIES_MARK_FLAG_ENUM_CLASS(QtUtilities, QtUtilities::UpdateCheckFlags);
53
54namespace QtUtilities {
55
59 class QT_UTILITIES_EXPORT UpdateNotifier : public QObject {
60 Q_OBJECT
61 Q_PROPERTY(bool supported READ isSupported)
62 Q_PROPERTY(bool inProgress READ isInProgress NOTIFY inProgressChanged)
63 Q_PROPERTY(bool updateAvailable READ isUpdateAvailable)
64 Q_PROPERTY(QString executableName READ executableName CONSTANT)
65 Q_PROPERTY(QString newVersion READ newVersion)
66 Q_PROPERTY(QString additionalInfo READ additionalInfo)
67 Q_PROPERTY(QString releaseNotes READ releaseNotes)
68 Q_PROPERTY(QString error READ error)
69 Q_PROPERTY(QUrl downloadUrl READ downloadUrl)
70 Q_PROPERTY(QUrl signatureUrl READ signatureUrl)
71 Q_PROPERTY(QUrl previousVersionDownloadUrl READ previousVersionDownloadUrl)
72 Q_PROPERTY(QUrl previousVersionSignatureUrl READ previousVersionSignatureUrl)
73
74public:
75 explicit UpdateNotifier(QObject *parent = nullptr);
76 ~UpdateNotifier() override;
77
78 bool isSupported() const;
79 bool isInProgress() const;
80 bool isUpdateAvailable() const;
81 UpdateCheckFlags flags() const;
82 void setFlags(UpdateCheckFlags flags);
83 const QString &executableName() const;
84 const QString &newVersion() const;
85 const QString &latestVersion() const;
86 const QString &additionalInfo() const;
87 const QString &releaseNotes() const;
88 const QString &error() const;
89 const QUrl &downloadUrl() const;
90 const QUrl &signatureUrl() const;
91 const QUrl &previousVersionDownloadUrl() const;
92 const QUrl &previousVersionSignatureUrl() const;
93 QString status() const;
94 CppUtilities::DateTime lastCheck() const;
95 void restore(QSettings *settings);
96 void save(QSettings *settings);
97 void setNetworkAccessManager(QNetworkAccessManager *nm);
98#ifdef QT_UTILITIES_SETUP_TOOLS_ENABLED
99 void setCacheLoadControl(QNetworkRequest::CacheLoadControl cacheLoadControl);
100#endif
101
102public Q_SLOTS:
103 void checkForUpdate();
104 void resetUpdateInfo();
105 void supplyNewReleaseData(const QByteArray &data);
106
107Q_SIGNALS:
108 void inProgressChanged(bool inProgress);
109 void checkedForUpdate();
110 void updateAvailable(const QString &version, const QString &additionalInfo);
111
112private Q_SLOTS:
113 void lastCheckNow() const;
114 void setError(const QString &context, QNetworkReply *reply);
115 void setError(const QString &context, const QJsonParseError &jsonError, const QByteArray &response);
116 void readReleases();
117 void queryRelease(const QUrl &releaseUrl, bool forUpdate, bool forPreviousVersion);
118 void readRelease();
119 void processAssets(const QJsonArray &assets, bool forUpdate, bool forPreviousVersion);
120
121private:
122 std::unique_ptr<UpdateNotifierPrivate> m_p;
123};
124
128 class QT_UTILITIES_EXPORT Updater : public QObject {
129 Q_OBJECT
130 Q_PROPERTY(bool inProgress READ isInProgress NOTIFY inProgressChanged)
131 Q_PROPERTY(QString overallStatus READ overallStatus NOTIFY inProgressChanged)
132 Q_PROPERTY(QString error READ error NOTIFY updateStatusChanged)
133 Q_PROPERTY(QString statusMessage READ statusMessage NOTIFY updateStatusChanged)
134 Q_PROPERTY(QString storedPath READ storedPath NOTIFY inProgressChanged)
135
136public:
137 struct Update {
138 std::string_view executableName;
139 std::string_view signatureName;
140 std::string_view data;
141 std::string_view signature;
142 };
143 using VerifyFunction = std::function<QString(const Update &)>;
144
145 explicit Updater(const QString &executableName, QObject *parent = nullptr);
146 explicit Updater(const QString &executableName, const QString &signatureExtension, QObject *parent = nullptr);
147 ~Updater() override;
148
149 bool isInProgress() const;
150 QString overallStatus() const;
151 const QString &error() const;
152 const QString &statusMessage() const;
153 const QString &storedPath() const;
154 void setNetworkAccessManager(QNetworkAccessManager *nm);
155 void setVerifier(VerifyFunction &&verifyFunction);
156#ifdef QT_UTILITIES_SETUP_TOOLS_ENABLED
157 void setCacheLoadControl(QNetworkRequest::CacheLoadControl cacheLoadControl);
158#endif
159
160public Q_SLOTS:
161 bool performUpdate(const QString &downloadUrl, const QString &signatureUrl);
162 void abortUpdate();
163
164Q_SIGNALS:
165 void inProgressChanged(bool inProgress);
166 void updateFailed(const QString &error);
167 void updateStored();
168 void updatePercentageChanged(qint64 bytesReceived, qint64 bytesTotal);
169 void updateStatusChanged(const QString &statusMessage);
170
171private Q_SLOTS:
172 void setError(const QString &error);
173 void startDownload(const QString &downloadUrl, const QString &signatureUrl);
174 void handleDownloadFinished();
175 void readSignature();
176 void storeExecutable();
177 void concludeUpdate();
178
179private:
180 std::unique_ptr<UpdaterPrivate> m_p;
181};
182
186 class QT_UTILITIES_EXPORT UpdateHandler : public QObject {
187 Q_OBJECT
188 Q_PROPERTY(UpdateNotifier *notifier READ notifier CONSTANT)
189 Q_PROPERTY(Updater *updater READ updater CONSTANT)
190
191public:
193 struct CheckInterval {
196 CppUtilities::TimeSpan duration;
198 bool enabled = true;
199 };
200
201 explicit UpdateHandler(QSettings *settings, QNetworkAccessManager *nm, QObject *parent = nullptr);
202 explicit UpdateHandler(
203 const QString &executableName, const QString &signatureExtension, QSettings *settings, QNetworkAccessManager *nm, QObject *parent = nullptr);
204 ~UpdateHandler() override;
205
206 UpdateNotifier *notifier();
207 Updater *updater();
208 const CheckInterval &checkInterval() const;
209 void setCheckInterval(CheckInterval checkInterval);
210 bool isConsideringSeparateSignature() const;
211 void setConsideringSeparateSignature(bool consideringSeparateSignature);
212 QString preCheck() const;
213 static UpdateHandler *mainInstance();
214 static void setMainInstance(UpdateHandler *mainInstance);
215#ifdef QT_UTILITIES_SETUP_TOOLS_ENABLED
216 void setCacheLoadControl(QNetworkRequest::CacheLoadControl cacheLoadControl);
217#endif
218
219public Q_SLOTS:
220 void applySettings();
221 void performUpdate();
222 void saveNotifierState();
223
224private Q_SLOTS:
225 void handleUpdateCheckDone();
226
227private:
228#ifdef QT_UTILITIES_SETUP_TOOLS_ENABLED
229 void scheduleNextUpdateCheck();
230#endif
231
232 std::unique_ptr<UpdateHandlerPrivate> m_p;
233 static UpdateHandler *s_mainInstance;
234};
235
236 inline UpdateHandler *UpdateHandler::mainInstance()
237{
238 return s_mainInstance;
239}
240
241 inline void UpdateHandler::setMainInstance(UpdateHandler *mainInstance)
242{
243 s_mainInstance = mainInstance;
244}
245
249 class QT_UTILITIES_EXPORT RestartHandler {
250public:
251 explicit RestartHandler()
252 : m_restartRequested(false)
253 {
254 }
255 bool isRestartRequested()
256 {
257 return m_restartRequested;
258 }
259 void requestRestart();
260 void reset()
261 {
262 m_restartRequested = false;
263 }
264 void respawnIfRestartRequested();
265 std::function<void()> requester()
266 {
267 return [this] { requestRestart(); };
268 }
269
270private:
271 std::atomic_bool m_restartRequested;
272};
273
274#ifdef QT_UTILITIES_GUI_QTWIDGETS
278 BEGIN_DECLARE_UI_FILE_BASED_OPTION_PAGE_CUSTOM_CTOR(UpdateOptionPage)
279public:
280explicit UpdateOptionPage(UpdateHandler *updateHandler, QWidget *parentWidget = nullptr);
281
282void setRestartHandler(std::function<void()> &&handler);
283
284private:
285 DECLARE_SETUP_WIDGETS
286void updateLatestVersion(bool inProgress = false);
287std::unique_ptr<UpdateOptionPagePrivate> m_p;
288 END_DECLARE_OPTION_PAGE
289
293class QT_UTILITIES_EXPORT VerificationErrorMessageBox : public QMessageBox {
294 Q_OBJECT
295
296public:
297 explicit VerificationErrorMessageBox();
298 ~VerificationErrorMessageBox() override;
299
300public Q_SLOTS:
301 int execForError(QString &errorMessage, const QString &explanation = QString());
302 void openForError(const QString &errorMessage, const QString &explanation = QString());
303};
304
308class QT_UTILITIES_EXPORT UpdateDialog : public SettingsDialog {
309 Q_OBJECT
310
311public:
312 explicit UpdateDialog(QWidget *parent = nullptr);
313 ~UpdateDialog() override;
314 UpdateOptionPage *page();
315 const UpdateOptionPage *page() const;
316
317private:
318 std::unique_ptr<UpdateDialogPrivate> m_p;
319};
320
321#endif
322
323} // namespace QtUtilities
324
325#endif // QT_UTILITIES_SETUP_UPDATER_H
326
327#if defined(QT_UTILITIES_GUI_QTWIDGETS)
328 DECLARE_EXTERN_UI_FILE_BASED_OPTION_PAGE(UpdateOptionPage)
329#endif
QtUtilities::RestartHandler::RestartHandler
RestartHandler()
Definition updater.h:251
QtUtilities::RestartHandler::requester
std::function< void()> requester()
Definition updater.h:265
QtUtilities::RestartHandler::requestRestart
void requestRestart()
Definition updater.cpp:1286
QtUtilities::RestartHandler::isRestartRequested
bool isRestartRequested()
Definition updater.h:255
QtUtilities::RestartHandler::reset
void reset()
Definition updater.h:260
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:1201
QtUtilities::UpdateHandler::updater
Updater * updater()
QtUtilities::UpdateHandler::mainInstance
static UpdateHandler * mainInstance()
Definition updater.h:236
QtUtilities::UpdateHandler::performUpdate
void performUpdate()
Definition updater.cpp:1246
QtUtilities::UpdateHandler::setConsideringSeparateSignature
void setConsideringSeparateSignature(bool consideringSeparateSignature)
Definition updater.cpp:1206
QtUtilities::UpdateHandler::setMainInstance
static void setMainInstance(UpdateHandler *mainInstance)
Definition updater.h:241
QtUtilities::UpdateHandler::updater
Updater * updater
Definition updater.h:189
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:1140
QtUtilities::UpdateHandler::checkInterval
const CheckInterval & checkInterval() const
Definition updater.cpp:1176
QtUtilities::UpdateHandler::saveNotifierState
void saveNotifierState()
Definition updater.cpp:1253
QtUtilities::UpdateHandler::notifier
UpdateNotifier * notifier
Definition updater.h:188
QtUtilities::UpdateHandler::preCheck
QString preCheck() const
Definition updater.cpp:1211
QtUtilities::UpdateHandler::setCheckInterval
void setCheckInterval(CheckInterval checkInterval)
Definition updater.cpp:1189
QtUtilities::UpdateHandler::notifier
UpdateNotifier * notifier()
QtUtilities::UpdateHandler::applySettings
void applySettings()
Definition updater.cpp:1238
QtUtilities::UpdateNotifier
The UpdateNotifier class allows checking for new updates.
Definition updater.h:59
QtUtilities::UpdateNotifier::setFlags
void setFlags(UpdateCheckFlags flags)
Definition updater.cpp:231
QtUtilities::UpdateNotifier::executableName
QString executableName
Definition updater.h:64
QtUtilities::UpdateNotifier::setNetworkAccessManager
void setNetworkAccessManager(QNetworkAccessManager *nm)
Definition updater.cpp:403
QtUtilities::UpdateNotifier::UpdateNotifier
UpdateNotifier(QObject *parent=nullptr)
Definition updater.cpp:147
QtUtilities::UpdateNotifier::downloadUrl
QUrl downloadUrl
Definition updater.h:69
QtUtilities::UpdateNotifier::newVersion
QString newVersion
Definition updater.h:65
QtUtilities::UpdateNotifier::checkedForUpdate
void checkedForUpdate()
QtUtilities::UpdateNotifier::status
QString status() const
Definition updater.cpp:382
QtUtilities::UpdateNotifier::updateAvailable
void updateAvailable(const QString &version, const QString &additionalInfo)
QtUtilities::UpdateNotifier::inProgress
bool inProgress
Definition updater.h:62
QtUtilities::UpdateNotifier::save
void save(QSettings *settings)
Definition updater.cpp:363
QtUtilities::UpdateNotifier::isInProgress
bool isInProgress() const
Definition updater.cpp:204
QtUtilities::UpdateNotifier::previousVersionSignatureUrl
QUrl previousVersionSignatureUrl
Definition updater.h:72
QtUtilities::UpdateNotifier::isSupported
bool isSupported() const
Definition updater.cpp:195
QtUtilities::UpdateNotifier::isUpdateAvailable
bool isUpdateAvailable() const
Definition updater.cpp:213
QtUtilities::UpdateNotifier::inProgressChanged
void inProgressChanged(bool inProgress)
QtUtilities::UpdateNotifier::updateAvailable
bool updateAvailable
Definition updater.h:63
QtUtilities::UpdateNotifier::previousVersionDownloadUrl
QUrl previousVersionDownloadUrl
Definition updater.h:71
QtUtilities::UpdateNotifier::additionalInfo
QString additionalInfo
Definition updater.h:66
QtUtilities::UpdateNotifier::signatureUrl
QUrl signatureUrl
Definition updater.h:70
QtUtilities::UpdateNotifier::error
QString error
Definition updater.h:68
QtUtilities::UpdateNotifier::supplyNewReleaseData
void supplyNewReleaseData(const QByteArray &data)
Definition updater.cpp:506
QtUtilities::UpdateNotifier::releaseNotes
QString releaseNotes
Definition updater.h:67
QtUtilities::UpdateNotifier::checkForUpdate
void checkForUpdate()
Definition updater.cpp:447
QtUtilities::UpdateNotifier::flags
UpdateCheckFlags flags() const
Definition updater.cpp:222
QtUtilities::UpdateNotifier::latestVersion
const QString & latestVersion() const
Definition updater.cpp:260
QtUtilities::UpdateNotifier::restore
void restore(QSettings *settings)
Definition updater.cpp:344
QtUtilities::UpdateNotifier::lastCheck
CppUtilities::DateTime lastCheck() const
Definition updater.cpp:335
QtUtilities::UpdateNotifier::supported
bool supported
Definition updater.h:61
QtUtilities::UpdateNotifier::resetUpdateInfo
void resetUpdateInfo()
Definition updater.cpp:465
QtUtilities::Updater
The Updater class allows downloading and applying an update.
Definition updater.h:128
QtUtilities::Updater::abortUpdate
void abortUpdate()
Definition updater.cpp:839
QtUtilities::Updater::Updater
Updater(const QString &executableName, QObject *parent=nullptr)
Definition updater.cpp:727
QtUtilities::Updater::inProgress
bool inProgress
Definition updater.h:130
QtUtilities::Updater::updateStored
void updateStored()
QtUtilities::Updater::updatePercentageChanged
void updatePercentageChanged(qint64 bytesReceived, qint64 bytesTotal)
QtUtilities::Updater::error
QString error
Definition updater.h:132
QtUtilities::Updater::updateStatusChanged
void updateStatusChanged(const QString &statusMessage)
QtUtilities::Updater::updateFailed
void updateFailed(const QString &error)
QtUtilities::Updater::storedPath
QString storedPath
Definition updater.h:134
QtUtilities::Updater::overallStatus
QString overallStatus
Definition updater.h:131
QtUtilities::Updater::VerifyFunction
std::function< QString(const Update &)> VerifyFunction
Definition updater.h:143
QtUtilities::Updater::statusMessage
QString statusMessage
Definition updater.h:133
QtUtilities::Updater::performUpdate
bool performUpdate(const QString &downloadUrl, const QString &signatureUrl)
Definition updater.cpp:823
QtUtilities::Updater::setVerifier
void setVerifier(VerifyFunction &&verifyFunction)
Definition updater.cpp:807
QtUtilities::Updater::inProgressChanged
void inProgressChanged(bool inProgress)
QtUtilities::Updater::setNetworkAccessManager
void setNetworkAccessManager(QNetworkAccessManager *nm)
Definition updater.cpp:798
QtUtilities::Updater::isInProgress
bool isInProgress() const
Definition updater.cpp:756
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:43
QtUtilities::UpdateCheckFlags::IncludeDrafts
@ IncludeDrafts
Definition updater.h:46
QtUtilities::UpdateCheckFlags::None
@ None
Definition updater.h:44
QtUtilities::UpdateCheckFlags::Default
@ Default
Definition updater.h:47
QtUtilities::UpdateCheckFlags::IncludePreReleases
@ IncludePreReleases
Definition updater.h:45
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:1120
QtUtilities::UpdateHandler::CheckInterval
The CheckInterval struct specifies whether automatic checks for updates are enabled and of often they...
Definition updater.h:193
QtUtilities::UpdateHandler::CheckInterval::enabled
bool enabled
Whether automatic checks for updates are enabled at all.
Definition updater.h:198
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:196
QtUtilities::UpdateNotifierPrivate
Definition updater.cpp:142
QtUtilities::UpdaterPrivate
Definition updater.cpp:722
QtUtilities::Updater::Update
Definition updater.h:137
QtUtilities::Updater::Update::executableName
std::string_view executableName
Definition updater.h:138
QtUtilities::Updater::Update::signatureName
std::string_view signatureName
Definition updater.h:139
QtUtilities::Updater::Update::data
std::string_view data
Definition updater.h:140
QtUtilities::Updater::Update::signature
std::string_view signature
Definition updater.h:141
CPP_UTILITIES_MARK_FLAG_ENUM_CLASS
CPP_UTILITIES_MARK_FLAG_ENUM_CLASS(QtUtilities, QtUtilities::UpdateCheckFlags)