Use QVersionNumber for Version Comparison

This commit is contained in:
Florian Märkl 2019-03-18 21:42:00 +01:00
parent a3e4daecca
commit 9f582b3e03
4 changed files with 32 additions and 18 deletions

View File

@ -39,8 +39,8 @@ int main(int argc, char *argv[])
if (Config()->getAutoUpdateEnabled()) {
UpdateWorker *updateWorker = new UpdateWorker;
QObject::connect(updateWorker, &UpdateWorker::checkComplete,
[=](const QString & version, const QString & error) {
if (error == "" && version != CUTTER_VERSION_FULL) {
[=](const QVersionNumber &version, const QString & error) {
if (error == "" && version > UpdateWorker::currentVersionNumber()) {
updateWorker->showUpdateDialog(true);
}
updateWorker->deleteLater();

View File

@ -20,15 +20,15 @@
#include "CutterConfig.h"
UpdateWorker::UpdateWorker(QObject *parent) :
QObject(parent), latestVersion(""), pending(false)
QObject(parent), pending(false)
{
connect(&t, &QTimer::timeout, [this]() {
if (pending) {
disconnect(checkReply, nullptr, this, nullptr);
checkReply->close();
checkReply->deleteLater();
emit checkComplete("", tr("Time limit exceeded during version check. Please check your "
"internet connection and try again."));
emit checkComplete(QVersionNumber(), tr("Time limit exceeded during version check. Please check your "
"internet connection and try again."));
}
});
}
@ -73,10 +73,10 @@ void UpdateWorker::showUpdateDialog(bool showDontCheckForUpdatesButton)
mb.setWindowTitle(tr("Version control"));
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 + "<br/><br/>"
+ "<b>" + tr("Latest version:") + "</b> " + latestVersion.toString() + "<br/><br/>"
+ tr("For update, please check the link:<br/>")
+ QString("<a href=\"https://github.com/radareorg/cutter/releases/tag/v%1\">"
"https://github.com/radareorg/cutter/releases/tag/v%1</a><br/>").arg(latestVersion)
"https://github.com/radareorg/cutter/releases/tag/v%1</a><br/>").arg(latestVersion.toString())
+ tr("or click \"Download\" to download latest version of Cutter."));
if (showDontCheckForUpdatesButton) {
mb.setStandardButtons(QMessageBox::Save | QMessageBox::Reset | QMessageBox::Ok);
@ -126,7 +126,7 @@ void UpdateWorker::showUpdateDialog(bool showDontCheckForUpdatesButton)
QDesktopServices::openUrl(path.join('/'));
}
});
download(fullFileName, latestVersion);
download(fullFileName, latestVersion.toString());
// Calling show() before exec() is only way make dialog non-modal
// it seems wierd, but it works
progressDial.show();
@ -149,15 +149,18 @@ void UpdateWorker::abortDownload()
void UpdateWorker::serveVersionCheckReply()
{
pending = false;
QString versionReply = "";
QString versionReplyStr = "";
QString errStr = "";
if (checkReply->error()) {
errStr = checkReply->errorString();
} else {
versionReply = QJsonDocument::fromJson(checkReply->readAll()).object().value("tag_name").toString();
versionReply.remove('v');
versionReplyStr = QJsonDocument::fromJson(checkReply->readAll()).object().value("tag_name").toString();
versionReplyStr.remove('v');
}
QVersionNumber versionReply = QVersionNumber::fromString(versionReplyStr);
if (!versionReply.isNull()) {
latestVersion = versionReply;
}
latestVersion = versionReply;
checkReply->close();
checkReply->deleteLater();
emit checkComplete(versionReply, errStr);
@ -202,10 +205,15 @@ QString UpdateWorker::getRepositoryFileName() const
downloadFileName = "Cutter-v%1-x%2.macOS.dmg";
#endif
downloadFileName = downloadFileName
.arg(latestVersion)
.arg(latestVersion.toString())
.arg(QSysInfo::buildAbi().split('-').at(2).contains("64")
? "64"
: "32");
return downloadFileName;
}
QVersionNumber UpdateWorker::currentVersionNumber()
{
return QVersionNumber(CUTTER_VERSION_MAJOR, CUTTER_VERSION_MINOR, CUTTER_VERSION_PATCH);
}

View File

@ -5,6 +5,7 @@
#include <QTimer>
#include <QObject>
#include <QtNetwork/QNetworkAccessManager>
#include <QVersionNumber>
class QNetworkReply;
@ -54,6 +55,11 @@ public:
*/
void showUpdateDialog(bool showDontCheckForUpdatesButton);
/**
* @return the version of this Cutter binary, derived from CUTTER_VERSION_MAJOR, CUTTER_VERSION_MINOR and CUTTER_VERSION_PATCH.
*/
static QVersionNumber currentVersionNumber();
public slots:
/**
* @fn void UpdateWorker::abortDownload()
@ -71,10 +77,10 @@ signals:
* @fn UpdateWorker::checkComplete(const QString& verson, const QString& errorMsg)
*
* The signal is emitted when check has been done with an empty @a errorMsg string.
* In case of an error @a currVerson is empty and @a errorMsg contains description
* In case of an error @a currVerson is null and @a errorMsg contains description
* of error.
*/
void checkComplete(const QString &currVerson, const QString &errorMsg);
void checkComplete(const QVersionNumber &currVerson, const QString &errorMsg);
/**
* @fn UpdateWorker::downloadProcess(size_t bytesReceived, size_t bytesTotal)
@ -112,7 +118,7 @@ private:
private:
QNetworkAccessManager nm;
QString latestVersion;
QVersionNumber latestVersion;
QTimer t;
bool pending;
QFile downloadFile;

View File

@ -91,11 +91,11 @@ void AboutDialog::on_checkForUpdatesButton_clicked()
connect(&updateWorker, &UpdateWorker::checkComplete, &waitDialog, &QProgressDialog::cancel);
connect(&updateWorker, &UpdateWorker::checkComplete,
[&updateWorker](const QString & version, const QString & error) {
[&updateWorker](const QVersionNumber &version, const QString & error) {
if (error != "") {
QMessageBox::critical(nullptr, tr("Error!"), error);
} else {
if (version == CUTTER_VERSION_FULL) {
if (version <= UpdateWorker::currentVersionNumber()) {
QMessageBox::information(nullptr, tr("Version control"), tr("Cutter is up to date!"));
} else {
updateWorker.showUpdateDialog(false);