| 1 | /* smplayer, GUI front-end for mplayer.
|
|---|
| 2 | Copyright (C) 2006-2013 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 "version.h"
|
|---|
| 21 | #include <QSettings>
|
|---|
| 22 | #include <QUrl>
|
|---|
| 23 | #include <QNetworkAccessManager>
|
|---|
| 24 | #include <QNetworkRequest>
|
|---|
| 25 | #include <QNetworkReply>
|
|---|
| 26 | #include <QRegExp>
|
|---|
| 27 | #include <QDate>
|
|---|
| 28 | #include <QDateTime>
|
|---|
| 29 |
|
|---|
| 30 | UpdateChecker::UpdateChecker(QObject * parent, QSettings * settings) : QObject(parent)
|
|---|
| 31 | {
|
|---|
| 32 | set = settings;
|
|---|
| 33 |
|
|---|
| 34 | set->beginGroup("update_checker");
|
|---|
| 35 | QDate last_checked = set->value("checked_date", 0).toDate();
|
|---|
| 36 | bool enabled = set->value("enabled", true).toBool();
|
|---|
| 37 | int days_to_check = set->value("days_to_check", 7).toInt();
|
|---|
| 38 | set->endGroup();
|
|---|
| 39 |
|
|---|
| 40 | QDate now = QDate::currentDate();
|
|---|
| 41 | //now = now.addDays(27);
|
|---|
| 42 | int days = QDateTime(last_checked).daysTo(QDateTime(now));
|
|---|
| 43 |
|
|---|
| 44 | qDebug("UpdateChecker::UpdateChecker: enabled: %d", enabled);
|
|---|
| 45 | qDebug("UpdateChecker::UpdateChecker: days_to_check: %d", days_to_check);
|
|---|
| 46 | qDebug("UpdateChecker::UpdateChecker: days since last check: %d", days);
|
|---|
| 47 |
|
|---|
| 48 | if ((!enabled) || (days < days_to_check)) return;
|
|---|
| 49 |
|
|---|
| 50 | net_manager = new QNetworkAccessManager();
|
|---|
| 51 | QUrl url("http://smplayer.sourceforge.net/current_version");
|
|---|
| 52 |
|
|---|
| 53 | QNetworkRequest req(url);
|
|---|
| 54 | QNetworkReply *reply = net_manager->get(req);
|
|---|
| 55 | connect(reply, SIGNAL(finished()), this, SLOT(gotReply()));
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | UpdateChecker::~UpdateChecker() {
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | void UpdateChecker::gotReply() {
|
|---|
| 62 | qDebug("UpdateChecker::gotReply");
|
|---|
| 63 |
|
|---|
| 64 | QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
|
|---|
| 65 |
|
|---|
| 66 | if (reply) {
|
|---|
| 67 | if (reply->error() == QNetworkReply::NoError) {
|
|---|
| 68 | //read data from reply
|
|---|
| 69 | QRegExp rx_version("^version=(.*)");
|
|---|
| 70 | QString version;
|
|---|
| 71 | while (reply->canReadLine()) {
|
|---|
| 72 | QByteArray line = reply->readLine().trimmed();
|
|---|
| 73 | //qDebug("line: %s", line.constData());
|
|---|
| 74 | if (rx_version.indexIn(line) != -1) {
|
|---|
| 75 | version = rx_version.cap(1);
|
|---|
| 76 | //qDebug("version: %s", version.toUtf8().constData());
|
|---|
| 77 | break;
|
|---|
| 78 | }
|
|---|
| 79 | }
|
|---|
| 80 | if (!version.isEmpty()) {
|
|---|
| 81 | set->beginGroup("update_checker");
|
|---|
| 82 | QString last_known_version = set->value("last_known_version", stableVersion()).toString();
|
|---|
| 83 | set->setValue("checked_date", QDate::currentDate());
|
|---|
| 84 | set->endGroup();
|
|---|
| 85 | if ((last_known_version != version) && (version !=stableVersion())) {
|
|---|
| 86 | qDebug("UpdateChecker::gotReply: new version found: %s", version.toUtf8().constData());
|
|---|
| 87 | emit newVersionFound(version);
|
|---|
| 88 | }
|
|---|
| 89 | }
|
|---|
| 90 | } else {
|
|---|
| 91 | //get http status code
|
|---|
| 92 | int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
|---|
| 93 | qDebug("UpdateChecker::gotReply: status: %d", status);
|
|---|
| 94 | }
|
|---|
| 95 | reply->deleteLater();
|
|---|
| 96 | }
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | void UpdateChecker::saveVersion(QString v) {
|
|---|
| 100 | set->beginGroup("update_checker");
|
|---|
| 101 | set->setValue("last_known_version", v);
|
|---|
| 102 | set->endGroup();
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | #include "moc_updatechecker.cpp"
|
|---|
| 106 |
|
|---|