Route and Results buttons updated.
[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     qDebug() << "__XmlReader";
20     myCategoryList = new CategoryList();
21 }
22
23 /**
24   *Destructor of this class. Should be used to release all allocated resources.
25   */
26 XmlReader::~XmlReader()
27 {
28     qDebug() << "__~XmlReader";
29     category = "";
30     unit = "";
31     position = "";
32     user = "";
33     value = "";
34
35     if(myCategoryList)
36         delete myCategoryList;
37 }
38
39 /**
40   *This function is used to parse top 10 results of a certain category.
41   */
42 void XmlReader::xmlReadTop10Results(QNetworkReply *device)
43 {
44     qDebug() << "_xmlReadTop10Results";
45
46     int i = 0;
47     int receivedFlag = 0;
48
49     xmlreader.clear();
50     QByteArray array = device->readAll();
51     //qDebug() << "array: " << array;
52     xmlreader.addData(array);
53     //xmlreader.addData(device->readAll());
54
55     if(!(myCategoryList->top10List.isEmpty())) {
56         myCategoryList->top10List.clear();
57     }
58
59     //Go trough the xml document
60     while(!xmlreader.atEnd())
61     {
62         //Read next node
63         xmlreader.readNext();
64         //Check if this element is starting element
65         if(xmlreader.isStartElement())
66         {
67             if(xmlreader.name() == "results")
68             {
69                 //qDebug() << xmlreader.name();
70             }
71             if(xmlreader.name() == "result")
72             {
73                 //qDebug() << xmlreader.name();
74                 attr = xmlreader.attributes();
75
76                 user = attr.value("username").toString();
77                 position = attr.value("position").toString();
78                 date = attr.value("date").toString();
79                 //unit = attr.value("unit").toString();
80                 unit = "s";
81                 value = attr.value("value").toString();
82
83                 myCategoryList->top10List.append(position + "\t" +
84                                                 user + "\t" +
85                                                 value + " " +
86                                                 unit + "\t" +
87                                                 date + "\n");
88
89                 //qDebug() << position << user << value << unit << date;
90                 i++;
91                 receivedFlag = 1;
92             }
93         }
94     }
95     //Only change labelTopList if a new top10List has been received
96     if(receivedFlag)
97     {
98         qDebug() << "receivedTop10List() emitted";
99         emit receivedTop10List();
100     }
101 }
102
103 void XmlReader::xmlReadCategories(QNetworkReply *device)
104 //void XmlReader::xmlReadCategories(QIODevice *device)
105 {
106     qDebug() << "_xmlReadCategories";
107
108     int i = 0;
109     int receivedFlag = 0;
110
111     xmlreader.clear();
112     QByteArray array = device->readAll();
113     //qDebug() << "array: " << array;
114     xmlreader.addData(array);
115     //xmlreader.addData(device->readAll());
116
117     myCategoryList->clearCats();
118
119     //Go trough the xml document
120     while(!xmlreader.atEnd())
121     {
122         //Read next node
123         xmlreader.readNext();
124
125         //Check if this element is starting element
126         if(xmlreader.isStartElement())
127         {
128             if(xmlreader.name() == "categories")
129             {
130                 //qDebug() << xmlreader.name();
131             }
132             if(xmlreader.name() == "category")
133             {
134                 //qDebug() << xmlreader.name();
135                 attr = xmlreader.attributes();
136                 description = attr.value("description").toString();
137                 unit = attr.value("unit").toString();
138                 category = xmlreader.readElementText();
139                 myCategoryList->appendCats(i, description, unit, category);
140                 //qDebug() << "description: " << description << "unit: " << unit << "category: " << category;
141                 i++;
142                 receivedFlag = 1;
143             }
144         }
145     }
146     //Only change comboBoxTopCategory if a new list has been received
147     if(receivedFlag)
148     {
149         qDebug() << "receivedCategoryList() emitted";
150         myCategoryList->realSizeOfCats = i;
151         emit receivedCategoryList();
152     }
153 }
154
155 /**
156   *This function is used to read example xml file (results.xml).
157   *@todo Read real xml.
158   */
159 void XmlReader::xmlShow()
160 {
161     //QString filename = "results.xml";
162     QString filename = "xmlcategoryfile.xml";
163     QFile file(filename);
164
165     if (!file.open(QFile::ReadOnly))
166     {
167         qDebug() << "_xmlShow fail";
168         return;
169     }
170
171     //xmlReadTop10Results(&file);
172     //xmlReadCategories(&file);
173     file.close();
174 }