diff options
Diffstat (limited to 'opentrack')
-rw-r--r-- | opentrack/main-window.cpp | 5 | ||||
-rw-r--r-- | opentrack/main-window.hpp | 4 | ||||
-rw-r--r-- | opentrack/software-update-dialog.cpp | 66 | ||||
-rw-r--r-- | opentrack/software-update-dialog.hpp | 34 | ||||
-rw-r--r-- | opentrack/software-update.ui | 118 |
5 files changed, 227 insertions, 0 deletions
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 <cstring> #include <utility> @@ -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<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 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<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 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<const char*>(opentrack_version)); + ui.ver_new->setTextFormat(Qt::RichText); + ui.ver_new->setText("<a href='https://www.trackhat.org/trackhat-opentrack'>" + new_version + "</a>"); + 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<QWidget*>(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 <QtNetwork> +#include <QDialog> +#include <QTimer> +#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 @@ +<?xml version="1.0" encoding="UTF-8"?> +<ui version="4.0"> + <class>UpdateDialog</class> + <widget class="QDialog" name="UpdateDialog"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>385</width> + <height>187</height> + </rect> + </property> + <property name="windowTitle"> + <string>Dialog</string> + </property> + <layout class="QVBoxLayout"> + <item> + <widget class="QLabel" name="label"> + <property name="font"> + <font> + <pointsize>16</pointsize> + </font> + </property> + <property name="text"> + <string>Software update released</string> + </property> + <property name="alignment"> + <set>Qt::AlignCenter</set> + </property> + </widget> + </item> + <item> + <widget class="Line" name="line"> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + </widget> + </item> + <item> + <widget class="QFrame" name="frame"> + <property name="frameShape"> + <enum>QFrame::NoFrame</enum> + </property> + <property name="frameShadow"> + <enum>QFrame::Raised</enum> + </property> + <property name="lineWidth"> + <number>0</number> + </property> + <layout class="QGridLayout" name="gridLayout"> + <property name="leftMargin"> + <number>12</number> + </property> + <property name="topMargin"> + <number>12</number> + </property> + <property name="rightMargin"> + <number>12</number> + </property> + <property name="bottomMargin"> + <number>12</number> + </property> + <property name="spacing"> + <number>12</number> + </property> + <item row="0" column="0"> + <widget class="QLabel" name="label_2"> + <property name="text"> + <string>Current version:</string> + </property> + </widget> + </item> + <item row="0" column="1"> + <widget class="QLabel" name="ver_current"> + <property name="text"> + <string>TextLabel</string> + </property> + <property name="openExternalLinks"> + <bool>true</bool> + </property> + </widget> + </item> + <item row="1" column="0"> + <widget class="QLabel" name="label_3"> + <property name="text"> + <string>Released version:</string> + </property> + </widget> + </item> + <item row="1" column="1"> + <widget class="QLabel" name="ver_new"> + <property name="text"> + <string>TextLabel</string> + </property> + </widget> + </item> + <item row="2" column="0" colspan="2"> + <widget class="QCheckBox" name="disable_reminder"> + <property name="text"> + <string>Don't remind me again about this version</string> + </property> + </widget> + </item> + </layout> + </widget> + </item> + <item> + <widget class="QDialogButtonBox" name="buttonBox"> + <property name="standardButtons"> + <set>QDialogButtonBox::Close</set> + </property> + </widget> + </item> + </layout> + </widget> + <resources/> + <connections/> +</ui> |