Transition to QNetworkAccessManager for compatibility to QT5

Former-commit-id: 6e65da9338d131d1017051e505db8c28696adf4f
This commit is contained in:
ntim 2016-01-10 22:17:59 +01:00
parent d5529e86df
commit 9c1e565d33
2 changed files with 19 additions and 24 deletions

View File

@ -6,9 +6,8 @@
// qt includes // qt includes
#include <QtCore/qmath.h> #include <QtCore/qmath.h>
#include <QUrl>
#include <QHttpRequestHeader>
#include <QEventLoop> #include <QEventLoop>
#include <QNetworkReply>
#include <set> #include <set>
@ -144,14 +143,14 @@ LedDevicePhilipsHue::LedDevicePhilipsHue(const std::string& output, const std::s
int transitiontime, std::vector<unsigned int> lightIds) : int transitiontime, std::vector<unsigned int> lightIds) :
host(output.c_str()), username(username.c_str()), switchOffOnBlack(switchOffOnBlack), transitiontime( host(output.c_str()), username(username.c_str()), switchOffOnBlack(switchOffOnBlack), transitiontime(
transitiontime), lightIds(lightIds) { transitiontime), lightIds(lightIds) {
http = new QHttp(host); manager = new QNetworkAccessManager();
timer.setInterval(3000); timer.setInterval(3000);
timer.setSingleShot(true); timer.setSingleShot(true);
connect(&timer, SIGNAL(timeout()), this, SLOT(restoreStates())); connect(&timer, SIGNAL(timeout()), this, SLOT(restoreStates()));
} }
LedDevicePhilipsHue::~LedDevicePhilipsHue() { LedDevicePhilipsHue::~LedDevicePhilipsHue() {
delete http; delete manager;
} }
int LedDevicePhilipsHue::write(const std::vector<ColorRgb> & ledValues) { int LedDevicePhilipsHue::write(const std::vector<ColorRgb> & ledValues) {
@ -216,33 +215,29 @@ int LedDevicePhilipsHue::switchOff() {
} }
void LedDevicePhilipsHue::put(QString route, QString content) { void LedDevicePhilipsHue::put(QString route, QString content) {
QString url = QString("/api/%1/%2").arg(username).arg(route); QString url = QString("http://%1/api/%2/%3").arg(host).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()));
// Perfrom request // 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. // Go into the loop until the request is finished.
loop.exec(); loop.exec();
} }
QByteArray LedDevicePhilipsHue::get(QString route) { QByteArray LedDevicePhilipsHue::get(QString route) {
QString url = QString("/api/%1/%2").arg(username).arg(route); QString url = QString("http://%1/api/%2/%3").arg(host).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()));
// Perfrom request // 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. // Go into the loop until the request is finished.
loop.exec(); loop.exec();
// Read all data of the response. // Read all data of the response.
return http->readAll(); return reply->readAll();
} }
QString LedDevicePhilipsHue::getStateRoute(unsigned int lightId) { QString LedDevicePhilipsHue::getStateRoute(unsigned int lightId) {

View File

@ -6,7 +6,7 @@
// Qt includes // Qt includes
#include <QObject> #include <QObject>
#include <QString> #include <QString>
#include <QHttp> #include <QNetworkAccessManager>
#include <QTimer> #include <QTimer>
// Leddevice includes // Leddevice includes
@ -164,8 +164,8 @@ private:
QString host; QString host;
/// User name for the API ("newdeveloper") /// User name for the API ("newdeveloper")
QString username; QString username;
/// Qhttp object for sending requests. /// QNetworkAccessManager object for sending requests.
QHttp* http; QNetworkAccessManager* manager;
/// Use timer to reset lights when we got into "GRABBINGMODE_OFF". /// Use timer to reset lights when we got into "GRABBINGMODE_OFF".
QTimer timer; QTimer timer;
/// ///