Merge branch 'update/httpClient'
[speedfreak] / Client / httpclient.cpp
index 12e681a..ce3f2d8 100644 (file)
@@ -10,6 +10,7 @@
 
 #include <QString>
 #include <QMessageBox>
+#include <QDir>
 #include "httpclient.h"
 #include "mainwindow.h"
 
@@ -32,6 +33,11 @@ HttpClient::HttpClient(MainWindow *myCarw)
 HttpClient::~HttpClient()
 {
     qDebug() << "__~HttpClient" ;
+
+    if(myXmlwriter)
+        delete myXmlwriter;
+    if(myXmlreader)
+        delete myXmlreader;
 }
 
 /**
@@ -44,7 +50,7 @@ void HttpClient::requestRegistration()
     qDebug() <<  myMainw->settingsDialog->getRegUserName() << "+" <<  myMainw->settingsDialog->getRegPassword() << "+" <<  myMainw->settingsDialog->getRegEmail();
 
     QBuffer *regbuffer = new QBuffer();
-    QUrl qurl("http://api.speedfreak-app.com/api/register");
+    QUrl qurl("http://www.speedfreak-app.com/users/register");
     QNetworkRequest request(qurl);
     qDebug() << qurl.toString();
     QNetworkReply *currentDownload;
@@ -53,7 +59,8 @@ void HttpClient::requestRegistration()
     myXmlwriter->writeRegistering(regbuffer,
                        myMainw->settingsDialog->getRegUserName(),
                        myMainw->settingsDialog->getRegPassword(),
-                       myMainw->settingsDialog->getRegEmail());
+                       myMainw->settingsDialog->getRegEmail(),
+                       "My car is cool");
     qDebug() << "carmainwindow: regbuffer->data(): " << regbuffer->data();
 
     currentDownload = netManager->post(request, ("xml=" + regbuffer->data()));
@@ -74,10 +81,12 @@ void HttpClient::requestRegistration()
 void HttpClient::sendResultXml(QString category, double result)
 {
     qDebug() << "_sendResultXml";
+    qDebug() << category;
 
     QBuffer *xmlbuffer = new QBuffer();
 
-    QUrl qurl("http://api.speedfreak-app.com/api/update/" + category);
+    QUrl qurl("http://www.speedfreak-app.com/results/update/" + category);
+
     qDebug() << qurl.toString();
     QNetworkRequest request(qurl);
     QNetworkReply *currentDownload;
@@ -102,39 +111,57 @@ void HttpClient::sendResultXml(QString category, double result)
 }
 
 /**
-  *@brief Sends route to the server in xml format.
-  *Send authentication information in the header.
-  *@todo Check destination URL.
+  * @brief Sends route to the server in xml format.
+  * Send authentication information in the header.
+  * @param QString filename
+  * @param int 1(send to server) or 0(no send)
+  * @todo Check destination URL.
   */
-void HttpClient::sendRouteXml()
+void HttpClient::sendRouteXml(QString oldName, QString newName, int i)
 {
     qDebug() << "_sendRouteXml";
 
-    QString filename = "route.xml";
-    QFile file(filename);
-    if (!file.open(QFile::ReadOnly)) {
-        qDebug() << "_sendRouteXml file.open() fail";
-        return;
-    }
+    //QString filename = "/home/user/MyDocs/speedfreak/route/route.xml";
+    qDebug() << "__old:" + oldName;
+    QString filename = newName; //+ ".xml";
 
-    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());
+    if(newName != "")
+    {
+        qDebug() << "_rename xml";
+        QDir dir(filename);
+        qDebug() << "__new:" + filename;
+        qDebug() << dir.rename(oldName, filename);
+    }
 
-    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)));
+    if(i == 1)
+    {
+        qDebug() << "_send route";
+        QFile file(filename);
+        if (!file.open(QFile::ReadOnly))
+        {
+            qDebug() << "_sendRouteXml file.open() fail";
+            return;
+        }
+
+        QUrl qurl("http://speedfreak-app.com/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");
+        //Indicating user
+        if(myMainw->routeSaveDialog->routeDialog)
+            myMainw->routeSaveDialog->routeDialog->setLabelInfoToUser("Sending route to server");
 
-    file.close();
+        file.close();
+    }
 }
 
 /**
@@ -145,9 +172,10 @@ void HttpClient::sendRouteXml()
   */
 void HttpClient::requestTopList(QString category, QString limit)
 {
-    qDebug() << "_requestTopList" ;
+    qDebug() << "_requestTopList";
+    qDebug() << category;
 
-    QString urlBase = "http://api.speedfreak-app.com/api/results/";
+    QString urlBase = "http://www.speedfreak-app.com/results/list/";
     QUrl qurl(urlBase + category + "/" + limit);
     qDebug() << qurl.toString();
     QNetworkRequest request(qurl);
@@ -173,8 +201,8 @@ void HttpClient::requestTopList(QString category, QString limit)
 void HttpClient::requestCategories()
 {
     qDebug() << "_requestCategories" ;
+    QUrl qurl("www.speedfreak-app.com/results/categories");
 
-    QUrl qurl("http://api.speedfreak-app.com/api/categories/");
     qDebug() << qurl.toString();
     QNetworkRequest request(qurl);
     QNetworkReply *currentDownload;
@@ -200,8 +228,9 @@ void HttpClient::checkLogin()
 {
     qDebug() << "_checkLogin";
 
-    QUrl qurl("http://api.speedfreak-app.com/api/login/");
+    QUrl qurl("http://www.speedfreak-app.com/users/login");
     qDebug() << qurl.toString();
+
     QNetworkRequest request(qurl);
     QNetworkReply *currentDownload;
 
@@ -306,7 +335,10 @@ void HttpClient::ackOfRegistration()
     else {
         qDebug() << "errorcode=0" << errorcode << reply->errorString();
         if(myMainw->settingsDialog)
+        {
             QMessageBox::about(myMainw->settingsDialog, "Server reply to registration", "User registration " + reply->readAll());
+            myMainw->settingsDialog->clearRegisterLineEdits();
+        }
     }
 }
 
@@ -374,7 +406,6 @@ void HttpClient::ackOfLogin()
                myMainw->settingsDialog->usernameOk(true);
                myMainw->settingsDialog->close();    
        }
-
     }
 }
 
@@ -404,20 +435,142 @@ void HttpClient::ackOfToplist()
 {
     qDebug() << "_ackOfToplist";
     QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
-    myXmlreader->xmlReadTop10Results(reply);
+    myXmlreader->xmlReadTop10Results(reply,myMainw->settingsDialog->getUserName());
 
     QNetworkReply::NetworkError errorcode;
     errorcode = reply->error();
     if(errorcode != 0) {
         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
-        //QMessageBox::about(myMainw->topResultDialog, "Server reply to requesting top 10 list",reply->errorString());
+        //Indicating user
         if(myMainw->topResultDialog)
-            myMainw->topResultDialog->setLabelInfoToUser("Error");
+        {
+            //QMessageBox::about(myMainw->topResultDialog, "Server reply to requesting top 10 list",reply->errorString());
+            myMainw->topResultDialog->setLabelInfoToUser("No results ;(");
+        }
     }
     else {
         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
-        //QMessageBox::about(myMainw->topResultDialog, "Server reply to requesting top 10 list", "OK " + reply->readAll());
+        //Indicating user
         if(myMainw->topResultDialog)
+        {
+            //QMessageBox::about(myMainw->topResultDialog, "Server reply to requesting top 10 list", "OK " + reply->readAll());
             myMainw->topResultDialog->setLabelInfoToUser("");
+        }
+    }
+}
+
+/**
+  * This function sends profile to the server in xml format.
+  * Send authentication information in the header.
+  */
+void HttpClient::sendProfileXml()
+{
+    qDebug() << "_sendProfileXml";
+
+    QString userName = myMainw->settingsDialog->getUserName();
+    QString filename = userName + "_profile.xml";
+    QFile file(filename);
+    if (!file.open(QFile::ReadWrite | QFile::Text))
+    {
+        qDebug() << "_xmlWrite fail";
+        return;
+    }
+    myXmlwriter->writeProfileXmlFile(&file, userName,
+            myMainw->settingsDialog->profileDialog->getManufacturer(),
+            myMainw->settingsDialog->profileDialog->getType(),
+            myMainw->settingsDialog->profileDialog->getModel(),
+            myMainw->settingsDialog->profileDialog->getDescription(),
+            myMainw->settingsDialog->profileDialog->getPicture());
+
+    //Indicating user
+    if(myMainw->settingsDialog->profileDialog)
+        myMainw->settingsDialog->profileDialog->setLabelInfoToUser("Profile saved to phone");
+
+    // Send xml to server
+    /*QUrl qurl("http://speedfreak-app.com/api/profile");
+    QNetworkRequest request(qurl);
+    qDebug() << qurl.toString();
+    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()));
+    bool error = connect(currentDownload, SIGNAL(finished()), this, SLOT(ackOfProfile()));*/
+
+    file.close();
+
+    // Send picture to server
+    /*if(myMainw->settingsDialog->profileDialog->getPicture() != "" && error == false)
+    {
+        QFile pictureFile( myMainw->settingsDialog->profileDialog->getPicture() );
+        if (!pictureFile.open(QIODevice::ReadOnly))
+        {
+            qDebug() << "__picture read fail";
+            return;
+        }
+        currentDownload = netManager->post(request, pictureFile.readAll());
+        connect(currentDownload, SIGNAL(finished()), this, SLOT(ackOfSendingPicture()));
+        pictureFile.close();
+    }*/
+}
+
+/**
+  * This slot function react to servers responce after request of profile has been sent.
+  */
+bool HttpClient::ackOfProfile()
+{
+    qDebug() << "__ackOfProfile";
+    QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
+    QNetworkReply::NetworkError errorcode;
+    errorcode = reply->error();
+    if(errorcode != 0) {
+        qDebug() <<  "errorcode:" << errorcode << reply->errorString();
+        //Indicating user
+        if(myMainw->settingsDialog->profileDialog)
+        {
+            //QMessageBox::about(myMainw->settingsDialog->profileDialog, "Server reply to requesting profile",reply->errorString());
+            myMainw->settingsDialog->profileDialog->setLabelInfoToUser("Profile save to server - fail");
+            return true;
+        }
+    }
+    else {
+        qDebug() <<  "errorcode:" << errorcode << reply->errorString();
+        //Indicating user
+        if(myMainw->settingsDialog->profileDialog)
+        {
+            //QMessageBox::about(myMainw->settingsDialog->profileDialog, "Server reply to requesting profile", "OK " + reply->readAll());
+            myMainw->settingsDialog->profileDialog->setLabelInfoToUser("Profile saved to server");
+            return false;
+        }
+    }
+}
+/**
+  * This slot function react to servers responce after request of picture has been sent.
+  */
+void HttpClient::ackOfSendingPicture()
+{
+    qDebug() << "__ackOfSendingPicture";
+    QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
+    QNetworkReply::NetworkError errorcode;
+    errorcode = reply->error();
+    if(errorcode != 0) {
+        qDebug() <<  "errorcode:" << errorcode << reply->errorString();
+        //Indicating user
+        if(myMainw->settingsDialog->profileDialog)
+        {
+            //QMessageBox::about(myMainw->settingsDialog->profileDialog, "Server reply to requesting picture",reply->errorString());
+            myMainw->settingsDialog->profileDialog->setLabelInfoToUser("Picture save to server - fail");
+        }
+    }
+    else {
+        qDebug() <<  "errorcode:" << errorcode << reply->errorString();
+        //Indicating user
+        if(myMainw->settingsDialog->profileDialog)
+        {
+            //QMessageBox::about(myMainw->settingsDialog->profileDialog, "Server reply to requesting picture", "OK " + reply->readAll());
+            myMainw->settingsDialog->profileDialog->setLabelInfoToUser("Picture saved to server");
+        }
     }
 }