Added sending route to server.
[speedfreak] / Client / httpclient.cpp
1 #include <QString>
2 #include <QMessageBox>
3 #include "httpclient.h"
4 #include "carmainwindow.h"
5
6
7 /**
8   *@brief Constructor, connects object to GUI
9   *@param Pointer to carmainwindow, which is temporarily used during development
10   */
11 HttpClient::HttpClient(CarMainWindow *myCarw)
12 {
13     myMainw = myCarw;
14     netManager = new QNetworkAccessManager();
15     myXmlwriter = new XmlWriter();
16     myXmlreader = new XmlReader();
17 }
18
19 /**
20   *@brief Destructor
21   */
22 HttpClient::~HttpClient()
23 {
24
25 }
26
27 /**
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.
31   */
32 void HttpClient::requestRegistration()
33 {
34     qDebug() << "_requestRegistration" ;
35     qDebug() <<  myMainw->myRegistration->getUserName() << "+" <<  myMainw->myRegistration->getPassword() << "+" <<  myMainw->myRegistration->getEmail();
36
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;
42
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();
49
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)));
53
54     regbuffer->close();
55 }
56
57 /**
58   *@brief Sends result(s) to the server in xml format.
59   *Send authentication information in the header.
60   */
61 void HttpClient::sendResultXml(QString category)
62 {
63     qDebug() << "_sendResultXml";
64
65     QBuffer *xmlbuffer = new QBuffer();
66
67     QUrl qurl("http://api.speedfreak-app.com/api/update/" + category);
68     qDebug() << qurl.toString();
69     QNetworkRequest request(qurl);
70     QNetworkReply *currentDownload;
71
72     xmlbuffer->open(QBuffer::ReadWrite);
73     myXmlwriter->writeResult(xmlbuffer);
74     qDebug() << "carmainwindow: xmlbuffer->data(): " << xmlbuffer->data();
75
76     QString credentials = myMainw->myLogin->getUserName() + ":" + myMainw->myLogin->getPassword();
77     credentials = "Basic " + credentials.toAscii().toBase64();
78     request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
79
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
84     xmlbuffer->close();
85 }
86
87 /**
88   *@brief Sends route to the server in xml format.
89   *Send authentication information in the header.
90   *@todo Check destination URL.
91   */
92 void HttpClient::sendRouteXml()
93 {
94     qDebug() << "_sendResultXml";
95
96     QBuffer *xmlbuffer = new QBuffer();
97
98     QUrl qurl("http://api.speedfreak-app.com/api/update/route");
99     qDebug() << qurl.toString();
100     QNetworkRequest request(qurl);
101     QNetworkReply *currentDownload;
102
103     xmlbuffer->open(QBuffer::ReadWrite);
104     myXmlwriter->writeGpsTrack(xmlbuffer, myMainw->gpsData->getGpsDataArray(), myMainw->gpsData->getRoundCounter());
105     qDebug() << "carmainwindow: xmlbuffer->data(): " << xmlbuffer->data();
106
107     QString credentials = myMainw->myLogin->getUserName() + ":" + myMainw->myLogin->getPassword();
108     credentials = "Basic " + credentials.toAscii().toBase64();
109     request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
110
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)));
114
115     xmlbuffer->close();
116 }
117
118 /**
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.
123   */
124 void HttpClient::requestTopList(QString category, QString limit)
125 {
126     qDebug() << "_requestTopList" ;
127
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;
133
134     QString credentials = myMainw->myLogin->getUserName() + ":" + myMainw->myLogin->getPassword();
135     credentials = "Basic " + credentials.toAscii().toBase64();
136     request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
137
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)));
141 }
142
143
144 /**
145   *@brief Request categories list from the server.
146   *Send authentication information in the header.
147   */
148 void HttpClient::requestCategories()
149 {
150     qDebug() << "_requestCategories" ;
151
152     QUrl qurl("http://api.speedfreak-app.com/api/categories/");
153     qDebug() << qurl.toString();
154     QNetworkRequest request(qurl);
155     QNetworkReply *currentDownload;
156
157     QString credentials = myMainw->myLogin->getUserName() + ":" + myMainw->myLogin->getPassword();
158     credentials = "Basic " + credentials.toAscii().toBase64();
159     request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
160
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)));
164 }
165
166
167 /**
168   *@brief Check that username and password exist on the server.
169   *Send authentication information in the header.
170   */
171 void HttpClient::checkLogin()
172 {
173     qDebug() << "_checkLogin";
174
175     QUrl qurl("http://api.speedfreak-app.com/api/login/");
176     qDebug() << qurl.toString();
177     QNetworkRequest request(qurl);
178     QNetworkReply *currentDownload;
179
180     QString credentials = myMainw->myLogin->getUserName() + ":" + myMainw->myLogin->getPassword();
181     credentials = "Basic " + credentials.toAscii().toBase64();
182     request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
183
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)));
187 }
188
189
190 /**
191   *@brief React to servers responce after result has been sent.
192   *@todo Implement consequencies of reply.
193   */
194 void HttpClient::ackOfResult()
195 {
196     qDebug() << "_ackOfResult";
197
198     QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
199
200     QNetworkReply::NetworkError errorcode;
201     errorcode = reply->error();
202     if(errorcode != 0) {
203         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
204         QMessageBox::about(myMainw, "Server reply to result sending ",reply->errorString());
205     }
206     else {
207         qDebug() << "errorcode:" << errorcode << reply->errorString();
208         QMessageBox::about(myMainw, "Server reply to result sending", "Result received " + reply->readAll());
209     }
210 }
211
212 /**
213   *@brief React to servers responce after result has been sent.
214   *@todo Implement consequencies of reply.
215   */
216 void HttpClient::ackOfRoute()
217 {
218     qDebug() << "_ackOfRoute";
219
220     QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
221
222     QNetworkReply::NetworkError errorcode;
223     errorcode = reply->error();
224     if(errorcode != 0) {
225         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
226         QMessageBox::about(myMainw, "Server reply to route sending ",reply->errorString());
227     }
228     else {
229         qDebug() << "errorcode:" << errorcode << reply->errorString();
230         QMessageBox::about(myMainw, "Server reply to route sending", "Route received " + reply->readAll());
231     }
232 }
233
234 /**
235   *@brief React to servers responce after registration has been sent.
236   *@todo Implement consequencies of reply.
237   */
238 void HttpClient::ackOfRegistration()
239 {
240     qDebug() << "_ackOfRegistration";
241
242     QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
243
244     QNetworkReply::NetworkError errorcode;
245     errorcode = reply->error();
246     if(errorcode != 0) {
247         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
248         QMessageBox::about(myMainw, "Server reply to registration",reply->readAll());
249     }
250     else {
251         qDebug() << "errorcode=0" << errorcode << reply->errorString();
252         QMessageBox::about(myMainw, "Server reply to registration", "User registration " + reply->readAll());
253     }
254
255 }
256
257
258 /**
259   *@brief React to servers responce after request for categories has been sent.
260   */
261 void HttpClient::ackOfCategories()
262 {
263     qDebug() << "_ackOfCategories";
264
265     QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
266     myXmlreader->xmlReadCategories(reply);
267
268     QNetworkReply::NetworkError errorcode;
269     errorcode = reply->error();
270     if(errorcode != 0) {
271         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
272         QMessageBox::about(myMainw, "Server reply to requesting categories",reply->errorString());
273     }
274     else {
275         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
276         QMessageBox::about(myMainw, "Server reply to requesting categories ", "OK");
277     }
278
279 }
280
281
282 /**
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.
285   */
286 void HttpClient::ackOfLogin()
287 {
288     qDebug() << "_ackOffLogin";
289
290     QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
291
292     QNetworkReply::NetworkError errorcode;
293     errorcode = reply->error();
294     if(errorcode != 0) {
295         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
296         QMessageBox::about(myMainw, "Server does not recognize your username. Please registrate.",reply->errorString());
297     }
298     else {
299         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
300         QMessageBox::about(myMainw, "Server reply to login", "User login " + reply->readAll());
301     }
302 }
303
304
305 /**
306   *@brief Reports errors, when server has sent error signal.
307   */
308 void HttpClient::errorFromServer(QNetworkReply::NetworkError errorcode)
309 {
310     qDebug() << "_errorFromServer";
311
312     QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
313
314     if(errorcode != 0) {
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());
318     }
319     else {
320         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
321         qDebug() << reply->readAll();
322     }
323
324 }
325
326
327 /**
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.
330   */
331 void HttpClient::ackOfToplist()
332 {
333     qDebug() << "_ackOfToplist";
334
335     QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
336     myXmlreader->xmlReadTop10Results(reply);
337
338     QNetworkReply::NetworkError errorcode;
339     errorcode = reply->error();
340     if(errorcode != 0) {
341         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
342         QMessageBox::about(myMainw, "Server reply to requesting top 10 list",reply->errorString());
343     }
344     else {
345         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
346         QMessageBox::about(myMainw, "Server reply to requesting top 10 list", "OK " + reply->readAll());
347     }
348
349 }
350