Files

109 lines
2.9 KiB
C++
Raw Permalink 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"
2018-03-24 14:51:56 +01:00
#include "updatersettings.h"
2017-12-18 22:39:10 +01:00
UpdaterDialog::UpdaterDialog(MainWindow &mainWindow) :
2017-12-29 12:37:40 +01:00
ZeiterfassungDialog(&mainWindow),
2017-12-18 22:39:10 +01:00
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
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
2018-03-24 14:51:56 +01:00
m_reply = m_mainWindow.erfassung().manager()->get(QNetworkRequest(UpdaterSettings(mainWindow.settings()).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())
2018-03-24 14:51:56 +01:00
UpdaterSettings(m_mainWindow.settings()).setLastUpdateCheck(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())
2018-03-24 14:51:56 +01:00
UpdaterSettings(m_mainWindow.settings()).setLastUpdateCheck(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
2018-03-24 14:51:56 +01:00
UpdaterSettings(m_mainWindow.settings()).setLastUpdateCheck(QDate::currentDate());
2017-12-14 00:44:44 +01:00
2017-12-07 00:51:45 +01:00
deleteLater();
}