Fixed bug 5709. Now result dialog shows only two decimals in time labels.
[speedfreak] / Client / httpclient.cpp
1 /*
2  * Http client Connects application to server.
3  *
4  * @author      Tiina Kivilinna-Korhola <tiina.kivilinna-korhola@fudeco.com>
5  * @author      Olavi Pulkkinen <olavi.pulkkinen@fudeco.com>
6  * @author      Toni Jussila    <toni.jussila@fudeco.com>
7  * @copyright   (c) 2010 Speed Freak team
8  * @license     http://opensource.org/licenses/gpl-license.php GNU Public License
9  */
10
11 #include <QString>
12 #include <QMessageBox>
13 #include <QDir>
14 #include "httpclient.h"
15 #include "mainwindow.h"
16
17 /**
18   *@brief Constructor, connects object to GUI
19   *@param Pointer to carmainwindow, which is temporarily used during development
20   */
21 HttpClient::HttpClient(MainWindow *myCarw)
22 {
23     qDebug() << "__HttpClient";
24     myMainw = myCarw;
25     netManager = new QNetworkAccessManager();
26     myXmlwriter = new XmlWriter();
27     myXmlreader = new XmlReader();
28     connect(myXmlreader, SIGNAL(userInfo(QStringList*)), this, SLOT(sendUsersInfo(QStringList*)));
29 }
30
31 /**
32   *@brief Destructor
33   */
34 HttpClient::~HttpClient()
35 {
36     qDebug() << "__~HttpClient" ;
37
38     if(myXmlwriter)
39         delete myXmlwriter;
40     if(myXmlreader)
41         delete myXmlreader;
42 }
43
44 /**
45   *@brief Sends registration information to the server in xml format.
46   *Reads user name, password and emaol address from resuldialogs internal variables.
47   */
48 void HttpClient::requestRegistration()
49 {
50     qDebug() << "_requestRegistration" ;
51     qDebug() <<  myMainw->settingsDialog->registerDialog->getRegUserName() << "+" <<  myMainw->settingsDialog->registerDialog->getRegPassword() << "+" <<  myMainw->settingsDialog->registerDialog->getRegEmail();
52
53     QBuffer *regbuffer = new QBuffer();
54     QUrl qurl("http://www.speedfreak-app.com/users/register");
55     QNetworkRequest request(qurl);
56     qDebug() << qurl.toString();
57     QNetworkReply *currentDownload;
58
59     regbuffer->open(QBuffer::ReadWrite);
60
61     // Without profile dialog
62     /*myXmlwriter->writeRegistering(regbuffer,
63                        myMainw->settingsDialog->getRegUserName(),
64                        myMainw->settingsDialog->getRegPassword(),
65                        myMainw->settingsDialog->getRegEmail());*/
66     // With profile dialog
67     /*myXmlwriter->writeRegistering(regbuffer,
68                                 myMainw->settingsDialog->getRegUserName(),
69                                 myMainw->settingsDialog->getRegPassword(),
70                                 myMainw->settingsDialog->getRegEmail(),
71                                 myMainw->settingsDialog->profileDialog->getDescription());
72                                 myMainw->settingsDialog->profileDialog->getManufacturer(),
73                                 myMainw->settingsDialog->profileDialog->getType(),
74                                 myMainw->settingsDialog->profileDialog->getModel(),
75                                 myMainw->settingsDialog->profileDialog->getDescription(),
76                                 myMainw->settingsDialog->profileDialog->getPicture());*/
77     // New way: Registerdialog = register + Profiledialog
78     myXmlwriter->writeRegistering(regbuffer,
79                                 myMainw->settingsDialog->registerDialog->getRegUserName(),
80                                 myMainw->settingsDialog->registerDialog->getRegPassword(),
81                                 myMainw->settingsDialog->registerDialog->getRegEmail(),
82                                 myMainw->settingsDialog->registerDialog->getDescription());
83
84     qDebug() << "carmainwindow: regbuffer->data(): " << regbuffer->data();
85
86     currentDownload = netManager->post(request, ("xml=" + regbuffer->data()));
87     connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfRegistration()));
88     //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
89
90     //Indicating user
91     if(myMainw->settingsDialog->registerDialog)
92         myMainw->settingsDialog->registerDialog->setLabelInfoToUser("Reguesting registration from server");
93
94     regbuffer->close();
95 }
96
97 /**
98   *@brief Sends result(s) to the server in xml format.
99   *Send authentication information in the header.
100   */
101 void HttpClient::sendResultXml(QString category, double result)
102 {
103     qDebug() << "_sendResultXml";
104     qDebug() << category;
105
106     QBuffer *xmlbuffer = new QBuffer();
107
108     QUrl qurl("http://www.speedfreak-app.com/results/update/" + category);
109
110     qDebug() << qurl.toString();
111     QNetworkRequest request(qurl);
112     QNetworkReply *currentDownload;
113
114     xmlbuffer->open(QBuffer::ReadWrite);
115     myXmlwriter->writeResult(xmlbuffer, result);
116     qDebug() << "carmainwindow: xmlbuffer->data(): " << xmlbuffer->data();
117
118     QString credentials = myMainw->settingsDialog->getUserName() + ":" + myMainw->settingsDialog->getPassword();
119     credentials = "Basic " + credentials.toAscii().toBase64();
120     request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
121
122     currentDownload = netManager->post(request, ("xml=" + xmlbuffer->data()));
123     connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfResult()));
124     //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
125
126     //Indicating user
127     if(myMainw->accstart->accRealTimeDialog->resultDialog)
128         myMainw->accstart->accRealTimeDialog->resultDialog->setLabelInfoToUser("Sending result to server");
129
130     xmlbuffer->close();
131 }
132
133 /**
134   * @brief Sends route to the server in xml format.
135   * Send authentication information in the header.
136   * @param QString filename
137   * @param int 1(send to server) or 0(no send)
138   * @todo Check destination URL.
139   */
140 void HttpClient::sendRouteXml(QString oldName, QString newName, int i)
141 {
142     qDebug() << "_sendRouteXml";
143
144     //QString filename = "/home/user/MyDocs/speedfreak/route/route.xml";
145     qDebug() << "__old:" + oldName;
146     QString filename = newName; //+ ".xml";
147
148     if(newName != "")
149     {
150         qDebug() << "_rename xml";
151         QDir dir(filename);
152         qDebug() << "__new:" + filename;
153         qDebug() << dir.rename(oldName, filename);
154     }
155
156     if(i == 1)
157     {
158         qDebug() << "_send route";
159         QFile file(filename);
160         if (!file.open(QFile::ReadOnly))
161         {
162             qDebug() << "_sendRouteXml file.open() fail";
163             return;
164         }
165
166         QUrl qurl("http://speedfreak-app.com/update/route");
167         qDebug() << qurl.toString();
168         QNetworkRequest request(qurl);
169         QNetworkReply *currentDownload;
170
171         QString credentials = myMainw->settingsDialog->getUserName() + ":" + myMainw->settingsDialog->getPassword();
172         credentials = "Basic " + credentials.toAscii().toBase64();
173         request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
174
175         currentDownload = netManager->post(request, ("xml=" + file.readAll()));
176         connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfRoute()));
177         //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
178
179         //Indicating user
180         if(myMainw->routeSaveDialog->routeDialog)
181             myMainw->routeSaveDialog->routeDialog->setLabelInfoToUser("Sending route to server");
182
183         file.close();
184     }
185 }
186
187 /**
188   *@brief Request the Top10List of certain category from the server.
189   *Send authentication information in the header.
190   *@param Category of results.
191   *@param Limit, the number of results.
192   */
193 void HttpClient::requestTopList(QString category, QString limit)
194 {
195     qDebug() << "_requestTopList";
196     qDebug() << category;
197
198     QString urlBase = "http://www.speedfreak-app.com/results/list_results/";
199     QUrl qurl(urlBase + category + "/" + limit);
200     qDebug() << qurl.toString();
201     QNetworkRequest request(qurl);
202     QNetworkReply *currentDownload;
203
204     QString credentials = myMainw->settingsDialog->getUserName() + ":" + myMainw->settingsDialog->getPassword();
205     credentials = "Basic " + credentials.toAscii().toBase64();
206     request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
207
208     currentDownload = netManager->post(request, ("data=" ));
209     connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfToplist()));
210     //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
211
212     //Indicating user
213     if(myMainw->topResultDialog)
214         myMainw->topResultDialog->setLabelInfoToUser("Reguesting top10 list from server");
215 }
216
217 /**
218   *@brief Request categories list from the server.
219   *Send authentication information in the header.
220   */
221 void HttpClient::requestCategories()
222 {
223     qDebug() << "_requestCategories" ;
224
225     QUrl qurl("http://www.speedfreak-app.com/results/categories");
226     qDebug() << qurl.toString();
227     QNetworkRequest request(qurl);
228     QNetworkReply *currentDownload;
229
230     QString credentials = myMainw->settingsDialog->getUserName() + ":" + myMainw->settingsDialog->getPassword();
231     credentials = "Basic " + credentials.toAscii().toBase64();
232     request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
233
234     currentDownload = netManager->post(request, ("data=" ));
235     connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfCategories()));
236     //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
237
238     //Indicating user
239     if(myMainw->topResultDialog)
240         myMainw->topResultDialog->setLabelInfoToUser("Reguesting categories from server");
241 }
242
243 /**
244   *@brief Check that username and password exist on the server.
245   *Send authentication information in the header.
246   */
247 void HttpClient::checkLogin()
248 {
249     qDebug() << "_checkLogin";
250
251     QUrl qurl("http://www.speedfreak-app.com/users/login");
252     qDebug() << qurl.toString();
253
254     QNetworkRequest request(qurl);
255     QNetworkReply *currentDownload;
256
257     QString credentials = myMainw->settingsDialog->getUserName() + ":" + myMainw->settingsDialog->getPassword();
258     credentials = "Basic " + credentials.toAscii().toBase64();
259     request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
260
261     currentDownload = netManager->post(request, ("data=" ));
262     connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfLogin()));
263     //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
264
265     //Indicating user
266     if(myMainw->settingsDialog)
267         myMainw->settingsDialog->setLabelInfoToUser("Checking login validity from server");
268 }
269
270 /**
271   *@brief React to servers responce after result has been sent.
272   */
273 void HttpClient::ackOfResult()
274 {
275     qDebug() << "_ackOfResult";
276
277     //Indicating user
278     if(myMainw->accstart->accRealTimeDialog->resultDialog)
279         myMainw->accstart->accRealTimeDialog->resultDialog->setLabelInfoToUser("");
280
281     QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
282
283     QNetworkReply::NetworkError errorcode;
284     errorcode = reply->error();
285     if(errorcode != 0) {
286         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
287
288         //Indicating user
289         if(myMainw->accstart->accRealTimeDialog->resultDialog)
290             QMessageBox::about(myMainw->accstart->accRealTimeDialog->resultDialog, "Server reply to result sending ",reply->errorString());
291         if(myMainw->accstart->accRealTimeDialog->resultDialog)
292             myMainw->accstart->accRealTimeDialog->resultDialog->setLabelInfoToUser("Error");
293         if(myMainw->accstart->accRealTimeDialog->resultDialog)
294             myMainw->accstart->accRealTimeDialog->resultDialog->setSendServerButtonEnabled();
295     }
296     else {
297         //Indicating user
298         if(myMainw->accstart->accRealTimeDialog->resultDialog)
299             QMessageBox::about(myMainw->accstart->accRealTimeDialog->resultDialog, "Server reply to result sending", "Result received " + reply->readAll());
300         if(myMainw->accstart->accRealTimeDialog->resultDialog)
301             myMainw->accstart->accRealTimeDialog->resultDialog->setLabelInfoToUser("Result received");
302     }
303 }
304
305 /**
306   *@brief React to servers responce after route has been sent.
307   */
308 void HttpClient::ackOfRoute()
309 {
310     qDebug() << "_ackOfRoute";
311
312     if(myMainw->routeSaveDialog->routeDialog)
313         myMainw->routeSaveDialog->routeDialog->setLabelInfoToUser("");
314
315     QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
316
317     QNetworkReply::NetworkError errorcode;
318     errorcode = reply->error();
319     if(errorcode != 0) {
320         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
321         if(myMainw->routeSaveDialog->routeDialog)
322             QMessageBox::about(myMainw->routeSaveDialog->routeDialog, "Server reply to route sending ",reply->errorString());
323         if(myMainw->routeSaveDialog->routeDialog)
324             myMainw->routeSaveDialog->routeDialog->setSendServerButtonEnabled();
325     }
326     else {
327         qDebug() << "errorcode:" << errorcode << reply->errorString();
328         if(myMainw->routeSaveDialog->routeDialog)
329             QMessageBox::about(myMainw->routeSaveDialog->routeDialog, "Server reply to route sending", "Route received " + reply->readAll());
330     }
331 }
332
333 /**
334   *@brief React to servers responce after registration has been sent.
335   *@todo Implement consequencies of reply.
336   */
337 void HttpClient::ackOfRegistration()
338 {
339     qDebug() << "_ackOfRegistration";
340
341     if(myMainw->settingsDialog)
342         myMainw->settingsDialog->registerDialog->setLabelInfoToUser("");
343
344     QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
345
346     QNetworkReply::NetworkError errorcode;
347     errorcode = reply->error();
348     if(errorcode != 0) {
349         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
350         if(myMainw->settingsDialog->registerDialog)
351             QMessageBox::about(myMainw->settingsDialog->registerDialog, "Server reply to registration",reply->readAll());
352     }
353     else {
354         qDebug() << "errorcode=0" << errorcode << reply->errorString();
355         if(myMainw->settingsDialog->registerDialog)
356         {
357             QMessageBox::about(myMainw->settingsDialog->registerDialog, "Server reply to registration", "User registration " + reply->readAll());
358             myMainw->settingsDialog->registerDialog->clearRegisterLineEdits();
359         }
360     }
361 }
362
363 /**
364   *@brief React to servers responce after request for categories has been sent.
365   */
366 void HttpClient::ackOfCategories()
367 {
368     qDebug() << "_ackOfCategories";
369
370     if(myMainw->topResultDialog)
371         myMainw->topResultDialog->setLabelInfoToUser("");
372
373     QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
374     myXmlreader->xmlReadCategories(reply);
375
376     QNetworkReply::NetworkError errorcode;
377     errorcode = reply->error();
378     if(errorcode != 0) {
379         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
380         //QMessageBox::about(myMainw->topResultDialog, "Server reply to requesting categories",reply->errorString());
381         if(myMainw->topResultDialog)
382             myMainw->topResultDialog->setLabelInfoToUser("You're not logged! Please register or log in.");
383     }
384     else {
385         //QMessageBox::about(myMainw->topResultDialog, "Server reply to requesting categories ", "OK");
386         if(myMainw->topResultDialog)
387             myMainw->topResultDialog->setLabelInfoToUser("");
388     }
389 }
390
391 /**
392   *@brief React to servers responce after request of TopList in certain category has been sent.
393   */
394 void HttpClient::ackOfLogin()
395 {
396     qDebug() << "_ackOffLogin";
397
398     if(myMainw->settingsDialog)
399         myMainw->settingsDialog->setLabelInfoToUser("");
400
401     QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
402
403     QNetworkReply::NetworkError errorcode;
404     errorcode = reply->error();
405     if(errorcode != 0) 
406     {
407         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
408         if(myMainw->settingsDialog)
409         {
410                 QMessageBox::about(myMainw->settingsDialog, "Server does not recognize your username. Please registrate.",reply->errorString());
411                 myMainw->settingsDialog->usernameOk(false);
412         }
413     }
414     else 
415     {
416         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
417         if(myMainw->settingsDialog)
418                 QMessageBox::about(myMainw->settingsDialog, "Server reply to login", "User login " + reply->readAll());
419         // here signal emit to mainwindow for username setting to main panel
420         emit loginOK();
421         if( myMainw->settingsDialog)
422         {
423                 myMainw->settingsDialog->usernameOk(true);
424                 myMainw->settingsDialog->close();    
425         }
426     }
427 }
428
429 /**
430   *@brief Reports errors, when server has sent error signal.
431   */
432 void HttpClient::errorFromServer(QNetworkReply::NetworkError errorcode)
433 {
434     qDebug() << "_errorFromServer";
435     QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
436
437     if(errorcode != 0) {
438         qDebug() <<  "errorcode:" << errorcode;
439         //Note that errors are already reported on other each functions for server communication
440         //QMessageBox::about(myMainw, "Server reported an error", reply->errorString());
441     }
442     else {
443         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
444         qDebug() << reply->readAll();
445     }
446 }
447
448 /**
449   *@brief React to servers responce after request of TopList in certain category has been sent.
450   */
451 void HttpClient::ackOfToplist()
452 {
453     qDebug() << "_ackOfToplist";
454     QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
455     myXmlreader->xmlReadTop10Results(reply,myMainw->settingsDialog->getUserName());
456
457     QNetworkReply::NetworkError errorcode;
458     errorcode = reply->error();
459     if(errorcode != 0) {
460         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
461         //Indicating user
462         if(myMainw->topResultDialog)
463         {
464             //QMessageBox::about(myMainw->topResultDialog, "Server reply to requesting top 10 list",reply->errorString());
465             myMainw->topResultDialog->setLabelInfoToUser("No results ;(");
466         }
467     }
468     else {
469         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
470         //Indicating user
471         if(myMainw->topResultDialog)
472         {
473             //QMessageBox::about(myMainw->topResultDialog, "Server reply to requesting top 10 list", "OK " + reply->readAll());
474             myMainw->topResultDialog->setLabelInfoToUser("");
475         }
476     }
477 }
478
479 /**
480   * This function sends profile to the server in xml format.
481   * Send authentication information in the header.
482   */
483 void HttpClient::sendProfileXml()
484 {
485     qDebug() << "_sendProfileXml";
486
487     QString userName = myMainw->settingsDialog->getUserName();
488     QString filename = userName + "_profile.xml";
489     QFile file(filename);
490     if (!file.open(QFile::ReadWrite | QFile::Text))
491     {
492         qDebug() << "_xmlWrite fail";
493         return;
494     }
495     myXmlwriter->writeProfileXmlFile(&file, userName,
496             myMainw->settingsDialog->registerDialog->getManufacturer(),
497             myMainw->settingsDialog->registerDialog->getType(),
498             myMainw->settingsDialog->registerDialog->getModel(),
499             myMainw->settingsDialog->registerDialog->getDescription(),
500             myMainw->settingsDialog->registerDialog->getPicture());
501
502     //Indicating user
503     if(myMainw->settingsDialog->registerDialog)
504         myMainw->settingsDialog->registerDialog->setLabelInfoToUser("Profile saved to phone");
505
506     // Send xml to server
507     /*QUrl qurl("http://speedfreak-app.com/api/profile");
508     QNetworkRequest request(qurl);
509     qDebug() << qurl.toString();
510     QNetworkReply *currentDownload;
511
512     QString credentials = myMainw->settingsDialog->getUserName() + ":" + myMainw->settingsDialog->getPassword();
513     credentials = "Basic " + credentials.toAscii().toBase64();
514     request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
515
516     currentDownload = netManager->post(request, ("xml=" + file.readAll()));
517     bool error = connect(currentDownload, SIGNAL(finished()), this, SLOT(ackOfProfile()));*/
518
519     file.close();
520
521     // Send picture to server
522     /*if(myMainw->settingsDialog->profileDialog->getPicture() != "" && error == false)
523     {
524         QFile pictureFile( myMainw->settingsDialog->profileDialog->getPicture() );
525         if (!pictureFile.open(QIODevice::ReadOnly))
526         {
527             qDebug() << "__picture read fail";
528             return;
529         }
530         currentDownload = netManager->post(request, pictureFile.readAll());
531         connect(currentDownload, SIGNAL(finished()), this, SLOT(ackOfSendingPicture()));
532         pictureFile.close();
533     }*/
534 }
535
536 /**
537   * This slot function react to servers responce after request of profile has been sent.
538   */
539 bool HttpClient::ackOfProfile()
540 {
541     qDebug() << "__ackOfProfile";
542     QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
543     QNetworkReply::NetworkError errorcode;
544     errorcode = reply->error();
545     if(errorcode != 0) {
546         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
547         //Indicating user
548         if(myMainw->settingsDialog->registerDialog)
549         {
550             //QMessageBox::about(myMainw->settingsDialog->profileDialog, "Server reply to requesting profile",reply->errorString());
551             myMainw->settingsDialog->registerDialog->setLabelInfoToUser("Profile save to server - fail");
552             return true;
553         }
554     }
555     else {
556         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
557         //Indicating user
558         if(myMainw->settingsDialog->registerDialog)
559         {
560             //QMessageBox::about(myMainw->settingsDialog->profileDialog, "Server reply to requesting profile", "OK " + reply->readAll());
561             myMainw->settingsDialog->registerDialog->setLabelInfoToUser("Profile saved to server");
562             return false;
563         }
564     }
565 }
566 /**
567   * This slot function react to servers responce after request of picture has been sent.
568   */
569 void HttpClient::ackOfSendingPicture()
570 {
571     qDebug() << "__ackOfSendingPicture";
572     QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
573     QNetworkReply::NetworkError errorcode;
574     errorcode = reply->error();
575     if(errorcode != 0) {
576         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
577         //Indicating user
578         if(myMainw->settingsDialog->registerDialog)
579         {
580             //QMessageBox::about(myMainw->settingsDialog->profileDialog, "Server reply to requesting picture",reply->errorString());
581             myMainw->settingsDialog->registerDialog->setLabelInfoToUser("Picture save to server - fail");
582         }
583     }
584     else {
585         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
586         //Indicating user
587         if(myMainw->settingsDialog->registerDialog)
588         {
589             //QMessageBox::about(myMainw->settingsDialog->profileDialog, "Server reply to requesting picture", "OK " + reply->readAll());
590             myMainw->settingsDialog->registerDialog->setLabelInfoToUser("Picture saved to server");
591         }
592     }
593 }
594
595 /**
596   *@brief Request the user information of certain user from the server.
597   *Send authentication information in the header.
598   *@param username which information we want.
599   */
600 void HttpClient::requestUserInfo(QString username)
601 {
602     qDebug() << "_requestUsersInfo" ;
603
604     QUrl qurl("http://speedfreak-app.com/users/info/" + username);
605     qDebug() << qurl.toString();
606     QNetworkRequest request(qurl);
607     QNetworkReply *currentDownload;
608
609     QString credentials = myMainw->settingsDialog->getUserName() + ":" + myMainw->settingsDialog->getPassword();
610     credentials = "Basic " + credentials.toAscii().toBase64();
611     request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
612
613     currentDownload = netManager->post(request, ("data=" ));
614     connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfUserInfo()));
615
616     //qDebug() << "requestUserInfo";
617     //ackOfUserInfo();
618 }
619
620 /**
621   *@brief React to servers responce after request the user information of certain user.
622   */
623 void HttpClient::ackOfUserInfo()
624 {
625     qDebug() << "ackUserInfo";
626     /*QString fileName = "user.xml";
627     QFile file(fileName);
628     //file.setFileName( "routetemp.xml");
629     if (!file.open(QFile::ReadOnly))
630     {
631         qDebug() << "_xmlShow fail";
632         return;
633     }
634
635     myXmlreader->xmlReadUserInfo(&file);
636     file.close();*/
637
638     QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
639     //myXmlreader->xmlReadUserInfo(reply);
640
641     //for(int i = 0; i < myXmlreader->usersList->count(); i++)
642     //{
643     //    myMainw->settingsDialog->sendUsernameToUsersDialog(myXmlreader->usersList->at(i));
644     //}
645     QNetworkReply::NetworkError errorcode;
646     errorcode = reply->error();
647     if(errorcode != 0) {
648         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
649         //QMessageBox::about(myMainw->topResultDialog, "Server reply to requesting categories",reply->errorString());
650         if(myMainw->usersDialog)
651             myMainw->usersDialog->setLabelInfoToUser("You're not logged! Please register or log in.");
652     }
653     else {
654         myXmlreader->xmlReadUserInfo(reply);
655         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
656         //QMessageBox::about(myMainw->topResultDialog, "Server reply to requesting categories ", "OK");
657         if(myMainw->usersDialog)
658             myMainw->usersDialog->setLabelInfoToUser("");
659     }
660 }
661
662 /**
663   *@brief Request the users list of all users from the server.
664   *Send authentication information in the header.
665   */
666 void HttpClient::requestUsers()
667 {  
668     qDebug() << "_requestUsers" ;
669
670     QUrl qurl("http://www.speedfreak-app.com/users/list_all");
671     qDebug() << qurl.toString();
672     QNetworkRequest request(qurl);
673     QNetworkReply *currentDownload;
674
675     QString credentials = myMainw->settingsDialog->getUserName() + ":" + myMainw->settingsDialog->getPassword();
676     credentials = "Basic " + credentials.toAscii().toBase64();
677     request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
678
679     currentDownload = netManager->post(request, ("data=" ));
680     connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfUsers()));
681
682
683     //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
684
685     //Indicating user
686     if(myMainw->usersDialog)
687         myMainw->usersDialog->setLabelInfoToUser("Reguesting users from server");
688
689     //ackOfUsers();
690 }
691
692 /**
693   *@brief React to servers responce after request the users list of all users.
694   */
695 void HttpClient::ackOfUsers()
696 {
697     qDebug() << "ackUsers";
698    /* QString fileName = "jtn.xml";
699     QFile file(fileName);
700     //file.setFileName( "routetemp.xml");
701     if (!file.open(QFile::ReadOnly))
702     {
703         qDebug() << "_xmlShow fail";
704         return;
705     }
706
707     myXmlreader->xmlReadUsers(&file);
708     file.close();
709
710     for(int i = 0; i < myXmlreader->usersList->count(); i++)
711     {
712         myMainw->settingsDialog->sendUsernameToUsersDialog(myXmlreader->usersList->at(i));
713     }*/
714
715     qDebug() << "ackUsers";
716
717     //if(myMainw->topResultDialog)
718     //    myMainw->topResultDialog->setLabelInfoToUser("");
719
720     QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
721     //myXmlreader->xmlReadUsers(reply);
722
723     QNetworkReply::NetworkError errorcode;
724     errorcode = reply->error();
725     if(errorcode != 0) {
726         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
727         //QMessageBox::about(myMainw->topResultDialog, "Server reply to requesting categories",reply->errorString());
728         if(myMainw->usersDialog)
729             myMainw->usersDialog->setLabelInfoToUser("You're not logged! Please register or log in.");
730     }
731     else {
732         myXmlreader->xmlReadUsers(reply);
733         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
734         //QMessageBox::about(myMainw->topResultDialog, "Server reply to requesting categories ", "OK");
735         if(myMainw->usersDialog)
736             myMainw->usersDialog->setLabelInfoToUser("");
737         for(int i = 0; i < myXmlreader->usersList->count(); i++)
738         {
739             myMainw->usersDialog->appendUserToList(myXmlreader->usersList->at(i));
740         }
741     }
742 }
743
744 /**
745   * This slot function called when userInfo signal is emitted from xmlreader.
746    *@param usersInfo includes information from certain user.
747   */
748 void HttpClient::sendUsersInfo(QStringList* usersInfo)
749 {
750     myMainw->usersDialog->setUserInfo(usersInfo);
751 }