2015-04-16 11:50:07 -05:00
|
|
|
/*
|
|
|
|
Copyright (C) 2015 Srivats P.
|
|
|
|
|
|
|
|
This file is part of "Ostinato"
|
|
|
|
|
|
|
|
This is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program 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 "updater.h"
|
|
|
|
|
2016-05-31 09:53:54 -05:00
|
|
|
#include <QCoreApplication>
|
2018-03-14 11:28:56 -05:00
|
|
|
#include <QNetworkAccessManager>
|
|
|
|
#include <QNetworkReply>
|
2015-04-16 11:50:07 -05:00
|
|
|
#include <QXmlStreamReader>
|
|
|
|
|
|
|
|
extern const char* version;
|
|
|
|
|
|
|
|
Updater::Updater()
|
|
|
|
{
|
2018-03-14 11:28:56 -05:00
|
|
|
http_ = new QNetworkAccessManager(this);
|
2015-04-16 11:50:07 -05:00
|
|
|
|
|
|
|
#if 1
|
|
|
|
// Tests!
|
|
|
|
Q_ASSERT(isVersionNewer("1.1", "1") == true);
|
|
|
|
Q_ASSERT(isVersionNewer("10.1", "2") == true);
|
|
|
|
Q_ASSERT(isVersionNewer("0.10", "0.2") == true);
|
|
|
|
Q_ASSERT(isVersionNewer("1.10.1", "1.2.3") == true);
|
2016-09-17 04:51:31 -05:00
|
|
|
Q_ASSERT(isVersionNewer("0.7.1", "0.8") == false);
|
2015-04-16 11:50:07 -05:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
Updater::~Updater()
|
|
|
|
{
|
|
|
|
delete http_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Updater::checkForNewVersion()
|
|
|
|
{
|
2018-03-14 11:28:56 -05:00
|
|
|
QNetworkRequest request(QUrl("http://update.ostinato.org/update/pad.xml"));
|
2015-04-16 11:50:07 -05:00
|
|
|
|
2018-03-14 11:28:56 -05:00
|
|
|
//reqHdr.setHeader("Host", host);
|
|
|
|
request.setHeader(QNetworkRequest::UserAgentHeader, userAgent());
|
2015-04-16 11:50:07 -05:00
|
|
|
|
2018-03-14 11:28:56 -05:00
|
|
|
connect(http_, SIGNAL(finished(QNetworkReply*)),
|
|
|
|
this, SLOT(parseXml(QNetworkReply*)));
|
|
|
|
|
|
|
|
http_->get(request);
|
|
|
|
|
|
|
|
QList<QByteArray> headers = request.rawHeaderList();
|
|
|
|
foreach(QByteArray hdr, headers ) {
|
|
|
|
QByteArray val = request.rawHeader(hdr);
|
|
|
|
qDebug("Updater: %s: %s", qPrintable(QString(hdr)),
|
|
|
|
qPrintable(QString(val)));
|
|
|
|
}
|
2015-04-16 11:50:07 -05:00
|
|
|
}
|
|
|
|
|
2018-03-14 11:28:56 -05:00
|
|
|
void Updater::parseXml(QNetworkReply *reply)
|
2015-04-16 11:50:07 -05:00
|
|
|
{
|
|
|
|
QXmlStreamReader xml;
|
|
|
|
QString newVersion;
|
|
|
|
|
2018-03-14 11:28:56 -05:00
|
|
|
if (reply->error()) {
|
|
|
|
qDebug("Updater: %s", qPrintable(reply->errorString()));
|
2015-04-16 11:50:07 -05:00
|
|
|
goto _exit;
|
|
|
|
}
|
|
|
|
|
2018-03-14 11:28:56 -05:00
|
|
|
xml.setDevice(reply);
|
2015-04-16 11:50:07 -05:00
|
|
|
|
|
|
|
while (!xml.atEnd()) {
|
|
|
|
xml.readNext();
|
|
|
|
if (xml.isStartElement() && (xml.name() == "Program_Version"))
|
|
|
|
newVersion = xml.readElementText();
|
|
|
|
}
|
|
|
|
|
|
|
|
qDebug("Updater: latest version = %s", qPrintable(newVersion));
|
|
|
|
if (!newVersion.isEmpty() && isVersionNewer(newVersion, QString(version)))
|
|
|
|
emit newVersionAvailable(newVersion);
|
|
|
|
|
2019-05-22 08:15:06 -05:00
|
|
|
emit latestVersion(newVersion);
|
|
|
|
|
2015-04-16 11:50:07 -05:00
|
|
|
_exit:
|
|
|
|
// Job done, time to self-destruct
|
|
|
|
deleteLater();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Updater::isVersionNewer(QString newVersion, QString curVersion)
|
|
|
|
{
|
|
|
|
QStringList curVer = QString(curVersion).split('.');
|
|
|
|
QStringList newVer = QString(newVersion).split('.');
|
|
|
|
|
|
|
|
for (int i = 0; i < qMin(curVer.size(), newVer.size()); i++) {
|
|
|
|
bool isOk;
|
2016-09-17 04:51:31 -05:00
|
|
|
uint n = newVer.at(i).toUInt(&isOk);
|
|
|
|
uint c = curVer.at(i).toUInt(&isOk);
|
|
|
|
if (n > c)
|
2015-04-16 11:50:07 -05:00
|
|
|
return true;
|
2016-09-17 04:51:31 -05:00
|
|
|
else if (n < c)
|
|
|
|
return false;
|
2015-04-16 11:50:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (newVer.size() > curVer.size())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-05-31 09:53:54 -05:00
|
|
|
QString Updater::userAgent()
|
|
|
|
{
|
|
|
|
QString ua = QString("Mozilla/5.0 (%1) %2/%3 (Qt/%6)")
|
|
|
|
.arg(sysInfo())
|
|
|
|
.arg(QCoreApplication::instance()->applicationName())
|
|
|
|
.arg(version)
|
|
|
|
.arg(qVersion());
|
|
|
|
|
|
|
|
return ua;
|
|
|
|
}
|
2015-04-16 11:50:07 -05:00
|
|
|
|
2016-05-31 09:53:54 -05:00
|
|
|
QString Updater::sysInfo()
|
|
|
|
{
|
2019-06-06 11:06:12 -05:00
|
|
|
#if QT_VERSION >= 0x050400
|
|
|
|
return QSysInfo::prettyProductName();
|
|
|
|
#else
|
2016-05-31 09:53:54 -05:00
|
|
|
#if defined(Q_OS_WIN32)
|
|
|
|
return QString("Windows/0x%1").arg(QSysInfo::WindowsVersion, 0, 16);
|
|
|
|
#elif defined(Q_OS_LINUX)
|
|
|
|
return QString("Linux");
|
|
|
|
#elif defined(Q_OS_MAC)
|
|
|
|
return QString("MacOSX/0x%1").arg(QSysInfo::MacintoshVersion, 0, 16);
|
|
|
|
#elif defined(Q_OS_BSD4)
|
|
|
|
return QString("BSD");
|
|
|
|
#elif defined(Q_OS_UNIX)
|
|
|
|
return QString("Unix");
|
|
|
|
#else
|
|
|
|
return QString("Unknown");
|
|
|
|
#endif
|
2019-06-06 11:06:12 -05:00
|
|
|
#endif
|
2016-05-31 09:53:54 -05:00
|
|
|
}
|