1 // For license of this file, see <project-root-folder>/LICENSE.md.
2 
3 #ifndef SYSTEMFACTORY_H
4 #define SYSTEMFACTORY_H
5 
6 #include <QObject>
7 
8 #include "network-web/downloader.h"
9 
10 #include <QHash>
11 #include <QMetaType>
12 #include <QNetworkReply>
13 #include <QPair>
14 #include <QRegularExpression>
15 
16 class UpdateUrl {
17   public:
18 
19     QString m_fileUrl;
20     QString m_name;
21     QString m_size;
22 };
23 
24 class UpdateInfo {
25   public:
26 
27     QString m_availableVersion;
28     QString m_changes;
29     QDateTime m_date;
30     QList<UpdateUrl> m_urls;
31 };
32 
Q_DECLARE_METATYPE(UpdateInfo)33 Q_DECLARE_METATYPE(UpdateInfo)
34 
35 class SystemFactory : public QObject {
36   Q_OBJECT
37 
38   public:
39 
40     // Specifies possible states of auto-start functionality.
41     enum class AutoStartStatus {
42       Enabled,
43       Disabled,
44       Unavailable
45     };
46 
47     explicit SystemFactory(QObject* parent = nullptr);
48     virtual ~SystemFactory();
49 
50     // Returns current status of auto-start function.
51     SystemFactory::AutoStartStatus autoStartStatus() const;
52 
53     // Sets new status for auto-start function.
54     // Function returns false if setting of
55     // new status failed.
56     bool setAutoStartStatus(AutoStartStatus new_status);
57 
58 #if defined(Q_OS_UNIX)
59     // Returns standard location where auto-start .desktop files
60     // should be placed.
61     QString autostartDesktopFileLocation() const;
62 #endif
63 
64     // Retrieves username of currently logged-in user.
65     QString loggedInUser() const;
66 
67     // Tries to download list with new updates.
68     void checkForUpdates() const;
69 
70   public slots:
71     void checkForUpdatesOnStartup();
72 
73     static QRegularExpression supportedUpdateFiles();
74 
75     // Checks if update is newer than current application version.
76     static bool isVersionNewer(const QString& new_version, const QString& base_version);
77     static bool isVersionEqualOrNewer(const QString& new_version, const QString& base_version);
78     static bool openFolderFile(const QString& file_path);
79 
80   signals:
81     void updatesChecked(QPair<QList<UpdateInfo>, QNetworkReply::NetworkError> updates) const;
82 
83   private:
84 
85     // Performs parsing of downloaded file with list of updates.
86     QList<UpdateInfo> parseUpdatesFile(const QByteArray& updates_file) const;
87 };
88 
89 #endif // SYSTEMFACTORY_H
90