Files
QtZeiterfassung/dialogs/updatedialog.cpp
T

99 lines
2.6 KiB
C++
Raw Permalink Normal View History

2017-12-07 00:24:00 +01:00
#include "updatedialog.h"
#include "ui_updatedialog.h"
#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>
#include "zeiterfassungsettings.h"
2017-12-07 00:27:39 +01:00
UpdateDialog::UpdateDialog(ZeiterfassungSettings &settings, QNetworkAccessManager *manager, QWidget *parent) :
2017-12-07 00:24:00 +01:00
QDialog(parent),
2017-12-07 00:27:39 +01:00
ui(new Ui::UpdateDialog),
m_settings(settings)
2017-12-07 00:24:00 +01:00
{
ui->setupUi(this);
2017-12-07 00:51:45 +01:00
setAttribute(Qt::WA_DeleteOnClose);
connect(ui->buttonBox, &QDialogButtonBox::accepted, this, &UpdateDialog::submit);
connect(ui->buttonBox, &QDialogButtonBox::rejected, this, [=](){
if(ui->checkBoxDontShow->isChecked())
m_settings.setLastUpdateCheck(QDate::currentDate());
reject();
});
m_reply = manager->get(QNetworkRequest(QUrl(QStringLiteral("https://api.github.com/repos/0xFEEDC0DE64/QtZeiterfassung/releases"))));
connect(m_reply, &QNetworkReply::finished, this, &UpdateDialog::finished);
2017-12-07 00:24:00 +01:00
}
UpdateDialog::~UpdateDialog()
{
delete ui;
}
void UpdateDialog::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("tag_name").toString());
2017-12-07 00:51:45 +01:00
if(appVersion < version)
{
m_url = QUrl(releaseObj.value("html_url").toString());
ui->labelDescription->setText(releaseObj.value("body").toString());
show();
return;
}
2017-12-07 00:32:26 +01:00
}
2017-12-07 00:51:45 +01:00
deleteLater();
}
void UpdateDialog::submit()
{
if(ui->checkBoxDontShow->isChecked())
m_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();
}