Added fuction for checking username registration on the server.
[speedfreak] / Client / httpclient.cpp
1 #include <QString>
2 #include <QMessageBox>
3 #include "httpclient.h"
4 #include "carmainwindow.h"
5
6 /**
7   *@brief Constructor, connects object to GUI
8   *@param Pointer to carmainwindow, which is temporarily used during development
9   */
10 HttpClient::HttpClient(CarMainWindow *myCarw)
11 {
12     myMainw = myCarw;
13     netManager = new QNetworkAccessManager();
14     myXmlwriter = new XmlWriter();
15     myXmlreader = new XmlReader();
16 }
17
18 /**
19   *@brief Destructor
20   */
21 HttpClient::~HttpClient()
22 {
23
24 }
25
26 /**
27   *@brief Sends registration information to the server in xml format.
28   *Reads user name, password and emaol address from resuldialogs internal variables.
29   *@todo Replace msg box with better reaction to server`s responce.
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
53     regbuffer->close();
54 }
55
56 /**
57   *@brief Sends result(s) to the server in xml format.
58   *Send authentication information in the header.
59   *@todo Read category elsewhere.
60   */
61 void HttpClient::sendResultXml()
62 {
63     qDebug() << "_sendResultXml";
64
65     QBuffer *xmlbuffer = new QBuffer();
66     QString category_name = "acceleration-0-100";    //replace with real value from category list
67
68     QUrl qurl("http://api.speedfreak-app.com/api/update/" + category_name);
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->myRegistration->getUserName() + ":" + myMainw->myRegistration->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->myRegistration->getUserName() + ":" + myMainw->myRegistration->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->myRegistration->getUserName() + ":" + myMainw->myRegistration->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->myRegistration->getUserName() + ":" + myMainw->myRegistration->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         qDebug() << 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         qDebug() << reply->readAll();
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     myXmlreader->xmlReadTop10Results(reply);
242
243     QNetworkReply::NetworkError errorcode;
244     errorcode = reply->error();
245     if(errorcode != 0) {
246         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
247         QMessageBox::about(myMainw, "Server does not recognize your username. Please registrate.",reply->errorString());
248     }
249     else {
250         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
251         QMessageBox::about(myMainw, "Server reply to login", "User login " + reply->readAll());
252     }
253 }
254
255
256 /**
257   *@brief Reports errors, when server has sent error signal.
258   */
259 void HttpClient::errorFromServer(QNetworkReply::NetworkError errorcode)
260 {
261     qDebug() << "_errorFromServer";
262
263     QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
264
265     if(errorcode != 0) {
266         qDebug() <<  "errorcode:" << errorcode;
267         //Note that errors are already reported on other ach-functions for server communication
268         //QMessageBox::about(myMainw, "Server reported an error", reply->errorString());
269     }
270     else {
271         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
272         qDebug() << reply->readAll();
273     }
274
275 }
276
277
278 /**
279   *@brief React to servers responce after request of TopList in certain category has been sent.
280   *@todo Implement routing reply`s contents to UI.
281   */
282 void HttpClient::ackOfToplist()
283 {
284     qDebug() << "_ackOfToplist";
285
286     QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
287     myXmlreader->xmlReadTop10Results(reply);
288
289     QNetworkReply::NetworkError errorcode;
290     errorcode = reply->error();
291     if(errorcode != 0) {
292         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
293         QMessageBox::about(myMainw, "Server reply to requesting top 10 list",reply->errorString());
294     }
295     else {
296         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
297         qDebug() << reply->readAll();
298     }
299
300 }
301