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