-
Notifications
You must be signed in to change notification settings - Fork 4
/
updates.cpp
134 lines (118 loc) · 3.6 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
126
127
128
129
130
131
132
133
134
#include "updates.h"
#include "ui_updates.h"
#include <QSettings>
#include <QDir>
#include <QProcess>
#include <QJsonDocument>
#include <QJsonObject>
#include <QDateTime>
// ../Update.exe --download=https://markwal.github.io/GpxUi
// ../Update.exe --update=https://markwal.github.io/GpxUi
Updates::Updates(QWidget *parent) :
QDialog(parent),
ui(new Ui::Updates)
{
ui->setupUi(this);
QSettings settings;
ui->cboxAutoUpdate->setChecked(settings.value("auto_update", true).toBool());
pprocess = new QProcess(this);
pprocess->setProcessChannelMode(QProcess::MergedChannels);
connect(pprocess, SIGNAL(readyRead()), SLOT(readStdout()));
connect(pprocess, SIGNAL(finished(int)), SLOT(finished(int)));
checkForUpdates();
}
Updates::~Updates()
{
if (pprocess->state() == QProcess::Running) {
pprocess->terminate();
pprocess->waitForFinished(3000);
}
delete pprocess;
pprocess = NULL;
delete ui;
}
bool Updates::runUpdateExe(QProcess *pprocess, UpdateAction ua)
{
QDir dir(QApplication::instance()->applicationDirPath());
dir.cdUp();
QString sApp = dir.absoluteFilePath("Update.exe");
QStringList slArgs;
if (ua == Check)
slArgs << QLatin1String("--download=https://markwal.github.io/GpxUi");
else
slArgs << QLatin1String("--update=https://markwal.github.io/GpxUi");
if (ua == UpdateDetached) {
return pprocess->startDetached(sApp, slArgs);
}
pprocess->start(sApp, slArgs);
return pprocess->waitForStarted();
}
void Updates::doAction(UpdateAction ua)
{
if (pprocess->state() == QProcess::Running) {
return; // one at a time
}
fUpdating = (ua == Update);
if (!runUpdateExe(pprocess, ua))
ui->textStatus->setText("Unable to check for updates.");
else if (fUpdating)
ui->textStatus->setText("Installing latest version.");
else
ui->textStatus->setText("Checking for available updates.");
}
void Updates::checkForUpdates()
{
doAction(Check);
}
void Updates::readStdout()
{
QByteArray rgb = pprocess->readLine();
if (rgb.isEmpty())
return;
if (QChar(rgb[0]).isDigit()) {
int percent = QString(rgb).toInt();
if (percent >= 0 || percent <= 100)
ui->progressBar->setValue(percent);
}
else if (rgb[0] == '{') {
ui->progressBar->setValue(0); // back to 0 to prepare for "Update Now"
QJsonDocument jd(QJsonDocument::fromJson(rgb));
const QJsonObject &json = jd.object();
ui->textCurrent->setText(json["currentVersion"].toString());
ui->textAvailable->setText(json["futureVersion"].toString());
ui->textStatus->setText("");
}
}
void Updates::autoUpdate()
{
QSettings settings;
if (!settings.value("auto_update", true).toBool())
return;
QDateTime dt = settings.value("last_update_check", QDateTime()).toDateTime();
QDateTime dtNow = QDateTime::currentDateTimeUtc();
if (dt.isNull() || dt.daysTo(dtNow) > 0) {
settings.setValue("last_update_check", dtNow);
if (!dt.isNull()) {
QProcess process;
runUpdateExe(&process, UpdateDetached);
}
}
}
#pragma GCC diagnostic ignored "-Wunused-parameter"
void Updates::finished(int ec)
{
if (fUpdating) {
ui->progressBar->setValue(100);
ui->textStatus->setText("New version installed for the next start of GpxUi.");
}
}
#pragma GCC diagnostic pop
void Updates::on_buttonBox_accepted()
{
QSettings settings;
settings.setValue("auto_update", ui->cboxAutoUpdate->isChecked());
}
void Updates::on_pbUpdateNow_clicked()
{
doAction(Update);
}