-
Notifications
You must be signed in to change notification settings - Fork 6
/
updates.cpp
125 lines (103 loc) · 3.76 KB
/
updates.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
#include "updates.h"
#include "ui_updates.h"
Updates::Updates(QWidget *parent) :
QDialog(parent),
ui(new Ui::Updates)
{
ui->setupUi(this);
}
Updates::~Updates()
{
delete ui;
}
void Updates::on_pushButton_clicked()
{
ui->pushButton->setEnabled(false);
ui->pushButton->setText(tr("Cheking..."));
QNetworkAccessManager *manager = new QNetworkAccessManager();
connect(manager, SIGNAL ( finished ( QNetworkReply* )),
this, SLOT(xmlDownloadFinished(QNetworkReply*)));
manager->get(QNetworkRequest(QUrl("http://linuxscout.users.sourceforge.net/nibras/version.xml")));
}
void Updates::xmlDownloadFinished(QNetworkReply *reply)
{
if(reply->error() != QNetworkReply::NoError )
{
QMessageBox::warning(this, tr("Error"), tr("Server not found, check your internet connection!\n"));
ui->pushButton->setText(tr("Check again"));
ui->pushButton->setEnabled(true);
return;
}
QFile file("version.xml");
if (!file.open(QIODevice::WriteOnly))
return;
file.write(reply->readAll());
file.close();
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QMessageBox::critical(this,
tr("Error parsing the XML file."),
tr("Error parsing the XML file."));
ui->pushButton->setText(tr("Check again"));
ui->pushButton->setEnabled(true);
return;
}
QXmlStreamReader xmlFile(&file);
QString version;
QString whatsNew = "";
while(!xmlFile.atEnd() && !xmlFile.hasError())
{
QXmlStreamReader::TokenType token = xmlFile.readNext();
if(token == QXmlStreamReader::StartDocument) {
continue;
}
if(token == QXmlStreamReader::StartElement) {
if(xmlFile.name() == "Nibras") {
continue;
}
if(xmlFile.name() == "Version") {
version = xmlFile.readElementText();
continue;
}
if(xmlFile.name() == "WhatsNew") {
continue;
}
if(xmlFile.name() == "feature"){
whatsNew += "- " + xmlFile.readElementText() + "\r\n";
}
}
}
if(xmlFile.hasError()) {
QMessageBox::critical(this,
tr("Error"), xmlFile.errorString(),
QMessageBox::Ok);
ui->pushButton->setText(tr("Check again"));
ui->pushButton->setEnabled(true);
return;
}
if(version.toInt() > 1)
{
QMessageBox message;
message.setWindowTitle(tr("Update available!"));
message.setIcon(QMessageBox::Information);
message.setStandardButtons(QMessageBox::Yes | QMessageBox::Cancel);
message.setDefaultButton(QMessageBox::Yes);
message.setText(tr("New version (v %1) is available!\n").arg(version));
message.setInformativeText(tr("Would you like to download the new version?"));
QString details = tr("What's New: \n");
details += whatsNew;
message.setDetailedText(details);
int response = message.exec();
ui->pushButton->setText(tr("Check again"));
ui->pushButton->setEnabled(true);
if(response == QMessageBox::Yes)
{
QDesktopServices::openUrl(QUrl("http://nibras.sourceforge.net/"));
}
return;
}
else QMessageBox::information(this, tr("No update available"), tr("You have the latest version!"
"\nNo update available for now."));
file.remove();
ui->pushButton->setText(tr("Check updates..."));
ui->pushButton->setEnabled(true);
}