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