Added functionality for other users listing and viewing other users info.
[speedfreak] / Client / httpclient.cpp
index 944749f..08e1695 100644 (file)
@@ -10,6 +10,7 @@
 
 #include <QString>
 #include <QMessageBox>
+#include <QDir>
 #include "httpclient.h"
 #include "mainwindow.h"
 
@@ -24,6 +25,7 @@ HttpClient::HttpClient(MainWindow *myCarw)
     netManager = new QNetworkAccessManager();
     myXmlwriter = new XmlWriter();
     myXmlreader = new XmlReader();
+    connect(myXmlreader, SIGNAL(userInfo(QStringList*)), this, SLOT(sendUsersInfo(QStringList*)));
 }
 
 /**
@@ -49,7 +51,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;
@@ -58,7 +60,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()));
@@ -79,10 +82,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;
@@ -107,39 +112,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";
+
+    if(newName != "")
+    {
+        qDebug() << "_rename xml";
+        QDir dir(filename);
+        qDebug() << "__new:" + filename;
+        qDebug() << dir.rename(oldName, filename);
     }
 
-    QUrl qurl("http://api.speedfreak-app.com/api/update/route");
-    qDebug() << qurl.toString();
-    QNetworkRequest request(qurl);
-    QNetworkReply *currentDownload;
+    if(i == 1)
+    {
+        qDebug() << "_send route";
+        QFile file(filename);
+        if (!file.open(QFile::ReadOnly))
+        {
+            qDebug() << "_sendRouteXml file.open() fail";
+            return;
+        }
 
-    QString credentials = myMainw->settingsDialog->getUserName() + ":" + myMainw->settingsDialog->getPassword();
-    credentials = "Basic " + credentials.toAscii().toBase64();
-    request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
+        QUrl qurl("http://speedfreak-app.com/update/route");
+        qDebug() << qurl.toString();
+        QNetworkRequest request(qurl);
+        QNetworkReply *currentDownload;
 
-    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)));
+        QString credentials = myMainw->settingsDialog->getUserName() + ":" + myMainw->settingsDialog->getPassword();
+        credentials = "Basic " + credentials.toAscii().toBase64();
+        request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
 
-    //Indicating user
-    if(myMainw->routeSaveDialog->routeDialog)
-        myMainw->routeSaveDialog->routeDialog->setLabelInfoToUser("Sending route to server");
+        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)));
 
-    file.close();
+        //Indicating user
+        if(myMainw->routeSaveDialog->routeDialog)
+            myMainw->routeSaveDialog->routeDialog->setLabelInfoToUser("Sending route to server");
+
+        file.close();
+    }
 }
 
 /**
@@ -153,7 +176,7 @@ void HttpClient::requestTopList(QString category, QString limit)
     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);
@@ -179,8 +202,8 @@ void HttpClient::requestTopList(QString category, QString limit)
 void HttpClient::requestCategories()
 {
     qDebug() << "_requestCategories" ;
+    QUrl qurl("http://www.speedfreak-app.com/results/categories");
 
-    QUrl qurl("http://api.speedfreak-app.com/api/categories/");
     qDebug() << qurl.toString();
     QNetworkRequest request(qurl);
     QNetworkReply *currentDownload;
@@ -206,8 +229,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;
 
@@ -251,7 +275,7 @@ void HttpClient::ackOfResult()
             myMainw->accstart->accRealTimeDialog->resultDialog->setSendServerButtonEnabled();
     }
     else {
-        qDebug() << "errorcode:" << errorcode << reply->errorString();
+        //qDebug() << "errorcode:" << errorcode << reply->errorString();
 
         //Indicating user
         if(myMainw->accstart->accRealTimeDialog->resultDialog)
@@ -341,7 +365,7 @@ void HttpClient::ackOfCategories()
             myMainw->topResultDialog->setLabelInfoToUser("You're not logged! Please register or log in.");
     }
     else {
-        qDebug() <<  "errorcode:" << errorcode << reply->errorString();
+        //qDebug() <<  "errorcode:" << errorcode << reply->errorString();
         //QMessageBox::about(myMainw->topResultDialog, "Server reply to requesting categories ", "OK");
         if(myMainw->topResultDialog)
             myMainw->topResultDialog->setLabelInfoToUser("");
@@ -464,7 +488,7 @@ void HttpClient::sendProfileXml()
         myMainw->settingsDialog->profileDialog->setLabelInfoToUser("Profile saved to phone");
 
     // Send xml to server
-    /*QUrl qurl("http://api.speedfreak-app.com/api/profile");
+    /*QUrl qurl("http://speedfreak-app.com/api/profile");
     QNetworkRequest request(qurl);
     qDebug() << qurl.toString();
     QNetworkReply *currentDownload;
@@ -551,3 +575,145 @@ void HttpClient::ackOfSendingPicture()
         }
     }
 }
+
+/**
+  *@brief Request the user information of certain user from the server.
+  *Send authentication information in the header.
+  *@param username which information we want.
+  */
+void HttpClient::requestUserInfo(QString username)
+{
+    qDebug() << "_requestUsersInfo" ;
+
+    QUrl qurl("http://speedfreak-app.com/users/info/" + username);
+    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, ("data=" ));
+    connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfUserInfo()));
+
+    //qDebug() << "requestUserInfo";
+    //ackOfUserInfo();
+}
+
+/**
+  *@brief React to servers responce after request the user information of certain user.
+  */
+void HttpClient::ackOfUserInfo()
+{
+    qDebug() << "ackUserInfo";
+    /*QString fileName = "user.xml";
+    QFile file(fileName);
+    //file.setFileName( "routetemp.xml");
+    if (!file.open(QFile::ReadOnly))
+    {
+        qDebug() << "_xmlShow fail";
+        return;
+    }
+
+    myXmlreader->xmlReadUserInfo(&file);
+    file.close();*/
+
+    QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
+    myXmlreader->xmlReadUserInfo(reply);
+
+    //for(int i = 0; i < myXmlreader->usersList->count(); i++)
+    //{
+    //    myMainw->settingsDialog->sendUsernameToUsersDialog(myXmlreader->usersList->at(i));
+    //}
+}
+
+/**
+  *@brief Request the users list of all users from the server.
+  *Send authentication information in the header.
+  */
+void HttpClient::requestUsers()
+{  
+    qDebug() << "_requestUsers" ;
+
+    QUrl qurl("http://www.speedfreak-app.com/users/list_all");
+    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, ("data=" ));
+    connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfUsers()));
+
+
+    //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
+
+    //Indicating user
+    //if(myMainw->topResultDialog)
+        //myMainw->topResultDialog->setLabelInfoToUser("Reguesting categories from server");
+
+    //ackOfUsers();
+}
+
+/**
+  *@brief React to servers responce after request the users list of all users.
+  */
+void HttpClient::ackOfUsers()
+{
+    qDebug() << "ackUsers";
+   /* QString fileName = "jtn.xml";
+    QFile file(fileName);
+    //file.setFileName( "routetemp.xml");
+    if (!file.open(QFile::ReadOnly))
+    {
+        qDebug() << "_xmlShow fail";
+        return;
+    }
+
+    myXmlreader->xmlReadUsers(&file);
+    file.close();
+
+    for(int i = 0; i < myXmlreader->usersList->count(); i++)
+    {
+        myMainw->settingsDialog->sendUsernameToUsersDialog(myXmlreader->usersList->at(i));
+    }*/
+
+    qDebug() << "ackUsers";
+
+    //if(myMainw->topResultDialog)
+    //    myMainw->topResultDialog->setLabelInfoToUser("");
+
+    QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
+    myXmlreader->xmlReadUsers(reply);
+
+    for(int i = 0; i < myXmlreader->usersList->count(); i++)
+    {
+        myMainw->usersDialog->appendUserToList(myXmlreader->usersList->at(i));
+    }
+   /* QNetworkReply::NetworkError errorcode;
+    errorcode = reply->error();
+    if(errorcode != 0) {
+        qDebug() <<  "errorcode:" << errorcode << 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();
+        //QMessageBox::about(myMainw->topResultDialog, "Server reply to requesting categories ", "OK");
+        if(myMainw->topResultDialog)
+            myMainw->topResultDialog->setLabelInfoToUser("");
+    }*/
+}
+
+/**
+  * This slot function called when userInfo signal is emitted from xmlreader.
+   *@param usersInfo includes information from certain user.
+  */
+void HttpClient::sendUsersInfo(QStringList* usersInfo)
+{
+    myMainw->usersDialog->setUserInfo(usersInfo);
+}