Route dialog height.
[speedfreak] / Client / httpclient.cpp
1 #include <QString>
2 #include <QMessageBox>
3 #include "httpclient.h"
4 #include "mainwindow.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(MainWindow *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->settingsDialog->getRegUserName() << "+" <<  myMainw->settingsDialog->getRegPassword() << "+" <<  myMainw->settingsDialog->getRegEmail();
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->settingsDialog->getRegUserName(),
45                        myMainw->settingsDialog->getRegPassword(),
46                        myMainw->settingsDialog->getRegEmail());
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->settingsDialog->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, double result)
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, result);
74     qDebug() << "carmainwindow: xmlbuffer->data(): " << xmlbuffer->data();
75
76     QString credentials = myMainw->settingsDialog->getUserName() + ":" + myMainw->settingsDialog->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->resultDialog->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() << "_sendRouteXml";
96
97     QString filename = "route.xml";
98     QFile file(filename);
99     if (!file.open(QFile::ReadOnly)) {
100         qDebug() << "_sendRouteXml file.open() fail";
101         return;
102     }
103
104     QUrl qurl("http://api.speedfreak-app.com/api/update/route");
105     qDebug() << qurl.toString();
106     QNetworkRequest request(qurl);
107     QNetworkReply *currentDownload;
108
109     QString credentials = myMainw->settingsDialog->getUserName() + ":" + myMainw->settingsDialog->getPassword();
110     credentials = "Basic " + credentials.toAscii().toBase64();
111     request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
112
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");
117
118     file.close();
119 }
120
121 /**
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.
126   */
127 void HttpClient::requestTopList(QString category, QString limit)
128 {
129     qDebug() << "_requestTopList" ;
130
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;
136
137     QString credentials = myMainw->settingsDialog->getUserName() + ":" + myMainw->settingsDialog->getPassword();
138     credentials = "Basic " + credentials.toAscii().toBase64();
139     request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
140
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");
145 }
146
147
148 /**
149   *@brief Request categories list from the server.
150   *Send authentication information in the header.
151   */
152 void HttpClient::requestCategories()
153 {
154     qDebug() << "_requestCategories" ;
155
156     QUrl qurl("http://api.speedfreak-app.com/api/categories/");
157     qDebug() << qurl.toString();
158     QNetworkRequest request(qurl);
159     QNetworkReply *currentDownload;
160
161     QString credentials = myMainw->settingsDialog->getUserName() + ":" + myMainw->settingsDialog->getPassword();
162     credentials = "Basic " + credentials.toAscii().toBase64();
163     request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
164
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");
169 }
170
171
172 /**
173   *@brief Check that username and password exist on the server.
174   *Send authentication information in the header.
175   */
176 void HttpClient::checkLogin()
177 {
178     qDebug() << "_checkLogin";
179
180     QUrl qurl("http://api.speedfreak-app.com/api/login/");
181     qDebug() << qurl.toString();
182     QNetworkRequest request(qurl);
183     QNetworkReply *currentDownload;
184
185     QString credentials = myMainw->settingsDialog->getUserName() + ":" + myMainw->settingsDialog->getPassword();
186     credentials = "Basic " + credentials.toAscii().toBase64();
187     request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
188
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");
193 }
194
195
196 /**
197   *@brief React to servers responce after result has been sent.
198   */
199 void HttpClient::ackOfResult()
200 {
201     qDebug() << "_ackOfResult";
202
203     myMainw->resultDialog->setLabelInfoToUser("");
204
205     QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
206
207     QNetworkReply::NetworkError errorcode;
208     errorcode = reply->error();
209     if(errorcode != 0) {
210         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
211         QMessageBox::about(myMainw->resultDialog, "Server reply to result sending ",reply->errorString());
212     }
213     else {
214         qDebug() << "errorcode:" << errorcode << reply->errorString();
215         //QMessageBox::about(myMainw->resultDialog, "Server reply to result sending", "Result received " + reply->readAll());
216     }
217 }
218
219 /**
220   *@brief React to servers responce after route has been sent.
221   */
222 void HttpClient::ackOfRoute()
223 {
224     qDebug() << "_ackOfRoute";
225
226     myMainw->routeDialog->setLabelInfoToUser("");
227
228     QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
229
230     QNetworkReply::NetworkError errorcode;
231     errorcode = reply->error();
232     if(errorcode != 0) {
233         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
234         QMessageBox::about(myMainw->routeDialog, "Server reply to route sending ",reply->errorString());
235     }
236     else {
237         qDebug() << "errorcode:" << errorcode << reply->errorString();
238         //QMessageBox::about(myMainw, "Server reply to route sending", "Route received " + reply->readAll());
239     }
240 }
241
242 /**
243   *@brief React to servers responce after registration has been sent.
244   *@todo Implement consequencies of reply.
245   */
246 void HttpClient::ackOfRegistration()
247 {
248     qDebug() << "_ackOfRegistration";
249
250     myMainw->settingsDialog->setLabelInfoToUser("");
251
252     QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
253
254     QNetworkReply::NetworkError errorcode;
255     errorcode = reply->error();
256     if(errorcode != 0) {
257         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
258         QMessageBox::about(myMainw->settingsDialog, "Server reply to registration",reply->readAll());
259     }
260     else {
261         qDebug() << "errorcode=0" << errorcode << reply->errorString();
262         //QMessageBox::about(myMainw->settingsDialog, "Server reply to registration", "User registration " + reply->readAll());
263     }
264 }
265
266
267 /**
268   *@brief React to servers responce after request for categories has been sent.
269   */
270 void HttpClient::ackOfCategories()
271 {
272     qDebug() << "_ackOfCategories";
273
274     myMainw->topResultDialog->setLabelInfoToUser("");
275
276     QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
277     myXmlreader->xmlReadCategories(reply);
278
279     QNetworkReply::NetworkError errorcode;
280     errorcode = reply->error();
281     if(errorcode != 0) {
282         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
283         QMessageBox::about(myMainw->topResultDialog, "Server reply to requesting categories",reply->errorString());
284     }
285     else {
286         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
287         //QMessageBox::about(myMainw->topResultDialog, "Server reply to requesting categories ", "OK");
288     }
289 }
290
291
292 /**
293   *@brief React to servers responce after request of TopList in certain category has been sent.
294   */
295 void HttpClient::ackOfLogin()
296 {
297     qDebug() << "_ackOffLogin";
298
299     myMainw->settingsDialog->setLabelInfoToUser("");
300
301     QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
302
303     QNetworkReply::NetworkError errorcode;
304     errorcode = reply->error();
305     if(errorcode != 0) {
306         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
307         QMessageBox::about(myMainw->settingsDialog, "Server does not recognize your username. Please registrate.",reply->errorString());
308     }
309     else {
310         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
311         //QMessageBox::about(myMainw->settingsDialog, "Server reply to login", "User login " + reply->readAll());
312     }
313
314     myMainw->settingsDialog->close();
315 }
316
317
318 /**
319   *@brief Reports errors, when server has sent error signal.
320   */
321 void HttpClient::errorFromServer(QNetworkReply::NetworkError errorcode)
322 {
323     qDebug() << "_errorFromServer";
324
325     //myMainw->setLabelInfoToUser("");
326
327     QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
328
329     if(errorcode != 0) {
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());
333     }
334     else {
335         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
336         qDebug() << reply->readAll();
337     }
338 }
339
340
341 /**
342   *@brief React to servers responce after request of TopList in certain category has been sent.
343   */
344 void HttpClient::ackOfToplist()
345 {
346     qDebug() << "_ackOfToplist";
347
348     //myMainw->setLabelInfoToUser("");
349
350     QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
351     myXmlreader->xmlReadTop10Results(reply);
352
353     QNetworkReply::NetworkError errorcode;
354     errorcode = reply->error();
355     if(errorcode != 0) {
356         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
357         QMessageBox::about(myMainw->topResultDialog, "Server reply to requesting top 10 list",reply->errorString());
358     }
359     else {
360         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
361         //QMessageBox::about(myMainw->topResultDialog, "Server reply to requesting top 10 list", "OK " + reply->readAll());
362     }
363 }
364