3 #include "httpclient.h"
4 #include "carmainwindow.h"
7 *@brief Constructor, connects object to GUI
8 *@param Pointer to carmainwindow, which is temporarily used during development
10 HttpClient::HttpClient(CarMainWindow *myCarw)
13 netManager = new QNetworkAccessManager();
14 myXmlwriter = new XmlWriter();
15 myXmlreader = new XmlReader();
21 HttpClient::~HttpClient()
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.
31 void HttpClient::requestRegistration()
33 qDebug() << "_requestRegistration" ;
34 qDebug() << myMainw->myRegistration->getUserName() << "+" << myMainw->myRegistration->getPassword() << "+" << myMainw->myRegistration->getEmail();
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;
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();
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)));
57 *@brief Sends result(s) to the server in xml format.
58 *Send authentication information in the header.
59 *@todo Read category elsewhere.
61 void HttpClient::sendResultXml()
63 qDebug() << "_sendResultXml";
65 QBuffer *xmlbuffer = new QBuffer();
66 QString category_name = "acceleration-0-100"; //replace with real value from category list
68 QUrl qurl("http://api.speedfreak-app.com/api/update/" + category_name);
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->myRegistration->getUserName() + ":" + myMainw->myRegistration->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->myRegistration->getUserName() + ":" + myMainw->myRegistration->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->myRegistration->getUserName() + ":" + myMainw->myRegistration->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 React to servers responce after result has been sent.
139 *@todo Implement consequencies of reply.
141 void HttpClient::ackOfResult()
143 qDebug() << "_ackOfResult";
145 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
147 QNetworkReply::NetworkError errorcode;
148 errorcode = reply->error();
150 qDebug() << "errorcode:" << errorcode << reply->errorString();
151 QMessageBox::about(myMainw, "Server reply to result sending ",reply->errorString());
154 qDebug() << "errorcode:" << errorcode << reply->errorString();
155 qDebug() << reply->readAll();
162 *@brief React to servers responce after registration has been sent.
163 *@todo Implement consequencies of reply.
165 void HttpClient::ackOfRegistration()
167 qDebug() << "_ackOfRegistration";
169 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
171 QNetworkReply::NetworkError errorcode;
172 errorcode = reply->error();
174 qDebug() << "errorcode:" << errorcode << reply->errorString();
175 QMessageBox::about(myMainw, "Server reply to registration",reply->readAll());
178 qDebug() << "errorcode=0" << errorcode << reply->errorString();
179 QMessageBox::about(myMainw, "Server reply to registration", "User registration " + reply->readAll());
186 *@brief React to servers responce after request for categories has been sent.
188 void HttpClient::ackOfCategories()
190 qDebug() << "_ackOfCategories";
192 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
193 myXmlreader->xmlReadCategories(reply);
195 QNetworkReply::NetworkError errorcode;
196 errorcode = reply->error();
198 qDebug() << "errorcode:" << errorcode << reply->errorString();
199 QMessageBox::about(myMainw, "Server reply to requesting categories",reply->errorString());
202 qDebug() << "errorcode:" << errorcode << reply->errorString();
203 qDebug() << reply->readAll();
209 *@brief Reports errors, when server has sent error signal.
211 void HttpClient::errorFromServer(QNetworkReply::NetworkError errorcode)
213 qDebug() << "_errorFromServer";
215 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
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());
223 qDebug() << "errorcode:" << errorcode << reply->errorString();
224 qDebug() << reply->readAll();
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.
234 void HttpClient::ackOfToplist()
236 qDebug() << "_ackOfToplist";
238 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
239 myXmlreader->xmlReadTop10Results(reply);
241 QNetworkReply::NetworkError errorcode;
242 errorcode = reply->error();
244 qDebug() << "errorcode:" << errorcode << reply->errorString();
245 QMessageBox::about(myMainw, "Server reply to requesting top 10 list",reply->errorString());
248 qDebug() << "errorcode:" << errorcode << reply->errorString();
249 qDebug() << reply->readAll();