2013-07-24 16:41:12 +00:00
|
|
|
/*
|
|
|
|
|
* synergy -- mouse and keyboard sharing utility
|
2016-09-07 14:24:00 +00:00
|
|
|
* Copyright (C) 2012-2016 Symless Ltd.
|
2013-07-24 16:41:12 +00:00
|
|
|
* 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
|
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"
|
|
|
|
|
|
|
|
|
|
#include <QNetworkAccessManager>
|
|
|
|
|
#include <QNetworkRequest>
|
|
|
|
|
#include <QNetworkReply>
|
|
|
|
|
#include <QProcess>
|
|
|
|
|
#include <QLocale>
|
|
|
|
|
|
|
|
|
|
#define VERSION_REGEX "(\\d+\\.\\d+\\.\\d+)"
|
2016-09-06 15:13:50 +00:00
|
|
|
#define VERSION_URL "http://symless.com/version/"
|
2013-07-24 16:41:12 +00:00
|
|
|
|
|
|
|
|
VersionChecker::VersionChecker()
|
|
|
|
|
{
|
2016-12-28 11:50:32 +00:00
|
|
|
m_manager = new QNetworkAccessManager(this);
|
2013-07-24 16:41:12 +00:00
|
|
|
|
2016-12-28 11:50:32 +00:00
|
|
|
connect(m_manager, SIGNAL(finished(QNetworkReply*)),
|
|
|
|
|
this, SLOT(replyFinished(QNetworkReply*)));
|
2013-07-24 16:41:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
VersionChecker::~VersionChecker()
|
|
|
|
|
{
|
2016-12-28 11:50:32 +00:00
|
|
|
delete m_manager;
|
2013-07-24 16:41:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void VersionChecker::checkLatest()
|
|
|
|
|
{
|
2016-12-28 11:50:32 +00:00
|
|
|
m_manager->get(QNetworkRequest(QUrl(VERSION_URL)));
|
2013-07-24 16:41:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void VersionChecker::replyFinished(QNetworkReply* reply)
|
|
|
|
|
{
|
2016-12-28 11:50:32 +00:00
|
|
|
QString newestVersion = QString(reply->readAll());
|
|
|
|
|
if (!newestVersion.isEmpty())
|
|
|
|
|
{
|
|
|
|
|
QString currentVersion = getVersion();
|
|
|
|
|
if (currentVersion != "Unknown") {
|
|
|
|
|
if (compareVersions(currentVersion, newestVersion) > 0)
|
|
|
|
|
emit updateFound(newestVersion);
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-07-24 16:41:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int VersionChecker::compareVersions(const QString& left, const QString& right)
|
|
|
|
|
{
|
2016-12-28 11:50:32 +00:00
|
|
|
if (left.compare(right) == 0)
|
|
|
|
|
return 0; // versions are same.
|
2013-07-24 16:41:12 +00:00
|
|
|
|
2016-12-28 11:50:32 +00:00
|
|
|
QStringList leftSplit = left.split(QRegExp("\\."));
|
|
|
|
|
if (leftSplit.size() != 3)
|
|
|
|
|
return 1; // assume right wins.
|
2013-07-24 16:41:12 +00:00
|
|
|
|
2016-12-28 11:50:32 +00:00
|
|
|
QStringList rightSplit = right.split(QRegExp("\\."));
|
|
|
|
|
if (rightSplit.size() != 3)
|
|
|
|
|
return -1; // assume left wins.
|
2013-07-24 16:41:12 +00:00
|
|
|
|
2016-12-28 11:50:32 +00:00
|
|
|
int leftMajor = leftSplit.at(0).toInt();
|
|
|
|
|
int leftMinor = leftSplit.at(1).toInt();
|
|
|
|
|
int leftRev = leftSplit.at(2).toInt();
|
2013-07-24 16:41:12 +00:00
|
|
|
|
2016-12-28 11:50:32 +00:00
|
|
|
int rightMajor = rightSplit.at(0).toInt();
|
|
|
|
|
int rightMinor = rightSplit.at(1).toInt();
|
|
|
|
|
int rightRev = rightSplit.at(2).toInt();
|
2013-07-24 16:41:12 +00:00
|
|
|
|
2016-12-28 11:50:32 +00:00
|
|
|
bool rightWins =
|
|
|
|
|
(rightMajor > leftMajor) ||
|
|
|
|
|
((rightMajor >= leftMajor) && (rightMinor > leftMinor)) ||
|
|
|
|
|
((rightMajor >= leftMajor) && (rightMinor >= leftMinor) && (rightRev > leftRev));
|
2013-07-24 16:41:12 +00:00
|
|
|
|
2016-12-28 11:50:32 +00:00
|
|
|
return rightWins ? 1 : -1;
|
2013-07-24 16:41:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString VersionChecker::getVersion()
|
|
|
|
|
{
|
2016-12-28 11:50:32 +00:00
|
|
|
QProcess process;
|
|
|
|
|
process.start(m_app, QStringList() << "--version");
|
|
|
|
|
|
|
|
|
|
process.setReadChannel(QProcess::StandardOutput);
|
|
|
|
|
if (process.waitForStarted() && process.waitForFinished())
|
|
|
|
|
{
|
|
|
|
|
QRegExp rx(VERSION_REGEX);
|
|
|
|
|
QString text = process.readLine();
|
|
|
|
|
if (rx.indexIn(text) != -1)
|
|
|
|
|
{
|
|
|
|
|
return rx.cap(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return tr("Unknown");
|
2013-07-24 16:41:12 +00:00
|
|
|
}
|