qt: Make update check concurrent and added connection timeout

Co-authored-by: PabloMK7 <hackyglitch2@gmail.com>
This commit is contained in:
OpenSauce04 2025-03-11 22:44:41 +00:00
parent a2438e0c6e
commit c468af1dd3
3 changed files with 58 additions and 22 deletions

View File

@ -308,24 +308,6 @@ GMainWindow::GMainWindow(Core::System& system_)
} }
} }
#ifdef ENABLE_QT_UPDATE_CHECKER
if (UISettings::values.check_for_update_on_start) {
const std::optional<std::string> latest_release = UpdateChecker::CheckForUpdate();
if (latest_release && latest_release.value() != Common::g_build_fullname) {
if (QMessageBox::information(
this, tr("Update Available"),
tr("Update %1 for Azahar is available.\nWould you like to download it?")
.arg(QString::fromStdString(latest_release.value())),
QMessageBox::Yes | QMessageBox::Ignore) == QMessageBox::Yes) {
QDesktopServices::openUrl(QUrl(
QString::fromStdString("https://github.com/azahar-emu/azahar/releases/tag/" +
latest_release.value())));
exit(0);
}
}
}
#endif
#ifdef __unix__ #ifdef __unix__
SetGamemodeEnabled(Settings::values.enable_gamemode.GetValue()); SetGamemodeEnabled(Settings::values.enable_gamemode.GetValue());
#endif #endif
@ -406,6 +388,21 @@ GMainWindow::GMainWindow(Core::System& system_)
show(); show();
#ifdef ENABLE_QT_UPDATE_CHECKER
if (UISettings::values.check_for_update_on_start) {
update_future = QtConcurrent::run([]() -> QString {
const std::optional<std::string> latest_release = UpdateChecker::CheckForUpdate();
if (latest_release && latest_release.value() != Common::g_build_fullname) {
return QString::fromStdString(latest_release.value());
}
return QString{};
});
QObject::connect(&update_watcher, &QFutureWatcher<QString>::finished, this,
&GMainWindow::OnEmulatorUpdateAvailable);
update_watcher.setFuture(update_future);
}
#endif
game_list->LoadCompatibilityList(); game_list->LoadCompatibilityList();
game_list->PopulateAsync(UISettings::values.game_dirs); game_list->PopulateAsync(UISettings::values.game_dirs);
@ -3598,6 +3595,28 @@ void GMainWindow::OnMoviePlaybackCompleted() {
QMessageBox::information(this, tr("Playback Completed"), tr("Movie playback completed.")); QMessageBox::information(this, tr("Playback Completed"), tr("Movie playback completed."));
} }
#ifdef ENABLE_QT_UPDATE_CHECKER
void GMainWindow::OnEmulatorUpdateAvailable() {
QString version_string = update_future.result();
if (version_string.isEmpty())
return;
QMessageBox update_prompt(this);
update_prompt.setWindowTitle(tr("Update Available"));
update_prompt.setIcon(QMessageBox::Information);
update_prompt.addButton(QMessageBox::Yes);
update_prompt.addButton(QMessageBox::Ignore);
update_prompt.setText(tr("Update %1 for Azahar is available.\nWould you like to download it?")
.arg(version_string));
update_prompt.exec();
if (update_prompt.button(QMessageBox::Yes) == update_prompt.clickedButton()) {
QDesktopServices::openUrl(
QUrl(QString::fromStdString("https://github.com/azahar-emu/azahar/releases/tag/") +
version_string));
}
}
#endif
void GMainWindow::UpdateWindowTitle() { void GMainWindow::UpdateWindowTitle() {
const QString full_name = QString::fromUtf8(Common::g_build_fullname); const QString full_name = QString::fromUtf8(Common::g_build_fullname);

View File

@ -7,6 +7,13 @@
#include <array> #include <array>
#include <memory> #include <memory>
#include <vector> #include <vector>
#ifdef __unix__
#include <QDBusObjectPath>
#endif
#ifdef ENABLE_QT_UPDATE_CHECKER
#include <QFuture>
#include <QFutureWatcher>
#endif
#include <QMainWindow> #include <QMainWindow>
#include <QPushButton> #include <QPushButton>
#include <QString> #include <QString>
@ -18,10 +25,6 @@
#include "core/core.h" #include "core/core.h"
#include "core/savestate.h" #include "core/savestate.h"
#ifdef __unix__
#include <QDBusObjectPath>
#endif
// Needs to be included at the end due to https://bugreports.qt.io/browse/QTBUG-73263 // Needs to be included at the end due to https://bugreports.qt.io/browse/QTBUG-73263
#include <filesystem> #include <filesystem>
@ -290,6 +293,9 @@ private slots:
void OnDecreaseVolume(); void OnDecreaseVolume();
void OnIncreaseVolume(); void OnIncreaseVolume();
void OnMute(); void OnMute();
#ifdef ENABLE_QT_UPDATE_CHECKER
void OnEmulatorUpdateAvailable();
#endif
private: private:
Q_INVOKABLE void OnMoviePlaybackCompleted(); Q_INVOKABLE void OnMoviePlaybackCompleted();
@ -415,6 +421,12 @@ private:
std::shared_ptr<Camera::QtMultimediaCameraHandlerFactory> qt_cameras; std::shared_ptr<Camera::QtMultimediaCameraHandlerFactory> qt_cameras;
// Prompt shown when update check succeeds
#ifdef ENABLE_QT_UPDATE_CHECKER
QFuture<QString> update_future;
QFutureWatcher<QString> update_watcher;
#endif
#ifdef __unix__ #ifdef __unix__
QDBusObjectPath wake_lock{}; QDBusObjectPath wake_lock{};
#endif #endif

View File

@ -12,8 +12,13 @@
std::optional<std::string> UpdateChecker::CheckForUpdate() { std::optional<std::string> UpdateChecker::CheckForUpdate() {
constexpr auto UPDATE_CHECK_URL = "http://api.github.com"; constexpr auto UPDATE_CHECK_URL = "http://api.github.com";
constexpr auto UPDATE_CHECK_PATH = "/repos/azahar-emu/azahar/releases/latest"; constexpr auto UPDATE_CHECK_PATH = "/repos/azahar-emu/azahar/releases/latest";
constexpr std::size_t UPDATE_CHECK_TIMEOUT_SECONDS = 15;
std::unique_ptr<httplib::Client> client = std::make_unique<httplib::Client>(UPDATE_CHECK_URL); std::unique_ptr<httplib::Client> client = std::make_unique<httplib::Client>(UPDATE_CHECK_URL);
client->set_connection_timeout(UPDATE_CHECK_TIMEOUT_SECONDS);
client->set_read_timeout(UPDATE_CHECK_TIMEOUT_SECONDS);
client->set_write_timeout(UPDATE_CHECK_TIMEOUT_SECONDS);
if (client == nullptr) { if (client == nullptr) {
LOG_ERROR(Frontend, "Invalid URL {}{}", UPDATE_CHECK_URL, UPDATE_CHECK_PATH); LOG_ERROR(Frontend, "Invalid URL {}{}", UPDATE_CHECK_URL, UPDATE_CHECK_PATH);
return {}; return {};