3 #include "httpclient.h"
4 #include "mainwindow.h"
8 *@brief Constructor, connects object to GUI
9 *@param Pointer to carmainwindow, which is temporarily used during development
11 HttpClient::HttpClient(MainWindow *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->settingsDialog->getRegUserName() << "+" << myMainw->settingsDialog->getRegPassword() << "+" << myMainw->settingsDialog->getRegEmail();
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->settingsDialog->getRegUserName(),
45 myMainw->settingsDialog->getRegPassword(),
46 myMainw->settingsDialog->getRegEmail());
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->settingsDialog->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, double result)
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, result);
74 qDebug() << "carmainwindow: xmlbuffer->data(): " << xmlbuffer->data();
76 QString credentials = myMainw->settingsDialog->getUserName() + ":" + myMainw->settingsDialog->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)));
85 myMainw->accstart->accRealTimeDialog->resultDialog->setLabelInfoToUser("Sending result to server");
91 *@brief Sends route to the server in xml format.
92 *Send authentication information in the header.
93 *@todo Check destination URL.
95 void HttpClient::sendRouteXml()
97 qDebug() << "_sendRouteXml";
99 QString filename = "route.xml";
100 QFile file(filename);
101 if (!file.open(QFile::ReadOnly)) {
102 qDebug() << "_sendRouteXml file.open() fail";
106 QUrl qurl("http://api.speedfreak-app.com/api/update/route");
107 qDebug() << qurl.toString();
108 QNetworkRequest request(qurl);
109 QNetworkReply *currentDownload;
111 QString credentials = myMainw->settingsDialog->getUserName() + ":" + myMainw->settingsDialog->getPassword();
112 credentials = "Basic " + credentials.toAscii().toBase64();
113 request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
115 currentDownload = netManager->post(request, ("xml=" + file.readAll()));
116 connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfRoute()));
117 //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
118 myMainw->routeDialog->setLabelInfoToUser("Sending route to server");
124 *@brief Request the Top10List of certain category from the server.
125 *Send authentication information in the header.
126 *@param Category of results.
127 *@param Limit, the number of results.
129 void HttpClient::requestTopList(QString category, QString limit)
131 qDebug() << "_requestTopList" ;
133 QString urlBase = "http://api.speedfreak-app.com/api/results/";
134 QUrl qurl(urlBase + category + "/" + limit);
135 qDebug() << qurl.toString();
136 QNetworkRequest request(qurl);
137 QNetworkReply *currentDownload;
139 QString credentials = myMainw->settingsDialog->getUserName() + ":" + myMainw->settingsDialog->getPassword();
140 credentials = "Basic " + credentials.toAscii().toBase64();
141 request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
143 currentDownload = netManager->post(request, ("data=" ));
144 connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfToplist()));
145 //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
146 myMainw->topResultDialog->setLabelInfoToUser("Reguesting top10 list from server");
151 *@brief Request categories list from the server.
152 *Send authentication information in the header.
154 void HttpClient::requestCategories()
156 qDebug() << "_requestCategories" ;
158 QUrl qurl("http://api.speedfreak-app.com/api/categories/");
159 qDebug() << qurl.toString();
160 QNetworkRequest request(qurl);
161 QNetworkReply *currentDownload;
163 QString credentials = myMainw->settingsDialog->getUserName() + ":" + myMainw->settingsDialog->getPassword();
164 credentials = "Basic " + credentials.toAscii().toBase64();
165 request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
167 currentDownload = netManager->post(request, ("data=" ));
168 connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfCategories()));
169 //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
170 myMainw->topResultDialog->setLabelInfoToUser("Reguesting categories from server");
175 *@brief Check that username and password exist on the server.
176 *Send authentication information in the header.
178 void HttpClient::checkLogin()
180 qDebug() << "_checkLogin";
182 QUrl qurl("http://api.speedfreak-app.com/api/login/");
183 qDebug() << qurl.toString();
184 QNetworkRequest request(qurl);
185 QNetworkReply *currentDownload;
187 QString credentials = myMainw->settingsDialog->getUserName() + ":" + myMainw->settingsDialog->getPassword();
188 credentials = "Basic " + credentials.toAscii().toBase64();
189 request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
191 currentDownload = netManager->post(request, ("data=" ));
192 connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfLogin()));
193 //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
194 myMainw->settingsDialog->setLabelInfoToUser("Checking login validity from server");
199 *@brief React to servers responce after result has been sent.
201 void HttpClient::ackOfResult()
203 qDebug() << "_ackOfResult";
206 myMainw->accstart->accRealTimeDialog->resultDialog->setLabelInfoToUser("");
208 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
210 QNetworkReply::NetworkError errorcode;
211 errorcode = reply->error();
213 qDebug() << "errorcode:" << errorcode << reply->errorString();
216 QMessageBox::about(myMainw->accstart->accRealTimeDialog->resultDialog, "Server reply to result sending ",reply->errorString());
219 qDebug() << "errorcode:" << errorcode << reply->errorString();
222 QMessageBox::about(myMainw->accstart->accRealTimeDialog->resultDialog, "Server reply to result sending", "Result received " + reply->readAll());
227 *@brief React to servers responce after route has been sent.
229 void HttpClient::ackOfRoute()
231 qDebug() << "_ackOfRoute";
233 myMainw->routeDialog->setLabelInfoToUser("");
235 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
237 QNetworkReply::NetworkError errorcode;
238 errorcode = reply->error();
240 qDebug() << "errorcode:" << errorcode << reply->errorString();
241 QMessageBox::about(myMainw->routeDialog, "Server reply to route sending ",reply->errorString());
244 qDebug() << "errorcode:" << errorcode << reply->errorString();
245 QMessageBox::about(myMainw, "Server reply to route sending", "Route received " + reply->readAll());
250 *@brief React to servers responce after registration has been sent.
251 *@todo Implement consequencies of reply.
253 void HttpClient::ackOfRegistration()
255 qDebug() << "_ackOfRegistration";
257 myMainw->settingsDialog->setLabelInfoToUser("");
259 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
261 QNetworkReply::NetworkError errorcode;
262 errorcode = reply->error();
264 qDebug() << "errorcode:" << errorcode << reply->errorString();
265 QMessageBox::about(myMainw->settingsDialog, "Server reply to registration",reply->readAll());
268 qDebug() << "errorcode=0" << errorcode << reply->errorString();
269 QMessageBox::about(myMainw->settingsDialog, "Server reply to registration", "User registration " + reply->readAll());
275 *@brief React to servers responce after request for categories has been sent.
277 void HttpClient::ackOfCategories()
279 qDebug() << "_ackOfCategories";
281 myMainw->topResultDialog->setLabelInfoToUser("");
283 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
284 myXmlreader->xmlReadCategories(reply);
286 QNetworkReply::NetworkError errorcode;
287 errorcode = reply->error();
289 qDebug() << "errorcode:" << errorcode << reply->errorString();
290 QMessageBox::about(myMainw->topResultDialog, "Server reply to requesting categories",reply->errorString());
293 qDebug() << "errorcode:" << errorcode << reply->errorString();
294 QMessageBox::about(myMainw->topResultDialog, "Server reply to requesting categories ", "OK");
300 *@brief React to servers responce after request of TopList in certain category has been sent.
302 void HttpClient::ackOfLogin()
304 qDebug() << "_ackOffLogin";
306 myMainw->settingsDialog->setLabelInfoToUser("");
308 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
310 QNetworkReply::NetworkError errorcode;
311 errorcode = reply->error();
313 qDebug() << "errorcode:" << errorcode << reply->errorString();
314 QMessageBox::about(myMainw->settingsDialog, "Server does not recognize your username. Please registrate.",reply->errorString());
317 qDebug() << "errorcode:" << errorcode << reply->errorString();
318 QMessageBox::about(myMainw->settingsDialog, "Server reply to login", "User login " + reply->readAll());
321 myMainw->settingsDialog->close();
326 *@brief Reports errors, when server has sent error signal.
328 void HttpClient::errorFromServer(QNetworkReply::NetworkError errorcode)
330 qDebug() << "_errorFromServer";
332 //myMainw->setLabelInfoToUser("");
334 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
337 qDebug() << "errorcode:" << errorcode;
338 //Note that errors are already reported on other each functions for server communication
339 //QMessageBox::about(myMainw, "Server reported an error", reply->errorString());
342 qDebug() << "errorcode:" << errorcode << reply->errorString();
343 qDebug() << reply->readAll();
349 *@brief React to servers responce after request of TopList in certain category has been sent.
351 void HttpClient::ackOfToplist()
353 qDebug() << "_ackOfToplist";
355 //myMainw->setLabelInfoToUser("");
357 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
358 myXmlreader->xmlReadTop10Results(reply);
360 QNetworkReply::NetworkError errorcode;
361 errorcode = reply->error();
363 qDebug() << "errorcode:" << errorcode << reply->errorString();
364 QMessageBox::about(myMainw->topResultDialog, "Server reply to requesting top 10 list",reply->errorString());
367 qDebug() << "errorcode:" << errorcode << reply->errorString();
368 //QMessageBox::about(myMainw->topResultDialog, "Server reply to requesting top 10 list", "OK " + reply->readAll());