Added function to kill usersDialog when dialog is closed.
[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 #include "profiledialog.h"
14
15 /**
16   *Constructor of this class.
17   */
18 XmlReader::XmlReader()
19 {
20     qDebug() << "__XmlReader";
21     myCategoryList = new CategoryList();
22     usersList = NULL;
23     usersInfo = new QStringList();
24 }
25
26 /**
27   *Destructor of this class. Should be used to release all allocated resources.
28   */
29 XmlReader::~XmlReader()
30 {
31     qDebug() << "__~XmlReader";
32     category = "";
33     unit = "";
34     position = "";
35     user = "";
36     value = "";
37
38     if(myCategoryList)
39         delete myCategoryList;
40 }
41
42 /**
43   *This function is used to parse top 10 results of a certain category.
44   */
45 void XmlReader::xmlReadTop10Results(QNetworkReply *device,  QString userName)
46 {
47     qDebug() << "_xmlReadTop10Results";
48
49     int i = 0;
50     int receivedFlag = 0;
51
52     xmlreader.clear();
53     QByteArray array = device->readAll();
54     qDebug() << "array: " << array;
55     xmlreader.addData(array);
56     //xmlreader.addData(device->readAll());
57
58     if(!(myCategoryList->top10List.isEmpty())) {
59         myCategoryList->top10List.clear();
60     }
61
62     //Go trough the xml document
63     while(!xmlreader.atEnd())
64     {
65         //Read next node
66         xmlreader.readNext();
67         //Check if this element is starting element
68         if(xmlreader.isStartElement())
69         {
70             if(xmlreader.name() == "results")
71             {
72                 qDebug() << xmlreader.name();
73             }
74
75             if(xmlreader.name() == "result")
76             {
77                 qDebug() << xmlreader.name();
78                 attr = xmlreader.attributes();
79
80                 user = attr.value("username").toString();
81                 position = attr.value("position").toString();
82                 date = attr.value("date").toString();
83                 //unit = attr.value("unit").toString();
84                 unit = "s";
85                 value = attr.value("value").toString();
86
87                 if (userName.toUpper() == user.toUpper())//If user name match highlight result
88                 {
89                     myCategoryList->top10List.append("<tr><td><b>" + position + "</b></td><td><b>" +
90                                                     user + "</b></td><td><b>" +
91                                                     value + " " +
92                                                     unit + "</b></td><td><b>" +
93                                                     date + "</b></td></tr>");
94                 }
95                 else//If user name not match
96                 {
97                     myCategoryList->top10List.append("<tr><td>" + position + "</td><td>" +
98                                                     user + "</td><td>" +
99                                                     value + "&nbsp;" +
100                                                     unit + "</td><td>" +
101                                                     date + "</td></tr>");
102                 }
103                 /* Old way, no highlight
104                 myCategoryList->top10List.append(position + "\t" +
105                                                 user + "\t" +
106                                                 value + " " +
107                                                 unit + "\t" +
108                                                 date + "\n");*/
109
110                 qDebug() << position << user << value << unit << date;
111                 i++;
112                 receivedFlag = 1;
113             }
114         }
115     }
116     //Only change labelTopList if a new top10List has been received
117     if(receivedFlag)
118     {
119         qDebug() << "receivedTop10List() emitted";
120         emit receivedTop10List();
121     }
122 }
123
124 /**
125   *
126   *
127   */
128 void XmlReader::xmlReadCategories(QNetworkReply *device)
129 //void XmlReader::xmlReadCategories(QIODevice *device)
130 {
131     qDebug() << "_xmlReadCategories";
132
133     int i = 0;
134     int receivedFlag = 0;
135
136     xmlreader.clear();
137     QByteArray array = device->readAll();
138     qDebug() << "array: " << array;
139     xmlreader.addData(array);
140     //xmlreader.addData(device->readAll());
141
142     myCategoryList->clearCats();
143
144     //Go trough the xml document
145     while(!xmlreader.atEnd())
146     {
147         //Read next node
148         xmlreader.readNext();
149
150         //Check if this element is starting element
151         if(xmlreader.isStartElement())
152         {
153             if(xmlreader.name() == "categories")
154             {
155                 qDebug() << xmlreader.name();
156             }
157             if(xmlreader.name() == "category")
158             {
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;
166                 i++;
167                 receivedFlag = 1;
168             }
169         }
170     }
171     //Only change comboBoxTopCategory if a new list has been received
172     if(receivedFlag)
173     {
174         qDebug() << "receivedCategoryList() emitted";
175         myCategoryList->realSizeOfCats = i;
176         emit receivedCategoryList();
177     }
178 }
179
180 /**
181   *This function is used to read example xml file (results.xml).
182   *@todo Read real xml.
183   */
184 void XmlReader::xmlShow()
185 {
186     //QString filename = "results.xml";
187     QString filename = "xmlcategoryfile.xml";
188     QFile file(filename);
189
190     if (!file.open(QFile::ReadOnly))
191     {
192         qDebug() << "_xmlShow fail";
193         return;
194     }
195
196     //xmlReadTop10Results(&file);
197     //xmlReadCategories(&file);
198     //xmlReadProfile(&file);
199     file.close();
200 }
201
202 /**
203   * This function is used to read profile xml.
204   * @param QIODevice device: target of reading, here filename.
205   * @param ProfileDialog *profileDialog
206   */
207 void XmlReader::xmlReadProfile(QIODevice *device, ProfileDialog *profileDialog)
208 {
209     qDebug() << "_xmlReadProfile";
210
211     profile = profileDialog;
212
213     xmlreader.clear();
214     QByteArray array = device->readAll();
215
216     xmlreader.addData(array);
217
218     QString login;
219     QString picture;
220
221     // Go trough the xml document
222     while(!xmlreader.atEnd())
223     {
224         // Read next node
225         xmlreader.readNext();
226
227         // Check if this element is starting element
228         if(xmlreader.isStartElement())
229         {
230             if(xmlreader.name() == "profile")
231             {
232                 qDebug() << xmlreader.name();
233                 attr = xmlreader.attributes();
234                 login = attr.value("login").toString();
235                 picture = attr.value("picture").toString();
236             }
237             if(xmlreader.name() == "manufacturer")
238             {
239                 qDebug() << xmlreader.name();
240                 profile->setManufacturer(xmlreader.readElementText());
241             }
242             if(xmlreader.name() == "type")
243             {
244                 qDebug() << xmlreader.name();
245                 profile->setType(xmlreader.readElementText());
246
247             }
248             if(xmlreader.name() == "model")
249             {
250                 qDebug() << xmlreader.name();
251                 profile->setModel(xmlreader.readElementText());
252
253             }
254             if(xmlreader.name() == "description")
255             {
256                 qDebug() << xmlreader.name();
257                 profile->setDescription(xmlreader.readElementText());
258             }
259             if(xmlreader.name() == "picture" && picture == "yes")
260             {
261                 qDebug() << xmlreader.name();
262                 profile->setPicture(xmlreader.readElementText());
263             }
264         }
265     }
266     profile = NULL;
267 }
268
269 /**
270   *This function is used to parse user's info of a certain username.
271   */
272 //void XmlReader::xmlReadUserInfo(QIODevice *device)
273 void XmlReader::xmlReadUserInfo(QNetworkReply *device)
274 {
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"/>
278     */
279
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"  />*/
283
284     usersInfo->clear();
285     xmlreader.clear();
286     QByteArray array = device->readAll();
287     qDebug() << "array: " << array;
288     xmlreader.addData(array);
289
290     while(!xmlreader.atEnd())
291     {
292         //Read next node
293         xmlreader.readNext();
294
295         //Check if this element is starting element
296         if(xmlreader.isStartElement())
297         {
298             if(xmlreader.name() == "user")
299             {
300                 qDebug() << xmlreader.name();
301                 attr = xmlreader.attributes();
302                 QString data;
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);
309
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);
320             }
321         }
322     }
323     qDebug() << "__emit";
324     emit userInfo(usersInfo);
325 }
326
327 /**
328   *This function is used to parse usernames.
329   */
330 void XmlReader::xmlReadUsers(QNetworkReply *device)
331 //void XmlReader::xmlReadUsers(QIODevice *device)
332 {
333     /* <?xml version="1.0" encoding="utf-8"?>
334      <users>
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"  />
337      </users>*/
338
339     xmlreader.clear();
340     QByteArray array = device->readAll();
341     qDebug() << "array: " << array;
342     xmlreader.addData(array);
343     //Go trough the xml document
344
345     if (!usersList)
346     {
347         usersList = new QStringList();
348         qDebug() << "userlist luodaan";
349     }
350
351     else
352     {
353         usersList->clear();
354         qDebug() << "userlist tyhjennetaan";
355     }
356
357     while(!xmlreader.atEnd())
358     {
359         //Read next node
360         xmlreader.readNext();
361         qDebug() << xmlreader.name();
362
363         //Check if this element is starting element
364         if(xmlreader.isStartElement())
365         {
366             if (xmlreader.name() == "user")
367             {
368                 qDebug() << xmlreader.name();
369                 attr = xmlreader.attributes();
370                 QString username;
371                 QString description;
372                 username = attr.value("login").toString();
373                 description = attr.value("description").toString();
374                 qDebug() << "user: " << username;
375                 qDebug() << "description: " << description;
376                 usersList->append(username);
377             }
378         }
379     }
380 }