/
checkforupdatesdialog.cpp
355 lines (252 loc) · 11.4 KB
/
checkforupdatesdialog.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*******************************************************************************
Copyright (C) The University of Auckland
OpenCOR is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenCOR is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*******************************************************************************/
//==============================================================================
// Check for updates dialog
//==============================================================================
#include "checkforupdatesdialog.h"
#include "cliutils.h"
#include "mainwindow.h"
//==============================================================================
#include "ui_checkforupdatesdialog.h"
//==============================================================================
#include <QCoreApplication>
#include <QCheckBox>
#include <QDesktopServices>
#include <QJsonDocument>
#include <QJsonObject>
#include <QLabel>
#include <QSettings>
#include <QTextEdit>
#include <QVariantMap>
//==============================================================================
namespace OpenCOR {
//==============================================================================
CheckForUpdatesEngine::CheckForUpdatesEngine(const QString &pApplicationDate) :
mApplicationDate(pApplicationDate),
mStatus(QString()),
mNewerVersions(QStringList())
{
}
//==============================================================================
void CheckForUpdatesEngine::check()
{
// Retrieve some information about the different versions of OpenCOR that
// are available
QByteArray fileVersionsContents;
QString errorMessage;
mNewerVersions.clear();
if (readFileContentsFromUrl("http://www.opencor.ws/downloads/index.js", fileVersionsContents, &errorMessage)) {
QJsonParseError jsonParseError;
QJsonDocument versions = QJsonDocument::fromJson(fileVersionsContents.mid(15, fileVersionsContents.length()-17), &jsonParseError);
if (jsonParseError.error == QJsonParseError::NoError) {
QVariantMap versionMap;
int versionMajor, versionMinor, versionPatch;
int versionDay, versionMonth, versionYear;
QString versionVersion;
QString versionDate;
foreach (const QVariant &version, versions.object().toVariantMap()["versions"].toList()) {
// Retrieve the version and date of the current version
versionMap = version.toMap();
versionMajor = versionMap["major"].toInt();
versionMinor = versionMap["minor"].toInt();
versionPatch = versionMap["patch"].toInt();
versionDay = versionMap["day"].toInt();
versionMonth = versionMap["month"].toInt();
versionYear = versionMap["year"].toInt();
versionDate = QDate(versionYear, versionMonth, versionDay).toString("yyyy-MM-dd");
if (!versionMajor && !versionMinor && !versionPatch) {
versionVersion = versionDate;
} else {
versionVersion = QString("%1.%2").arg(QString::number(versionMajor), QString::number(versionMinor));
if (versionPatch)
versionVersion = QString("%1.%2").arg(versionVersion, QString::number(versionPatch));
}
// Check whether the version is newer and, if so, add it to our
// list
if (mApplicationDate.compare(versionDate) < 0)
mNewerVersions << versionVersion;
}
mStatus = QString();
} else {
mStatus = tr("The version information is mal-formatted.")
+"<br/>"
+tr("Please <a href=\"http://opencor.ws/user/contactUs.html\">contact us</a> about this error.");
}
} else {
mStatus = formatMessage(errorMessage, false, true);
}
}
//==============================================================================
QString CheckForUpdatesEngine::status() const
{
// Return our status
return mStatus;
}
//==============================================================================
QStringList CheckForUpdatesEngine::newerVersions() const
{
// Return our newer versions
return mNewerVersions;
}
//==============================================================================
bool CheckForUpdatesEngine::hasNewerVersion() const
{
// Return whether we have a newer version
return !mNewerVersions.isEmpty();
}
//==============================================================================
bool CheckForUpdatesEngine::hasNewerOfficialVersion() const
{
// Return whether we have a newer official version
foreach (const QString &newerVersion, mNewerVersions) {
if (!newerVersion.contains('-'))
return true;
}
return false;
}
//==============================================================================
CheckForUpdatesDialog::CheckForUpdatesDialog(QSettings *pSettings,
const QString &pApplicationDate,
CheckForUpdatesEngine *pEngine,
QWidget *pParent) :
Dialog(pSettings, pParent)
{
// We are not yet initialised
mInitialized = false;
// Set up the GUI
mGui = new Ui::CheckForUpdatesDialog;
mGui->setupUi(this);
// Create/set our engine and check for updates in the former case
if (pEngine) {
mEngine = pEngine;
} else {
mEngine = new CheckForUpdatesEngine(pApplicationDate);
mEngine->check();
}
// Retrieve and set some properties
mGui->checkForUpdatesAtStartupCheckBox->setChecked(mSettings->value(SettingsCheckForUpdatesAtStartup, true).toBool());
mGui->includeSnapshotsCheckBox->setChecked(mSettings->value(SettingsIncludeSnapshots, false).toBool());
// Update our GUI
updateGui();
mInitialized = true;
}
//==============================================================================
CheckForUpdatesDialog::CheckForUpdatesDialog(QSettings *pSettings,
const QString &pApplicationDate,
QWidget *pParent) :
CheckForUpdatesDialog(pSettings, pApplicationDate, 0, pParent)
{
}
//==============================================================================
CheckForUpdatesDialog::CheckForUpdatesDialog(QSettings *pSettings,
CheckForUpdatesEngine *pEngine) :
CheckForUpdatesDialog(pSettings, QString(), pEngine, 0)
{
}
//==============================================================================
CheckForUpdatesDialog::~CheckForUpdatesDialog()
{
// Delete some internal objects
delete mEngine;
// Delete the GUI
delete mGui;
}
//==============================================================================
void CheckForUpdatesDialog::updateGui()
{
// Determine the status of our check
static const QString WhatIsNewUrl = "http://opencor.ws/user/whatIsNew.html?#";
QString versionInformation = tr("<a href=\"%1\">%2 %3</a> is ready for you to <a href=\"http://opencor.ws/downloads/\">download</a>.");
QString snapshotInformation = tr("The <a href=\"%1\">latest snapshot</a> (%2) is ready for you to <a href=\"http://opencor.ws/downloads/\">download</a>.");
if (mEngine->status().isEmpty()) {
if (mGui->includeSnapshotsCheckBox->checkState() == Qt::Checked) {
if (mEngine->hasNewerVersion()) {
// The user is after either an official version or a snapshot of
// OpenCOR, so first look for a snapshot and go for an official
// version, if no snapshot is available
QString version = QString();
foreach (const QString &newerVersion, mEngine->newerVersions()) {
if (newerVersion.contains('-')) {
version = newerVersion;
break;
}
}
if (version.isEmpty())
version = mEngine->newerVersions().first();
if (version.contains('-'))
mGui->statusLabel->setText(snapshotInformation.arg(WhatIsNewUrl+"latest", version));
else
mGui->statusLabel->setText(versionInformation.arg(WhatIsNewUrl+version, qAppName(), version));
} else {
mGui->statusLabel->setText(tr("No newer version or snapshot of %1 is available.").arg(qAppName()));
}
} else if (mEngine->hasNewerOfficialVersion()) {
// The user is only after an official version of OpenCOR, so look
// for the first one available
QString version = QString();
foreach (const QString &newerVersion, mEngine->newerVersions()) {
if (!newerVersion.contains('-')) {
version = newerVersion;
break;
}
}
mGui->statusLabel->setText(versionInformation.arg(WhatIsNewUrl+version, qAppName(), version));
} else {
mGui->statusLabel->setText(tr("No newer version of %1 is available.").arg(qAppName()));
}
} else {
mGui->statusLabel->setText(mEngine->status());
}
}
//==============================================================================
void CheckForUpdatesDialog::on_buttonBox_accepted()
{
// Simply close ourselves
close();
}
//==============================================================================
void CheckForUpdatesDialog::on_statusLabel_linkActivated(const QString &pLink)
{
// Open the link in the user's browser
QDesktopServices::openUrl(pLink);
}
//==============================================================================
void CheckForUpdatesDialog::on_recheckButton_clicked()
{
// Recheck for updates and then update our GUI accordingly
mEngine->check();
updateGui();
}
//==============================================================================
void CheckForUpdatesDialog::on_checkForUpdatesAtStartupCheckBox_toggled(bool pChecked)
{
Q_UNUSED(pChecked);
// Keep track of our property
mSettings->setValue(SettingsCheckForUpdatesAtStartup,
mGui->checkForUpdatesAtStartupCheckBox->isChecked());
}
//==============================================================================
void CheckForUpdatesDialog::on_includeSnapshotsCheckBox_toggled(bool pChecked)
{
Q_UNUSED(pChecked);
// Keep track of our property
mSettings->setValue(SettingsIncludeSnapshots,
mGui->includeSnapshotsCheckBox->isChecked());
updateGui();
}
//==============================================================================
} // namespace OpenCOR
//==============================================================================
// End of file
//==============================================================================