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"
13 #include "profiledialog.h"
16 *Constructor of this class.
18 XmlReader::XmlReader()
20 qDebug() << "__XmlReader";
21 myCategoryList = new CategoryList();
25 *Destructor of this class. Should be used to release all allocated resources.
27 XmlReader::~XmlReader()
29 qDebug() << "__~XmlReader";
37 delete myCategoryList;
41 *This function is used to parse top 10 results of a certain category.
43 void XmlReader::xmlReadTop10Results(QNetworkReply *device, QString userName)
45 qDebug() << "_xmlReadTop10Results";
51 QByteArray array = device->readAll();
52 qDebug() << "array: " << array;
53 xmlreader.addData(array);
54 //xmlreader.addData(device->readAll());
56 if(!(myCategoryList->top10List.isEmpty())) {
57 myCategoryList->top10List.clear();
60 //Go trough the xml document
61 while(!xmlreader.atEnd())
65 //Check if this element is starting element
66 if(xmlreader.isStartElement())
68 if(xmlreader.name() == "results")
70 qDebug() << xmlreader.name();
73 if(xmlreader.name() == "result")
75 qDebug() << xmlreader.name();
76 attr = xmlreader.attributes();
78 user = attr.value("username").toString();
79 position = attr.value("position").toString();
80 date = attr.value("date").toString();
81 //unit = attr.value("unit").toString();
83 value = attr.value("value").toString();
85 if (userName.toUpper() == user.toUpper())//If user name match highlight result
87 myCategoryList->top10List.append("<tr><td><b>" + position + "</b></td><td><b>" +
88 user + "</b></td><td><b>" +
90 unit + "</b></td><td><b>" +
91 date + "</b></td></tr>");
93 else//If user name not match
95 myCategoryList->top10List.append("<tr><td>" + position + "</td><td>" +
101 /* Old way, no highlight
102 myCategoryList->top10List.append(position + "\t" +
108 qDebug() << position << user << value << unit << date;
114 //Only change labelTopList if a new top10List has been received
117 qDebug() << "receivedTop10List() emitted";
118 emit receivedTop10List();
126 void XmlReader::xmlReadCategories(QNetworkReply *device)
127 //void XmlReader::xmlReadCategories(QIODevice *device)
129 qDebug() << "_xmlReadCategories";
132 int receivedFlag = 0;
135 QByteArray array = device->readAll();
136 qDebug() << "array: " << array;
137 xmlreader.addData(array);
138 //xmlreader.addData(device->readAll());
140 myCategoryList->clearCats();
142 //Go trough the xml document
143 while(!xmlreader.atEnd())
146 xmlreader.readNext();
148 //Check if this element is starting element
149 if(xmlreader.isStartElement())
151 if(xmlreader.name() == "categories")
153 qDebug() << xmlreader.name();
155 if(xmlreader.name() == "category")
157 qDebug() << xmlreader.name();
158 attr = xmlreader.attributes();
159 description = attr.value("description").toString();
160 unit = attr.value("unit").toString();
161 category = xmlreader.readElementText();
162 myCategoryList->appendCats(i, description, unit, category);
163 qDebug() << "description: " << description << "unit: " << unit << "category: " << category;
169 //Only change comboBoxTopCategory if a new list has been received
172 qDebug() << "receivedCategoryList() emitted";
173 myCategoryList->realSizeOfCats = i;
174 emit receivedCategoryList();
179 *This function is used to read example xml file (results.xml).
180 *@todo Read real xml.
182 void XmlReader::xmlShow()
184 //QString filename = "results.xml";
185 QString filename = "xmlcategoryfile.xml";
186 QFile file(filename);
188 if (!file.open(QFile::ReadOnly))
190 qDebug() << "_xmlShow fail";
194 //xmlReadTop10Results(&file);
195 //xmlReadCategories(&file);
196 //xmlReadProfile(&file);
201 * This function is used to read profile xml.
202 * @param QIODevice device: target of reading, here filename.
203 * @param ProfileDialog *profileDialog
205 void XmlReader::xmlReadProfile(QIODevice *device, ProfileDialog *profileDialog)
207 qDebug() << "_xmlReadProfile";
209 profile = profileDialog;
212 QByteArray array = device->readAll();
214 xmlreader.addData(array);
219 // Go trough the xml document
220 while(!xmlreader.atEnd())
223 xmlreader.readNext();
225 // Check if this element is starting element
226 if(xmlreader.isStartElement())
228 if(xmlreader.name() == "profile")
230 qDebug() << xmlreader.name();
231 attr = xmlreader.attributes();
232 login = attr.value("login").toString();
233 picture = attr.value("picture").toString();
235 if(xmlreader.name() == "manufacturer")
237 qDebug() << xmlreader.name();
238 profile->setManufacturer(xmlreader.readElementText());
240 if(xmlreader.name() == "type")
242 qDebug() << xmlreader.name();
243 profile->setType(xmlreader.readElementText());
246 if(xmlreader.name() == "model")
248 qDebug() << xmlreader.name();
249 profile->setModel(xmlreader.readElementText());
252 if(xmlreader.name() == "description")
254 qDebug() << xmlreader.name();
255 profile->setDescription(xmlreader.readElementText());
257 if(xmlreader.name() == "picture" && picture == "yes")
259 qDebug() << xmlreader.name();
260 profile->setPicture(xmlreader.readElementText());