Added implementation to handleCheckPoint slot function in carmainwindow.cpp. Now...
[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::xmlReadTop10Results(QNetworkReply *device)
39 {
40     qDebug() << "_xmlReadTop10Results";
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 void XmlReader::xmlReadCategories(QNetworkReply *device)
107 //void XmlReader::xmlReadCategories(QIODevice *device)
108 {
109     qDebug() << "_xmlReadCategories";
110
111     int i = 0;
112
113     QByteArray array = device->readAll();
114     qDebug() << array;
115     xmlreader.addData(array);
116
117     //Go trough the xml document
118     while(!xmlreader.atEnd())
119     {
120         //Read next node
121         xmlreader.readNext();
122
123         //Check if this element is starting element
124         if(xmlreader.isStartElement())
125         {
126             if(xmlreader.name() == "categories")
127             {
128                 qDebug() << xmlreader.name();
129             }
130             if(xmlreader.name() == "category")
131             {
132                 qDebug() << xmlreader.name();
133                 categoryList.insert(i, xmlreader.readElementText());
134                 qDebug() << "i=" << i << categoryList.at(i);
135                 i++;
136             }
137         }
138     }
139 }
140
141 /**
142   *This function is used to read example xml file (results.xml).
143   *@todo Read real xml.
144   */
145 void XmlReader::xmlShow()
146 {
147     //QString filename = "results.xml";
148     QString filename = "xmlcategoryfile.xml";
149     QFile file(filename);
150
151     if (!file.open(QFile::ReadOnly))
152     {
153         qDebug() << "_xmlShow fail";
154         return;
155     }
156
157     //xmlReadTop10Results(&file);
158     //xmlReadCategories(&file);
159     file.close();
160 }
161