Settings dialog changed. Log in/Log out functionality improved.
[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() << xmlreader.name();
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                 unit = "s";
77                 value = attr.value("value").toString();
78
79                 myCategoryList->top10List.append(position + "\t" +
80                                                 user + "\t" +
81                                                 value + " " +
82                                                 unit + "\t" +
83                                                 date + "\n");
84
85                 //qDebug() << position << user << value << unit << date;
86                 i++;
87                 receivedFlag = 1;
88             }
89         }
90     }
91     //Only change labelTopList if a new top10List has been received
92     if(receivedFlag)
93     {
94         qDebug() << "receivedTop10List() emitted";
95         emit receivedTop10List();
96     }
97 }
98
99 void XmlReader::xmlReadCategories(QNetworkReply *device)
100 //void XmlReader::xmlReadCategories(QIODevice *device)
101 {
102     qDebug() << "_xmlReadCategories";
103
104     int i = 0;
105     int receivedFlag = 0;
106
107     xmlreader.clear();
108     QByteArray array = device->readAll();
109     qDebug() << "array: " << array;
110     xmlreader.addData(array);
111     //xmlreader.addData(device->readAll());
112
113     myCategoryList->clearCats();
114
115     //Go trough the xml document
116     while(!xmlreader.atEnd())
117     {
118         //Read next node
119         xmlreader.readNext();
120
121         //Check if this element is starting element
122         if(xmlreader.isStartElement())
123         {
124             if(xmlreader.name() == "categories")
125             {
126                 //qDebug() << xmlreader.name();
127             }
128             if(xmlreader.name() == "category")
129             {
130                 //qDebug() << xmlreader.name();
131                 attr = xmlreader.attributes();
132                 description = attr.value("description").toString();
133                 unit = attr.value("unit").toString();
134                 category = xmlreader.readElementText();
135                 myCategoryList->appendCats(i, description, unit, category);
136                 //qDebug() << "description: " << description << "unit: " << unit << "category: " << category;
137                 i++;
138                 receivedFlag = 1;
139             }
140         }
141     }
142     //Only change comboBoxTopCategory if a new list has been received
143     if(receivedFlag)
144     {
145         qDebug() << "receivedCategoryList() emitted";
146         myCategoryList->realSizeOfCats = i;
147         emit receivedCategoryList();
148     }
149 }
150
151 /**
152   *This function is used to read example xml file (results.xml).
153   *@todo Read real xml.
154   */
155 void XmlReader::xmlShow()
156 {
157     //QString filename = "results.xml";
158     QString filename = "xmlcategoryfile.xml";
159     QFile file(filename);
160
161     if (!file.open(QFile::ReadOnly))
162     {
163         qDebug() << "_xmlShow fail";
164         return;
165     }
166
167     //xmlReadTop10Results(&file);
168     //xmlReadCategories(&file);
169     file.close();
170 }