3 #include "httpclient.h"
4 #include "carmainwindow.h"
8 *@brief Constructor, connects object to GUI
9 *@param Pointer to carmainwindow, which is temporarily used during development
11 HttpClient::HttpClient(CarMainWindow *myCarw)
14 netManager = new QNetworkAccessManager();
15 myXmlwriter = new XmlWriter();
16 myXmlreader = new XmlReader();
22 HttpClient::~HttpClient()
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.
32 void HttpClient::requestRegistration()
34 qDebug() << "_requestRegistration" ;
35 qDebug() << myMainw->myRegistration->getUserName() << "+" << myMainw->myRegistration->getPassword() << "+" << myMainw->myRegistration->getEmail();
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;
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();
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)));
58 *@brief Sends result(s) to the server in xml format.
59 *Send authentication information in the header.
60 *@todo Read category elsewhere.
62 void HttpClient::sendResultXml(QString category)
64 qDebug() << "_sendResultXml";
66 QBuffer *xmlbuffer = new QBuffer();
68 QUrl qurl("http://api.speedfreak-app.com/api/update/" + category);
69 qDebug() << qurl.toString();
70 QNetworkRequest request(qurl);
71 QNetworkReply *currentDownload;
73 xmlbuffer->open(QBuffer::ReadWrite);
74 myXmlwriter->writeResult(xmlbuffer);
75 qDebug() << "carmainwindow: xmlbuffer->data(): " << xmlbuffer->data();
77 QString credentials = myMainw->myLogin->getUserName() + ":" + myMainw->myLogin->getPassword();
78 credentials = "Basic " + credentials.toAscii().toBase64();
79 request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
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)));
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.
94 void HttpClient::requestTopList(QString category, QString limit)
96 qDebug() << "_requestTopList" ;
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;
104 QString credentials = myMainw->myLogin->getUserName() + ":" + myMainw->myLogin->getPassword();
105 credentials = "Basic " + credentials.toAscii().toBase64();
106 request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
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)));
115 *@brief Request categories list from the server.
116 *Send authentication information in the header.
118 void HttpClient::requestCategories()
120 qDebug() << "_requestCategories" ;
122 QUrl qurl("http://api.speedfreak-app.com/api/categories/");
123 qDebug() << qurl.toString();
124 QNetworkRequest request(qurl);
125 QNetworkReply *currentDownload;
127 QString credentials = myMainw->myLogin->getUserName() + ":" + myMainw->myLogin->getPassword();
128 credentials = "Basic " + credentials.toAscii().toBase64();
129 request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
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)));
138 *@brief Check that username and password exist on the server.
139 *Send authentication information in the header.
141 void HttpClient::checkLogin()
143 qDebug() << "_checkLogin";
145 QUrl qurl("http://api.speedfreak-app.com/api/login/");
146 qDebug() << qurl.toString();
147 QNetworkRequest request(qurl);
148 QNetworkReply *currentDownload;
150 QString credentials = myMainw->myLogin->getUserName() + ":" + myMainw->myLogin->getPassword();
151 credentials = "Basic " + credentials.toAscii().toBase64();
152 request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
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)));
161 *@brief React to servers responce after result has been sent.
162 *@todo Implement consequencies of reply.
164 void HttpClient::ackOfResult()
166 qDebug() << "_ackOfResult";
168 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
170 QNetworkReply::NetworkError errorcode;
171 errorcode = reply->error();
173 qDebug() << "errorcode:" << errorcode << reply->errorString();
174 QMessageBox::about(myMainw, "Server reply to result sending ",reply->errorString());
177 qDebug() << "errorcode:" << errorcode << reply->errorString();
178 QMessageBox::about(myMainw, "Server reply to result sending", "Result received " + reply->readAll());
185 *@brief React to servers responce after registration has been sent.
186 *@todo Implement consequencies of reply.
188 void HttpClient::ackOfRegistration()
190 qDebug() << "_ackOfRegistration";
192 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
194 QNetworkReply::NetworkError errorcode;
195 errorcode = reply->error();
197 qDebug() << "errorcode:" << errorcode << reply->errorString();
198 QMessageBox::about(myMainw, "Server reply to registration",reply->readAll());
201 qDebug() << "errorcode=0" << errorcode << reply->errorString();
202 QMessageBox::about(myMainw, "Server reply to registration", "User registration " + reply->readAll());
209 *@brief React to servers responce after request for categories has been sent.
211 void HttpClient::ackOfCategories()
213 qDebug() << "_ackOfCategories";
215 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
216 myXmlreader->xmlReadCategories(reply);
218 QNetworkReply::NetworkError errorcode;
219 errorcode = reply->error();
221 qDebug() << "errorcode:" << errorcode << reply->errorString();
222 QMessageBox::about(myMainw, "Server reply to requesting categories",reply->errorString());
225 qDebug() << "errorcode:" << errorcode << reply->errorString();
226 QMessageBox::about(myMainw, "Server reply to requesting categories ", "OK");
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.
236 void HttpClient::ackOfLogin()
238 qDebug() << "_ackOffLogin";
240 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
242 QNetworkReply::NetworkError errorcode;
243 errorcode = reply->error();
245 qDebug() << "errorcode:" << errorcode << reply->errorString();
246 QMessageBox::about(myMainw, "Server does not recognize your username. Please registrate.",reply->errorString());
249 qDebug() << "errorcode:" << errorcode << reply->errorString();
250 QMessageBox::about(myMainw, "Server reply to login", "User login " + reply->readAll());
256 *@brief Reports errors, when server has sent error signal.
258 void HttpClient::errorFromServer(QNetworkReply::NetworkError errorcode)
260 qDebug() << "_errorFromServer";
262 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
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());
270 qDebug() << "errorcode:" << errorcode << reply->errorString();
271 qDebug() << reply->readAll();
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.
281 void HttpClient::ackOfToplist()
283 qDebug() << "_ackOfToplist";
285 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
286 myXmlreader->xmlReadTop10Results(reply);
288 QNetworkReply::NetworkError errorcode;
289 errorcode = reply->error();
291 qDebug() << "errorcode:" << errorcode << reply->errorString();
292 QMessageBox::about(myMainw, "Server reply to requesting top 10 list",reply->errorString());
295 qDebug() << "errorcode:" << errorcode << reply->errorString();
296 QMessageBox::about(myMainw, "Server reply to requesting top 10 list", "OK " + reply->readAll());