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@263
|
28 static QNetworkAccessManager nm;
|
Chris@180
|
29
|
Chris@180
|
30 VersionTester::VersionTester(QString hostname, QString versionFilePath,
|
Chris@180
|
31 QString myVersion) :
|
Chris@263
|
32 m_myVersion(myVersion),
|
Chris@263
|
33 m_reply(0),
|
Chris@263
|
34 m_httpFailed(false)
|
Chris@180
|
35 {
|
Chris@263
|
36 QUrl url(QString("http://%1/%2").arg(hostname).arg(versionFilePath));
|
Chris@263
|
37 std::cerr << "VersionTester: URL is " << url << std::endl;
|
Chris@263
|
38 m_reply = 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@180
|
50 }
|
Chris@180
|
51
|
Chris@180
|
52 bool
|
Chris@180
|
53 VersionTester::isVersionNewerThan(QString a, QString b)
|
Chris@180
|
54 {
|
Chris@180
|
55 QRegExp re("[._-]");
|
Chris@180
|
56 QStringList alist = a.split(re, QString::SkipEmptyParts);
|
Chris@180
|
57 QStringList blist = b.split(re, QString::SkipEmptyParts);
|
Chris@180
|
58 int ae = alist.size();
|
Chris@180
|
59 int be = blist.size();
|
Chris@180
|
60 int e = std::max(ae, be);
|
Chris@180
|
61 for (int i = 0; i < e; ++i) {
|
Chris@180
|
62 int an = 0, bn = 0;
|
Chris@180
|
63 if (i < ae) {
|
Chris@180
|
64 an = alist[i].toInt();
|
Chris@180
|
65 if (an == 0) an = -1; // non-numeric field -> "-pre1" etc
|
Chris@180
|
66 }
|
Chris@180
|
67 if (i < be) {
|
Chris@180
|
68 bn = blist[i].toInt();
|
Chris@180
|
69 if (bn == 0) bn = -1;
|
Chris@180
|
70 }
|
Chris@180
|
71 if (an < bn) return false;
|
Chris@180
|
72 if (an > bn) return true;
|
Chris@180
|
73 }
|
Chris@180
|
74 return false;
|
Chris@180
|
75 }
|
Chris@180
|
76
|
Chris@180
|
77 void
|
Chris@263
|
78 VersionTester::error(QNetworkReply::NetworkError)
|
Chris@180
|
79 {
|
Chris@263
|
80 std::cerr << "VersionTester: error: " << m_reply->errorString() << std::endl;
|
Chris@263
|
81 m_httpFailed = true;
|
Chris@180
|
82 }
|
Chris@180
|
83
|
Chris@180
|
84 void
|
Chris@263
|
85 VersionTester::finished()
|
Chris@180
|
86 {
|
Chris@263
|
87 m_reply->deleteLater();
|
Chris@263
|
88 if (m_httpFailed) return;
|
Chris@180
|
89
|
Chris@263
|
90 int status = m_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
Chris@263
|
91 if (status / 100 != 2) {
|
Chris@263
|
92 std::cerr << "VersionTester: error: http status = " << status << std::endl;
|
Chris@263
|
93 return;
|
Chris@263
|
94 }
|
Chris@263
|
95
|
Chris@263
|
96 QByteArray responseData = m_reply->readAll();
|
Chris@180
|
97 QString str = QString::fromUtf8(responseData.data());
|
Chris@180
|
98 QStringList lines = str.split('\n', QString::SkipEmptyParts);
|
Chris@180
|
99 if (lines.empty()) return;
|
Chris@180
|
100
|
Chris@180
|
101 QString latestVersion = lines[0];
|
Chris@233
|
102 SVDEBUG << "Comparing current version \"" << m_myVersion << "\" with latest version \"" << latestVersion << "\"" << endl;
|
Chris@180
|
103 if (isVersionNewerThan(latestVersion, m_myVersion)) {
|
Chris@180
|
104 emit newerVersionAvailable(latestVersion);
|
Chris@180
|
105 }
|
Chris@180
|
106 }
|
Chris@180
|
107
|
Chris@180
|
108
|