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@180
|
26 #include <QHttp>
|
Chris@180
|
27
|
Chris@180
|
28 VersionTester::VersionTester(QString hostname, QString versionFilePath,
|
Chris@180
|
29 QString myVersion) :
|
Chris@180
|
30 m_httpFailed(false),
|
Chris@180
|
31 m_myVersion(myVersion)
|
Chris@180
|
32 {
|
Chris@180
|
33 QHttp *http = new QHttp();
|
Chris@180
|
34 connect(http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader &)),
|
Chris@180
|
35 this, SLOT(httpResponseHeaderReceived(const QHttpResponseHeader &)));
|
Chris@180
|
36 connect(http, SIGNAL(done(bool)),
|
Chris@180
|
37 this, SLOT(httpDone(bool)));
|
Chris@180
|
38 http->setHost(hostname);
|
Chris@180
|
39 http->get(versionFilePath);
|
Chris@180
|
40 }
|
Chris@180
|
41
|
Chris@180
|
42 VersionTester::~VersionTester()
|
Chris@180
|
43 {
|
Chris@180
|
44 }
|
Chris@180
|
45
|
Chris@180
|
46 bool
|
Chris@180
|
47 VersionTester::isVersionNewerThan(QString a, QString b)
|
Chris@180
|
48 {
|
Chris@180
|
49 QRegExp re("[._-]");
|
Chris@180
|
50 QStringList alist = a.split(re, QString::SkipEmptyParts);
|
Chris@180
|
51 QStringList blist = b.split(re, QString::SkipEmptyParts);
|
Chris@180
|
52 int ae = alist.size();
|
Chris@180
|
53 int be = blist.size();
|
Chris@180
|
54 int e = std::max(ae, be);
|
Chris@180
|
55 for (int i = 0; i < e; ++i) {
|
Chris@180
|
56 int an = 0, bn = 0;
|
Chris@180
|
57 if (i < ae) {
|
Chris@180
|
58 an = alist[i].toInt();
|
Chris@180
|
59 if (an == 0) an = -1; // non-numeric field -> "-pre1" etc
|
Chris@180
|
60 }
|
Chris@180
|
61 if (i < be) {
|
Chris@180
|
62 bn = blist[i].toInt();
|
Chris@180
|
63 if (bn == 0) bn = -1;
|
Chris@180
|
64 }
|
Chris@180
|
65 if (an < bn) return false;
|
Chris@180
|
66 if (an > bn) return true;
|
Chris@180
|
67 }
|
Chris@180
|
68 return false;
|
Chris@180
|
69 }
|
Chris@180
|
70
|
Chris@180
|
71 void
|
Chris@180
|
72 VersionTester::httpResponseHeaderReceived(const QHttpResponseHeader &h)
|
Chris@180
|
73 {
|
Chris@180
|
74 if (h.statusCode() / 100 != 2) m_httpFailed = true;
|
Chris@180
|
75 }
|
Chris@180
|
76
|
Chris@180
|
77 void
|
Chris@180
|
78 VersionTester::httpDone(bool error)
|
Chris@180
|
79 {
|
Chris@180
|
80 QHttp *http = const_cast<QHttp *>(dynamic_cast<const QHttp *>(sender()));
|
Chris@180
|
81 if (!http) return;
|
Chris@180
|
82 http->deleteLater();
|
Chris@180
|
83 if (error || m_httpFailed) return;
|
Chris@180
|
84
|
Chris@180
|
85 QByteArray responseData = http->readAll();
|
Chris@180
|
86 QString str = QString::fromUtf8(responseData.data());
|
Chris@180
|
87 QStringList lines = str.split('\n', QString::SkipEmptyParts);
|
Chris@180
|
88 if (lines.empty()) return;
|
Chris@180
|
89
|
Chris@180
|
90 QString latestVersion = lines[0];
|
Chris@233
|
91 SVDEBUG << "Comparing current version \"" << m_myVersion << "\" with latest version \"" << latestVersion << "\"" << endl;
|
Chris@180
|
92 if (isVersionNewerThan(latestVersion, m_myVersion)) {
|
Chris@180
|
93 emit newerVersionAvailable(latestVersion);
|
Chris@180
|
94 }
|
Chris@180
|
95 }
|
Chris@180
|
96
|
Chris@180
|
97
|