1 #ifndef UPDATEWORKER_H
2 #define UPDATEWORKER_H
3 
4 #include <QtGlobal>
5 
6 #if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
7 #define CUTTER_UPDATE_WORKER_AVAILABLE 1
8 #else
9 #define CUTTER_UPDATE_WORKER_AVAILABLE 0
10 #endif
11 
12 #if CUTTER_UPDATE_WORKER_AVAILABLE
13 
14 #include <QDir>
15 #include <QTimer>
16 #include <QObject>
17 #include <QtNetwork/QNetworkAccessManager>
18 
19 #include <QVersionNumber>
20 
21 class QNetworkReply;
22 
23 /**
24  * @class UpdateWorker
25  * @brief The UpdateWorker class is a class providing API to check for current Cutter version
26  *        and download specific version of one.
27  */
28 
29 class UpdateWorker : public QObject
30 {
31     Q_OBJECT
32 public:
33     explicit UpdateWorker(QObject *parent = nullptr);
34 
35     /**
36      * @fn void UpdateWorker::checkCurrentVersion(time_t timeoutMs)
37      *
38      * Sends request to determine current version of Cutter.
39      * If there is no response in @a timeoutMs milliseconds, emits
40      * @fn UpdateWorker::checkComplete(const QString& currVerson, const QString& errorMsg)
41      * with timeout error message.
42      *
43      *
44      * @sa checkComplete(const QString& verson, const QString& errorMsg)
45      */
46 
47     void checkCurrentVersion(time_t timeoutMs);
48 
49     /**
50      * @fn void UpdateWorker::download(QDir downloadPath, QString version)
51      *
52      * @brief Downloads provided @a version of Cutter into @a downloadDir.
53      *
54      * @sa downloadProcess(size_t bytesReceived, size_t bytesTotal)
55      */
56     void download(QString filename, QString version);
57 
58     /**
59      * @fn void UpdateWorker::showUpdateDialog()
60      *
61      * Shows dialog that allows user to either download latest version of Cutter from website
62      * or download it by clicking on a button. This dialog also has "Don't check for updates"
63      * button which disables on-start update checks if @a showDontCheckForUpdatesButton is true.
64      *
65      * @sa downloadProcess(size_t bytesReceived, size_t bytesTotal)
66      */
67     void showUpdateDialog(bool showDontCheckForUpdatesButton);
68 
69     /**
70      * @return the version of this Cutter binary, derived from CUTTER_VERSION_MAJOR, CUTTER_VERSION_MINOR and CUTTER_VERSION_PATCH.
71      */
72     static QVersionNumber currentVersionNumber();
73 
74 public slots:
75     /**
76      * @fn void UpdateWorker::abortDownload()
77      *
78      * @brief Stops current process of downloading.
79      *
80      * @note UpdateWorker::downloadFinished(QString filename) is not send after this function.
81      *
82      * @sa download(QDir downloadDir, QString version)
83      */
84     void abortDownload();
85 
86 signals:
87     /**
88      * @fn UpdateWorker::checkComplete(const QString& verson, const QString& errorMsg)
89      *
90      * The signal is emitted when check has been done with an empty @a errorMsg string.
91      * In case of an error @a currVerson is null and @a errorMsg contains description
92      * of error.
93      */
94     void checkComplete(const QVersionNumber &currVerson, const QString &errorMsg);
95 
96     /**
97      * @fn UpdateWorker::downloadProcess(size_t bytesReceived, size_t bytesTotal)
98      *
99      * The signal is emitted each time when some amount of bytes was downloaded.
100      * May be used as indicator of download progress.
101      */
102     void downloadProcess(size_t bytesReceived, size_t bytesTotal);
103 
104 
105     /**
106      * @fn UpdateWorker::downloadFinished(QString filename)
107      *
108      * @brief The signal is emitted as soon as downloading completes.
109      */
110     void downloadFinished(QString filename);
111 
112     /**
113      * @fn UpdateWorker::downloadError(QString errorStr)
114      *
115      * @brief The signal is emitted when error occures during download.
116      */
117     void downloadError(QString errorStr);
118 
119 private slots:
120     void serveVersionCheckReply();
121 
122     void serveDownloadFinish();
123 
124     void process(size_t bytesReceived, size_t bytesTotal);
125 
126 private:
127     QString getRepositeryExt() const;
128     QString getRepositoryFileName() const;
129 
130 private:
131     QNetworkAccessManager nm;
132     QVersionNumber latestVersion;
133     QTimer t;
134     bool pending;
135     QFile downloadFile;
136     QNetworkReply *downloadReply;
137     QNetworkReply *checkReply;
138 };
139 
140 #endif //CUTTER_UPDATE_WORKER_AVAILABLE
141 #endif // UPDATEWORKER_H
142