Skip to content

Commit

Permalink
Merge pull request #6 from Sorok-Dva/feat/update
Browse files Browse the repository at this point in the history
➕ feat: check update
  • Loading branch information
Sorok-Dva authored Nov 17, 2024
2 parents fdb17de + 53f2917 commit 9678d1b
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 2 deletions.
1 change: 1 addition & 0 deletions ScreenMe.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@
</ClCompile>
<ClCompile Include="src\config_manager.cpp" />
<ClCompile Include="src\options_window.cpp" />
<ClCompile Include="update.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="include\config_manager.h" />
Expand Down
3 changes: 3 additions & 0 deletions ScreenMe.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@
<ClCompile Include="src\customTextInput.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="update.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<QtMoc Include="include\options_window.h">
Expand Down
7 changes: 7 additions & 0 deletions include/main_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <QWidget>
#include <QMainWindow>
#include <QNetworkReply>
#include <QJsonObject>
#include <QPointer>
#include "screenshotdisplay.h"
Expand All @@ -19,6 +20,12 @@ public slots:
void handleHotkeyActivated(size_t id);
void handleScreenshotClosed();
void reloadHotkeys();
void onUpdateCheckFinished(QNetworkReply* reply);
void downloadUpdate(const QString& downloadUrl);
void onDownloadFinished(QNetworkReply* reply);

public:
void checkForUpdates();

signals:
void screenshotClosed();
Expand Down
3 changes: 2 additions & 1 deletion include/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ void setAutoStart(bool enable);


//const QString SCREEN_ME_HOST = "http://127.0.0.1:3001";
const QString SCREEN_ME_HOST = "https://screen-me.cloud";
const QString SCREEN_ME_HOST = "https://screen-me.cloud";
const QString VERSION = "1.2.0";
13 changes: 12 additions & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
using namespace std;

#define SHARED_MEM_KEY "ScreenMeSharedMemory"
const QString VERSION = "1.2.0";

static void showAboutDialog() {
QMessageBox aboutBox;
Expand Down Expand Up @@ -79,13 +78,17 @@ int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, char*, int nShowCmd)
setAutoStart(false);
}

MainWindow w(&configManager);
w.checkForUpdates();

QAction loginAction("Login to ScreenMe", &trayMenu);
QAction takeScreenshotAction("Take Screenshot", &trayMenu);
QAction takeFullscreenScreenshotAction("Take Fullscreen Screenshot", &trayMenu);
QAction aboutAction("About...", &trayMenu);
QAction helpAction("❓Help", &trayMenu);
QAction reportBugAction("🛠️ Report a bug", &trayMenu);
QAction optionsAction("Options", &trayMenu);
QAction checkUpdateAction("Check for update", &trayMenu);
QAction exitAction("Exit", &trayMenu);

QAction myGalleryAction("My Gallery", &trayMenu);
Expand All @@ -111,6 +114,7 @@ int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, char*, int nShowCmd)
trayMenu.addAction(&reportBugAction);
trayMenu.addSeparator();
trayMenu.addAction(&optionsAction);
trayMenu.addAction(&checkUpdateAction);
trayMenu.addAction(&exitAction);
trayIcon.setContextMenu(&trayMenu);
trayIcon.setToolTip("Press the configured key combination to take a screenshot");
Expand Down Expand Up @@ -185,6 +189,13 @@ int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, char*, int nShowCmd)
optionsWindow.exec();
});

QObject::connect(&checkUpdateAction, &QAction::triggered, [&]() {
config["skipVersion"] = "";

configManager.saveConfig(config);
w.checkForUpdates();
});

trayIcon.show();

QObject::connect(&trayIcon, &QSystemTrayIcon::activated, [&](QSystemTrayIcon::ActivationReason reason) {
Expand Down
1 change: 1 addition & 0 deletions src/config_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ ConfigManager::ConfigManager(const QString& configPath) : configPath(configPath)
defaultConfig["image_quality"] = 90;
defaultConfig["default_save_folder"] = QDir::homePath() + "/Pictures/ScreenMe";
defaultConfig["start_with_system"] = true;
defaultConfig["skipVersion"] = "";
saveConfig(defaultConfig);
}
}
Expand Down
99 changes: 99 additions & 0 deletions update.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#include <QApplication>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QJsonDocument>
#include <QJsonObject>
#include <QMessageBox>
#include <QFile>
#include <QProcess>
#include <QProgressDialog>
#include "include/main_window.h"
#include "include/utils.h"

void MainWindow::checkForUpdates() {
QNetworkAccessManager* manager = new QNetworkAccessManager(this);
connect(manager, &QNetworkAccessManager::finished, this, &MainWindow::onUpdateCheckFinished);

QUrl url("https://screen-me.cloud/update.json");
QNetworkRequest request(url);
manager->get(request);
}

void MainWindow::onUpdateCheckFinished(QNetworkReply* reply) {
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);
}
}
}
}
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) {
QFile file("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("update.exe");
QApplication::exit(0);
}
}
}
reply->deleteLater();
}

0 comments on commit 9678d1b

Please sign in to comment.