Display top performer in acceleration 0-100 category. Read and parsing results.xml...
[speedfreak] / Client / xmlreader.cpp
1 #include <QFile>
2 #include <QDebug>
3 //#include <QApplication>
4 #include "xmlreader.h"
5
6 /**
7   *Constructor of this class.
8   */
9 XmlReader::XmlReader()
10 {
11     xmlShow();
12 }
13
14 /**
15   *Destructor of this class. Should be used to release all allocated resources.
16   */
17 XmlReader::~XmlReader()
18 {
19     category = "";
20     unit = "";
21     description = "";
22     position = "";
23     user = "";
24     value = "";
25 }
26
27 /**
28   *This function is used to parsing xml file.
29   */
30 void XmlReader::xmlRead(QIODevice *device)
31 {
32     qDebug() << "_xmlRead";
33
34     xmlreader.addData(device->readAll());
35
36     //Go trough the xml document
37     while(!xmlreader.atEnd())
38     {
39         //Read next node
40         xmlreader.readNext();
41         //Check if this element is starting element
42         if(xmlreader.isStartElement())
43         {
44             if(xmlreader.name() == "results")
45             {
46                 qDebug() << xmlreader.name();
47                 attr = xmlreader.attributes();
48
49                 category = attr.value("category").toString();
50                 unit = attr.value("unit").toString();
51                 description = attr.value("description").toString();
52
53                 top10List << category;
54                 qDebug() << top10List << unit << description;
55             }
56
57             if(xmlreader.name() == "result")
58             {
59                 qDebug() << "result";
60                 attr = xmlreader.attributes();
61
62                 position = attr.value("position").toString();
63                 user = attr.value("user").toString();
64                 value = attr.value("value").toString();
65
66                 if (category == "acceleration-0-100")
67                 {
68                     top10AccelerationList.append(position + "\t" +
69                                                 user + "\t" +
70                                                 value +
71                                                 unit + "\t" +
72                                                 description + "\n");
73                 }
74
75                 if(category == "top10speed")
76                 {
77                     top10SpeedList.append(position + "\t" +
78                                           user + "\t" +
79                                           value +
80                                           unit + "\t" +
81                                           description + "\n");
82                 }
83
84                 if(category == "top10gforce")
85                 {
86                     top10GforceList.append(position + "\t" +
87                                            user + "\t" +
88                                            value +
89                                            unit + "\t" +
90                                            description + "\n");
91                 }
92                 qDebug() << position << user << value << unit;
93             }
94         }
95     }
96 }
97
98 /**
99   *This function is used to read example xml file (results.xml).
100   *@todo Read real xml.
101   */
102 void XmlReader::xmlShow()
103 {
104     QString filename = "results.xml";
105     QFile file(filename);
106
107     if (!file.open(QFile::ReadOnly))
108     {
109         qDebug() << "_xmlShow fail";
110         return;
111     }
112
113     xmlRead(&file);
114     file.close();
115 }
116
117 /**
118   *This is return function.
119   *@todo Read real top 10 category list
120   *@return QStringList top10List
121   */
122 QStringList XmlReader::getTop10List()
123 {
124     return top10List;
125 }
126
127 /**
128   *This is return function.
129   *@return QString top10AccelerationList
130   */
131 QString XmlReader::getTop10AccelerationList()
132 {
133     return top10AccelerationList;
134 }
135
136 /**
137   *This is return function.
138   *@return QString top10SpeedList
139   */
140 QString XmlReader::getTop10SpeedList()
141 {
142     return top10SpeedList;
143 }
144
145 /**
146   *This is return function.
147   *@return QString top10GforceList
148   */
149 QString XmlReader::getTop10GforceList()
150 {
151     return top10GforceList;
152 }