]> git.sur5r.net Git - minitube/blob - src/updatechecker.cpp
Imported Upstream version 2.3.1
[minitube] / src / updatechecker.cpp
1 /* $BEGIN_LICENSE
2
3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
5
6 Minitube is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 Minitube is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Minitube.  If not, see <http://www.gnu.org/licenses/>.
18
19 $END_LICENSE */
20
21 #include "updatechecker.h"
22 #include "networkaccess.h"
23 #include "constants.h"
24 #ifdef APP_ACTIVATION
25 #include "activation.h"
26 #endif
27
28 namespace The {
29 NetworkAccess* http();
30 }
31
32 UpdateChecker::UpdateChecker() {
33     m_needUpdate = false;
34 }
35
36 void UpdateChecker::checkForUpdate() {
37     QUrl url(QLatin1String(Constants::WEBSITE) + "-ws/release.xml");
38
39 #if QT_VERSION >= 0x050000
40     {
41         QUrl &u = url;
42         QUrlQuery url;
43 #endif
44
45         url.addQueryItem("v", Constants::VERSION);
46
47 #ifdef APP_MAC
48         url.addQueryItem("os", "mac");
49 #endif
50 #ifdef APP_WIN
51         url.addQueryItem("os", "win");
52 #endif
53 #ifdef APP_ACTIVATION
54         QString t = "demo";
55         if (Activation::instance().isActivated()) t = "active";
56         url.addQueryItem("t", t);
57 #endif
58 #ifdef APP_MAC_STORE
59         url.addQueryItem("store", "mac");
60 #endif
61
62 #if QT_VERSION >= 0x050000
63         u.setQuery(url);
64     }
65 #endif
66
67     QObject *reply = The::http()->get(url);
68     connect(reply, SIGNAL(data(QByteArray)), SLOT(requestFinished(QByteArray)));
69
70 }
71
72 void UpdateChecker::requestFinished(QByteArray data) {
73     UpdateCheckerStreamReader reader;
74     reader.read(data);
75     m_needUpdate = reader.needUpdate();
76     m_remoteVersion = reader.remoteVersion();
77     if (m_needUpdate && !m_remoteVersion.isEmpty()) emit newVersion(m_remoteVersion);
78 }
79
80 QString UpdateChecker::remoteVersion() {
81     return m_remoteVersion;
82 }
83
84 // --- Reader ---
85
86 bool UpdateCheckerStreamReader::read(QByteArray data) {
87     addData(data);
88
89     while (!atEnd()) {
90         readNext();
91         if (isStartElement()) {
92             if (name() == QLatin1String("release")) {
93                 while (!atEnd()) {
94                     readNext();
95                     if (isStartElement() && name() == QLatin1String("version")) {
96                         QString remoteVersion = readElementText();
97                         qDebug() << remoteVersion << QString(Constants::VERSION);
98                         m_needUpdate = remoteVersion != QString(Constants::VERSION);
99                         m_remoteVersion = remoteVersion;
100                         break;
101                     }
102                 }
103             }
104         }
105     }
106
107     return !error();
108 }
109
110 QString UpdateCheckerStreamReader::remoteVersion() {
111     return m_remoteVersion;
112 }