From a6c4bab412e45c327c32e56fb77e235bbc9f9d84 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Fri, 6 May 2022 19:44:07 +0200 Subject: trackhat: add software update check --- opentrack/lang/nl_NL.ts | 27 ++++++++ opentrack/lang/ru_RU.ts | 27 ++++++++ opentrack/lang/stub.ts | 27 ++++++++ opentrack/lang/zh_CN.ts | 27 ++++++++ opentrack/main-window.cpp | 6 ++ opentrack/software-update-dialog.cpp | 65 +++++++++++++++++++ opentrack/software-update-dialog.hpp | 36 +++++++++++ opentrack/software-update.ui | 118 +++++++++++++++++++++++++++++++++++ 8 files changed, 333 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/lang/nl_NL.ts b/opentrack/lang/nl_NL.ts index 15acd701..d69141cd 100644 --- a/opentrack/lang/nl_NL.ts +++ b/opentrack/lang/nl_NL.ts @@ -12,6 +12,33 @@ + + UpdateDialog + + Dialog + + + + Software update released + + + + Current version: + + + + TextLabel + + + + Released version: + + + + Don't remind me again about this version + + + main_window diff --git a/opentrack/lang/ru_RU.ts b/opentrack/lang/ru_RU.ts index c869bae8..860e68fb 100644 --- a/opentrack/lang/ru_RU.ts +++ b/opentrack/lang/ru_RU.ts @@ -12,6 +12,33 @@ Новое имя профиля: + + UpdateDialog + + Dialog + + + + Software update released + + + + Current version: + + + + TextLabel + + + + Released version: + + + + Don't remind me again about this version + + + main_window diff --git a/opentrack/lang/stub.ts b/opentrack/lang/stub.ts index 66917925..7727ab29 100644 --- a/opentrack/lang/stub.ts +++ b/opentrack/lang/stub.ts @@ -12,6 +12,33 @@ + + UpdateDialog + + Dialog + + + + Software update released + + + + Current version: + + + + TextLabel + + + + Released version: + + + + Don't remind me again about this version + + + main_window diff --git a/opentrack/lang/zh_CN.ts b/opentrack/lang/zh_CN.ts index fd9ea20f..b3764a8f 100644 --- a/opentrack/lang/zh_CN.ts +++ b/opentrack/lang/zh_CN.ts @@ -12,6 +12,33 @@ 新文件名字: + + UpdateDialog + + Dialog + + + + Software update released + + + + Current version: + + + + TextLabel + + + + Released version: + + + + Don't remind me again about this version + + + main_window diff --git a/opentrack/main-window.cpp b/opentrack/main-window.cpp index 2d8dbbd3..bff3d080 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 @@ -63,6 +64,11 @@ main_window::main_window() : State(OPENTRACK_BASE_PATH + OPENTRACK_LIBRARY_PATH) connect(&*s.b, &options::bundle_::saving, this, &main_window::register_shortcuts); ui.btnStartTracker->setFocus(); + + { + auto dlg = update_query{this}; + dlg.maybe_show_dialog(); + } } void main_window::init_shortcuts() diff --git a/opentrack/software-update-dialog.cpp b/opentrack/software-update-dialog.cpp new file mode 100644 index 00000000..45e41738 --- /dev/null +++ b/opentrack/software-update-dialog.cpp @@ -0,0 +1,65 @@ +#include "software-update-dialog.hpp" + +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 (abort) + return; + if (r->error() != QNetworkReply::NoError) + { + qDebug() << "update error" << r->errorString(); + return; + } + QString str(buf); + QRegExp re("SOFTWARE-UPDATE-V2: ([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() << "update version" << str; + update_dialog dlg(parent, *this, str); + dlg.show(); + dlg.raise(); + dlg.exec(); + if (dlg.ui.disable_reminder->isChecked()) + s.setValue("quiet-update-version", str); + } + } + buf.clear(); + r->deleteLater(); +} + +void update_query::maybe_show_dialog() +{ + QEventLoop ev; + QTimer t{&ev}; 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(); +} diff --git a/opentrack/software-update-dialog.hpp b/opentrack/software-update-dialog.hpp new file mode 100644 index 00000000..caad7cfa --- /dev/null +++ b/opentrack/software-update-dialog.hpp @@ -0,0 +1,36 @@ +#pragma once + +#include +#include +#include "ui_software-update.h" + +extern "C" const char* const opentrack_version; + +struct update_query final +{ + explicit update_query(QWidget* parent) : parent(parent), qnam(parent) {} + + QWidget* parent; + QNetworkReply* r = nullptr; + QNetworkAccessManager qnam; + QByteArray buf; + bool abort = false; + + void on_finished(); + void on_ready() { buf.append(r->readAll()); } + void maybe_show_dialog(); +}; + +class update_dialog : public QDialog +{ + Q_OBJECT + friend struct 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); +}; + 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