OSDN Git Service

Happy new year 2015 !!!
[mutilities/MUtilities.git] / include / MUtils / UpdateChecker.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // MuldeR's Utilities for Qt
3 // Copyright (C) 2004-2015 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 //MUtils
25 #include <MUtils/Global.h>
26
27 //Qt
28 #include <QThread>
29 #include <QDate>
30
31 ///////////////////////////////////////////////////////////////////////////////
32
33 namespace MUtils
34 {
35         class MUTILS_API UpdateCheckerInfo
36         {
37                 friend class UpdateChecker;
38
39         public:
40                 UpdateCheckerInfo(void);
41                 void resetInfo(void);
42
43                 const quint32 &getBuildNo(void)          const { return m_buildNo;          }
44                 const QDate   &getBuildDate(void)        const { return m_buildDate;        }
45                 const QString &getDownloadSite(void)     const { return m_downloadSite;     }
46                 const QString &getDownloadAddress(void)  const { return m_downloadAddress;  }
47                 const QString &getDownloadFilename(void) const { return m_downloadFilename; }
48                 const QString &getDownloadFilecode(void) const { return m_downloadFilecode; }
49
50         private:
51                 quint32 m_buildNo;
52                 QDate m_buildDate;
53                 QString m_downloadSite;
54                 QString m_downloadAddress;
55                 QString m_downloadFilename;
56                 QString m_downloadFilecode;
57         };
58
59         // ----------------------------------------------------------------
60
61         class MUTILS_API UpdateChecker : public QThread
62         {
63                 Q_OBJECT
64
65         public:
66                 enum
67                 {
68                         UpdateStatus_NotStartedYet             = 0,
69                         UpdateStatus_CheckingConnection        = 1,
70                         UpdateStatus_FetchingUpdates           = 2,
71                         UpdateStatus_CompletedUpdateAvailable  = 3,
72                         UpdateStatus_CompletedNoUpdates        = 4,
73                         UpdateStatus_CompletedNewVersionOlder  = 5,
74                         UpdateStatus_ErrorNoConnection         = 6,
75                         UpdateStatus_ErrorConnectionTestFailed = 7,
76                         UpdateStatus_ErrorFetchUpdateInfo      = 8
77                 }
78                 update_status_t;
79
80                 UpdateChecker(const QString &binWGet, const QString &binGnuPG, const QString &binKeys, const QString &applicationId, const quint32 &installedBuildNo, const bool betaUpdates, const bool testMode = false);
81                 ~UpdateChecker(void);
82
83                 const int getUpdateStatus(void) const { return m_status; }
84                 const bool getSuccess(void) const { return m_success; };
85                 const int getMaximumProgress(void) const { return m_maxProgress; };
86                 const int getCurrentProgress(void) const { return m_progress; };
87                 const UpdateCheckerInfo *getUpdateInfo(void) const { return m_updateInfo; }
88
89         protected:
90                 void run(void);
91                 void checkForUpdates(void);
92                 void testKnownHosts(void);
93
94         signals:
95                 void statusChanged(const int status);
96                 void progressChanged(const int progress);
97                 void messageLogged(const QString &text);
98
99         private:
100                 const int m_maxProgress;
101                 UpdateCheckerInfo *const m_updateInfo;
102         
103                 const bool m_betaUpdates;
104                 const bool m_testMode;
105
106                 const QString m_applicationId;
107                 const quint32 m_installedBuildNo;
108
109                 const QString m_binaryWGet;
110                 const QString m_binaryGnuPG;
111                 const QString m_binaryKeys;
112
113                 volatile bool m_success;
114
115                 int m_status;
116                 int m_progress;
117
118                 inline void setStatus(const int status);
119                 inline void setProgress(const int progress);
120                 inline void log(const QString &str1, const QString &str2 = QString(), const QString &str3 = QString(), const QString &str4 = QString());
121
122                 bool getFile(const QString &url, const QString &outFile, unsigned int maxRedir = 5, bool *httpOk = NULL);
123                 bool getUpdateInfo(const QString &url, const QString &outFileVers, const QString &outFileSign);
124                 int tryContactHost(const QString &url);
125                 bool tryUpdateMirror(UpdateCheckerInfo *updateInfo, const QString &url);
126                 bool checkSignature(const QString &file, const QString &signature);
127                 bool parseVersionInfo(const QString &file, UpdateCheckerInfo *updateInfo);
128         };
129 }