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