Routing of server results on GUI.
[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   *@todo Read category elsewhere.
61   */
62 void HttpClient::sendResultXml(QString category)
63 {
64     qDebug() << "_sendResultXml";
65
66     QBuffer *xmlbuffer = new QBuffer();
67
68     QUrl qurl("http://api.speedfreak-app.com/api/update/" + category);
69     qDebug() << qurl.toString();
70     QNetworkRequest request(qurl);
71     QNetworkReply *currentDownload;
72
73     xmlbuffer->open(QBuffer::ReadWrite);
74     myXmlwriter->writeResult(xmlbuffer);
75     qDebug() << "carmainwindow: xmlbuffer->data(): " << xmlbuffer->data();
76
77     QString credentials = myMainw->myLogin->getUserName() + ":" + myMainw->myLogin->getPassword();
78     credentials = "Basic " + credentials.toAscii().toBase64();
79     request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
80
81     currentDownload = netManager->post(request, ("xml=" + xmlbuffer->data()));
82     connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfResult()));
83     //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
84
85     xmlbuffer->close();
86 }
87
88 /**
89   *@brief Request the Top10List of certain category from the server.
90   *Send authentication information in the header.
91   *@param Category of results.
92   *@param Limit, the number of results.
93   */
94 void HttpClient::requestTopList(QString category, QString limit)
95 {
96     qDebug() << "_requestTopList" ;
97
98     QString urlBase = "http://api.speedfreak-app.com/api/results/";
99     QUrl qurl(urlBase + category + "/" + limit);
100     qDebug() << qurl.toString();
101     QNetworkRequest request(qurl);
102     QNetworkReply *currentDownload;
103
104     QString credentials = myMainw->myLogin->getUserName() + ":" + myMainw->myLogin->getPassword();
105     credentials = "Basic " + credentials.toAscii().toBase64();
106     request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
107
108     currentDownload = netManager->post(request, ("data=" ));
109     connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfToplist()));
110     //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
111 }
112
113
114 /**
115   *@brief Request categories list from the server.
116   *Send authentication information in the header.
117   */
118 void HttpClient::requestCategories()
119 {
120     qDebug() << "_requestCategories" ;
121
122     QUrl qurl("http://api.speedfreak-app.com/api/categories/");
123     qDebug() << qurl.toString();
124     QNetworkRequest request(qurl);
125     QNetworkReply *currentDownload;
126
127     QString credentials = myMainw->myLogin->getUserName() + ":" + myMainw->myLogin->getPassword();
128     credentials = "Basic " + credentials.toAscii().toBase64();
129     request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
130
131     currentDownload = netManager->post(request, ("data=" ));
132     connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfCategories()));
133     //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
134 }
135
136
137 /**
138   *@brief Check that username and password exist on the server.
139   *Send authentication information in the header.
140   */
141 void HttpClient::checkLogin()
142 {
143     qDebug() << "_checkLogin";
144
145     QUrl qurl("http://api.speedfreak-app.com/api/login/");
146     qDebug() << qurl.toString();
147     QNetworkRequest request(qurl);
148     QNetworkReply *currentDownload;
149
150     QString credentials = myMainw->myLogin->getUserName() + ":" + myMainw->myLogin->getPassword();
151     credentials = "Basic " + credentials.toAscii().toBase64();
152     request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
153
154     currentDownload = netManager->post(request, ("data=" ));
155     connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfLogin()));
156     //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
157 }
158
159
160 /**
161   *@brief React to servers responce after result has been sent.
162   *@todo Implement consequencies of reply.
163   */
164 void HttpClient::ackOfResult()
165 {
166     qDebug() << "_ackOfResult";
167
168     QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
169
170     QNetworkReply::NetworkError errorcode;
171     errorcode = reply->error();
172     if(errorcode != 0) {
173         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
174         QMessageBox::about(myMainw, "Server reply to result sending ",reply->errorString());
175     }
176     else {
177         qDebug() << "errorcode:" << errorcode << reply->errorString();
178         QMessageBox::about(myMainw, "Server reply to result sending", "Result received " + reply->readAll());
179     }
180
181 }
182
183
184 /**
185   *@brief React to servers responce after registration has been sent.
186   *@todo Implement consequencies of reply.
187   */
188 void HttpClient::ackOfRegistration()
189 {
190     qDebug() << "_ackOfRegistration";
191
192     QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
193
194     QNetworkReply::NetworkError errorcode;
195     errorcode = reply->error();
196     if(errorcode != 0) {
197         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
198         QMessageBox::about(myMainw, "Server reply to registration",reply->readAll());
199     }
200     else {
201         qDebug() << "errorcode=0" << errorcode << reply->errorString();
202         QMessageBox::about(myMainw, "Server reply to registration", "User registration " + reply->readAll());
203     }
204
205 }
206
207
208 /**
209   *@brief React to servers responce after request for categories has been sent.
210   */
211 void HttpClient::ackOfCategories()
212 {
213     qDebug() << "_ackOfCategories";
214
215     QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
216     myXmlreader->xmlReadCategories(reply);
217
218     QNetworkReply::NetworkError errorcode;
219     errorcode = reply->error();
220     if(errorcode != 0) {
221         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
222         QMessageBox::about(myMainw, "Server reply to requesting categories",reply->errorString());
223     }
224     else {
225         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
226         QMessageBox::about(myMainw, "Server reply to requesting categories ", "OK");
227     }
228
229 }
230
231
232 /**
233   *@brief React to servers responce after request of TopList in certain category has been sent.
234   *@todo Implement routing reply`s contents to UI.
235   */
236 void HttpClient::ackOfLogin()
237 {
238     qDebug() << "_ackOffLogin";
239
240     QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
241
242     QNetworkReply::NetworkError errorcode;
243     errorcode = reply->error();
244     if(errorcode != 0) {
245         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
246         QMessageBox::about(myMainw, "Server does not recognize your username. Please registrate.",reply->errorString());
247     }
248     else {
249         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
250         QMessageBox::about(myMainw, "Server reply to login", "User login " + reply->readAll());
251     }
252 }
253
254
255 /**
256   *@brief Reports errors, when server has sent error signal.
257   */
258 void HttpClient::errorFromServer(QNetworkReply::NetworkError errorcode)
259 {
260     qDebug() << "_errorFromServer";
261
262     QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
263
264     if(errorcode != 0) {
265         qDebug() <<  "errorcode:" << errorcode;
266         //Note that errors are already reported on other ach-functions for server communication
267         //QMessageBox::about(myMainw, "Server reported an error", reply->errorString());
268     }
269     else {
270         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
271         qDebug() << reply->readAll();
272     }
273
274 }
275
276
277 /**
278   *@brief React to servers responce after request of TopList in certain category has been sent.
279   *@todo Implement routing reply`s contents to UI.
280   */
281 void HttpClient::ackOfToplist()
282 {
283     qDebug() << "_ackOfToplist";
284
285     QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
286     myXmlreader->xmlReadTop10Results(reply);
287
288     QNetworkReply::NetworkError errorcode;
289     errorcode = reply->error();
290     if(errorcode != 0) {
291         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
292         QMessageBox::about(myMainw, "Server reply to requesting top 10 list",reply->errorString());
293     }
294     else {
295         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
296         QMessageBox::about(myMainw, "Server reply to requesting top 10 list", "OK " + reply->readAll());
297     }
298
299 }
300