From ea01c86821892ca65ed97a5086dede7bda052d51 Mon Sep 17 00:00:00 2001 From: Rosalie Wanders Date: Thu, 23 Feb 2023 15:38:17 +0100 Subject: [PATCH] RMG: check for updates once an hour and add 'Check For Updates' to View --- Source/RMG/UserInterface/MainWindow.cpp | 58 +++++++++++++++---- Source/RMG/UserInterface/MainWindow.hpp | 5 +- Source/RMG/UserInterface/MainWindow.ui | 11 ++++ .../icons/black/svg/download-line.svg | 1 + .../icons/white/svg/download-line.svg | 1 + Source/RMG/UserInterface/UIResources.qrc | 2 + 6 files changed, 67 insertions(+), 11 deletions(-) create mode 100644 Source/RMG/UserInterface/Resource/icons/black/svg/download-line.svg create mode 100644 Source/RMG/UserInterface/Resource/icons/white/svg/download-line.svg diff --git a/Source/RMG/UserInterface/MainWindow.cpp b/Source/RMG/UserInterface/MainWindow.cpp index 6d3d3da5..ed33c0a0 100644 --- a/Source/RMG/UserInterface/MainWindow.cpp +++ b/Source/RMG/UserInterface/MainWindow.cpp @@ -71,7 +71,9 @@ bool MainWindow::Init(QApplication* app, bool showUI) this->updateActions(false, false); #ifdef UPDATER - this->checkForUpdates(); + this->checkForUpdates(true, false); +#else + this->action_Help_Update->setVisible(false); #endif // UPDATER this->initializeEmulationThread(); @@ -890,16 +892,44 @@ void MainWindow::connectActionSignals(void) connect(this->action_Help_Github, &QAction::triggered, this, &MainWindow::on_Action_Help_Github); connect(this->action_Help_About, &QAction::triggered, this, &MainWindow::on_Action_Help_About); + connect(this->action_Help_Update, &QAction::triggered, this, &MainWindow::on_Action_Help_Update); } #ifdef UPDATER -void MainWindow::checkForUpdates(void) +void MainWindow::checkForUpdates(bool silent, bool force) { - if (!CoreSettingsGetBoolValue(SettingsID::GUI_CheckForUpdates)) + if (!force && !CoreSettingsGetBoolValue(SettingsID::GUI_CheckForUpdates)) { return; } + // only check for updates on the stable versions unless forced + QString currentVersion = QString::fromStdString(CoreGetVersion()); + if (!force && currentVersion.size() != 6) + { + return; + } + + QString dateTimeFormat = "dd-MM-yyyy_hh:mm"; + QString lastUpdateCheckDateTimeString = QString::fromStdString(CoreSettingsGetStringValue(SettingsID::GUI_LastUpdateCheck)); + QDateTime lastUpdateCheckDateTime = QDateTime::fromString(lastUpdateCheckDateTimeString, dateTimeFormat); + QDateTime currentDateTime = QDateTime::currentDateTime(); + + // only check for updates once every hour unless forced + if (!force && + lastUpdateCheckDateTime.isValid() && + lastUpdateCheckDateTime.addSecs(3600) > currentDateTime) + { + return; + } + + // update last update check date & time + CoreSettingsSetValue(SettingsID::GUI_LastUpdateCheck, currentDateTime.toString(dateTimeFormat).toStdString()); + + // whether or not the update check is silent + this->ui_SilentUpdateCheck = silent; + + // execute update check QNetworkAccessManager* networkAccessManager = new QNetworkAccessManager(this); connect(networkAccessManager, &QNetworkAccessManager::finished, this, &MainWindow::on_networkAccessManager_Finished); networkAccessManager->get(QNetworkRequest(QUrl("https://api.github.com/repos/Rosalie241/RMG/releases/latest"))); @@ -1011,6 +1041,10 @@ void MainWindow::on_networkAccessManager_Finished(QNetworkReply* reply) { if (reply->error()) { + if (!this->ui_SilentUpdateCheck) + { + this->showErrorMessage("Failed to check for updates!", reply->errorString()); + } reply->deleteLater(); return; } @@ -1023,16 +1057,13 @@ void MainWindow::on_networkAccessManager_Finished(QNetworkReply* reply) reply->deleteLater(); - // make sure the current version is valid - // and not a dev version - if (currentVersion.size() != 6) - { - return; - } - // do nothing when versions match if (currentVersion == latestVersion) { + if (!this->ui_SilentUpdateCheck) + { + this->showErrorMessage("You're already on the latest version!"); + } return; } @@ -1518,6 +1549,13 @@ void MainWindow::on_Action_Help_About(void) dialog.exec(); } +void MainWindow::on_Action_Help_Update(void) +{ +#ifdef UPDATER + this->checkForUpdates(false, true); +#endif // UPDATER +} + void MainWindow::on_Emulation_Started(void) { this->logDialog.Clear(); diff --git a/Source/RMG/UserInterface/MainWindow.hpp b/Source/RMG/UserInterface/MainWindow.hpp index 78aef184..0f69c0c4 100644 --- a/Source/RMG/UserInterface/MainWindow.hpp +++ b/Source/RMG/UserInterface/MainWindow.hpp @@ -82,6 +82,8 @@ class MainWindow : public QMainWindow, private Ui::MainWindow bool ui_AddedActions = false; + bool ui_SilentUpdateCheck = false; + int ui_TimerId = 0; int ui_TimerTimeout = 0; @@ -119,7 +121,7 @@ class MainWindow : public QMainWindow, private Ui::MainWindow void removeActions(void); #ifdef UPDATER - void checkForUpdates(void); + void checkForUpdates(bool, bool); #endif // UPDATER protected: void timerEvent(QTimerEvent *) Q_DECL_OVERRIDE; @@ -170,6 +172,7 @@ class MainWindow : public QMainWindow, private Ui::MainWindow void on_Action_Help_Github(void); void on_Action_Help_About(void); + void on_Action_Help_Update(void); void on_Emulation_Started(void); void on_Emulation_Finished(bool); diff --git a/Source/RMG/UserInterface/MainWindow.ui b/Source/RMG/UserInterface/MainWindow.ui index 02709a5b..9e62e8de 100644 --- a/Source/RMG/UserInterface/MainWindow.ui +++ b/Source/RMG/UserInterface/MainWindow.ui @@ -147,6 +147,8 @@ + + @@ -620,6 +622,15 @@ 300% + + + + .. + + + Check For Updates + + diff --git a/Source/RMG/UserInterface/Resource/icons/black/svg/download-line.svg b/Source/RMG/UserInterface/Resource/icons/black/svg/download-line.svg new file mode 100644 index 00000000..1d40a4fd --- /dev/null +++ b/Source/RMG/UserInterface/Resource/icons/black/svg/download-line.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/Source/RMG/UserInterface/Resource/icons/white/svg/download-line.svg b/Source/RMG/UserInterface/Resource/icons/white/svg/download-line.svg new file mode 100644 index 00000000..7f496c7e --- /dev/null +++ b/Source/RMG/UserInterface/Resource/icons/white/svg/download-line.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/Source/RMG/UserInterface/UIResources.qrc b/Source/RMG/UserInterface/UIResources.qrc index eb69a794..17bc14d3 100644 --- a/Source/RMG/UserInterface/UIResources.qrc +++ b/Source/RMG/UserInterface/UIResources.qrc @@ -11,6 +11,7 @@ Resource/icons/black/svg/delete-back-line.svg Resource/icons/black/svg/delete-bin-line.svg Resource/icons/black/svg/door-open-line.svg + Resource/icons/black/svg/download-line.svg Resource/icons/black/svg/file-line.svg Resource/icons/black/svg/file-list-line.svg Resource/icons/black/svg/folder-open-line.svg @@ -40,6 +41,7 @@ Resource/icons/white/svg/delete-back-line.svg Resource/icons/white/svg/delete-bin-line.svg Resource/icons/white/svg/door-open-line.svg + Resource/icons/white/svg/download-line.svg Resource/icons/white/svg/file-line.svg Resource/icons/white/svg/file-list-line.svg Resource/icons/white/svg/folder-open-line.svg