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@263: #include Chris@263: Chris@180: Chris@180: VersionTester::VersionTester(QString hostname, QString versionFilePath, Chris@180: QString myVersion) : Chris@263: m_myVersion(myVersion), Chris@263: m_reply(0), Chris@287: m_httpFailed(false), Chris@287: m_nm(new QNetworkAccessManager) Chris@180: { Chris@263: QUrl url(QString("http://%1/%2").arg(hostname).arg(versionFilePath)); Chris@293: cerr << "VersionTester: URL is " << url << endl; Chris@287: m_reply = m_nm->get(QNetworkRequest(url)); Chris@263: connect(m_reply, SIGNAL(error(QNetworkReply::NetworkError)), Chris@263: this, SLOT(error(QNetworkReply::NetworkError))); Chris@263: connect(m_reply, SIGNAL(finished()), this, SLOT(finished())); Chris@180: } Chris@180: Chris@180: VersionTester::~VersionTester() Chris@180: { Chris@263: if (m_reply) { Chris@263: m_reply->abort(); Chris@263: m_reply->deleteLater(); Chris@263: } Chris@287: delete m_nm; 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@336: int an = 0, bn = 0; Chris@336: if (i < ae) { Chris@336: an = alist[i].toInt(); Chris@336: if (an == 0 && alist[i] != "0") { Chris@336: an = -1; // non-numeric field -> "-pre1" etc Chris@336: } Chris@336: } Chris@336: if (i < be) { Chris@336: bn = blist[i].toInt(); Chris@336: if (bn == 0 && blist[i] != "0") { Chris@336: bn = -1; Chris@336: } Chris@336: } Chris@336: if (an < bn) return false; Chris@336: if (an > bn) return true; Chris@180: } Chris@180: return false; Chris@180: } Chris@180: Chris@180: void Chris@263: VersionTester::error(QNetworkReply::NetworkError) Chris@180: { Chris@293: cerr << "VersionTester: error: " << m_reply->errorString() << endl; Chris@263: m_httpFailed = true; Chris@180: } Chris@180: Chris@180: void Chris@263: VersionTester::finished() Chris@180: { Chris@287: QNetworkReply *r = m_reply; Chris@287: m_reply = 0; Chris@287: Chris@287: r->deleteLater(); Chris@263: if (m_httpFailed) return; Chris@180: Chris@287: int status = r->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); Chris@263: if (status / 100 != 2) { Chris@293: cerr << "VersionTester: error: http status = " << status << endl; Chris@263: return; Chris@263: } Chris@263: Chris@287: QByteArray responseData = r->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@336: cerr << "Comparing current version \"" << m_myVersion << "\" with latest version \"" << latestVersion << "\"" << endl; Chris@180: if (isVersionNewerThan(latestVersion, m_myVersion)) { Chris@336: cerr << "Latest version \"" << latestVersion << "\" is newer than current version \"" << m_myVersion << "\"" << endl; Chris@180: emit newerVersionAvailable(latestVersion); Chris@180: } Chris@180: } Chris@180: Chris@180: