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();
23 usersInfo = new QStringList();
27 *Destructor of this class. Should be used to release all allocated resources.
29 XmlReader::~XmlReader()
31 qDebug() << "__~XmlReader";
39 delete myCategoryList;
43 *This function is used to parse top 10 results of a certain category.
45 void XmlReader::xmlReadTop10Results(QNetworkReply *device, QString userName)
47 qDebug() << "_xmlReadTop10Results";
53 QByteArray array = device->readAll();
54 qDebug() << "array: " << array;
55 xmlreader.addData(array);
56 //xmlreader.addData(device->readAll());
58 if(!(myCategoryList->top10List.isEmpty())) {
59 myCategoryList->top10List.clear();
62 //Go trough the xml document
63 while(!xmlreader.atEnd())
67 //Check if this element is starting element
68 if(xmlreader.isStartElement())
70 if(xmlreader.name() == "results")
72 qDebug() << xmlreader.name();
75 if(xmlreader.name() == "result")
77 qDebug() << xmlreader.name();
78 attr = xmlreader.attributes();
80 user = attr.value("username").toString();
81 position = attr.value("position").toString();
82 date = attr.value("date").toString();
83 //unit = attr.value("unit").toString();
85 value = attr.value("value").toString();
87 if (userName.toUpper() == user.toUpper())//If user name match highlight result
89 myCategoryList->top10List.append("<tr><td><b>" + position + "</b></td><td><b>" +
90 user + "</b></td><td><b>" +
92 unit + "</b></td><td><b>" +
93 date + "</b></td></tr>");
95 else//If user name not match
97 myCategoryList->top10List.append("<tr><td>" + position + "</td><td>" +
101 date + "</td></tr>");
103 /* Old way, no highlight
104 myCategoryList->top10List.append(position + "\t" +
110 qDebug() << position << user << value << unit << date;
116 //Only change labelTopList if a new top10List has been received
119 qDebug() << "receivedTop10List() emitted";
120 emit receivedTop10List();
128 void XmlReader::xmlReadCategories(QNetworkReply *device)
129 //void XmlReader::xmlReadCategories(QIODevice *device)
131 qDebug() << "_xmlReadCategories";
134 int receivedFlag = 0;
137 QByteArray array = device->readAll();
138 qDebug() << "array: " << array;
139 xmlreader.addData(array);
140 //xmlreader.addData(device->readAll());
142 myCategoryList->clearCats();
144 //Go trough the xml document
145 while(!xmlreader.atEnd())
148 xmlreader.readNext();
150 //Check if this element is starting element
151 if(xmlreader.isStartElement())
153 if(xmlreader.name() == "categories")
155 qDebug() << xmlreader.name();
157 if(xmlreader.name() == "category")
159 qDebug() << xmlreader.name();
160 attr = xmlreader.attributes();
161 description = attr.value("description").toString();
162 unit = attr.value("unit").toString();
163 category = xmlreader.readElementText();
164 myCategoryList->appendCats(i, description, unit, category);
165 qDebug() << "description: " << description << "unit: " << unit << "category: " << category;
171 //Only change comboBoxTopCategory if a new list has been received
174 qDebug() << "receivedCategoryList() emitted";
175 myCategoryList->realSizeOfCats = i;
176 emit receivedCategoryList();
181 *This function is used to read example xml file (results.xml).
182 *@todo Read real xml.
184 void XmlReader::xmlShow()
186 //QString filename = "results.xml";
187 QString filename = "xmlcategoryfile.xml";
188 QFile file(filename);
190 if (!file.open(QFile::ReadOnly))
192 qDebug() << "_xmlShow fail";
196 //xmlReadTop10Results(&file);
197 //xmlReadCategories(&file);
198 //xmlReadProfile(&file);
203 * This function is used to read profile xml.
204 * @param QIODevice device: target of reading, here filename.
205 * @param ProfileDialog *profileDialog
207 void XmlReader::xmlReadProfile(QIODevice *device, ProfileDialog *profileDialog)
209 qDebug() << "_xmlReadProfile";
211 profile = profileDialog;
214 QByteArray array = device->readAll();
216 xmlreader.addData(array);
221 // Go trough the xml document
222 while(!xmlreader.atEnd())
225 xmlreader.readNext();
227 // Check if this element is starting element
228 if(xmlreader.isStartElement())
230 if(xmlreader.name() == "profile")
232 qDebug() << xmlreader.name();
233 attr = xmlreader.attributes();
234 login = attr.value("login").toString();
235 picture = attr.value("picture").toString();
237 if(xmlreader.name() == "manufacturer")
239 qDebug() << xmlreader.name();
240 profile->setManufacturer(xmlreader.readElementText());
242 if(xmlreader.name() == "type")
244 qDebug() << xmlreader.name();
245 profile->setType(xmlreader.readElementText());
248 if(xmlreader.name() == "model")
250 qDebug() << xmlreader.name();
251 profile->setModel(xmlreader.readElementText());
254 if(xmlreader.name() == "description")
256 qDebug() << xmlreader.name();
257 profile->setDescription(xmlreader.readElementText());
259 if(xmlreader.name() == "picture" && picture == "yes")
261 qDebug() << xmlreader.name();
262 profile->setPicture(xmlreader.readElementText());
270 *This function is used to parse user's info of a certain username.
272 //void XmlReader::xmlReadUserInfo(QIODevice *device)
273 void XmlReader::xmlReadUserInfo(QNetworkReply *device)
275 /* <?xml version="1.0" encoding="utf-8"?>
276 <userinfo user="test928" manufacturer="Toyota" type="corolla" model="1983" description="Fuel
277 efficient, GPS system, only one owner"/>
280 /* <?xml version="1.0" encoding="utf-8"?>
281 <user login="test" description="<![CDATA my car rox! ]]>" last_activity="2010-05-13 21:47:15"
282 avatar="http://www.speedfreak-api.com/static/uploads/avatars/22.jpg" />*/
286 QByteArray array = device->readAll();
287 qDebug() << "array: " << array;
288 xmlreader.addData(array);
290 while(!xmlreader.atEnd())
293 xmlreader.readNext();
295 //Check if this element is starting element
296 if(xmlreader.isStartElement())
298 if(xmlreader.name() == "user")
300 qDebug() << xmlreader.name();
301 attr = xmlreader.attributes();
303 data = attr.value("login").toString();
304 qDebug() << "user: " << data;
305 usersInfo->append(data);
306 data = attr.value("description").toString();
307 qDebug() << "description: " << data;
308 usersInfo->append(data);
310 /*data = attr.value("type").toString();
311 qDebug() << "type: " << data;
312 usersInfo.append(data);
313 data = attr.value("model").toString();
314 qDebug() << "model: " << data;
315 usersInfo.append(data);
316 data = attr.value("description").toString();
317 qDebug() << "description: " << data;
318 usersInfo.append(data);*/
319 //usersList->append(username);
323 qDebug() << "__emit";
324 emit userInfo(usersInfo);
328 *This function is used to parse usernames.
330 void XmlReader::xmlReadUsers(QNetworkReply *device)
331 //void XmlReader::xmlReadUsers(QIODevice *device)
333 /* <?xml version="1.0" encoding="utf-8"?>
335 <user login="test" description="<![CDATA my car rox! ]]>" last_activity="2010-05-13 21:47:15" />
336 <user login="test1" description="<![CDATA my car rox too! ]]>" last_activity="2010-05-13 21:28:00" />
340 QByteArray array = device->readAll();
341 qDebug() << "array: " << array;
342 xmlreader.addData(array);
343 //Go trough the xml document
347 usersList = new QStringList();
348 qDebug() << "userlist luodaan";
354 qDebug() << "userlist tyhjennetaan";
357 while(!xmlreader.atEnd())
360 xmlreader.readNext();
361 qDebug() << xmlreader.name();
363 //Check if this element is starting element
364 if(xmlreader.isStartElement())
366 if (xmlreader.name() == "user")
368 qDebug() << xmlreader.name();
369 attr = xmlreader.attributes();
372 username = attr.value("login").toString();
373 description = attr.value("description").toString();
374 qDebug() << "user: " << username;
375 qDebug() << "description: " << description;
376 usersList->append(username);