comparison framework/VersionTester.cpp @ 180:84b2c1a4984a

* Offer the user a chance to answer our survey (only once, and only after several runs of the program, and only if the survey is known to be live on the website)
author Chris Cannam
date Thu, 27 Aug 2009 16:31:45 +0000
parents
children 8c13e8219903
comparison
equal deleted inserted replaced
179:0db3fc28a2a1 180:84b2c1a4984a
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2
3 /*
4 Sonic Visualiser
5 An audio file viewer and annotation editor.
6 Centre for Digital Music, Queen Mary, University of London.
7
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of the
11 License, or (at your option) any later version. See the file
12 COPYING included with this distribution for more information.
13 */
14
15 /*
16 This is a modified version of a source file from the
17 Rosegarden MIDI and audio sequencer and notation editor.
18 This file copyright 2000-2009 Chris Cannam.
19 */
20
21 #include "VersionTester.h"
22
23 #include <iostream>
24
25 #include <QHttp>
26
27 VersionTester::VersionTester(QString hostname, QString versionFilePath,
28 QString myVersion) :
29 m_httpFailed(false),
30 m_myVersion(myVersion)
31 {
32 QHttp *http = new QHttp();
33 connect(http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader &)),
34 this, SLOT(httpResponseHeaderReceived(const QHttpResponseHeader &)));
35 connect(http, SIGNAL(done(bool)),
36 this, SLOT(httpDone(bool)));
37 http->setHost(hostname);
38 http->get(versionFilePath);
39 }
40
41 VersionTester::~VersionTester()
42 {
43 }
44
45 bool
46 VersionTester::isVersionNewerThan(QString a, QString b)
47 {
48 QRegExp re("[._-]");
49 QStringList alist = a.split(re, QString::SkipEmptyParts);
50 QStringList blist = b.split(re, QString::SkipEmptyParts);
51 int ae = alist.size();
52 int be = blist.size();
53 int e = std::max(ae, be);
54 for (int i = 0; i < e; ++i) {
55 int an = 0, bn = 0;
56 if (i < ae) {
57 an = alist[i].toInt();
58 if (an == 0) an = -1; // non-numeric field -> "-pre1" etc
59 }
60 if (i < be) {
61 bn = blist[i].toInt();
62 if (bn == 0) bn = -1;
63 }
64 if (an < bn) return false;
65 if (an > bn) return true;
66 }
67 return false;
68 }
69
70 void
71 VersionTester::httpResponseHeaderReceived(const QHttpResponseHeader &h)
72 {
73 if (h.statusCode() / 100 != 2) m_httpFailed = true;
74 }
75
76 void
77 VersionTester::httpDone(bool error)
78 {
79 QHttp *http = const_cast<QHttp *>(dynamic_cast<const QHttp *>(sender()));
80 if (!http) return;
81 http->deleteLater();
82 if (error || m_httpFailed) return;
83
84 QByteArray responseData = http->readAll();
85 QString str = QString::fromUtf8(responseData.data());
86 QStringList lines = str.split('\n', QString::SkipEmptyParts);
87 if (lines.empty()) return;
88
89 QString latestVersion = lines[0];
90 std::cerr << "Comparing current version \"" << m_myVersion.toStdString()
91 << "\" with latest version \"" << latestVersion.toStdString()
92 << "\"" << std::endl;
93 if (isVersionNewerThan(latestVersion, m_myVersion)) {
94 emit newerVersionAvailable(latestVersion);
95 }
96 }
97
98