QtAutoUpdater  3.0.0
A Qt library to automatically check for updates and install them
updater.h
1 #ifndef QTAUTOUPDATER_UPDATER_H
2 #define QTAUTOUPDATER_UPDATER_H
3 
4 #include <chrono>
5 
6 #include <QtCore/qobject.h>
7 #include <QtCore/qstring.h>
8 #include <QtCore/qlist.h>
9 #include <QtCore/qstringlist.h>
10 #include <QtCore/qdatetime.h>
11 
12 #include "QtAutoUpdaterCore/qtautoupdatercore_global.h"
13 #include "QtAutoUpdaterCore/updateinfo.h"
14 #include "QtAutoUpdaterCore/adminauthoriser.h"
15 #include "QtAutoUpdaterCore/updaterbackend.h"
16 
17 class QSettings;
18 
19 namespace QtAutoUpdater
20 {
21 
22 class UpdateInstaller;
23 
24 class UpdaterPrivate;
26 class Q_AUTOUPDATERCORE_EXPORT Updater : public QObject
27 {
28  Q_OBJECT
29 
31  Q_PROPERTY(QtAutoUpdater::UpdaterBackend::Features features READ features CONSTANT)
33  Q_PROPERTY(QtAutoUpdater::Updater::State state READ state NOTIFY stateChanged)
35  Q_PROPERTY(bool running READ isRunning NOTIFY runningChanged)
37  Q_PROPERTY(QList<QtAutoUpdater::UpdateInfo> updateInfo READ updateInfo NOTIFY updateInfoChanged)
39  Q_PROPERTY(bool runOnExit READ willRunOnExit RESET cancelExitRun NOTIFY runOnExitChanged)
40 
41 public:
43  enum class State {
44  NoUpdates = 0,
45  Checking,
46  Canceling,
47  NewUpdates,
48  Installing,
49 
50  Error = -1
51  };
52  Q_ENUM(State)
53 
54 
55  enum class InstallModeFlag {
56  Parallel = 0x00,
57  OnExit = 0x01,
58 
59  Force = 0x02,
60 
61  ForceOnExit = (OnExit | Force)
62  };
63  Q_DECLARE_FLAGS(InstallMode, InstallModeFlag)
64  Q_FLAG(InstallMode)
65 
66 
67  enum class InstallScope {
68  PreferInternal,
69  PreferExternal
70  };
71  Q_ENUM(InstallScope)
72 
73 
74  static QStringList supportedUpdaterBackends();
75 
77  static Updater *create(QObject *parent = nullptr);
79  static Updater *create(const QString &configPath,
80  QObject *parent = nullptr);
82  static Updater *create(QSettings *config,
83  QObject *parent = nullptr);
85  static Updater *create(QString key,
86  QVariantMap configuration,
87  QObject *parent = nullptr);
89  static Updater *create(UpdaterBackend::IConfigReader *configReader,
90  QObject *parent = nullptr);
91 
93  ~Updater() override;
94 
96  Q_INVOKABLE QtAutoUpdater::UpdaterBackend *backend() const;
97 
99  UpdaterBackend::Features features() const;
101  State state() const;
103  bool isRunning() const;
105  QList<UpdateInfo> updateInfo() const;
107  bool willRunOnExit() const;
108 
110  int scheduleUpdate(std::chrono::seconds delaySeconds, bool repeated = false);
112  Q_INVOKABLE int scheduleUpdate(int delaySeconds, bool repeated = false);
114  template <typename TRep, typename TPeriod>
115  int scheduleUpdate(const std::chrono::duration<TRep, TPeriod> &delaySeconds, bool repeated = false);
117  Q_INVOKABLE int scheduleUpdate(const QDateTime &when);
119  template <typename TClock, typename TDur>
120  int scheduleUpdate(const std::chrono::time_point<TClock, TDur> &when);
121 
123  Q_INVOKABLE bool runUpdater(InstallMode mode = InstallModeFlag::Parallel,
124  InstallScope scope = InstallScope::PreferInternal);
125 
126 public Q_SLOTS:
128  void checkForUpdates();
130  void abortUpdateCheck(int killDelay = 5000);
131 
133  void cancelScheduledUpdate(int taskId);
134 
136  void cancelExitRun();
137 
138 Q_SIGNALS:
140  void checkUpdatesDone(QtAutoUpdater::Updater::State result, QPrivateSignal);
142  void progressChanged(double progress, const QString &status, QPrivateSignal);
144  void showInstaller(QtAutoUpdater::UpdateInstaller *installer, QPrivateSignal);
146  void installDone(bool success, QPrivateSignal);
147 
149  void stateChanged(QtAutoUpdater::Updater::State state, QPrivateSignal);
151  void runningChanged(bool running, QPrivateSignal);
153  void updateInfoChanged(QList<QtAutoUpdater::UpdateInfo> updateInfo, QPrivateSignal);
155  void runOnExitChanged(bool runOnExit, QPrivateSignal);
156 
157 protected:
159  explicit Updater(QObject *parent = nullptr);
161  explicit Updater(UpdaterPrivate &dd, QObject *parent = nullptr);
162 
163 private:
164  Q_DECLARE_PRIVATE(Updater)
165  Q_DISABLE_COPY(Updater)
166 
167  Q_PRIVATE_SLOT(d_func(), void _q_appAboutToExit())
168  Q_PRIVATE_SLOT(d_func(), void _q_checkDone(bool, QList<UpdateInfo>))
169  Q_PRIVATE_SLOT(d_func(), void _q_triggerInstallDone(bool))
170 };
171 
172 template<typename TRep, typename TPeriod>
173 int Updater::scheduleUpdate(const std::chrono::duration<TRep, TPeriod> &delaySeconds, bool repeated)
174 {
175  using namespace std::chrono;
176  return scheduleUpdate(duration_cast<seconds>(delaySeconds), repeated);
177 }
178 
179 template<typename TClock, typename TDur>
180 int Updater::scheduleUpdate(const std::chrono::time_point<TClock, TDur> &when)
181 {
182  using namespace std::chrono;
183  return scheduleUpdate(QDateTime::fromSecsSinceEpoch(duration_cast<seconds>(
184  time_point_cast<system_clock>(when)
185  .time_since_epoch())));
186 }
187 
188 }
189 
190 Q_DECLARE_METATYPE(QtAutoUpdater::Updater::State)
191 Q_DECLARE_OPERATORS_FOR_FLAGS(QtAutoUpdater::Updater::InstallMode)
192 
193 #endif // QTAUTOUPDATER_UPDATER_H
QtAutoUpdater::UpdateInfo
Provides information about updates for components.
Definition: updateinfo.h:14
QtAutoUpdater::Updater
The main updater. Can check for updates and trigger update installations.
Definition: updater.h:26
QtAutoUpdater::Updater::scheduleUpdate
int scheduleUpdate(std::chrono::seconds delaySeconds, bool repeated=false)
Schedules an update after a specific delay, optionally repeated.
QtAutoUpdater::Updater::InstallModeFlag
InstallModeFlag
Flags to indicate how to run the installer.
Definition: updater.h:55
QtAutoUpdater::Updater::State
State
The state of the updater.
Definition: updater.h:43
QList
QString
QtAutoUpdater::Updater::InstallScope
InstallScope
Defines the different installation scopes.
Definition: updater.h:67
QtAutoUpdater::UpdaterBackend::IConfigReader
A helper interface to allow backend generic access to a backends configuration.
Definition: updaterbackend.h:39
QDateTime::fromSecsSinceEpoch
QDateTime fromSecsSinceEpoch(qint64 secs, Qt::TimeSpec spec, int offsetSeconds)
QtAutoUpdater::UpdateInstaller
An interface to implement and consume custom in-process installer.
Definition: updateinstaller.h:17
QSettings
QDateTime
QObject
QtAutoUpdater
The QtAutoUpdater namespace holds all classes that are related with the updater.
Definition: adminauthoriser.h:9
QStringList
QtAutoUpdater::UpdaterBackend
An interface to be implemented by updater plugins to provide the updater functionalities.
Definition: updaterbackend.h:22