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)));
83 myMainw->resultDialog->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() << "_sendRouteXml";
97 QString filename = "route.xml";
99 if (!file.open(QFile::ReadOnly)) {
100 qDebug() << "_sendRouteXml file.open() fail";
104 QUrl qurl("http://api.speedfreak-app.com/api/update/route");
105 qDebug() << qurl.toString();
106 QNetworkRequest request(qurl);
107 QNetworkReply *currentDownload;
109 QString credentials = myMainw->settingsDialog->getUserName() + ":" + myMainw->settingsDialog->getPassword();
110 credentials = "Basic " + credentials.toAscii().toBase64();
111 request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
113 currentDownload = netManager->post(request, ("xml=" + file.readAll()));
114 connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfRoute()));
115 //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
116 myMainw->routeDialog->setLabelInfoToUser("Sending route to server");
122 *@brief Request the Top10List of certain category from the server.
123 *Send authentication information in the header.
124 *@param Category of results.
125 *@param Limit, the number of results.
127 void HttpClient::requestTopList(QString category, QString limit)
129 qDebug() << "_requestTopList" ;
131 QString urlBase = "http://api.speedfreak-app.com/api/results/";
132 QUrl qurl(urlBase + category + "/" + limit);
133 qDebug() << qurl.toString();
134 QNetworkRequest request(qurl);
135 QNetworkReply *currentDownload;
137 QString credentials = myMainw->settingsDialog->getUserName() + ":" + myMainw->settingsDialog->getPassword();
138 credentials = "Basic " + credentials.toAscii().toBase64();
139 request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
141 currentDownload = netManager->post(request, ("data=" ));
142 connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfToplist()));
143 //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
144 myMainw->topResultDialog->setLabelInfoToUser("Reguesting top10 list from server");
149 *@brief Request categories list from the server.
150 *Send authentication information in the header.
152 void HttpClient::requestCategories()
154 qDebug() << "_requestCategories" ;
156 QUrl qurl("http://api.speedfreak-app.com/api/categories/");
157 qDebug() << qurl.toString();
158 QNetworkRequest request(qurl);
159 QNetworkReply *currentDownload;
161 QString credentials = myMainw->settingsDialog->getUserName() + ":" + myMainw->settingsDialog->getPassword();
162 credentials = "Basic " + credentials.toAscii().toBase64();
163 request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
165 currentDownload = netManager->post(request, ("data=" ));
166 connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfCategories()));
167 //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
168 myMainw->topResultDialog->setLabelInfoToUser("Reguesting categories from server");
173 *@brief Check that username and password exist on the server.
174 *Send authentication information in the header.
176 void HttpClient::checkLogin()
178 qDebug() << "_checkLogin";
180 QUrl qurl("http://api.speedfreak-app.com/api/login/");
181 qDebug() << qurl.toString();
182 QNetworkRequest request(qurl);
183 QNetworkReply *currentDownload;
185 QString credentials = myMainw->settingsDialog->getUserName() + ":" + myMainw->settingsDialog->getPassword();
186 credentials = "Basic " + credentials.toAscii().toBase64();
187 request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
189 currentDownload = netManager->post(request, ("data=" ));
190 connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfLogin()));
191 //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
192 myMainw->settingsDialog->setLabelInfoToUser("Checking login validity from server");
197 *@brief React to servers responce after result has been sent.
199 void HttpClient::ackOfResult()
201 qDebug() << "_ackOfResult";
203 myMainw->resultDialog->setLabelInfoToUser("");
205 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
207 QNetworkReply::NetworkError errorcode;
208 errorcode = reply->error();
210 qDebug() << "errorcode:" << errorcode << reply->errorString();
211 QMessageBox::about(myMainw->resultDialog, "Server reply to result sending ",reply->errorString());
214 qDebug() << "errorcode:" << errorcode << reply->errorString();
215 QMessageBox::about(myMainw->resultDialog, "Server reply to result sending", "Result received " + reply->readAll());
220 *@brief React to servers responce after route has been sent.
222 void HttpClient::ackOfRoute()
224 qDebug() << "_ackOfRoute";
226 myMainw->routeDialog->setLabelInfoToUser("");
228 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
230 QNetworkReply::NetworkError errorcode;
231 errorcode = reply->error();
233 qDebug() << "errorcode:" << errorcode << reply->errorString();
234 QMessageBox::about(myMainw->routeDialog, "Server reply to route sending ",reply->errorString());
237 qDebug() << "errorcode:" << errorcode << reply->errorString();
238 QMessageBox::about(myMainw, "Server reply to route sending", "Route received " + reply->readAll());
243 *@brief React to servers responce after registration has been sent.
244 *@todo Implement consequencies of reply.
246 void HttpClient::ackOfRegistration()
248 qDebug() << "_ackOfRegistration";
250 myMainw->settingsDialog->setLabelInfoToUser("");
252 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
254 QNetworkReply::NetworkError errorcode;
255 errorcode = reply->error();
257 qDebug() << "errorcode:" << errorcode << reply->errorString();
258 QMessageBox::about(myMainw->settingsDialog, "Server reply to registration",reply->readAll());
261 qDebug() << "errorcode=0" << errorcode << reply->errorString();
262 QMessageBox::about(myMainw->settingsDialog, "Server reply to registration", "User registration " + reply->readAll());
268 *@brief React to servers responce after request for categories has been sent.
270 void HttpClient::ackOfCategories()
272 qDebug() << "_ackOfCategories";
274 myMainw->topResultDialog->setLabelInfoToUser("");
276 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
277 myXmlreader->xmlReadCategories(reply);
279 QNetworkReply::NetworkError errorcode;
280 errorcode = reply->error();
282 qDebug() << "errorcode:" << errorcode << reply->errorString();
283 QMessageBox::about(myMainw->topResultDialog, "Server reply to requesting categories",reply->errorString());
286 qDebug() << "errorcode:" << errorcode << reply->errorString();
287 QMessageBox::about(myMainw->topResultDialog, "Server reply to requesting categories ", "OK");
293 *@brief React to servers responce after request of TopList in certain category has been sent.
295 void HttpClient::ackOfLogin()
297 qDebug() << "_ackOffLogin";
299 myMainw->settingsDialog->setLabelInfoToUser("");
301 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
303 QNetworkReply::NetworkError errorcode;
304 errorcode = reply->error();
306 qDebug() << "errorcode:" << errorcode << reply->errorString();
307 QMessageBox::about(myMainw->settingsDialog, "Server does not recognize your username. Please registrate.",reply->errorString());
310 qDebug() << "errorcode:" << errorcode << reply->errorString();
311 QMessageBox::about(myMainw->settingsDialog, "Server reply to login", "User login " + reply->readAll());
314 myMainw->settingsDialog->close();
319 *@brief Reports errors, when server has sent error signal.
321 void HttpClient::errorFromServer(QNetworkReply::NetworkError errorcode)
323 qDebug() << "_errorFromServer";
325 //myMainw->setLabelInfoToUser("");
327 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
330 qDebug() << "errorcode:" << errorcode;
331 //Note that errors are already reported on other each functions for server communication
332 //QMessageBox::about(myMainw, "Server reported an error", reply->errorString());
335 qDebug() << "errorcode:" << errorcode << reply->errorString();
336 qDebug() << reply->readAll();
342 *@brief React to servers responce after request of TopList in certain category has been sent.
344 void HttpClient::ackOfToplist()
346 qDebug() << "_ackOfToplist";
348 //myMainw->setLabelInfoToUser("");
350 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
351 myXmlreader->xmlReadTop10Results(reply);
353 QNetworkReply::NetworkError errorcode;
354 errorcode = reply->error();
356 qDebug() << "errorcode:" << errorcode << reply->errorString();
357 QMessageBox::about(myMainw->topResultDialog, "Server reply to requesting top 10 list",reply->errorString());
360 qDebug() << "errorcode:" << errorcode << reply->errorString();
361 //QMessageBox::about(myMainw->topResultDialog, "Server reply to requesting top 10 list", "OK " + reply->readAll());