VersionTester.cpp
Go to the documentation of this file.
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 #include "base/Debug.h"
23 
24 #include <iostream>
25 
26 #include <QNetworkAccessManager>
27 
28 
29 VersionTester::VersionTester(QString hostname, QString versionFilePath,
30  QString myVersion) :
31  m_myVersion(myVersion),
32  m_reply(nullptr),
33  m_httpFailed(false),
34  m_nm(new QNetworkAccessManager)
35 {
36  QUrl url(QString("http://%1/%2").arg(hostname).arg(versionFilePath));
37  SVDEBUG << "VersionTester: URL is " << url << endl;
38  m_reply = m_nm->get(QNetworkRequest(url));
39  connect(m_reply, SIGNAL(error(QNetworkReply::NetworkError)),
40  this, SLOT(error(QNetworkReply::NetworkError)));
41  connect(m_reply, SIGNAL(finished()), this, SLOT(finished()));
42 }
43 
45 {
46  if (m_reply) {
47  m_reply->abort();
48  m_reply->deleteLater();
49  }
50  delete m_nm;
51 }
52 
53 bool
54 VersionTester::isVersionNewerThan(QString a, QString b)
55 {
56  QRegExp re("[._-]");
57  QStringList alist = a.split(re, QString::SkipEmptyParts);
58  QStringList blist = b.split(re, QString::SkipEmptyParts);
59  int ae = alist.size();
60  int be = blist.size();
61  int e = std::max(ae, be);
62  for (int i = 0; i < e; ++i) {
63  int an = 0, bn = 0;
64  if (i < ae) {
65  an = alist[i].toInt();
66  if (an == 0 && alist[i] != "0") {
67  an = -1; // non-numeric field -> "-pre1" etc
68  }
69  }
70  if (i < be) {
71  bn = blist[i].toInt();
72  if (bn == 0 && blist[i] != "0") {
73  bn = -1;
74  }
75  }
76  if (an < bn) return false;
77  if (an > bn) return true;
78  }
79  return false;
80 }
81 
82 void
83 VersionTester::error(QNetworkReply::NetworkError)
84 {
85  SVDEBUG << "VersionTester: error: " << m_reply->errorString() << endl;
86  m_httpFailed = true;
87 }
88 
89 void
91 {
92  QNetworkReply *r = m_reply;
93  m_reply = nullptr;
94 
95  r->deleteLater();
96  if (m_httpFailed) return;
97 
98  int status = r->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
99  if (status / 100 != 2) {
100  SVDEBUG << "VersionTester: error: http status = " << status << endl;
101  return;
102  }
103 
104  QByteArray responseData = r->readAll();
105  QString str = QString::fromUtf8(responseData.data());
106  QStringList lines = str.split('\n', QString::SkipEmptyParts);
107  if (lines.empty()) return;
108 
109  QString latestVersion = lines[0];
110  SVDEBUG << "Comparing current version \"" << m_myVersion << "\" with latest version \"" << latestVersion << "\"" << endl;
111  if (isVersionNewerThan(latestVersion, m_myVersion)) {
112  SVDEBUG << "Latest version \"" << latestVersion << "\" is newer than current version \"" << m_myVersion << "\"" << endl;
113  emit newerVersionAvailable(latestVersion);
114  }
115 }
116 
117 
static bool isVersionNewerThan(QString, QString)
QString m_myVersion
Definition: VersionTester.h:49
virtual ~VersionTester()
void newerVersionAvailable(QString)
void error(QNetworkReply::NetworkError)
QNetworkAccessManager * m_nm
Definition: VersionTester.h:52
VersionTester(QString hostname, QString versionFilePath, QString myVersion)
QNetworkReply * m_reply
Definition: VersionTester.h:50