2019-03-09 13:11:39 +00:00
|
|
|
#include "UpdateWorker.h"
|
|
|
|
|
2019-10-06 17:35:44 +00:00
|
|
|
#if CUTTER_UPDATE_WORKER_AVAILABLE
|
2021-01-24 14:50:13 +00:00
|
|
|
# include <QUrl>
|
|
|
|
# include <QFile>
|
|
|
|
# include <QTimer>
|
|
|
|
# include <QEventLoop>
|
|
|
|
# include <QDataStream>
|
|
|
|
# include <QJsonObject>
|
|
|
|
# include <QApplication>
|
|
|
|
# include <QJsonDocument>
|
|
|
|
# include <QDesktopServices>
|
|
|
|
# include <QtNetwork/QNetworkReply>
|
|
|
|
# include <QtNetwork/QNetworkRequest>
|
2021-04-13 18:40:56 +00:00
|
|
|
# include <QStandardPaths>
|
2021-01-24 14:50:13 +00:00
|
|
|
|
|
|
|
# include <QProgressDialog>
|
|
|
|
# include <QPushButton>
|
|
|
|
# include <QFileDialog>
|
|
|
|
# include <QMessageBox>
|
|
|
|
# include "common/Configuration.h"
|
|
|
|
# include "CutterConfig.h"
|
2021-01-11 21:49:54 +00:00
|
|
|
#endif
|
2019-03-09 13:11:39 +00:00
|
|
|
|
2021-01-11 21:49:54 +00:00
|
|
|
#if CUTTER_UPDATE_WORKER_AVAILABLE
|
2021-01-24 14:50:13 +00:00
|
|
|
UpdateWorker::UpdateWorker(QObject *parent) : QObject(parent), pending(false)
|
2019-03-09 13:11:39 +00:00
|
|
|
{
|
2020-08-07 14:18:42 +00:00
|
|
|
connect(&t, &QTimer::timeout, this, [this]() {
|
2019-03-09 13:11:39 +00:00
|
|
|
if (pending) {
|
|
|
|
disconnect(checkReply, nullptr, this, nullptr);
|
|
|
|
checkReply->close();
|
|
|
|
checkReply->deleteLater();
|
2021-01-24 14:50:13 +00:00
|
|
|
emit checkComplete(QVersionNumber(),
|
|
|
|
tr("Time limit exceeded during version check. Please check your "
|
|
|
|
"internet connection and try again."));
|
2019-03-09 13:11:39 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void UpdateWorker::checkCurrentVersion(time_t timeoutMs)
|
|
|
|
{
|
2020-12-16 10:59:23 +00:00
|
|
|
QUrl url("https://api.github.com/repos/rizinorg/cutter/releases/latest");
|
2019-03-09 13:11:39 +00:00
|
|
|
QNetworkRequest request;
|
|
|
|
request.setUrl(url);
|
|
|
|
|
|
|
|
t.setInterval(timeoutMs);
|
|
|
|
t.setSingleShot(true);
|
|
|
|
t.start();
|
|
|
|
|
|
|
|
checkReply = nm.get(request);
|
2021-01-24 14:50:13 +00:00
|
|
|
connect(checkReply, &QNetworkReply::finished, this, &UpdateWorker::serveVersionCheckReply);
|
2019-03-09 13:11:39 +00:00
|
|
|
pending = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void UpdateWorker::showUpdateDialog(bool showDontCheckForUpdatesButton)
|
|
|
|
{
|
|
|
|
QMessageBox mb;
|
|
|
|
mb.setWindowTitle(tr("Version control"));
|
2021-01-24 14:50:13 +00:00
|
|
|
mb.setText(tr("There is an update available for Cutter.<br/>") + "<b>" + tr("Current version:")
|
|
|
|
+ "</b> " CUTTER_VERSION_FULL "<br/>" + "<b>" + tr("Latest version:") + "</b> "
|
|
|
|
+ latestVersion.toString() + "<br/><br/>"
|
2022-07-02 12:23:13 +00:00
|
|
|
+ tr("To update, please check the link:<br/>")
|
2020-12-16 10:59:23 +00:00
|
|
|
+ QString("<a href=\"https://github.com/rizinorg/cutter/releases/tag/v%1\">"
|
2021-01-24 14:50:13 +00:00
|
|
|
"https://github.com/rizinorg/cutter/releases/tag/v%1</a><br/>")
|
2022-07-02 12:23:13 +00:00
|
|
|
.arg(latestVersion.toString()));
|
2019-03-09 13:11:39 +00:00
|
|
|
if (showDontCheckForUpdatesButton) {
|
2022-07-02 12:23:13 +00:00
|
|
|
mb.setStandardButtons(QMessageBox::Reset | QMessageBox::Ok);
|
|
|
|
mb.button(QMessageBox::Reset)->setText(tr("Don't check for updates automatically"));
|
2019-03-09 13:11:39 +00:00
|
|
|
} else {
|
2022-07-02 12:23:13 +00:00
|
|
|
mb.setStandardButtons(QMessageBox::Ok);
|
2019-03-09 13:11:39 +00:00
|
|
|
}
|
|
|
|
mb.setDefaultButton(QMessageBox::Ok);
|
|
|
|
int ret = mb.exec();
|
|
|
|
if (ret == QMessageBox::Reset) {
|
|
|
|
Config()->setAutoUpdateEnabled(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void UpdateWorker::serveVersionCheckReply()
|
|
|
|
{
|
|
|
|
pending = false;
|
2019-03-18 20:42:00 +00:00
|
|
|
QString versionReplyStr = "";
|
2019-03-09 13:11:39 +00:00
|
|
|
QString errStr = "";
|
|
|
|
if (checkReply->error()) {
|
|
|
|
errStr = checkReply->errorString();
|
|
|
|
} else {
|
2021-01-24 14:50:13 +00:00
|
|
|
versionReplyStr = QJsonDocument::fromJson(checkReply->readAll())
|
|
|
|
.object()
|
|
|
|
.value("tag_name")
|
|
|
|
.toString();
|
2019-03-18 20:42:00 +00:00
|
|
|
versionReplyStr.remove('v');
|
|
|
|
}
|
|
|
|
QVersionNumber versionReply = QVersionNumber::fromString(versionReplyStr);
|
|
|
|
if (!versionReply.isNull()) {
|
|
|
|
latestVersion = versionReply;
|
2019-03-09 13:11:39 +00:00
|
|
|
}
|
|
|
|
checkReply->close();
|
|
|
|
checkReply->deleteLater();
|
|
|
|
emit checkComplete(versionReply, errStr);
|
|
|
|
}
|
|
|
|
|
2019-03-18 20:42:00 +00:00
|
|
|
QVersionNumber UpdateWorker::currentVersionNumber()
|
|
|
|
{
|
|
|
|
return QVersionNumber(CUTTER_VERSION_MAJOR, CUTTER_VERSION_MINOR, CUTTER_VERSION_PATCH);
|
2019-10-06 17:35:44 +00:00
|
|
|
}
|
|
|
|
#endif // CUTTER_UPDATE_WORKER_AVAILABLE
|