source: smplayer/vendor/current/src/updatechecker.cpp

Last change on this file was 186, checked in by Silvan Scherrer, 8 years ago

SMPlayer: update vendor to 17.1.0

File size: 7.6 KB
Line 
1/* smplayer, GUI front-end for mplayer.
2 Copyright (C) 2006-2017 Ricardo Villalba <rvm@users.sourceforge.net>
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17*/
18
19#include "updatechecker.h"
20#include "updatecheckerdata.h"
21#include "version.h"
22#include "links.h"
23#include <QUrl>
24#include <QNetworkAccessManager>
25#include <QNetworkRequest>
26#include <QNetworkReply>
27#include <QRegExp>
28#include <QDate>
29#include <QDateTime>
30#include <QStringList>
31#include <QMessageBox>
32#include <QDesktopServices>
33#include <QTemporaryFile>
34#include <QSettings>
35#include <QDebug>
36
37
38UpdateChecker::UpdateChecker(QWidget * parent, UpdateCheckerData * data) : QObject(parent)
39 , net_manager(0)
40 , d(0)
41{
42 d = data;
43
44 check_url = URL_VERSION_INFO;
45 user_agent = "SMPlayer";
46
47 /*
48 It looks like some distro maintainers disable this update checker...
49 This is a very very bad idea. Main reason: in order to play Youtube videos
50 with SMPlayer a very recent version is required. Distros usually provide
51 ancient versions...
52 If you remove the possibility to inform the user about new versions they will
53 probably use a version with broken support for Youtube like forever.
54
55 If you still want to disable this option, the correct way to do it is by
56 removing the UPDATE_CHECKER define in smplayer.pro, and not by removing the
57 following code.
58 */
59
60 connect(this, SIGNAL(newVersionFound(const QString &)),
61 this, SLOT(reportNewVersionAvailable(const QString &)));
62
63 connect(this, SIGNAL(noNewVersionFound(const QString &)),
64 this, SLOT(reportNoNewVersionFound(const QString &)));
65
66 connect(this, SIGNAL(errorOcurred(int, QString)), this, SLOT(reportError(int, QString)));
67
68 net_manager = new QNetworkAccessManager(this);
69
70 QDate now = QDate::currentDate();
71 //now = now.addDays(27);
72 int days = QDateTime(d->last_checked).daysTo(QDateTime(now));
73
74 qDebug() << "UpdateChecker::UpdateChecker: enabled:" << d->enabled;
75 qDebug() << "UpdateChecker::UpdateChecker: days_to_check:" << d->days_to_check;
76 qDebug() << "UpdateChecker::UpdateChecker: days since last check:" << days;
77
78 if ((!d->enabled) || (days < d->days_to_check)) return;
79
80 QNetworkRequest req(check_url);
81 req.setRawHeader("User-Agent", user_agent);
82 QNetworkReply *reply = net_manager->get(req);
83 connect(reply, SIGNAL(finished()), this, SLOT(gotReply()));
84}
85
86UpdateChecker::~UpdateChecker() {
87}
88
89// Force a check, requested by the user
90void UpdateChecker::check() {
91 qDebug("UpdateChecker::check");
92
93 QNetworkRequest req(check_url);
94 req.setRawHeader("User-Agent", user_agent);
95 QNetworkReply *reply = net_manager->get(req);
96 connect(reply, SIGNAL(finished()), this, SLOT(gotReplyFromUserRequest()));
97}
98
99QString UpdateChecker::parseVersion(const QByteArray & data, const QString & name) {
100 QTemporaryFile tf;
101 tf.open();
102 tf.write(data);
103 tf.close();
104 QString tfile = tf.fileName();
105 qDebug() << "UpdateChecker::parseVersion: tfile:" << tfile;
106
107 #ifdef Q_OS_WIN
108 QString grname = "windows";
109 #else
110 QString grname = "linux";
111 #endif
112 QSettings set(tfile, QSettings::IniFormat);
113 set.beginGroup(grname);
114 QString version = set.value(name, "").toString();
115 set.endGroup();
116 return version;
117}
118
119void UpdateChecker::gotReply() {
120 qDebug("UpdateChecker::gotReply");
121
122 QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
123
124 if (reply) {
125 if (reply->error() == QNetworkReply::NoError) {
126 QString version = parseVersion(reply->readAll(), "stable");
127 if (!version.isEmpty()) {
128 d->last_checked = QDate::currentDate();
129 qDebug() << "UpdateChecker::gotReply: last known version:" << d->last_known_version << "received version:" << version;
130 qDebug() << "UpdateChecker::gotReply: installed version:" << Version::with_revision();
131 if ((d->last_known_version != version) && (formattedVersion(version) > formattedVersion(Version::with_revision()))) {
132 qDebug() << "UpdateChecker::gotReply: new version found:" << version;
133 emit newVersionFound(version);
134 }
135 }
136 } else {
137 //get http status code
138 int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
139 qDebug("UpdateChecker::gotReply: status: %d", status);
140 }
141 reply->deleteLater();
142 }
143}
144
145void UpdateChecker::gotReplyFromUserRequest() {
146 qDebug("UpdateChecker::gotReplyFromUserRequest");
147
148 QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
149
150 if (reply) {
151 if (reply->error() == QNetworkReply::NoError) {
152 QString version = parseVersion(reply->readAll(), "unstable");
153 if (!version.isEmpty()) {
154 if ((formattedVersion(version) > formattedVersion(Version::with_revision()))) {
155 qDebug("UpdateChecker::gotReplyFromUserRequest: new version found: %s", version.toUtf8().constData());
156 emit newVersionFound(version);
157 } else {
158 emit noNewVersionFound(version);
159 }
160 } else {
161 emit errorOcurred(1, tr("Failed to get the latest version number") );
162 }
163 } else {
164 int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
165 qDebug("UpdateChecker::gotReplyFromUserRequest: status: %d", status);
166 emit errorOcurred((int)reply->error(), reply->errorString());
167 }
168 reply->deleteLater();
169 }
170}
171
172void UpdateChecker::saveVersion(QString v) {
173 d->last_known_version = v;
174}
175
176QString UpdateChecker::formattedVersion(const QString & version) {
177 int n1 = 0, n2 = 0, n3 = 0, n4 = 0;
178 QStringList l = version.split(".");
179 int c = l.count();
180 if (c >= 1) n1 = l[0].toInt();
181 if (c >= 2) n2 = l[1].toInt();
182 if (c >= 3) n3 = l[2].toInt();
183 if (c >= 4) n4 = l[3].toInt();
184
185 QString res = QString("%1.%2.%3.%4").arg(n1, 2, 10, QChar('0'))
186 .arg(n2, 2, 10, QChar('0'))
187 .arg(n3, 2, 10, QChar('0'))
188 .arg(n4, 4, 10, QChar('0'));
189 //qDebug() << "UpdateChecker::formattedVersion:" << res;
190 return res;
191}
192
193void UpdateChecker::reportNewVersionAvailable(const QString & new_version) {
194 QWidget * p = qobject_cast<QWidget*>(parent());
195
196 QMessageBox::StandardButton button = QMessageBox::information(p, tr("New version available"),
197 tr("A new version of SMPlayer is available.") + "<br><br>" +
198 tr("Installed version: %1").arg(Version::with_revision()) + "<br>" +
199 tr("Available version: %1").arg(new_version) + "<br><br>" +
200 tr("Would you like to know more about this new version?"),
201 QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
202
203 if (button == QMessageBox::Yes) {
204 QDesktopServices::openUrl(QUrl(URL_CHANGES));
205 }
206
207 saveVersion(new_version);
208}
209
210void UpdateChecker::reportNoNewVersionFound(const QString & version) {
211 QWidget * p = qobject_cast<QWidget*>(parent());
212
213 QMessageBox::information(p, tr("Checking for updates"),
214 tr("Congratulations, SMPlayer is up to date.") + "<br><br>" +
215 tr("Installed version: %1").arg(Version::with_revision()) + "<br>" +
216 tr("Available version: %1").arg(version));
217}
218
219void UpdateChecker::reportError(int error_number, QString error_str) {
220 QWidget * p = qobject_cast<QWidget*>(parent());
221 QMessageBox::warning(p, tr("Error"),
222 tr("An error happened while trying to retrieve information about the latest version available.") +
223 "<br>" + tr("Error code: %1").arg(error_number) + "<br>" + error_str);
224}
225
226#include "moc_updatechecker.cpp"
227
Note: See TracBrowser for help on using the repository browser.