750aa42810744c1ad8cd650471fa63b08f368840
[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   */
31 void HttpClient::requestRegistration()
32 {
33     qDebug() << "_requestRegistration" ;
34     qDebug() <<  myMainw->myRegistration->getUserName() << "+" <<  myMainw->myRegistration->getPassword() << "+" <<  myMainw->myRegistration->getEmail();
35
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;
41
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();
48
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");
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     myMainw->setLabelInfoToUser("Sending result to server");
84
85     xmlbuffer->close();
86 }
87
88 /**
89   *@brief Sends route to the server in xml format.
90   *Send authentication information in the header.
91   *@todo Check destination URL.
92   */
93 void HttpClient::sendRouteXml()
94 {
95     qDebug() << "_sendResultXml";
96
97     QBuffer *xmlbuffer = new QBuffer();
98
99     QUrl qurl("http://api.speedfreak-app.com/api/update/route");
100     qDebug() << qurl.toString();
101     QNetworkRequest request(qurl);
102     QNetworkReply *currentDownload;
103
104     xmlbuffer->open(QBuffer::ReadWrite);
105     myXmlwriter->writeGpsTrack(xmlbuffer, myMainw->gpsData->getGpsDataArray(), myMainw->gpsData->getRoundCounter());
106     qDebug() << "carmainwindow: xmlbuffer->data(): " << xmlbuffer->data();
107
108     QString credentials = myMainw->myLogin->getUserName() + ":" + myMainw->myLogin->getPassword();
109     credentials = "Basic " + credentials.toAscii().toBase64();
110     request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
111
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");
116
117     xmlbuffer->close();
118 }
119
120 /**
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.
125   */
126 void HttpClient::requestTopList(QString category, QString limit)
127 {
128     qDebug() << "_requestTopList" ;
129
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;
135
136     QString credentials = myMainw->myLogin->getUserName() + ":" + myMainw->myLogin->getPassword();
137     credentials = "Basic " + credentials.toAscii().toBase64();
138     request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
139
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");
144 }
145
146
147 /**
148   *@brief Request categories list from the server.
149   *Send authentication information in the header.
150   */
151 void HttpClient::requestCategories()
152 {
153     qDebug() << "_requestCategories" ;
154
155     QUrl qurl("http://api.speedfreak-app.com/api/categories/");
156     qDebug() << qurl.toString();
157     QNetworkRequest request(qurl);
158     QNetworkReply *currentDownload;
159
160     QString credentials = myMainw->myLogin->getUserName() + ":" + myMainw->myLogin->getPassword();
161     credentials = "Basic " + credentials.toAscii().toBase64();
162     request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
163
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");
168 }
169
170
171 /**
172   *@brief Check that username and password exist on the server.
173   *Send authentication information in the header.
174   */
175 void HttpClient::checkLogin()
176 {
177     qDebug() << "_checkLogin";
178
179     QUrl qurl("http://api.speedfreak-app.com/api/login/");
180     qDebug() << qurl.toString();
181     QNetworkRequest request(qurl);
182     QNetworkReply *currentDownload;
183
184     QString credentials = myMainw->myLogin->getUserName() + ":" + myMainw->myLogin->getPassword();
185     credentials = "Basic " + credentials.toAscii().toBase64();
186     request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
187
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");
192 }
193
194
195 /**
196   *@brief React to servers responce after result has been sent.
197   */
198 void HttpClient::ackOfResult()
199 {
200     qDebug() << "_ackOfResult";
201
202     myMainw->setLabelInfoToUser("");
203
204     QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
205
206     QNetworkReply::NetworkError errorcode;
207     errorcode = reply->error();
208     if(errorcode != 0) {
209         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
210         QMessageBox::about(myMainw, "Server reply to result sending ",reply->errorString());
211     }
212     else {
213         qDebug() << "errorcode:" << errorcode << reply->errorString();
214         QMessageBox::about(myMainw, "Server reply to result sending", "Result received " + reply->readAll());
215     }
216 }
217
218 /**
219   *@brief React to servers responce after route has been sent.
220   */
221 void HttpClient::ackOfRoute()
222 {
223     qDebug() << "_ackOfRoute";
224
225     myMainw->setLabelInfoToUser("");
226
227     QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
228
229     QNetworkReply::NetworkError errorcode;
230     errorcode = reply->error();
231     if(errorcode != 0) {
232         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
233         QMessageBox::about(myMainw, "Server reply to route sending ",reply->errorString());
234     }
235     else {
236         qDebug() << "errorcode:" << errorcode << reply->errorString();
237         QMessageBox::about(myMainw, "Server reply to route sending", "Route received " + reply->readAll());
238     }
239 }
240
241 /**
242   *@brief React to servers responce after registration has been sent.
243   *@todo Implement consequencies of reply.
244   */
245 void HttpClient::ackOfRegistration()
246 {
247     qDebug() << "_ackOfRegistration";
248
249     myMainw->setLabelInfoToUser("");
250
251     QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
252
253     QNetworkReply::NetworkError errorcode;
254     errorcode = reply->error();
255     if(errorcode != 0) {
256         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
257         QMessageBox::about(myMainw, "Server reply to registration",reply->readAll());
258     }
259     else {
260         qDebug() << "errorcode=0" << errorcode << reply->errorString();
261         QMessageBox::about(myMainw, "Server reply to registration", "User registration " + reply->readAll());
262     }
263 }
264
265
266 /**
267   *@brief React to servers responce after request for categories has been sent.
268   */
269 void HttpClient::ackOfCategories()
270 {
271     qDebug() << "_ackOfCategories";
272
273     myMainw->setLabelInfoToUser("");
274
275     QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
276     myXmlreader->xmlReadCategories(reply);
277
278     QNetworkReply::NetworkError errorcode;
279     errorcode = reply->error();
280     if(errorcode != 0) {
281         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
282         QMessageBox::about(myMainw, "Server reply to requesting categories",reply->errorString());
283     }
284     else {
285         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
286         QMessageBox::about(myMainw, "Server reply to requesting categories ", "OK");
287     }
288 }
289
290
291 /**
292   *@brief React to servers responce after request of TopList in certain category has been sent.
293   */
294 void HttpClient::ackOfLogin()
295 {
296     qDebug() << "_ackOffLogin";
297
298     myMainw->setLabelInfoToUser("");
299
300     QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
301
302     QNetworkReply::NetworkError errorcode;
303     errorcode = reply->error();
304     if(errorcode != 0) {
305         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
306         QMessageBox::about(myMainw, "Server does not recognize your username. Please registrate.",reply->errorString());
307     }
308     else {
309         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
310         QMessageBox::about(myMainw, "Server reply to login", "User login " + reply->readAll());
311     }
312 }
313
314
315 /**
316   *@brief Reports errors, when server has sent error signal.
317   */
318 void HttpClient::errorFromServer(QNetworkReply::NetworkError errorcode)
319 {
320     qDebug() << "_errorFromServer";
321
322     myMainw->setLabelInfoToUser("");
323
324     QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
325
326     if(errorcode != 0) {
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());
330     }
331     else {
332         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
333         qDebug() << reply->readAll();
334     }
335 }
336
337
338 /**
339   *@brief React to servers responce after request of TopList in certain category has been sent.
340   */
341 void HttpClient::ackOfToplist()
342 {
343     qDebug() << "_ackOfToplist";
344
345     myMainw->setLabelInfoToUser("");
346
347     QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
348     myXmlreader->xmlReadTop10Results(reply);
349
350     QNetworkReply::NetworkError errorcode;
351     errorcode = reply->error();
352     if(errorcode != 0) {
353         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
354         QMessageBox::about(myMainw, "Server reply to requesting top 10 list",reply->errorString());
355     }
356     else {
357         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
358         QMessageBox::about(myMainw, "Server reply to requesting top 10 list", "OK " + reply->readAll());
359     }
360 }
361