diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2022-05-08 19:59:35 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2022-05-10 05:01:33 +0200 |
commit | fa7261f124327ccac5212e7fed23cd21cc062fef (patch) | |
tree | 0591a8d9d15b1ff241c5b4480072d397c23880c7 | |
parent | a6c4bab412e45c327c32e56fb77e235bbc9f9d84 (diff) |
trackhat: run update check in the background
-rw-r--r-- | opentrack/main-window.cpp | 6 | ||||
-rw-r--r-- | opentrack/main-window.hpp | 4 | ||||
-rw-r--r-- | opentrack/software-update-dialog.cpp | 32 | ||||
-rw-r--r-- | opentrack/software-update-dialog.hpp | 20 |
4 files changed, 31 insertions, 31 deletions
diff --git a/opentrack/main-window.cpp b/opentrack/main-window.cpp index bff3d080..9e5711db 100644 --- a/opentrack/main-window.cpp +++ b/opentrack/main-window.cpp @@ -65,10 +65,8 @@ main_window::main_window() : State(OPENTRACK_BASE_PATH + OPENTRACK_LIBRARY_PATH) ui.btnStartTracker->setFocus(); - { - auto dlg = update_query{this}; - dlg.maybe_show_dialog(); - } + updater = std::make_unique<update_query>(this); + updater->maybe_show_dialog(); } void main_window::init_shortcuts() diff --git a/opentrack/main-window.hpp b/opentrack/main-window.hpp index e1724a9d..517c019a 100644 --- a/opentrack/main-window.hpp +++ b/opentrack/main-window.hpp @@ -34,6 +34,8 @@ #include "ui_main-window.h" +class update_query; + class main_window final : public QMainWindow, private State { Q_DECLARE_TR_FUNCTIONS(main_window) @@ -78,6 +80,8 @@ class main_window final : public QMainWindow, private State qt_signal<void> toggle_tracker { this, &main_window::toggle_tracker_, Qt::QueuedConnection }; qt_signal<void> restart_tracker { this, &main_window::restart_tracker_, Qt::QueuedConnection }; + std::unique_ptr<update_query> updater; + public: void init_dylibs(); void init_tray_menu(); diff --git a/opentrack/software-update-dialog.cpp b/opentrack/software-update-dialog.cpp index 45e41738..ccdf2f0e 100644 --- a/opentrack/software-update-dialog.cpp +++ b/opentrack/software-update-dialog.cpp @@ -13,11 +13,12 @@ update_dialog::update_dialog(QWidget* parent, update_query& q, const QString& ne void update_query::on_finished() { - if (abort) + if (!t.isActive()) return; + t.stop(); if (r->error() != QNetworkReply::NoError) { - qDebug() << "update error" << r->errorString(); + qDebug() << "updater: error" << r->error() << r->errorString(); return; } QString str(buf); @@ -31,8 +32,8 @@ void update_query::on_finished() if (!str.isEmpty() && str != opentrack_version && str != quiet_version) { - qDebug() << "update version" << str; - update_dialog dlg(parent, *this, str); + qDebug() << "updater: new version" << str; + update_dialog dlg(qobject_cast<QWidget*>(parent()), *this, str); dlg.show(); dlg.raise(); dlg.exec(); @@ -40,26 +41,25 @@ void update_query::on_finished() 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() { - QEventLoop ev; - QTimer t{&ev}; t.setSingleShot(true); + t.stop(); + t.setSingleShot(true); t.start(1000 * 10); - QObject::connect(&t, &QTimer::timeout, [this, &ev] { - abort = true; - ev.quit(); - }); r = qnam.get(QNetworkRequest(QStringLiteral("https://www.trackhat.org/thotversion"))); - QObject::connect(r, &QIODevice::readyRead, [this] { on_ready(); }); - QObject::connect(r, &QNetworkReply::finished, [&]() { - on_finished(); - ev.quit(); - }); - ev.exec(); + 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 index caad7cfa..1e150e62 100644 --- a/opentrack/software-update-dialog.hpp +++ b/opentrack/software-update-dialog.hpp @@ -2,35 +2,33 @@ #include <QtNetwork> #include <QDialog> +#include <QTimer> #include "ui_software-update.h" extern "C" const char* const opentrack_version; -struct update_query final +class update_query final : public QObject { - explicit update_query(QWidget* parent) : parent(parent), qnam(parent) {} + Q_OBJECT +public: + explicit update_query(QWidget* parent) : QObject{parent} {} - QWidget* parent; QNetworkReply* r = nullptr; - QNetworkAccessManager qnam; + QNetworkAccessManager qnam{this}; QByteArray buf; - bool abort = false; + QTimer t{this}; void on_finished(); void on_ready() { buf.append(r->readAll()); } void maybe_show_dialog(); }; -class update_dialog : public QDialog +class update_dialog : QDialog { Q_OBJECT - friend struct update_query; + friend class update_query; private: Ui::UpdateDialog ui; update_query& q; -private slots: - void close(QAbstractButton*) { QDialog::close(); } -public: update_dialog(QWidget* parent, update_query& q, const QString& new_version); }; - |