Mercurial > hg > easyhg
comparison src/versiontester.cpp @ 491:1d90cd7a1c5f
Add tester for newer version
author | Chris Cannam |
---|---|
date | Thu, 18 Aug 2011 13:15:38 +0100 |
parents | |
children | 533519ebc0cb |
comparison
equal
deleted
inserted
replaced
489:86cdaa346e59 | 491:1d90cd7a1c5f |
---|---|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ | |
2 | |
3 /* | |
4 EasyMercurial | |
5 | |
6 Based on hgExplorer by Jari Korhonen | |
7 Copyright (c) 2010 Jari Korhonen | |
8 Copyright (c) 2011 Chris Cannam | |
9 Copyright (c) 2011 Queen Mary, University of London | |
10 | |
11 This program is free software; you can redistribute it and/or | |
12 modify it under the terms of the GNU General Public License as | |
13 published by the Free Software Foundation; either version 2 of the | |
14 License, or (at your option) any later version. See the file | |
15 COPYING included with this distribution for more information. | |
16 */ | |
17 | |
18 #include "versiontester.h" | |
19 #include "debug.h" | |
20 | |
21 #include <iostream> | |
22 | |
23 #include <QHttp> | |
24 | |
25 VersionTester::VersionTester(QString hostname, QString versionFilePath, | |
26 QString myVersion) : | |
27 m_httpFailed(false), | |
28 m_myVersion(myVersion) | |
29 { | |
30 QHttp *http = new QHttp(); | |
31 connect(http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader &)), | |
32 this, SLOT(httpResponseHeaderReceived(const QHttpResponseHeader &))); | |
33 connect(http, SIGNAL(done(bool)), | |
34 this, SLOT(httpDone(bool))); | |
35 http->setHost(hostname); | |
36 http->get(versionFilePath); | |
37 } | |
38 | |
39 VersionTester::~VersionTester() | |
40 { | |
41 } | |
42 | |
43 bool | |
44 VersionTester::isVersionNewerThan(QString a, QString b) | |
45 { | |
46 QRegExp re("[._-]"); | |
47 QStringList alist = a.split(re, QString::SkipEmptyParts); | |
48 QStringList blist = b.split(re, QString::SkipEmptyParts); | |
49 int ae = alist.size(); | |
50 int be = blist.size(); | |
51 int e = std::max(ae, be); | |
52 for (int i = 0; i < e; ++i) { | |
53 int an = 0, bn = 0; | |
54 if (i < ae) { | |
55 an = alist[i].toInt(); | |
56 if (an == 0) an = -1; // non-numeric field -> "-pre1" etc | |
57 } | |
58 if (i < be) { | |
59 bn = blist[i].toInt(); | |
60 if (bn == 0) bn = -1; | |
61 } | |
62 if (an < bn) return false; | |
63 if (an > bn) return true; | |
64 } | |
65 return false; | |
66 } | |
67 | |
68 void | |
69 VersionTester::httpResponseHeaderReceived(const QHttpResponseHeader &h) | |
70 { | |
71 if (h.statusCode() / 100 != 2) m_httpFailed = true; | |
72 } | |
73 | |
74 void | |
75 VersionTester::httpDone(bool error) | |
76 { | |
77 QHttp *http = const_cast<QHttp *>(dynamic_cast<const QHttp *>(sender())); | |
78 if (!http) return; | |
79 http->deleteLater(); | |
80 if (error || m_httpFailed) return; | |
81 | |
82 QByteArray responseData = http->readAll(); | |
83 QString str = QString::fromUtf8(responseData.data()); | |
84 QStringList lines = str.split('\n', QString::SkipEmptyParts); | |
85 if (lines.empty()) return; | |
86 | |
87 QString latestVersion = lines[0]; | |
88 DEBUG << "Comparing current version \"" << m_myVersion | |
89 << "\" with latest version \"" << latestVersion | |
90 << "\"" << endl; | |
91 if (isVersionNewerThan(latestVersion, m_myVersion)) { | |
92 emit newerVersionAvailable(latestVersion); | |
93 } | |
94 } | |
95 | |
96 |