Updated documentation preparing for refactoring
[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 }
23
24 /**
25   *Destructor of this class. Should be used to release all allocated resources.
26   */
27 XmlReader::~XmlReader()
28 {
29     qDebug() << "__~XmlReader";
30     category = "";
31     unit = "";
32     position = "";
33     user = "";
34     value = "";
35
36     if(myCategoryList)
37         delete myCategoryList;
38 }
39
40 /**
41   *This function is used to parse top 10 results of a certain category.
42   */
43 void XmlReader::xmlReadTop10Results(QNetworkReply *device,  QString userName)
44 {
45     qDebug() << "_xmlReadTop10Results";
46
47     int i = 0;
48     int receivedFlag = 0;
49
50     xmlreader.clear();
51     QByteArray array = device->readAll();
52     qDebug() << "array: " << array;
53     xmlreader.addData(array);
54     //xmlreader.addData(device->readAll());
55
56     if(!(myCategoryList->top10List.isEmpty())) {
57         myCategoryList->top10List.clear();
58     }
59
60     //Go trough the xml document
61     while(!xmlreader.atEnd())
62     {
63         //Read next node
64         xmlreader.readNext();
65         //Check if this element is starting element
66         if(xmlreader.isStartElement())
67         {
68             if(xmlreader.name() == "results")
69             {
70                 qDebug() << xmlreader.name();
71             }
72
73             if(xmlreader.name() == "result")
74             {
75                 qDebug() << xmlreader.name();
76                 attr = xmlreader.attributes();
77
78                 user = attr.value("username").toString();
79                 position = attr.value("position").toString();
80                 date = attr.value("date").toString();
81                 //unit = attr.value("unit").toString();
82                 unit = "s";
83                 value = attr.value("value").toString();
84
85                 if (userName.toUpper() == user.toUpper())//If user name match highlight result
86                 {
87                     myCategoryList->top10List.append("<tr><td><b>" + position + "</b></td><td><b>" +
88                                                     user + "</b></td><td><b>" +
89                                                     value + " " +
90                                                     unit + "</b></td><td><b>" +
91                                                     date + "</b></td></tr>");
92                 }
93                 else//If user name not match
94                 {
95                     myCategoryList->top10List.append("<tr><td>" + position + "</td><td>" +
96                                                     user + "</td><td>" +
97                                                     value + "&nbsp;" +
98                                                     unit + "</td><td>" +
99                                                     date + "</td></tr>");
100                 }
101                 /* Old way, no highlight
102                 myCategoryList->top10List.append(position + "\t" +
103                                                 user + "\t" +
104                                                 value + " " +
105                                                 unit + "\t" +
106                                                 date + "\n");*/
107
108                 qDebug() << position << user << value << unit << date;
109                 i++;
110                 receivedFlag = 1;
111             }
112         }
113     }
114     //Only change labelTopList if a new top10List has been received
115     if(receivedFlag)
116     {
117         qDebug() << "receivedTop10List() emitted";
118         emit receivedTop10List();
119     }
120 }
121
122 /**
123   *
124   *
125   */
126 void XmlReader::xmlReadCategories(QNetworkReply *device)
127 //void XmlReader::xmlReadCategories(QIODevice *device)
128 {
129     qDebug() << "_xmlReadCategories";
130
131     int i = 0;
132     int receivedFlag = 0;
133
134     xmlreader.clear();
135     QByteArray array = device->readAll();
136     qDebug() << "array: " << array;
137     xmlreader.addData(array);
138     //xmlreader.addData(device->readAll());
139
140     myCategoryList->clearCats();
141
142     //Go trough the xml document
143     while(!xmlreader.atEnd())
144     {
145         //Read next node
146         xmlreader.readNext();
147
148         //Check if this element is starting element
149         if(xmlreader.isStartElement())
150         {
151             if(xmlreader.name() == "categories")
152             {
153                 qDebug() << xmlreader.name();
154             }
155             if(xmlreader.name() == "category")
156             {
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;
164                 i++;
165                 receivedFlag = 1;
166             }
167         }
168     }
169     //Only change comboBoxTopCategory if a new list has been received
170     if(receivedFlag)
171     {
172         qDebug() << "receivedCategoryList() emitted";
173         myCategoryList->realSizeOfCats = i;
174         emit receivedCategoryList();
175     }
176 }
177
178 /**
179   *This function is used to read example xml file (results.xml).
180   *@todo Read real xml.
181   */
182 void XmlReader::xmlShow()
183 {
184     //QString filename = "results.xml";
185     QString filename = "xmlcategoryfile.xml";
186     QFile file(filename);
187
188     if (!file.open(QFile::ReadOnly))
189     {
190         qDebug() << "_xmlShow fail";
191         return;
192     }
193
194     //xmlReadTop10Results(&file);
195     //xmlReadCategories(&file);
196     //xmlReadProfile(&file);
197     file.close();
198 }
199
200 /**
201   * This function is used to read profile xml.
202   * @param QIODevice device: target of reading, here filename.
203   * @param ProfileDialog *profileDialog
204   */
205 void XmlReader::xmlReadProfile(QIODevice *device, ProfileDialog *profileDialog)
206 {
207     qDebug() << "_xmlReadProfile";
208
209     profile = profileDialog;
210
211     xmlreader.clear();
212     QByteArray array = device->readAll();
213
214     xmlreader.addData(array);
215
216     QString login;
217     QString picture;
218
219     // Go trough the xml document
220     while(!xmlreader.atEnd())
221     {
222         // Read next node
223         xmlreader.readNext();
224
225         // Check if this element is starting element
226         if(xmlreader.isStartElement())
227         {
228             if(xmlreader.name() == "profile")
229             {
230                 qDebug() << xmlreader.name();
231                 attr = xmlreader.attributes();
232                 login = attr.value("login").toString();
233                 picture = attr.value("picture").toString();
234             }
235             if(xmlreader.name() == "manufacturer")
236             {
237                 qDebug() << xmlreader.name();
238                 profile->setManufacturer(xmlreader.readElementText());
239             }
240             if(xmlreader.name() == "type")
241             {
242                 qDebug() << xmlreader.name();
243                 profile->setType(xmlreader.readElementText());
244
245             }
246             if(xmlreader.name() == "model")
247             {
248                 qDebug() << xmlreader.name();
249                 profile->setModel(xmlreader.readElementText());
250
251             }
252             if(xmlreader.name() == "description")
253             {
254                 qDebug() << xmlreader.name();
255                 profile->setDescription(xmlreader.readElementText());
256             }
257             if(xmlreader.name() == "picture" && picture == "yes")
258             {
259                 qDebug() << xmlreader.name();
260                 profile->setPicture(xmlreader.readElementText());
261             }
262         }
263     }
264     profile = NULL;
265 }