Debian Codegraph @

src/GUI/UpdateChecker.cxx

raw symbols SCIP
1 // Written by James Turner, started October 2020
2 //
3 // Copyright (C) 2020 James Turner <[email protected]>
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 2 of the
8 // License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // 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, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
19 #include "config.h"
20
21 #include "UpdateChecker.hxx"
22
23 #include <QDate>
24 #include <QDebug>
25 #include <QSettings>
26
27 #if (QT_VERSION >= QT_VERSION_CHECK(6, 1, 0))
28 #include <QNetworkInformation>
29 #endif
30
31 #include <simgear/io/HTTPMemoryRequest.hxx>
32 #include <simgear/props/props_io.hxx>
33 #include <simgear/structure/exception.hxx>
34
35 #include <Network/HTTPClient.hxx>
36 #include <Main/globals.hxx>
37
38 #include "LauncherNotificationsController.hxx"
39 #include "SettingsWrapper.hxx"
40
41 namespace {
42
43 class UpdateXMLRequest : public simgear::HTTP::MemoryRequest
44 {
45 public:
46 UpdateXMLRequest(UpdateChecker* u, const std::string& uri) :
47 simgear::HTTP::MemoryRequest(uri),
48 _owner(u)
49 {
50 // in case it's useful, send our version as an additional header
51 // this would allow a server side script to return different content
52 // based on our version in the future
53 requestHeader("FlightGear-Version") = FLIGHTGEAR_VERSION;
54 }
55 private:
56 void onFail() override
57 {
58 didFail();
59 }
60
61 void onDone() override
62 {
63 int response = responseCode();
64 if (response == 200) {
65 // send response to the main thread for processing
66 QMetaObject::invokeMethod(_owner, "receivedUpdateXML", Qt::QueuedConnection,
67 Q_ARG(QByteArray, QByteArray::fromStdString(responseBody())));
68
69 } else {
70 didFail();
71 }
72 }
73
74 void didFail()
75 {
76 // reset check time to tomorrow
77 auto settings = flightgear::getQSettings();
78 const QDate n = QDate::currentDate().addDays(1);
79 settings.setValue("next-update-check", n);
80 }
81 private:
82 UpdateChecker* _owner;
83 };
84
85 } // of namespace
86
87 UpdateChecker::UpdateChecker(QObject *parent) : QObject(parent)
88 {
89 auto settings = flightgear::getQSettings();
90 QDate nextCheck = settings.value("next-update-check").toDate();
91 if (!nextCheck.isValid()) {
92 // check tomorrow, so we don't nag immediately after installaion
93 const QDate n = QDate::currentDate().addDays(1);
94 settings.setValue("next-update-check", n);
95
96 return;
97 }
98
99 // if we're offline when the launcher starts, skip the check
100 #if (QT_VERSION >= QT_VERSION_CHECK(6, 1, 0))
101 if (QNetworkInformation::instance()->reachability() != QNetworkInformation::Reachability::Online) {
102 qInfo() << "Launcher update check: not online, deferring";
103 return;
104 }
105 #endif
106
107 if (nextCheck <= QDate::currentDate()) {
108 // start a check
109 auto http = globals->get_subsystem<FGHTTPClient>();
110 if (!http) {
111 return;
112 }
113
114 const string_list versionParts = simgear::strutils::split(FLIGHTGEAR_VERSION, ".");
115 _majorMinorVersion = versionParts[0] + "." + versionParts[1];
116
117 // definitiely want to ensure HTTPS for this.
118 std::string uri = "https://update.flightgear.org/" + _majorMinorVersion + "/updates.xml";
119 m_request = new UpdateXMLRequest(this, uri);
120 http->makeRequest(m_request);
121 } else {
122 // nothing to do
123 }
124 }
125
126 UpdateChecker::~UpdateChecker()
127 {
128 if (m_request) {
129 auto http = globals->get_subsystem<FGHTTPClient>();
130 http->client()->cancelRequest(m_request);
131 m_request.clear();
132 }
133 }
134
135 void UpdateChecker::ignoreUpdate()
136 {
137 auto settings = flightgear::getQSettings();
138 if (m_status == PointUpdate) {
139 settings.setValue("ignored-point-release", _currentUpdateVersion);
140 } else if (m_status == MajorUpdate) {
141 settings.setValue("ignored-major-release", _currentUpdateVersion);
142 } else {
143 return;
144 }
145
146 m_status = NoUpdate;
147 m_updateUri.clear();
148 _currentUpdateVersion.clear();
149 emit statusChanged(m_status);
150 }
151
152 void UpdateChecker::receivedUpdateXML(QByteArray body)
153 {
154 SGPropertyNode_ptr props(new SGPropertyNode);
155 const auto s = body.toStdString();
156
157 auto settings = flightgear::getQSettings();
158 auto nc = LauncherNotificationsController::instance();
159
160 try {
161 const char* buffer = s.c_str();
162 readProperties(buffer, s.size(), props, true);
163
164 const QDate n = QDate::currentDate().addDays(7);
165 settings.setValue("next-update-check", n);
166
167 const std::string newMajorVersion = props->getStringValue("current-major-release");
168 if (simgear::strutils::compare_versions(FLIGHTGEAR_VERSION, newMajorVersion) < 0) {
169 // we have a newer version!
170 _currentUpdateVersion = QString::fromStdString(newMajorVersion);
171 if (settings.value("ignored-major-release") == _currentUpdateVersion) {
172 // ignore it
173 } else {
174 m_status = MajorUpdate;
175
176 const std::string newVersionUri = props->getStringValue("upgrade-uri");
177 m_updateUri = QUrl(QString::fromStdString(newVersionUri));
178 emit statusChanged(m_status);
179
180 nc->postNotification("flightgear-update-major", QUrl{"qrc:///qml/NewVersionNotification.qml"});
181
182 return; // don't consider minor updates
183 }
184 }
185
186 // check current version
187 const std::string newPointVersion = props->getStringValue("current-point-release");
188 if (simgear::strutils::compare_versions(FLIGHTGEAR_VERSION, newPointVersion) < 0) {
189 // we have a newer version!
190 _currentUpdateVersion = QString::fromStdString(newPointVersion);
191 if (settings.value("ignored-point-release") == _currentUpdateVersion) {
192 // ignore it
193 } else {
194 m_status = PointUpdate;
195 const std::string newVersionUri = props->getStringValue("download-uri");
196 m_updateUri = QUrl(QString::fromStdString(newVersionUri));
197 emit statusChanged(m_status);
198
199 nc->postNotification("flightgear-update-point", QUrl{"qrc:///qml/NewVersionNotification.qml"});
200 }
201 }
202 } catch (const sg_exception &e) {
203 SG_LOG(SG_IO, SG_WARN, "parsing update XML failed: " << e.getFormattedMessage());
204 }
205 }