2013-07-24 16:41:12 +00:00
|
|
|
/*
|
|
|
|
|
* synergy -- mouse and keyboard sharing utility
|
2024-07-26 22:53:52 +00:00
|
|
|
* Copyright (C) 2012 Symless Ltd.
|
2013-07-24 16:41:12 +00:00
|
|
|
*
|
|
|
|
|
* This package is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU General Public License
|
2015-05-03 02:33:52 +00:00
|
|
|
* found in the file LICENSE that should have accompanied this file.
|
2013-07-24 16:41:12 +00:00
|
|
|
*
|
|
|
|
|
* This package is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "VersionChecker.h"
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
#include <QLocale>
|
2013-07-24 16:41:12 +00:00
|
|
|
#include <QNetworkAccessManager>
|
|
|
|
|
#include <QNetworkReply>
|
2024-07-02 19:07:06 +00:00
|
|
|
#include <QNetworkRequest>
|
2013-07-24 16:41:12 +00:00
|
|
|
#include <QProcess>
|
2024-07-08 14:37:51 +00:00
|
|
|
#include <QRegularExpression>
|
2024-07-16 08:30:35 +00:00
|
|
|
#include <memory>
|
2013-07-24 16:41:12 +00:00
|
|
|
|
2024-07-26 22:53:52 +00:00
|
|
|
const char *const kVersion = SYNERGY_VERSION;
|
|
|
|
|
|
|
|
|
|
VersionChecker::VersionChecker(std::shared_ptr<QNetworkAccessManager> network)
|
|
|
|
|
: m_network(
|
|
|
|
|
network ? network : std::make_shared<QNetworkAccessManager>(this)) {
|
2024-07-16 13:36:44 +00:00
|
|
|
connect(
|
2024-07-26 22:53:52 +00:00
|
|
|
m_network.get(), SIGNAL(finished(QNetworkReply *)), this,
|
2024-07-16 13:36:44 +00:00
|
|
|
SLOT(replyFinished(QNetworkReply *)));
|
2013-07-24 16:41:12 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-26 22:53:52 +00:00
|
|
|
void VersionChecker::checkLatest() const {
|
|
|
|
|
const QString url =
|
|
|
|
|
qEnvironmentVariable("SYNERGY_VERSION_URL", SYNERGY_VERSION_URL);
|
|
|
|
|
auto request = QNetworkRequest(url);
|
|
|
|
|
auto userAgent = QString("Synergy %1 on %2")
|
|
|
|
|
.arg(kVersion)
|
|
|
|
|
.arg(QSysInfo::prettyProductName());
|
|
|
|
|
request.setHeader(QNetworkRequest::UserAgentHeader, userAgent);
|
|
|
|
|
request.setRawHeader("X-Synergy-Version", kVersion);
|
2024-07-16 13:36:44 +00:00
|
|
|
request.setRawHeader(
|
|
|
|
|
"X-Synergy-Language", QLocale::system().name().toStdString().c_str());
|
2024-07-26 22:53:52 +00:00
|
|
|
m_network->get(request);
|
2013-07-24 16:41:12 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
void VersionChecker::replyFinished(QNetworkReply *reply) {
|
|
|
|
|
auto newestVersion = QString(reply->readAll());
|
|
|
|
|
if (!newestVersion.isEmpty() &&
|
|
|
|
|
compareVersions(SYNERGY_VERSION, newestVersion) > 0) {
|
|
|
|
|
emit updateFound(newestVersion);
|
|
|
|
|
}
|
2013-07-24 16:41:12 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-26 22:53:52 +00:00
|
|
|
int VersionChecker::getStageVersion(QString stage) {
|
2024-07-02 19:07:06 +00:00
|
|
|
const char *stableName = "stable";
|
|
|
|
|
const char *rcName = "rc";
|
|
|
|
|
const char *betaName = "beta";
|
|
|
|
|
|
|
|
|
|
// use max int for stable so it's always the highest value.
|
|
|
|
|
const int stableValue = INT_MAX;
|
|
|
|
|
const int rcValue = 2;
|
|
|
|
|
const int betaValue = 1;
|
|
|
|
|
const int otherValue = 0;
|
|
|
|
|
|
2024-07-08 14:37:51 +00:00
|
|
|
if (stage.isEmpty() || stage == stableName) {
|
2024-07-02 19:07:06 +00:00
|
|
|
return stableValue;
|
|
|
|
|
} else if (stage.toLower().startsWith(rcName)) {
|
2024-07-08 14:37:51 +00:00
|
|
|
QRegularExpression re("\\d*", QRegularExpression::CaseInsensitiveOption);
|
|
|
|
|
if (re.match(stage).hasMatch()) {
|
2024-07-02 19:07:06 +00:00
|
|
|
// return the rc value plus the rc number (e.g. 2 + 1)
|
|
|
|
|
// this should be ok since stable is max int.
|
2024-07-08 14:37:51 +00:00
|
|
|
return rcValue + re.match(stage).captured(1).toInt();
|
2016-12-28 11:50:32 +00:00
|
|
|
}
|
2024-07-02 19:07:06 +00:00
|
|
|
} else if (stage == betaName) {
|
|
|
|
|
return betaValue;
|
|
|
|
|
}
|
2013-07-24 16:41:12 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
return otherValue;
|
2019-08-22 13:02:40 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
int VersionChecker::compareVersions(const QString &left, const QString &right) {
|
|
|
|
|
if (left.compare(right) == 0)
|
|
|
|
|
return 0; // versions are same.
|
2013-07-24 16:41:12 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
QStringList leftParts = left.split("-");
|
|
|
|
|
QStringList rightParts = right.split("-");
|
2024-06-28 09:35:18 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
QString leftNumber = leftParts.at(0);
|
|
|
|
|
QString rightNumber = rightParts.at(0);
|
2013-07-24 16:41:12 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
QStringList leftNumberParts = left.split(".");
|
|
|
|
|
QStringList rightNumberParts = right.split(".");
|
2013-07-24 16:41:12 +00:00
|
|
|
|
2024-07-08 14:37:51 +00:00
|
|
|
auto leftStagePart = leftParts.size() > 1 ? leftParts.at(1) : "";
|
|
|
|
|
auto rightStagePart = rightParts.size() > 1 ? rightParts.at(1) : "";
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
const int leftMajor = leftNumberParts.at(0).toInt();
|
|
|
|
|
const int leftMinor = leftNumberParts.at(1).toInt();
|
|
|
|
|
const int leftPatch = leftNumberParts.at(2).toInt();
|
2024-07-08 14:37:51 +00:00
|
|
|
const int leftStage = getStageVersion(leftStagePart);
|
2013-07-24 16:41:12 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
const int rightMajor = rightNumberParts.at(0).toInt();
|
|
|
|
|
const int rightMinor = rightNumberParts.at(1).toInt();
|
|
|
|
|
const int rightPatch = rightNumberParts.at(2).toInt();
|
2024-07-08 14:37:51 +00:00
|
|
|
const int rightStage = getStageVersion(rightStagePart);
|
2013-07-24 16:41:12 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
const bool rightWins =
|
|
|
|
|
(rightMajor > leftMajor) ||
|
|
|
|
|
((rightMajor >= leftMajor) && (rightMinor > leftMinor)) ||
|
|
|
|
|
((rightMajor >= leftMajor) && (rightMinor >= leftMinor) &&
|
|
|
|
|
(rightPatch > leftPatch)) ||
|
|
|
|
|
((rightMajor >= leftMajor) && (rightMinor >= leftMinor) &&
|
|
|
|
|
(rightPatch >= leftPatch) && (rightStage > leftStage));
|
2013-07-24 16:41:12 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
return rightWins ? 1 : -1;
|
2013-07-24 16:41:12 +00:00
|
|
|
}
|