deskflow/src/lib/gui/VersionChecker.cpp

119 lines
4.1 KiB
C++
Raw Normal View History

/*
* synergy -- mouse and keyboard sharing utility
2016-09-07 14:24:00 +00:00
* Copyright (C) 2012-2016 Symless Ltd.
* Copyright (C) 2012 Nick Bolton
*
* This package is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* found in the file LICENSE that should have accompanied this file.
*
* 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"
#include <QLocale>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QProcess>
#include <QRegularExpression>
Solve SonarCloud security hotspots and bugs (#7383) * Test: allocLockCursorToScreenInfo_withState_setsState * Merge remote-tracking branch 'origin/master' into S1-1717-sonarcloud-issues * Remove header wrappers for gmock/gtest * Convenience wrapper script for tests * Use `std::copy` instead of `strcpy` * Use `std::ranges::copy` instead of `stdcpy` * Delete dead code * Add guitests VS Code config and test wrapper * Revert "Delete dead code" This reverts commit aa40f5cd35a22b4b69acfd1876aceea053088060. * Build guitests with CMake * Run all tests * Don't use --gtest_filter for Qt tests * Undo skip for GUI tests * Coverage for `IpcClient::sendCommand` * Remove provider and proxy * Use lamda for StreamProvider to reduce boilerplate * Restore version checker tests * Remove activation souces * Tasks for current/all tests * Change command for tasks * Mock QNetworkAccessManager * Create core app to satisfy Qt assertations * Use `std::copy` instead of `std::ranges::copy` * Remove integtests * Merge guitests into unittests * Use std::string::length * Remove include (resolves to root) * Fix memory leaks * Disable sigsegv tests * Fixed formatting * Remove guitests from CI * New MainWindowTests * Passing test for MainWindowTests * Use alternative to strlen in MainWindow::checkSecureSocket * Passing test for Log::print * Fixed dtor call order * Fixed var name typo * Use proxy instead of `#define protected public` * Add args test for log * Add license ctor * Fixed log test for release * Add error log test * Init qt with -platform offscreen * Back-out initQt function * Use QT_QPA_PLATFORM * Try QT_QPA_PLATFORM=offscreen * Use more readable env node * Set QT_QPA_PLATFORM in CI and CodeQL * Remove env not needed * Modernize Log::print * Calculate the length of the format string fmt during the initial scan loop * More direct and efficient alterative to `strlen` * Fixed major maint issues in AppConfig * New clang and cmake rules * Set `max_pargs_hwrap` to 4 * Undo clang format for now * Fix missing `Setting::` * Fixed missing `m_S` on Windows * Re-add accidental dep resolution (fix later) * Fixed missing buffer position increment * Turn on errors * Fixed tests and improve error message * Fixed segfault on log refactor * Extract log time to function * Copyright date * Update ChangeLog
2024-07-16 08:30:35 +00:00
#include <memory>
Solve SonarCloud security hotspots and bugs (#7383) * Test: allocLockCursorToScreenInfo_withState_setsState * Merge remote-tracking branch 'origin/master' into S1-1717-sonarcloud-issues * Remove header wrappers for gmock/gtest * Convenience wrapper script for tests * Use `std::copy` instead of `strcpy` * Use `std::ranges::copy` instead of `stdcpy` * Delete dead code * Add guitests VS Code config and test wrapper * Revert "Delete dead code" This reverts commit aa40f5cd35a22b4b69acfd1876aceea053088060. * Build guitests with CMake * Run all tests * Don't use --gtest_filter for Qt tests * Undo skip for GUI tests * Coverage for `IpcClient::sendCommand` * Remove provider and proxy * Use lamda for StreamProvider to reduce boilerplate * Restore version checker tests * Remove activation souces * Tasks for current/all tests * Change command for tasks * Mock QNetworkAccessManager * Create core app to satisfy Qt assertations * Use `std::copy` instead of `std::ranges::copy` * Remove integtests * Merge guitests into unittests * Use std::string::length * Remove include (resolves to root) * Fix memory leaks * Disable sigsegv tests * Fixed formatting * Remove guitests from CI * New MainWindowTests * Passing test for MainWindowTests * Use alternative to strlen in MainWindow::checkSecureSocket * Passing test for Log::print * Fixed dtor call order * Fixed var name typo * Use proxy instead of `#define protected public` * Add args test for log * Add license ctor * Fixed log test for release * Add error log test * Init qt with -platform offscreen * Back-out initQt function * Use QT_QPA_PLATFORM * Try QT_QPA_PLATFORM=offscreen * Use more readable env node * Set QT_QPA_PLATFORM in CI and CodeQL * Remove env not needed * Modernize Log::print * Calculate the length of the format string fmt during the initial scan loop * More direct and efficient alterative to `strlen` * Fixed major maint issues in AppConfig * New clang and cmake rules * Set `max_pargs_hwrap` to 4 * Undo clang format for now * Fix missing `Setting::` * Fixed missing `m_S` on Windows * Re-add accidental dep resolution (fix later) * Fixed missing buffer position increment * Turn on errors * Fixed tests and improve error message * Fixed segfault on log refactor * Extract log time to function * Copyright date * Update ChangeLog
2024-07-16 08:30:35 +00:00
VersionChecker::VersionChecker(std::shared_ptr<QNetworkAccessManager> nam)
: m_manager(nam ? nam : std::make_shared<QNetworkAccessManager>(this)) {
connect(
m_manager.get(), SIGNAL(finished(QNetworkReply *)), this,
SLOT(replyFinished(QNetworkReply *)));
}
void VersionChecker::checkLatest() {
2024-07-04 19:57:28 +00:00
auto request = QNetworkRequest(QUrl(SYNERGY_VERSION_URL));
request.setHeader(
QNetworkRequest::UserAgentHeader, QString("Synergy (") + SYNERGY_VERSION +
") " +
QSysInfo::prettyProductName());
request.setRawHeader("X-Synergy-Version", SYNERGY_VERSION);
request.setRawHeader(
"X-Synergy-Language", QLocale::system().name().toStdString().c_str());
m_manager->get(request);
}
void VersionChecker::replyFinished(QNetworkReply *reply) {
auto newestVersion = QString(reply->readAll());
if (!newestVersion.isEmpty() &&
compareVersions(SYNERGY_VERSION, newestVersion) > 0) {
emit updateFound(newestVersion);
}
}
int VersionChecker::getStageVersion(QString stage) const {
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;
if (stage.isEmpty() || stage == stableName) {
return stableValue;
} else if (stage.toLower().startsWith(rcName)) {
QRegularExpression re("\\d*", QRegularExpression::CaseInsensitiveOption);
if (re.match(stage).hasMatch()) {
// return the rc value plus the rc number (e.g. 2 + 1)
// this should be ok since stable is max int.
return rcValue + re.match(stage).captured(1).toInt();
}
} else if (stage == betaName) {
return betaValue;
}
return otherValue;
}
int VersionChecker::compareVersions(const QString &left, const QString &right) {
if (left.compare(right) == 0)
return 0; // versions are same.
QStringList leftParts = left.split("-");
QStringList rightParts = right.split("-");
QString leftNumber = leftParts.at(0);
QString rightNumber = rightParts.at(0);
QStringList leftNumberParts = left.split(".");
QStringList rightNumberParts = right.split(".");
auto leftStagePart = leftParts.size() > 1 ? leftParts.at(1) : "";
auto rightStagePart = rightParts.size() > 1 ? rightParts.at(1) : "";
const int leftMajor = leftNumberParts.at(0).toInt();
const int leftMinor = leftNumberParts.at(1).toInt();
const int leftPatch = leftNumberParts.at(2).toInt();
const int leftStage = getStageVersion(leftStagePart);
const int rightMajor = rightNumberParts.at(0).toInt();
const int rightMinor = rightNumberParts.at(1).toInt();
const int rightPatch = rightNumberParts.at(2).toInt();
const int rightStage = getStageVersion(rightStagePart);
const bool rightWins =
(rightMajor > leftMajor) ||
((rightMajor >= leftMajor) && (rightMinor > leftMinor)) ||
((rightMajor >= leftMajor) && (rightMinor >= leftMinor) &&
(rightPatch > leftPatch)) ||
((rightMajor >= leftMajor) && (rightMinor >= leftMinor) &&
(rightPatch >= leftPatch) && (rightStage > leftStage));
return rightWins ? 1 : -1;
}