svapp
1.9
|
00001 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ 00002 00003 /* 00004 Sonic Visualiser 00005 An audio file viewer and annotation editor. 00006 Centre for Digital Music, Queen Mary, University of London. 00007 00008 This program is free software; you can redistribute it and/or 00009 modify it under the terms of the GNU General Public License as 00010 published by the Free Software Foundation; either version 2 of the 00011 License, or (at your option) any later version. See the file 00012 COPYING included with this distribution for more information. 00013 */ 00014 00015 /* 00016 This is a modified version of a source file from the 00017 Rosegarden MIDI and audio sequencer and notation editor. 00018 This file copyright 2000-2009 Chris Cannam. 00019 */ 00020 00021 #include "VersionTester.h" 00022 #include "base/Debug.h" 00023 00024 #include <iostream> 00025 00026 #include <QNetworkAccessManager> 00027 00028 00029 VersionTester::VersionTester(QString hostname, QString versionFilePath, 00030 QString myVersion) : 00031 m_myVersion(myVersion), 00032 m_reply(0), 00033 m_httpFailed(false), 00034 m_nm(new QNetworkAccessManager) 00035 { 00036 QUrl url(QString("http://%1/%2").arg(hostname).arg(versionFilePath)); 00037 cerr << "VersionTester: URL is " << url << endl; 00038 m_reply = m_nm->get(QNetworkRequest(url)); 00039 connect(m_reply, SIGNAL(error(QNetworkReply::NetworkError)), 00040 this, SLOT(error(QNetworkReply::NetworkError))); 00041 connect(m_reply, SIGNAL(finished()), this, SLOT(finished())); 00042 } 00043 00044 VersionTester::~VersionTester() 00045 { 00046 if (m_reply) { 00047 m_reply->abort(); 00048 m_reply->deleteLater(); 00049 } 00050 delete m_nm; 00051 } 00052 00053 bool 00054 VersionTester::isVersionNewerThan(QString a, QString b) 00055 { 00056 QRegExp re("[._-]"); 00057 QStringList alist = a.split(re, QString::SkipEmptyParts); 00058 QStringList blist = b.split(re, QString::SkipEmptyParts); 00059 int ae = alist.size(); 00060 int be = blist.size(); 00061 int e = std::max(ae, be); 00062 for (int i = 0; i < e; ++i) { 00063 int an = 0, bn = 0; 00064 if (i < ae) { 00065 an = alist[i].toInt(); 00066 if (an == 0 && alist[i] != "0") { 00067 an = -1; // non-numeric field -> "-pre1" etc 00068 } 00069 } 00070 if (i < be) { 00071 bn = blist[i].toInt(); 00072 if (bn == 0 && blist[i] != "0") { 00073 bn = -1; 00074 } 00075 } 00076 if (an < bn) return false; 00077 if (an > bn) return true; 00078 } 00079 return false; 00080 } 00081 00082 void 00083 VersionTester::error(QNetworkReply::NetworkError) 00084 { 00085 cerr << "VersionTester: error: " << m_reply->errorString() << endl; 00086 m_httpFailed = true; 00087 } 00088 00089 void 00090 VersionTester::finished() 00091 { 00092 QNetworkReply *r = m_reply; 00093 m_reply = 0; 00094 00095 r->deleteLater(); 00096 if (m_httpFailed) return; 00097 00098 int status = r->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); 00099 if (status / 100 != 2) { 00100 cerr << "VersionTester: error: http status = " << status << endl; 00101 return; 00102 } 00103 00104 QByteArray responseData = r->readAll(); 00105 QString str = QString::fromUtf8(responseData.data()); 00106 QStringList lines = str.split('\n', QString::SkipEmptyParts); 00107 if (lines.empty()) return; 00108 00109 QString latestVersion = lines[0]; 00110 cerr << "Comparing current version \"" << m_myVersion << "\" with latest version \"" << latestVersion << "\"" << endl; 00111 if (isVersionNewerThan(latestVersion, m_myVersion)) { 00112 cerr << "Latest version \"" << latestVersion << "\" is newer than current version \"" << m_myVersion << "\"" << endl; 00113 emit newerVersionAvailable(latestVersion); 00114 } 00115 } 00116 00117