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>
13 #include "httpclient.h"
14 #include "mainwindow.h"
17 *@brief Constructor, connects object to GUI
18 *@param Pointer to carmainwindow, which is temporarily used during development
20 HttpClient::HttpClient(MainWindow *myCarw)
22 qDebug() << "__HttpClient";
24 netManager = new QNetworkAccessManager();
25 myXmlwriter = new XmlWriter();
26 myXmlreader = new XmlReader();
32 HttpClient::~HttpClient()
34 qDebug() << "__~HttpClient" ;
43 *@brief Sends registration information to the server in xml format.
44 *Reads user name, password and emaol address from resuldialogs internal variables.
46 void HttpClient::requestRegistration()
48 qDebug() << "_requestRegistration" ;
49 qDebug() << myMainw->settingsDialog->getRegUserName() << "+" << myMainw->settingsDialog->getRegPassword() << "+" << myMainw->settingsDialog->getRegEmail();
51 QBuffer *regbuffer = new QBuffer();
52 QUrl qurl("http://api.speedfreak-app.com/api/register");
53 QNetworkRequest request(qurl);
54 qDebug() << qurl.toString();
55 QNetworkReply *currentDownload;
57 regbuffer->open(QBuffer::ReadWrite);
58 myXmlwriter->writeRegistering(regbuffer,
59 myMainw->settingsDialog->getRegUserName(),
60 myMainw->settingsDialog->getRegPassword(),
61 myMainw->settingsDialog->getRegEmail());
62 qDebug() << "carmainwindow: regbuffer->data(): " << regbuffer->data();
64 currentDownload = netManager->post(request, ("xml=" + regbuffer->data()));
65 connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfRegistration()));
66 //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
69 if(myMainw->settingsDialog)
70 myMainw->settingsDialog->setLabelInfoToUser("Reguesting registration from server");
76 *@brief Sends result(s) to the server in xml format.
77 *Send authentication information in the header.
79 void HttpClient::sendResultXml(QString category, double result)
81 qDebug() << "_sendResultXml";
83 QBuffer *xmlbuffer = new QBuffer();
85 QUrl qurl("http://api.speedfreak-app.com/api/update/" + category);
86 qDebug() << qurl.toString();
87 QNetworkRequest request(qurl);
88 QNetworkReply *currentDownload;
90 xmlbuffer->open(QBuffer::ReadWrite);
91 myXmlwriter->writeResult(xmlbuffer, result);
92 qDebug() << "carmainwindow: xmlbuffer->data(): " << xmlbuffer->data();
94 QString credentials = myMainw->settingsDialog->getUserName() + ":" + myMainw->settingsDialog->getPassword();
95 credentials = "Basic " + credentials.toAscii().toBase64();
96 request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
98 currentDownload = netManager->post(request, ("xml=" + xmlbuffer->data()));
99 connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfResult()));
100 //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
103 if(myMainw->accstart->accRealTimeDialog->resultDialog)
104 myMainw->accstart->accRealTimeDialog->resultDialog->setLabelInfoToUser("Sending result to server");
110 *@brief Sends route to the server in xml format.
111 *Send authentication information in the header.
112 *@todo Check destination URL.
114 void HttpClient::sendRouteXml()
116 qDebug() << "_sendRouteXml";
118 QString filename = "route.xml";
119 QFile file(filename);
120 if (!file.open(QFile::ReadOnly)) {
121 qDebug() << "_sendRouteXml file.open() fail";
125 QUrl qurl("http://api.speedfreak-app.com/api/update/route");
126 qDebug() << qurl.toString();
127 QNetworkRequest request(qurl);
128 QNetworkReply *currentDownload;
130 QString credentials = myMainw->settingsDialog->getUserName() + ":" + myMainw->settingsDialog->getPassword();
131 credentials = "Basic " + credentials.toAscii().toBase64();
132 request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
134 currentDownload = netManager->post(request, ("xml=" + file.readAll()));
135 connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfRoute()));
136 //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
139 if(myMainw->routeSaveDialog->routeDialog)
140 myMainw->routeSaveDialog->routeDialog->setLabelInfoToUser("Sending route to server");
146 *@brief Request the Top10List of certain category from the server.
147 *Send authentication information in the header.
148 *@param Category of results.
149 *@param Limit, the number of results.
151 void HttpClient::requestTopList(QString category, QString limit)
153 qDebug() << "_requestTopList";
154 qDebug() << category;
156 QString urlBase = "http://api.speedfreak-app.com/api/results/";
157 QUrl qurl(urlBase + category + "/" + limit);
158 qDebug() << qurl.toString();
159 QNetworkRequest request(qurl);
160 QNetworkReply *currentDownload;
162 QString credentials = myMainw->settingsDialog->getUserName() + ":" + myMainw->settingsDialog->getPassword();
163 credentials = "Basic " + credentials.toAscii().toBase64();
164 request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
166 currentDownload = netManager->post(request, ("data=" ));
167 connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfToplist()));
168 //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
171 if(myMainw->topResultDialog)
172 myMainw->topResultDialog->setLabelInfoToUser("Reguesting top10 list from server");
176 *@brief Request categories list from the server.
177 *Send authentication information in the header.
179 void HttpClient::requestCategories()
181 qDebug() << "_requestCategories" ;
183 QUrl qurl("http://api.speedfreak-app.com/api/categories/");
184 qDebug() << qurl.toString();
185 QNetworkRequest request(qurl);
186 QNetworkReply *currentDownload;
188 QString credentials = myMainw->settingsDialog->getUserName() + ":" + myMainw->settingsDialog->getPassword();
189 credentials = "Basic " + credentials.toAscii().toBase64();
190 request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
192 currentDownload = netManager->post(request, ("data=" ));
193 connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfCategories()));
194 //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
197 if(myMainw->topResultDialog)
198 myMainw->topResultDialog->setLabelInfoToUser("Reguesting categories from server");
202 *@brief Check that username and password exist on the server.
203 *Send authentication information in the header.
205 void HttpClient::checkLogin()
207 qDebug() << "_checkLogin";
209 QUrl qurl("http://api.speedfreak-app.com/api/login/");
210 qDebug() << qurl.toString();
211 QNetworkRequest request(qurl);
212 QNetworkReply *currentDownload;
214 QString credentials = myMainw->settingsDialog->getUserName() + ":" + myMainw->settingsDialog->getPassword();
215 credentials = "Basic " + credentials.toAscii().toBase64();
216 request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
218 currentDownload = netManager->post(request, ("data=" ));
219 connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfLogin()));
220 //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
223 if(myMainw->settingsDialog)
224 myMainw->settingsDialog->setLabelInfoToUser("Checking login validity from server");
228 *@brief React to servers responce after result has been sent.
230 void HttpClient::ackOfResult()
232 qDebug() << "_ackOfResult";
235 if(myMainw->accstart->accRealTimeDialog->resultDialog)
236 myMainw->accstart->accRealTimeDialog->resultDialog->setLabelInfoToUser("");
238 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
240 QNetworkReply::NetworkError errorcode;
241 errorcode = reply->error();
243 qDebug() << "errorcode:" << errorcode << reply->errorString();
246 if(myMainw->accstart->accRealTimeDialog->resultDialog)
247 QMessageBox::about(myMainw->accstart->accRealTimeDialog->resultDialog, "Server reply to result sending ",reply->errorString());
248 if(myMainw->accstart->accRealTimeDialog->resultDialog)
249 myMainw->accstart->accRealTimeDialog->resultDialog->setLabelInfoToUser("Error");
250 if(myMainw->accstart->accRealTimeDialog->resultDialog)
251 myMainw->accstart->accRealTimeDialog->resultDialog->setSendServerButtonEnabled();
254 qDebug() << "errorcode:" << errorcode << reply->errorString();
257 if(myMainw->accstart->accRealTimeDialog->resultDialog)
258 QMessageBox::about(myMainw->accstart->accRealTimeDialog->resultDialog, "Server reply to result sending", "Result received " + reply->readAll());
259 if(myMainw->accstart->accRealTimeDialog->resultDialog)
260 myMainw->accstart->accRealTimeDialog->resultDialog->setLabelInfoToUser("Result received");
265 *@brief React to servers responce after route has been sent.
267 void HttpClient::ackOfRoute()
269 qDebug() << "_ackOfRoute";
271 if(myMainw->routeSaveDialog->routeDialog)
272 myMainw->routeSaveDialog->routeDialog->setLabelInfoToUser("");
274 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
276 QNetworkReply::NetworkError errorcode;
277 errorcode = reply->error();
279 qDebug() << "errorcode:" << errorcode << reply->errorString();
280 if(myMainw->routeSaveDialog->routeDialog)
281 QMessageBox::about(myMainw->routeSaveDialog->routeDialog, "Server reply to route sending ",reply->errorString());
282 if(myMainw->routeSaveDialog->routeDialog)
283 myMainw->routeSaveDialog->routeDialog->setSendServerButtonEnabled();
286 qDebug() << "errorcode:" << errorcode << reply->errorString();
287 if(myMainw->routeSaveDialog->routeDialog)
288 QMessageBox::about(myMainw->routeSaveDialog->routeDialog, "Server reply to route sending", "Route received " + reply->readAll());
293 *@brief React to servers responce after registration has been sent.
294 *@todo Implement consequencies of reply.
296 void HttpClient::ackOfRegistration()
298 qDebug() << "_ackOfRegistration";
300 if(myMainw->settingsDialog)
301 myMainw->settingsDialog->setLabelInfoToUser("");
303 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
305 QNetworkReply::NetworkError errorcode;
306 errorcode = reply->error();
308 qDebug() << "errorcode:" << errorcode << reply->errorString();
309 if(myMainw->settingsDialog)
310 QMessageBox::about(myMainw->settingsDialog, "Server reply to registration",reply->readAll());
313 qDebug() << "errorcode=0" << errorcode << reply->errorString();
314 if(myMainw->settingsDialog)
316 QMessageBox::about(myMainw->settingsDialog, "Server reply to registration", "User registration " + reply->readAll());
317 myMainw->settingsDialog->clearRegisterLineEdits();
323 *@brief React to servers responce after request for categories has been sent.
325 void HttpClient::ackOfCategories()
327 qDebug() << "_ackOfCategories";
329 if(myMainw->topResultDialog)
330 myMainw->topResultDialog->setLabelInfoToUser("");
332 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
333 myXmlreader->xmlReadCategories(reply);
335 QNetworkReply::NetworkError errorcode;
336 errorcode = reply->error();
338 qDebug() << "errorcode:" << errorcode << reply->errorString();
339 //QMessageBox::about(myMainw->topResultDialog, "Server reply to requesting categories",reply->errorString());
340 if(myMainw->topResultDialog)
341 myMainw->topResultDialog->setLabelInfoToUser("You're not logged! Please register or log in.");
344 qDebug() << "errorcode:" << errorcode << reply->errorString();
345 //QMessageBox::about(myMainw->topResultDialog, "Server reply to requesting categories ", "OK");
346 if(myMainw->topResultDialog)
347 myMainw->topResultDialog->setLabelInfoToUser("");
352 *@brief React to servers responce after request of TopList in certain category has been sent.
354 void HttpClient::ackOfLogin()
356 qDebug() << "_ackOffLogin";
358 if(myMainw->settingsDialog)
359 myMainw->settingsDialog->setLabelInfoToUser("");
361 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
363 QNetworkReply::NetworkError errorcode;
364 errorcode = reply->error();
367 qDebug() << "errorcode:" << errorcode << reply->errorString();
368 if(myMainw->settingsDialog)
370 QMessageBox::about(myMainw->settingsDialog, "Server does not recognize your username. Please registrate.",reply->errorString());
371 myMainw->settingsDialog->usernameOk(false);
376 qDebug() << "errorcode:" << errorcode << reply->errorString();
377 if(myMainw->settingsDialog)
378 QMessageBox::about(myMainw->settingsDialog, "Server reply to login", "User login " + reply->readAll());
379 // here signal emit to mainwindow for username setting to main panel
381 if( myMainw->settingsDialog)
383 myMainw->settingsDialog->usernameOk(true);
384 myMainw->settingsDialog->close();
390 *@brief Reports errors, when server has sent error signal.
392 void HttpClient::errorFromServer(QNetworkReply::NetworkError errorcode)
394 qDebug() << "_errorFromServer";
395 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
398 qDebug() << "errorcode:" << errorcode;
399 //Note that errors are already reported on other each functions for server communication
400 //QMessageBox::about(myMainw, "Server reported an error", reply->errorString());
403 qDebug() << "errorcode:" << errorcode << reply->errorString();
404 qDebug() << reply->readAll();
409 *@brief React to servers responce after request of TopList in certain category has been sent.
411 void HttpClient::ackOfToplist()
413 qDebug() << "_ackOfToplist";
414 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
415 myXmlreader->xmlReadTop10Results(reply,myMainw->settingsDialog->getUserName());
417 QNetworkReply::NetworkError errorcode;
418 errorcode = reply->error();
420 qDebug() << "errorcode:" << errorcode << reply->errorString();
422 if(myMainw->topResultDialog)
424 //QMessageBox::about(myMainw->topResultDialog, "Server reply to requesting top 10 list",reply->errorString());
425 myMainw->topResultDialog->setLabelInfoToUser("No results ;(");
429 qDebug() << "errorcode:" << errorcode << reply->errorString();
431 if(myMainw->topResultDialog)
433 //QMessageBox::about(myMainw->topResultDialog, "Server reply to requesting top 10 list", "OK " + reply->readAll());
434 myMainw->topResultDialog->setLabelInfoToUser("");
440 * This function sends profile to the server in xml format.
441 * Send authentication information in the header.
443 void HttpClient::sendProfileXml()
445 qDebug() << "_sendProfileXml";
447 QString userName = myMainw->settingsDialog->getUserName();
448 QString filename = userName + "_profile.xml";
449 QFile file(filename);
450 if (!file.open(QFile::ReadWrite | QFile::Text))
452 qDebug() << "_xmlWrite fail";
455 myXmlwriter->writeProfileXmlFile(&file, userName,
456 myMainw->settingsDialog->profileDialog->getManufacturer(),
457 myMainw->settingsDialog->profileDialog->getType(),
458 myMainw->settingsDialog->profileDialog->getModel(),
459 myMainw->settingsDialog->profileDialog->getDescription(),
460 myMainw->settingsDialog->profileDialog->getPicture());
463 if(myMainw->settingsDialog->profileDialog)
464 myMainw->settingsDialog->profileDialog->setLabelInfoToUser("Profile saved to phone");
466 // Send xml to server
467 /*QUrl qurl("http://api.speedfreak-app.com/api/profile");
468 QNetworkRequest request(qurl);
469 qDebug() << qurl.toString();
470 QNetworkReply *currentDownload;
472 QString credentials = myMainw->settingsDialog->getUserName() + ":" + myMainw->settingsDialog->getPassword();
473 credentials = "Basic " + credentials.toAscii().toBase64();
474 request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
476 currentDownload = netManager->post(request, ("xml=" + file.readAll()));
477 bool error = connect(currentDownload, SIGNAL(finished()), this, SLOT(ackOfProfile()));*/
481 // Send picture to server
482 /*if(myMainw->settingsDialog->profileDialog->getPicture() != "" && error == false)
484 QFile pictureFile( myMainw->settingsDialog->profileDialog->getPicture() );
485 if (!pictureFile.open(QIODevice::ReadOnly))
487 qDebug() << "__picture read fail";
490 currentDownload = netManager->post(request, pictureFile.readAll());
491 connect(currentDownload, SIGNAL(finished()), this, SLOT(ackOfSendingPicture()));
497 * This slot function react to servers responce after request of profile has been sent.
499 bool HttpClient::ackOfProfile()
501 qDebug() << "__ackOfProfile";
502 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
503 QNetworkReply::NetworkError errorcode;
504 errorcode = reply->error();
506 qDebug() << "errorcode:" << errorcode << reply->errorString();
508 if(myMainw->settingsDialog->profileDialog)
510 //QMessageBox::about(myMainw->settingsDialog->profileDialog, "Server reply to requesting profile",reply->errorString());
511 myMainw->settingsDialog->profileDialog->setLabelInfoToUser("Profile save to server - fail");
516 qDebug() << "errorcode:" << errorcode << reply->errorString();
518 if(myMainw->settingsDialog->profileDialog)
520 //QMessageBox::about(myMainw->settingsDialog->profileDialog, "Server reply to requesting profile", "OK " + reply->readAll());
521 myMainw->settingsDialog->profileDialog->setLabelInfoToUser("Profile saved to server");
527 * This slot function react to servers responce after request of picture has been sent.
529 void HttpClient::ackOfSendingPicture()
531 qDebug() << "__ackOfSendingPicture";
532 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
533 QNetworkReply::NetworkError errorcode;
534 errorcode = reply->error();
536 qDebug() << "errorcode:" << errorcode << reply->errorString();
538 if(myMainw->settingsDialog->profileDialog)
540 //QMessageBox::about(myMainw->settingsDialog->profileDialog, "Server reply to requesting picture",reply->errorString());
541 myMainw->settingsDialog->profileDialog->setLabelInfoToUser("Picture save to server - fail");
545 qDebug() << "errorcode:" << errorcode << reply->errorString();
547 if(myMainw->settingsDialog->profileDialog)
549 //QMessageBox::about(myMainw->settingsDialog->profileDialog, "Server reply to requesting picture", "OK " + reply->readAll());
550 myMainw->settingsDialog->profileDialog->setLabelInfoToUser("Picture saved to server");