forked from pypt/fervor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfvupdater.cpp
622 lines (490 loc) · 16 KB
/
fvupdater.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
#include "fvupdater.h"
#include "fvupdatewindow.h"
#include "fvupdateconfirmdialog.h"
#include "fvplatform.h"
#include "fvignoredversions.h"
#include "fvavailableupdate.h"
#include <QApplication>
#include <QtNetwork>
#include <QMessageBox>
#include <QDesktopServices>
#include <QDebug>
#ifndef FV_APP_NAME
# error "FV_APP_NAME is undefined (must have been defined by Fervor.pri)"
#endif
#ifndef FV_APP_VERSION
# error "FV_APP_VERSION is undefined (must have been defined by Fervor.pri)"
#endif
#ifdef FV_DEBUG
// Unit tests
# include "fvversioncomparatortest.h"
#endif
FvUpdater* FvUpdater::m_Instance = 0;
FvUpdater* FvUpdater::sharedUpdater()
{
static QMutex mutex;
if (! m_Instance) {
mutex.lock();
if (! m_Instance) {
m_Instance = new FvUpdater;
}
mutex.unlock();
}
return m_Instance;
}
void FvUpdater::drop()
{
static QMutex mutex;
mutex.lock();
delete m_Instance;
m_Instance = 0;
mutex.unlock();
}
FvUpdater::FvUpdater() : QObject(0)
{
m_reply = 0;
m_updaterWindow = 0;
m_updateConfirmationDialog = 0;
m_proposedUpdate = 0;
// Translation mechanism
installTranslator();
#ifdef FV_DEBUG
// Unit tests
FvVersionComparatorTest* test = new FvVersionComparatorTest();
test->runAll();
delete test;
#endif
}
FvUpdater::~FvUpdater()
{
if (m_proposedUpdate) {
delete m_proposedUpdate;
m_proposedUpdate = 0;
}
hideUpdateConfirmationDialog();
hideUpdaterWindow();
}
void FvUpdater::installTranslator()
{
QTranslator translator;
QString locale = QLocale::system().name();
translator.load(QString("fervor_") + locale);
//QTextCodec::setCodecForTr(QTextCodec::codecForName("utf8"));
qApp->installTranslator(&translator);
}
void FvUpdater::showUpdaterWindowUpdatedWithCurrentUpdateProposal()
{
// Destroy window if already exists
hideUpdaterWindow();
// Create a new window
m_updaterWindow = new FvUpdateWindow();
m_updaterWindow->UpdateWindowWithCurrentProposedUpdate();
m_updaterWindow->show();
}
void FvUpdater::hideUpdaterWindow()
{
if (m_updaterWindow) {
if (! m_updaterWindow->close()) {
qWarning() << "Update window didn't close, leaking memory from now on";
}
// not deleting because of Qt::WA_DeleteOnClose
m_updaterWindow = 0;
}
}
void FvUpdater::updaterWindowWasClosed()
{
// (Re-)nullify a pointer to a destroyed QWidget or you're going to have a bad time.
m_updaterWindow = 0;
}
void FvUpdater::showUpdateConfirmationDialogUpdatedWithCurrentUpdateProposal()
{
// Destroy dialog if already exists
hideUpdateConfirmationDialog();
// Create a new window
m_updateConfirmationDialog = new FvUpdateConfirmDialog();
m_updateConfirmationDialog->UpdateWindowWithCurrentProposedUpdate();
m_updateConfirmationDialog->show();
}
void FvUpdater::hideUpdateConfirmationDialog()
{
if (m_updateConfirmationDialog) {
if (! m_updateConfirmationDialog->close()) {
qWarning() << "Update confirmation dialog didn't close, leaking memory from now on";
}
// not deleting because of Qt::WA_DeleteOnClose
m_updateConfirmationDialog = 0;
}
}
void FvUpdater::updateConfirmationDialogWasClosed()
{
// (Re-)nullify a pointer to a destroyed QWidget or you're going to have a bad time.
m_updateConfirmationDialog = 0;
}
void FvUpdater::SetDependencies(QString dep )
{
m_Dependencies = dep;
}
void FvUpdater::SetFeedURL(QUrl feedURL)
{
m_feedURL = feedURL;
}
void FvUpdater::SetFeedURL(QString feedURL)
{
SetFeedURL(QUrl(feedURL));
}
QString FvUpdater::GetFeedURL()
{
return m_feedURL.toString();
}
FvAvailableUpdate* FvUpdater::GetProposedUpdate()
{
return m_proposedUpdate;
}
void FvUpdater::InstallUpdate()
{
qDebug() << "Install update";
showUpdateConfirmationDialogUpdatedWithCurrentUpdateProposal();
}
void FvUpdater::SkipUpdate()
{
qDebug() << "Skip update";
FvAvailableUpdate* proposedUpdate = GetProposedUpdate();
if (! proposedUpdate) {
qWarning() << "Proposed update is NULL (shouldn't be at this point)";
return;
}
// Start ignoring this particular version
FVIgnoredVersions::IgnoreVersion(proposedUpdate->GetEnclosureVersion());
hideUpdaterWindow();
hideUpdateConfirmationDialog(); // if any; shouldn't be shown at this point, but who knows
}
void FvUpdater::RemindMeLater()
{
qDebug() << "Remind me later";
hideUpdaterWindow();
hideUpdateConfirmationDialog(); // if any; shouldn't be shown at this point, but who knows
}
void FvUpdater::UpdateInstallationConfirmed()
{
qDebug() << "Confirm update installation";
FvAvailableUpdate* proposedUpdate = GetProposedUpdate();
if (! proposedUpdate) {
qWarning() << "Proposed update is NULL (shouldn't be at this point)";
return;
}
// Open a link
if (! QDesktopServices::openUrl(proposedUpdate->GetEnclosureUrl())) {
showErrorDialog(tr("Unable to open this link in a browser. Please do it manually."), true);
return;
}
hideUpdaterWindow();
hideUpdateConfirmationDialog();
}
void FvUpdater::UpdateInstallationNotConfirmed()
{
qDebug() << "Do not confirm update installation";
hideUpdateConfirmationDialog(); // if any; shouldn't be shown at this point, but who knows
// leave the "update proposal window" inact
}
bool FvUpdater::CheckForUpdates(bool silentAsMuchAsItCouldGet)
{
if (m_feedURL.isEmpty()) {
qCritical() << "Please set feed URL via setFeedURL() before calling CheckForUpdates().";
return false;
}
m_silentAsMuchAsItCouldGet = silentAsMuchAsItCouldGet;
// Check if application's organization name and domain are set, fail otherwise
// (nowhere to store QSettings to)
if (QApplication::organizationName().isEmpty()) {
qCritical() << "QApplication::organizationName is not set. Please do that.";
return false;
}
if (QApplication::organizationDomain().isEmpty()) {
qCritical() << "QApplication::organizationDomain is not set. Please do that.";
return false;
}
// Set application name / version is not set yet
if (QApplication::applicationName().isEmpty()) {
QString appName = QString::fromUtf8(FV_APP_NAME);
qWarning() << "QApplication::applicationName is not set, setting it to '" << appName << "'";
QApplication::setApplicationName(appName);
}
if (QApplication::applicationVersion().isEmpty()) {
QString appVersion = QString::fromUtf8(FV_APP_VERSION);
qWarning() << "QApplication::applicationVersion is not set, setting it to '" << appVersion << "'";
QApplication::setApplicationVersion(appVersion);
}
cancelDownloadFeed();
m_httpRequestAborted = false;
startDownloadFeed(m_feedURL);
return true;
}
bool FvUpdater::CheckForUpdatesSilent()
{
return CheckForUpdates(true);
}
bool FvUpdater::CheckForUpdatesNotSilent()
{
return CheckForUpdates(false);
}
void FvUpdater::startDownloadFeed(QUrl url)
{
m_xml.clear();
QNetworkRequest request;
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/xml");
#ifdef USE_QT4_FERVOR
request.setHeader((QNetworkRequest::KnownHeaders)7, QApplication::applicationName());
#else
request.setHeader(QNetworkRequest::UserAgentHeader, QApplication::applicationName());
#endif
request.setUrl(url);
m_reply = m_qnam.get(request);
#ifdef FERVOR_IGNORE_SSL_ERRORS
m_reply->ignoreSslErrors();
#endif
connect(m_reply, SIGNAL(readyRead()), this, SLOT(httpFeedReadyRead()));
connect(m_reply, SIGNAL(downloadProgress(qint64, qint64)), this, SLOT(httpFeedUpdateDataReadProgress(qint64, qint64)));
connect(m_reply, SIGNAL(finished()), this, SLOT(httpFeedDownloadFinished()));
}
void FvUpdater::cancelDownloadFeed()
{
if (m_reply) {
m_httpRequestAborted = true;
m_reply->abort();
}
}
void FvUpdater::httpFeedReadyRead()
{
// this slot gets called every time the QNetworkReply has new data.
// We read all of its new data and write it into the file.
// That way we use less RAM than when reading it at the finished()
// signal of the QNetworkReply
m_xml.addData(m_reply->readAll());
}
void FvUpdater::httpFeedUpdateDataReadProgress(qint64 bytesRead,
qint64 totalBytes)
{
Q_UNUSED(bytesRead);
Q_UNUSED(totalBytes);
if (m_httpRequestAborted) {
return;
}
}
void FvUpdater::httpFeedDownloadFinished()
{
if (m_httpRequestAborted) {
m_reply->deleteLater();
return;
}
QVariant redirectionTarget = m_reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
if (m_reply->error()) {
// Error.
showErrorDialog(tr("Feed download failed: %1.").arg(m_reply->errorString()), false);
} else if (! redirectionTarget.isNull()) {
QUrl newUrl = m_feedURL.resolved(redirectionTarget.toUrl());
m_feedURL = newUrl;
m_reply->deleteLater();
startDownloadFeed(m_feedURL);
return;
} else {
// Done.
xmlParseFeed();
}
m_reply->deleteLater();
m_reply = 0;
}
bool FvUpdater::xmlParseFeed()
{
QString currentTag, currentQualifiedTag;
QString xmlTitle, xmlLink, xmlReleaseNotesLink, xmlPubDate, xmlEnclosureUrl, xmlEnclosureDependencies,
xmlEnclosureVersion, xmlEnclosurePlatform, xmlEnclosureType;
unsigned long xmlEnclosureLength = 0;
// Parse
while (! m_xml.atEnd()) {
m_xml.readNext();
if (m_xml.isStartElement()) {
currentTag = m_xml.name().toString();
currentQualifiedTag = m_xml.qualifiedName().toString();
if (m_xml.name() == "item") {
xmlTitle.clear();
xmlLink.clear();
xmlReleaseNotesLink.clear();
xmlPubDate.clear();
xmlEnclosureUrl.clear();
xmlEnclosureVersion.clear();
xmlEnclosurePlatform.clear();
xmlEnclosureLength = 0;
xmlEnclosureType.clear();
} else if (m_xml.name() == "enclosure") {
QXmlStreamAttributes attribs = m_xml.attributes();
if (attribs.hasAttribute("fervor:platform")) {
if (FvPlatform::CurrentlyRunningOnPlatform(attribs.value("fervor:platform").toString().trimmed())) {
xmlEnclosurePlatform = attribs.value("fervor:platform").toString().trimmed();
if (attribs.hasAttribute("fervor:dependencies")) {
xmlEnclosureDependencies = attribs.value("fervor:dependencies").toString().trimmed();
} else {
xmlEnclosureDependencies = "";
}
if( xmlEnclosureDependencies != QString(m_Dependencies) )
{
continue;
}
if (attribs.hasAttribute("url")) {
xmlEnclosureUrl = attribs.value("url").toString().trimmed();
} else {
xmlEnclosureUrl = "";
}
// First check for Sparkle's version, then overwrite with Fervor's version (if any)
if (attribs.hasAttribute("sparkle:version")) {
QString candidateVersion = attribs.value("sparkle:version").toString().trimmed();
if (! candidateVersion.isEmpty()) {
xmlEnclosureVersion = candidateVersion;
}
}
if (attribs.hasAttribute("fervor:version")) {
QString candidateVersion = attribs.value("fervor:version").toString().trimmed();
if (! candidateVersion.isEmpty()) {
xmlEnclosureVersion = candidateVersion;
}
}
if (attribs.hasAttribute("length")) {
xmlEnclosureLength = attribs.value("length").toString().toLong();
} else {
xmlEnclosureLength = 0;
}
if (attribs.hasAttribute("type")) {
xmlEnclosureType = attribs.value("type").toString().trimmed();
} else {
xmlEnclosureType = "";
}
}
}
}
} else if (m_xml.isEndElement()) {
if (m_xml.name() == "item") {
// That's it - we have analyzed a single <item> and we'll stop
// here (because the topmost is the most recent one, and thus
// the newest version.
return searchDownloadedFeedForUpdates(xmlTitle,
xmlLink,
xmlReleaseNotesLink,
xmlPubDate,
xmlEnclosureUrl,
xmlEnclosureVersion,
xmlEnclosurePlatform,
xmlEnclosureLength,
xmlEnclosureType);
}
} else if (m_xml.isCharacters() && ! m_xml.isWhitespace()) {
if (currentTag == "title") {
xmlTitle += m_xml.text().toString().trimmed();
} else if (currentTag == "link") {
xmlLink += m_xml.text().toString().trimmed();
} else if (currentQualifiedTag == "sparkle:releaseNotesLink") {
xmlReleaseNotesLink += m_xml.text().toString().trimmed();
} else if (currentTag == "pubDate") {
xmlPubDate += m_xml.text().toString().trimmed();
}
}
if (m_xml.error() && m_xml.error() != QXmlStreamReader::PrematureEndOfDocumentError) {
showErrorDialog(tr("Feed parsing failed: %1 %2.").arg(QString::number(m_xml.lineNumber()), m_xml.errorString()), false);
return false;
}
}
// No updates were found if we're at this point
// (not a single <item> element found)
showInformationDialog(tr("No updates were found."), false);
return false;
}
bool FvUpdater::searchDownloadedFeedForUpdates(QString xmlTitle,
QString xmlLink,
QString xmlReleaseNotesLink,
QString xmlPubDate,
QString xmlEnclosureUrl,
QString xmlEnclosureVersion,
QString xmlEnclosurePlatform,
unsigned long xmlEnclosureLength,
QString xmlEnclosureType)
{
qDebug() << "Title:" << xmlTitle;
qDebug() << "Link:" << xmlLink;
qDebug() << "Release notes link:" << xmlReleaseNotesLink;
qDebug() << "Pub. date:" << xmlPubDate;
qDebug() << "Enclosure URL:" << xmlEnclosureUrl;
qDebug() << "Enclosure version:" << xmlEnclosureVersion;
qDebug() << "Enclosure platform:" << xmlEnclosurePlatform;
qDebug() << "Enclosure length:" << xmlEnclosureLength;
qDebug() << "Enclosure type:" << xmlEnclosureType;
// Validate
if (xmlReleaseNotesLink.isEmpty()) {
if (xmlLink.isEmpty()) {
showErrorDialog(tr("Feed error: \"release notes\" link is empty"), false);
return false;
} else {
xmlReleaseNotesLink = xmlLink;
}
} else {
xmlLink = xmlReleaseNotesLink;
}
if (! (xmlLink.startsWith("http://") || xmlLink.startsWith("https://"))) {
showErrorDialog(tr("Feed error: invalid \"release notes\" link"), false);
return false;
}
if (xmlEnclosureUrl.isEmpty() || xmlEnclosureVersion.isEmpty() || xmlEnclosurePlatform.isEmpty()) {
showErrorDialog(tr("Feed error: invalid \"enclosure\" with the download link"), false);
return false;
}
// Relevant version?
if (FVIgnoredVersions::VersionIsIgnored(xmlEnclosureVersion)) {
qDebug() << "Version '" << xmlEnclosureVersion << "' is ignored, too old or something like that.";
showInformationDialog(tr("No updates were found."), false);
return true; // Things have succeeded when you think of it.
}
//
// Success! At this point, we have found an update that can be proposed
// to the user.
//
if (m_proposedUpdate) {
delete m_proposedUpdate; m_proposedUpdate = 0;
}
m_proposedUpdate = new FvAvailableUpdate();
m_proposedUpdate->SetTitle(xmlTitle);
m_proposedUpdate->SetReleaseNotesLink(xmlReleaseNotesLink);
m_proposedUpdate->SetPubDate(xmlPubDate);
m_proposedUpdate->SetEnclosureUrl(xmlEnclosureUrl);
m_proposedUpdate->SetEnclosureVersion(xmlEnclosureVersion);
m_proposedUpdate->SetEnclosurePlatform(xmlEnclosurePlatform);
m_proposedUpdate->SetEnclosureLength(xmlEnclosureLength);
m_proposedUpdate->SetEnclosureType(xmlEnclosureType);
// Show "look, there's an update" window
showUpdaterWindowUpdatedWithCurrentUpdateProposal();
return true;
}
void FvUpdater::showErrorDialog(QString message, bool showEvenInSilentMode)
{
if (m_silentAsMuchAsItCouldGet) {
if (! showEvenInSilentMode) {
// Don't show errors in the silent mode
return;
}
}
QMessageBox dlFailedMsgBox;
dlFailedMsgBox.setIcon(QMessageBox::Critical);
dlFailedMsgBox.setText(tr("Error"));
dlFailedMsgBox.setInformativeText(message);
dlFailedMsgBox.exec();
}
void FvUpdater::showInformationDialog(QString message, bool showEvenInSilentMode)
{
if (m_silentAsMuchAsItCouldGet) {
if (! showEvenInSilentMode) {
// Don't show information dialogs in the silent mode
return;
}
}
QMessageBox dlInformationMsgBox;
dlInformationMsgBox.setIcon(QMessageBox::Information);
dlInformationMsgBox.setText(tr("Information"));
dlInformationMsgBox.setInformativeText(message);
dlInformationMsgBox.exec();
}