From 9c1e565d33e72cd1f2cfcde11d2a6e190cdc91f0 Mon Sep 17 00:00:00 2001 From: ntim Date: Sun, 10 Jan 2016 22:17:59 +0100 Subject: [PATCH] Transition to QNetworkAccessManager for compatibility to QT5 Former-commit-id: 6e65da9338d131d1017051e505db8c28696adf4f --- libsrc/leddevice/LedDevicePhilipsHue.cpp | 37 ++++++++++-------------- libsrc/leddevice/LedDevicePhilipsHue.h | 6 ++-- 2 files changed, 19 insertions(+), 24 deletions(-) diff --git a/libsrc/leddevice/LedDevicePhilipsHue.cpp b/libsrc/leddevice/LedDevicePhilipsHue.cpp index 056e9ead..dc95d421 100755 --- a/libsrc/leddevice/LedDevicePhilipsHue.cpp +++ b/libsrc/leddevice/LedDevicePhilipsHue.cpp @@ -6,9 +6,8 @@ // qt includes #include -#include -#include #include +#include #include @@ -144,14 +143,14 @@ LedDevicePhilipsHue::LedDevicePhilipsHue(const std::string& output, const std::s int transitiontime, std::vector lightIds) : host(output.c_str()), username(username.c_str()), switchOffOnBlack(switchOffOnBlack), transitiontime( transitiontime), lightIds(lightIds) { - http = new QHttp(host); + manager = new QNetworkAccessManager(); timer.setInterval(3000); timer.setSingleShot(true); connect(&timer, SIGNAL(timeout()), this, SLOT(restoreStates())); } LedDevicePhilipsHue::~LedDevicePhilipsHue() { - delete http; + delete manager; } int LedDevicePhilipsHue::write(const std::vector & ledValues) { @@ -216,33 +215,29 @@ int LedDevicePhilipsHue::switchOff() { } void LedDevicePhilipsHue::put(QString route, QString content) { - QString url = QString("/api/%1/%2").arg(username).arg(route); - QHttpRequestHeader header("PUT", url); - header.setValue("Host", host); - header.setValue("Accept-Encoding", "identity"); - header.setValue("Connection", "keep-alive"); - header.setValue("Content-Length", QString("%1").arg(content.size())); - QEventLoop loop; - // Connect requestFinished signal to quit slot of the loop. - loop.connect(http, SIGNAL(requestFinished(int, bool)), SLOT(quit())); + QString url = QString("http://%1/api/%2/%3").arg(host).arg(username).arg(route); // Perfrom request - http->request(header, content.toAscii()); + QNetworkRequest request(url); + QNetworkReply* reply = manager->put(request, content.toAscii()); + // Connect finished signal to quit slot of the loop. + QEventLoop loop; + loop.connect(reply, SIGNAL(finished()), SLOT(quit())); // Go into the loop until the request is finished. loop.exec(); } QByteArray LedDevicePhilipsHue::get(QString route) { - QString url = QString("/api/%1/%2").arg(username).arg(route); - // Event loop to block until request finished. - QEventLoop loop; - // Connect requestFinished signal to quit slot of the loop. - loop.connect(http, SIGNAL(requestFinished(int, bool)), SLOT(quit())); + QString url = QString("http://%1/api/%2/%3").arg(host).arg(username).arg(route); // Perfrom request - http->get(url); + QNetworkRequest request(url); + QNetworkReply* reply = manager->get(request); + // Connect requestFinished signal to quit slot of the loop. + QEventLoop loop; + loop.connect(reply, SIGNAL(finished()), SLOT(quit())); // Go into the loop until the request is finished. loop.exec(); // Read all data of the response. - return http->readAll(); + return reply->readAll(); } QString LedDevicePhilipsHue::getStateRoute(unsigned int lightId) { diff --git a/libsrc/leddevice/LedDevicePhilipsHue.h b/libsrc/leddevice/LedDevicePhilipsHue.h index 59c0383d..2d6f9056 100755 --- a/libsrc/leddevice/LedDevicePhilipsHue.h +++ b/libsrc/leddevice/LedDevicePhilipsHue.h @@ -6,7 +6,7 @@ // Qt includes #include #include -#include +#include #include // Leddevice includes @@ -164,8 +164,8 @@ private: QString host; /// User name for the API ("newdeveloper") QString username; - /// Qhttp object for sending requests. - QHttp* http; + /// QNetworkAccessManager object for sending requests. + QNetworkAccessManager* manager; /// Use timer to reset lights when we got into "GRABBINGMODE_OFF". QTimer timer; ///