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@491: Copyright (c) 2011 Chris Cannam Chris@491: Copyright (c) 2011 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@491: #include Chris@491: Chris@491: VersionTester::VersionTester(QString hostname, QString versionFilePath, Chris@491: QString myVersion) : Chris@491: m_httpFailed(false), Chris@491: m_myVersion(myVersion) Chris@491: { Chris@491: QHttp *http = new QHttp(); Chris@491: connect(http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader &)), Chris@491: this, SLOT(httpResponseHeaderReceived(const QHttpResponseHeader &))); Chris@491: connect(http, SIGNAL(done(bool)), Chris@491: this, SLOT(httpDone(bool))); Chris@491: http->setHost(hostname); Chris@491: http->get(versionFilePath); Chris@491: } Chris@491: Chris@491: VersionTester::~VersionTester() Chris@491: { 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@491: VersionTester::httpResponseHeaderReceived(const QHttpResponseHeader &h) Chris@491: { Chris@491: if (h.statusCode() / 100 != 2) m_httpFailed = true; Chris@491: } Chris@491: Chris@491: void Chris@491: VersionTester::httpDone(bool error) Chris@491: { Chris@491: QHttp *http = const_cast(dynamic_cast(sender())); Chris@491: if (!http) return; Chris@491: http->deleteLater(); Chris@491: if (error || m_httpFailed) return; Chris@491: Chris@491: QByteArray responseData = http->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@491: << "\" with latest version \"" << latestVersion Chris@491: << "\"" << endl; Chris@491: if (isVersionNewerThan(latestVersion, m_myVersion)) { Chris@491: emit newerVersionAvailable(latestVersion); Chris@491: } Chris@491: } Chris@491: Chris@491: