Skip to content

Commit 9678d1b

Browse files
authored
Merge pull request #6 from Sorok-Dva/feat/update
➕ feat: check update
2 parents fdb17de + 53f2917 commit 9678d1b

File tree

7 files changed

+125
-2
lines changed

7 files changed

+125
-2
lines changed

ScreenMe.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@
130130
</ClCompile>
131131
<ClCompile Include="src\config_manager.cpp" />
132132
<ClCompile Include="src\options_window.cpp" />
133+
<ClCompile Include="update.cpp" />
133134
</ItemGroup>
134135
<ItemGroup>
135136
<ClInclude Include="include\config_manager.h" />

ScreenMe.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@
6868
<ClCompile Include="src\customTextInput.cpp">
6969
<Filter>Source Files</Filter>
7070
</ClCompile>
71+
<ClCompile Include="update.cpp">
72+
<Filter>Source Files</Filter>
73+
</ClCompile>
7174
</ItemGroup>
7275
<ItemGroup>
7376
<QtMoc Include="include\options_window.h">

include/main_window.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <QWidget>
44
#include <QMainWindow>
5+
#include <QNetworkReply>
56
#include <QJsonObject>
67
#include <QPointer>
78
#include "screenshotdisplay.h"
@@ -19,6 +20,12 @@ public slots:
1920
void handleHotkeyActivated(size_t id);
2021
void handleScreenshotClosed();
2122
void reloadHotkeys();
23+
void onUpdateCheckFinished(QNetworkReply* reply);
24+
void downloadUpdate(const QString& downloadUrl);
25+
void onDownloadFinished(QNetworkReply* reply);
26+
27+
public:
28+
void checkForUpdates();
2229

2330
signals:
2431
void screenshotClosed();

include/utils.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ void setAutoStart(bool enable);
1515

1616

1717
//const QString SCREEN_ME_HOST = "http://127.0.0.1:3001";
18-
const QString SCREEN_ME_HOST = "https://screen-me.cloud";
18+
const QString SCREEN_ME_HOST = "https://screen-me.cloud";
19+
const QString VERSION = "1.2.0";

main.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
using namespace std;
2424

2525
#define SHARED_MEM_KEY "ScreenMeSharedMemory"
26-
const QString VERSION = "1.2.0";
2726

2827
static void showAboutDialog() {
2928
QMessageBox aboutBox;
@@ -79,13 +78,17 @@ int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, char*, int nShowCmd)
7978
setAutoStart(false);
8079
}
8180

81+
MainWindow w(&configManager);
82+
w.checkForUpdates();
83+
8284
QAction loginAction("Login to ScreenMe", &trayMenu);
8385
QAction takeScreenshotAction("Take Screenshot", &trayMenu);
8486
QAction takeFullscreenScreenshotAction("Take Fullscreen Screenshot", &trayMenu);
8587
QAction aboutAction("About...", &trayMenu);
8688
QAction helpAction("❓Help", &trayMenu);
8789
QAction reportBugAction("🛠️ Report a bug", &trayMenu);
8890
QAction optionsAction("Options", &trayMenu);
91+
QAction checkUpdateAction("Check for update", &trayMenu);
8992
QAction exitAction("Exit", &trayMenu);
9093

9194
QAction myGalleryAction("My Gallery", &trayMenu);
@@ -111,6 +114,7 @@ int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, char*, int nShowCmd)
111114
trayMenu.addAction(&reportBugAction);
112115
trayMenu.addSeparator();
113116
trayMenu.addAction(&optionsAction);
117+
trayMenu.addAction(&checkUpdateAction);
114118
trayMenu.addAction(&exitAction);
115119
trayIcon.setContextMenu(&trayMenu);
116120
trayIcon.setToolTip("Press the configured key combination to take a screenshot");
@@ -185,6 +189,13 @@ int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, char*, int nShowCmd)
185189
optionsWindow.exec();
186190
});
187191

192+
QObject::connect(&checkUpdateAction, &QAction::triggered, [&]() {
193+
config["skipVersion"] = "";
194+
195+
configManager.saveConfig(config);
196+
w.checkForUpdates();
197+
});
198+
188199
trayIcon.show();
189200

190201
QObject::connect(&trayIcon, &QSystemTrayIcon::activated, [&](QSystemTrayIcon::ActivationReason reason) {

src/config_manager.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ ConfigManager::ConfigManager(const QString& configPath) : configPath(configPath)
1616
defaultConfig["image_quality"] = 90;
1717
defaultConfig["default_save_folder"] = QDir::homePath() + "/Pictures/ScreenMe";
1818
defaultConfig["start_with_system"] = true;
19+
defaultConfig["skipVersion"] = "";
1920
saveConfig(defaultConfig);
2021
}
2122
}

update.cpp

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#include <QApplication>
2+
#include <QNetworkAccessManager>
3+
#include <QNetworkReply>
4+
#include <QJsonDocument>
5+
#include <QJsonObject>
6+
#include <QMessageBox>
7+
#include <QFile>
8+
#include <QProcess>
9+
#include <QProgressDialog>
10+
#include "include/main_window.h"
11+
#include "include/utils.h"
12+
13+
void MainWindow::checkForUpdates() {
14+
QNetworkAccessManager* manager = new QNetworkAccessManager(this);
15+
connect(manager, &QNetworkAccessManager::finished, this, &MainWindow::onUpdateCheckFinished);
16+
17+
QUrl url("https://screen-me.cloud/update.json");
18+
QNetworkRequest request(url);
19+
manager->get(request);
20+
}
21+
22+
void MainWindow::onUpdateCheckFinished(QNetworkReply* reply) {
23+
if (reply->error() == QNetworkReply::NoError) {
24+
QByteArray response = reply->readAll();
25+
QJsonDocument jsonDoc = QJsonDocument::fromJson(response);
26+
if (jsonDoc.isObject()) {
27+
QJsonObject jsonObj = jsonDoc.object();
28+
QString latestVersion = jsonObj.value("version").toString();
29+
QString currentVersion = VERSION;
30+
31+
ConfigManager configManager("config.json");
32+
QJsonObject config = configManager.loadConfig();
33+
34+
if (latestVersion > currentVersion && config["skipVersion"] != latestVersion) {
35+
QString downloadUrl = jsonObj.value("download_url").toString();
36+
37+
QMessageBox::StandardButton replyButton;
38+
replyButton = QMessageBox::question(this,
39+
"New update available !",
40+
QString("A new update (%1) has been released. Do you want to download and install now ?").arg(latestVersion),
41+
QMessageBox::Yes | QMessageBox::No | QMessageBox::Ignore);
42+
if (replyButton == QMessageBox::Yes) {
43+
downloadUpdate(downloadUrl);
44+
} else if (replyButton == QMessageBox::Ignore) {
45+
config["skipVersion"] = latestVersion;
46+
47+
configManager.saveConfig(config);
48+
}
49+
}
50+
}
51+
}
52+
reply->deleteLater();
53+
}
54+
55+
void MainWindow::downloadUpdate(const QString& downloadUrl) {
56+
QNetworkAccessManager* manager = new QNetworkAccessManager(this);
57+
QUrl url(downloadUrl);
58+
if (!url.isValid()) {
59+
qDebug() << "Invalid URL: " << url;
60+
return;
61+
}
62+
QNetworkRequest request(url);
63+
64+
QProgressDialog* progressDialog = new QProgressDialog("Downloading update...", "Cancel", 0, 100, this);
65+
progressDialog->setWindowModality(Qt::WindowModal);
66+
progressDialog->setMinimumDuration(0);
67+
68+
QNetworkReply* networkReply = manager->get(request);
69+
70+
connect(manager, &QNetworkAccessManager::finished, this, &MainWindow::onDownloadFinished);
71+
connect(networkReply, &QNetworkReply::downloadProgress, this, [progressDialog](qint64 bytesReceived, qint64 bytesTotal) {
72+
if (bytesTotal > 0) {
73+
progressDialog->setMaximum(100);
74+
progressDialog->setValue(static_cast<int>((bytesReceived * 100) / bytesTotal));
75+
}
76+
});
77+
connect(progressDialog, &QProgressDialog::canceled, networkReply, &QNetworkReply::abort);
78+
}
79+
80+
void MainWindow::onDownloadFinished(QNetworkReply* reply) {
81+
if (reply->error() == QNetworkReply::NoError) {
82+
QFile file("update.exe");
83+
if (file.open(QIODevice::WriteOnly)) {
84+
file.write(reply->readAll());
85+
file.close();
86+
87+
QMessageBox::StandardButton replyButton;
88+
replyButton = QMessageBox::question(this,
89+
"Download finished",
90+
"The update has been downloaded. Do you want to install now ?",
91+
QMessageBox::Yes | QMessageBox::No);
92+
if (replyButton == QMessageBox::Yes) {
93+
QProcess::startDetached("update.exe");
94+
QApplication::exit(0);
95+
}
96+
}
97+
}
98+
reply->deleteLater();
99+
}

0 commit comments

Comments
 (0)