Falkon Develop
Cross-platform Qt-based web browser
updater.cpp
Go to the documentation of this file.
1/* ============================================================
2* Falkon - Qt web browser
3* Copyright (C) 2010-2014 David Rosca <nowrep@gmail.com>
4*
5* This program is free software: you can redistribute it and/or modify
6* it under the terms of the GNU General Public License as published by
7* the Free Software Foundation, either version 3 of the License, or
8* (at your option) any later version.
9*
10* This program 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
13* GNU General Public License for more details.
14*
15* You should have received a copy of the GNU General Public License
16* along with this program. If not, see <http://www.gnu.org/licenses/>.
17* ============================================================ */
18#include "updater.h"
19#include "browserwindow.h"
20#include "qztools.h"
21#include "mainapplication.h"
22#include "tabwidget.h"
24#include "networkmanager.h"
25
26#include <QTimer>
27#include <QNetworkRequest>
28#include <QNetworkReply>
29
31 : isValid(false)
32 , majorVersion(-1)
33 , minorVersion(-1)
34 , revisionNumber(-1)
35{
36 isValid = false;
37
38 QStringList v = s.split(QLatin1Char('.'));
39 if (v.count() != 3) {
40 return;
41 }
42
43 bool ok;
44
45 majorVersion = v.at(0).toInt(&ok);
46 if (!ok) {
47 return;
48 }
49
50 minorVersion = v.at(1).toInt(&ok);
51 if (!ok) {
52 return;
53 }
54
55 revisionNumber = v.at(2).toInt(&ok);
56 if (!ok) {
57 return;
58 }
59
60 isValid = majorVersion >= 0 && minorVersion >= 0 && revisionNumber >= 0;
61}
62
64{
65 if (this->majorVersion != other.majorVersion) {
66 return this->majorVersion < other.majorVersion;
67 }
68 if (this->minorVersion != other.minorVersion) {
69 return this->minorVersion < other.minorVersion;
70 }
71 if (this->revisionNumber != other.revisionNumber) {
72 return this->revisionNumber < other.revisionNumber;
73 }
74
75 return false;
76}
77
79{
80 if (*this == other) {
81 return false;
82 }
83 return !operator<(other);
84}
85
87{
88 return (this->majorVersion == other.majorVersion &&
89 this->minorVersion == other.minorVersion &&
90 this->revisionNumber == other.revisionNumber);
91}
92
94{
95 if (*this == other) {
96 return true;
97 }
98 return *this > other;
99}
100
102{
103 if (*this == other) {
104 return true;
105 }
106 return *this < other;
107}
108
110{
111 return QSL("%1.%2.%3").arg(majorVersion, minorVersion, revisionNumber);
112}
113
114Updater::Updater(BrowserWindow* window, QObject* parent)
115 : QObject(parent)
116 , m_window(window)
117{
118 QTimer::singleShot(60 * 1000, this, &Updater::start); // Start checking after 1 minute
119}
120
121void Updater::start()
122{
123 QUrl url = QUrl(QSL("%1/update.php?v=%2&os=%3").arg(QString::fromLatin1(Qz::WWWADDRESS),
124 QString::fromLatin1(Qz::VERSION),
126
127 startDownloadingUpdateInfo(url);
128}
129
130void Updater::startDownloadingUpdateInfo(const QUrl &url)
131{
132 QNetworkReply *reply = mApp->networkManager()->get(QNetworkRequest(QUrl(url)));
133
134 connect(reply, &QNetworkReply::finished, this, &Updater::downCompleted);
135}
136
137void Updater::downCompleted()
138{
139 auto* reply = qobject_cast<QNetworkReply*>(sender());
140 if (!reply)
141 return;
142
143 QString html = QString::fromUtf8(reply->readAll());
144
145 if (html.startsWith(QLatin1String("Version:"))) {
146 html.remove(QLatin1String("Version:"));
147 Version current(QString::fromLatin1(Qz::VERSION));
148 Version updated(html);
149
150 if (current.isValid && updated.isValid && current < updated) {
151 mApp->desktopNotifications()->showNotification(QIcon(QSL(":icons/falkon.svg")).pixmap(48), tr("Update available"), tr("New version of Falkon is ready to download."));
152 }
153 }
154
155 reply->deleteLater();
156}
157
158void Updater::downloadNewVersion()
159{
160 m_window->tabWidget()->addView(
161 QUrl::fromEncoded(
162 QByteArray(QByteArray(Qz::WWWADDRESS) + QByteArray("/download"))),
163 tr("Update"), Qz::NT_NotSelectedTab);
164}
TabWidget * tabWidget() const
static QString operatingSystem()
Definition: qztools.cpp:904
int addView(const LoadRequest &req, const Qz::NewTabPositionFlags &openFlags, bool selectLine=false, bool pinned=false)
Definition: tabwidget.cpp:314
Updater(BrowserWindow *window, QObject *parent=nullptr)
Definition: updater.cpp:114
#define mApp
FALKON_EXPORT const char * VERSION
Definition: qzcommon.cpp:26
FALKON_EXPORT const char * WWWADDRESS
Definition: qzcommon.cpp:29
@ NT_NotSelectedTab
Definition: qzcommon.h:98
#define QSL(x)
Definition: qzcommon.h:40
bool operator<=(const Version &other) const
Definition: updater.cpp:101
int majorVersion
Definition: updater.h:38
int minorVersion
Definition: updater.h:39
bool operator>(const Version &other) const
Definition: updater.cpp:78
bool operator==(const Version &other) const
Definition: updater.cpp:86
QString versionString() const
Definition: updater.cpp:109
bool operator>=(const Version &other) const
Definition: updater.cpp:93
Version(const QString &s)
Definition: updater.cpp:30
bool operator<(const Version &other) const
Definition: updater.cpp:63
int revisionNumber
Definition: updater.h:40