#include "updater.h"
Updater
::Updater(QWidget *parent
) : QDialog(parent
), ui
(new Ui
::Updater){
_parent = parent;
params << "-y" << "x" << "*******.Launcher.tmp";
ui->setupUi(this);
ui->pBar->setRange(0, 3);
QTimer::singleShot(0,
this,
SLOT( versionCheck
() ));
}
Updater::~Updater()
{
if( http )
delete http;
delete this;
}
int Updater::getCurrentVersion()
{
int ret = 0;
ret = atoi(stream.readLine().toLatin1().data());
file->close();
}
return ret;
}
void Updater::updateCurrentVersion()
{
//gotta fix this one later
s << (int) getCurrentVersion() + 1;
file->close();
}
}
void Updater::versionCheck()
{
ui->pBar->setValue(1);
ui->pLabel->setText("Checking for updates...");
http->setHost("launcher.*******.pl", 80);
http->get("/version");
connect(http, SIGNAL( done(bool) ), this, SLOT( finishVersionCheck() ));
ui->pBar->setValue(2);
ui->pLabel->setText("Retrieving data from server...");
}
void Updater::finishVersionCheck()
{
ui->pBar->setValue(3);
ui->pLabel->setText("Checking version...");
if( atoi(http->readAll().data()) > getCurrentVersion() ) {
ui->pBar->setValue(0);
ui->pLabel->setText("Downloading...");
http->setHost("launcher.*******.pl", 80);
http->get("/test.7z");
connect(http, SIGNAL( done(bool) ), this, SLOT( finishDownload() ));
connect(http, SIGNAL( dataReadProgress(int, int) ), this, SLOT( updateProgress(int, int) ));
}
else {
_parent->close();
}
}
else {
_parent->show();
close();
}
}
void Updater::finishDownload()
{
if( file->exists() )
file->remove();
QMessageBox::critical(this,
"Updater",
"Failed to open file.");
return;
}
data.setCodec("UTf-8");
data << http->readAll();
file->close();
proc->start("7z.exe", params);
delete proc;
//file->remove();
updateCurrentVersion();
QMessageBox::information(this,
"Updater",
"New version has been downloaded.");
_parent->show();
close();
delete file;
}
void Updater::updateProgress(int done, int total)
{
ui->pBar->setRange(0, total);
ui->pBar->setValue(done);
text.sprintf("Downloading: %d/%d kb (%3.2f%%)", done / 1024, total / 1024, (done / (double) total) * 100.0);
ui->pLabel->setText(text);
}
#include "updater.h"
Updater::Updater(QWidget *parent) : QDialog(parent), ui(new Ui::Updater)
{
_parent = parent;
params << "-y" << "x" << "*******.Launcher.tmp";
ui->setupUi(this);
ui->pBar->setRange(0, 3);
QTimer::singleShot(0, this, SLOT( versionCheck() ));
}
Updater::~Updater()
{
if( http )
delete http;
delete this;
}
int Updater::getCurrentVersion()
{
int ret = 0;
QFile *file = new QFile("./data/version");
if( file->exists() && file->open(QIODevice::ReadOnly | QIODevice::Text) ) {
QTextStream stream(file);
ret = atoi(stream.readLine().toLatin1().data());
file->close();
}
return ret;
}
void Updater::updateCurrentVersion()
{
//gotta fix this one later
QFile *file = new QFile("./data/version");
if( file->exists() && file->open(QIODevice::WriteOnly | QIODevice::Text) ) {
QTextStream s(file);
s << (int) getCurrentVersion() + 1;
file->close();
}
}
void Updater::versionCheck()
{
ui->pBar->setValue(1);
ui->pLabel->setText("Checking for updates...");
http = new QHttp();
http->setHost("launcher.*******.pl", 80);
http->get("/version");
connect(http, SIGNAL( done(bool) ), this, SLOT( finishVersionCheck() ));
ui->pBar->setValue(2);
ui->pLabel->setText("Retrieving data from server...");
}
void Updater::finishVersionCheck()
{
ui->pBar->setValue(3);
ui->pLabel->setText("Checking version...");
if( atoi(http->readAll().data()) > getCurrentVersion() ) {
if( QMessageBox::question(this, "Updater", "There is an update available.\nWould you like to download it now?", QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes ) {
ui->pBar->setValue(0);
ui->pLabel->setText("Downloading...");
http = new QHttp();
http->setHost("launcher.*******.pl", 80);
http->get("/test.7z");
connect(http, SIGNAL( done(bool) ), this, SLOT( finishDownload() ));
connect(http, SIGNAL( dataReadProgress(int, int) ), this, SLOT( updateProgress(int, int) ));
}
else {
_parent->close();
}
}
else {
_parent->show();
close();
}
}
void Updater::finishDownload()
{
QFile *file = new QFile("*******.Launcher.tmp");
if( file->exists() )
file->remove();
if( !file->open(QIODevice::WriteOnly | QIODevice::Text) ) {
QMessageBox::critical(this, "Updater", "Failed to open file.");
return;
}
QTextStream data(file);
data.setCodec("UTf-8");
data << http->readAll();
file->close();
QProcess *proc = new QProcess();
proc->start("7z.exe", params);
delete proc;
//file->remove();
updateCurrentVersion();
QMessageBox::information(this, "Updater", "New version has been downloaded.");
_parent->show();
close();
delete file;
}
void Updater::updateProgress(int done, int total)
{
ui->pBar->setRange(0, total);
ui->pBar->setValue(done);
QString text;
text.sprintf("Downloading: %d/%d kb (%3.2f%%)", done / 1024, total / 1024, (done / (double) total) * 100.0);
ui->pLabel->setText(text);
}
To copy to clipboard, switch view to plain text mode
Bookmarks