From 553e72a5ce38c12856a2222010c4851d35ee42e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Du=C5=A1an=20Poizl?= Date: Thu, 19 Sep 2024 13:39:07 +0200 Subject: [PATCH] Add check for new version --- about.cpp | 7 +++++++ about.h | 2 ++ mainwindow.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ mainwindow.h | 1 + 4 files changed, 50 insertions(+) diff --git a/about.cpp b/about.cpp index 73888b0..e607256 100644 --- a/about.cpp +++ b/about.cpp @@ -46,3 +46,10 @@ HelpDialog::HelpDialog(QWidget *parent) : QDialog(parent) layout->addWidget(helpText); } + +QString getVersion() +{ + QString version = GITVERSION; + version.truncate(8); + return version; +} diff --git a/about.h b/about.h index ba0fb6a..964be04 100644 --- a/about.h +++ b/about.h @@ -17,4 +17,6 @@ public: HelpDialog(QWidget *parent = nullptr); }; +QString getVersion(); + #endif // ABOUT_H diff --git a/mainwindow.cpp b/mainwindow.cpp index 7d39bf3..a0f7cde 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -19,6 +19,9 @@ #include #include #include +#include +#include +#include #include "loadrunable.h" #include "markedfiles.h" #include "about.h" @@ -254,6 +257,7 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) helpMenu->addAction(tr("Help"), QKeySequence::HelpContents, [this]{ HelpDialog help(this); help.exec(); }); helpMenu->addAction(tr("About Tenmon"), [this]{ About about(this); about.exec(); }); helpMenu->addAction(tr("About Qt"), [this](){ QMessageBox::aboutQt(this); }); + helpMenu->addAction(tr("Check update"), this, &MainWindow::checkNewVersion); setupSigterm(); QSettings settings; @@ -692,6 +696,42 @@ void MainWindow::exportCSV() m_databaseView->exportCSV(file); } +void MainWindow::checkNewVersion() +{ + QNetworkAccessManager *manager = new QNetworkAccessManager(this); + QNetworkRequest request(QUrl("https://gitea.nouspiro.space/api/v1/repos/nou/tenmon/releases/latest")); + request.setRawHeader("accept", "application/json"); + QNetworkReply *reply = manager->get(request); + connect(reply, &QNetworkReply::finished, [this, manager, reply](){ + QJsonParseError error; + QJsonDocument json = QJsonDocument::fromJson(reply->readAll(), &error); + if(json.isObject() && json.object().contains("tag_name")) + { + QString tag = json.object().value("tag_name").toString(); + QString version = getVersion(); + if(version < tag) + QMessageBox::information(this, tr("Update check"), tr("You have newest version")); + else + { + if(QMessageBox::question(this, tr("Update check"), tr("New version %1 is available. Do you want to download it now?").arg(tag)) == QMessageBox::Yes) + { + QUrl url(json.object().value("html_url").toString()); + qDebug() << url; + if(url.host() == "gitea.nouspiro.space") + QDesktopServices::openUrl(url); + } + } + } + else + { + QMessageBox::warning(this, tr("Update check"), tr("Failed to check version")); + } + + reply->deleteLater(); + manager->deleteLater(); + }); +} + void MainWindow::updateWindowTitle() { ImagePtr ptr = m_ringList->currentImage(); diff --git a/mainwindow.h b/mainwindow.h index 81765bd..81586d5 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -65,6 +65,7 @@ protected slots: void showMarkFilesDialog(); void showSettingsDialog(); void exportCSV(); + void checkNewVersion(); }; #endif // MAINWINDOW_H