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://www.speedfreak-app.com/users/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(),
64 qDebug() << "carmainwindow: regbuffer->data(): " << regbuffer->data();
66 currentDownload = netManager->post(request, ("xml=" + regbuffer->data()));
67 connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfRegistration()));
68 //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
71 if(myMainw->settingsDialog)
72 myMainw->settingsDialog->setLabelInfoToUser("Reguesting registration from server");
78 *@brief Sends result(s) to the server in xml format.
79 *Send authentication information in the header.
81 void HttpClient::sendResultXml(QString category, double result)
83 qDebug() << "_sendResultXml";
86 QBuffer *xmlbuffer = new QBuffer();
88 QUrl qurl("http://www.speedfreak-app.com/results/update/" + category);
90 qDebug() << qurl.toString();
91 QNetworkRequest request(qurl);
92 QNetworkReply *currentDownload;
94 xmlbuffer->open(QBuffer::ReadWrite);
95 myXmlwriter->writeResult(xmlbuffer, result);
96 qDebug() << "carmainwindow: xmlbuffer->data(): " << xmlbuffer->data();
98 QString credentials = myMainw->settingsDialog->getUserName() + ":" + myMainw->settingsDialog->getPassword();
99 credentials = "Basic " + credentials.toAscii().toBase64();
100 request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
102 currentDownload = netManager->post(request, ("xml=" + xmlbuffer->data()));
103 connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfResult()));
104 //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
107 if(myMainw->accstart->accRealTimeDialog->resultDialog)
108 myMainw->accstart->accRealTimeDialog->resultDialog->setLabelInfoToUser("Sending result to server");
114 * @brief Sends route to the server in xml format.
115 * Send authentication information in the header.
116 * @param QString filename
117 * @param int 1(send to server) or 0(no send)
118 * @todo Check destination URL.
120 void HttpClient::sendRouteXml(QString s, int i)
122 qDebug() << "_sendRouteXml";
124 QString filename = ".//speedfreak_route/route.xml";
128 qDebug() << "_rename xml";
129 filename = s + ".xml";
131 qDebug() << dir.rename(".//speedfreak_route/route.xml", filename);
136 qDebug() << "_send route";
137 QFile file(filename);
138 if (!file.open(QFile::ReadOnly))
140 qDebug() << "_sendRouteXml file.open() fail";
144 QUrl qurl("http://speedfreak-app.com/update/route");
145 qDebug() << qurl.toString();
146 QNetworkRequest request(qurl);
147 QNetworkReply *currentDownload;
149 QString credentials = myMainw->settingsDialog->getUserName() + ":" + myMainw->settingsDialog->getPassword();
150 credentials = "Basic " + credentials.toAscii().toBase64();
151 request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
153 currentDownload = netManager->post(request, ("xml=" + file.readAll()));
154 connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfRoute()));
155 //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
158 if(myMainw->routeSaveDialog->routeDialog)
159 myMainw->routeSaveDialog->routeDialog->setLabelInfoToUser("Sending route to server");
166 *@brief Request the Top10List of certain category from the server.
167 *Send authentication information in the header.
168 *@param Category of results.
169 *@param Limit, the number of results.
171 void HttpClient::requestTopList(QString category, QString limit)
173 qDebug() << "_requestTopList";
174 qDebug() << category;
176 QString urlBase = "http://www.speedfreak-app.com/results/list/";
177 QUrl qurl(urlBase + category + "/" + limit);
178 qDebug() << qurl.toString();
179 QNetworkRequest request(qurl);
180 QNetworkReply *currentDownload;
182 QString credentials = myMainw->settingsDialog->getUserName() + ":" + myMainw->settingsDialog->getPassword();
183 credentials = "Basic " + credentials.toAscii().toBase64();
184 request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
186 currentDownload = netManager->post(request, ("data=" ));
187 connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfToplist()));
188 //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
191 if(myMainw->topResultDialog)
192 myMainw->topResultDialog->setLabelInfoToUser("Reguesting top10 list from server");
196 *@brief Request categories list from the server.
197 *Send authentication information in the header.
199 void HttpClient::requestCategories()
201 qDebug() << "_requestCategories" ;
202 QUrl qurl("www.speedfreak-app.com/results/categories");
204 qDebug() << qurl.toString();
205 QNetworkRequest request(qurl);
206 QNetworkReply *currentDownload;
208 QString credentials = myMainw->settingsDialog->getUserName() + ":" + myMainw->settingsDialog->getPassword();
209 credentials = "Basic " + credentials.toAscii().toBase64();
210 request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
212 currentDownload = netManager->post(request, ("data=" ));
213 connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfCategories()));
214 //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
217 if(myMainw->topResultDialog)
218 myMainw->topResultDialog->setLabelInfoToUser("Reguesting categories from server");
222 *@brief Check that username and password exist on the server.
223 *Send authentication information in the header.
225 void HttpClient::checkLogin()
227 qDebug() << "_checkLogin";
229 QUrl qurl("http://www.speedfreak-app.com/users/login");
230 qDebug() << qurl.toString();
232 QNetworkRequest request(qurl);
233 QNetworkReply *currentDownload;
235 QString credentials = myMainw->settingsDialog->getUserName() + ":" + myMainw->settingsDialog->getPassword();
236 credentials = "Basic " + credentials.toAscii().toBase64();
237 request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
239 currentDownload = netManager->post(request, ("data=" ));
240 connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfLogin()));
241 //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
244 if(myMainw->settingsDialog)
245 myMainw->settingsDialog->setLabelInfoToUser("Checking login validity from server");
249 *@brief React to servers responce after result has been sent.
251 void HttpClient::ackOfResult()
253 qDebug() << "_ackOfResult";
256 if(myMainw->accstart->accRealTimeDialog->resultDialog)
257 myMainw->accstart->accRealTimeDialog->resultDialog->setLabelInfoToUser("");
259 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
261 QNetworkReply::NetworkError errorcode;
262 errorcode = reply->error();
264 qDebug() << "errorcode:" << errorcode << reply->errorString();
267 if(myMainw->accstart->accRealTimeDialog->resultDialog)
268 QMessageBox::about(myMainw->accstart->accRealTimeDialog->resultDialog, "Server reply to result sending ",reply->errorString());
269 if(myMainw->accstart->accRealTimeDialog->resultDialog)
270 myMainw->accstart->accRealTimeDialog->resultDialog->setLabelInfoToUser("Error");
271 if(myMainw->accstart->accRealTimeDialog->resultDialog)
272 myMainw->accstart->accRealTimeDialog->resultDialog->setSendServerButtonEnabled();
275 qDebug() << "errorcode:" << errorcode << reply->errorString();
278 if(myMainw->accstart->accRealTimeDialog->resultDialog)
279 QMessageBox::about(myMainw->accstart->accRealTimeDialog->resultDialog, "Server reply to result sending", "Result received " + reply->readAll());
280 if(myMainw->accstart->accRealTimeDialog->resultDialog)
281 myMainw->accstart->accRealTimeDialog->resultDialog->setLabelInfoToUser("Result received");
286 *@brief React to servers responce after route has been sent.
288 void HttpClient::ackOfRoute()
290 qDebug() << "_ackOfRoute";
292 if(myMainw->routeSaveDialog->routeDialog)
293 myMainw->routeSaveDialog->routeDialog->setLabelInfoToUser("");
295 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
297 QNetworkReply::NetworkError errorcode;
298 errorcode = reply->error();
300 qDebug() << "errorcode:" << errorcode << reply->errorString();
301 if(myMainw->routeSaveDialog->routeDialog)
302 QMessageBox::about(myMainw->routeSaveDialog->routeDialog, "Server reply to route sending ",reply->errorString());
303 if(myMainw->routeSaveDialog->routeDialog)
304 myMainw->routeSaveDialog->routeDialog->setSendServerButtonEnabled();
307 qDebug() << "errorcode:" << errorcode << reply->errorString();
308 if(myMainw->routeSaveDialog->routeDialog)
309 QMessageBox::about(myMainw->routeSaveDialog->routeDialog, "Server reply to route sending", "Route received " + reply->readAll());
314 *@brief React to servers responce after registration has been sent.
315 *@todo Implement consequencies of reply.
317 void HttpClient::ackOfRegistration()
319 qDebug() << "_ackOfRegistration";
321 if(myMainw->settingsDialog)
322 myMainw->settingsDialog->setLabelInfoToUser("");
324 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
326 QNetworkReply::NetworkError errorcode;
327 errorcode = reply->error();
329 qDebug() << "errorcode:" << errorcode << reply->errorString();
330 if(myMainw->settingsDialog)
331 QMessageBox::about(myMainw->settingsDialog, "Server reply to registration",reply->readAll());
334 qDebug() << "errorcode=0" << errorcode << reply->errorString();
335 if(myMainw->settingsDialog)
337 QMessageBox::about(myMainw->settingsDialog, "Server reply to registration", "User registration " + reply->readAll());
338 myMainw->settingsDialog->clearRegisterLineEdits();
344 *@brief React to servers responce after request for categories has been sent.
346 void HttpClient::ackOfCategories()
348 qDebug() << "_ackOfCategories";
350 if(myMainw->topResultDialog)
351 myMainw->topResultDialog->setLabelInfoToUser("");
353 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
354 myXmlreader->xmlReadCategories(reply);
356 QNetworkReply::NetworkError errorcode;
357 errorcode = reply->error();
359 qDebug() << "errorcode:" << errorcode << reply->errorString();
360 //QMessageBox::about(myMainw->topResultDialog, "Server reply to requesting categories",reply->errorString());
361 if(myMainw->topResultDialog)
362 myMainw->topResultDialog->setLabelInfoToUser("You're not logged! Please register or log in.");
365 qDebug() << "errorcode:" << errorcode << reply->errorString();
366 //QMessageBox::about(myMainw->topResultDialog, "Server reply to requesting categories ", "OK");
367 if(myMainw->topResultDialog)
368 myMainw->topResultDialog->setLabelInfoToUser("");
373 *@brief React to servers responce after request of TopList in certain category has been sent.
375 void HttpClient::ackOfLogin()
377 qDebug() << "_ackOffLogin";
379 if(myMainw->settingsDialog)
380 myMainw->settingsDialog->setLabelInfoToUser("");
382 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
384 QNetworkReply::NetworkError errorcode;
385 errorcode = reply->error();
388 qDebug() << "errorcode:" << errorcode << reply->errorString();
389 if(myMainw->settingsDialog)
391 QMessageBox::about(myMainw->settingsDialog, "Server does not recognize your username. Please registrate.",reply->errorString());
392 myMainw->settingsDialog->usernameOk(false);
397 qDebug() << "errorcode:" << errorcode << reply->errorString();
398 if(myMainw->settingsDialog)
399 QMessageBox::about(myMainw->settingsDialog, "Server reply to login", "User login " + reply->readAll());
400 // here signal emit to mainwindow for username setting to main panel
402 if( myMainw->settingsDialog)
404 myMainw->settingsDialog->usernameOk(true);
405 myMainw->settingsDialog->close();
411 *@brief Reports errors, when server has sent error signal.
413 void HttpClient::errorFromServer(QNetworkReply::NetworkError errorcode)
415 qDebug() << "_errorFromServer";
416 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
419 qDebug() << "errorcode:" << errorcode;
420 //Note that errors are already reported on other each functions for server communication
421 //QMessageBox::about(myMainw, "Server reported an error", reply->errorString());
424 qDebug() << "errorcode:" << errorcode << reply->errorString();
425 qDebug() << reply->readAll();
430 *@brief React to servers responce after request of TopList in certain category has been sent.
432 void HttpClient::ackOfToplist()
434 qDebug() << "_ackOfToplist";
435 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
436 myXmlreader->xmlReadTop10Results(reply,myMainw->settingsDialog->getUserName());
438 QNetworkReply::NetworkError errorcode;
439 errorcode = reply->error();
441 qDebug() << "errorcode:" << errorcode << reply->errorString();
443 if(myMainw->topResultDialog)
445 //QMessageBox::about(myMainw->topResultDialog, "Server reply to requesting top 10 list",reply->errorString());
446 myMainw->topResultDialog->setLabelInfoToUser("No results ;(");
450 qDebug() << "errorcode:" << errorcode << reply->errorString();
452 if(myMainw->topResultDialog)
454 //QMessageBox::about(myMainw->topResultDialog, "Server reply to requesting top 10 list", "OK " + reply->readAll());
455 myMainw->topResultDialog->setLabelInfoToUser("");
461 * This function sends profile to the server in xml format.
462 * Send authentication information in the header.
464 void HttpClient::sendProfileXml()
466 qDebug() << "_sendProfileXml";
468 QString userName = myMainw->settingsDialog->getUserName();
469 QString filename = userName + "_profile.xml";
470 QFile file(filename);
471 if (!file.open(QFile::ReadWrite | QFile::Text))
473 qDebug() << "_xmlWrite fail";
476 myXmlwriter->writeProfileXmlFile(&file, userName,
477 myMainw->settingsDialog->profileDialog->getManufacturer(),
478 myMainw->settingsDialog->profileDialog->getType(),
479 myMainw->settingsDialog->profileDialog->getModel(),
480 myMainw->settingsDialog->profileDialog->getDescription(),
481 myMainw->settingsDialog->profileDialog->getPicture());
484 if(myMainw->settingsDialog->profileDialog)
485 myMainw->settingsDialog->profileDialog->setLabelInfoToUser("Profile saved to phone");
487 // Send xml to server
488 /*QUrl qurl("http://speedfreak-app.com/api/profile");
489 QNetworkRequest request(qurl);
490 qDebug() << qurl.toString();
491 QNetworkReply *currentDownload;
493 QString credentials = myMainw->settingsDialog->getUserName() + ":" + myMainw->settingsDialog->getPassword();
494 credentials = "Basic " + credentials.toAscii().toBase64();
495 request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
497 currentDownload = netManager->post(request, ("xml=" + file.readAll()));
498 bool error = connect(currentDownload, SIGNAL(finished()), this, SLOT(ackOfProfile()));*/
502 // Send picture to server
503 /*if(myMainw->settingsDialog->profileDialog->getPicture() != "" && error == false)
505 QFile pictureFile( myMainw->settingsDialog->profileDialog->getPicture() );
506 if (!pictureFile.open(QIODevice::ReadOnly))
508 qDebug() << "__picture read fail";
511 currentDownload = netManager->post(request, pictureFile.readAll());
512 connect(currentDownload, SIGNAL(finished()), this, SLOT(ackOfSendingPicture()));
518 * This slot function react to servers responce after request of profile has been sent.
520 bool HttpClient::ackOfProfile()
522 qDebug() << "__ackOfProfile";
523 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
524 QNetworkReply::NetworkError errorcode;
525 errorcode = reply->error();
527 qDebug() << "errorcode:" << errorcode << reply->errorString();
529 if(myMainw->settingsDialog->profileDialog)
531 //QMessageBox::about(myMainw->settingsDialog->profileDialog, "Server reply to requesting profile",reply->errorString());
532 myMainw->settingsDialog->profileDialog->setLabelInfoToUser("Profile save to server - fail");
537 qDebug() << "errorcode:" << errorcode << reply->errorString();
539 if(myMainw->settingsDialog->profileDialog)
541 //QMessageBox::about(myMainw->settingsDialog->profileDialog, "Server reply to requesting profile", "OK " + reply->readAll());
542 myMainw->settingsDialog->profileDialog->setLabelInfoToUser("Profile saved to server");
548 * This slot function react to servers responce after request of picture has been sent.
550 void HttpClient::ackOfSendingPicture()
552 qDebug() << "__ackOfSendingPicture";
553 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
554 QNetworkReply::NetworkError errorcode;
555 errorcode = reply->error();
557 qDebug() << "errorcode:" << errorcode << reply->errorString();
559 if(myMainw->settingsDialog->profileDialog)
561 //QMessageBox::about(myMainw->settingsDialog->profileDialog, "Server reply to requesting picture",reply->errorString());
562 myMainw->settingsDialog->profileDialog->setLabelInfoToUser("Picture save to server - fail");
566 qDebug() << "errorcode:" << errorcode << reply->errorString();
568 if(myMainw->settingsDialog->profileDialog)
570 //QMessageBox::about(myMainw->settingsDialog->profileDialog, "Server reply to requesting picture", "OK " + reply->readAll());
571 myMainw->settingsDialog->profileDialog->setLabelInfoToUser("Picture saved to server");