forked from enricoros/fotowall
/
VersionCheckDialog.cpp
104 lines (92 loc) · 3.53 KB
/
VersionCheckDialog.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
/***************************************************************************
* *
* This file is part of the Fotowall project, *
* http://code.google.com/p/fotowall *
* *
* Copyright (C) 2009 by Enrico Ros <enrico.ros@gmail.com> *
* *
* This program 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 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "VersionCheckDialog.h"
#include <QCoreApplication>
#include <QDesktopServices>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QPushButton>
#include <QTimer>
#include <QUrl>
#include "ui_VersionCheckDialog.h"
#define MAGIC_TOKEN "Last version is "
#define DOWNLOADS_URL "http://code.google.com/p/fotowall/downloads/list"
#define NETWORK_TIMEOUT 10000
VersionCheckDialog::VersionCheckDialog(QWidget * parent)
: QDialog(parent)
, ui(new Ui::VersionCheckDialog)
, m_nam(new QNetworkAccessManager)
{
// inject ui
ui->setupUi(this);
ui->currVersion->setText(QCoreApplication::applicationVersion());
ui->nextVersion->setText(tr("checking"));
// start the network request
QNetworkRequest request(QUrl("http://code.google.com/p/fotowall/"));
QNetworkReply * reply = m_nam->get(request);
connect(reply, SIGNAL(finished()), this, SLOT(slotGotReply()));
QTimer::singleShot(NETWORK_TIMEOUT, this, SLOT(slotTimeOut()));
}
VersionCheckDialog::~VersionCheckDialog()
{
delete m_nam;
delete ui;
}
void VersionCheckDialog::slotGotReply()
{
if (!m_nam)
return;
QNetworkReply * reply = static_cast<QNetworkReply *>(sender());
QString replyStr = reply->readAll();
ui->progressBar->hide();
reply->deleteLater();
if (reply->error() != QNetworkReply::NoError) {
ui->nextVersion->setText("network error");
reply->deleteLater();
return;
}
QString magic(MAGIC_TOKEN);
int pos1 = replyStr.indexOf(magic) + magic.length();
if (pos1 == -1 || pos1 >= (replyStr.length() - 1)) {
ui->nextVersion->setText("page error (1)");
return;
}
int pos2 = replyStr.indexOf("<", pos1);
if (pos2 <= pos1 || pos2 > pos1 + 20) {
ui->nextVersion->setText("page error (2)");
return;
}
// show version
QString version = replyStr.mid(pos1, pos2 - pos1);
ui->nextVersion->setText(version);
// show download button
if (version.compare(QCoreApplication::applicationVersion(), Qt::CaseInsensitive)) {
QPushButton * dlButton = ui->buttonBox->addButton(tr("Download"), QDialogButtonBox::NoRole);
connect(dlButton, SIGNAL(clicked()), this, SLOT(slotDownload()));
}
}
void VersionCheckDialog::slotTimeOut()
{
if (ui->progressBar->isVisible()) {
m_nam->deleteLater();
m_nam = 0;
ui->progressBar->hide();
ui->nextVersion->setText(tr("network timeout"));
}
}
void VersionCheckDialog::slotDownload()
{
QDesktopServices::openUrl(QUrl(DOWNLOADS_URL));
}