Changed one function prototype in xmlwrite to reduce compile warnings.
welcomeDialog = new WelcomeDialog();
welcomeDialog->show();
- xmlreader = new XmlReader();
-
initComboBoxStartTabUnits();
initListViewStartTabAccelerationCategories();
myLogin = new LoginWindow(this);
+ categorylist = new CategoryList();
+ myHttpClient = new HttpClient(this);
myRegistration = new Registration(this);
- xmlwriter = new XmlWriter();
- manager = new QNetworkAccessManager(this);
connect(myRegistration,SIGNAL(sendregistration()),this,SLOT(registrate()));
- categorylist = new CategoryList();
-
time = 0;
speed = 0;
timer = new QTimer();
delete ui;
//delete result;
//delete measure;
- delete xmlreader;
- delete xmlwriter;
- delete manager;
delete categorylist;
delete welcomeDialog;
}
*/
void CarMainWindow::on_buttonTopRefresh_clicked()
{
- requestCategories();
+ myHttpClient->requestCategories();
setCategoryCompoBox();
}
int limitNr = 5; //replace with real value?
QString limit = QString::number(limitNr);
category = "acceleration-0-100"; //replace with real value from category list/top window
- requestTopList(category, limit);
+ myHttpClient->requestTopList(category, limit);
setListViewTopList(category,10);
}
}
/**
- *@brief Sends registration information to the server in xml format.
- *Reads user name, password and emaol address from resuldialogs internal variables.
- *@todo Replace msg box with better reaction to server`s responce.
- */
-void CarMainWindow::registrate()
-{
- qDebug() << "_registrate" ;
- qDebug() << this->myRegistration->getUserName() << "+" << this->myRegistration->getPassword() << "+" << this->myRegistration->getEmail();
-
- QBuffer *regbuffer = new QBuffer();
- QUrl qurl("http://api.speedfreak-app.com/api/register");
- QNetworkRequest request(qurl);
- qDebug() << qurl.toString();
- QNetworkReply *currentDownload;
-
- regbuffer->open(QBuffer::ReadWrite);
- xmlwriter->writeRegistering(regbuffer,
- this->myRegistration->getUserName(),
- this->myRegistration->getPassword(),
- this->myRegistration->getEmail());
-
- currentDownload = manager->post(request, ("xml=" + regbuffer->data()));
- qDebug() << "carmainwindow: regbuffer->data(): " << regbuffer->data();
-
- connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfRegistration()));
- //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(errorFromServer(QNetworkReply::NetworkError)));
-
- regbuffer->close();
-}
-
-
-/**
- *@brief Sends result(s) to the server in xml format.
- *Send authentication information in the header.
- *@todo Read category elsewhere.
- */
-void CarMainWindow::sendResultXml()
-{
- qDebug() << "_sendResultXml";
-
- QBuffer *xmlbuffer = new QBuffer();
- QString category_name = "acceleration-0-100"; //replace with real value from category list
-
- QUrl qurl("http://api.speedfreak-app.com/api/update/" + category_name);
- qDebug() << qurl.toString();
- QNetworkRequest request(qurl);
- QNetworkReply *currentDownload;
-
- xmlbuffer->open(QBuffer::ReadWrite);
- xmlwriter->writeResult(xmlbuffer);
- qDebug() << "carmainwindow: xmlbuffer->data(): " << xmlbuffer->data();
-
- QString credentials = this->myRegistration->getUserName() + ":" + this->myRegistration->getPassword();
- credentials = "Basic " + credentials.toAscii().toBase64();
- request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
-
- currentDownload = manager->post(request, ("xml=" + xmlbuffer->data()));
- connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfResult()));
- //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(errorFromServer(QNetworkReply::NetworkError)));
-
- xmlbuffer->close();
-}
-
-/**
- *@brief Request the Top10List of certain category from the server.
- *Send authentication information in the header.
- *@param Category of results.
- *@param Limit, the number of results.
- */
-void CarMainWindow::requestTopList(QString category, QString limit)
-{
- qDebug() << "_requestTopList" ;
-
- QString urlBase = "http://api.speedfreak-app.com/api/results/";
- QUrl qurl(urlBase + category + "/" + limit);
- qDebug() << qurl.toString();
- QNetworkRequest request(qurl);
- QNetworkReply *currentDownload;
-
- QString credentials = this->myRegistration->getUserName() + ":" + this->myRegistration->getPassword();
- credentials = "Basic " + credentials.toAscii().toBase64();
- request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
-
- currentDownload = manager->post(request, ("data=" ));
- connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfToplist()));
- //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(errorFromServer(QNetworkReply::NetworkError)));
-}
-
-
-/**
- *@brief Request categories list from the server.
- *Send authentication information in the header.
- */
-void CarMainWindow::requestCategories()
-{
- qDebug() << "_requestCategories" ;
-
- QUrl qurl("http://api.speedfreak-app.com/api/categories/");
- qDebug() << qurl.toString();
- QNetworkRequest request(qurl);
- QNetworkReply *currentDownload;
-
- QString credentials = this->myRegistration->getUserName() + ":" + this->myRegistration->getPassword();
- credentials = "Basic " + credentials.toAscii().toBase64();
- request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
-
- currentDownload = manager->post(request, ("data=" ));
- connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfCategories()));
- //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(errorFromServer(QNetworkReply::NetworkError)));
-}
-
-
-/**
- *@brief React to servers responce after result has been sent.
- *@todo Implement consequencies of reply.
- */
-void CarMainWindow::ackOfResult()
-{
- qDebug() << "_ackOfResult";
-
- QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
-
- QNetworkReply::NetworkError errorcode;
- errorcode = reply->error();
- if(errorcode != 0) {
- qDebug() << "errorcode:" << errorcode << reply->errorString();
- QMessageBox::about(this, "Server reply to result sending ",reply->errorString());
- }
- else {
- qDebug() << "errorcode:" << errorcode << reply->errorString();
- qDebug() << reply->readAll();
- }
-}
-
-
-/**
- *@brief React to servers responce after registration has been sent.
- *@todo Implement consequencies of reply.
- */
-void CarMainWindow::ackOfRegistration()
-{
- qDebug() << "_ackOfRegistration";
-
- QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
-
- QNetworkReply::NetworkError errorcode;
- errorcode = reply->error();
- if(errorcode != 0) {
- qDebug() << "errorcode:" << errorcode << reply->errorString();
- QMessageBox::about(this, "Server reply to registration",reply->readAll());
- }
- else {
- qDebug() << "errorcode=0" << errorcode << reply->errorString();
- QMessageBox::about(this, "Server reply to registration", "User registration " + reply->readAll());
- }
-}
-
-
-/**
- *@brief React to servers responce after request for categories has been sent.
- */
-void CarMainWindow::ackOfCategories()
-{
- qDebug() << "_ackOfCategories";
-
- QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
- xmlreader->xmlReadCategories(reply);
-
- QNetworkReply::NetworkError errorcode;
- errorcode = reply->error();
- if(errorcode != 0) {
- qDebug() << "errorcode:" << errorcode << reply->errorString();
- QMessageBox::about(this, "Server reply to requesting categories",reply->errorString());
- }
- else {
- qDebug() << "errorcode:" << errorcode << reply->errorString();
- qDebug() << reply->readAll();
- }
-}
-
-/**
- *@brief Reports errors, when server has sent error signal.
- */
-void CarMainWindow::errorFromServer(QNetworkReply::NetworkError errorcode)
-{
- qDebug() << "_errorFromServer";
-
- QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
-
- if(errorcode != 0) {
- qDebug() << "errorcode:" << errorcode;
- //Note that errors are already reported on other ach-functions for server communication
- //QMessageBox::about(this, "Server reported an error", reply->errorString());
- }
- else {
- qDebug() << "errorcode:" << errorcode << reply->errorString();
- qDebug() << reply->readAll();
- }
-}
-
-
-/**
- *@brief React to servers responce after request of TopList in certain category has been sent.
- *@todo Implement routing reply`s contents to UI.
- */
-void CarMainWindow::ackOfToplist()
-{
- qDebug() << "_ackOfToplist";
-
- QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
- xmlreader->xmlReadTop10Results(reply);
-
- QNetworkReply::NetworkError errorcode;
- errorcode = reply->error();
- if(errorcode != 0) {
- qDebug() << "errorcode:" << errorcode << reply->errorString();
- QMessageBox::about(this, "Server reply to requesting top 10 list",reply->errorString());
- }
- else {
- qDebug() << "errorcode:" << errorcode << reply->errorString();
- qDebug() << reply->readAll();
- }
-}
-
-
-/**
*@brief Just for development, for the real button is not shown until
*measurin started and there are results.
*@todo Implement with real code and yet leave sendXml in the bottom in use.
void CarMainWindow::on_pushButtonSendResult_clicked()
{
- sendResultXml();
+ myHttpClient->sendResultXml();
ui->pushButtonSendResult->setEnabled(false);
}
this->setWindowTitle("Speed freak");
}
}
+
+void CarMainWindow::regUserToServer()
+{
+ myHttpClient->requestRegistration();
+}
#include "measures.h"
#include "accelerometer.h"
#include "categorylist.h"
+#include "httpclient.h"
namespace Ui {
class CarMainWindow;
public:
CarMainWindow(QWidget *parent = 0);
~CarMainWindow();
+ Registration *myRegistration; //Check if this should be public or private
- void setComboBoxStartTabUnits(QStringList units); //Start-tab view
+
+ void setComboBoxStartTabUnits(QStringList units); //Start-tab view
void setListViewStartTabAccelerationCategories(QStringList numbers); //Start-tab view
- void setListViewTopList(QString category, int size); //Top-tab view
- void setCategoryCompoBox(); //Top-tab
+ void setListViewTopList(QString category, int size); //Top-tab view
+ void setCategoryCompoBox(); //Top-tab
protected:
void changeEvent(QEvent *e);
//ResultDialog *result;
//MeasureDialog *measure;
WelcomeDialog *welcomeDialog;
- XmlReader *xmlreader;
- XmlWriter *xmlwriter;
- QNetworkAccessManager* manager;
+ CategoryList *categorylist;
+ HttpClient *myHttpClient;
LoginWindow *myLogin;
- Registration *myRegistration;
- //void initCategoryCompoBox(); //
- void initComboBoxStartTabUnits(); //Start-tab view
- void initListViewStartTabAccelerationCategories(); //Start-tab view
-
+ //void initCategoryCompoBox();
+ void initComboBoxStartTabUnits(); //Start-tab view
+ void initListViewStartTabAccelerationCategories(); //Start-tab view
void initializeMeasures();
private:
- QStringList accelerationCategoriesStartTab; //Start-tab view
- QStringList units; //Start-tab view
- QStringList categories; //Top-tab view
- CategoryList *categorylist;
+ QStringList accelerationCategoriesStartTab; //Start-tab view
+ QStringList units; //Start-tab view
+ QStringList categories; //Top-tab view
QTimer *timer;
Accelerometer *accelerometer;
void on_registratePushButton_clicked();
void on_comboBoxTopCategory_activated(QString );
//void on_pushButton_clicked();
- void on_comboBoxTopCategory_currentIndexChanged(QString category); //Top-tab view
- void on_listViewStartTabAccelerationCategories_clicked(QModelIndex index); //Start-tab view
- void updateComboBoxStartTabUnits(QString unit); //Start-tab view
+ void on_comboBoxTopCategory_currentIndexChanged(QString category); //Top-tab view
+ void on_listViewStartTabAccelerationCategories_clicked(QModelIndex index); //Start-tab view
+ void updateComboBoxStartTabUnits(QString unit); //Start-tab view
void openResultView();
- void on_buttonTopRefresh_clicked(); //Top-tab view: button
- void on_autoStartButton_clicked(); //Start-tab view: button
- void registrate();
- void sendResultXml();
- void requestTopList(QString category, QString limit);
- void requestCategories();
- void ackOfResult();
- void ackOfRegistration();
- void ackOfCategories();
- void ackOfToplist();
- void errorFromServer(QNetworkReply::NetworkError);
+ void on_buttonTopRefresh_clicked(); //Top-tab view: button
+ void on_autoStartButton_clicked(); //Start-tab view: button
void after_timeout();
void updateUserName();
+ void regUserToServer();
};
#endif // CARMAINWINDOW_H
+#include <QString>
+#include <QMessageBox>
#include "httpclient.h"
+#include "carmainwindow.h"
/**
*@brief Constructor, connects object to GUI
*@param Pointer to carmainwindow, which is temporarily used during development
*/
-HttpClient::HttpClient()
+HttpClient::HttpClient(CarMainWindow *myCarw)
{
+ myMainw = myCarw;
netManager = new QNetworkAccessManager();
+ myXmlwriter = new XmlWriter();
+ myXmlreader = new XmlReader();
}
/**
{
}
+
+/**
+ *@brief Sends registration information to the server in xml format.
+ *Reads user name, password and emaol address from resuldialogs internal variables.
+ *@todo Replace msg box with better reaction to server`s responce.
+ */
+void HttpClient::requestRegistration()
+{
+ qDebug() << "_requestRegistration" ;
+ qDebug() << myMainw->myRegistration->getUserName() << "+" << myMainw->myRegistration->getPassword() << "+" << myMainw->myRegistration->getEmail();
+
+ QBuffer *regbuffer = new QBuffer();
+ QUrl qurl("http://api.speedfreak-app.com/api/register");
+ QNetworkRequest request(qurl);
+ qDebug() << qurl.toString();
+ QNetworkReply *currentDownload;
+
+ regbuffer->open(QBuffer::ReadWrite);
+ myXmlwriter->writeRegistering(regbuffer,
+ myMainw->myRegistration->getUserName(),
+ myMainw->myRegistration->getPassword(),
+ myMainw->myRegistration->getEmail());
+ qDebug() << "carmainwindow: regbuffer->data(): " << regbuffer->data();
+
+ currentDownload = netManager->post(request, ("xml=" + regbuffer->data()));
+ connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfRegistration()));
+ //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
+
+ regbuffer->close();
+}
+
+/**
+ *@brief Sends result(s) to the server in xml format.
+ *Send authentication information in the header.
+ *@todo Read category elsewhere.
+ */
+void HttpClient::sendResultXml()
+{
+ qDebug() << "_sendResultXml";
+
+ QBuffer *xmlbuffer = new QBuffer();
+ QString category_name = "acceleration-0-100"; //replace with real value from category list
+
+ QUrl qurl("http://api.speedfreak-app.com/api/update/" + category_name);
+ qDebug() << qurl.toString();
+ QNetworkRequest request(qurl);
+ QNetworkReply *currentDownload;
+
+ xmlbuffer->open(QBuffer::ReadWrite);
+ myXmlwriter->writeResult(xmlbuffer);
+ qDebug() << "carmainwindow: xmlbuffer->data(): " << xmlbuffer->data();
+
+ QString credentials = myMainw->myRegistration->getUserName() + ":" + myMainw->myRegistration->getPassword();
+ credentials = "Basic " + credentials.toAscii().toBase64();
+ request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
+
+ currentDownload = netManager->post(request, ("xml=" + xmlbuffer->data()));
+ connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfResult()));
+ //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
+
+ xmlbuffer->close();
+}
+
+/**
+ *@brief Request the Top10List of certain category from the server.
+ *Send authentication information in the header.
+ *@param Category of results.
+ *@param Limit, the number of results.
+ */
+void HttpClient::requestTopList(QString category, QString limit)
+{
+ qDebug() << "_requestTopList" ;
+
+ QString urlBase = "http://api.speedfreak-app.com/api/results/";
+ QUrl qurl(urlBase + category + "/" + limit);
+ qDebug() << qurl.toString();
+ QNetworkRequest request(qurl);
+ QNetworkReply *currentDownload;
+
+ QString credentials = myMainw->myRegistration->getUserName() + ":" + myMainw->myRegistration->getPassword();
+ credentials = "Basic " + credentials.toAscii().toBase64();
+ request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
+
+ currentDownload = netManager->post(request, ("data=" ));
+ connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfToplist()));
+ //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
+}
+
+
+/**
+ *@brief Request categories list from the server.
+ *Send authentication information in the header.
+ */
+void HttpClient::requestCategories()
+{
+ qDebug() << "_requestCategories" ;
+
+ QUrl qurl("http://api.speedfreak-app.com/api/categories/");
+ qDebug() << qurl.toString();
+ QNetworkRequest request(qurl);
+ QNetworkReply *currentDownload;
+
+ QString credentials = myMainw->myRegistration->getUserName() + ":" + myMainw->myRegistration->getPassword();
+ credentials = "Basic " + credentials.toAscii().toBase64();
+ request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
+
+ currentDownload = netManager->post(request, ("data=" ));
+ connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfCategories()));
+ //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
+}
+
+
+/**
+ *@brief React to servers responce after result has been sent.
+ *@todo Implement consequencies of reply.
+ */
+void HttpClient::ackOfResult()
+{
+ qDebug() << "_ackOfResult";
+
+ QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
+
+ QNetworkReply::NetworkError errorcode;
+ errorcode = reply->error();
+ if(errorcode != 0) {
+ qDebug() << "errorcode:" << errorcode << reply->errorString();
+ QMessageBox::about(myMainw, "Server reply to result sending ",reply->errorString());
+ }
+ else {
+ qDebug() << "errorcode:" << errorcode << reply->errorString();
+ qDebug() << reply->readAll();
+ }
+
+}
+
+
+/**
+ *@brief React to servers responce after registration has been sent.
+ *@todo Implement consequencies of reply.
+ */
+void HttpClient::ackOfRegistration()
+{
+ qDebug() << "_ackOfRegistration";
+
+ QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
+
+ QNetworkReply::NetworkError errorcode;
+ errorcode = reply->error();
+ if(errorcode != 0) {
+ qDebug() << "errorcode:" << errorcode << reply->errorString();
+ QMessageBox::about(myMainw, "Server reply to registration",reply->readAll());
+ }
+ else {
+ qDebug() << "errorcode=0" << errorcode << reply->errorString();
+ QMessageBox::about(myMainw, "Server reply to registration", "User registration " + reply->readAll());
+ }
+
+}
+
+
+/**
+ *@brief React to servers responce after request for categories has been sent.
+ */
+void HttpClient::ackOfCategories()
+{
+ qDebug() << "_ackOfCategories";
+
+ QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
+ myXmlreader->xmlReadCategories(reply);
+
+ QNetworkReply::NetworkError errorcode;
+ errorcode = reply->error();
+ if(errorcode != 0) {
+ qDebug() << "errorcode:" << errorcode << reply->errorString();
+ QMessageBox::about(myMainw, "Server reply to requesting categories",reply->errorString());
+ }
+ else {
+ qDebug() << "errorcode:" << errorcode << reply->errorString();
+ qDebug() << reply->readAll();
+ }
+
+}
+
+/**
+ *@brief Reports errors, when server has sent error signal.
+ */
+void HttpClient::errorFromServer(QNetworkReply::NetworkError errorcode)
+{
+ qDebug() << "_errorFromServer";
+
+ QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
+
+ if(errorcode != 0) {
+ qDebug() << "errorcode:" << errorcode;
+ //Note that errors are already reported on other ach-functions for server communication
+ //QMessageBox::about(myMainw, "Server reported an error", reply->errorString());
+ }
+ else {
+ qDebug() << "errorcode:" << errorcode << reply->errorString();
+ qDebug() << reply->readAll();
+ }
+
+}
+
+
+/**
+ *@brief React to servers responce after request of TopList in certain category has been sent.
+ *@todo Implement routing reply`s contents to UI.
+ */
+void HttpClient::ackOfToplist()
+{
+ qDebug() << "_ackOfToplist";
+
+ QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
+ myXmlreader->xmlReadTop10Results(reply);
+
+ QNetworkReply::NetworkError errorcode;
+ errorcode = reply->error();
+ if(errorcode != 0) {
+ qDebug() << "errorcode:" << errorcode << reply->errorString();
+ QMessageBox::about(myMainw, "Server reply to requesting top 10 list",reply->errorString());
+ }
+ else {
+ qDebug() << "errorcode:" << errorcode << reply->errorString();
+ qDebug() << reply->readAll();
+ }
+
+}
+
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
+#include "xmlwriter.h"
+#include "xmlreader.h"
+class CarMainWindow;
+
class HttpClient : public QObject
{
Q_OBJECT
public:
- HttpClient();
+ HttpClient(CarMainWindow *myCarw);
~HttpClient();
private:
+ CarMainWindow *myMainw;
QNetworkAccessManager *netManager;
+ XmlWriter *myXmlwriter;
+ XmlReader *myXmlreader;
public slots:
+ void requestRegistration();
+ void sendResultXml();
+ void requestTopList(QString category, QString limit);
+ void requestCategories();
+ void ackOfResult();
+ void ackOfRegistration();
+ void ackOfCategories();
+ void ackOfToplist();
+ void errorFromServer(QNetworkReply::NetworkError);
};
*@param email.
*@todo Replace hardcoced filename to finally GUI entry widget.
*/
-void XmlWriter::writeXml(QString usr, QString psswd, QString email)
+//void XmlWriter::writeXml(QString usr, QString psswd, QString email)
+void XmlWriter::writeXml()
{
QString filename = "xmlcategoryfile.xml";
QFile file(filename);
void writeRegistering(QBuffer *netbuf, QString usr, QString psswd, QString email);
void writeResult(QBuffer *netbuf);
bool writeXmlFile(QIODevice *device);
- void writeXml(QString usr, QString psswd, QString email);
+ //void writeXml(QString usr, QString psswd, QString email);
+ void writeXml();
void writeItems();
void serverWritesXml();