Route and Results buttons updated.
[speedfreak] / Client / httpclient.cpp
index 5a25562..9c32c38 100644 (file)
@@ -1,14 +1,25 @@
+/*
+ * Http client Connects application to server.
+ *
+ * @author      Tiina Kivilinna-Korhola <tiina.kivilinna-korhola@fudeco.com>
+ * @author      Olavi Pulkkinen <olavi.pulkkinen@fudeco.com>
+ * @author      Toni Jussila   <toni.jussila@fudeco.com>
+ * @copyright   (c) 2010 Speed Freak team
+ * @license     http://opensource.org/licenses/gpl-license.php GNU Public License
+ */
+
 #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)
 {
+    qDebug() << "__HttpClient";
     myMainw = myCarw;
     netManager = new QNetworkAccessManager();
     myXmlwriter = new XmlWriter();
@@ -20,18 +31,17 @@ HttpClient::HttpClient(CarMainWindow *myCarw)
   */
 HttpClient::~HttpClient()
 {
-
+    qDebug() << "__~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()
 {
     qDebug() << "_requestRegistration" ;
-    qDebug() <<  myMainw->myRegistration->getUserName() << "+" <<  myMainw->myRegistration->getPassword() << "+" <<  myMainw->myRegistration->getEmail();
+    qDebug() <<  myMainw->settingsDialog->getRegUserName() << "+" <<  myMainw->settingsDialog->getRegPassword() << "+" <<  myMainw->settingsDialog->getRegEmail();
 
     QBuffer *regbuffer = new QBuffer();
     QUrl qurl("http://api.speedfreak-app.com/api/register");
@@ -41,40 +51,42 @@ void HttpClient::requestRegistration()
 
     regbuffer->open(QBuffer::ReadWrite);
     myXmlwriter->writeRegistering(regbuffer,
-                       myMainw->myRegistration->getUserName(),
-                       myMainw->myRegistration->getPassword(),
-                       myMainw->myRegistration->getEmail());
+                       myMainw->settingsDialog->getRegUserName(),
+                       myMainw->settingsDialog->getRegPassword(),
+                       myMainw->settingsDialog->getRegEmail());
     qDebug() << "carmainwindow: regbuffer->data(): " << regbuffer->data();
 
     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)));
 
+    //Indicating user
+    if(myMainw->settingsDialog)
+        myMainw->settingsDialog->setLabelInfoToUser("Reguesting registration from server");
+
     regbuffer->close();
 }
 
 /**
   *@brief Sends result(s) to the server in xml format.
   *Send authentication information in the header.
-  *@todo Read category elsewhere.
   */
-void HttpClient::sendResultXml()
+void HttpClient::sendResultXml(QString category, double result)
 {
     qDebug() << "_sendResultXml";
 
     QBuffer *xmlbuffer = new QBuffer();
-    QString category_name = "acceleration-0-100";    //replace with real value from category list
 
-    QUrl qurl("http://api.speedfreak-app.com/api/update/" + category_name);
+    QUrl qurl("http://api.speedfreak-app.com/api/update/" + category);
     qDebug() << qurl.toString();
     QNetworkRequest request(qurl);
     QNetworkReply *currentDownload;
 
     xmlbuffer->open(QBuffer::ReadWrite);
-    myXmlwriter->writeResult(xmlbuffer);
+    myXmlwriter->writeResult(xmlbuffer, result);
     qDebug() << "carmainwindow: xmlbuffer->data(): " << xmlbuffer->data();
 
-    QString credentials = myMainw->myRegistration->getUserName() + ":" + myMainw->myRegistration->getPassword();
+    QString credentials = myMainw->settingsDialog->getUserName() + ":" + myMainw->settingsDialog->getPassword();
     credentials = "Basic " + credentials.toAscii().toBase64();
     request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
 
@@ -82,10 +94,50 @@ void HttpClient::sendResultXml()
     connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfResult()));
     //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
 
+    //Indicating user
+    if(myMainw->accstart->accRealTimeDialog->resultDialog)
+        myMainw->accstart->accRealTimeDialog->resultDialog->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->settingsDialog->getUserName() + ":" + myMainw->settingsDialog->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)));
+
+    //Indicating user
+    if(myMainw->routeSaveDialog->routeDialog)
+        myMainw->routeSaveDialog->routeDialog->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.
@@ -101,15 +153,18 @@ void HttpClient::requestTopList(QString category, QString limit)
     QNetworkRequest request(qurl);
     QNetworkReply *currentDownload;
 
-    QString credentials = myMainw->myRegistration->getUserName() + ":" + myMainw->myRegistration->getPassword();
+    QString credentials = myMainw->settingsDialog->getUserName() + ":" + myMainw->settingsDialog->getPassword();
     credentials = "Basic " + credentials.toAscii().toBase64();
     request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
 
     currentDownload = netManager->post(request, ("data=" ));
     connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfToplist()));
     //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
-}
 
+    //Indicating user
+    if(myMainw->topResultDialog)
+        myMainw->topResultDialog->setLabelInfoToUser("Reguesting top10 list from server");
+}
 
 /**
   *@brief Request categories list from the server.
@@ -124,15 +179,18 @@ void HttpClient::requestCategories()
     QNetworkRequest request(qurl);
     QNetworkReply *currentDownload;
 
-    QString credentials = myMainw->myRegistration->getUserName() + ":" + myMainw->myRegistration->getPassword();
+    QString credentials = myMainw->settingsDialog->getUserName() + ":" + myMainw->settingsDialog->getPassword();
     credentials = "Basic " + credentials.toAscii().toBase64();
     request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
 
     currentDownload = netManager->post(request, ("data=" ));
     connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfCategories()));
     //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
-}
 
+    //Indicating user
+    if(myMainw->topResultDialog)
+        myMainw->topResultDialog->setLabelInfoToUser("Reguesting categories from server");
+}
 
 /**
   *@brief Check that username and password exist on the server.
@@ -147,39 +205,83 @@ void HttpClient::checkLogin()
     QNetworkRequest request(qurl);
     QNetworkReply *currentDownload;
 
-    QString credentials = myMainw->myRegistration->getUserName() + ":" + myMainw->myRegistration->getPassword();
+    QString credentials = myMainw->settingsDialog->getUserName() + ":" + myMainw->settingsDialog->getPassword();
     credentials = "Basic " + credentials.toAscii().toBase64();
     request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
 
     currentDownload = netManager->post(request, ("data=" ));
     connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfLogin()));
     //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
-}
 
+    //Indicating user
+    if(myMainw->settingsDialog)
+        myMainw->settingsDialog->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";
 
+    //Indicating user
+    if(myMainw->accstart->accRealTimeDialog->resultDialog)
+        myMainw->accstart->accRealTimeDialog->resultDialog->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 result sending ",reply->errorString());
+
+        //Indicating user
+        if(myMainw->accstart->accRealTimeDialog->resultDialog)
+            QMessageBox::about(myMainw->accstart->accRealTimeDialog->resultDialog, "Server reply to result sending ",reply->errorString());
+        if(myMainw->accstart->accRealTimeDialog->resultDialog)
+            myMainw->accstart->accRealTimeDialog->resultDialog->setLabelInfoToUser("Error");
+        if(myMainw->accstart->accRealTimeDialog->resultDialog)
+            myMainw->accstart->accRealTimeDialog->resultDialog->setSendServerButtonEnabled();
     }
     else {
         qDebug() << "errorcode:" << errorcode << reply->errorString();
-        qDebug() << reply->readAll();
-    }
 
+        //Indicating user
+        if(myMainw->accstart->accRealTimeDialog->resultDialog)
+            QMessageBox::about(myMainw->accstart->accRealTimeDialog->resultDialog, "Server reply to result sending", "Result received " + reply->readAll());
+        if(myMainw->accstart->accRealTimeDialog->resultDialog)
+            myMainw->accstart->accRealTimeDialog->resultDialog->setLabelInfoToUser("Result received");
+    }
 }
 
+/**
+  *@brief React to servers responce after route has been sent.
+  */
+void HttpClient::ackOfRoute()
+{
+    qDebug() << "_ackOfRoute";
+
+    if(myMainw->routeSaveDialog->routeDialog)
+        myMainw->routeSaveDialog->routeDialog->setLabelInfoToUser("");
+
+    QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
+
+    QNetworkReply::NetworkError errorcode;
+    errorcode = reply->error();
+    if(errorcode != 0) {
+        qDebug() <<  "errorcode:" << errorcode << reply->errorString();
+        if(myMainw->routeSaveDialog->routeDialog)
+            QMessageBox::about(myMainw->routeSaveDialog->routeDialog, "Server reply to route sending ",reply->errorString());
+        if(myMainw->routeSaveDialog->routeDialog)
+            myMainw->routeSaveDialog->routeDialog->setSendServerButtonEnabled();
+    }
+    else {
+        qDebug() << "errorcode:" << errorcode << reply->errorString();
+        if(myMainw->routeSaveDialog->routeDialog)
+            QMessageBox::about(myMainw->routeSaveDialog->routeDialog, "Server reply to route sending", "Route received " + reply->readAll());
+    }
+}
 
 /**
   *@brief React to servers responce after registration has been sent.
@@ -189,22 +291,28 @@ void HttpClient::ackOfRegistration()
 {
     qDebug() << "_ackOfRegistration";
 
+    if(myMainw->settingsDialog)
+        myMainw->settingsDialog->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 registration",reply->readAll());
+        if(myMainw->settingsDialog)
+            QMessageBox::about(myMainw->settingsDialog, "Server reply to registration",reply->readAll());
     }
     else {
         qDebug() << "errorcode=0" << errorcode << reply->errorString();
-        QMessageBox::about(myMainw, "Server reply to registration", "User registration " + reply->readAll());
+        if(myMainw->settingsDialog)
+        {
+            QMessageBox::about(myMainw->settingsDialog, "Server reply to registration", "User registration " + reply->readAll());
+            myMainw->settingsDialog->clearRegisterLineEdits();
+        }
     }
-
 }
 
-
 /**
   *@brief React to servers responce after request for categories has been sent.
   */
@@ -212,6 +320,9 @@ void HttpClient::ackOfCategories()
 {
     qDebug() << "_ackOfCategories";
 
+    if(myMainw->topResultDialog)
+        myMainw->topResultDialog->setLabelInfoToUser("");
+
     QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
     myXmlreader->xmlReadCategories(reply);
 
@@ -219,70 +330,82 @@ void HttpClient::ackOfCategories()
     errorcode = reply->error();
     if(errorcode != 0) {
         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
-        QMessageBox::about(myMainw, "Server reply to requesting categories",reply->errorString());
+        //QMessageBox::about(myMainw->topResultDialog, "Server reply to requesting categories",reply->errorString());
+        if(myMainw->topResultDialog)
+            myMainw->topResultDialog->setLabelInfoToUser("You're not logged! Please register or log in.");
     }
     else {
         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
-        qDebug() << reply->readAll();
+        //QMessageBox::about(myMainw->topResultDialog, "Server reply to requesting categories ", "OK");
+        if(myMainw->topResultDialog)
+            myMainw->topResultDialog->setLabelInfoToUser("");
     }
-
 }
 
-
 /**
   *@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";
 
+    if(myMainw->settingsDialog)
+        myMainw->settingsDialog->setLabelInfoToUser("");
+
     QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
-    myXmlreader->xmlReadTop10Results(reply);
 
     QNetworkReply::NetworkError errorcode;
     errorcode = reply->error();
-    if(errorcode != 0) {
+    if(errorcode != 0) 
+    {
         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
-        QMessageBox::about(myMainw, "Server does not recognize your username. Please registrate.",reply->errorString());
+       if(myMainw->settingsDialog)
+       {
+               QMessageBox::about(myMainw->settingsDialog, "Server does not recognize your username. Please registrate.",reply->errorString());
+               myMainw->settingsDialog->usernameOk(false);
+       }
     }
-    else {
+    else 
+    {
         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
-        QMessageBox::about(myMainw, "Server reply to login", "User login " + reply->readAll());
+       if(myMainw->settingsDialog)
+               QMessageBox::about(myMainw->settingsDialog, "Server reply to login", "User login " + reply->readAll());
+        // here signal emit to mainwindow for username setting to main panel
+        emit loginOK();
+       if( myMainw->settingsDialog)
+       {
+               myMainw->settingsDialog->usernameOk(true);
+               myMainw->settingsDialog->close();    
+       }
+
     }
 }
 
-
 /**
   *@brief Reports errors, when server has sent error signal.
   */
 void HttpClient::errorFromServer(QNetworkReply::NetworkError errorcode)
 {
     qDebug() << "_errorFromServer";
-
     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";
-
     QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
     myXmlreader->xmlReadTop10Results(reply);
 
@@ -290,12 +413,14 @@ void HttpClient::ackOfToplist()
     errorcode = reply->error();
     if(errorcode != 0) {
         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
-        QMessageBox::about(myMainw, "Server reply to requesting top 10 list",reply->errorString());
+        //QMessageBox::about(myMainw->topResultDialog, "Server reply to requesting top 10 list",reply->errorString());
+        if(myMainw->topResultDialog)
+            myMainw->topResultDialog->setLabelInfoToUser("Error");
     }
     else {
         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
-        qDebug() << reply->readAll();
+        //QMessageBox::about(myMainw->topResultDialog, "Server reply to requesting top 10 list", "OK " + reply->readAll());
+        if(myMainw->topResultDialog)
+            myMainw->topResultDialog->setLabelInfoToUser("");
     }
-
 }
-