Added sending route to 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     myCategoryList = new CategoryList();
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     position = "";
30     user = "";
31     value = "";
32     delete myCategoryList;
33 }
34
35 /**
36   *This function is used to parse top 10 results of a certain category.
37   */
38 void XmlReader::xmlReadTop10Results(QNetworkReply *device)
39 {
40     qDebug() << "_xmlReadTop10Results";
41
42     int i = 0;
43     int receivedFlag = 0;
44
45     xmlreader.clear();
46     QByteArray array = device->readAll();
47     qDebug() << "array: " << array;
48     xmlreader.addData(array);
49     //xmlreader.addData(device->readAll());
50
51     if(!(myCategoryList->top10List.isEmpty())) {
52         myCategoryList->top10List.clear();
53     }
54
55     //Go trough the xml document
56     while(!xmlreader.atEnd())
57     {
58         //Read next node
59         xmlreader.readNext();
60         //Check if this element is starting element
61         if(xmlreader.isStartElement())
62         {
63             if(xmlreader.name() == "results")
64             {
65                 qDebug() << xmlreader.name();
66             }
67             if(xmlreader.name() == "result")
68             {
69                 qDebug() << "result";
70                 attr = xmlreader.attributes();
71
72                 user = attr.value("username").toString();
73                 position = attr.value("position").toString();
74                 date = attr.value("date").toString();
75                 unit = attr.value("unit").toString();
76                 value = attr.value("value").toString();
77
78                 myCategoryList->top10List.append(position + "\t" +
79                                                 user + "\t" +
80                                                 value + " " +
81                                                 unit + "\t" +
82                                                 date + "\n");
83
84                 qDebug() << position << user << value << unit << date;
85                 i++;
86                 receivedFlag = 1;
87             }
88         }
89     }
90     //Only change labelTopList if a new top10List has been received
91     if(receivedFlag)
92     {
93         qDebug() << "receivedTop10List() emitted";
94         emit receivedTop10List();
95     }
96 }
97
98 void XmlReader::xmlReadCategories(QNetworkReply *device)
99 //void XmlReader::xmlReadCategories(QIODevice *device)
100 {
101     qDebug() << "_xmlReadCategories";
102
103     int i = 0;
104     int receivedFlag = 0;
105
106     xmlreader.clear();
107     QByteArray array = device->readAll();
108     qDebug() << "array: " << array;
109     xmlreader.addData(array);
110     //xmlreader.addData(device->readAll());
111
112     if(myCategoryList->sizeOfCategoryList() != 0) {
113         myCategoryList->clearCategoryList();
114     }
115     myCategoryList->clearCats();
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                 attr = xmlreader.attributes();
134                 description = attr.value("description").toString();
135                 unit = attr.value("unit").toString();
136                 category = xmlreader.readElementText();
137                 myCategoryList->appendCategoryList(category);
138                 myCategoryList->appendCats(i, description, unit, category);
139                 //qDebug() << "i=" << i << myCategoryList->itemOfCategoryList(i);
140                 //qDebug() << "description: " << description << "unit: " << unit << "category: " << category;
141                 i++;
142                 receivedFlag = 1;
143             }
144         }
145     }
146     //Only change comboBoxTopCategory if a new list has been received
147     if(receivedFlag)
148     {
149         qDebug() << "receivedCategoryList() emitted";
150         emit receivedCategoryList();
151     }
152 }
153
154 /**
155   *This function is used to read example xml file (results.xml).
156   *@todo Read real xml.
157   */
158 void XmlReader::xmlShow()
159 {
160     //QString filename = "results.xml";
161     QString filename = "xmlcategoryfile.xml";
162     QFile file(filename);
163
164     if (!file.open(QFile::ReadOnly))
165     {
166         qDebug() << "_xmlShow fail";
167         return;
168     }
169
170     //xmlReadTop10Results(&file);
171     //xmlReadCategories(&file);
172     file.close();
173 }
174