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.
31 void HttpClient::requestRegistration()
33 qDebug() << "_requestRegistration" ;
34 qDebug() << myMainw->myRegistration->getUserName() << "+" << myMainw->myRegistration->getPassword() << "+" << myMainw->myRegistration->getEmail();
36 QBuffer *regbuffer = new QBuffer();
37 QUrl qurl("http://api.speedfreak-app.com/api/register");
38 QNetworkRequest request(qurl);
39 qDebug() << qurl.toString();
40 QNetworkReply *currentDownload;
42 regbuffer->open(QBuffer::ReadWrite);
43 myXmlwriter->writeRegistering(regbuffer,
44 myMainw->myRegistration->getUserName(),
45 myMainw->myRegistration->getPassword(),
46 myMainw->myRegistration->getEmail());
47 qDebug() << "carmainwindow: regbuffer->data(): " << regbuffer->data();
49 currentDownload = netManager->post(request, ("xml=" + regbuffer->data()));
50 connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfRegistration()));
51 //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
52 myMainw->setLabelInfoToUser("Reguesting registration from server");
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)));
83 myMainw->setLabelInfoToUser("Sending result to server");
89 *@brief Sends route to the server in xml format.
90 *Send authentication information in the header.
91 *@todo Check destination URL.
93 void HttpClient::sendRouteXml()
95 qDebug() << "_sendResultXml";
97 QBuffer *xmlbuffer = new QBuffer();
99 QUrl qurl("http://api.speedfreak-app.com/api/update/route");
100 qDebug() << qurl.toString();
101 QNetworkRequest request(qurl);
102 QNetworkReply *currentDownload;
104 xmlbuffer->open(QBuffer::ReadWrite);
105 myXmlwriter->writeGpsTrack(xmlbuffer, myMainw->gpsData->getGpsDataArray(), myMainw->gpsData->getRoundCounter());
106 qDebug() << "carmainwindow: xmlbuffer->data(): " << xmlbuffer->data();
108 QString credentials = myMainw->myLogin->getUserName() + ":" + myMainw->myLogin->getPassword();
109 credentials = "Basic " + credentials.toAscii().toBase64();
110 request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
112 currentDownload = netManager->post(request, ("xml=" + xmlbuffer->data()));
113 connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfRoute()));
114 //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
115 myMainw->setLabelInfoToUser("Sending route to server");
121 *@brief Request the Top10List of certain category from the server.
122 *Send authentication information in the header.
123 *@param Category of results.
124 *@param Limit, the number of results.
126 void HttpClient::requestTopList(QString category, QString limit)
128 qDebug() << "_requestTopList" ;
130 QString urlBase = "http://api.speedfreak-app.com/api/results/";
131 QUrl qurl(urlBase + category + "/" + limit);
132 qDebug() << qurl.toString();
133 QNetworkRequest request(qurl);
134 QNetworkReply *currentDownload;
136 QString credentials = myMainw->myLogin->getUserName() + ":" + myMainw->myLogin->getPassword();
137 credentials = "Basic " + credentials.toAscii().toBase64();
138 request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
140 currentDownload = netManager->post(request, ("data=" ));
141 connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfToplist()));
142 //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
143 myMainw->setLabelInfoToUser("Reguesting top10 list from server");
148 *@brief Request categories list from the server.
149 *Send authentication information in the header.
151 void HttpClient::requestCategories()
153 qDebug() << "_requestCategories" ;
155 QUrl qurl("http://api.speedfreak-app.com/api/categories/");
156 qDebug() << qurl.toString();
157 QNetworkRequest request(qurl);
158 QNetworkReply *currentDownload;
160 QString credentials = myMainw->myLogin->getUserName() + ":" + myMainw->myLogin->getPassword();
161 credentials = "Basic " + credentials.toAscii().toBase64();
162 request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
164 currentDownload = netManager->post(request, ("data=" ));
165 connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfCategories()));
166 //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
167 myMainw->setLabelInfoToUser("Reguesting categories from server");
172 *@brief Check that username and password exist on the server.
173 *Send authentication information in the header.
175 void HttpClient::checkLogin()
177 qDebug() << "_checkLogin";
179 QUrl qurl("http://api.speedfreak-app.com/api/login/");
180 qDebug() << qurl.toString();
181 QNetworkRequest request(qurl);
182 QNetworkReply *currentDownload;
184 QString credentials = myMainw->myLogin->getUserName() + ":" + myMainw->myLogin->getPassword();
185 credentials = "Basic " + credentials.toAscii().toBase64();
186 request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
188 currentDownload = netManager->post(request, ("data=" ));
189 connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfLogin()));
190 //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
191 myMainw->setLabelInfoToUser("Checking login validity from server");
196 *@brief React to servers responce after result has been sent.
198 void HttpClient::ackOfResult()
200 qDebug() << "_ackOfResult";
202 myMainw->setLabelInfoToUser("");
204 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
206 QNetworkReply::NetworkError errorcode;
207 errorcode = reply->error();
209 qDebug() << "errorcode:" << errorcode << reply->errorString();
210 QMessageBox::about(myMainw, "Server reply to result sending ",reply->errorString());
213 qDebug() << "errorcode:" << errorcode << reply->errorString();
214 QMessageBox::about(myMainw, "Server reply to result sending", "Result received " + reply->readAll());
219 *@brief React to servers responce after route has been sent.
221 void HttpClient::ackOfRoute()
223 qDebug() << "_ackOfRoute";
225 myMainw->setLabelInfoToUser("");
227 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
229 QNetworkReply::NetworkError errorcode;
230 errorcode = reply->error();
232 qDebug() << "errorcode:" << errorcode << reply->errorString();
233 QMessageBox::about(myMainw, "Server reply to route sending ",reply->errorString());
236 qDebug() << "errorcode:" << errorcode << reply->errorString();
237 QMessageBox::about(myMainw, "Server reply to route sending", "Route received " + reply->readAll());
242 *@brief React to servers responce after registration has been sent.
243 *@todo Implement consequencies of reply.
245 void HttpClient::ackOfRegistration()
247 qDebug() << "_ackOfRegistration";
249 myMainw->setLabelInfoToUser("");
251 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
253 QNetworkReply::NetworkError errorcode;
254 errorcode = reply->error();
256 qDebug() << "errorcode:" << errorcode << reply->errorString();
257 QMessageBox::about(myMainw, "Server reply to registration",reply->readAll());
260 qDebug() << "errorcode=0" << errorcode << reply->errorString();
261 QMessageBox::about(myMainw, "Server reply to registration", "User registration " + reply->readAll());
267 *@brief React to servers responce after request for categories has been sent.
269 void HttpClient::ackOfCategories()
271 qDebug() << "_ackOfCategories";
273 myMainw->setLabelInfoToUser("");
275 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
276 myXmlreader->xmlReadCategories(reply);
278 QNetworkReply::NetworkError errorcode;
279 errorcode = reply->error();
281 qDebug() << "errorcode:" << errorcode << reply->errorString();
282 QMessageBox::about(myMainw, "Server reply to requesting categories",reply->errorString());
285 qDebug() << "errorcode:" << errorcode << reply->errorString();
286 QMessageBox::about(myMainw, "Server reply to requesting categories ", "OK");
292 *@brief React to servers responce after request of TopList in certain category has been sent.
294 void HttpClient::ackOfLogin()
296 qDebug() << "_ackOffLogin";
298 myMainw->setLabelInfoToUser("");
300 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
302 QNetworkReply::NetworkError errorcode;
303 errorcode = reply->error();
305 qDebug() << "errorcode:" << errorcode << reply->errorString();
306 QMessageBox::about(myMainw, "Server does not recognize your username. Please registrate.",reply->errorString());
309 qDebug() << "errorcode:" << errorcode << reply->errorString();
310 QMessageBox::about(myMainw, "Server reply to login", "User login " + reply->readAll());
316 *@brief Reports errors, when server has sent error signal.
318 void HttpClient::errorFromServer(QNetworkReply::NetworkError errorcode)
320 qDebug() << "_errorFromServer";
322 myMainw->setLabelInfoToUser("");
324 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
327 qDebug() << "errorcode:" << errorcode;
328 //Note that errors are already reported on other each functions for server communication
329 //QMessageBox::about(myMainw, "Server reported an error", reply->errorString());
332 qDebug() << "errorcode:" << errorcode << reply->errorString();
333 qDebug() << reply->readAll();
339 *@brief React to servers responce after request of TopList in certain category has been sent.
341 void HttpClient::ackOfToplist()
343 qDebug() << "_ackOfToplist";
345 myMainw->setLabelInfoToUser("");
347 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
348 myXmlreader->xmlReadTop10Results(reply);
350 QNetworkReply::NetworkError errorcode;
351 errorcode = reply->error();
353 qDebug() << "errorcode:" << errorcode << reply->errorString();
354 QMessageBox::about(myMainw, "Server reply to requesting top 10 list",reply->errorString());
357 qDebug() << "errorcode:" << errorcode << reply->errorString();
358 QMessageBox::about(myMainw, "Server reply to requesting top 10 list", "OK " + reply->readAll());