OSDN Git Service

Initial commit.
[mutilities/MUtilities.git] / include / Mutils / UpdateChecker.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // MuldeR's Utilities for Qt
3 // Copyright (C) 2004-2014 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18 //
19 // http://www.gnu.org/licenses/lgpl-2.1.txt
20 //////////////////////////////////////////////////////////////////////////////////
21
22 #pragma once
23
24 #include <QThread>
25 #include <QDate>
26
27 ///////////////////////////////////////////////////////////////////////////////
28
29 namespace MUtils {
30
31         class UpdateCheckerInfo
32         {
33                 friend class UpdateChecker;
34
35         public:
36                 UpdateCheckerInfo(void);
37                 void resetInfo(void);
38
39                 const quint32 &getBuildNo(void)          const { return m_buildNo;          }
40                 const QDate   &getBuildDate(void)        const { return m_buildDate;        }
41                 const QString &getDownloadSite(void)     const { return m_downloadSite;     }
42                 const QString &getDownloadAddress(void)  const { return m_downloadAddress;  }
43                 const QString &getDownloadFilename(void) const { return m_downloadFilename; }
44                 const QString &getDownloadFilecode(void) const { return m_downloadFilecode; }
45
46         private:
47                 quint32 m_buildNo;
48                 QDate m_buildDate;
49                 QString m_downloadSite;
50                 QString m_downloadAddress;
51                 QString m_downloadFilename;
52                 QString m_downloadFilecode;
53         };
54
55         // ----------------------------------------------------------------
56
57         class UpdateChecker : public QThread
58         {
59                 Q_OBJECT
60
61         public:
62                 enum
63                 {
64                         UpdateStatus_NotStartedYet             = 0,
65                         UpdateStatus_CheckingConnection        = 1,
66                         UpdateStatus_FetchingUpdates           = 2,
67                         UpdateStatus_CompletedUpdateAvailable  = 3,
68                         UpdateStatus_CompletedNoUpdates        = 4,
69                         UpdateStatus_CompletedNewVersionOlder  = 5,
70                         UpdateStatus_ErrorNoConnection         = 6,
71                         UpdateStatus_ErrorConnectionTestFailed = 7,
72                         UpdateStatus_ErrorFetchUpdateInfo      = 8
73                 }
74                 update_status_t;
75
76                 UpdateChecker(const QString &binWGet, const QString &binGnuPG, const QString &binKeys, const bool betaUpdates, const bool testMode = false);
77                 ~UpdateChecker(void);
78
79                 const int getUpdateStatus(void) const { return m_status; }
80                 const bool getSuccess(void) const { return m_success; };
81                 const int getMaximumProgress(void) const { return m_maxProgress; };
82                 const int getCurrentProgress(void) const { return m_progress; };
83                 const UpdateCheckerInfo *getUpdateInfo(void) const { return m_updateInfo; }
84
85         protected:
86                 void run(void);
87                 void checkForUpdates(void);
88                 void testKnownHosts(void);
89
90         signals:
91                 void statusChanged(const int status);
92                 void progressChanged(const int progress);
93                 void messageLogged(const QString &text);
94
95         private:
96                 const int m_maxProgress;
97                 UpdateCheckerInfo *const m_updateInfo;
98         
99                 const bool m_betaUpdates;
100                 const bool m_testMode;
101
102                 const QString m_binaryWGet;
103                 const QString m_binaryGnuPG;
104                 const QString m_binaryKeys;
105
106                 volatile bool m_success;
107
108                 int m_status;
109                 int m_progress;
110
111                 inline void setStatus(const int status);
112                 inline void setProgress(const int progress);
113                 inline void log(const QString &str1, const QString &str2 = QString(), const QString &str3 = QString(), const QString &str4 = QString());
114
115                 bool getFile(const QString &url, const QString &outFile, unsigned int maxRedir = 5, bool *httpOk = NULL);
116                 bool getUpdateInfo(const QString &url, const QString &outFileVers, const QString &outFileSign);
117                 int tryContactHost(const QString &url);
118                 bool tryUpdateMirror(UpdateCheckerInfo *updateInfo, const QString &url);
119                 bool checkSignature(const QString &file, const QString &signature);
120                 bool parseVersionInfo(const QString &file, UpdateCheckerInfo *updateInfo);
121         };
122 }