Chris@491: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@491: Chris@491: /* Chris@491: EasyMercurial Chris@491: Chris@491: Based on hgExplorer by Jari Korhonen Chris@491: Copyright (c) 2010 Jari Korhonen Chris@644: Copyright (c) 2013 Chris Cannam Chris@644: Copyright (c) 2013 Queen Mary, University of London Chris@491: Chris@491: This program is free software; you can redistribute it and/or Chris@491: modify it under the terms of the GNU General Public License as Chris@491: published by the Free Software Foundation; either version 2 of the Chris@491: License, or (at your option) any later version. See the file Chris@491: COPYING included with this distribution for more information. Chris@491: */ Chris@491: Chris@491: #include "versiontester.h" Chris@491: #include "debug.h" Chris@491: Chris@491: #include Chris@491: Chris@663: #include Chris@663: Chris@663: static QNetworkAccessManager nm; Chris@491: Chris@491: VersionTester::VersionTester(QString hostname, QString versionFilePath, Chris@491: QString myVersion) : Chris@663: m_myVersion(myVersion), Chris@663: m_reply(0), Chris@663: m_httpFailed(false) Chris@491: { Chris@663: QUrl url(QString("http://%1/%2").arg(hostname).arg(versionFilePath)); Chris@663: std::cerr << "VersionTester: URL is " << url << std::endl; Chris@663: m_reply = nm.get(QNetworkRequest(url)); Chris@663: connect(m_reply, SIGNAL(error(QNetworkReply::NetworkError)), Chris@663: this, SLOT(error(QNetworkReply::NetworkError))); Chris@663: connect(m_reply, SIGNAL(finished()), this, SLOT(finished())); Chris@491: } Chris@491: Chris@491: VersionTester::~VersionTester() Chris@491: { Chris@663: if (m_reply) { Chris@663: m_reply->abort(); Chris@663: m_reply->deleteLater(); Chris@663: } Chris@491: } Chris@491: Chris@491: bool Chris@491: VersionTester::isVersionNewerThan(QString a, QString b) Chris@491: { Chris@491: QRegExp re("[._-]"); Chris@491: QStringList alist = a.split(re, QString::SkipEmptyParts); Chris@491: QStringList blist = b.split(re, QString::SkipEmptyParts); Chris@491: int ae = alist.size(); Chris@491: int be = blist.size(); Chris@491: int e = std::max(ae, be); Chris@491: for (int i = 0; i < e; ++i) { Chris@491: int an = 0, bn = 0; Chris@491: if (i < ae) { Chris@491: an = alist[i].toInt(); Chris@491: if (an == 0) an = -1; // non-numeric field -> "-pre1" etc Chris@491: } Chris@491: if (i < be) { Chris@491: bn = blist[i].toInt(); Chris@491: if (bn == 0) bn = -1; Chris@491: } Chris@491: if (an < bn) return false; Chris@491: if (an > bn) return true; Chris@491: } Chris@491: return false; Chris@491: } Chris@491: Chris@491: void Chris@663: VersionTester::error(QNetworkReply::NetworkError) Chris@491: { Chris@663: std::cerr << "VersionTester: error: " << m_reply->errorString() << std::endl; Chris@663: m_httpFailed = true; Chris@491: } Chris@491: Chris@491: void Chris@663: VersionTester::finished() Chris@491: { Chris@663: m_reply->deleteLater(); Chris@663: if (m_httpFailed) return; Chris@491: Chris@663: int status = m_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); Chris@663: if (status / 100 != 2) { Chris@663: std::cerr << "VersionTester: error: http status = " << status << std::endl; Chris@663: return; Chris@663: } Chris@663: Chris@663: QByteArray responseData = m_reply->readAll(); Chris@491: QString str = QString::fromUtf8(responseData.data()); Chris@491: QStringList lines = str.split('\n', QString::SkipEmptyParts); Chris@491: if (lines.empty()) return; Chris@491: Chris@491: QString latestVersion = lines[0]; Chris@491: DEBUG << "Comparing current version \"" << m_myVersion Chris@663: << "\" with latest version \"" << latestVersion Chris@663: << "\"" << endl; Chris@491: if (isVersionNewerThan(latestVersion, m_myVersion)) { Chris@491: emit newerVersionAvailable(latestVersion); Chris@491: } Chris@491: } Chris@491: