ea44e7a676cc3936830f915412444de220068523
[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 React to servers responce after result has been sent.
139   *@todo Implement consequencies of reply.
140   */
141 void HttpClient::ackOfResult()
142 {
143     qDebug() << "_ackOfResult";
144
145     QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
146
147     QNetworkReply::NetworkError errorcode;
148     errorcode = reply->error();
149     if(errorcode != 0) {
150         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
151         QMessageBox::about(myMainw, "Server reply to result sending ",reply->errorString());
152     }
153     else {
154         qDebug() << "errorcode:" << errorcode << reply->errorString();
155         qDebug() << reply->readAll();
156     }
157
158 }
159
160
161 /**
162   *@brief React to servers responce after registration has been sent.
163   *@todo Implement consequencies of reply.
164   */
165 void HttpClient::ackOfRegistration()
166 {
167     qDebug() << "_ackOfRegistration";
168
169     QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
170
171     QNetworkReply::NetworkError errorcode;
172     errorcode = reply->error();
173     if(errorcode != 0) {
174         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
175         QMessageBox::about(myMainw, "Server reply to registration",reply->readAll());
176     }
177     else {
178         qDebug() << "errorcode=0" << errorcode << reply->errorString();
179         QMessageBox::about(myMainw, "Server reply to registration", "User registration " + reply->readAll());
180     }
181
182 }
183
184
185 /**
186   *@brief React to servers responce after request for categories has been sent.
187   */
188 void HttpClient::ackOfCategories()
189 {
190     qDebug() << "_ackOfCategories";
191
192     QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
193     myXmlreader->xmlReadCategories(reply);
194
195     QNetworkReply::NetworkError errorcode;
196     errorcode = reply->error();
197     if(errorcode != 0) {
198         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
199         QMessageBox::about(myMainw, "Server reply to requesting categories",reply->errorString());
200     }
201     else {
202         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
203         qDebug() << reply->readAll();
204     }
205
206 }
207
208 /**
209   *@brief Reports errors, when server has sent error signal.
210   */
211 void HttpClient::errorFromServer(QNetworkReply::NetworkError errorcode)
212 {
213     qDebug() << "_errorFromServer";
214
215     QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
216
217     if(errorcode != 0) {
218         qDebug() <<  "errorcode:" << errorcode;
219         //Note that errors are already reported on other ach-functions for server communication
220         //QMessageBox::about(myMainw, "Server reported an error", reply->errorString());
221     }
222     else {
223         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
224         qDebug() << reply->readAll();
225     }
226
227 }
228
229
230 /**
231   *@brief React to servers responce after request of TopList in certain category has been sent.
232   *@todo Implement routing reply`s contents to UI.
233   */
234 void HttpClient::ackOfToplist()
235 {
236     qDebug() << "_ackOfToplist";
237
238     QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
239     myXmlreader->xmlReadTop10Results(reply);
240
241     QNetworkReply::NetworkError errorcode;
242     errorcode = reply->error();
243     if(errorcode != 0) {
244         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
245         QMessageBox::about(myMainw, "Server reply to requesting top 10 list",reply->errorString());
246     }
247     else {
248         qDebug() <<  "errorcode:" << errorcode << reply->errorString();
249         qDebug() << reply->readAll();
250     }
251
252 }
253