annotate src/versiontester.cpp @ 698:52b8a499f379

Further packaging/signing fixes
author Chris Cannam
date Mon, 10 Dec 2018 17:52:27 +0000
parents f9b805d8cab4
children
rev   line source
Chris@491 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@491 2
Chris@491 3 /*
Chris@491 4 EasyMercurial
Chris@491 5
Chris@491 6 Based on hgExplorer by Jari Korhonen
Chris@491 7 Copyright (c) 2010 Jari Korhonen
Chris@644 8 Copyright (c) 2013 Chris Cannam
Chris@644 9 Copyright (c) 2013 Queen Mary, University of London
Chris@491 10
Chris@491 11 This program is free software; you can redistribute it and/or
Chris@491 12 modify it under the terms of the GNU General Public License as
Chris@491 13 published by the Free Software Foundation; either version 2 of the
Chris@491 14 License, or (at your option) any later version. See the file
Chris@491 15 COPYING included with this distribution for more information.
Chris@491 16 */
Chris@491 17
Chris@491 18 #include "versiontester.h"
Chris@491 19 #include "debug.h"
Chris@491 20
Chris@491 21 #include <iostream>
Chris@491 22
Chris@663 23 #include <QNetworkAccessManager>
Chris@663 24
Chris@663 25 static QNetworkAccessManager nm;
Chris@491 26
Chris@491 27 VersionTester::VersionTester(QString hostname, QString versionFilePath,
Chris@491 28 QString myVersion) :
Chris@663 29 m_myVersion(myVersion),
Chris@663 30 m_reply(0),
Chris@663 31 m_httpFailed(false)
Chris@491 32 {
Chris@663 33 QUrl url(QString("http://%1/%2").arg(hostname).arg(versionFilePath));
Chris@663 34 std::cerr << "VersionTester: URL is " << url << std::endl;
Chris@663 35 m_reply = nm.get(QNetworkRequest(url));
Chris@663 36 connect(m_reply, SIGNAL(error(QNetworkReply::NetworkError)),
Chris@663 37 this, SLOT(error(QNetworkReply::NetworkError)));
Chris@663 38 connect(m_reply, SIGNAL(finished()), this, SLOT(finished()));
Chris@491 39 }
Chris@491 40
Chris@491 41 VersionTester::~VersionTester()
Chris@491 42 {
Chris@663 43 if (m_reply) {
Chris@663 44 m_reply->abort();
Chris@663 45 m_reply->deleteLater();
Chris@663 46 }
Chris@491 47 }
Chris@491 48
Chris@491 49 bool
Chris@491 50 VersionTester::isVersionNewerThan(QString a, QString b)
Chris@491 51 {
Chris@491 52 QRegExp re("[._-]");
Chris@491 53 QStringList alist = a.split(re, QString::SkipEmptyParts);
Chris@491 54 QStringList blist = b.split(re, QString::SkipEmptyParts);
Chris@491 55 int ae = alist.size();
Chris@491 56 int be = blist.size();
Chris@491 57 int e = std::max(ae, be);
Chris@491 58 for (int i = 0; i < e; ++i) {
Chris@491 59 int an = 0, bn = 0;
Chris@491 60 if (i < ae) {
Chris@491 61 an = alist[i].toInt();
Chris@491 62 if (an == 0) an = -1; // non-numeric field -> "-pre1" etc
Chris@491 63 }
Chris@491 64 if (i < be) {
Chris@491 65 bn = blist[i].toInt();
Chris@491 66 if (bn == 0) bn = -1;
Chris@491 67 }
Chris@491 68 if (an < bn) return false;
Chris@491 69 if (an > bn) return true;
Chris@491 70 }
Chris@491 71 return false;
Chris@491 72 }
Chris@491 73
Chris@491 74 void
Chris@663 75 VersionTester::error(QNetworkReply::NetworkError)
Chris@491 76 {
Chris@663 77 std::cerr << "VersionTester: error: " << m_reply->errorString() << std::endl;
Chris@663 78 m_httpFailed = true;
Chris@491 79 }
Chris@491 80
Chris@491 81 void
Chris@663 82 VersionTester::finished()
Chris@491 83 {
Chris@663 84 m_reply->deleteLater();
Chris@663 85 if (m_httpFailed) return;
Chris@491 86
Chris@663 87 int status = m_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
Chris@663 88 if (status / 100 != 2) {
Chris@663 89 std::cerr << "VersionTester: error: http status = " << status << std::endl;
Chris@663 90 return;
Chris@663 91 }
Chris@663 92
Chris@663 93 QByteArray responseData = m_reply->readAll();
Chris@491 94 QString str = QString::fromUtf8(responseData.data());
Chris@491 95 QStringList lines = str.split('\n', QString::SkipEmptyParts);
Chris@491 96 if (lines.empty()) return;
Chris@491 97
Chris@491 98 QString latestVersion = lines[0];
Chris@491 99 DEBUG << "Comparing current version \"" << m_myVersion
Chris@663 100 << "\" with latest version \"" << latestVersion
Chris@663 101 << "\"" << endl;
Chris@491 102 if (isVersionNewerThan(latestVersion, m_myVersion)) {
Chris@491 103 emit newerVersionAvailable(latestVersion);
Chris@491 104 }
Chris@491 105 }
Chris@491 106