annotate framework/VersionTester.cpp @ 715:ec8f21a20fa7

Use updated snap api
author Chris Cannam
date Thu, 17 Oct 2019 11:13:11 +0100
parents beefce638f91
children 1089d65c585d
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@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@180 28
Chris@180 29 VersionTester::VersionTester(QString hostname, QString versionFilePath,
Chris@595 30 QString myVersion) :
Chris@263 31 m_myVersion(myVersion),
Chris@636 32 m_reply(nullptr),
Chris@287 33 m_httpFailed(false),
Chris@287 34 m_nm(new QNetworkAccessManager)
Chris@180 35 {
Chris@263 36 QUrl url(QString("http://%1/%2").arg(hostname).arg(versionFilePath));
Chris@705 37 SVCERR << "VersionTester: URL is " << url << endl;
Chris@287 38 m_reply = m_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@287 50 delete m_nm;
Chris@180 51 }
Chris@180 52
Chris@180 53 bool
Chris@180 54 VersionTester::isVersionNewerThan(QString a, QString b)
Chris@180 55 {
Chris@180 56 QRegExp re("[._-]");
Chris@180 57 QStringList alist = a.split(re, QString::SkipEmptyParts);
Chris@180 58 QStringList blist = b.split(re, QString::SkipEmptyParts);
Chris@180 59 int ae = alist.size();
Chris@180 60 int be = blist.size();
Chris@180 61 int e = std::max(ae, be);
Chris@180 62 for (int i = 0; i < e; ++i) {
Chris@336 63 int an = 0, bn = 0;
Chris@336 64 if (i < ae) {
Chris@336 65 an = alist[i].toInt();
Chris@336 66 if (an == 0 && alist[i] != "0") {
Chris@336 67 an = -1; // non-numeric field -> "-pre1" etc
Chris@336 68 }
Chris@336 69 }
Chris@336 70 if (i < be) {
Chris@336 71 bn = blist[i].toInt();
Chris@336 72 if (bn == 0 && blist[i] != "0") {
Chris@336 73 bn = -1;
Chris@336 74 }
Chris@336 75 }
Chris@336 76 if (an < bn) return false;
Chris@336 77 if (an > bn) return true;
Chris@180 78 }
Chris@180 79 return false;
Chris@180 80 }
Chris@180 81
Chris@180 82 void
Chris@263 83 VersionTester::error(QNetworkReply::NetworkError)
Chris@180 84 {
Chris@705 85 SVCERR << "VersionTester: error: " << m_reply->errorString() << endl;
Chris@263 86 m_httpFailed = true;
Chris@180 87 }
Chris@180 88
Chris@180 89 void
Chris@263 90 VersionTester::finished()
Chris@180 91 {
Chris@287 92 QNetworkReply *r = m_reply;
Chris@636 93 m_reply = nullptr;
Chris@287 94
Chris@287 95 r->deleteLater();
Chris@263 96 if (m_httpFailed) return;
Chris@180 97
Chris@287 98 int status = r->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
Chris@263 99 if (status / 100 != 2) {
Chris@705 100 SVCERR << "VersionTester: error: http status = " << status << endl;
Chris@263 101 return;
Chris@263 102 }
Chris@263 103
Chris@287 104 QByteArray responseData = r->readAll();
Chris@180 105 QString str = QString::fromUtf8(responseData.data());
Chris@180 106 QStringList lines = str.split('\n', QString::SkipEmptyParts);
Chris@180 107 if (lines.empty()) return;
Chris@180 108
Chris@180 109 QString latestVersion = lines[0];
Chris@705 110 SVCERR << "Comparing current version \"" << m_myVersion << "\" with latest version \"" << latestVersion << "\"" << endl;
Chris@180 111 if (isVersionNewerThan(latestVersion, m_myVersion)) {
Chris@705 112 SVCERR << "Latest version \"" << latestVersion << "\" is newer than current version \"" << m_myVersion << "\"" << endl;
Chris@180 113 emit newerVersionAvailable(latestVersion);
Chris@180 114 }
Chris@180 115 }
Chris@180 116
Chris@180 117