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