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 //qDebug() << "sizeOfCategoryList(): " << myCategoryList->sizeOfCategoryList();
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 myCategoryList->appendCategoryList(xmlreader.readElementText());
134 qDebug() << "i=" << i << myCategoryList->itemOfCategoryList(i);
140 //Only change comboBoxTopCategory if a new list has been received
143 qDebug() << "receivedCategoryList() emitted";
144 emit receivedCategoryList();
149 *This function is used to read example xml file (results.xml).
150 *@todo Read real xml.
152 void XmlReader::xmlShow()
154 //QString filename = "results.xml";
155 QString filename = "xmlcategoryfile.xml";
156 QFile file(filename);
158 if (!file.open(QFile::ReadOnly))
160 qDebug() << "_xmlShow fail";
164 //xmlReadTop10Results(&file);
165 //xmlReadCategories(&file);