c3a439c39ac865819e7fe6f4e61f50c46338fd57
[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     xmlShow();
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     description = "";
30     position = "";
31     user = "";
32     value = "";
33 }
34
35 /**
36   *This function is used to parsing xml file.
37   */
38 void XmlReader::xmlRead(QIODevice *device)
39 {
40     qDebug() << "_xmlRead";
41
42     xmlreader.addData(device->readAll());
43
44     //Go trough the xml document
45     while(!xmlreader.atEnd())
46     {
47         //Read next node
48         xmlreader.readNext();
49         //Check if this element is starting element
50         if(xmlreader.isStartElement())
51         {
52             if(xmlreader.name() == "results")
53             {
54                 qDebug() << xmlreader.name();
55                 attr = xmlreader.attributes();
56
57                 category = attr.value("category").toString();
58                 unit = attr.value("unit").toString();
59                 description = attr.value("description").toString();
60
61                 top10List << category;
62                 qDebug() << top10List << unit << description;
63             }
64
65             if(xmlreader.name() == "result")
66             {
67                 qDebug() << "result";
68                 attr = xmlreader.attributes();
69
70                 position = attr.value("position").toString();
71                 user = attr.value("user").toString();
72                 value = attr.value("value").toString();
73
74                 if (category == "acceleration-0-100")
75                 {
76                     top10AccelerationList.append(position + "\t" +
77                                                 user + "\t" +
78                                                 value +
79                                                 unit + "\t" +
80                                                 description + "\n");
81                 }
82
83                 if(category == "top10speed")
84                 {
85                     top10SpeedList.append(position + "\t" +
86                                           user + "\t" +
87                                           value +
88                                           unit + "\t" +
89                                           description + "\n");
90                 }
91
92                 if(category == "top10gforce")
93                 {
94                     top10GforceList.append(position + "\t" +
95                                            user + "\t" +
96                                            value +
97                                            unit + "\t" +
98                                            description + "\n");
99                 }
100                 qDebug() << position << user << value << unit;
101             }
102         }
103     }
104 }
105
106 /**
107   *This function is used to read example xml file (results.xml).
108   *@todo Read real xml.
109   */
110 void XmlReader::xmlShow()
111 {
112     QString filename = "results.xml";
113     QFile file(filename);
114
115     if (!file.open(QFile::ReadOnly))
116     {
117         qDebug() << "_xmlShow fail";
118         return;
119     }
120
121     xmlRead(&file);
122     file.close();
123 }
124
125 /**
126   *This is return function.
127   *@todo Read real top 10 category list
128   *@return QStringList top10List
129   */
130 QStringList XmlReader::getTop10List()
131 {
132     return top10List;
133 }
134
135 /**
136   *This is return function.
137   *@return QString top10AccelerationList
138   */
139 QString XmlReader::getTop10AccelerationList()
140 {
141     return top10AccelerationList;
142 }
143
144 /**
145   *This is return function.
146   *@return QString top10SpeedList
147   */
148 QString XmlReader::getTop10SpeedList()
149 {
150     return top10SpeedList;
151 }
152
153 /**
154   *This is return function.
155   *@return QString top10GforceList
156   */
157 QString XmlReader::getTop10GforceList()
158 {
159     return top10GforceList;
160 }