forked from docsteer/sacnview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathversioncheck.cpp
266 lines (224 loc) · 8.67 KB
/
versioncheck.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#include "versioncheck.h"
#include "ui_newversiondialog.h"
#include <QStandardPaths>
#include <QProcess>
#ifdef Q_OS_WIN
static const QString OS_FILE_IDENTIFIER = ".exe";
static const QString OS_EXE_HANDLER = "";
#endif
#ifdef Q_OS_MACOS
static const QString OS_FILE_IDENTIFIER = ".dmg";
static const QString OS_EXE_HANDLER = "open";
#endif
#ifdef Q_OS_LINUX
static const QString OS_FILE_IDENTIFIER = ".deb";
static const QString OS_EXE_HANDLER = "xdg-open";
#endif
// Enable to force the version on Github to be considered newer
//#define DEBUG_VERSIONCHECK
NewVersionDialog::NewVersionDialog(QWidget *parent) : QDialog(parent), ui(new Ui::NewVersionDialog)
{
ui->setupUi(this);
ui->stackedWidget->setCurrentIndex(0);
#if (QT_VERSION >= QT_VERSION_CHECK(5, 9, 0))
setWindowFlag(Qt::WindowContextHelpButtonHint, false);
#endif
connect(ui->btnLater, SIGNAL(pressed()), this, SLOT(reject()));
m_manager = new QNetworkAccessManager(this);
}
void NewVersionDialog::setNewVersionNumber(const QString &version)
{
ui->lblVersion->setText(tr("sACNView %1 is available (you have %2). Would you like to download it now?")
.arg(version)
.arg(VERSION));
m_newVersion = version;
}
void NewVersionDialog::setDownloadUrl(const QString &url)
{
m_dlUrl = url;
}
void NewVersionDialog::on_btnInstall_pressed()
{
ui->lblDownloadInfo->setText(tr("Downloading %1 from %2")
.arg(m_newVersion)
.arg(m_dlUrl));
ui->stackedWidget->setCurrentIndex(1);
this->adjustSize();
ui->btnExitInstall->setEnabled(false);
ui->progressBar->setValue(0);
m_storagePath = QStandardPaths::writableLocation(QStandardPaths::DownloadLocation);
QUrl url(m_dlUrl);
m_storagePath += QString("/");
m_storagePath += url.path().split(QChar('/')).last();
doDownload(url);
}
void NewVersionDialog::doDownload(const QUrl &url)
{
// Begin the download
if(m_dlFile.isOpen())
m_dlFile.close();
m_dlFile.setFileName(m_storagePath);
bool ok = m_dlFile.open(QIODevice::WriteOnly);
if(!ok)
{
ui->lblDownloadInfo->setText(
tr("Could not open file %1 to save - please download and install manually").arg(m_storagePath));
return;
}
QNetworkReply *reply = m_manager->get(QNetworkRequest(url));
connect(reply, SIGNAL(finished()), this, SLOT(finished()));
connect(reply, SIGNAL(readyRead()), this, SLOT(dataReadyRead()));
connect(reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(progress(qint64,qint64)));
}
void NewVersionDialog::progress(qint64 bytes, qint64 total)
{
if (!total) return;
auto percent = static_cast<int>((bytes * 100) / total);
ui->progressBar->setMaximum(100);
ui->progressBar->setMinimum(0);
ui->progressBar->setValue(percent);
#if (QT_VERSION < QT_VERSION_CHECK(5, 10, 0))
auto strProgress = tr("%1 of %2 bytes").arg(bytes).arg(total);
#else
auto strProgress = tr("%1 of %2")
.arg(this->locale().formattedDataSize(bytes))
.arg(this->locale().formattedDataSize(total));
#endif
ui->progressBar->setTextVisible(true);
ui->progressBar->setFormat(strProgress);
ui->progressBar->setAlignment(Qt::AlignCenter);
}
void NewVersionDialog::finished()
{
QNetworkReply *reply = dynamic_cast<QNetworkReply *>(sender());
if(!reply) return;
// Github redirects to a CDN, so we check for that:
QVariant possibleRedirectUrl =
reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
if(!possibleRedirectUrl.toString().isEmpty())
{
ui->progressBar->setValue(0);
QUrl redirUrl(possibleRedirectUrl.toString());
doDownload(redirUrl);
return;
}
if(reply->error() == QNetworkReply::NoError)
{
ui->btnCancelDl->setEnabled(false);
ui->btnExitInstall->setEnabled(true);
if(m_dlFile.isOpen())
m_dlFile.close();
}
else
{
ui->lblDownloadInfo->setText(tr("Error downloading : please try again"));
}
}
void NewVersionDialog::dataReadyRead()
{
QNetworkReply *reply = dynamic_cast<QNetworkReply *>(sender());
if(!reply) return;
QByteArray data = reply->readAll();
m_dlFile.write(data);
}
void NewVersionDialog::setNewVersionInfo(const QString &info)
{
ui->teReleaseNotes->clear();
ui->teReleaseNotes->setPlainText(info);
}
void NewVersionDialog::on_btnExitInstall_pressed()
{
bool ok = false;
if (!OS_EXE_HANDLER.isEmpty())
ok = QProcess::startDetached(OS_EXE_HANDLER, QStringList(m_storagePath));
else
ok = QProcess::startDetached(m_storagePath);
if(!ok)
QMessageBox::warning(this, tr("Couldn't Run Installer"), tr("Unable to run installer - please run %1").arg(m_storagePath));
qApp->exit();
}
void NewVersionDialog::on_btnCancelDl_pressed()
{
reject();
}
VersionCheck::VersionCheck(QObject *parent):
QObject(parent)
{
manager = new QNetworkAccessManager(this);
QNetworkRequest request;
connect(manager, SIGNAL(finished(QNetworkReply*)),
this, SLOT(replyFinished(QNetworkReply*)));
request.setRawHeader("User-Agent", QString("%1 %2").arg(APP_NAME).arg(VERSION).toUtf8());
request.setRawHeader("Accept", "application/vnd.github.v3.raw+json");
request.setUrl(QUrl("https://api.github.com/repos/docsteer/sacnview/releases"));
manager->get(request);
}
void VersionCheck::replyFinished (QNetworkReply *reply)
{
QString downloadUrl;
if(reply->error())
{
qDebug() << "[Version check] Unable to check for updated version: " << reply->errorString();
}
else
{
QJsonDocument jDoc = QJsonDocument::fromJson(reply->readAll());
if (jDoc.isNull()) {
qDebug() << "[Version check] Response not valid JSON";
} else {
QLocale locale("US");
QDate myDate = locale.toDate(
QString("%1 %2 %3").arg(GIT_DATE_DATE).arg(GIT_DATE_MONTH).arg(GIT_DATE_YEAR),
QString("dd MMM yyyy")
);
qDebug() << "[Version check] My version:" << VERSION << myDate.toString() << "Pre Release - " << PRERELEASE;
QJsonArray jArray = jDoc.array();
foreach (const QJsonValue &jValue, jArray) {
QJsonObject jObj = jValue.toObject();
QDate objectDate = locale.toDateTime(
jObj["published_at"].toString(),
QString("yyyy-MM-dd'T'hh':'mm':'ss'Z'")
).date();
QString remote_version = jObj["tag_name"].toString();
bool remote_is_prerelease = jObj["prerelease"].toBool();
#ifdef DEBUG_VERSIONCHECK
objectDate = QDate(2199, 12, 12);
remote_version = QString("AwesomeVersion");
#endif
qDebug() << "[Version check] Remote version:" << remote_version << objectDate.toString() << "Pre Release - " << remote_is_prerelease ;
// Find the appropriate download URL for the platform
QJsonArray assetsArray = jObj["assets"].toArray();
foreach(const QJsonValue &asset, assetsArray)
{
QJsonObject oAsset = asset.toObject();
if(oAsset.contains("browser_download_url"))
{
QString url = oAsset.value("browser_download_url").toString();
if(url.contains(OS_FILE_IDENTIFIER))
{
downloadUrl = url;
}
}
}
if (myDate.isValid() && objectDate.isValid()) {
if (VERSION != remote_version) {
if (objectDate > myDate) {
if (PRERELEASE || !remote_is_prerelease) { // If prerelease, always upgrade; if not, only upgrade to non-prerelease
// Newer!
qDebug() << "[Version check] Remote version" << jObj["tag_name"].toString() << "is newer!";
// Tell the user
NewVersionDialog dlg;
dlg.setNewVersionNumber(jObj["tag_name"].toString());
dlg.setNewVersionInfo(jObj["body"].toString());
dlg.setDownloadUrl(downloadUrl);
dlg.exec();
return;
}
}
}
}
}
}
}
reply->deleteLater();
}