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
12 #include "xmlreader.h"
15 *Constructor of this class.
17 XmlReader::XmlReader()
19 myCategoryList = new CategoryList();
23 *Destructor of this class. Should be used to release all allocated resources.
25 XmlReader::~XmlReader()
32 delete myCategoryList;
36 *This function is used to parse top 10 results of a certain category.
38 void XmlReader::xmlReadTop10Results(QNetworkReply *device)
40 qDebug() << "_xmlReadTop10Results";
46 QByteArray array = device->readAll();
47 qDebug() << "array: " << array;
48 xmlreader.addData(array);
49 //xmlreader.addData(device->readAll());
51 if(!(myCategoryList->top10List.isEmpty())) {
52 myCategoryList->top10List.clear();
55 //Go trough the xml document
56 while(!xmlreader.atEnd())
60 //Check if this element is starting element
61 if(xmlreader.isStartElement())
63 if(xmlreader.name() == "results")
65 qDebug() << xmlreader.name();
67 if(xmlreader.name() == "result")
70 attr = xmlreader.attributes();
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();
78 myCategoryList->top10List.append(position + "\t" +
84 qDebug() << position << user << value << unit << date;
90 //Only change labelTopList if a new top10List has been received
93 qDebug() << "receivedTop10List() emitted";
94 emit receivedTop10List();
98 void XmlReader::xmlReadCategories(QNetworkReply *device)
99 //void XmlReader::xmlReadCategories(QIODevice *device)
101 qDebug() << "_xmlReadCategories";
104 int receivedFlag = 0;
107 QByteArray array = device->readAll();
108 qDebug() << "array: " << array;
109 xmlreader.addData(array);
110 //xmlreader.addData(device->readAll());
112 if(myCategoryList->sizeOfCategoryList() != 0) {
113 myCategoryList->clearCategoryList();
115 myCategoryList->clearCats();
117 //Go trough the xml document
118 while(!xmlreader.atEnd())
121 xmlreader.readNext();
123 //Check if this element is starting element
124 if(xmlreader.isStartElement())
126 if(xmlreader.name() == "categories")
128 qDebug() << xmlreader.name();
130 if(xmlreader.name() == "category")
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;
146 //Only change comboBoxTopCategory if a new list has been received
149 qDebug() << "receivedCategoryList() emitted";
150 emit receivedCategoryList();
155 *This function is used to read example xml file (results.xml).
156 *@todo Read real xml.
158 void XmlReader::xmlShow()
160 //QString filename = "results.xml";
161 QString filename = "xmlcategoryfile.xml";
162 QFile file(filename);
164 if (!file.open(QFile::ReadOnly))
166 qDebug() << "_xmlShow fail";
170 //xmlReadTop10Results(&file);
171 //xmlReadCategories(&file);