-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathupdate.cpp
110 lines (97 loc) · 4.23 KB
/
update.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
#include <QApplication>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QJsonDocument>
#include <QJsonObject>
#include <QMessageBox>
#include <QFile>
#include <QProcess>
#include <QProgressDialog>
#include <QStandardPaths>
#include "include/main_window.h"
#include "include/utils.h"
void MainWindow::checkForUpdates(bool fromAction) {
QNetworkAccessManager* manager = new QNetworkAccessManager(this);
connect(manager, &QNetworkAccessManager::finished, this, [this, fromAction](QNetworkReply* reply) {
this->onUpdateCheckFinished(reply, fromAction);
});
QUrl url("https://screen-me.cloud/update.json");
QNetworkRequest request(url);
manager->get(request);
}
void MainWindow::onUpdateCheckFinished(QNetworkReply* reply, bool fromAction) {
if (reply->error() == QNetworkReply::NoError) {
QByteArray response = reply->readAll();
QJsonDocument jsonDoc = QJsonDocument::fromJson(response);
if (jsonDoc.isObject()) {
QJsonObject jsonObj = jsonDoc.object();
QString latestVersion = jsonObj.value("version").toString();
QString currentVersion = VERSION;
ConfigManager configManager("config.json");
QJsonObject config = configManager.loadConfig();
if (latestVersion > currentVersion && config["skipVersion"] != latestVersion) {
QString downloadUrl = jsonObj.value("download_url").toString();
QMessageBox::StandardButton replyButton;
replyButton = QMessageBox::question(this,
"New update available !",
QString("A new update (%1) has been released. Do you want to download and install now ?").arg(latestVersion),
QMessageBox::Yes | QMessageBox::No | QMessageBox::Ignore);
if (replyButton == QMessageBox::Yes) {
downloadUpdate(downloadUrl);
} else if (replyButton == QMessageBox::Ignore) {
config["skipVersion"] = latestVersion;
configManager.saveConfig(config);
}
}
else {
if (fromAction) {
QMessageBox::information(this,
"Update Check",
"Your software is up to date.");
}
}
}
}
reply->deleteLater();
}
void MainWindow::downloadUpdate(const QString& downloadUrl) {
QNetworkAccessManager* manager = new QNetworkAccessManager(this);
QUrl url(downloadUrl);
if (!url.isValid()) {
qDebug() << "Invalid URL: " << url;
return;
}
QNetworkRequest request(url);
QProgressDialog* progressDialog = new QProgressDialog("Downloading update...", "Cancel", 0, 100, this);
progressDialog->setWindowModality(Qt::WindowModal);
progressDialog->setMinimumDuration(0);
QNetworkReply* networkReply = manager->get(request);
connect(manager, &QNetworkAccessManager::finished, this, &MainWindow::onDownloadFinished);
connect(networkReply, &QNetworkReply::downloadProgress, this, [progressDialog](qint64 bytesReceived, qint64 bytesTotal) {
if (bytesTotal > 0) {
progressDialog->setMaximum(100);
progressDialog->setValue(static_cast<int>((bytesReceived * 100) / bytesTotal));
}
});
connect(progressDialog, &QProgressDialog::canceled, networkReply, &QNetworkReply::abort);
}
void MainWindow::onDownloadFinished(QNetworkReply* reply) {
if (reply->error() == QNetworkReply::NoError) {
QString tempDir = QStandardPaths::writableLocation(QStandardPaths::TempLocation);
QFile file(tempDir + "/update.exe");
if (file.open(QIODevice::WriteOnly)) {
file.write(reply->readAll());
file.close();
QMessageBox::StandardButton replyButton;
replyButton = QMessageBox::question(this,
"Download finished",
"The update has been downloaded. Do you want to install now ?",
QMessageBox::Yes | QMessageBox::No);
if (replyButton == QMessageBox::Yes) {
QProcess::startDetached(tempDir + "/update.exe");
QApplication::exit(0);
}
}
}
reply->deleteLater();
}