From b05464749c618b83167e23f8b7046c1d29cbd9a3 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Mon, 27 Feb 2023 14:26:43 +0100 Subject: opentrack: add back the updater --- opentrack/main-window.cpp | 5 ++ opentrack/main-window.hpp | 4 ++ opentrack/software-update-dialog.cpp | 66 ++++++++++++++++++++ opentrack/software-update-dialog.hpp | 34 ++++++++++ opentrack/software-update.ui | 118 +++++++++++++++++++++++++++++++++++ 5 files changed, 227 insertions(+) create mode 100644 opentrack/software-update-dialog.cpp create mode 100644 opentrack/software-update-dialog.hpp create mode 100644 opentrack/software-update.ui diff --git a/opentrack/main-window.cpp b/opentrack/main-window.cpp index ab8255a8..896247c4 100644 --- a/opentrack/main-window.cpp +++ b/opentrack/main-window.cpp @@ -18,6 +18,7 @@ #include "compat/math.hpp" #include "compat/sysexits.hpp" #include "opentrack/defs.hpp" +#include "software-update-dialog.hpp" #include #include @@ -76,6 +77,10 @@ main_window::main_window() : State(OPENTRACK_BASE_PATH + OPENTRACK_LIBRARY_PATH) #elif defined UI_COMPACT_VIDEO_FEED connect(ui.preview_checkbox, &QCheckBox::toggled, this, &main_window::toggle_video_preview); #endif + + updater = std::make_unique(this); + updater->maybe_show_dialog(); + } void main_window::init_shortcuts() diff --git a/opentrack/main-window.hpp b/opentrack/main-window.hpp index 322c74c5..c0724a35 100644 --- a/opentrack/main-window.hpp +++ b/opentrack/main-window.hpp @@ -35,6 +35,8 @@ #include "ui_main-window.h" +class update_query; + class main_window final : public QMainWindow, private State { Q_DECLARE_TR_FUNCTIONS(main_window) @@ -84,6 +86,8 @@ class main_window final : public QMainWindow, private State qt_signal toggle_tracker { this, &main_window::toggle_tracker_, Qt::QueuedConnection }; qt_signal restart_tracker { this, &main_window::restart_tracker_, Qt::QueuedConnection }; + std::unique_ptr updater; + public: void init_dylibs(); void init_tray_menu(); diff --git a/opentrack/software-update-dialog.cpp b/opentrack/software-update-dialog.cpp new file mode 100644 index 00000000..778814c3 --- /dev/null +++ b/opentrack/software-update-dialog.cpp @@ -0,0 +1,66 @@ +#include "software-update-dialog.hpp" +#include "opentrack-org.hxx" + +update_dialog::update_dialog(QWidget* parent, update_query& q, const QString& new_version) + : QDialog(parent), q(q) +{ + ui.setupUi(this); + ui.ver_current->setText(const_cast(opentrack_version)); + ui.ver_new->setTextFormat(Qt::RichText); + ui.ver_new->setText("" + new_version + ""); + ui.ver_new->setOpenExternalLinks(true); + connect(ui.buttonBox, &QDialogButtonBox::clicked, this, &update_dialog::close); +} + +void update_query::on_finished() +{ + if (!t.isActive()) + return; + t.stop(); + if (r->error() != QNetworkReply::NoError) + { + qDebug() << "updater: error" << r->error() << r->errorString(); + return; + } + QString str(buf); + QRegExp re("SOFTWARE-UPDATE-V3: ([a-zA-Z0-9_.-+]+)"); + int idx = re.indexIn(str); + if (idx != -1) + { + str = re.cap(1); + QSettings s(OPENTRACK_ORG); + QString quiet_version = s.value("quiet-update-version").toString(); + + if (!str.isEmpty() && str != opentrack_version && str != quiet_version) + { + qDebug() << "updater: new version" << str; + update_dialog dlg(qobject_cast(parent()), *this, str); + dlg.show(); + dlg.raise(); + dlg.exec(); + if (dlg.ui.disable_reminder->isChecked()) + s.setValue("quiet-update-version", str); + } + } + else + { + if (buf.isEmpty()) + qDebug() << "updater: empty response"; + else + qDebug() << "updater: can't parse response"; + } + buf.clear(); + r->deleteLater(); +} + +void update_query::maybe_show_dialog() +{ + t.stop(); + t.setSingleShot(true); + t.start(1000 * 10); + + r = qnam.get(QNetworkRequest(QStringLiteral("https://www.trackhat.org/thotv3-version"))); + + QObject::connect(r, &QIODevice::readyRead, this, &update_query::on_ready); + QObject::connect(r, &QNetworkReply::finished, this, &update_query::on_finished); +} diff --git a/opentrack/software-update-dialog.hpp b/opentrack/software-update-dialog.hpp new file mode 100644 index 00000000..1e150e62 --- /dev/null +++ b/opentrack/software-update-dialog.hpp @@ -0,0 +1,34 @@ +#pragma once + +#include +#include +#include +#include "ui_software-update.h" + +extern "C" const char* const opentrack_version; + +class update_query final : public QObject +{ + Q_OBJECT +public: + explicit update_query(QWidget* parent) : QObject{parent} {} + + QNetworkReply* r = nullptr; + QNetworkAccessManager qnam{this}; + QByteArray buf; + QTimer t{this}; + + void on_finished(); + void on_ready() { buf.append(r->readAll()); } + void maybe_show_dialog(); +}; + +class update_dialog : QDialog +{ + Q_OBJECT + friend class update_query; +private: + Ui::UpdateDialog ui; + update_query& q; + update_dialog(QWidget* parent, update_query& q, const QString& new_version); +}; diff --git a/opentrack/software-update.ui b/opentrack/software-update.ui new file mode 100644 index 00000000..07edf66c --- /dev/null +++ b/opentrack/software-update.ui @@ -0,0 +1,118 @@ + + + UpdateDialog + + + + 0 + 0 + 385 + 187 + + + + Dialog + + + + + + + 16 + + + + Software update released + + + Qt::AlignCenter + + + + + + + Qt::Horizontal + + + + + + + QFrame::NoFrame + + + QFrame::Raised + + + 0 + + + + 12 + + + 12 + + + 12 + + + 12 + + + 12 + + + + + Current version: + + + + + + + TextLabel + + + true + + + + + + + Released version: + + + + + + + TextLabel + + + + + + + Don't remind me again about this version + + + + + + + + + + QDialogButtonBox::Close + + + + + + + + -- cgit v1.2.3