Added classes.
[speedfreak] / Client / httpclient.cpp
index 53ad791..984a0db 100644 (file)
@@ -1,14 +1,14 @@
 #include <QString>
 #include <QMessageBox>
 #include "httpclient.h"
-#include "carmainwindow.h"
+#include "mainwindow.h"
 
 
 /**
   *@brief Constructor, connects object to GUI
   *@param Pointer to carmainwindow, which is temporarily used during development
   */
-HttpClient::HttpClient(CarMainWindow *myCarw)
+HttpClient::HttpClient(MainWindow *myCarw)
 {
     myMainw = myCarw;
     netManager = new QNetworkAccessManager();
@@ -27,7 +27,6 @@ HttpClient::~HttpClient()
 /**
   *@brief Sends registration information to the server in xml format.
   *Reads user name, password and emaol address from resuldialogs internal variables.
-  *@todo Replace msg box with better reaction to server`s responce.
   */
 void HttpClient::requestRegistration()
 {
@@ -50,6 +49,7 @@ void HttpClient::requestRegistration()
     currentDownload = netManager->post(request, ("xml=" + regbuffer->data()));
     connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfRegistration()));
     //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
+    myMainw->setLabelInfoToUser("Reguesting registration from server");
 
     regbuffer->close();
 }
@@ -57,9 +57,8 @@ void HttpClient::requestRegistration()
 /**
   *@brief Sends result(s) to the server in xml format.
   *Send authentication information in the header.
-  *@todo Read category elsewhere.
   */
-void HttpClient::sendResultXml(QString category)
+void HttpClient::sendResultXml(QString category, double result)
 {
     qDebug() << "_sendResultXml";
 
@@ -71,7 +70,7 @@ void HttpClient::sendResultXml(QString category)
     QNetworkReply *currentDownload;
 
     xmlbuffer->open(QBuffer::ReadWrite);
-    myXmlwriter->writeResult(xmlbuffer);
+    myXmlwriter->writeResult(xmlbuffer, result);
     qDebug() << "carmainwindow: xmlbuffer->data(): " << xmlbuffer->data();
 
     QString credentials = myMainw->myLogin->getUserName() + ":" + myMainw->myLogin->getPassword();
@@ -81,11 +80,45 @@ void HttpClient::sendResultXml(QString category)
     currentDownload = netManager->post(request, ("xml=" + xmlbuffer->data()));
     connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfResult()));
     //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
+    myMainw->setLabelInfoToUser("Sending result to server");
 
     xmlbuffer->close();
 }
 
 /**
+  *@brief Sends route to the server in xml format.
+  *Send authentication information in the header.
+  *@todo Check destination URL.
+  */
+void HttpClient::sendRouteXml()
+{
+    qDebug() << "_sendRouteXml";
+
+    QString filename = "route.xml";
+    QFile file(filename);
+    if (!file.open(QFile::ReadOnly)) {
+        qDebug() << "_sendRouteXml file.open() fail";
+        return;
+    }
+
+    QUrl qurl("http://api.speedfreak-app.com/api/update/route");
+    qDebug() << qurl.toString();
+    QNetworkRequest request(qurl);
+    QNetworkReply *currentDownload;
+
+    QString credentials = myMainw->myLogin->getUserName() + ":" + myMainw->myLogin->getPassword();
+    credentials = "Basic " + credentials.toAscii().toBase64();
+    request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
+
+    currentDownload = netManager->post(request, ("xml=" + file.readAll()));
+    connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfRoute()));
+    //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
+    myMainw->setLabelInfoToUser("Sending route to server");
+
+    file.close();
+}
+
+/**
   *@brief Request the Top10List of certain category from the server.
   *Send authentication information in the header.
   *@param Category of results.
@@ -108,6 +141,7 @@ void HttpClient::requestTopList(QString category, QString limit)
     currentDownload = netManager->post(request, ("data=" ));
     connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfToplist()));
     //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
+    myMainw->setLabelInfoToUser("Reguesting top10 list from server");
 }
 
 
@@ -131,6 +165,7 @@ void HttpClient::requestCategories()
     currentDownload = netManager->post(request, ("data=" ));
     connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfCategories()));
     //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
+    myMainw->setLabelInfoToUser("Reguesting categories from server");
 }
 
 
@@ -154,17 +189,19 @@ void HttpClient::checkLogin()
     currentDownload = netManager->post(request, ("data=" ));
     connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfLogin()));
     //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
+    myMainw->setLabelInfoToUser("Checking login validity from server");
 }
 
 
 /**
   *@brief React to servers responce after result has been sent.
-  *@todo Implement consequencies of reply.
   */
 void HttpClient::ackOfResult()
 {
     qDebug() << "_ackOfResult";
 
+    myMainw->setLabelInfoToUser("");
+
     QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
 
     QNetworkReply::NetworkError errorcode;
@@ -177,9 +214,30 @@ void HttpClient::ackOfResult()
         qDebug() << "errorcode:" << errorcode << reply->errorString();
         QMessageBox::about(myMainw, "Server reply to result sending", "Result received " + reply->readAll());
     }
-
 }
 
+/**
+  *@brief React to servers responce after route has been sent.
+  */
+void HttpClient::ackOfRoute()
+{
+    qDebug() << "_ackOfRoute";
+
+    myMainw->setLabelInfoToUser("");
+
+    QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
+
+    QNetworkReply::NetworkError errorcode;
+    errorcode = reply->error();
+    if(errorcode != 0) {
+        qDebug() <<  "errorcode:" << errorcode << reply->errorString();
+        QMessageBox::about(myMainw, "Server reply to route sending ",reply->errorString());
+    }
+    else {
+        qDebug() << "errorcode:" << errorcode << reply->errorString();
+        QMessageBox::about(myMainw, "Server reply to route sending", "Route received " + reply->readAll());
+    }
+}
 
 /**
   *@brief React to servers responce after registration has been sent.
@@ -189,6 +247,8 @@ void HttpClient::ackOfRegistration()
 {
     qDebug() << "_ackOfRegistration";
 
+    myMainw->setLabelInfoToUser("");
+
     QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
 
     QNetworkReply::NetworkError errorcode;
@@ -201,7 +261,6 @@ void HttpClient::ackOfRegistration()
         qDebug() << "errorcode=0" << errorcode << reply->errorString();
         QMessageBox::about(myMainw, "Server reply to registration", "User registration " + reply->readAll());
     }
-
 }
 
 
@@ -212,6 +271,8 @@ void HttpClient::ackOfCategories()
 {
     qDebug() << "_ackOfCategories";
 
+    myMainw->setLabelInfoToUser("");
+
     QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
     myXmlreader->xmlReadCategories(reply);
 
@@ -225,18 +286,18 @@ void HttpClient::ackOfCategories()
         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
         QMessageBox::about(myMainw, "Server reply to requesting categories ", "OK");
     }
-
 }
 
 
 /**
   *@brief React to servers responce after request of TopList in certain category has been sent.
-  *@todo Implement routing reply`s contents to UI.
   */
 void HttpClient::ackOfLogin()
 {
     qDebug() << "_ackOffLogin";
 
+    myMainw->setLabelInfoToUser("");
+
     QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
 
     QNetworkReply::NetworkError errorcode;
@@ -259,29 +320,31 @@ void HttpClient::errorFromServer(QNetworkReply::NetworkError errorcode)
 {
     qDebug() << "_errorFromServer";
 
+    myMainw->setLabelInfoToUser("");
+
     QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
 
     if(errorcode != 0) {
         qDebug() <<  "errorcode:" << errorcode;
-        //Note that errors are already reported on other ach-functions for server communication
+        //Note that errors are already reported on other each functions for server communication
         //QMessageBox::about(myMainw, "Server reported an error", reply->errorString());
     }
     else {
         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
         qDebug() << reply->readAll();
     }
-
 }
 
 
 /**
   *@brief React to servers responce after request of TopList in certain category has been sent.
-  *@todo Implement routing reply`s contents to UI.
   */
 void HttpClient::ackOfToplist()
 {
     qDebug() << "_ackOfToplist";
 
+    myMainw->setLabelInfoToUser("");
+
     QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
     myXmlreader->xmlReadTop10Results(reply);
 
@@ -295,6 +358,5 @@ void HttpClient::ackOfToplist()
         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
         QMessageBox::about(myMainw, "Server reply to requesting top 10 list", "OK " + reply->readAll());
     }
-
 }