Youtube video and text (draft).
[speedfreak] / Client / xmlreader.cpp
index cf86ef1..2140ca7 100644 (file)
@@ -1,59 +1,56 @@
-#include <QtGui>
-#include <QNetworkRequest>
-#include <QNetworkReply>
-#include <QIODevice>
+/*
+ * 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 <QMessageBox>
 #include <QDebug>
-#include <QDateTime>
-#include <QDate>
-#include <QTime>
-#include <QApplication>
 #include "xmlreader.h"
 
-
-/*! @brief Constructor, connects object to GUI
- */
-XmlReader::XmlReader(Ui_MainWindow* myMainWindow)
+/**
+  *Constructor of this class.
+  */
+XmlReader::XmlReader()
 {
-    ui = myMainWindow;
+    myCategoryList = new CategoryList();
 }
 
-/*! @brief Destructor
+/**
+  *Destructor of this class. Should be used to release all allocated resources.
   */
 XmlReader::~XmlReader()
 {
-
+    category = "";
+    unit = "";
+    position = "";
+    user = "";
+    value = "";
+    delete myCategoryList;
 }
 
-QString XmlReader::errorString() const
+/**
+  *This function is used to parse top 10 results of a certain category.
+  */
+void XmlReader::xmlReadTop10Results(QNetworkReply *device)
 {
-    return QObject::tr("%1\nLine %2, column %3")
-            .arg(xmlreader.errorString())
-            .arg(xmlreader.lineNumber())
-            .arg(xmlreader.columnNumber());
-}
+    qDebug() << "_xmlReadTop10Results";
 
+    int i = 0;
+    int receivedFlag = 0;
 
-/*! @brief parsing xml file that is sent by server over network.
-  * Shows received information on screen and parsed information on command line.
-  * @note Partly commented for git.
-  * @todo (Development: Seems to omit element tags and some values when reading from QFile. Why? This used
-  * earlier with QNetworkReply printed all xml contents.)
-  * @todo Remove command line output and use final GUI widget names.
-  * @todo Replace using of QFile to QNetworkReply.
-  */
-void XmlReader::xmlRead(QIODevice *device)
-//void XmlReader::xmlRead(QNetworkReply *device)
-{
-    qDebug() << "_xmlRead";
+    xmlreader.clear();
+    QByteArray array = device->readAll();
+    qDebug() << "array: " << array;
+    xmlreader.addData(array);
+    //xmlreader.addData(device->readAll());
 
-    xmlreader.addData(device->readAll());
-    //Or: View webpage contents on textEdit by adding all data to xml stream reader,
-    //since readAll() empties the buffer
-    /*QByteArray readAllArr = device->readAll();
-    ui->textEditXml->append(readAllArr);
-    xmlreader.addData(readAllArr);*/
+    if(!(myCategoryList->top10List.isEmpty())) {
+        myCategoryList->top10List.clear();
+    }
 
     //Go trough the xml document
     while(!xmlreader.atEnd())
@@ -63,75 +60,110 @@ void XmlReader::xmlRead(QIODevice *device)
         //Check if this element is starting element
         if(xmlreader.isStartElement())
         {
-            if(xmlreader.name() == "place")
+            if(xmlreader.name() == "results")
             {
-                qDebug() << xmlreader.name();
-            }
-            if(xmlreader.name() == "date")
-            {
-                 qDebug() << xmlreader.name();
-            }
-            if(xmlreader.name() == "time")
-            {
-                 qDebug() << xmlreader.name();
+                //qDebug() << xmlreader.name();
             }
             if(xmlreader.name() == "result")
             {
-                //Two consequent start elements
-                if(xmlreader.readNextStartElement())
-                {
-                    if(xmlreader.name() == "speed")
-                    {
-                    QString speed;
-                    QXmlStreamAttributes attr = xmlreader.attributes();
-                    speed = (attr.value("value").toString() + " " + attr.value("unit").toString());
-                    //ui->lineEditSpeed->setText(speed);
-                    qDebug() << xmlreader.name() << speed;
-                    }
-                }
+                //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;
             }
-            if(xmlreader.name() == "distance")
+        }
+    }
+    //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")
             {
-                QString distance;
-                QXmlStreamAttributes attr = xmlreader.attributes();
-                distance = (attr.value("value").toString() + " " + attr.value("unit").toString());
-                //ui->lineEditDist->setText(distance);
-                qDebug() << xmlreader.name() << distance;
+                //qDebug() << xmlreader.name();
             }
-            if(xmlreader.name() == "acceleration")
+            if(xmlreader.name() == "category")
             {
-                QString acceleration;
-                QXmlStreamAttributes attr = xmlreader.attributes();
-                acceleration = (attr.value("value").toString()+ " " + attr.value("unit").toString());
-                //ui->lineEditAccel->setText(acceleration);
-                qDebug() << xmlreader.name() << acceleration;
+                //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();
+    }
 }
 
-/*! @brief A function used tmp in development.
-  * @note Partly harcoded and commented for git.
+/**
+  *This function is used to read example xml file (results.xml).
+  *@todo Read real xml.
   */
 void XmlReader::xmlShow()
 {
-    //QString filename = ui->lineEditFile->text();
-    QString filename = "xmlfile.xml";
+    //QString filename = "results.xml";
+    QString filename = "xmlcategoryfile.xml";
     QFile file(filename);
 
     if (!file.open(QFile::ReadOnly))
     {
-        /*QMessageBox::warning(this->ui->centralWidget,
-                                tr("QXmlStream Bookmarks"),
-                                tr("Cannot write file %1:\n%2.")
-                                .arg(file.fileName())
-                                .arg(file.errorString()));*/
         qDebug() << "_xmlShow fail";
         return;
     }
 
-    xmlRead(&file);
-
+    //xmlReadTop10Results(&file);
+    //xmlReadCategories(&file);
     file.close();
 }
-