added timeout to event getter

This commit is contained in:
Dorian Zedler 2018-12-30 21:10:36 +01:00
parent b5e69fc7f2
commit 071b4d1f21
2 changed files with 13 additions and 33 deletions

View file

@ -54,7 +54,6 @@ public:
public slots: public slots:
Q_INVOKABLE int login(QString username, QString password, bool permanent); Q_INVOKABLE int login(QString username, QString password, bool permanent);
Q_INVOKABLE int logout(); Q_INVOKABLE int logout();
Q_INVOKABLE QString getDay(QString day);
Q_INVOKABLE int checkConn(); Q_INVOKABLE int checkConn();
Q_INVOKABLE float getProgress(); Q_INVOKABLE float getProgress();
Q_INVOKABLE int getFoodPlan(); Q_INVOKABLE int getFoodPlan();

View file

@ -172,33 +172,6 @@ float ServerConn::getProgress()
int ServerConn::getEvents(QString day){ 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; this->progress = 0;
// Create request // Create request
QNetworkRequest request; QNetworkRequest request;
@ -211,14 +184,22 @@ int ServerConn::getEvents(QString day){
request.setRawHeader( "Authorization", headerData.toLocal8Bit() ); request.setRawHeader( "Authorization", headerData.toLocal8Bit() );
QUrlQuery pdata; QUrlQuery pdata;
// Send request and connect all possible signals //QNetworkReply*reply = this->networkManager->post(request, pdata.toString(QUrl::FullyEncoded).toUtf8());
QNetworkReply*reply = this->networkManager->post(request, pdata.toString(QUrl::FullyEncoded).toUtf8()); QNetworkReply*reply = networkManager->get( request );
//QNetworkReply*reply = networkManager->get( request );
connect(reply, SIGNAL(downloadProgress(qint64, qint64)), // loop to wait until the request has finished before processing the data
this, SLOT(updateProgress(qint64, qint64)));
QEventLoop loop; 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())); 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(); loop.exec();
this->progress = 1; this->progress = 1;