From 071b4d1f21f8d720496a76d6d9cce5963a342721 Mon Sep 17 00:00:00 2001 From: dorian Date: Sun, 30 Dec 2018 21:10:36 +0100 Subject: [PATCH] added timeout to event getter --- headers/serverconn.h | 1 - sources/serverconn.cpp | 45 ++++++++++++------------------------------ 2 files changed, 13 insertions(+), 33 deletions(-) diff --git a/headers/serverconn.h b/headers/serverconn.h index 8f121e0..1d8558d 100644 --- a/headers/serverconn.h +++ b/headers/serverconn.h @@ -54,7 +54,6 @@ public: public slots: Q_INVOKABLE int login(QString username, QString password, bool permanent); Q_INVOKABLE int logout(); - Q_INVOKABLE QString getDay(QString day); Q_INVOKABLE int checkConn(); Q_INVOKABLE float getProgress(); Q_INVOKABLE int getFoodPlan(); diff --git a/sources/serverconn.cpp b/sources/serverconn.cpp index 2c92d9a..f3b19c9 100644 --- a/sources/serverconn.cpp +++ b/sources/serverconn.cpp @@ -172,33 +172,6 @@ float ServerConn::getProgress() int ServerConn::getEvents(QString day){ -/* - this->progress = 0; - ReturnData_t ret; //this is a custom type to store the returned data - // Call the webservice - - QNetworkRequest request(QUrl("http://api.itsblue.de/fanny/vertretung.php?uname=" + this->username + "&passwd=" + this->password + "&day=" + day + "&agree=true")); - request.setHeader(QNetworkRequest::ContentTypeHeader, - "application/x-www-form-urlencoded"); - - //send a POST request with the given url and data to the server - QNetworkReply* reply; - - QUrlQuery pdata; - reply = this->networkManager->post(request, pdata.toString(QUrl::FullyEncoded).toUtf8()); - - connect(reply, SIGNAL(downloadProgress(qint64, qint64)), - this, SLOT(updateProgress(qint64, qint64))); - //wait until the request has finished - QEventLoop loop; - loop.connect(this->networkManager, SIGNAL(finished(QNetworkReply*)), SLOT(quit())); - loop.exec(); - - //get the status code - QVariant status_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute); - this->progress = 1; - */ - this->progress = 0; // Create request QNetworkRequest request; @@ -211,14 +184,22 @@ int ServerConn::getEvents(QString day){ request.setRawHeader( "Authorization", headerData.toLocal8Bit() ); QUrlQuery pdata; - // Send request and connect all possible signals - QNetworkReply*reply = this->networkManager->post(request, pdata.toString(QUrl::FullyEncoded).toUtf8()); - //QNetworkReply*reply = networkManager->get( request ); + //QNetworkReply*reply = this->networkManager->post(request, pdata.toString(QUrl::FullyEncoded).toUtf8()); + QNetworkReply*reply = networkManager->get( request ); - connect(reply, SIGNAL(downloadProgress(qint64, qint64)), - this, SLOT(updateProgress(qint64, qint64))); + // loop to wait until the request has finished before processing the data QEventLoop loop; + // timer to cancel the request after 3 seconds + QTimer timer; + timer.setSingleShot(true); + + // quit the loop when the request finised loop.connect(this->networkManager, SIGNAL(finished(QNetworkReply*)), SLOT(quit())); + // or the timer timed out + loop.connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit())); + // start the timer + timer.start(2000); + // start the loop loop.exec(); this->progress = 1;