]> git.sur5r.net Git - minitube/blob - src/updatechecker.cpp
Upload 3.9.3-2 to unstable
[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 "http.h"
23 #include "httputils.h"
24 #include "constants.h"
25 #ifdef APP_ACTIVATION
26 #include "activation.h"
27 #endif
28
29 UpdateChecker::UpdateChecker() {
30     m_needUpdate = false;
31 }
32
33 void UpdateChecker::checkForUpdate() {
34     QUrl url(QLatin1String(Constants::WEBSITE) + "-ws/release.xml");
35     QUrlQuery q;
36     q.addQueryItem("v", Constants::VERSION);
37 #ifdef APP_MAC
38     q.addQueryItem("os", "mac");
39 #endif
40 #ifdef APP_WIN
41     q.addQueryItem("os", "win");
42 #endif
43 #ifdef APP_ACTIVATION
44     QString t = "demo";
45     if (Activation::instance().isActivated()) t = "active";
46     q.addQueryItem("t", t);
47 #endif
48 #ifdef APP_MAC_STORE
49     q.addQueryItem("store", "mac");
50 #endif
51     url.setQuery(q);
52
53     QObject *reply = HttpUtils::notCached().get(url);
54     connect(reply, SIGNAL(data(QByteArray)), SLOT(requestFinished(QByteArray)));
55 }
56
57 void UpdateChecker::requestFinished(QByteArray data) {
58     UpdateCheckerStreamReader reader;
59     reader.read(data);
60     m_needUpdate = reader.needUpdate();
61     m_remoteVersion = reader.remoteVersion();
62     if (m_needUpdate && !m_remoteVersion.isEmpty()) emit newVersion(m_remoteVersion);
63 }
64
65 QString UpdateChecker::remoteVersion() {
66     return m_remoteVersion;
67 }
68
69 // --- Reader ---
70
71 bool UpdateCheckerStreamReader::read(QByteArray data) {
72     addData(data);
73
74     while (!atEnd()) {
75         readNext();
76         if (isStartElement()) {
77             if (name() == QLatin1String("release")) {
78                 while (!atEnd()) {
79                     readNext();
80                     if (isStartElement() && name() == QLatin1String("version")) {
81                         QString remoteVersion = readElementText();
82                         qDebug() << remoteVersion << QString(Constants::VERSION);
83                         m_needUpdate = remoteVersion != QString(Constants::VERSION);
84                         m_remoteVersion = remoteVersion;
85                         break;
86                     }
87                 }
88             }
89         }
90     }
91
92     return !error();
93 }
94
95 QString UpdateCheckerStreamReader::remoteVersion() {
96     return m_remoteVersion;
97 }