cutter/src/common/UpdateWorker.h
optizone 3fed97ad86 Auto update check (#1235)
* init commit

* bug fix

* call slot of null object bug fix

* delete extra disconnect() func

* change api and add doc

* run astyle

* some improvements

* memory leak fix

* add check on start checkbox

* add checkbox to about page

* serve version check reply using lambda instead of slot

* fix grammar mistakes

* more docs

* save some lines

* change button text

* astyle

* change message text

* dont use QApplication pointer as a parent for network manager

* proper deletion of QNetworkReply*

* VersionChecker -> UpdateWorker

* windows dll hack

* after rebase fix

* some improvements

* better determination of arch

* more docs

* improvements

* add UpdateWorker::showUpdateDialog

* remove odd condition

* more improvements

* fix windows bug

* make dialog non-blocking

* change text in download progress dialog

* bug fix

* remove debug conditions

* change docs format
2019-03-09 14:11:39 +01:00

124 lines
3.5 KiB
C++

#ifndef UPDATEWORKER_H
#define UPDATEWORKER_H
#include <QDir>
#include <QTimer>
#include <QObject>
#include <QtNetwork/QNetworkAccessManager>
class QNetworkReply;
/**
* @class UpdateWorker
* @brief The UpdateWorker class is a class providing API to check for current Cutter version
* and download specific version of one.
*/
class UpdateWorker : public QObject
{
Q_OBJECT
public:
explicit UpdateWorker(QObject *parent = nullptr);
/**
* @fn void UpdateWorker::checkCurrentVersion(time_t timeoutMs)
*
* Sends request to determine current version of Cutter.
* If there is no response in @a timeoutMs milliseconds, emits
* @fn UpdateWorker::checkComplete(const QString& currVerson, const QString& errorMsg)
* with timeout error message.
*
*
* @sa checkComplete(const QString& verson, const QString& errorMsg)
*/
void checkCurrentVersion(time_t timeoutMs);
/**
* @fn void UpdateWorker::download(QDir downloadPath, QString version)
*
* @brief Downloads provided @a version of Cutter into @a downloadDir.
*
* @sa downloadProcess(size_t bytesReceived, size_t bytesTotal)
*/
void download(QString filename, QString version);
/**
* @fn void UpdateWorker::showUpdateDialog()
*
* Shows dialog that allows user to either download latest version of Cutter from website
* or download it by clicking on a button. This dialog also has "Don't check for updates"
* button which disables on-start update checks if @a showDontCheckForUpdatesButton is true.
*
* @sa downloadProcess(size_t bytesReceived, size_t bytesTotal)
*/
void showUpdateDialog(bool showDontCheckForUpdatesButton);
public slots:
/**
* @fn void UpdateWorker::abortDownload()
*
* @brief Stops current process of downloading.
*
* @note UpdateWorker::downloadFinished(QString filename) is not send after this function.
*
* @sa download(QDir downloadDir, QString version)
*/
void abortDownload();
signals:
/**
* @fn UpdateWorker::checkComplete(const QString& verson, const QString& errorMsg)
*
* The signal is emitted when check has been done with an empty @a errorMsg string.
* In case of an error @a currVerson is empty and @a errorMsg contains description
* of error.
*/
void checkComplete(const QString &currVerson, const QString &errorMsg);
/**
* @fn UpdateWorker::downloadProcess(size_t bytesReceived, size_t bytesTotal)
*
* The signal is emitted each time when some amount of bytes was downloaded.
* May be used as indicator of download progress.
*/
void downloadProcess(size_t bytesReceived, size_t bytesTotal);
/**
* @fn UpdateWorker::downloadFinished(QString filename)
*
* @brief The signal is emitted as soon as downloading completes.
*/
void downloadFinished(QString filename);
/**
* @fn UpdateWorker::downloadError(QString errorStr)
*
* @brief The signal is emitted when error occures during download.
*/
void downloadError(QString errorStr);
private slots:
void serveVersionCheckReply();
void serveDownloadFinish();
void process(size_t bytesReceived, size_t bytesTotal);
private:
QString getRepositeryExt() const;
QString getRepositoryFileName() const;
private:
QNetworkAccessManager nm;
QString latestVersion;
QTimer t;
bool pending;
QFile downloadFile;
QNetworkReply *downloadReply;
QNetworkReply *checkReply;
};
#endif // UPDATEWORKER_H