Chris@180: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@180: Chris@180: /* Chris@180: Sonic Visualiser Chris@180: An audio file viewer and annotation editor. Chris@180: Centre for Digital Music, Queen Mary, University of London. Chris@180: Chris@180: This program is free software; you can redistribute it and/or Chris@180: modify it under the terms of the GNU General Public License as Chris@180: published by the Free Software Foundation; either version 2 of the Chris@180: License, or (at your option) any later version. See the file Chris@180: COPYING included with this distribution for more information. Chris@180: */ Chris@180: Chris@180: /* Chris@180: This is a modified version of a source file from the Chris@180: Rosegarden MIDI and audio sequencer and notation editor. Chris@180: This file copyright 2000-2009 Chris Cannam. Chris@180: */ Chris@180: Chris@180: #include "VersionTester.h" Chris@229: #include "base/Debug.h" Chris@180: Chris@180: #include Chris@180: Chris@180: #include Chris@180: Chris@180: VersionTester::VersionTester(QString hostname, QString versionFilePath, Chris@180: QString myVersion) : Chris@180: m_httpFailed(false), Chris@180: m_myVersion(myVersion) Chris@180: { Chris@180: QHttp *http = new QHttp(); Chris@180: connect(http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader &)), Chris@180: this, SLOT(httpResponseHeaderReceived(const QHttpResponseHeader &))); Chris@180: connect(http, SIGNAL(done(bool)), Chris@180: this, SLOT(httpDone(bool))); Chris@180: http->setHost(hostname); Chris@180: http->get(versionFilePath); Chris@180: } Chris@180: Chris@180: VersionTester::~VersionTester() Chris@180: { Chris@180: } Chris@180: Chris@180: bool Chris@180: VersionTester::isVersionNewerThan(QString a, QString b) Chris@180: { Chris@180: QRegExp re("[._-]"); Chris@180: QStringList alist = a.split(re, QString::SkipEmptyParts); Chris@180: QStringList blist = b.split(re, QString::SkipEmptyParts); Chris@180: int ae = alist.size(); Chris@180: int be = blist.size(); Chris@180: int e = std::max(ae, be); Chris@180: for (int i = 0; i < e; ++i) { Chris@180: int an = 0, bn = 0; Chris@180: if (i < ae) { Chris@180: an = alist[i].toInt(); Chris@180: if (an == 0) an = -1; // non-numeric field -> "-pre1" etc Chris@180: } Chris@180: if (i < be) { Chris@180: bn = blist[i].toInt(); Chris@180: if (bn == 0) bn = -1; Chris@180: } Chris@180: if (an < bn) return false; Chris@180: if (an > bn) return true; Chris@180: } Chris@180: return false; Chris@180: } Chris@180: Chris@180: void Chris@180: VersionTester::httpResponseHeaderReceived(const QHttpResponseHeader &h) Chris@180: { Chris@180: if (h.statusCode() / 100 != 2) m_httpFailed = true; Chris@180: } Chris@180: Chris@180: void Chris@180: VersionTester::httpDone(bool error) Chris@180: { Chris@180: QHttp *http = const_cast(dynamic_cast(sender())); Chris@180: if (!http) return; Chris@180: http->deleteLater(); Chris@180: if (error || m_httpFailed) return; Chris@180: Chris@180: QByteArray responseData = http->readAll(); Chris@180: QString str = QString::fromUtf8(responseData.data()); Chris@180: QStringList lines = str.split('\n', QString::SkipEmptyParts); Chris@180: if (lines.empty()) return; Chris@180: Chris@180: QString latestVersion = lines[0]; Chris@229: DEBUG << "Comparing current version \"" << m_myVersion << "\" with latest version \"" << latestVersion << "\"" << endl; Chris@180: if (isVersionNewerThan(latestVersion, m_myVersion)) { Chris@180: emit newerVersionAvailable(latestVersion); Chris@180: } Chris@180: } Chris@180: Chris@180: