forked from dddaisuke/Caesium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
updater.cpp
executable file
·222 lines (201 loc) · 6.88 KB
/
updater.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
/*******************************************************************************
#
# Copyright (C) 2010-2011 Matteo Paonessa <matteo.paonessa@gmail.com>
#
# This file is part of the Caesium distribution.
#
# Caesium 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.
#
# Caesium 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 Caesium; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301 USA.
#
# Author: Matteo Paonessa <matteo.paonessa@gmail.com>
#
# ******************************************************************************/
#include "updater.h"
#include "ui_updater.h"
#include "global.h"
#include <QtCore>
#include <QtGui>
#include <QtNetwork>
#include <stdlib.h>
#include <stdio.h>
QString c_v = "";
QString fileName;
int d = 0, p = 0;
bool downFinished = false;
Updater::Updater(QWidget *parent) :
QDialog(parent),
ui(new Ui::Updater)
{
ui->setupUi(this);
u_parent = parent;
ui->progressBar->setValue(1);
ui->progressBar->setRange(0,0);
initGetCurrentVersion();
}
Updater::~Updater()
{
delete ui;
}
QString buildToString(QString build)
{
build.insert(1, ".");
build.insert(3, ".");
return build;
}
QString toRealURL(QString u)
{
QStringList splitted;
splitted = u.split('=');
return "http://" + splitted[1] + ".dl.sourceforge.net/project/caesium/" + c_v + "/" + fileName;
}
void Updater::initGetCurrentVersion()
{
QString url = "http://caesium.sourceforge.net/current_version.txt";
QNetworkAccessManager *m_NetworkMngr = new QNetworkAccessManager(this);
m_NetworkMngr->get(QNetworkRequest(url));
connect(m_NetworkMngr, SIGNAL(finished(QNetworkReply*)), this, SLOT(getCurrentVersion(QNetworkReply*)));
}
void Updater::getCurrentVersion(QNetworkReply *reply)
{
if (reply->error() == QNetworkReply::NoError)
{
QString reply_result = reply->readAll();
int c_v_int = reply_result.toInt();
c_v = buildToString(reply_result);
reply->close();
ui->statusLabel->setText(tr("Caesium current version is: ") + "<b>" + c_v + "</b>");
ui->progressBar->setRange(0,1);
if (c_v_int > version_build)
{
ui->startButton->setEnabled(true);
}
ui->versionLabel->setText(ui->versionLabel->text() + "<b> " + version_string + "</b>");
fileName = "caesium-" + c_v + "-win.exe";
getChangelog();
}
else if (reply->error() == QNetworkReply::TimeoutError)
{
ui->statusLabel->setText(tr("ERROR: Connection timed out..."));
ui->progressBar->setRange(0,1);
}
else
{
ui->statusLabel->setText(tr("An error occurred. Please check your internet connection."));
ui->progressBar->setRange(0,1);
}
ui->progressBar->setValue(0);
}
void Updater::on_startButton_clicked()
{
downFinished = false;
ui->startButton->setEnabled(false);
QString url = "http://downloads.sourceforge.net/project/caesium/" + c_v + "/" + fileName;
ui->statusLabel->setText(tr("Contacting server..."));
QNetworkAccessManager *m_NetworkMngr = new QNetworkAccessManager(this);
m_NetworkMngr->get(QNetworkRequest(url));
connect(m_NetworkMngr, SIGNAL(finished(QNetworkReply*)), this, SLOT(getUrl(QNetworkReply*)));
}
void Updater::getUrl(QNetworkReply *reply)
{
if (reply->error() == QNetworkReply::NoError)
{
QString url = toRealURL(reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toString());
initDownload(url);
}
else if (reply->error() == QNetworkReply::TimeoutError)
{
ui->statusLabel->setText(tr("ERROR: Connection timed out..."));
ui->startButton->setEnabled(true);
}
else
{
ui->statusLabel->setText(tr("An error occurred. Please check your internet connection."));
ui->startButton->setEnabled(true);
}
}
void Updater::initDownload(QString url)
{
ui->statusLabel->setText(tr("Downloading ") + fileName + "...");
QNetworkAccessManager *m_NetworkMngr = new QNetworkAccessManager(this);
QNetworkReply *reply= m_NetworkMngr->get(QNetworkRequest(url));
QEventLoop loop;
connect(reply, SIGNAL(finished()),&loop, SLOT(quit()));
connect(reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(updateProgressBar(qint64, qint64)));
loop.exec();
if (reply->error() == QNetworkReply::NoError)
{
file.setFileName(QDir::tempPath() + "/" + fileName);
file.open(QIODevice::WriteOnly);
file.write(reply->readAll());
file.close();
ui->statusLabel->setText(tr("Download completed."));
int r = QMessageBox::information(this, tr("Information"), tr("Caesium will exit now in order to allow the update to run"), QMessageBox::Ok);
if (r == QMessageBox::Ok)
{
downFinished = true;
reply->close();
delete reply;
this->close();
}
}
else if (reply->error() == QNetworkReply::TimeoutError)
{
ui->statusLabel->setText(tr("ERROR: Connection timed out..."));
}
else
{
ui->statusLabel->setText(tr("An error occurred. Please check your internet connection."));
}
ui->startButton->setEnabled(true);
}
void Updater::updateProgressBar(qint64 done, qint64 total)
{
d = done;
ui->progressBar->setMaximum(total);
ui->progressBar->setValue(done);
}
void Updater::closeEvent(QCloseEvent *event)
{
if (downFinished)
{
QProcess *process;
process->startDetached(QDir::tempPath() + "/" + fileName);
u_parent->close();
}
event->setAccepted(true);
}
void Updater::on_closeButton_clicked()
{
this->close();
}
void Updater::getChangelog()
{
QString url = "http://caesium.sourceforge.net/changelog.txt";
QNetworkAccessManager *networkMngr = new QNetworkAccessManager(this);
networkMngr->get(QNetworkRequest(url));
connect(networkMngr, SIGNAL(finished(QNetworkReply*)), this, SLOT(getChangelogHTML(QNetworkReply*)));
}
void Updater::getChangelogHTML(QNetworkReply *reply)
{
if (reply->error() == QNetworkReply::NoError)
{
QString reply_result = reply->readAll();
ui->changelogTextEdit->setText(reply_result);
}
else
{
ui->changelogTextEdit->setText("Failed to retrieve the changelog");
}
reply->close();
}