3 #include "httpclient.h"
4 #include "carmainwindow.h"
8 *@brief Constructor, connects object to GUI
9 *@param Pointer to carmainwindow, which is temporarily used during development
11 HttpClient::HttpClient(CarMainWindow *myCarw)
14 netManager = new QNetworkAccessManager();
15 myXmlwriter = new XmlWriter();
16 myXmlreader = new XmlReader();
22 HttpClient::~HttpClient()
28 *@brief Sends registration information to the server in xml format.
29 *Reads user name, password and emaol address from resuldialogs internal variables.
30 *@todo Replace msg box with better reaction to server`s responce.
32 void HttpClient::requestRegistration()
34 qDebug() << "_requestRegistration" ;
35 qDebug() << myMainw->myRegistration->getUserName() << "+" << myMainw->myRegistration->getPassword() << "+" << myMainw->myRegistration->getEmail();
37 QBuffer *regbuffer = new QBuffer();
38 QUrl qurl("http://api.speedfreak-app.com/api/register");
39 QNetworkRequest request(qurl);
40 qDebug() << qurl.toString();
41 QNetworkReply *currentDownload;
43 regbuffer->open(QBuffer::ReadWrite);
44 myXmlwriter->writeRegistering(regbuffer,
45 myMainw->myRegistration->getUserName(),
46 myMainw->myRegistration->getPassword(),
47 myMainw->myRegistration->getEmail());
48 qDebug() << "carmainwindow: regbuffer->data(): " << regbuffer->data();
50 currentDownload = netManager->post(request, ("xml=" + regbuffer->data()));
51 connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfRegistration()));
52 //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
58 *@brief Sends result(s) to the server in xml format.
59 *Send authentication information in the header.
61 void HttpClient::sendResultXml(QString category)
63 qDebug() << "_sendResultXml";
65 QBuffer *xmlbuffer = new QBuffer();
67 QUrl qurl("http://api.speedfreak-app.com/api/update/" + category);
68 qDebug() << qurl.toString();
69 QNetworkRequest request(qurl);
70 QNetworkReply *currentDownload;
72 xmlbuffer->open(QBuffer::ReadWrite);
73 myXmlwriter->writeResult(xmlbuffer);
74 qDebug() << "carmainwindow: xmlbuffer->data(): " << xmlbuffer->data();
76 QString credentials = myMainw->myLogin->getUserName() + ":" + myMainw->myLogin->getPassword();
77 credentials = "Basic " + credentials.toAscii().toBase64();
78 request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
80 currentDownload = netManager->post(request, ("xml=" + xmlbuffer->data()));
81 connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfResult()));
82 //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
88 *@brief Sends route to the server in xml format.
89 *Send authentication information in the header.
90 *@todo Check destination URL.
92 void HttpClient::sendRouteXml()
94 qDebug() << "_sendResultXml";
96 QBuffer *xmlbuffer = new QBuffer();
98 QUrl qurl("http://api.speedfreak-app.com/api/update/route");
99 qDebug() << qurl.toString();
100 QNetworkRequest request(qurl);
101 QNetworkReply *currentDownload;
103 xmlbuffer->open(QBuffer::ReadWrite);
104 myXmlwriter->writeGpsTrack(xmlbuffer, myMainw->gpsData->getGpsDataArray(), myMainw->gpsData->getRoundCounter());
105 qDebug() << "carmainwindow: xmlbuffer->data(): " << xmlbuffer->data();
107 QString credentials = myMainw->myLogin->getUserName() + ":" + myMainw->myLogin->getPassword();
108 credentials = "Basic " + credentials.toAscii().toBase64();
109 request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
111 currentDownload = netManager->post(request, ("xml=" + xmlbuffer->data()));
112 connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfRoute()));
113 //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
119 *@brief Request the Top10List of certain category from the server.
120 *Send authentication information in the header.
121 *@param Category of results.
122 *@param Limit, the number of results.
124 void HttpClient::requestTopList(QString category, QString limit)
126 qDebug() << "_requestTopList" ;
128 QString urlBase = "http://api.speedfreak-app.com/api/results/";
129 QUrl qurl(urlBase + category + "/" + limit);
130 qDebug() << qurl.toString();
131 QNetworkRequest request(qurl);
132 QNetworkReply *currentDownload;
134 QString credentials = myMainw->myLogin->getUserName() + ":" + myMainw->myLogin->getPassword();
135 credentials = "Basic " + credentials.toAscii().toBase64();
136 request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
138 currentDownload = netManager->post(request, ("data=" ));
139 connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfToplist()));
140 //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
145 *@brief Request categories list from the server.
146 *Send authentication information in the header.
148 void HttpClient::requestCategories()
150 qDebug() << "_requestCategories" ;
152 QUrl qurl("http://api.speedfreak-app.com/api/categories/");
153 qDebug() << qurl.toString();
154 QNetworkRequest request(qurl);
155 QNetworkReply *currentDownload;
157 QString credentials = myMainw->myLogin->getUserName() + ":" + myMainw->myLogin->getPassword();
158 credentials = "Basic " + credentials.toAscii().toBase64();
159 request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
161 currentDownload = netManager->post(request, ("data=" ));
162 connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfCategories()));
163 //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
168 *@brief Check that username and password exist on the server.
169 *Send authentication information in the header.
171 void HttpClient::checkLogin()
173 qDebug() << "_checkLogin";
175 QUrl qurl("http://api.speedfreak-app.com/api/login/");
176 qDebug() << qurl.toString();
177 QNetworkRequest request(qurl);
178 QNetworkReply *currentDownload;
180 QString credentials = myMainw->myLogin->getUserName() + ":" + myMainw->myLogin->getPassword();
181 credentials = "Basic " + credentials.toAscii().toBase64();
182 request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
184 currentDownload = netManager->post(request, ("data=" ));
185 connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfLogin()));
186 //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
191 *@brief React to servers responce after result has been sent.
192 *@todo Implement consequencies of reply.
194 void HttpClient::ackOfResult()
196 qDebug() << "_ackOfResult";
198 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
200 QNetworkReply::NetworkError errorcode;
201 errorcode = reply->error();
203 qDebug() << "errorcode:" << errorcode << reply->errorString();
204 QMessageBox::about(myMainw, "Server reply to result sending ",reply->errorString());
207 qDebug() << "errorcode:" << errorcode << reply->errorString();
208 QMessageBox::about(myMainw, "Server reply to result sending", "Result received " + reply->readAll());
213 *@brief React to servers responce after result has been sent.
214 *@todo Implement consequencies of reply.
216 void HttpClient::ackOfRoute()
218 qDebug() << "_ackOfRoute";
220 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
222 QNetworkReply::NetworkError errorcode;
223 errorcode = reply->error();
225 qDebug() << "errorcode:" << errorcode << reply->errorString();
226 QMessageBox::about(myMainw, "Server reply to route sending ",reply->errorString());
229 qDebug() << "errorcode:" << errorcode << reply->errorString();
230 QMessageBox::about(myMainw, "Server reply to route sending", "Route received " + reply->readAll());
235 *@brief React to servers responce after registration has been sent.
236 *@todo Implement consequencies of reply.
238 void HttpClient::ackOfRegistration()
240 qDebug() << "_ackOfRegistration";
242 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
244 QNetworkReply::NetworkError errorcode;
245 errorcode = reply->error();
247 qDebug() << "errorcode:" << errorcode << reply->errorString();
248 QMessageBox::about(myMainw, "Server reply to registration",reply->readAll());
251 qDebug() << "errorcode=0" << errorcode << reply->errorString();
252 QMessageBox::about(myMainw, "Server reply to registration", "User registration " + reply->readAll());
259 *@brief React to servers responce after request for categories has been sent.
261 void HttpClient::ackOfCategories()
263 qDebug() << "_ackOfCategories";
265 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
266 myXmlreader->xmlReadCategories(reply);
268 QNetworkReply::NetworkError errorcode;
269 errorcode = reply->error();
271 qDebug() << "errorcode:" << errorcode << reply->errorString();
272 QMessageBox::about(myMainw, "Server reply to requesting categories",reply->errorString());
275 qDebug() << "errorcode:" << errorcode << reply->errorString();
276 QMessageBox::about(myMainw, "Server reply to requesting categories ", "OK");
283 *@brief React to servers responce after request of TopList in certain category has been sent.
284 *@todo Implement routing reply`s contents to UI.
286 void HttpClient::ackOfLogin()
288 qDebug() << "_ackOffLogin";
290 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
292 QNetworkReply::NetworkError errorcode;
293 errorcode = reply->error();
295 qDebug() << "errorcode:" << errorcode << reply->errorString();
296 QMessageBox::about(myMainw, "Server does not recognize your username. Please registrate.",reply->errorString());
299 qDebug() << "errorcode:" << errorcode << reply->errorString();
300 QMessageBox::about(myMainw, "Server reply to login", "User login " + reply->readAll());
306 *@brief Reports errors, when server has sent error signal.
308 void HttpClient::errorFromServer(QNetworkReply::NetworkError errorcode)
310 qDebug() << "_errorFromServer";
312 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
315 qDebug() << "errorcode:" << errorcode;
316 //Note that errors are already reported on other ach-functions for server communication
317 //QMessageBox::about(myMainw, "Server reported an error", reply->errorString());
320 qDebug() << "errorcode:" << errorcode << reply->errorString();
321 qDebug() << reply->readAll();
328 *@brief React to servers responce after request of TopList in certain category has been sent.
329 *@todo Implement routing reply`s contents to UI.
331 void HttpClient::ackOfToplist()
333 qDebug() << "_ackOfToplist";
335 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
336 myXmlreader->xmlReadTop10Results(reply);
338 QNetworkReply::NetworkError errorcode;
339 errorcode = reply->error();
341 qDebug() << "errorcode:" << errorcode << reply->errorString();
342 QMessageBox::about(myMainw, "Server reply to requesting top 10 list",reply->errorString());
345 qDebug() << "errorcode:" << errorcode << reply->errorString();
346 QMessageBox::about(myMainw, "Server reply to requesting top 10 list", "OK " + reply->readAll());