Settings dialog changed. Log in/Log out functionality improved.
[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
84     //Indicating user
85     myMainw->accstart->accRealTimeDialog->resultDialog->setLabelInfoToUser("Sending result to server");
86
87     xmlbuffer->close();
88 }
89
90 /**
91   *@brief Sends route to the server in xml format.
92   *Send authentication information in the header.
93   *@todo Check destination URL.
94   */
95 void HttpClient::sendRouteXml()
96 {
97     qDebug() << "_sendRouteXml";
98
99     QString filename = "route.xml";
100     QFile file(filename);
101     if (!file.open(QFile::ReadOnly)) {
102         qDebug() << "_sendRouteXml file.open() fail";
103         return;
104     }
105
106     QUrl qurl("http://api.speedfreak-app.com/api/update/route");
107     qDebug() << qurl.toString();
108     QNetworkRequest request(qurl);
109     QNetworkReply *currentDownload;
110
111     QString credentials = myMainw->settingsDialog->getUserName() + ":" + myMainw->settingsDialog->getPassword();
112     credentials = "Basic " + credentials.toAscii().toBase64();
113     request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
114
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->routeSaveDialog->routeDialog->setLabelInfoToUser("Sending route to server");
119
120     file.close();
121 }
122
123 /**
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.
128   */
129 void HttpClient::requestTopList(QString category, QString limit)
130 {
131     qDebug() << "_requestTopList" ;
132
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;
138
139     QString credentials = myMainw->settingsDialog->getUserName() + ":" + myMainw->settingsDialog->getPassword();
140     credentials = "Basic " + credentials.toAscii().toBase64();
141     request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
142
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");
147 }
148
149
150 /**
151   *@brief Request categories list from the server.
152   *Send authentication information in the header.
153   */
154 void HttpClient::requestCategories()
155 {
156     qDebug() << "_requestCategories" ;
157
158     QUrl qurl("http://api.speedfreak-app.com/api/categories/");
159     qDebug() << qurl.toString();
160     QNetworkRequest request(qurl);
161     QNetworkReply *currentDownload;
162
163     QString credentials = myMainw->settingsDialog->getUserName() + ":" + myMainw->settingsDialog->getPassword();
164     credentials = "Basic " + credentials.toAscii().toBase64();
165     request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
166
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");
171 }
172
173
174 /**
175   *@brief Check that username and password exist on the server.
176   *Send authentication information in the header.
177   */
178 void HttpClient::checkLogin()
179 {
180     qDebug() << "_checkLogin";
181
182     QUrl qurl("http://api.speedfreak-app.com/api/login/");
183     qDebug() << qurl.toString();
184     QNetworkRequest request(qurl);
185     QNetworkReply *currentDownload;
186
187     QString credentials = myMainw->settingsDialog->getUserName() + ":" + myMainw->settingsDialog->getPassword();
188     credentials = "Basic " + credentials.toAscii().toBase64();
189     request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
190
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");
195 }
196
197
198 /**
199   *@brief React to servers responce after result has been sent.
200   */
201 void HttpClient::ackOfResult()
202 {
203     qDebug() << "_ackOfResult";
204
205     //Indicating user
206     myMainw->accstart->accRealTimeDialog->resultDialog->setLabelInfoToUser("");
207
208     QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
209
210     QNetworkReply::NetworkError errorcode;
211     errorcode = reply->error();
212     if(errorcode != 0) {
213         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
214
215         //Indicating user
216         QMessageBox::about(myMainw->accstart->accRealTimeDialog->resultDialog, "Server reply to result sending ",reply->errorString());
217     }
218     else {
219         qDebug() << "errorcode:" << errorcode << reply->errorString();
220
221         //Indicating user
222         QMessageBox::about(myMainw->accstart->accRealTimeDialog->resultDialog, "Server reply to result sending", "Result received " + reply->readAll());
223     }
224 }
225
226 /**
227   *@brief React to servers responce after route has been sent.
228   */
229 void HttpClient::ackOfRoute()
230 {
231     qDebug() << "_ackOfRoute";
232
233     myMainw->routeSaveDialog->routeDialog->setLabelInfoToUser("");
234
235     QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
236
237     QNetworkReply::NetworkError errorcode;
238     errorcode = reply->error();
239     if(errorcode != 0) {
240         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
241         QMessageBox::about(myMainw->routeSaveDialog->routeDialog, "Server reply to route sending ",reply->errorString());
242     }
243     else {
244         qDebug() << "errorcode:" << errorcode << reply->errorString();
245         QMessageBox::about(myMainw, "Server reply to route sending", "Route received " + reply->readAll());
246     }
247 }
248
249 /**
250   *@brief React to servers responce after registration has been sent.
251   *@todo Implement consequencies of reply.
252   */
253 void HttpClient::ackOfRegistration()
254 {
255     qDebug() << "_ackOfRegistration";
256
257     myMainw->settingsDialog->setLabelInfoToUser("");
258
259     QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
260
261     QNetworkReply::NetworkError errorcode;
262     errorcode = reply->error();
263     if(errorcode != 0) {
264         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
265         QMessageBox::about(myMainw->settingsDialog, "Server reply to registration",reply->readAll());
266     }
267     else {
268         qDebug() << "errorcode=0" << errorcode << reply->errorString();
269         QMessageBox::about(myMainw->settingsDialog, "Server reply to registration", "User registration " + reply->readAll());
270     }
271 }
272
273
274 /**
275   *@brief React to servers responce after request for categories has been sent.
276   */
277 void HttpClient::ackOfCategories()
278 {
279     qDebug() << "_ackOfCategories";
280
281     myMainw->topResultDialog->setLabelInfoToUser("");
282
283     QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
284     myXmlreader->xmlReadCategories(reply);
285
286     QNetworkReply::NetworkError errorcode;
287     errorcode = reply->error();
288     if(errorcode != 0) {
289         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
290         //QMessageBox::about(myMainw->topResultDialog, "Server reply to requesting categories",reply->errorString());
291     }
292     else {
293         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
294         //QMessageBox::about(myMainw->topResultDialog, "Server reply to requesting categories ", "OK");
295     }
296 }
297
298
299 /**
300   *@brief React to servers responce after request of TopList in certain category has been sent.
301   */
302 void HttpClient::ackOfLogin()
303 {
304     qDebug() << "_ackOffLogin";
305
306     myMainw->settingsDialog->setLabelInfoToUser("");
307
308     QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
309
310     QNetworkReply::NetworkError errorcode;
311     errorcode = reply->error();
312     if(errorcode != 0) {
313         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
314         QMessageBox::about(myMainw->settingsDialog, "Server does not recognize your username. Please registrate.",reply->errorString());
315         myMainw->settingsDialog->usernameOk(false);
316     }
317     else {
318         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
319         QMessageBox::about(myMainw->settingsDialog, "Server reply to login", "User login " + reply->readAll());
320         // here signal emit to mainwindow for username setting to main panel
321         emit loginOK();
322         myMainw->settingsDialog->usernameOk(true);
323         myMainw->settingsDialog->close();    
324     }
325
326
327 }
328
329
330 /**
331   *@brief Reports errors, when server has sent error signal.
332   */
333 void HttpClient::errorFromServer(QNetworkReply::NetworkError errorcode)
334 {
335     qDebug() << "_errorFromServer";
336
337     //myMainw->setLabelInfoToUser("");
338
339     QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
340
341     if(errorcode != 0) {
342         qDebug() <<  "errorcode:" << errorcode;
343         //Note that errors are already reported on other each functions for server communication
344         //QMessageBox::about(myMainw, "Server reported an error", reply->errorString());
345     }
346     else {
347         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
348         qDebug() << reply->readAll();
349     }
350 }
351
352
353 /**
354   *@brief React to servers responce after request of TopList in certain category has been sent.
355   */
356 void HttpClient::ackOfToplist()
357 {
358     qDebug() << "_ackOfToplist";
359
360     //myMainw->setLabelInfoToUser("");
361
362     QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
363     myXmlreader->xmlReadTop10Results(reply);
364
365     QNetworkReply::NetworkError errorcode;
366     errorcode = reply->error();
367     if(errorcode != 0) {
368         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
369         //QMessageBox::about(myMainw->topResultDialog, "Server reply to requesting top 10 list",reply->errorString());
370     }
371     else {
372         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
373         //QMessageBox::about(myMainw->topResultDialog, "Server reply to requesting top 10 list", "OK " + reply->readAll());
374     }
375 }
376