Qt Utilities 6.16.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
8#endif
9
10#include <c++utilities/chrono/datetime.h>
11
12#include <QObject>
13#include <QUrl>
14
15#ifdef QT_UTILITIES_SETUP_TOOLS_ENABLED
16#include <QNetworkRequest>
17#endif
18
19#include <memory>
20
21QT_FORWARD_DECLARE_CLASS(QJsonParseError)
22QT_FORWARD_DECLARE_CLASS(QJsonArray)
23QT_FORWARD_DECLARE_CLASS(QNetworkAccessManager)
24QT_FORWARD_DECLARE_CLASS(QNetworkReply)
25QT_FORWARD_DECLARE_CLASS(QSettings)
26
27namespace QtUtilities {
28
31struct UpdaterPrivate;
33struct UpdateOptionPagePrivate;
35
39class QT_UTILITIES_EXPORT UpdateNotifier : public QObject {
40 Q_OBJECT
41 Q_PROPERTY(bool supported READ isSupported)
42 Q_PROPERTY(bool inProgress READ isInProgress NOTIFY inProgressChanged)
43 Q_PROPERTY(bool updateAvailable READ isUpdateAvailable)
44 Q_PROPERTY(QString executableName READ executableName CONSTANT)
45 Q_PROPERTY(QString newVersion READ newVersion)
46 Q_PROPERTY(QString additionalInfo READ additionalInfo)
47 Q_PROPERTY(QString error READ error)
48 Q_PROPERTY(QUrl downloadUrl READ downloadUrl)
49 Q_PROPERTY(QUrl signatureUrl READ signatureUrl)
50
51public:
52 explicit UpdateNotifier(QObject *parent = nullptr);
53 ~UpdateNotifier() override;
54
55 bool isSupported() const;
56 bool isInProgress() const;
57 bool isUpdateAvailable() const;
58 const QString &executableName() const;
59 const QString &newVersion() const;
60 const QString &latestVersion() const;
61 const QString &additionalInfo() const;
62 const QString &error() const;
63 const QUrl &downloadUrl() const;
64 const QUrl &signatureUrl() const;
65 QString status() const;
66 CppUtilities::DateTime lastCheck() const;
67 void restore(QSettings *settings);
68 void save(QSettings *settings);
69 void setNetworkAccessManager(QNetworkAccessManager *nm);
70#ifdef QT_UTILITIES_SETUP_TOOLS_ENABLED
71 void setCacheLoadControl(QNetworkRequest::CacheLoadControl cacheLoadControl);
72#endif
73
74public Q_SLOTS:
75 void checkForUpdate();
76 void resetUpdateInfo();
77
78Q_SIGNALS:
81 void updateAvailable(const QString &version, const QString &additionalInfo);
82
83private Q_SLOTS:
84 void lastCheckNow() const;
85 void setError(const QString &context, QNetworkReply *reply);
86 void setError(const QString &context, const QJsonParseError &jsonError, const QByteArray &response, QNetworkReply *reply);
87 void readReleases();
88 void queryRelease(const QUrl &releaseUrl);
89 void readRelease();
90 void processAssets(const QJsonArray &assets, bool forUpdate);
91
92private:
93 std::unique_ptr<UpdateNotifierPrivate> m_p;
94};
95
99class QT_UTILITIES_EXPORT Updater : public QObject {
100 Q_OBJECT
101 Q_PROPERTY(bool inProgress READ isInProgress NOTIFY inProgressChanged)
102 Q_PROPERTY(QString overallStatus READ overallStatus NOTIFY inProgressChanged)
103 Q_PROPERTY(QString error READ error NOTIFY updateStatusChanged)
104 Q_PROPERTY(QString statusMessage READ statusMessage NOTIFY updateStatusChanged)
105 Q_PROPERTY(QString storedPath READ storedPath NOTIFY inProgressChanged)
106
107public:
108 struct Update {
109 std::string_view executableName;
110 std::string_view signatureName;
111 std::string_view data;
112 std::string_view signature;
113 };
114 using VerifyFunction = std::function<QString(const Update &)>;
115
116 explicit Updater(const QString &executableName, QObject *parent = nullptr);
117 explicit Updater(const QString &executableName, const QString &signatureExtension, QObject *parent = nullptr);
118 ~Updater() override;
119
120 bool isInProgress() const;
121 QString overallStatus() const;
122 const QString &error() const;
123 const QString &statusMessage() const;
124 const QString &storedPath() const;
125 void setNetworkAccessManager(QNetworkAccessManager *nm);
126 void setVerifier(VerifyFunction &&verifyFunction);
127#ifdef QT_UTILITIES_SETUP_TOOLS_ENABLED
128 void setCacheLoadControl(QNetworkRequest::CacheLoadControl cacheLoadControl);
129#endif
130
131public Q_SLOTS:
132 bool performUpdate(const QString &downloadUrl, const QString &signatureUrl);
133 void abortUpdate();
134
135Q_SIGNALS:
137 void updateFailed(const QString &error);
139 void updatePercentageChanged(qint64 bytesReceived, qint64 bytesTotal);
140 void updateStatusChanged(const QString &statusMessage);
141
142private Q_SLOTS:
143 void setError(const QString &error);
144 void startDownload(const QString &downloadUrl, const QString &signatureUrl);
145 void handleDownloadFinished();
146 void readSignature();
147 void storeExecutable();
148 void concludeUpdate();
149
150private:
151 std::unique_ptr<UpdaterPrivate> m_p;
152};
153
157class QT_UTILITIES_EXPORT UpdateHandler : public QObject {
158 Q_OBJECT
159 Q_PROPERTY(UpdateNotifier *notifier READ notifier CONSTANT)
160 Q_PROPERTY(Updater *updater READ updater CONSTANT)
161
162public:
167 CppUtilities::TimeSpan duration;
169 bool enabled = true;
170 };
171
172 explicit UpdateHandler(QSettings *settings, QNetworkAccessManager *nm, QObject *parent = nullptr);
173 explicit UpdateHandler(
174 const QString &executableName, const QString &signatureExtension, QSettings *settings, QNetworkAccessManager *nm, QObject *parent = nullptr);
175 ~UpdateHandler() override;
176
179 const CheckInterval &checkInterval() const;
182 void setConsideringSeparateSignature(bool consideringSeparateSignature);
183 static UpdateHandler *mainInstance();
185#ifdef QT_UTILITIES_SETUP_TOOLS_ENABLED
186 void setCacheLoadControl(QNetworkRequest::CacheLoadControl cacheLoadControl);
187#endif
188
189public Q_SLOTS:
190 void applySettings();
191 void performUpdate();
192
193private Q_SLOTS:
194 void handleUpdateCheckDone();
195
196private:
197#ifdef QT_UTILITIES_SETUP_TOOLS_ENABLED
198 void scheduleNextUpdateCheck();
199#endif
200
201 std::unique_ptr<UpdateHandlerPrivate> m_p;
202 static UpdateHandler *s_mainInstance;
203};
204
206{
207 return s_mainInstance;
208}
209
211{
212 s_mainInstance = mainInstance;
213}
214
215#ifdef QT_UTILITIES_GUI_QTWIDGETS
220public:
221explicit UpdateOptionPage(UpdateHandler *updateHandler, QWidget *parentWidget = nullptr);
222
223void setRestartHandler(std::function<void()> &&handler);
224
225private:
227void updateLatestVersion(bool inProgress = false);
228std::unique_ptr<UpdateOptionPagePrivate> m_p;
230
231#endif
232
233} // namespace QtUtilities
234
235#endif // QT_UTILITIES_SETUP_UPDATER_H
236
237#if defined(QT_UTILITIES_GUI_QTWIDGETS)
239#endif
The UpdateHandler class manages the non-graphical aspects of checking for new updates and performing ...
Definition updater.h:157
bool isConsideringSeparateSignature() const
Definition updater.cpp:1042
static UpdateHandler * mainInstance()
Definition updater.h:205
void setConsideringSeparateSignature(bool consideringSeparateSignature)
Definition updater.cpp:1047
static void setMainInstance(UpdateHandler *mainInstance)
Definition updater.h:210
UpdateHandler(QSettings *settings, QNetworkAccessManager *nm, QObject *parent=nullptr)
Handles checking for updates and performing an update of the application if available.
Definition updater.cpp:981
const CheckInterval & checkInterval() const
Definition updater.cpp:1017
UpdateNotifier * notifier
Definition updater.h:159
void setCheckInterval(CheckInterval checkInterval)
Definition updater.cpp:1030
UpdateNotifier * notifier()
The UpdateNotifier class allows checking for new updates.
Definition updater.h:39
void setNetworkAccessManager(QNetworkAccessManager *nm)
Definition updater.cpp:319
UpdateNotifier(QObject *parent=nullptr)
Definition updater.cpp:122
void updateAvailable(const QString &version, const QString &additionalInfo)
void save(QSettings *settings)
Definition updater.cpp:283
bool isUpdateAvailable() const
Definition updater.cpp:185
void inProgressChanged(bool inProgress)
const QString & latestVersion() const
Definition updater.cpp:214
void restore(QSettings *settings)
Definition updater.cpp:268
CppUtilities::DateTime lastCheck() const
Definition updater.cpp:259
The Updater class allows downloading and applying an update.
Definition updater.h:99
Updater(const QString &executableName, QObject *parent=nullptr)
Definition updater.cpp:573
void updatePercentageChanged(qint64 bytesReceived, qint64 bytesTotal)
void updateStatusChanged(const QString &statusMessage)
void updateFailed(const QString &error)
QString overallStatus
Definition updater.h:102
std::function< QString(const Update &)> VerifyFunction
Definition updater.h:114
QString statusMessage
Definition updater.h:104
bool performUpdate(const QString &downloadUrl, const QString &signatureUrl)
Definition updater.cpp:669
void setVerifier(VerifyFunction &&verifyFunction)
Definition updater.cpp:653
void inProgressChanged(bool inProgress)
void setNetworkAccessManager(QNetworkAccessManager *nm)
Definition updater.cpp:644
bool isInProgress() const
Definition updater.cpp:602
#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:164
bool enabled
Whether automatic checks for updates are enabled at all.
Definition updater.h:169
CppUtilities::TimeSpan duration
The duration of the interval. Only durations up to around 24 days are supported. Only full-second acc...
Definition updater.h:167
std::string_view executableName
Definition updater.h:109
std::string_view signatureName
Definition updater.h:110
std::string_view data
Definition updater.h:111
std::string_view signature
Definition updater.h:112