1 /*!
2  * \copyright Copyright (c) 2016-2021 Governikus GmbH & Co. KG, Germany
3  */
4 
5 #pragma once
6 
7 #include "AppUpdateData.h"
8 #include "Env.h"
9 #include "GlobalStatus.h"
10 
11 #include <QDateTime>
12 #include <QUrl>
13 
14 class test_AppUpdatr;
15 
16 namespace governikus
17 {
18 class AppUpdater
19 	: public QObject
20 {
21 	Q_OBJECT
22 
23 	private:
24 		friend class Env;
25 		friend class ::test_AppUpdatr;
26 
27 		bool mForceUpdate;
28 		QUrl mAppUpdateJsonUrl;
29 		AppUpdateData mAppUpdateData;
30 		QString mDownloadPath;
31 		bool mDownloadInProgress;
32 
33 		static QCryptographicHash::Algorithm getHashAlgo(const QByteArray& pAlgo);
34 
35 		AppUpdater();
36 		~AppUpdater() override = default;
37 
38 		void clearDownloaderConnection();
39 		bool download(const QUrl& pUrl);
40 		QString save(const QByteArray& pData, const QString& pFilename);
41 
42 	public:
43 		bool abortDownload();
44 		bool downloadUpdate();
45 		bool checkAppUpdate(bool pForceUpdate = false);
46 		[[nodiscard]] const AppUpdateData& getUpdateData() const;
47 		void skipVersion(const QString& pVersion);
48 
49 #ifndef QT_NO_DEBUG
50 		[[nodiscard]] QString getDownloadPath() const;
51 		void setDownloadPath(const QString& pPath);
52 #endif
53 
54 	private Q_SLOTS:
55 		void onDownloadFinished(const QUrl& pUpdateUrl, const QDateTime& pNewTimestamp, const QByteArray& pData);
56 		void onDownloadFailed(const QUrl& pUpdateUrl, GlobalStatus::Code pErrorCode);
57 		void onDownloadUnnecessary(const QUrl& pUpdateUrl);
58 		void onDownloadProgress(const QUrl& pUpdateUrl, qint64 pBytesReceived, qint64 pBytesTotal);
59 
60 	Q_SIGNALS:
61 		void fireAppcastCheckFinished(bool pUpdateAvailable, const GlobalStatus& pError);
62 		void fireAppDownloadFinished(const GlobalStatus& pError);
63 		void fireAppDownloadProgress(qint64 pBytesReceived, qint64 pBytesTotal);
64 };
65 
66 } // namespace governikus
67