2 * Http client Connects application to server.
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
12 #include <QMessageBox>
14 #include "httpclient.h"
15 #include "mainwindow.h"
18 *@brief Constructor, connects object to GUI
19 *@param Pointer to carmainwindow, which is temporarily used during development
21 HttpClient::HttpClient(MainWindow *myCarw)
23 qDebug() << "__HttpClient";
25 netManager = new QNetworkAccessManager();
26 myXmlwriter = new XmlWriter();
27 myXmlreader = new XmlReader();
33 HttpClient::~HttpClient()
35 qDebug() << "__~HttpClient" ;
44 *@brief Sends registration information to the server in xml format.
45 *Reads user name, password and emaol address from resuldialogs internal variables.
47 void HttpClient::requestRegistration()
49 qDebug() << "_requestRegistration" ;
50 qDebug() << myMainw->settingsDialog->getRegUserName() << "+" << myMainw->settingsDialog->getRegPassword() << "+" << myMainw->settingsDialog->getRegEmail();
52 QBuffer *regbuffer = new QBuffer();
53 QUrl qurl("http://api.speedfreak-app.com/api/register");
54 QNetworkRequest request(qurl);
55 qDebug() << qurl.toString();
56 QNetworkReply *currentDownload;
58 regbuffer->open(QBuffer::ReadWrite);
59 myXmlwriter->writeRegistering(regbuffer,
60 myMainw->settingsDialog->getRegUserName(),
61 myMainw->settingsDialog->getRegPassword(),
62 myMainw->settingsDialog->getRegEmail());
63 qDebug() << "carmainwindow: regbuffer->data(): " << regbuffer->data();
65 currentDownload = netManager->post(request, ("xml=" + regbuffer->data()));
66 connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfRegistration()));
67 //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
70 if(myMainw->settingsDialog)
71 myMainw->settingsDialog->setLabelInfoToUser("Reguesting registration from server");
77 *@brief Sends result(s) to the server in xml format.
78 *Send authentication information in the header.
80 void HttpClient::sendResultXml(QString category, double result)
82 qDebug() << "_sendResultXml";
84 QBuffer *xmlbuffer = new QBuffer();
86 QUrl qurl("http://api.speedfreak-app.com/api/update/" + category);
87 qDebug() << qurl.toString();
88 QNetworkRequest request(qurl);
89 QNetworkReply *currentDownload;
91 xmlbuffer->open(QBuffer::ReadWrite);
92 myXmlwriter->writeResult(xmlbuffer, result);
93 qDebug() << "carmainwindow: xmlbuffer->data(): " << xmlbuffer->data();
95 QString credentials = myMainw->settingsDialog->getUserName() + ":" + myMainw->settingsDialog->getPassword();
96 credentials = "Basic " + credentials.toAscii().toBase64();
97 request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
99 currentDownload = netManager->post(request, ("xml=" + xmlbuffer->data()));
100 connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfResult()));
101 //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
104 if(myMainw->accstart->accRealTimeDialog->resultDialog)
105 myMainw->accstart->accRealTimeDialog->resultDialog->setLabelInfoToUser("Sending result to server");
111 * @brief Sends route to the server in xml format.
112 * Send authentication information in the header.
113 * @param QString filename
114 * @param int 1(send to server) or 0(no send)
115 * @todo Check destination URL.
117 void HttpClient::sendRouteXml(QString oldName, QString newName, int i)
119 qDebug() << "_sendRouteXml";
121 //QString filename = "/home/user/MyDocs/speedfreak/route/route.xml";
122 qDebug() << "__old:" + oldName;
123 QString filename = newName; //+ ".xml";
127 qDebug() << "_rename xml";
129 qDebug() << "__new:" + filename;
130 qDebug() << dir.rename(oldName, filename);
135 qDebug() << "_send route";
136 QFile file(filename);
137 if (!file.open(QFile::ReadOnly))
139 qDebug() << "_sendRouteXml file.open() fail";
143 QUrl qurl("http://api.speedfreak-app.com/api/update/route");
144 qDebug() << qurl.toString();
145 QNetworkRequest request(qurl);
146 QNetworkReply *currentDownload;
148 QString credentials = myMainw->settingsDialog->getUserName() + ":" + myMainw->settingsDialog->getPassword();
149 credentials = "Basic " + credentials.toAscii().toBase64();
150 request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
152 currentDownload = netManager->post(request, ("xml=" + file.readAll()));
153 connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfRoute()));
154 //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
157 if(myMainw->routeSaveDialog->routeDialog)
158 myMainw->routeSaveDialog->routeDialog->setLabelInfoToUser("Sending route to server");
165 *@brief Request the Top10List of certain category from the server.
166 *Send authentication information in the header.
167 *@param Category of results.
168 *@param Limit, the number of results.
170 void HttpClient::requestTopList(QString category, QString limit)
172 qDebug() << "_requestTopList";
173 qDebug() << category;
175 QString urlBase = "http://api.speedfreak-app.com/api/results/";
176 QUrl qurl(urlBase + category + "/" + limit);
177 qDebug() << qurl.toString();
178 QNetworkRequest request(qurl);
179 QNetworkReply *currentDownload;
181 QString credentials = myMainw->settingsDialog->getUserName() + ":" + myMainw->settingsDialog->getPassword();
182 credentials = "Basic " + credentials.toAscii().toBase64();
183 request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
185 currentDownload = netManager->post(request, ("data=" ));
186 connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfToplist()));
187 //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
190 if(myMainw->topResultDialog)
191 myMainw->topResultDialog->setLabelInfoToUser("Reguesting top10 list from server");
195 *@brief Request categories list from the server.
196 *Send authentication information in the header.
198 void HttpClient::requestCategories()
200 qDebug() << "_requestCategories" ;
202 QUrl qurl("http://api.speedfreak-app.com/api/categories/");
203 qDebug() << qurl.toString();
204 QNetworkRequest request(qurl);
205 QNetworkReply *currentDownload;
207 QString credentials = myMainw->settingsDialog->getUserName() + ":" + myMainw->settingsDialog->getPassword();
208 credentials = "Basic " + credentials.toAscii().toBase64();
209 request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
211 currentDownload = netManager->post(request, ("data=" ));
212 connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfCategories()));
213 //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
216 if(myMainw->topResultDialog)
217 myMainw->topResultDialog->setLabelInfoToUser("Reguesting categories from server");
221 *@brief Check that username and password exist on the server.
222 *Send authentication information in the header.
224 void HttpClient::checkLogin()
226 qDebug() << "_checkLogin";
228 QUrl qurl("http://api.speedfreak-app.com/api/login/");
229 qDebug() << qurl.toString();
230 QNetworkRequest request(qurl);
231 QNetworkReply *currentDownload;
233 QString credentials = myMainw->settingsDialog->getUserName() + ":" + myMainw->settingsDialog->getPassword();
234 credentials = "Basic " + credentials.toAscii().toBase64();
235 request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
237 currentDownload = netManager->post(request, ("data=" ));
238 connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfLogin()));
239 //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
242 if(myMainw->settingsDialog)
243 myMainw->settingsDialog->setLabelInfoToUser("Checking login validity from server");
247 *@brief React to servers responce after result has been sent.
249 void HttpClient::ackOfResult()
251 qDebug() << "_ackOfResult";
254 if(myMainw->accstart->accRealTimeDialog->resultDialog)
255 myMainw->accstart->accRealTimeDialog->resultDialog->setLabelInfoToUser("");
257 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
259 QNetworkReply::NetworkError errorcode;
260 errorcode = reply->error();
262 qDebug() << "errorcode:" << errorcode << reply->errorString();
265 if(myMainw->accstart->accRealTimeDialog->resultDialog)
266 QMessageBox::about(myMainw->accstart->accRealTimeDialog->resultDialog, "Server reply to result sending ",reply->errorString());
267 if(myMainw->accstart->accRealTimeDialog->resultDialog)
268 myMainw->accstart->accRealTimeDialog->resultDialog->setLabelInfoToUser("Error");
269 if(myMainw->accstart->accRealTimeDialog->resultDialog)
270 myMainw->accstart->accRealTimeDialog->resultDialog->setSendServerButtonEnabled();
273 qDebug() << "errorcode:" << errorcode << reply->errorString();
276 if(myMainw->accstart->accRealTimeDialog->resultDialog)
277 QMessageBox::about(myMainw->accstart->accRealTimeDialog->resultDialog, "Server reply to result sending", "Result received " + reply->readAll());
278 if(myMainw->accstart->accRealTimeDialog->resultDialog)
279 myMainw->accstart->accRealTimeDialog->resultDialog->setLabelInfoToUser("Result received");
284 *@brief React to servers responce after route has been sent.
286 void HttpClient::ackOfRoute()
288 qDebug() << "_ackOfRoute";
290 if(myMainw->routeSaveDialog->routeDialog)
291 myMainw->routeSaveDialog->routeDialog->setLabelInfoToUser("");
293 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
295 QNetworkReply::NetworkError errorcode;
296 errorcode = reply->error();
298 qDebug() << "errorcode:" << errorcode << reply->errorString();
299 if(myMainw->routeSaveDialog->routeDialog)
300 QMessageBox::about(myMainw->routeSaveDialog->routeDialog, "Server reply to route sending ",reply->errorString());
301 if(myMainw->routeSaveDialog->routeDialog)
302 myMainw->routeSaveDialog->routeDialog->setSendServerButtonEnabled();
305 qDebug() << "errorcode:" << errorcode << reply->errorString();
306 if(myMainw->routeSaveDialog->routeDialog)
307 QMessageBox::about(myMainw->routeSaveDialog->routeDialog, "Server reply to route sending", "Route received " + reply->readAll());
312 *@brief React to servers responce after registration has been sent.
313 *@todo Implement consequencies of reply.
315 void HttpClient::ackOfRegistration()
317 qDebug() << "_ackOfRegistration";
319 if(myMainw->settingsDialog)
320 myMainw->settingsDialog->setLabelInfoToUser("");
322 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
324 QNetworkReply::NetworkError errorcode;
325 errorcode = reply->error();
327 qDebug() << "errorcode:" << errorcode << reply->errorString();
328 if(myMainw->settingsDialog)
329 QMessageBox::about(myMainw->settingsDialog, "Server reply to registration",reply->readAll());
332 qDebug() << "errorcode=0" << errorcode << reply->errorString();
333 if(myMainw->settingsDialog)
335 QMessageBox::about(myMainw->settingsDialog, "Server reply to registration", "User registration " + reply->readAll());
336 myMainw->settingsDialog->clearRegisterLineEdits();
342 *@brief React to servers responce after request for categories has been sent.
344 void HttpClient::ackOfCategories()
346 qDebug() << "_ackOfCategories";
348 if(myMainw->topResultDialog)
349 myMainw->topResultDialog->setLabelInfoToUser("");
351 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
352 myXmlreader->xmlReadCategories(reply);
354 QNetworkReply::NetworkError errorcode;
355 errorcode = reply->error();
357 qDebug() << "errorcode:" << errorcode << reply->errorString();
358 //QMessageBox::about(myMainw->topResultDialog, "Server reply to requesting categories",reply->errorString());
359 if(myMainw->topResultDialog)
360 myMainw->topResultDialog->setLabelInfoToUser("You're not logged! Please register or log in.");
363 qDebug() << "errorcode:" << errorcode << reply->errorString();
364 //QMessageBox::about(myMainw->topResultDialog, "Server reply to requesting categories ", "OK");
365 if(myMainw->topResultDialog)
366 myMainw->topResultDialog->setLabelInfoToUser("");
371 *@brief React to servers responce after request of TopList in certain category has been sent.
373 void HttpClient::ackOfLogin()
375 qDebug() << "_ackOffLogin";
377 if(myMainw->settingsDialog)
378 myMainw->settingsDialog->setLabelInfoToUser("");
380 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
382 QNetworkReply::NetworkError errorcode;
383 errorcode = reply->error();
386 qDebug() << "errorcode:" << errorcode << reply->errorString();
387 if(myMainw->settingsDialog)
389 QMessageBox::about(myMainw->settingsDialog, "Server does not recognize your username. Please registrate.",reply->errorString());
390 myMainw->settingsDialog->usernameOk(false);
395 qDebug() << "errorcode:" << errorcode << reply->errorString();
396 if(myMainw->settingsDialog)
397 QMessageBox::about(myMainw->settingsDialog, "Server reply to login", "User login " + reply->readAll());
398 // here signal emit to mainwindow for username setting to main panel
400 if( myMainw->settingsDialog)
402 myMainw->settingsDialog->usernameOk(true);
403 myMainw->settingsDialog->close();
409 *@brief Reports errors, when server has sent error signal.
411 void HttpClient::errorFromServer(QNetworkReply::NetworkError errorcode)
413 qDebug() << "_errorFromServer";
414 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
417 qDebug() << "errorcode:" << errorcode;
418 //Note that errors are already reported on other each functions for server communication
419 //QMessageBox::about(myMainw, "Server reported an error", reply->errorString());
422 qDebug() << "errorcode:" << errorcode << reply->errorString();
423 qDebug() << reply->readAll();
428 *@brief React to servers responce after request of TopList in certain category has been sent.
430 void HttpClient::ackOfToplist()
432 qDebug() << "_ackOfToplist";
433 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
434 myXmlreader->xmlReadTop10Results(reply,myMainw->settingsDialog->getUserName());
436 QNetworkReply::NetworkError errorcode;
437 errorcode = reply->error();
439 qDebug() << "errorcode:" << errorcode << reply->errorString();
441 if(myMainw->topResultDialog)
443 //QMessageBox::about(myMainw->topResultDialog, "Server reply to requesting top 10 list",reply->errorString());
444 myMainw->topResultDialog->setLabelInfoToUser("No results ;(");
448 qDebug() << "errorcode:" << errorcode << reply->errorString();
450 if(myMainw->topResultDialog)
452 //QMessageBox::about(myMainw->topResultDialog, "Server reply to requesting top 10 list", "OK " + reply->readAll());
453 myMainw->topResultDialog->setLabelInfoToUser("");
459 * This function sends profile to the server in xml format.
460 * Send authentication information in the header.
462 void HttpClient::sendProfileXml()
464 qDebug() << "_sendProfileXml";
466 QString userName = myMainw->settingsDialog->getUserName();
467 QString filename = userName + "_profile.xml";
468 QFile file(filename);
469 if (!file.open(QFile::ReadWrite | QFile::Text))
471 qDebug() << "_xmlWrite fail";
474 myXmlwriter->writeProfileXmlFile(&file, userName,
475 myMainw->settingsDialog->profileDialog->getManufacturer(),
476 myMainw->settingsDialog->profileDialog->getType(),
477 myMainw->settingsDialog->profileDialog->getModel(),
478 myMainw->settingsDialog->profileDialog->getDescription(),
479 myMainw->settingsDialog->profileDialog->getPicture());
482 if(myMainw->settingsDialog->profileDialog)
483 myMainw->settingsDialog->profileDialog->setLabelInfoToUser("Profile saved to phone");
485 // Send xml to server
486 /*QUrl qurl("http://api.speedfreak-app.com/api/profile");
487 QNetworkRequest request(qurl);
488 qDebug() << qurl.toString();
489 QNetworkReply *currentDownload;
491 QString credentials = myMainw->settingsDialog->getUserName() + ":" + myMainw->settingsDialog->getPassword();
492 credentials = "Basic " + credentials.toAscii().toBase64();
493 request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
495 currentDownload = netManager->post(request, ("xml=" + file.readAll()));
496 bool error = connect(currentDownload, SIGNAL(finished()), this, SLOT(ackOfProfile()));*/
500 // Send picture to server
501 /*if(myMainw->settingsDialog->profileDialog->getPicture() != "" && error == false)
503 QFile pictureFile( myMainw->settingsDialog->profileDialog->getPicture() );
504 if (!pictureFile.open(QIODevice::ReadOnly))
506 qDebug() << "__picture read fail";
509 currentDownload = netManager->post(request, pictureFile.readAll());
510 connect(currentDownload, SIGNAL(finished()), this, SLOT(ackOfSendingPicture()));
516 * This slot function react to servers responce after request of profile has been sent.
518 bool HttpClient::ackOfProfile()
520 qDebug() << "__ackOfProfile";
521 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
522 QNetworkReply::NetworkError errorcode;
523 errorcode = reply->error();
525 qDebug() << "errorcode:" << errorcode << reply->errorString();
527 if(myMainw->settingsDialog->profileDialog)
529 //QMessageBox::about(myMainw->settingsDialog->profileDialog, "Server reply to requesting profile",reply->errorString());
530 myMainw->settingsDialog->profileDialog->setLabelInfoToUser("Profile save to server - fail");
535 qDebug() << "errorcode:" << errorcode << reply->errorString();
537 if(myMainw->settingsDialog->profileDialog)
539 //QMessageBox::about(myMainw->settingsDialog->profileDialog, "Server reply to requesting profile", "OK " + reply->readAll());
540 myMainw->settingsDialog->profileDialog->setLabelInfoToUser("Profile saved to server");
546 * This slot function react to servers responce after request of picture has been sent.
548 void HttpClient::ackOfSendingPicture()
550 qDebug() << "__ackOfSendingPicture";
551 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
552 QNetworkReply::NetworkError errorcode;
553 errorcode = reply->error();
555 qDebug() << "errorcode:" << errorcode << reply->errorString();
557 if(myMainw->settingsDialog->profileDialog)
559 //QMessageBox::about(myMainw->settingsDialog->profileDialog, "Server reply to requesting picture",reply->errorString());
560 myMainw->settingsDialog->profileDialog->setLabelInfoToUser("Picture save to server - fail");
564 qDebug() << "errorcode:" << errorcode << reply->errorString();
566 if(myMainw->settingsDialog->profileDialog)
568 //QMessageBox::about(myMainw->settingsDialog->profileDialog, "Server reply to requesting picture", "OK " + reply->readAll());
569 myMainw->settingsDialog->profileDialog->setLabelInfoToUser("Picture saved to server");