UI design document 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     xmlShow();
20     // Remove next line from final code
21     categoryList << "Speed" << "acceleration-0-40" << "acceleration-0-100" << "G-force";
22 }
23
24 /**
25   *Destructor of this class. Should be used to release all allocated resources.
26   */
27 XmlReader::~XmlReader()
28 {
29     category = "";
30     unit = "";
31     description = "";
32     position = "";
33     user = "";
34     value = "";
35 }
36
37 /**
38   *This function is used to parsing xml file.
39   */
40 void XmlReader::xmlRead(QNetworkReply *device)
41 {
42     qDebug() << "_xmlRead";
43
44     xmlreader.addData(device->readAll());
45
46     //Go trough the xml document
47     while(!xmlreader.atEnd())
48     {
49         //Read next node
50         xmlreader.readNext();
51         //Check if this element is starting element
52         if(xmlreader.isStartElement())
53         {
54             if(xmlreader.name() == "results")
55             {
56                 qDebug() << xmlreader.name();
57                 attr = xmlreader.attributes();
58
59                 category = attr.value("category").toString();
60                 unit = attr.value("unit").toString();
61                 description = attr.value("description").toString();
62
63                 top10List << category;
64                 qDebug() << top10List << unit << description;
65             }
66
67             if(xmlreader.name() == "result")
68             {
69                 qDebug() << "result";
70                 attr = xmlreader.attributes();
71
72                 position = attr.value("position").toString();
73                 user = attr.value("user").toString();
74                 value = attr.value("value").toString();
75
76                 if (category == "acceleration-0-100")
77                 {
78                     top10AccelerationList.append(position + "\t" +
79                                                 user + "\t" +
80                                                 value +
81                                                 unit + "\t" +
82                                                 description + "\n");
83                 }
84
85                 if(category == "top10speed")
86                 {
87                     top10SpeedList.append(position + "\t" +
88                                           user + "\t" +
89                                           value +
90                                           unit + "\t" +
91                                           description + "\n");
92                 }
93
94                 if(category == "top10gforce")
95                 {
96                     top10GforceList.append(position + "\t" +
97                                            user + "\t" +
98                                            value +
99                                            unit + "\t" +
100                                            description + "\n");
101                 }
102                 qDebug() << position << user << value << unit;
103             }
104         }
105     }
106 }
107
108 /**
109   *This function is used to read example xml file (results.xml).
110   *@todo Read real xml.
111   */
112 void XmlReader::xmlShow()
113 {
114     QString filename = "results.xml";
115     QFile file(filename);
116
117     if (!file.open(QFile::ReadOnly))
118     {
119         qDebug() << "_xmlShow fail";
120         return;
121     }
122
123     //xmlRead(&file);
124     file.close();
125 }
126
127 /**
128   *This is return function.
129   *@todo Read real category list
130   *@return QStringList categoryList
131   */
132 QStringList XmlReader::getCategoryList()
133 {
134     return categoryList;
135 }
136
137 /**
138   *This is return function.
139   *@todo Read real top 10 category list
140   *@return QStringList top10List
141   */
142 QStringList XmlReader::getTop10List()
143 {
144     //During development is needed some values for categorylist
145     top10List.clear();
146     top10List << "acceleration-0-10" << "acceleration-0-60" << "acceleration-0-100";
147
148     return top10List;
149 }
150
151 QString XmlReader::getTopList( QString category, int size)
152 {
153     if (category == "acceleration-0-100")
154     {
155         //return top10AccelerationList;
156         return "acc-tulos1\nacc-tulos2\nacc-tulos3\nacc-tulos4\nacc-tulos5\nacc-tulos6\nacc-tulos7\nacc-tulos8\nacc-tulos9\nacc-tulos10";
157     }
158     else if (category == "acceleration-0-40")
159     {
160          //return top10AccelerationList;
161          return "acc-40-tulos1\nacc-40-tulos2\nacc-40-tulos3\nacc-40-tulos4\nacc-40-tulos5\nacc-40-tulos6\nacc-40-tulos7\nacc-40-tulos8\nacc-40-tulos9\nacc-40-tulos10";
162     }
163     else if (category == "Speed")
164     {
165         //return top10SpeedList;
166         return "speed-tulos1\nspeed-tulos2\nspeed-tulos3\nspeed-tulos4\nspeed-tulos5\nspeed-tulos6\nspeed-tulos7\nspeed-tulos8\nspeed-tulos9\nspeed-tulos10";
167     }
168     else if (category == "G-force")
169     {
170         //return top10GforceList;
171         return "g-tulos1\ng-tulos2\ng-tulos3\ng-tulos4\ng-tulos5\ng-tulos6\ng-tulos7\ng-tulos8\ng-tulos9\ng-tulos10";
172     }
173 }