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 s, int i)
119 qDebug() << "_sendRouteXml";
121 QString filename = ".//speedfreak_route/route.xml";
125 qDebug() << "_rename xml";
126 filename = s + ".xml";
128 qDebug() << dir.rename(".//speedfreak_route/route.xml", filename);
133 qDebug() << "_send route";
134 QFile file(filename);
135 if (!file.open(QFile::ReadOnly))
137 qDebug() << "_sendRouteXml file.open() fail";
141 QUrl qurl("http://api.speedfreak-app.com/api/update/route");
142 qDebug() << qurl.toString();
143 QNetworkRequest request(qurl);
144 QNetworkReply *currentDownload;
146 QString credentials = myMainw->settingsDialog->getUserName() + ":" + myMainw->settingsDialog->getPassword();
147 credentials = "Basic " + credentials.toAscii().toBase64();
148 request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
150 currentDownload = netManager->post(request, ("xml=" + file.readAll()));
151 connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfRoute()));
152 //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
155 if(myMainw->routeSaveDialog->routeDialog)
156 myMainw->routeSaveDialog->routeDialog->setLabelInfoToUser("Sending route to server");
163 *@brief Request the Top10List of certain category from the server.
164 *Send authentication information in the header.
165 *@param Category of results.
166 *@param Limit, the number of results.
168 void HttpClient::requestTopList(QString category, QString limit)
170 qDebug() << "_requestTopList";
171 qDebug() << category;
173 QString urlBase = "http://api.speedfreak-app.com/api/results/";
174 QUrl qurl(urlBase + category + "/" + limit);
175 qDebug() << qurl.toString();
176 QNetworkRequest request(qurl);
177 QNetworkReply *currentDownload;
179 QString credentials = myMainw->settingsDialog->getUserName() + ":" + myMainw->settingsDialog->getPassword();
180 credentials = "Basic " + credentials.toAscii().toBase64();
181 request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
183 currentDownload = netManager->post(request, ("data=" ));
184 connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfToplist()));
185 //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
188 if(myMainw->topResultDialog)
189 myMainw->topResultDialog->setLabelInfoToUser("Reguesting top10 list from server");
193 *@brief Request categories list from the server.
194 *Send authentication information in the header.
196 void HttpClient::requestCategories()
198 qDebug() << "_requestCategories" ;
200 QUrl qurl("http://api.speedfreak-app.com/api/categories/");
201 qDebug() << qurl.toString();
202 QNetworkRequest request(qurl);
203 QNetworkReply *currentDownload;
205 QString credentials = myMainw->settingsDialog->getUserName() + ":" + myMainw->settingsDialog->getPassword();
206 credentials = "Basic " + credentials.toAscii().toBase64();
207 request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
209 currentDownload = netManager->post(request, ("data=" ));
210 connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfCategories()));
211 //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
214 if(myMainw->topResultDialog)
215 myMainw->topResultDialog->setLabelInfoToUser("Reguesting categories from server");
219 *@brief Check that username and password exist on the server.
220 *Send authentication information in the header.
222 void HttpClient::checkLogin()
224 qDebug() << "_checkLogin";
226 QUrl qurl("http://api.speedfreak-app.com/api/login/");
227 qDebug() << qurl.toString();
228 QNetworkRequest request(qurl);
229 QNetworkReply *currentDownload;
231 QString credentials = myMainw->settingsDialog->getUserName() + ":" + myMainw->settingsDialog->getPassword();
232 credentials = "Basic " + credentials.toAscii().toBase64();
233 request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
235 currentDownload = netManager->post(request, ("data=" ));
236 connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfLogin()));
237 //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
240 if(myMainw->settingsDialog)
241 myMainw->settingsDialog->setLabelInfoToUser("Checking login validity from server");
245 *@brief React to servers responce after result has been sent.
247 void HttpClient::ackOfResult()
249 qDebug() << "_ackOfResult";
252 if(myMainw->accstart->accRealTimeDialog->resultDialog)
253 myMainw->accstart->accRealTimeDialog->resultDialog->setLabelInfoToUser("");
255 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
257 QNetworkReply::NetworkError errorcode;
258 errorcode = reply->error();
260 qDebug() << "errorcode:" << errorcode << reply->errorString();
263 if(myMainw->accstart->accRealTimeDialog->resultDialog)
264 QMessageBox::about(myMainw->accstart->accRealTimeDialog->resultDialog, "Server reply to result sending ",reply->errorString());
265 if(myMainw->accstart->accRealTimeDialog->resultDialog)
266 myMainw->accstart->accRealTimeDialog->resultDialog->setLabelInfoToUser("Error");
267 if(myMainw->accstart->accRealTimeDialog->resultDialog)
268 myMainw->accstart->accRealTimeDialog->resultDialog->setSendServerButtonEnabled();
271 qDebug() << "errorcode:" << errorcode << reply->errorString();
274 if(myMainw->accstart->accRealTimeDialog->resultDialog)
275 QMessageBox::about(myMainw->accstart->accRealTimeDialog->resultDialog, "Server reply to result sending", "Result received " + reply->readAll());
276 if(myMainw->accstart->accRealTimeDialog->resultDialog)
277 myMainw->accstart->accRealTimeDialog->resultDialog->setLabelInfoToUser("Result received");
282 *@brief React to servers responce after route has been sent.
284 void HttpClient::ackOfRoute()
286 qDebug() << "_ackOfRoute";
288 if(myMainw->routeSaveDialog->routeDialog)
289 myMainw->routeSaveDialog->routeDialog->setLabelInfoToUser("");
291 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
293 QNetworkReply::NetworkError errorcode;
294 errorcode = reply->error();
296 qDebug() << "errorcode:" << errorcode << reply->errorString();
297 if(myMainw->routeSaveDialog->routeDialog)
298 QMessageBox::about(myMainw->routeSaveDialog->routeDialog, "Server reply to route sending ",reply->errorString());
299 if(myMainw->routeSaveDialog->routeDialog)
300 myMainw->routeSaveDialog->routeDialog->setSendServerButtonEnabled();
303 qDebug() << "errorcode:" << errorcode << reply->errorString();
304 if(myMainw->routeSaveDialog->routeDialog)
305 QMessageBox::about(myMainw->routeSaveDialog->routeDialog, "Server reply to route sending", "Route received " + reply->readAll());
310 *@brief React to servers responce after registration has been sent.
311 *@todo Implement consequencies of reply.
313 void HttpClient::ackOfRegistration()
315 qDebug() << "_ackOfRegistration";
317 if(myMainw->settingsDialog)
318 myMainw->settingsDialog->setLabelInfoToUser("");
320 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
322 QNetworkReply::NetworkError errorcode;
323 errorcode = reply->error();
325 qDebug() << "errorcode:" << errorcode << reply->errorString();
326 if(myMainw->settingsDialog)
327 QMessageBox::about(myMainw->settingsDialog, "Server reply to registration",reply->readAll());
330 qDebug() << "errorcode=0" << errorcode << reply->errorString();
331 if(myMainw->settingsDialog)
333 QMessageBox::about(myMainw->settingsDialog, "Server reply to registration", "User registration " + reply->readAll());
334 myMainw->settingsDialog->clearRegisterLineEdits();
340 *@brief React to servers responce after request for categories has been sent.
342 void HttpClient::ackOfCategories()
344 qDebug() << "_ackOfCategories";
346 if(myMainw->topResultDialog)
347 myMainw->topResultDialog->setLabelInfoToUser("");
349 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
350 myXmlreader->xmlReadCategories(reply);
352 QNetworkReply::NetworkError errorcode;
353 errorcode = reply->error();
355 qDebug() << "errorcode:" << errorcode << reply->errorString();
356 //QMessageBox::about(myMainw->topResultDialog, "Server reply to requesting categories",reply->errorString());
357 if(myMainw->topResultDialog)
358 myMainw->topResultDialog->setLabelInfoToUser("You're not logged! Please register or log in.");
361 qDebug() << "errorcode:" << errorcode << reply->errorString();
362 //QMessageBox::about(myMainw->topResultDialog, "Server reply to requesting categories ", "OK");
363 if(myMainw->topResultDialog)
364 myMainw->topResultDialog->setLabelInfoToUser("");
369 *@brief React to servers responce after request of TopList in certain category has been sent.
371 void HttpClient::ackOfLogin()
373 qDebug() << "_ackOffLogin";
375 if(myMainw->settingsDialog)
376 myMainw->settingsDialog->setLabelInfoToUser("");
378 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
380 QNetworkReply::NetworkError errorcode;
381 errorcode = reply->error();
384 qDebug() << "errorcode:" << errorcode << reply->errorString();
385 if(myMainw->settingsDialog)
387 QMessageBox::about(myMainw->settingsDialog, "Server does not recognize your username. Please registrate.",reply->errorString());
388 myMainw->settingsDialog->usernameOk(false);
393 qDebug() << "errorcode:" << errorcode << reply->errorString();
394 if(myMainw->settingsDialog)
395 QMessageBox::about(myMainw->settingsDialog, "Server reply to login", "User login " + reply->readAll());
396 // here signal emit to mainwindow for username setting to main panel
398 if( myMainw->settingsDialog)
400 myMainw->settingsDialog->usernameOk(true);
401 myMainw->settingsDialog->close();
407 *@brief Reports errors, when server has sent error signal.
409 void HttpClient::errorFromServer(QNetworkReply::NetworkError errorcode)
411 qDebug() << "_errorFromServer";
412 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
415 qDebug() << "errorcode:" << errorcode;
416 //Note that errors are already reported on other each functions for server communication
417 //QMessageBox::about(myMainw, "Server reported an error", reply->errorString());
420 qDebug() << "errorcode:" << errorcode << reply->errorString();
421 qDebug() << reply->readAll();
426 *@brief React to servers responce after request of TopList in certain category has been sent.
428 void HttpClient::ackOfToplist()
430 qDebug() << "_ackOfToplist";
431 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
432 myXmlreader->xmlReadTop10Results(reply,myMainw->settingsDialog->getUserName());
434 QNetworkReply::NetworkError errorcode;
435 errorcode = reply->error();
437 qDebug() << "errorcode:" << errorcode << reply->errorString();
439 if(myMainw->topResultDialog)
441 //QMessageBox::about(myMainw->topResultDialog, "Server reply to requesting top 10 list",reply->errorString());
442 myMainw->topResultDialog->setLabelInfoToUser("No results ;(");
446 qDebug() << "errorcode:" << errorcode << reply->errorString();
448 if(myMainw->topResultDialog)
450 //QMessageBox::about(myMainw->topResultDialog, "Server reply to requesting top 10 list", "OK " + reply->readAll());
451 myMainw->topResultDialog->setLabelInfoToUser("");
457 * This function sends profile to the server in xml format.
458 * Send authentication information in the header.
460 void HttpClient::sendProfileXml()
462 qDebug() << "_sendProfileXml";
464 QString userName = myMainw->settingsDialog->getUserName();
465 QString filename = userName + "_profile.xml";
466 QFile file(filename);
467 if (!file.open(QFile::ReadWrite | QFile::Text))
469 qDebug() << "_xmlWrite fail";
472 myXmlwriter->writeProfileXmlFile(&file, userName,
473 myMainw->settingsDialog->profileDialog->getManufacturer(),
474 myMainw->settingsDialog->profileDialog->getType(),
475 myMainw->settingsDialog->profileDialog->getModel(),
476 myMainw->settingsDialog->profileDialog->getDescription(),
477 myMainw->settingsDialog->profileDialog->getPicture());
480 if(myMainw->settingsDialog->profileDialog)
481 myMainw->settingsDialog->profileDialog->setLabelInfoToUser("Profile saved to phone");
483 // Send xml to server
484 /*QUrl qurl("http://api.speedfreak-app.com/api/profile");
485 QNetworkRequest request(qurl);
486 qDebug() << qurl.toString();
487 QNetworkReply *currentDownload;
489 QString credentials = myMainw->settingsDialog->getUserName() + ":" + myMainw->settingsDialog->getPassword();
490 credentials = "Basic " + credentials.toAscii().toBase64();
491 request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
493 currentDownload = netManager->post(request, ("xml=" + file.readAll()));
494 bool error = connect(currentDownload, SIGNAL(finished()), this, SLOT(ackOfProfile()));*/
498 // Send picture to server
499 /*if(myMainw->settingsDialog->profileDialog->getPicture() != "" && error == false)
501 QFile pictureFile( myMainw->settingsDialog->profileDialog->getPicture() );
502 if (!pictureFile.open(QIODevice::ReadOnly))
504 qDebug() << "__picture read fail";
507 currentDownload = netManager->post(request, pictureFile.readAll());
508 connect(currentDownload, SIGNAL(finished()), this, SLOT(ackOfSendingPicture()));
514 * This slot function react to servers responce after request of profile has been sent.
516 bool HttpClient::ackOfProfile()
518 qDebug() << "__ackOfProfile";
519 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
520 QNetworkReply::NetworkError errorcode;
521 errorcode = reply->error();
523 qDebug() << "errorcode:" << errorcode << reply->errorString();
525 if(myMainw->settingsDialog->profileDialog)
527 //QMessageBox::about(myMainw->settingsDialog->profileDialog, "Server reply to requesting profile",reply->errorString());
528 myMainw->settingsDialog->profileDialog->setLabelInfoToUser("Profile save to server - fail");
533 qDebug() << "errorcode:" << errorcode << reply->errorString();
535 if(myMainw->settingsDialog->profileDialog)
537 //QMessageBox::about(myMainw->settingsDialog->profileDialog, "Server reply to requesting profile", "OK " + reply->readAll());
538 myMainw->settingsDialog->profileDialog->setLabelInfoToUser("Profile saved to server");
544 * This slot function react to servers responce after request of picture has been sent.
546 void HttpClient::ackOfSendingPicture()
548 qDebug() << "__ackOfSendingPicture";
549 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
550 QNetworkReply::NetworkError errorcode;
551 errorcode = reply->error();
553 qDebug() << "errorcode:" << errorcode << reply->errorString();
555 if(myMainw->settingsDialog->profileDialog)
557 //QMessageBox::about(myMainw->settingsDialog->profileDialog, "Server reply to requesting picture",reply->errorString());
558 myMainw->settingsDialog->profileDialog->setLabelInfoToUser("Picture save to server - fail");
562 qDebug() << "errorcode:" << errorcode << reply->errorString();
564 if(myMainw->settingsDialog->profileDialog)
566 //QMessageBox::about(myMainw->settingsDialog->profileDialog, "Server reply to requesting picture", "OK " + reply->readAll());
567 myMainw->settingsDialog->profileDialog->setLabelInfoToUser("Picture saved to server");