annotate framework/VersionTester.cpp @ 317:abfde177731f tonioni

Free, not delete
author Chris Cannam
date Thu, 09 Jan 2014 14:14:02 +0000
parents 068235cf5bf7
children 3144af512ea3
rev   line source
Chris@180 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@180 2
Chris@180 3 /*
Chris@180 4 Sonic Visualiser
Chris@180 5 An audio file viewer and annotation editor.
Chris@180 6 Centre for Digital Music, Queen Mary, University of London.
Chris@180 7
Chris@180 8 This program is free software; you can redistribute it and/or
Chris@180 9 modify it under the terms of the GNU General Public License as
Chris@180 10 published by the Free Software Foundation; either version 2 of the
Chris@180 11 License, or (at your option) any later version. See the file
Chris@180 12 COPYING included with this distribution for more information.
Chris@180 13 */
Chris@180 14
Chris@180 15 /*
Chris@180 16 This is a modified version of a source file from the
Chris@180 17 Rosegarden MIDI and audio sequencer and notation editor.
Chris@180 18 This file copyright 2000-2009 Chris Cannam.
Chris@180 19 */
Chris@180 20
Chris@180 21 #include "VersionTester.h"
Chris@229 22 #include "base/Debug.h"
Chris@180 23
Chris@180 24 #include <iostream>
Chris@180 25
Chris@263 26 #include <QNetworkAccessManager>
Chris@263 27
Chris@180 28
Chris@180 29 VersionTester::VersionTester(QString hostname, QString versionFilePath,
Chris@180 30 QString myVersion) :
Chris@263 31 m_myVersion(myVersion),
Chris@263 32 m_reply(0),
Chris@287 33 m_httpFailed(false),
Chris@287 34 m_nm(new QNetworkAccessManager)
Chris@180 35 {
Chris@263 36 QUrl url(QString("http://%1/%2").arg(hostname).arg(versionFilePath));
Chris@293 37 cerr << "VersionTester: URL is " << url << endl;
Chris@287 38 m_reply = m_nm->get(QNetworkRequest(url));
Chris@263 39 connect(m_reply, SIGNAL(error(QNetworkReply::NetworkError)),
Chris@263 40 this, SLOT(error(QNetworkReply::NetworkError)));
Chris@263 41 connect(m_reply, SIGNAL(finished()), this, SLOT(finished()));
Chris@180 42 }
Chris@180 43
Chris@180 44 VersionTester::~VersionTester()
Chris@180 45 {
Chris@263 46 if (m_reply) {
Chris@263 47 m_reply->abort();
Chris@263 48 m_reply->deleteLater();
Chris@263 49 }
Chris@287 50 delete m_nm;
Chris@180 51 }
Chris@180 52
Chris@180 53 bool
Chris@180 54 VersionTester::isVersionNewerThan(QString a, QString b)
Chris@180 55 {
Chris@180 56 QRegExp re("[._-]");
Chris@180 57 QStringList alist = a.split(re, QString::SkipEmptyParts);
Chris@180 58 QStringList blist = b.split(re, QString::SkipEmptyParts);
Chris@180 59 int ae = alist.size();
Chris@180 60 int be = blist.size();
Chris@180 61 int e = std::max(ae, be);
Chris@180 62 for (int i = 0; i < e; ++i) {
Chris@180 63 int an = 0, bn = 0;
Chris@180 64 if (i < ae) {
Chris@180 65 an = alist[i].toInt();
Chris@180 66 if (an == 0) an = -1; // non-numeric field -> "-pre1" etc
Chris@180 67 }
Chris@180 68 if (i < be) {
Chris@180 69 bn = blist[i].toInt();
Chris@180 70 if (bn == 0) bn = -1;
Chris@180 71 }
Chris@180 72 if (an < bn) return false;
Chris@180 73 if (an > bn) return true;
Chris@180 74 }
Chris@180 75 return false;
Chris@180 76 }
Chris@180 77
Chris@180 78 void
Chris@263 79 VersionTester::error(QNetworkReply::NetworkError)
Chris@180 80 {
Chris@293 81 cerr << "VersionTester: error: " << m_reply->errorString() << endl;
Chris@263 82 m_httpFailed = true;
Chris@180 83 }
Chris@180 84
Chris@180 85 void
Chris@263 86 VersionTester::finished()
Chris@180 87 {
Chris@287 88 QNetworkReply *r = m_reply;
Chris@287 89 m_reply = 0;
Chris@287 90
Chris@287 91 r->deleteLater();
Chris@263 92 if (m_httpFailed) return;
Chris@180 93
Chris@287 94 int status = r->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
Chris@263 95 if (status / 100 != 2) {
Chris@293 96 cerr << "VersionTester: error: http status = " << status << endl;
Chris@263 97 return;
Chris@263 98 }
Chris@263 99
Chris@287 100 QByteArray responseData = r->readAll();
Chris@180 101 QString str = QString::fromUtf8(responseData.data());
Chris@180 102 QStringList lines = str.split('\n', QString::SkipEmptyParts);
Chris@180 103 if (lines.empty()) return;
Chris@180 104
Chris@180 105 QString latestVersion = lines[0];
Chris@233 106 SVDEBUG << "Comparing current version \"" << m_myVersion << "\" with latest version \"" << latestVersion << "\"" << endl;
Chris@180 107 if (isVersionNewerThan(latestVersion, m_myVersion)) {
Chris@180 108 emit newerVersionAvailable(latestVersion);
Chris@180 109 }
Chris@180 110 }
Chris@180 111
Chris@180 112