1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
/*
CheckForUpdatesJob.cpp
This file is part of Charm, a task-based time tracking application.
Copyright (C) 2011-2018 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
Author: Michel Boyer de la Giroday <michel.giroday@kdab.com>
This program 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 2 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 "CheckForUpdatesJob.h"
#include <QBuffer>
#include <QByteArray>
#include <QDomDocument>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QStringList>
bool Charm::versionLessThan(const QString &lhs, const QString &rhs)
{
const QStringList lhsSplit = lhs.split(QLatin1Char('.'));
const QStringList rhsSplit = rhs.split(QLatin1Char('.'));
for (int i = 0; i < lhsSplit.count() && i < rhsSplit.count(); ++i) {
const int diff = rhsSplit[i].toInt() - lhsSplit[i].toInt();
if (diff != 0)
return diff > 0;
}
for (int i = lhsSplit.size(); i < rhsSplit.size(); ++i) {
if (rhsSplit[i].toInt() > 0)
return true;
}
return false;
}
CheckForUpdatesJob::CheckForUpdatesJob(QObject *parent)
: QObject(parent)
{
}
CheckForUpdatesJob::~CheckForUpdatesJob()
{
}
void CheckForUpdatesJob::start()
{
Q_ASSERT(!m_url.toString().isEmpty());
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
connect(manager, &QNetworkAccessManager::finished, this, &CheckForUpdatesJob::jobFinished);
manager->get(QNetworkRequest(m_url));
}
void CheckForUpdatesJob::jobFinished(QNetworkReply *reply)
{
if (reply->error()) {
const QString errorString = tr("Could not download update information from %1: %2").arg(
m_url.toString(), reply->errorString());
m_jobData.errorString = errorString;
m_jobData.error = reply->error();
} else {
QByteArray data = reply->readAll();
parseXmlData(data);
}
reply->deleteLater();
emit finished(m_jobData);
deleteLater();
}
void CheckForUpdatesJob::setVerbose(bool verbose)
{
m_jobData.verbose = verbose;
}
void CheckForUpdatesJob::parseXmlData(const QByteArray &data)
{
QBuffer buffer;
buffer.setData(data);
buffer.open(QIODevice::ReadOnly);
QDomDocument document;
QString errorMessage;
int errorLine = 0;
int errorColumn = 0;
if (!document.setContent(&buffer, &errorMessage, &errorLine, &errorColumn)) {
m_jobData.errorString = tr("Invalid XML: [%1:%2] %3").arg(QString::number(
errorLine),
QString::number(
errorColumn), errorMessage);
m_jobData.error = 999; // this value is just to have an and does not mean anything - error != 0
return;
}
QDomElement element = document.documentElement();
QDomElement versionElement = element.firstChildElement(QStringLiteral("version"));
QDomElement linkElement = versionElement.nextSiblingElement(QStringLiteral("link"));
const QString releaseVersion = versionElement.text();
m_jobData.releaseVersion = releaseVersion;
QUrl link(linkElement.text());
m_jobData.link = link;
QString releaseInfoLink(linkElement.nextSiblingElement(QStringLiteral("releaseinfolink")).text());
m_jobData.releaseInformationLink = releaseInfoLink;
}
void CheckForUpdatesJob::setUrl(const QUrl &url)
{
m_url = url;
}
|