annotate src/versiontester.cpp @ 494:41a156d439d0

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