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" ;
38 *@brief Sends registration information to the server in xml format.
39 *Reads user name, password and emaol address from resuldialogs internal variables.
41 void HttpClient::requestRegistration()
43 qDebug() << "_requestRegistration" ;
44 qDebug() << myMainw->settingsDialog->getRegUserName() << "+" << myMainw->settingsDialog->getRegPassword() << "+" << myMainw->settingsDialog->getRegEmail();
46 QBuffer *regbuffer = new QBuffer();
47 QUrl qurl("http://api.speedfreak-app.com/api/register");
48 QNetworkRequest request(qurl);
49 qDebug() << qurl.toString();
50 QNetworkReply *currentDownload;
52 regbuffer->open(QBuffer::ReadWrite);
53 myXmlwriter->writeRegistering(regbuffer,
54 myMainw->settingsDialog->getRegUserName(),
55 myMainw->settingsDialog->getRegPassword(),
56 myMainw->settingsDialog->getRegEmail());
57 qDebug() << "carmainwindow: regbuffer->data(): " << regbuffer->data();
59 currentDownload = netManager->post(request, ("xml=" + regbuffer->data()));
60 connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfRegistration()));
61 //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
64 if(myMainw->settingsDialog)
65 myMainw->settingsDialog->setLabelInfoToUser("Reguesting registration from server");
71 *@brief Sends result(s) to the server in xml format.
72 *Send authentication information in the header.
74 void HttpClient::sendResultXml(QString category, double result)
76 qDebug() << "_sendResultXml";
78 QBuffer *xmlbuffer = new QBuffer();
80 QUrl qurl("http://api.speedfreak-app.com/api/update/" + category);
81 qDebug() << qurl.toString();
82 QNetworkRequest request(qurl);
83 QNetworkReply *currentDownload;
85 xmlbuffer->open(QBuffer::ReadWrite);
86 myXmlwriter->writeResult(xmlbuffer, result);
87 qDebug() << "carmainwindow: xmlbuffer->data(): " << xmlbuffer->data();
89 QString credentials = myMainw->settingsDialog->getUserName() + ":" + myMainw->settingsDialog->getPassword();
90 credentials = "Basic " + credentials.toAscii().toBase64();
91 request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
93 currentDownload = netManager->post(request, ("xml=" + xmlbuffer->data()));
94 connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfResult()));
95 //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
98 if(myMainw->accstart->accRealTimeDialog->resultDialog)
99 myMainw->accstart->accRealTimeDialog->resultDialog->setLabelInfoToUser("Sending result to server");
105 *@brief Sends route to the server in xml format.
106 *Send authentication information in the header.
107 *@todo Check destination URL.
109 void HttpClient::sendRouteXml()
111 qDebug() << "_sendRouteXml";
113 QString filename = "route.xml";
114 QFile file(filename);
115 if (!file.open(QFile::ReadOnly)) {
116 qDebug() << "_sendRouteXml file.open() fail";
120 QUrl qurl("http://api.speedfreak-app.com/api/update/route");
121 qDebug() << qurl.toString();
122 QNetworkRequest request(qurl);
123 QNetworkReply *currentDownload;
125 QString credentials = myMainw->settingsDialog->getUserName() + ":" + myMainw->settingsDialog->getPassword();
126 credentials = "Basic " + credentials.toAscii().toBase64();
127 request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
129 currentDownload = netManager->post(request, ("xml=" + file.readAll()));
130 connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfRoute()));
131 //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
134 if(myMainw->routeSaveDialog->routeDialog)
135 myMainw->routeSaveDialog->routeDialog->setLabelInfoToUser("Sending route to server");
141 *@brief Request the Top10List of certain category from the server.
142 *Send authentication information in the header.
143 *@param Category of results.
144 *@param Limit, the number of results.
146 void HttpClient::requestTopList(QString category, QString limit)
148 qDebug() << "_requestTopList" ;
150 QString urlBase = "http://api.speedfreak-app.com/api/results/";
151 QUrl qurl(urlBase + category + "/" + limit);
152 qDebug() << qurl.toString();
153 QNetworkRequest request(qurl);
154 QNetworkReply *currentDownload;
156 QString credentials = myMainw->settingsDialog->getUserName() + ":" + myMainw->settingsDialog->getPassword();
157 credentials = "Basic " + credentials.toAscii().toBase64();
158 request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
160 currentDownload = netManager->post(request, ("data=" ));
161 connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfToplist()));
162 //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
165 if(myMainw->topResultDialog)
166 myMainw->topResultDialog->setLabelInfoToUser("Reguesting top10 list from server");
170 *@brief Request categories list from the server.
171 *Send authentication information in the header.
173 void HttpClient::requestCategories()
175 qDebug() << "_requestCategories" ;
177 QUrl qurl("http://api.speedfreak-app.com/api/categories/");
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(ackOfCategories()));
188 //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
191 if(myMainw->topResultDialog)
192 myMainw->topResultDialog->setLabelInfoToUser("Reguesting categories from server");
196 *@brief Check that username and password exist on the server.
197 *Send authentication information in the header.
199 void HttpClient::checkLogin()
201 qDebug() << "_checkLogin";
203 QUrl qurl("http://api.speedfreak-app.com/api/login/");
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(ackOfLogin()));
214 //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
217 if(myMainw->settingsDialog)
218 myMainw->settingsDialog->setLabelInfoToUser("Checking login validity from server");
222 *@brief React to servers responce after result has been sent.
224 void HttpClient::ackOfResult()
226 qDebug() << "_ackOfResult";
229 if(myMainw->accstart->accRealTimeDialog->resultDialog)
230 myMainw->accstart->accRealTimeDialog->resultDialog->setLabelInfoToUser("");
232 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
234 QNetworkReply::NetworkError errorcode;
235 errorcode = reply->error();
237 qDebug() << "errorcode:" << errorcode << reply->errorString();
240 if(myMainw->accstart->accRealTimeDialog->resultDialog)
241 QMessageBox::about(myMainw->accstart->accRealTimeDialog->resultDialog, "Server reply to result sending ",reply->errorString());
242 if(myMainw->accstart->accRealTimeDialog->resultDialog)
243 myMainw->accstart->accRealTimeDialog->resultDialog->setLabelInfoToUser("Error");
244 if(myMainw->accstart->accRealTimeDialog->resultDialog)
245 myMainw->accstart->accRealTimeDialog->resultDialog->setSendServerButtonEnabled();
248 qDebug() << "errorcode:" << errorcode << reply->errorString();
251 if(myMainw->accstart->accRealTimeDialog->resultDialog)
252 QMessageBox::about(myMainw->accstart->accRealTimeDialog->resultDialog, "Server reply to result sending", "Result received " + reply->readAll());
253 if(myMainw->accstart->accRealTimeDialog->resultDialog)
254 myMainw->accstart->accRealTimeDialog->resultDialog->setLabelInfoToUser("Result received");
259 *@brief React to servers responce after route has been sent.
261 void HttpClient::ackOfRoute()
263 qDebug() << "_ackOfRoute";
265 if(myMainw->routeSaveDialog->routeDialog)
266 myMainw->routeSaveDialog->routeDialog->setLabelInfoToUser("");
268 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
270 QNetworkReply::NetworkError errorcode;
271 errorcode = reply->error();
273 qDebug() << "errorcode:" << errorcode << reply->errorString();
274 if(myMainw->routeSaveDialog->routeDialog)
275 QMessageBox::about(myMainw->routeSaveDialog->routeDialog, "Server reply to route sending ",reply->errorString());
276 if(myMainw->routeSaveDialog->routeDialog)
277 myMainw->routeSaveDialog->routeDialog->setSendServerButtonEnabled();
280 qDebug() << "errorcode:" << errorcode << reply->errorString();
281 if(myMainw->routeSaveDialog->routeDialog)
282 QMessageBox::about(myMainw->routeSaveDialog->routeDialog, "Server reply to route sending", "Route received " + reply->readAll());
287 *@brief React to servers responce after registration has been sent.
288 *@todo Implement consequencies of reply.
290 void HttpClient::ackOfRegistration()
292 qDebug() << "_ackOfRegistration";
294 if(myMainw->settingsDialog)
295 myMainw->settingsDialog->setLabelInfoToUser("");
297 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
299 QNetworkReply::NetworkError errorcode;
300 errorcode = reply->error();
302 qDebug() << "errorcode:" << errorcode << reply->errorString();
303 if(myMainw->settingsDialog)
304 QMessageBox::about(myMainw->settingsDialog, "Server reply to registration",reply->readAll());
307 qDebug() << "errorcode=0" << errorcode << reply->errorString();
308 if(myMainw->settingsDialog)
309 QMessageBox::about(myMainw->settingsDialog, "Server reply to registration", "User registration " + reply->readAll());
314 *@brief React to servers responce after request for categories has been sent.
316 void HttpClient::ackOfCategories()
318 qDebug() << "_ackOfCategories";
320 if(myMainw->topResultDialog)
321 myMainw->topResultDialog->setLabelInfoToUser("");
323 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
324 myXmlreader->xmlReadCategories(reply);
326 QNetworkReply::NetworkError errorcode;
327 errorcode = reply->error();
329 qDebug() << "errorcode:" << errorcode << reply->errorString();
330 //QMessageBox::about(myMainw->topResultDialog, "Server reply to requesting categories",reply->errorString());
331 if(myMainw->topResultDialog)
332 myMainw->topResultDialog->setLabelInfoToUser("You're not logged! Please register or log in.");
335 qDebug() << "errorcode:" << errorcode << reply->errorString();
336 //QMessageBox::about(myMainw->topResultDialog, "Server reply to requesting categories ", "OK");
337 if(myMainw->topResultDialog)
338 myMainw->topResultDialog->setLabelInfoToUser("");
343 *@brief React to servers responce after request of TopList in certain category has been sent.
345 void HttpClient::ackOfLogin()
347 qDebug() << "_ackOffLogin";
349 if(myMainw->settingsDialog)
350 myMainw->settingsDialog->setLabelInfoToUser("");
352 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
354 QNetworkReply::NetworkError errorcode;
355 errorcode = reply->error();
358 qDebug() << "errorcode:" << errorcode << reply->errorString();
359 if(myMainw->settingsDialog)
361 QMessageBox::about(myMainw->settingsDialog, "Server does not recognize your username. Please registrate.",reply->errorString());
362 myMainw->settingsDialog->usernameOk(false);
367 qDebug() << "errorcode:" << errorcode << reply->errorString();
368 if(myMainw->settingsDialog)
369 QMessageBox::about(myMainw->settingsDialog, "Server reply to login", "User login " + reply->readAll());
370 // here signal emit to mainwindow for username setting to main panel
372 if( myMainw->settingsDialog)
374 myMainw->settingsDialog->usernameOk(true);
375 myMainw->settingsDialog->close();
382 *@brief Reports errors, when server has sent error signal.
384 void HttpClient::errorFromServer(QNetworkReply::NetworkError errorcode)
386 qDebug() << "_errorFromServer";
387 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
390 qDebug() << "errorcode:" << errorcode;
391 //Note that errors are already reported on other each functions for server communication
392 //QMessageBox::about(myMainw, "Server reported an error", reply->errorString());
395 qDebug() << "errorcode:" << errorcode << reply->errorString();
396 qDebug() << reply->readAll();
401 *@brief React to servers responce after request of TopList in certain category has been sent.
403 void HttpClient::ackOfToplist()
405 qDebug() << "_ackOfToplist";
406 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
407 myXmlreader->xmlReadTop10Results(reply);
409 QNetworkReply::NetworkError errorcode;
410 errorcode = reply->error();
412 qDebug() << "errorcode:" << errorcode << reply->errorString();
413 //QMessageBox::about(myMainw->topResultDialog, "Server reply to requesting top 10 list",reply->errorString());
414 if(myMainw->topResultDialog)
415 myMainw->topResultDialog->setLabelInfoToUser("Error");
418 qDebug() << "errorcode:" << errorcode << reply->errorString();
419 //QMessageBox::about(myMainw->topResultDialog, "Server reply to requesting top 10 list", "OK " + reply->readAll());
420 if(myMainw->topResultDialog)
421 myMainw->topResultDialog->setLabelInfoToUser("");