Added error handling to listing of users and showing of user information.
[speedfreak] / Client / httpclient.cpp
index 933de3d..e43f761 100644 (file)
@@ -25,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*)));
 }
 
 /**
@@ -47,19 +48,39 @@ HttpClient::~HttpClient()
 void HttpClient::requestRegistration()
 {
     qDebug() << "_requestRegistration" ;
-    qDebug() <<  myMainw->settingsDialog->getRegUserName() << "+" <<  myMainw->settingsDialog->getRegPassword() << "+" <<  myMainw->settingsDialog->getRegEmail();
+    qDebug() <<  myMainw->settingsDialog->registerDialog->getRegUserName() << "+" <<  myMainw->settingsDialog->registerDialog->getRegPassword() << "+" <<  myMainw->settingsDialog->registerDialog->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;
 
     regbuffer->open(QBuffer::ReadWrite);
-    myXmlwriter->writeRegistering(regbuffer,
+
+    // Without profile dialog
+    /*myXmlwriter->writeRegistering(regbuffer,
                        myMainw->settingsDialog->getRegUserName(),
                        myMainw->settingsDialog->getRegPassword(),
-                       myMainw->settingsDialog->getRegEmail());
+                       myMainw->settingsDialog->getRegEmail());*/
+    // With profile dialog
+    /*myXmlwriter->writeRegistering(regbuffer,
+                                myMainw->settingsDialog->getRegUserName(),
+                                myMainw->settingsDialog->getRegPassword(),
+                                myMainw->settingsDialog->getRegEmail(),
+                                myMainw->settingsDialog->profileDialog->getDescription());
+                                myMainw->settingsDialog->profileDialog->getManufacturer(),
+                                myMainw->settingsDialog->profileDialog->getType(),
+                                myMainw->settingsDialog->profileDialog->getModel(),
+                                myMainw->settingsDialog->profileDialog->getDescription(),
+                                myMainw->settingsDialog->profileDialog->getPicture());*/
+    // New way: Registerdialog = register + Profiledialog
+    myXmlwriter->writeRegistering(regbuffer,
+                                myMainw->settingsDialog->registerDialog->getRegUserName(),
+                                myMainw->settingsDialog->registerDialog->getRegPassword(),
+                                myMainw->settingsDialog->registerDialog->getRegEmail(),
+                                myMainw->settingsDialog->registerDialog->getDescription());
+
     qDebug() << "carmainwindow: regbuffer->data(): " << regbuffer->data();
 
     currentDownload = netManager->post(request, ("xml=" + regbuffer->data()));
@@ -67,8 +88,8 @@ void HttpClient::requestRegistration()
     //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
 
     //Indicating user
-    if(myMainw->settingsDialog)
-        myMainw->settingsDialog->setLabelInfoToUser("Reguesting registration from server");
+    if(myMainw->settingsDialog->registerDialog)
+        myMainw->settingsDialog->registerDialog->setLabelInfoToUser("Reguesting registration from server");
 
     regbuffer->close();
 }
@@ -80,10 +101,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;
@@ -114,18 +137,20 @@ void HttpClient::sendResultXml(QString category, double result)
   * @param int 1(send to server) or 0(no send)
   * @todo Check destination URL.
   */
-void HttpClient::sendRouteXml(QString s, int i)
+void HttpClient::sendRouteXml(QString oldName, QString newName, int i)
 {
     qDebug() << "_sendRouteXml";
 
-    QString filename = ".//speedfreak_route/route.xml";
+    //QString filename = "/home/user/MyDocs/speedfreak/route/route.xml";
+    qDebug() << "__old:" + oldName;
+    QString filename = newName; //+ ".xml";
 
-    if(s != "")
+    if(newName != "")
     {
         qDebug() << "_rename xml";
-        filename = s + ".xml";
         QDir dir(filename);
-        qDebug() << dir.rename(".//speedfreak_route/route.xml", filename);
+        qDebug() << "__new:" + filename;
+        qDebug() << dir.rename(oldName, filename);
     }
 
     if(i == 1)
@@ -138,7 +163,7 @@ void HttpClient::sendRouteXml(QString s, int i)
             return;
         }
 
-        QUrl qurl("http://api.speedfreak-app.com/api/update/route");
+        QUrl qurl("http://speedfreak-app.com/update/route");
         qDebug() << qurl.toString();
         QNetworkRequest request(qurl);
         QNetworkReply *currentDownload;
@@ -170,7 +195,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_results/";
     QUrl qurl(urlBase + category + "/" + limit);
     qDebug() << qurl.toString();
     QNetworkRequest request(qurl);
@@ -197,7 +222,7 @@ void HttpClient::requestCategories()
 {
     qDebug() << "_requestCategories" ;
 
-    QUrl qurl("http://api.speedfreak-app.com/api/categories/");
+    QUrl qurl("http://www.speedfreak-app.com/results/categories");
     qDebug() << qurl.toString();
     QNetworkRequest request(qurl);
     QNetworkReply *currentDownload;
@@ -223,8 +248,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;
 
@@ -268,8 +294,6 @@ void HttpClient::ackOfResult()
             myMainw->accstart->accRealTimeDialog->resultDialog->setSendServerButtonEnabled();
     }
     else {
-        qDebug() << "errorcode:" << errorcode << reply->errorString();
-
         //Indicating user
         if(myMainw->accstart->accRealTimeDialog->resultDialog)
             QMessageBox::about(myMainw->accstart->accRealTimeDialog->resultDialog, "Server reply to result sending", "Result received " + reply->readAll());
@@ -315,7 +339,7 @@ void HttpClient::ackOfRegistration()
     qDebug() << "_ackOfRegistration";
 
     if(myMainw->settingsDialog)
-        myMainw->settingsDialog->setLabelInfoToUser("");
+        myMainw->settingsDialog->registerDialog->setLabelInfoToUser("");
 
     QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
 
@@ -323,15 +347,15 @@ void HttpClient::ackOfRegistration()
     errorcode = reply->error();
     if(errorcode != 0) {
         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
-        if(myMainw->settingsDialog)
-            QMessageBox::about(myMainw->settingsDialog, "Server reply to registration",reply->readAll());
+        if(myMainw->settingsDialog->registerDialog)
+            QMessageBox::about(myMainw->settingsDialog->registerDialog, "Server reply to registration",reply->readAll());
     }
     else {
         qDebug() << "errorcode=0" << errorcode << reply->errorString();
-        if(myMainw->settingsDialog)
+        if(myMainw->settingsDialog->registerDialog)
         {
-            QMessageBox::about(myMainw->settingsDialog, "Server reply to registration", "User registration " + reply->readAll());
-            myMainw->settingsDialog->clearRegisterLineEdits();
+            QMessageBox::about(myMainw->settingsDialog->registerDialog, "Server reply to registration", "User registration " + reply->readAll());
+            myMainw->settingsDialog->registerDialog->clearRegisterLineEdits();
         }
     }
 }
@@ -358,7 +382,6 @@ void HttpClient::ackOfCategories()
             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("");
@@ -470,18 +493,18 @@ void HttpClient::sendProfileXml()
         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());
+            myMainw->settingsDialog->registerDialog->getManufacturer(),
+            myMainw->settingsDialog->registerDialog->getType(),
+            myMainw->settingsDialog->registerDialog->getModel(),
+            myMainw->settingsDialog->registerDialog->getDescription(),
+            myMainw->settingsDialog->registerDialog->getPicture());
 
     //Indicating user
-    if(myMainw->settingsDialog->profileDialog)
-        myMainw->settingsDialog->profileDialog->setLabelInfoToUser("Profile saved to phone");
+    if(myMainw->settingsDialog->registerDialog)
+        myMainw->settingsDialog->registerDialog->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;
@@ -522,20 +545,20 @@ bool HttpClient::ackOfProfile()
     if(errorcode != 0) {
         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
         //Indicating user
-        if(myMainw->settingsDialog->profileDialog)
+        if(myMainw->settingsDialog->registerDialog)
         {
             //QMessageBox::about(myMainw->settingsDialog->profileDialog, "Server reply to requesting profile",reply->errorString());
-            myMainw->settingsDialog->profileDialog->setLabelInfoToUser("Profile save to server - fail");
+            myMainw->settingsDialog->registerDialog->setLabelInfoToUser("Profile save to server - fail");
             return true;
         }
     }
     else {
         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
         //Indicating user
-        if(myMainw->settingsDialog->profileDialog)
+        if(myMainw->settingsDialog->registerDialog)
         {
             //QMessageBox::about(myMainw->settingsDialog->profileDialog, "Server reply to requesting profile", "OK " + reply->readAll());
-            myMainw->settingsDialog->profileDialog->setLabelInfoToUser("Profile saved to server");
+            myMainw->settingsDialog->registerDialog->setLabelInfoToUser("Profile saved to server");
             return false;
         }
     }
@@ -552,19 +575,177 @@ void HttpClient::ackOfSendingPicture()
     if(errorcode != 0) {
         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
         //Indicating user
-        if(myMainw->settingsDialog->profileDialog)
+        if(myMainw->settingsDialog->registerDialog)
         {
             //QMessageBox::about(myMainw->settingsDialog->profileDialog, "Server reply to requesting picture",reply->errorString());
-            myMainw->settingsDialog->profileDialog->setLabelInfoToUser("Picture save to server - fail");
+            myMainw->settingsDialog->registerDialog->setLabelInfoToUser("Picture save to server - fail");
         }
     }
     else {
         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
         //Indicating user
-        if(myMainw->settingsDialog->profileDialog)
+        if(myMainw->settingsDialog->registerDialog)
         {
             //QMessageBox::about(myMainw->settingsDialog->profileDialog, "Server reply to requesting picture", "OK " + reply->readAll());
-            myMainw->settingsDialog->profileDialog->setLabelInfoToUser("Picture saved to server");
+            myMainw->settingsDialog->registerDialog->setLabelInfoToUser("Picture saved to server");
         }
     }
 }
+
+/**
+  *@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));
+    //}
+    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->usersDialog)
+            myMainw->usersDialog->setLabelInfoToUser("You're not logged! Please register or log in.");
+    }
+    else {
+        myXmlreader->xmlReadUserInfo(reply);
+        qDebug() <<  "errorcode:" << errorcode << reply->errorString();
+        //QMessageBox::about(myMainw->topResultDialog, "Server reply to requesting categories ", "OK");
+        if(myMainw->usersDialog)
+            myMainw->usersDialog->setLabelInfoToUser("");
+    }
+}
+
+/**
+  *@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->usersDialog)
+        myMainw->usersDialog->setLabelInfoToUser("Reguesting users 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);
+
+    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->usersDialog)
+            myMainw->usersDialog->setLabelInfoToUser("You're not logged! Please register or log in.");
+    }
+    else {
+        myXmlreader->xmlReadUsers(reply);
+        qDebug() <<  "errorcode:" << errorcode << reply->errorString();
+        //QMessageBox::about(myMainw->topResultDialog, "Server reply to requesting categories ", "OK");
+        if(myMainw->usersDialog)
+            myMainw->usersDialog->setLabelInfoToUser("");
+        for(int i = 0; i < myXmlreader->usersList->count(); i++)
+        {
+            myMainw->usersDialog->appendUserToList(myXmlreader->usersList->at(i));
+        }
+    }
+}
+
+/**
+  * 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);
+}