Debian Codegraph @

setup/updater.h

raw symbols SCIP
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
27 QT_FORWARD_DECLARE_CLASS(QJsonParseError)
28 QT_FORWARD_DECLARE_CLASS(QJsonArray)
29 QT_FORWARD_DECLARE_CLASS(QNetworkAccessManager)
30 QT_FORWARD_DECLARE_CLASS(QNetworkReply)
31 QT_FORWARD_DECLARE_CLASS(QSettings)
32 QT_FORWARD_DECLARE_CLASS(QVersionNumber)
33
34 namespace QtUtilities {
35
36 /// \cond
37 struct VersionAndSuffix;
38 struct UpdateNotifierPrivate;
39 struct UpdaterPrivate;
40 struct UpdateHandlerPrivate;
41 struct UpdateOptionPagePrivate;
42 struct UpdateDialogPrivate;
43 /// \endcond
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
54 CPP_UTILITIES_MARK_FLAG_ENUM_CLASS(QtUtilities, QtUtilities::UpdateCheckFlags);
55
56 namespace QtUtilities {
57
58 /// \brief The UpdateNotifier class allows checking for new updates.
59 /// \remarks This class is experimental and might be changed in incompatible ways (API and ABI wise) or completely removed
60 /// in further minor/patch releases.
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
76 public:
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
105 public Q_SLOTS:
106 void checkForUpdate();
107 void resetUpdateInfo();
108 void supplyNewReleaseData(const QByteArray &data);
109
110 Q_SIGNALS:
111 void inProgressChanged(bool inProgress);
112 void checkedForUpdate();
113 void updateAvailable(const QString &version, const QString &additionalInfo);
114
115 private 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
124 private:
125 std::unique_ptr<UpdateNotifierPrivate> m_p;
126 };
127
128 /// \brief The Updater class allows downloading and applying an update.
129 /// \remarks This class is experimental and might be changed in incompatible ways (API and ABI wise) or completely removed
130 /// in further minor/patch releases.
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
139 public:
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
163 public Q_SLOTS:
164 bool performUpdate(const QString &downloadUrl, const QString &signatureUrl);
165 void abortUpdate();
166
167 Q_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
174 private 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
182 private:
183 std::unique_ptr<UpdaterPrivate> m_p;
184 };
185
186 /// \brief The UpdateHandler class manages the non-graphical aspects of checking for new updates and performing them.
187 /// \remarks This class is experimental and might be changed in incompatible ways (API and ABI wise) or completely removed
188 /// in further minor/patch releases.
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
194 public:
195 /// \brief The CheckInterval struct specifies whether automatic checks for updates are enabled and of often they should be done.
196 struct CheckInterval {
197 /// \brief The duration of the interval. Only durations up to around 24 days are supported. Only full-second accuracy is supported.
198 /// \remarks A value of zero indicates that the check is supposed to be done only once every time the application launches (i.e. shortly after startup).
199 CppUtilities::TimeSpan duration;
200 /// \brief Whether automatic checks for updates are enabled at all.
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
222 public Q_SLOTS:
223 void applySettings();
224 void performUpdate();
225 void saveNotifierState();
226
227 private Q_SLOTS:
228 void handleUpdateCheckDone();
229
230 private:
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
249 /// \brief The RestartHandler class allows quitting and respawning the application if a restart is requested.
250 /// \remarks This class is experimental and might be changed in incompatible ways (API and ABI wise) or completely removed
251 /// in further minor/patch releases.
252 class QT_UTILITIES_EXPORT RestartHandler {
253 public:
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
273 private:
274 std::atomic_bool m_restartRequested;
275 };
276
277 #ifdef QT_UTILITIES_GUI_QTWIDGETS
278 /// \brief The UpdateOptionPage class provides a settings page to manage automatic updates for applications using Qt Widgets.
279 /// \remarks This class is experimental and might be changed in incompatible ways (API and ABI wise) or completely removed
280 /// in further minor/patch releases.
281 BEGIN_DECLARE_UI_FILE_BASED_OPTION_PAGE_CUSTOM_CTOR(UpdateOptionPage)
282 public:
283 explicit UpdateOptionPage(UpdateHandler *updateHandler, QWidget *parentWidget = nullptr);
284
285 void setRestartHandler(std::function<void()> &&handler);
286
287 private:
288 DECLARE_SETUP_WIDGETS
289 void updateLatestVersion(bool inProgress = false);
290 std::unique_ptr<UpdateOptionPagePrivate> m_p;
291 END_DECLARE_OPTION_PAGE
292
293 /// \brief The VerificationErrorMessageBox class provides message box for showing signature validation errors during updates.
294 /// \remarks This class is experimental and might be changed in incompatible ways (API and ABI wise) or completely removed
295 /// in further minor/patch releases.
296 class QT_UTILITIES_EXPORT VerificationErrorMessageBox : public QMessageBox {
297 Q_OBJECT
298
299 public:
300 explicit VerificationErrorMessageBox();
301 ~VerificationErrorMessageBox() override;
302
303 public Q_SLOTS:
304 int execForError(QString &errorMessage, const QString &explanation = QString());
305 void openForError(const QString &errorMessage, const QString &explanation = QString());
306 };
307
308 /// \brief The UpdateDialog class provides a settings dialog to manage automatic updates for applications using Qt Widgets.
309 /// \remarks This class is experimental and might be changed in incompatible ways (API and ABI wise) or completely removed
310 /// in further minor/patch releases.
311 class QT_UTILITIES_EXPORT UpdateDialog : public SettingsDialog {
312 Q_OBJECT
313
314 public:
315 explicit UpdateDialog(QWidget *parent = nullptr);
316 ~UpdateDialog() override;
317 UpdateOptionPage *page();
318 const UpdateOptionPage *page() const;
319
320 private:
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