# HG changeset patch # User Hasan Yavuz ÖZDERYA # Date 2017-08-19 15:19:31 # Node ID ceb86f2d0c0afe9237ea8527d90f9afc4eb3695f # Parent be15373e2a3899a25fb80b24eb071cd5c113d4e8 added update check dialog diff --git a/CMakeLists.txt b/CMakeLists.txt --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -77,6 +77,7 @@ qt5_wrap_ui(UI_FILES src/binarystreamreadersettings.ui src/asciireadersettings.ui src/framedreadersettings.ui + src/updatecheckdialog.ui ) if (WIN32) @@ -128,6 +129,7 @@ add_executable(${PROGRAM_NAME} WIN32 src/numberformat.cpp src/updatechecker.cpp src/versionnumber.cpp + src/updatecheckdialog.cpp misc/windows_icon.rc ${UI_FILES} ${RES_FILES} diff --git a/serialplot.pro b/serialplot.pro --- a/serialplot.pro +++ b/serialplot.pro @@ -69,7 +69,8 @@ SOURCES += \ src/plotmanager.cpp \ src/numberformat.cpp \ src/recordpanel.cpp \ - src/updatechecker.cpp + src/updatechecker.cpp \ + src/updatecheckdialog.cpp HEADERS += \ src/mainwindow.h \ @@ -110,7 +111,8 @@ HEADERS += \ src/setting_defines.h \ src/numberformat.h \ src/recordpanel.h \ - src/updatechecker.h + src/updatechecker.h \ + src/updatecheckdialog.h FORMS += \ src/mainwindow.ui \ @@ -126,7 +128,8 @@ FORMS += \ src/framedreadersettings.ui \ src/binarystreamreadersettings.ui \ src/asciireadersettings.ui \ - src/recordpanel.ui + src/recordpanel.ui \ + src/updatecheckdialog.ui INCLUDEPATH += qmake/ src/ diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -60,11 +60,11 @@ MainWindow::MainWindow(QWidget *parent) aboutDialog(this), portControl(&serialPort), channelMan(1, 1, this), - updateChecker(this), snapshotMan(this, &channelMan), commandPanel(&serialPort), dataFormatPanel(&serialPort, &channelMan, &recorder), - recordPanel(&recorder, &channelMan) + recordPanel(&recorder, &channelMan), + updateCheckDialog(this) { ui->setupUi(this); @@ -113,6 +113,9 @@ MainWindow::MainWindow(QWidget *parent) QObject::connect(ui->actionHelpAbout, &QAction::triggered, &aboutDialog, &QWidget::show); + QObject::connect(ui->actionCheckUpdate, &QAction::triggered, + &updateCheckDialog, &QWidget::show); + QObject::connect(ui->actionReportBug, &QAction::triggered, [](){QDesktopServices::openUrl(QUrl(BUG_REPORT_URL));}); @@ -239,9 +242,6 @@ MainWindow::MainWindow(QWidget *parent) QObject::connect(ui->actionDemoMode, &QAction::toggled, plotMan, &PlotManager::showDemoIndicator); - // TEST UPDATE CHECKER - updateChecker.checkUpdate(); - // load default settings QSettings settings("serialplot", "serialplot"); loadAllSettings(&settings); diff --git a/src/mainwindow.h b/src/mainwindow.h --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -45,7 +45,7 @@ #include "snapshotmanager.h" #include "plotmanager.h" #include "datarecorder.h" -#include "updatechecker.h" +#include "updatecheckdialog.h" namespace Ui { class MainWindow; @@ -80,13 +80,13 @@ private: PlotManager* plotMan; SnapshotManager snapshotMan; DataRecorder recorder; // operated by `recordPanel` - UpdateChecker updateChecker; QLabel spsLabel; CommandPanel commandPanel; DataFormatPanel dataFormatPanel; RecordPanel recordPanel; PlotControlPanel plotControlPanel; + UpdateCheckDialog updateCheckDialog; bool isDemoRunning(); /// Stores settings for all modules diff --git a/src/mainwindow.ui b/src/mainwindow.ui --- a/src/mainwindow.ui +++ b/src/mainwindow.ui @@ -90,7 +90,7 @@ 0 0 653 - 27 + 25 @@ -99,6 +99,7 @@ + @@ -142,7 +143,9 @@ false - + + + Pause @@ -156,7 +159,9 @@ - + + + Clear @@ -221,6 +226,11 @@ Load Settings from a File + + + &Check Update + + diff --git a/src/updatecheckdialog.cpp b/src/updatecheckdialog.cpp new file mode 100644 --- /dev/null +++ b/src/updatecheckdialog.cpp @@ -0,0 +1,68 @@ +/* + Copyright © 2017 Hasan Yavuz Özderya + + This file is part of serialplot. + + serialplot is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + serialplot is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with serialplot. If not, see . +*/ + +#include "updatecheckdialog.h" +#include "ui_updatecheckdialog.h" + +UpdateCheckDialog::UpdateCheckDialog(QWidget *parent) : + QDialog(parent), + ui(new Ui::UpdateCheckDialog) +{ + ui->setupUi(this); + + connect(&updateChecker, &UpdateChecker::checkFailed, + [this](QString errorMessage) + { + ui->label->setText(QString("Update check failed.\n") + errorMessage); + }); + + connect(&updateChecker, &UpdateChecker::checkFinished, + [this](bool found, QString newVersion, QString downloadUrl) + { + QString text; + if (!found) + { + text = "There is no update yet."; + } + else + { + text = QString("Found update to version %1. Click to download.")\ + .arg(newVersion).arg(downloadUrl); + qDebug() << text; + } + + ui->label->setText(text); + }); +} + +UpdateCheckDialog::~UpdateCheckDialog() +{ + delete ui; +} + +void UpdateCheckDialog::showEvent(QShowEvent *event) +{ + updateChecker.checkUpdate(); + ui->label->setText("Checking update..."); +} + +void UpdateCheckDialog::closeEvent(QShowEvent *event) +{ + if (updateChecker.isChecking()) updateChecker.cancelCheck(); +} diff --git a/src/updatecheckdialog.h b/src/updatecheckdialog.h new file mode 100644 --- /dev/null +++ b/src/updatecheckdialog.h @@ -0,0 +1,46 @@ +/* + Copyright © 2017 Hasan Yavuz Özderya + + This file is part of serialplot. + + serialplot is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + serialplot is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with serialplot. If not, see . +*/ + +#ifndef UPDATECHECKDIALOG_H +#define UPDATECHECKDIALOG_H + +#include +#include "updatechecker.h" + +namespace Ui { +class UpdateCheckDialog; +} + +class UpdateCheckDialog : public QDialog +{ + Q_OBJECT + +public: + explicit UpdateCheckDialog(QWidget *parent = 0); + ~UpdateCheckDialog(); + +private: + Ui::UpdateCheckDialog *ui; + UpdateChecker updateChecker; + + void showEvent(QShowEvent *event); + void closeEvent(QShowEvent *event); +}; + +#endif // UPDATECHECKDIALOG_H diff --git a/src/updatecheckdialog.ui b/src/updatecheckdialog.ui new file mode 100644 --- /dev/null +++ b/src/updatecheckdialog.ui @@ -0,0 +1,58 @@ + + + UpdateCheckDialog + + + + 0 + 0 + 400 + 148 + + + + Check Update + + + + + + Checking update... + + + true + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Close + + + + + + + + + buttonBox + clicked(QAbstractButton*) + UpdateCheckDialog + close() + + + 199 + 125 + + + 199 + 73 + + + + + diff --git a/src/updatechecker.cpp b/src/updatechecker.cpp --- a/src/updatechecker.cpp +++ b/src/updatechecker.cpp @@ -34,14 +34,26 @@ const char BB_DOWNLOADS_URL[] = "https:/ UpdateChecker::UpdateChecker(QObject *parent) : QObject(parent), nam(this) { + activeReply = NULL; + connect(&nam, &QNetworkAccessManager::finished, this, &UpdateChecker::onReqFinished); } +bool UpdateChecker::isChecking() const +{ + return activeReply != NULL && !activeReply->isFinished(); +} + void UpdateChecker::checkUpdate() { auto req = QNetworkRequest(QUrl(BB_DOWNLOADS_URL)); - nam.get(req); + activeReply = nam.get(req); +} + +void UpdateChecker::cancelCheck() +{ + if (activeReply != NULL) activeReply->abort(); } void UpdateChecker::onReqFinished(QNetworkReply* reply) @@ -82,6 +94,7 @@ void UpdateChecker::onReqFinished(QNetwo } } reply->deleteLater(); + activeReply = NULL; } bool UpdateChecker::parseData(const QJsonDocument& data, QList& files) const diff --git a/src/updatechecker.h b/src/updatechecker.h --- a/src/updatechecker.h +++ b/src/updatechecker.h @@ -33,12 +33,15 @@ class UpdateChecker : public QObject public: explicit UpdateChecker(QObject *parent = 0); + bool isChecking() const; + signals: void checkFinished(bool found, QString newVersion, QString downloadUrl); void checkFailed(QString errorMessage); public slots: void checkUpdate(); + void cancelCheck(); private: enum class FileArch @@ -59,6 +62,8 @@ private: }; QNetworkAccessManager nam; + QNetworkReply* activeReply; + /// Parses json and creates a list of files bool parseData(const QJsonDocument& data, QList& files) const; /// Finds the update file in the file list. Returns `-1` if no new version