Files
QtZeiterfassung/plugins/updaterplugin/updaterdialog.cpp
T

115 lines
3.3 KiB
C++
Raw Normal View History

2017-12-18 22:39:10 +01:00
#include "updaterdialog.h"
#include "ui_updaterdialog.h"
2017-12-07 00:24:00 +01:00
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QDebug>
2017-12-07 00:34:29 +01:00
#include <QVersionNumber>
2017-12-07 00:32:26 +01:00
#include <QJsonParseError>
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonValue>
#include <QJsonObject>
2017-12-07 00:51:45 +01:00
#include <QDesktopServices>
#include <QMessageBox>
2017-12-18 22:39:10 +01:00
#include "mainwindow.h"
2017-12-07 00:51:45 +01:00
#include "zeiterfassungsettings.h"
2017-12-18 22:39:10 +01:00
#include "zeiterfassungapi.h"
2017-12-18 22:39:10 +01:00
UpdaterDialog::UpdaterDialog(MainWindow &mainWindow) :
QDialog(&mainWindow),
ui(new Ui::UpdaterDialog),
m_mainWindow(mainWindow)
2017-12-07 00:24:00 +01:00
{
ui->setupUi(this);
2017-12-22 23:14:03 +01:00
#if (QT_VERSION >= QT_VERSION_CHECK(5, 9, 0))
2017-12-22 21:02:01 +01:00
setWindowFlag(Qt::WindowContextHelpButtonHint, false);
2017-12-22 23:14:03 +01:00
#else
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
#endif
2017-12-07 00:51:45 +01:00
setAttribute(Qt::WA_DeleteOnClose);
connect(ui->buttonBox, &QDialogButtonBox::accepted, this, &UpdaterDialog::acceptedSlot);
connect(ui->buttonBox, &QDialogButtonBox::rejected, this, &UpdaterDialog::rejectedSlot);
2017-12-07 00:51:45 +01:00
2017-12-18 22:39:10 +01:00
auto url = m_mainWindow.settings().value(QStringLiteral("UpdaterPlugin/url"),
2017-12-21 21:49:02 +01:00
QStringLiteral("https://api.github.com/repos/0xFEEDC0DE64/QtZeiterfassung/releases")).toString();
m_reply = m_mainWindow.erfassung().manager()->get(QNetworkRequest(QUrl(url)));
2017-12-18 22:39:10 +01:00
connect(m_reply, &QNetworkReply::finished, this, &UpdaterDialog::finished);
2017-12-07 00:24:00 +01:00
}
2017-12-18 22:39:10 +01:00
UpdaterDialog::~UpdaterDialog()
2017-12-07 00:24:00 +01:00
{
delete ui;
}
void UpdaterDialog::acceptedSlot()
{
if(ui->checkBoxDontShow->isChecked())
m_mainWindow.settings().setValue(QStringLiteral("UpdaterPlugin/lastUpdateCheck"), QDate::currentDate());
if(!QDesktopServices::openUrl(m_url))
QMessageBox::warning(this, tr("Could not open default webbrowser!"), tr("Could not open default webbrowser!"));
accept();
}
void UpdaterDialog::rejectedSlot()
{
if(ui->checkBoxDontShow->isChecked())
m_mainWindow.settings().setValue(QStringLiteral("UpdaterPlugin/lastUpdateCheck"), QDate::currentDate());
reject();
}
2017-12-18 22:39:10 +01:00
void UpdaterDialog::finished()
{
if(m_reply->error() != QNetworkReply::NoError)
{
2017-12-07 00:32:26 +01:00
qWarning() << "request error" << m_reply->error() << m_reply->errorString();
return;
}
2017-12-07 00:32:26 +01:00
QJsonParseError error;
auto document = QJsonDocument::fromJson(m_reply->readAll(), &error);
if(error.error != QJsonParseError::NoError)
{
qWarning() << "parse error" << error.error << error.errorString();
return;
}
if(!document.isArray())
{
qWarning() << "document is not an array!";
return;
}
2017-12-07 00:34:29 +01:00
auto appVersion = QVersionNumber::fromString(QCoreApplication::applicationVersion());
2017-12-07 00:32:26 +01:00
auto array = document.array();
for(const auto &releaseVal : array)
{
2017-12-07 00:34:29 +01:00
auto releaseObj = releaseVal.toObject();
auto version = QVersionNumber::fromString(releaseObj.value(QStringLiteral("tag_name")).toString());
2017-12-07 00:34:29 +01:00
2017-12-07 00:51:45 +01:00
if(appVersion < version)
{
m_url = QUrl(releaseObj.value(QStringLiteral("html_url")).toString());
ui->labelDescription->setText(releaseObj.value(QStringLiteral("body")).toString());
2017-12-07 00:51:45 +01:00
show();
return;
}
2017-12-07 00:32:26 +01:00
}
2017-12-07 00:51:45 +01:00
2017-12-18 22:39:10 +01:00
m_mainWindow.settings().setValue(QStringLiteral("UpdaterPlugin/lastUpdateCheck"), QDate::currentDate());
2017-12-14 00:44:44 +01:00
2017-12-07 00:51:45 +01:00
deleteLater();
}