Corrected route drawing dialog to use mxl-file reading route points.
[speedfreak] / Client / xmlreader.cpp
1 /*
2  * Parse xml file
3  *
4  * @author     Toni Jussila <toni.jussila@fudeco.com>
5  * @author     Tiina Kivilinna-Korhola <tiina.kivilinna-korhola@fudeco.com>
6  * @copyright  (c) 2010 Speed Freak team
7  * @license    http://opensource.org/licenses/gpl-license.php GNU Public License
8  */
9
10 #include <QFile>
11 #include <QDebug>
12 #include "xmlreader.h"
13
14 /**
15   *Constructor of this class.
16   */
17 XmlReader::XmlReader()
18 {
19     myCategoryList = new CategoryList();
20 }
21
22 /**
23   *Destructor of this class. Should be used to release all allocated resources.
24   */
25 XmlReader::~XmlReader()
26 {
27     category = "";
28     unit = "";
29     position = "";
30     user = "";
31     value = "";
32     delete myCategoryList;
33 }
34
35 /**
36   *This function is used to parse top 10 results of a certain category.
37   */
38 void XmlReader::xmlReadTop10Results(QNetworkReply *device)
39 {
40     qDebug() << "_xmlReadTop10Results";
41
42     int i = 0;
43     int receivedFlag = 0;
44
45     xmlreader.clear();
46     QByteArray array = device->readAll();
47     qDebug() << "array: " << array;
48     xmlreader.addData(array);
49     //xmlreader.addData(device->readAll());
50
51     if(!(myCategoryList->top10List.isEmpty())) {
52         myCategoryList->top10List.clear();
53     }
54
55     //Go trough the xml document
56     while(!xmlreader.atEnd())
57     {
58         //Read next node
59         xmlreader.readNext();
60         //Check if this element is starting element
61         if(xmlreader.isStartElement())
62         {
63             if(xmlreader.name() == "results")
64             {
65                 //qDebug() << xmlreader.name();
66             }
67             if(xmlreader.name() == "result")
68             {
69                 //qDebug() << xmlreader.name();
70                 attr = xmlreader.attributes();
71
72                 user = attr.value("username").toString();
73                 position = attr.value("position").toString();
74                 date = attr.value("date").toString();
75                 unit = attr.value("unit").toString();
76                 value = attr.value("value").toString();
77
78                 myCategoryList->top10List.append(position + "\t" +
79                                                 user + "\t" +
80                                                 value + " " +
81                                                 unit + "\t" +
82                                                 date + "\n");
83
84                 //qDebug() << position << user << value << unit << date;
85                 i++;
86                 receivedFlag = 1;
87             }
88         }
89     }
90     //Only change labelTopList if a new top10List has been received
91     if(receivedFlag)
92     {
93         qDebug() << "receivedTop10List() emitted";
94         emit receivedTop10List();
95     }
96 }
97
98 void XmlReader::xmlReadCategories(QNetworkReply *device)
99 //void XmlReader::xmlReadCategories(QIODevice *device)
100 {
101     qDebug() << "_xmlReadCategories";
102
103     int i = 0;
104     int receivedFlag = 0;
105
106     xmlreader.clear();
107     QByteArray array = device->readAll();
108     qDebug() << "array: " << array;
109     xmlreader.addData(array);
110     //xmlreader.addData(device->readAll());
111
112     myCategoryList->clearCats();
113
114     //Go trough the xml document
115     while(!xmlreader.atEnd())
116     {
117         //Read next node
118         xmlreader.readNext();
119
120         //Check if this element is starting element
121         if(xmlreader.isStartElement())
122         {
123             if(xmlreader.name() == "categories")
124             {
125                 //qDebug() << xmlreader.name();
126             }
127             if(xmlreader.name() == "category")
128             {
129                 //qDebug() << xmlreader.name();
130                 attr = xmlreader.attributes();
131                 description = attr.value("description").toString();
132                 unit = attr.value("unit").toString();
133                 category = xmlreader.readElementText();
134                 myCategoryList->appendCats(i, description, unit, category);
135                 //qDebug() << "description: " << description << "unit: " << unit << "category: " << category;
136                 i++;
137                 receivedFlag = 1;
138             }
139         }
140     }
141     //Only change comboBoxTopCategory if a new list has been received
142     if(receivedFlag)
143     {
144         qDebug() << "receivedCategoryList() emitted";
145         myCategoryList->realSizeOfCats = i;
146         emit receivedCategoryList();
147     }
148 }
149
150 /**
151   *This function is used to read example xml file (results.xml).
152   *@todo Read real xml.
153   */
154 void XmlReader::xmlShow()
155 {
156     //QString filename = "results.xml";
157     QString filename = "xmlcategoryfile.xml";
158     QFile file(filename);
159
160     if (!file.open(QFile::ReadOnly))
161     {
162         qDebug() << "_xmlShow fail";
163         return;
164     }
165
166     //xmlReadTop10Results(&file);
167     //xmlReadCategories(&file);
168     file.close();
169 }
170