Mercurial > hg > easyhg
diff src/versiontester.cpp @ 663:f9b805d8cab4 qt5
build with qt5 (in branch currently)
author | Chris Cannam |
---|---|
date | Thu, 14 Mar 2013 16:43:30 +0000 |
parents | ae67ea0af696 |
children |
line wrap: on
line diff
--- a/src/versiontester.cpp Fri Mar 01 09:20:11 2013 +0000 +++ b/src/versiontester.cpp Thu Mar 14 16:43:30 2013 +0000 @@ -20,24 +20,30 @@ #include <iostream> -#include <QHttp> +#include <QNetworkAccessManager> + +static QNetworkAccessManager nm; VersionTester::VersionTester(QString hostname, QString versionFilePath, QString myVersion) : - m_httpFailed(false), - m_myVersion(myVersion) + m_myVersion(myVersion), + m_reply(0), + m_httpFailed(false) { - QHttp *http = new QHttp(); - connect(http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader &)), - this, SLOT(httpResponseHeaderReceived(const QHttpResponseHeader &))); - connect(http, SIGNAL(done(bool)), - this, SLOT(httpDone(bool))); - http->setHost(hostname); - http->get(versionFilePath); + QUrl url(QString("http://%1/%2").arg(hostname).arg(versionFilePath)); + std::cerr << "VersionTester: URL is " << url << std::endl; + m_reply = nm.get(QNetworkRequest(url)); + connect(m_reply, SIGNAL(error(QNetworkReply::NetworkError)), + this, SLOT(error(QNetworkReply::NetworkError))); + connect(m_reply, SIGNAL(finished()), this, SLOT(finished())); } VersionTester::~VersionTester() { + if (m_reply) { + m_reply->abort(); + m_reply->deleteLater(); + } } bool @@ -66,31 +72,35 @@ } void -VersionTester::httpResponseHeaderReceived(const QHttpResponseHeader &h) +VersionTester::error(QNetworkReply::NetworkError) { - if (h.statusCode() / 100 != 2) m_httpFailed = true; + std::cerr << "VersionTester: error: " << m_reply->errorString() << std::endl; + m_httpFailed = true; } void -VersionTester::httpDone(bool error) +VersionTester::finished() { - QHttp *http = const_cast<QHttp *>(dynamic_cast<const QHttp *>(sender())); - if (!http) return; - http->deleteLater(); - if (error || m_httpFailed) return; + m_reply->deleteLater(); + if (m_httpFailed) return; - QByteArray responseData = http->readAll(); + int status = m_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); + if (status / 100 != 2) { + std::cerr << "VersionTester: error: http status = " << status << std::endl; + return; + } + + QByteArray responseData = m_reply->readAll(); QString str = QString::fromUtf8(responseData.data()); QStringList lines = str.split('\n', QString::SkipEmptyParts); if (lines.empty()) return; QString latestVersion = lines[0]; DEBUG << "Comparing current version \"" << m_myVersion - << "\" with latest version \"" << latestVersion - << "\"" << endl; + << "\" with latest version \"" << latestVersion + << "\"" << endl; if (isVersionNewerThan(latestVersion, m_myVersion)) { emit newerVersionAvailable(latestVersion); } } -