Added classes.
authorTiina Kivilinna-Korhola <tiina.kivilinna-korhola@fudeco.com>
Tue, 30 Mar 2010 11:47:46 +0000 (14:47 +0300)
committerTiina Kivilinna-Korhola <tiina.kivilinna-korhola@fudeco.com>
Wed, 31 Mar 2010 07:09:06 +0000 (10:09 +0300)
14 files changed:
Client/categorylist.cpp [new file with mode: 0644]
Client/categorylist.h [new file with mode: 0644]
Client/httpclient.cpp [new file with mode: 0644]
Client/httpclient.h [new file with mode: 0644]
Client/mainwindow.cpp
Client/mainwindow.h
Client/speedfreak.pro
Client/topresultdialog.cpp [new file with mode: 0644]
Client/topresultdialog.h [new file with mode: 0644]
Client/topresultdialog.ui [new file with mode: 0644]
Client/xmlreader.cpp [new file with mode: 0644]
Client/xmlreader.h [new file with mode: 0644]
Client/xmlwriter.cpp [new file with mode: 0644]
Client/xmlwriter.h [new file with mode: 0644]

diff --git a/Client/categorylist.cpp b/Client/categorylist.cpp
new file mode 100644 (file)
index 0000000..f15fde0
--- /dev/null
@@ -0,0 +1,156 @@
+/*
+ * Categorylist
+ *
+ * @author     Olavi Pulkkinen <olavi.pulkkinena@fudeco.com>
+ * @copyright  (c) 2010 Speed Freak team
+ * @license    http://opensource.org/licenses/gpl-license.php GNU Public License
+ */
+
+#include <QDebug>
+#include "categorylist.h"
+
+/**
+  *Constructor of this class.
+  */
+CategoryList::CategoryList()
+{
+
+}
+
+/**
+  *Destructor of this class. Should be used to release all allocated resources.
+  */
+CategoryList::~CategoryList()
+{
+}
+
+/**
+  *This is return function.
+  *@return QStringList categoryList
+  */
+QStringList CategoryList::getCategoryList()
+{
+    qDebug() << "_getCategoryList" << realSizeOfCats;
+
+    if(sizeOfCategoryList() != 0) {
+        clearCategoryList();
+    }
+
+    for(int i = 0; i < realSizeOfCats; i++)
+    {
+        categoryList.append(cats[i].description);
+    }
+
+    return categoryList;
+}
+
+/**
+  *Append an item in the end of the categorylist.
+  *@param Item.
+  */
+void CategoryList::appendCategoryList(QString item)
+{
+    categoryList.append(item);
+}
+
+/**
+  *Input an item into the categorylist.
+  *@param Index.
+  *@param Item to be appended.
+  */
+void CategoryList::fillCategoryList(int index, QString item)
+{
+    categoryList.insert(index, item);
+}
+
+/**
+  *Show an item of the categorylist.
+  *@param Index.
+  */
+QString CategoryList::itemOfCategoryList(int index)
+{
+    return categoryList.at(index);
+}
+
+/**
+  *Clear categorylist.
+  */
+void CategoryList::clearCategoryList()
+{
+    categoryList.clear();
+}
+
+/**
+  *Read size of categorylist.
+  */
+int CategoryList::sizeOfCategoryList()
+{
+    return categoryList.size();
+}
+
+/**
+  *Append an item in the end of the categoryelementable.
+  *@param Index.
+  *@param Description of category.
+  *@param Unit.
+  *@param Category.
+  */
+void CategoryList::appendCats(int ind, QString des, QString uni, QString cat)
+{
+    cats[ind].description = des;
+    cats[ind].unit = uni;
+    cats[ind].category = cat;
+}
+
+/**
+  *Search description for an index af cats table.
+  *@param Index.
+  */
+QString CategoryList::getRecentDescription(int ind)
+{
+    return cats[ind].description;
+}
+
+/**
+  *Search category for an index af cats table.
+  *@param Index.
+  */
+QString CategoryList::getRecentCategory(int ind)
+{
+    return cats[ind].category;
+}
+
+/**
+  *Clear cats.
+  */
+void CategoryList::clearCats()
+{
+    for(int i = 0; i < 20; i++)
+    {
+        cats[i].description.clear();
+        cats[i].unit.clear();
+        cats[i].category.clear();
+    }
+}
+
+/**
+  *This function is used to get items to top list of the category that is chosen from combobox.
+  *@param QString category
+  *@param int size
+  */
+QString CategoryList::getTopList( QString category, int size)
+{
+    qDebug() << "_getTopList";
+
+    if(!(top10List.isEmpty()))
+    {
+        return top10List;
+    }
+    else
+    {
+        QString emptyStr("");
+        qDebug() << "_getTopList: Category not found";
+        return emptyStr;
+    }
+}
+
diff --git a/Client/categorylist.h b/Client/categorylist.h
new file mode 100644 (file)
index 0000000..60d60ad
--- /dev/null
@@ -0,0 +1,46 @@
+/*
+ * Categorylist
+ *
+ * @author     Olavi Pulkkinen <olavi.pulkkinena@fudeco.com>
+ * @author     Tiina Kivilinna-Korhola <tiina.kivilinna-korhola@fudeco.com>
+ * @copyright  (c) 2010 Speed Freak team
+ * @license    http://opensource.org/licenses/gpl-license.php GNU Public License
+ */
+
+#ifndef CATEGORYLIST_H
+#define CATEGORYLIST_H
+
+#include <QStringList>
+
+class CategoryList : public QObject
+{
+public:
+    CategoryList();
+    ~CategoryList();
+    QString top10List;
+    int realSizeOfCats;
+
+    QStringList getCategoryList();
+    void fillCategoryList(int index, QString item);
+    void appendCategoryList(QString item);
+    QString itemOfCategoryList(int index);
+    QString getTopList( QString category, int size);
+    void clearCategoryList();
+    int sizeOfCategoryList();
+    void appendCats(int ind, QString des, QString uni, QString cat);
+    void clearCats();
+    QString getRecentDescription(int i);
+    QString getRecentCategory(int ind);
+
+private:
+    QStringList categoryList;   //Stores categories. categoryList is routed to UI.
+    typedef struct {
+        QString category;       //name of category variable
+        QString description;    //verbal description of category
+        QString unit;           //km/h, miles/h
+    } categoryElements;
+    categoryElements cats[20];
+
+};
+
+#endif // CATEGORYLIST_H
diff --git a/Client/httpclient.cpp b/Client/httpclient.cpp
new file mode 100644 (file)
index 0000000..984a0db
--- /dev/null
@@ -0,0 +1,362 @@
+#include <QString>
+#include <QMessageBox>
+#include "httpclient.h"
+#include "mainwindow.h"
+
+
+/**
+  *@brief Constructor, connects object to GUI
+  *@param Pointer to carmainwindow, which is temporarily used during development
+  */
+HttpClient::HttpClient(MainWindow *myCarw)
+{
+    myMainw = myCarw;
+    netManager = new QNetworkAccessManager();
+    myXmlwriter = new XmlWriter();
+    myXmlreader = new XmlReader();
+}
+
+/**
+  *@brief Destructor
+  */
+HttpClient::~HttpClient()
+{
+
+}
+
+/**
+  *@brief Sends registration information to the server in xml format.
+  *Reads user name, password and emaol address from resuldialogs internal variables.
+  */
+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)));
+    myMainw->setLabelInfoToUser("Reguesting registration from server");
+
+    regbuffer->close();
+}
+
+/**
+  *@brief Sends result(s) to the server in xml format.
+  *Send authentication information in the header.
+  */
+void HttpClient::sendResultXml(QString category, double result)
+{
+    qDebug() << "_sendResultXml";
+
+    QBuffer *xmlbuffer = new QBuffer();
+
+    QUrl qurl("http://api.speedfreak-app.com/api/update/" + category);
+    qDebug() << qurl.toString();
+    QNetworkRequest request(qurl);
+    QNetworkReply *currentDownload;
+
+    xmlbuffer->open(QBuffer::ReadWrite);
+    myXmlwriter->writeResult(xmlbuffer, result);
+    qDebug() << "carmainwindow: xmlbuffer->data(): " << xmlbuffer->data();
+
+    QString credentials = myMainw->myLogin->getUserName() + ":" + myMainw->myLogin->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)));
+    myMainw->setLabelInfoToUser("Sending result to server");
+
+    xmlbuffer->close();
+}
+
+/**
+  *@brief Sends route to the server in xml format.
+  *Send authentication information in the header.
+  *@todo Check destination URL.
+  */
+void HttpClient::sendRouteXml()
+{
+    qDebug() << "_sendRouteXml";
+
+    QString filename = "route.xml";
+    QFile file(filename);
+    if (!file.open(QFile::ReadOnly)) {
+        qDebug() << "_sendRouteXml file.open() fail";
+        return;
+    }
+
+    QUrl qurl("http://api.speedfreak-app.com/api/update/route");
+    qDebug() << qurl.toString();
+    QNetworkRequest request(qurl);
+    QNetworkReply *currentDownload;
+
+    QString credentials = myMainw->myLogin->getUserName() + ":" + myMainw->myLogin->getPassword();
+    credentials = "Basic " + credentials.toAscii().toBase64();
+    request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
+
+    currentDownload = netManager->post(request, ("xml=" + file.readAll()));
+    connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfRoute()));
+    //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
+    myMainw->setLabelInfoToUser("Sending route to server");
+
+    file.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->myLogin->getUserName() + ":" + myMainw->myLogin->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)));
+    myMainw->setLabelInfoToUser("Reguesting top10 list from server");
+}
+
+
+/**
+  *@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->myLogin->getUserName() + ":" + myMainw->myLogin->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)));
+    myMainw->setLabelInfoToUser("Reguesting categories from server");
+}
+
+
+/**
+  *@brief Check that username and password exist on the server.
+  *Send authentication information in the header.
+  */
+void HttpClient::checkLogin()
+{
+    qDebug() << "_checkLogin";
+
+    QUrl qurl("http://api.speedfreak-app.com/api/login/");
+    qDebug() << qurl.toString();
+    QNetworkRequest request(qurl);
+    QNetworkReply *currentDownload;
+
+    QString credentials = myMainw->myLogin->getUserName() + ":" + myMainw->myLogin->getPassword();
+    credentials = "Basic " + credentials.toAscii().toBase64();
+    request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
+
+    currentDownload = netManager->post(request, ("data=" ));
+    connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfLogin()));
+    //connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),myMainw,SLOT(errorFromServer(QNetworkReply::NetworkError)));
+    myMainw->setLabelInfoToUser("Checking login validity from server");
+}
+
+
+/**
+  *@brief React to servers responce after result has been sent.
+  */
+void HttpClient::ackOfResult()
+{
+    qDebug() << "_ackOfResult";
+
+    myMainw->setLabelInfoToUser("");
+
+    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();
+        QMessageBox::about(myMainw, "Server reply to result sending", "Result received " + reply->readAll());
+    }
+}
+
+/**
+  *@brief React to servers responce after route has been sent.
+  */
+void HttpClient::ackOfRoute()
+{
+    qDebug() << "_ackOfRoute";
+
+    myMainw->setLabelInfoToUser("");
+
+    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 route sending ",reply->errorString());
+    }
+    else {
+        qDebug() << "errorcode:" << errorcode << reply->errorString();
+        QMessageBox::about(myMainw, "Server reply to route sending", "Route received " + reply->readAll());
+    }
+}
+
+/**
+  *@brief React to servers responce after registration has been sent.
+  *@todo Implement consequencies of reply.
+  */
+void HttpClient::ackOfRegistration()
+{
+    qDebug() << "_ackOfRegistration";
+
+    myMainw->setLabelInfoToUser("");
+
+    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";
+
+    myMainw->setLabelInfoToUser("");
+
+    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();
+        QMessageBox::about(myMainw, "Server reply to requesting categories ", "OK");
+    }
+}
+
+
+/**
+  *@brief React to servers responce after request of TopList in certain category has been sent.
+  */
+void HttpClient::ackOfLogin()
+{
+    qDebug() << "_ackOffLogin";
+
+    myMainw->setLabelInfoToUser("");
+
+    QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
+
+    QNetworkReply::NetworkError errorcode;
+    errorcode = reply->error();
+    if(errorcode != 0) {
+        qDebug() <<  "errorcode:" << errorcode << reply->errorString();
+        QMessageBox::about(myMainw, "Server does not recognize your username. Please registrate.",reply->errorString());
+    }
+    else {
+        qDebug() <<  "errorcode:" << errorcode << reply->errorString();
+        QMessageBox::about(myMainw, "Server reply to login", "User login " + reply->readAll());
+    }
+}
+
+
+/**
+  *@brief Reports errors, when server has sent error signal.
+  */
+void HttpClient::errorFromServer(QNetworkReply::NetworkError errorcode)
+{
+    qDebug() << "_errorFromServer";
+
+    myMainw->setLabelInfoToUser("");
+
+    QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
+
+    if(errorcode != 0) {
+        qDebug() <<  "errorcode:" << errorcode;
+        //Note that errors are already reported on other each 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.
+  */
+void HttpClient::ackOfToplist()
+{
+    qDebug() << "_ackOfToplist";
+
+    myMainw->setLabelInfoToUser("");
+
+    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();
+        QMessageBox::about(myMainw, "Server reply to requesting top 10 list", "OK " + reply->readAll());
+    }
+}
+
diff --git a/Client/httpclient.h b/Client/httpclient.h
new file mode 100644 (file)
index 0000000..672a5a3
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * Http client Connects application to server.
+ *
+ * @author     Tiina Kivilinna-Korhola
+ * @copyright  (c) 2010 Speed Freak team
+ * license     http://opensource.org/licenses/gpl-license.php GNU Public License
+ */
+
+#ifndef HTTPCLIENT_H
+#define HTTPCLIENT_H
+
+#include <QNetworkAccessManager>
+#include <QNetworkRequest>
+#include <QNetworkReply>
+#include "xmlwriter.h"
+#include "xmlreader.h"
+class MainWindow;
+
+
+
+class HttpClient : public QObject {
+    Q_OBJECT
+public:
+    HttpClient(MainWindow *myCarw);
+    ~HttpClient();
+    XmlWriter *myXmlwriter;
+    XmlReader *myXmlreader;
+
+private:
+    MainWindow *myMainw;
+    QNetworkAccessManager *netManager;
+
+public slots:
+    void requestRegistration();
+    void checkLogin();
+    void sendResultXml(QString category, double result);
+    void sendRouteXml();
+    void requestTopList(QString category, QString limit);
+    void requestCategories();
+    void ackOfResult();
+    void ackOfRoute();
+    void ackOfRegistration();
+    void ackOfCategories();
+    void ackOfToplist();
+    void ackOfLogin();
+    void errorFromServer(QNetworkReply::NetworkError);
+
+};
+
+#endif // HTTPCLIENT_H
index 6ea222a..a304463 100644 (file)
@@ -12,6 +12,7 @@
 #include <QDesktopServices>
 #include <QUrl>
 #include <QSettings>
 #include <QDesktopServices>
 #include <QUrl>
 #include <QSettings>
+#include <QDebug>
 
 MainWindow::MainWindow(QWidget *parent) :
     QMainWindow(parent),
 
 MainWindow::MainWindow(QWidget *parent) :
     QMainWindow(parent),
@@ -26,8 +27,13 @@ MainWindow::MainWindow(QWidget *parent) :
     creditsDialog = new CreditsDialog;
     routeSaveDialog = new RouteSaveDialog;
     settingsDialog = new SettingsDialog;
     creditsDialog = new CreditsDialog;
     routeSaveDialog = new RouteSaveDialog;
     settingsDialog = new SettingsDialog;
+    topResultDialog = new TopResultDialog;
+    connect(topResultDialog, SIGNAL(refreshCategoryList()), this, SLOT(clientRequestCategoryList()));
+    connect(topResultDialog, SIGNAL(refreshTopList(int)), this, SLOT(clientRequestTopList(int)));
     accstart = NULL;
 
     accstart = NULL;
 
+    httpClient = new HttpClient(this);
+
     welcomeDialog = new WelcomeDialog;
     welcomeDialog->show();
 }
     welcomeDialog = new WelcomeDialog;
     welcomeDialog->show();
 }
@@ -80,3 +86,26 @@ void MainWindow::on_pushButtonAccelerate_clicked()
         accstart = new accelerationstart(this);
     accstart->show();
 }
         accstart = new accelerationstart(this);
     accstart->show();
 }
+
+void MainWindow::on_pushButtonResults_clicked()
+{
+    topResultDialog->show();
+}
+
+/**
+  *This slot function is called when ever mytTopResultDialog emits signal refreshCategoryList button clicked.
+  */
+void MainWindow::clientRequestCategoryList()
+{
+    httpClient->requestCategories();
+}
+
+/**
+  *This slot function is called when ever mytTopResultDialog emits signal refreshTopList button clicked.
+  */
+void MainWindow::clientRequestTopList(int index)
+{
+    qDebug() << "index" << index << httpClient->myXmlreader->myCategoryList->getRecentCategory(index);
+    QString limit = QString::number(topResultDialog->getLimitNr());
+    httpClient->requestTopList(httpClient->myXmlreader->myCategoryList->getRecentCategory(index), limit);
+}
index 14ab094..8bd9d8c 100644 (file)
 #define MAINWINDOW_H
 
 #include <QMainWindow>
 #define MAINWINDOW_H
 
 #include <QMainWindow>
+#include <QNetworkAccessManager>
+#include <QNetworkRequest>
+#include <QNetworkReply>
+#include <QMessageBox>
 #include "creditsdialog.h"
 #include "routesavedialog.h"
 #include "welcomedialog.h"
 #include "settingsdialog.h"
 #include "accelerationstart.h"
 #include "creditsdialog.h"
 #include "routesavedialog.h"
 #include "welcomedialog.h"
 #include "settingsdialog.h"
 #include "accelerationstart.h"
+#include "topresultdialog.h"
+#include "httpclient.h"
 
 namespace Ui {
     class MainWindow;
 
 namespace Ui {
     class MainWindow;
@@ -31,6 +37,8 @@ public:
     WelcomeDialog *welcomeDialog;
     SettingsDialog *settingsDialog;
     accelerationstart* accstart;
     WelcomeDialog *welcomeDialog;
     SettingsDialog *settingsDialog;
     accelerationstart* accstart;
+    TopResultDialog *topResultDialog;
+    HttpClient *httpClient;
 
 protected:
     void changeEvent(QEvent *e);
 
 protected:
     void changeEvent(QEvent *e);
@@ -39,11 +47,14 @@ private:
     Ui::MainWindow *ui;
 
 private slots:
     Ui::MainWindow *ui;
 
 private slots:
+    void on_pushButtonResults_clicked();
     void on_pushButtonAccelerate_clicked();
     void on_pushButtonSettings_clicked();
     void on_pushButtonRoute_clicked();
     void on_pushButtonCredits_clicked();
     void on_pushButtonWWW_clicked();
     void on_pushButtonAccelerate_clicked();
     void on_pushButtonSettings_clicked();
     void on_pushButtonRoute_clicked();
     void on_pushButtonCredits_clicked();
     void on_pushButtonWWW_clicked();
+    void clientRequestCategoryList();
+    void clientRequestTopList(int index);
 };
 
 #endif // MAINWINDOW_H
 };
 
 #endif // MAINWINDOW_H
index 6ea2e2e..1a3253b 100644 (file)
@@ -1,7 +1,9 @@
 # -------------------------------------------------
 # Project created by QtCreator 2010-03-29T09:21:42
 # -------------------------------------------------
 # -------------------------------------------------
 # Project created by QtCreator 2010-03-29T09:21:42
 # -------------------------------------------------
-QT += dbus
+QT += dbus \
+    network \
+    xml
 TARGET = speedfreak
 TEMPLATE = app
 SOURCES += main.cpp \
 TARGET = speedfreak
 TEMPLATE = app
 SOURCES += main.cpp \
@@ -19,7 +21,12 @@ SOURCES += main.cpp \
     accelerometer.cpp \
     movingaverage.cpp \
     calculate.cpp \
     accelerometer.cpp \
     movingaverage.cpp \
     calculate.cpp \
-    calibratedialog.cpp
+    calibratedialog.cpp \
+    topresultdialog.cpp \
+    categorylist.cpp \
+    httpclient.cpp \
+    xmlreader.cpp \
+    xmlwriter.cpp
 HEADERS += mainwindow.h \
     creditsdialog.h \
     routedialog.h \
 HEADERS += mainwindow.h \
     creditsdialog.h \
     routedialog.h \
@@ -34,7 +41,12 @@ HEADERS += mainwindow.h \
     accelerometer.h \
     movingaverage.h \
     calculate.h \
     accelerometer.h \
     movingaverage.h \
     calculate.h \
-    calibratedialog.h
+    calibratedialog.h \
+    topresultdialog.h \
+    categorylist.h \
+    httpclient.h \
+    xmlreader.h \
+    xmlwriter.h
 FORMS += mainwindow.ui \
     creditsdialog.ui \
     routedialog.ui \
 FORMS += mainwindow.ui \
     creditsdialog.ui \
     routedialog.ui \
@@ -45,5 +57,6 @@ FORMS += mainwindow.ui \
     settingsdialog.ui \
     accelerationstartdialog.ui \
     accrealtimedialog.ui \
     settingsdialog.ui \
     accelerationstartdialog.ui \
     accrealtimedialog.ui \
-    calibratedialog.ui
+    calibratedialog.ui \
+    topresultdialog.ui
 RESOURCES += graphics.qrc
 RESOURCES += graphics.qrc
diff --git a/Client/topresultdialog.cpp b/Client/topresultdialog.cpp
new file mode 100644 (file)
index 0000000..ef4478b
--- /dev/null
@@ -0,0 +1,73 @@
+#include <QDebug>
+#include "topresultdialog.h"
+#include "ui_topresultdialog.h"
+
+TopResultDialog::TopResultDialog(QWidget *parent) :
+    QDialog(parent),
+    ui(new Ui::TopResultDialog)
+{
+    ui->setupUi(this);
+    this->setWindowTitle("Top Results");
+
+    //Set the amount of requested top results here, untill there is user input
+    setLimitNr(10);
+}
+
+TopResultDialog::~TopResultDialog()
+{
+    delete ui;
+}
+
+void TopResultDialog::changeEvent(QEvent *e)
+{
+    QDialog::changeEvent(e);
+    switch (e->type()) {
+    case QEvent::LanguageChange:
+        ui->retranslateUi(this);
+        break;
+    default:
+        break;
+    }
+}
+
+
+void TopResultDialog::on_buttonTopRefresh_clicked()
+{
+    emit refreshCategoryList();
+}
+
+void TopResultDialog::on_pushButton_debclose_clicked()
+{
+    close();
+}
+
+void TopResultDialog::setCompoBoxCategories(QStringList list)
+{
+    ui->comboBoxTopCategory->addItems(list);
+}
+
+void TopResultDialog::showTopList(QString str)
+{
+    ui->labelTopList->setText(str);
+}
+
+int TopResultDialog::getRecentCategoryIndex()
+{
+    return recentCategoryIndex;
+}
+
+void TopResultDialog::setLimitNr(int number)
+{
+    limitNr = number;
+}
+
+int TopResultDialog::getLimitNr()
+{
+    return limitNr;
+}
+
+void TopResultDialog::on_comboBoxTopCategory_currentIndexChanged(int index)
+{
+    recentCategoryIndex = index;
+    emit refreshTopList(index);
+}
diff --git a/Client/topresultdialog.h b/Client/topresultdialog.h
new file mode 100644 (file)
index 0000000..a46b916
--- /dev/null
@@ -0,0 +1,40 @@
+#ifndef TOPRESULTDIALOG_H
+#define TOPRESULTDIALOG_H
+
+#include <QDialog>
+
+namespace Ui {
+    class TopResultDialog;
+}
+
+class TopResultDialog : public QDialog {
+    Q_OBJECT
+public:
+    TopResultDialog(QWidget *parent = 0);
+    ~TopResultDialog();
+    void setCompoBoxCategories(QStringList list);
+    void showTopList(QString str);
+    int getRecentCategoryIndex();
+    int getLimitNr();
+    void setLimitNr(int number);
+
+protected:
+    void changeEvent(QEvent *e);
+
+signals:
+    void refreshCategoryList();
+    void refreshTopList(int index);
+
+private:
+    Ui::TopResultDialog *ui;
+    int recentCategoryIndex;
+    int limitNr;
+
+private slots:
+    void on_comboBoxTopCategory_currentIndexChanged(int index);
+    void on_pushButton_debclose_clicked();
+    void on_buttonTopRefresh_clicked();
+
+};
+
+#endif // TOPRESULTDIALOG_H
diff --git a/Client/topresultdialog.ui b/Client/topresultdialog.ui
new file mode 100644 (file)
index 0000000..8a77ac7
--- /dev/null
@@ -0,0 +1,122 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
+ <class>TopResultDialog</class>
+ <widget class="QDialog" name="TopResultDialog">
+  <property name="geometry">
+   <rect>
+    <x>0</x>
+    <y>0</y>
+    <width>800</width>
+    <height>480</height>
+   </rect>
+  </property>
+  <property name="windowTitle">
+   <string>Dialog</string>
+  </property>
+  <widget class="QPushButton" name="buttonTopRefresh">
+   <property name="geometry">
+    <rect>
+     <x>10</x>
+     <y>70</y>
+     <width>221</width>
+     <height>71</height>
+    </rect>
+   </property>
+   <property name="font">
+    <font>
+     <family>Bitstream Charter</family>
+     <pointsize>16</pointsize>
+     <weight>75</weight>
+     <bold>true</bold>
+    </font>
+   </property>
+   <property name="text">
+    <string>Refresh list</string>
+   </property>
+  </widget>
+  <widget class="QListView" name="listViewTopList">
+   <property name="geometry">
+    <rect>
+     <x>300</x>
+     <y>0</y>
+     <width>471</width>
+     <height>261</height>
+    </rect>
+   </property>
+   <property name="font">
+    <font>
+     <family>Bitstream Charter</family>
+     <pointsize>10</pointsize>
+    </font>
+   </property>
+   <property name="flow">
+    <enum>QListView::LeftToRight</enum>
+   </property>
+  </widget>
+  <widget class="QLabel" name="labelTopList">
+   <property name="geometry">
+    <rect>
+     <x>320</x>
+     <y>0</y>
+     <width>431</width>
+     <height>261</height>
+    </rect>
+   </property>
+   <property name="font">
+    <font>
+     <pointsize>9</pointsize>
+    </font>
+   </property>
+   <property name="text">
+    <string/>
+   </property>
+   <property name="alignment">
+    <set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
+   </property>
+  </widget>
+  <widget class="QPushButton" name="pushButton_debclose">
+   <property name="geometry">
+    <rect>
+     <x>10</x>
+     <y>20</y>
+     <width>91</width>
+     <height>27</height>
+    </rect>
+   </property>
+   <property name="text">
+    <string>PushButtondebclose</string>
+   </property>
+  </widget>
+  <widget class="QLabel" name="labelCategory">
+   <property name="geometry">
+    <rect>
+     <x>10</x>
+     <y>210</y>
+     <width>221</width>
+     <height>51</height>
+    </rect>
+   </property>
+   <property name="font">
+    <font>
+     <family>Bitstream Charter</family>
+     <pointsize>16</pointsize>
+    </font>
+   </property>
+   <property name="text">
+    <string>Categories:</string>
+   </property>
+  </widget>
+  <widget class="QComboBox" name="comboBoxTopCategory">
+   <property name="geometry">
+    <rect>
+     <x>10</x>
+     <y>269</y>
+     <width>601</width>
+     <height>41</height>
+    </rect>
+   </property>
+  </widget>
+ </widget>
+ <resources/>
+ <connections/>
+</ui>
diff --git a/Client/xmlreader.cpp b/Client/xmlreader.cpp
new file mode 100644 (file)
index 0000000..8c75c97
--- /dev/null
@@ -0,0 +1,170 @@
+/*
+ * Parse xml file
+ *
+ * @author     Toni Jussila <toni.jussila@fudeco.com>
+ * @author     Tiina Kivilinna-Korhola <tiina.kivilinna-korhola@fudeco.com>
+ * @copyright  (c) 2010 Speed Freak team
+ * @license    http://opensource.org/licenses/gpl-license.php GNU Public License
+ */
+
+#include <QFile>
+#include <QDebug>
+#include "xmlreader.h"
+
+/**
+  *Constructor of this class.
+  */
+XmlReader::XmlReader()
+{
+    myCategoryList = new CategoryList();
+}
+
+/**
+  *Destructor of this class. Should be used to release all allocated resources.
+  */
+XmlReader::~XmlReader()
+{
+    category = "";
+    unit = "";
+    position = "";
+    user = "";
+    value = "";
+    delete myCategoryList;
+}
+
+/**
+  *This function is used to parse top 10 results of a certain category.
+  */
+void XmlReader::xmlReadTop10Results(QNetworkReply *device)
+{
+    qDebug() << "_xmlReadTop10Results";
+
+    int i = 0;
+    int receivedFlag = 0;
+
+    xmlreader.clear();
+    QByteArray array = device->readAll();
+    qDebug() << "array: " << array;
+    xmlreader.addData(array);
+    //xmlreader.addData(device->readAll());
+
+    if(!(myCategoryList->top10List.isEmpty())) {
+        myCategoryList->top10List.clear();
+    }
+
+    //Go trough the xml document
+    while(!xmlreader.atEnd())
+    {
+        //Read next node
+        xmlreader.readNext();
+        //Check if this element is starting element
+        if(xmlreader.isStartElement())
+        {
+            if(xmlreader.name() == "results")
+            {
+                //qDebug() << xmlreader.name();
+            }
+            if(xmlreader.name() == "result")
+            {
+                //qDebug() << xmlreader.name();
+                attr = xmlreader.attributes();
+
+                user = attr.value("username").toString();
+                position = attr.value("position").toString();
+                date = attr.value("date").toString();
+                unit = attr.value("unit").toString();
+                value = attr.value("value").toString();
+
+                myCategoryList->top10List.append(position + "\t" +
+                                                user + "\t" +
+                                                value + " " +
+                                                unit + "\t" +
+                                                date + "\n");
+
+                //qDebug() << position << user << value << unit << date;
+                i++;
+                receivedFlag = 1;
+            }
+        }
+    }
+    //Only change labelTopList if a new top10List has been received
+    if(receivedFlag)
+    {
+        qDebug() << "receivedTop10List() emitted";
+        emit receivedTop10List();
+    }
+}
+
+void XmlReader::xmlReadCategories(QNetworkReply *device)
+//void XmlReader::xmlReadCategories(QIODevice *device)
+{
+    qDebug() << "_xmlReadCategories";
+
+    int i = 0;
+    int receivedFlag = 0;
+
+    xmlreader.clear();
+    QByteArray array = device->readAll();
+    qDebug() << "array: " << array;
+    xmlreader.addData(array);
+    //xmlreader.addData(device->readAll());
+
+    myCategoryList->clearCats();
+
+    //Go trough the xml document
+    while(!xmlreader.atEnd())
+    {
+        //Read next node
+        xmlreader.readNext();
+
+        //Check if this element is starting element
+        if(xmlreader.isStartElement())
+        {
+            if(xmlreader.name() == "categories")
+            {
+                //qDebug() << xmlreader.name();
+            }
+            if(xmlreader.name() == "category")
+            {
+                //qDebug() << xmlreader.name();
+                attr = xmlreader.attributes();
+                description = attr.value("description").toString();
+                unit = attr.value("unit").toString();
+                category = xmlreader.readElementText();
+                myCategoryList->appendCats(i, description, unit, category);
+                //qDebug() << "description: " << description << "unit: " << unit << "category: " << category;
+                i++;
+                receivedFlag = 1;
+            }
+        }
+    }
+    //Only change comboBoxTopCategory if a new list has been received
+    if(receivedFlag)
+    {
+        qDebug() << "receivedCategoryList() emitted";
+        myCategoryList->realSizeOfCats = i;
+        emit receivedCategoryList();
+    }
+}
+
+/**
+  *This function is used to read example xml file (results.xml).
+  *@todo Read real xml.
+  */
+void XmlReader::xmlShow()
+{
+    //QString filename = "results.xml";
+    QString filename = "xmlcategoryfile.xml";
+    QFile file(filename);
+
+    if (!file.open(QFile::ReadOnly))
+    {
+        qDebug() << "_xmlShow fail";
+        return;
+    }
+
+    //xmlReadTop10Results(&file);
+    //xmlReadCategories(&file);
+    file.close();
+}
+
diff --git a/Client/xmlreader.h b/Client/xmlreader.h
new file mode 100644 (file)
index 0000000..4a751a3
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+ * Parse xml file
+ *
+ * @author     Toni Jussila <toni.jussila@fudeco.com>
+ * @author     Tiina Kivilinna-Korhola <tiina.kivilinna-korhola@fudeco.com>
+ * @copyright  (c) 2010 Speed Freak team
+ * @license    http://opensource.org/licenses/gpl-license.php GNU Public License
+ */
+
+#ifndef XMLREADER_H
+#define XMLREADER_H
+
+#include <QXmlStreamReader>
+#include <QStringList>
+#include <QNetworkReply>
+#include "categorylist.h"
+
+class XmlReader : public QObject {
+    Q_OBJECT
+public:
+    XmlReader();
+    ~XmlReader();
+    CategoryList *myCategoryList;
+
+private:
+    QXmlStreamReader xmlreader;
+
+    QXmlStreamAttributes attr;
+    QString category;
+    QString unit;
+    QString date;
+    QString position;
+    QString user;
+    QString value;
+    QString description;
+
+signals:
+    void receivedCategoryList();
+    void receivedTop10List();
+
+public slots:
+    void xmlReadTop10Results(QNetworkReply *device);
+    void xmlReadCategories(QNetworkReply *device);
+    //void xmlReadCategories(QIODevice *device);
+    void xmlShow();
+};
+
+#endif // XMLREADER_H
diff --git a/Client/xmlwriter.cpp b/Client/xmlwriter.cpp
new file mode 100644 (file)
index 0000000..7fca219
--- /dev/null
@@ -0,0 +1,175 @@
+/*
+ * Xml writer
+ *
+ * @author     Tiina Kivilinna-Korhola
+ * @copyright  (c) 2010 Speed Freak team
+ * license     http://opensource.org/licenses/gpl-license.php GNU Public License
+ */
+
+#include "xmlwriter.h"
+
+
+
+/**
+  *@brief Constructor, connects object to GUI
+  *@param Pointer to carmainwindow, which is temporarily used during development
+  */
+XmlWriter::XmlWriter()
+{
+
+}
+
+/**
+  *@brief Destructor
+  */
+XmlWriter::~XmlWriter()
+{
+
+}
+
+/**
+  *@brief Writes registration items into tags.
+  *@param netbuf a buffer where xmlstreamwriter writes to.
+  *@param usr for user name.
+  *@param psswd for password.
+  *@param email.
+  */
+void XmlWriter::writeRegistering(QBuffer *netbuf, QString usr, QString psswd, QString email)
+{
+    qDebug() << "_writeRegistering";
+
+    xmlwriter.setDevice(netbuf);
+
+    xmlwriter.writeStartDocument();
+    xmlwriter.writeStartElement("user");
+
+    xmlwriter.writeStartElement("login");
+    xmlwriter.writeCharacters(usr);
+    xmlwriter.writeEndElement();
+
+    xmlwriter.writeStartElement("password");
+    xmlwriter.writeCharacters(psswd);
+    xmlwriter.writeEndElement();
+
+    xmlwriter.writeStartElement("email");
+    xmlwriter.writeCharacters(email);
+    xmlwriter.writeEndElement();
+
+    xmlwriter.writeEndElement();
+    xmlwriter.writeEndDocument();
+}
+
+
+/**
+  *@brief Writes Speed Freek results items as tags and contents into a buffer.
+  *@todo Consider looping when writing many values.
+  *@todo Replace test value to finally used variables.
+  */
+void XmlWriter::writeResult(QBuffer *netbuf, double result)
+{
+    qDebug() << "_writeResult";
+
+    xmlwriter.setDevice(netbuf);
+
+    xmlwriter.writeStartDocument();
+    xmlwriter.writeStartElement("result");
+    xmlwriter.writeAttribute("value", QString::number(result));
+    xmlwriter.writeEndElement();
+    xmlwriter.writeEndDocument();
+}
+
+
+/**
+  *@brief Write track to server.
+  *@param netbuf where to write.
+  *@param counter is GPSData::roundCounter.
+  *@todo Decide suitable attributes.
+  */
+void XmlWriter::writeGpsTrack(QBuffer *netbuf, int counter, int start, int stop, int lat, int lon, int alt, int speed, int time)
+{
+    qDebug() << "_writeGpsTrack";
+
+    double *ptrValue;
+    //ptrValue = ptrTable;
+    double tmp = 0;
+
+    xmlwriter.setDevice(netbuf);
+
+    xmlwriter.writeStartDocument();
+
+    xmlwriter.writeStartElement("Route");
+    xmlwriter.writeAttribute("starttime", QDateTime::currentDateTime().toString());
+    xmlwriter.writeAttribute("endtime", QDateTime::currentDateTime().toString());
+    xmlwriter.writeAttribute("points", QDateTime::currentDateTime().toString());
+    for(int i = 0; i < counter; i++)
+    {
+        xmlwriter.writeStartElement("point");
+        xmlwriter.writeAttribute("lat", QString::number(lat));
+        xmlwriter.writeAttribute("lon", QString::number(lon));
+        xmlwriter.writeAttribute("alt", QString::number(alt));
+        xmlwriter.writeAttribute("speed", QString::number(speed));
+        xmlwriter.writeAttribute("time", QString::number(time));
+        xmlwriter.writeEndElement();
+    }
+    xmlwriter.writeEndElement();
+    xmlwriter.writeEndDocument();
+}
+
+
+/**
+  *@brief Opens and closes a file, when xml information is written into a file,
+  *and passes file to writeXmlFile()
+  *@param usr for user name.
+  *@param psswd for password.
+  *@param email.
+  *@todo Replace hardcoced filename to finally GUI entry widget.
+  */
+//void XmlWriter::writeXml(QString usr, QString psswd, QString email)
+void XmlWriter::writeXml()
+{
+    QString filename = "xmlcategoryfile.xml";
+    QFile file(filename);
+    if (!file.open(QFile::WriteOnly | QFile::Text)) {
+        qDebug() << "_xmlWrite fail";
+        return;
+    }
+
+    writeXmlFile(&file);
+    //writeRegistering(&file, usr, psswd, email);
+    //writeResult(&file);
+    file.close();
+}
+
+/**
+  *@brief Writes general xml information.
+  *Calls other functions to insert login and result information.
+  *@param device: target of writing, here filename.
+  *@param usr for user name.
+  *@param psswd for password.
+  *@param email.
+  */
+bool XmlWriter::writeXmlFile(QIODevice *device)
+{
+    xmlwriter.setDevice(device);
+    xmlwriter.writeStartDocument();
+    writeItems();
+    xmlwriter.writeEndDocument();
+
+    return true;
+}
+
+
+/**
+  *@brief Writes Speed Freek results items as tags and contents to earlier defined target.
+  *@todo Consider looping when writing many values.
+  *@todo Replace testing values to finally used variabls.
+  */
+void XmlWriter::writeItems()
+{
+    xmlwriter.writeStartElement("result");
+    xmlwriter.writeAttribute("value", QString::number(14)); //tmp testing value
+    xmlwriter.writeAttribute("unit", "seconds");
+    xmlwriter.writeAttribute("date", QDateTime::currentDateTime().toString());
+    xmlwriter.writeEndElement();
+}
+
diff --git a/Client/xmlwriter.h b/Client/xmlwriter.h
new file mode 100644 (file)
index 0000000..ac60175
--- /dev/null
@@ -0,0 +1,47 @@
+/*
+ * Xml writer
+ *
+ * @author     Tiina Kivilinna-Korhola
+ * @copyright  (c) 2010 Speed Freak team
+ * license     http://opensource.org/licenses/gpl-license.php GNU Public License
+ */
+
+#ifndef XMLWRITER_H
+#define XMLWRITER_H
+
+#include <QXmlStreamWriter>
+#include <QBuffer>
+#include <QtGui>
+#include <QNetworkRequest>
+#include <QIODevice>
+#include <QFile>
+#include <QMessageBox>
+#include <QDebug>
+#include <QDateTime>
+#include <QDate>
+#include <QTime>
+#include <QByteArray>
+#include <QApplication>
+
+
+class XmlWriter : public QObject {
+    Q_OBJECT
+public:
+    XmlWriter();
+    ~XmlWriter();
+
+private:
+    QXmlStreamWriter xmlwriter;
+
+public slots:
+    void writeRegistering(QBuffer *netbuf, QString usr, QString psswd, QString email);
+    void writeResult(QBuffer *netbuf, double result);
+    void writeGpsTrack(QBuffer *netbuf, int counter, int start, int stop, int lat, int lon, int alt, int speed, int time);
+    bool writeXmlFile(QIODevice *device);
+    //void writeXml(QString usr, QString psswd, QString email);
+    void writeXml();
+    void writeItems();
+};
+
+#endif // XMLWRITER_H
+