#include <QFile>
#include <QDebug>
#include "xmlreader.h"
+#include "profiledialog.h"
/**
*Constructor of this class.
*/
XmlReader::XmlReader()
{
- xmlShow();
- // Remove next line from final code
- categoryList << "Speed" << "acceleration-0-40" << "acceleration-0-100" << "G-force";
+ qDebug() << "__XmlReader";
+ myCategoryList = new CategoryList();
}
/**
*/
XmlReader::~XmlReader()
{
+ qDebug() << "__~XmlReader";
category = "";
unit = "";
- description = "";
position = "";
user = "";
value = "";
+
+ if(myCategoryList)
+ delete myCategoryList;
}
/**
- *This function is used to parsing xml file.
+ *This function is used to parse top 10 results of a certain category.
*/
-void XmlReader::xmlRead(QNetworkReply *device)
+void XmlReader::xmlReadTop10Results(QNetworkReply *device, QString userName)
{
- qDebug() << "_xmlRead";
+ qDebug() << "_xmlReadTop10Results";
+
+ int i = 0;
+ int receivedFlag = 0;
+
+ xmlreader.clear();
+ QByteArray array = device->readAll();
+ qDebug() << "array: " << array;
+ xmlreader.addData(array);
+ //xmlreader.addData(device->readAll());
- xmlreader.addData(device->readAll());
+ if(!(myCategoryList->top10List.isEmpty())) {
+ myCategoryList->top10List.clear();
+ }
//Go trough the xml document
while(!xmlreader.atEnd())
if(xmlreader.name() == "results")
{
qDebug() << xmlreader.name();
- attr = xmlreader.attributes();
-
- category = attr.value("category").toString();
- unit = attr.value("unit").toString();
- description = attr.value("description").toString();
-
- top10List << category;
- qDebug() << top10List << unit << description;
}
if(xmlreader.name() == "result")
{
- qDebug() << "result";
+ qDebug() << xmlreader.name();
attr = xmlreader.attributes();
+ user = attr.value("username").toString();
position = attr.value("position").toString();
- user = attr.value("user").toString();
+ date = attr.value("date").toString();
+ //unit = attr.value("unit").toString();
+ unit = "s";
value = attr.value("value").toString();
- if (category == "acceleration-0-100")
+ if (userName.toUpper() == user.toUpper())//If user name match highlight result
{
- top10AccelerationList.append(position + "\t" +
- user + "\t" +
- value +
- unit + "\t" +
- description + "\n");
+ myCategoryList->top10List.append("<tr><td><b>" + position + "</b></td><td><b>" +
+ user + "</b></td><td><b>" +
+ value + " " +
+ unit + "</b></td><td><b>" +
+ date + "</b></td></tr>");
}
-
- if(category == "top10speed")
+ else//If user name not match
{
- top10SpeedList.append(position + "\t" +
- user + "\t" +
- value +
- unit + "\t" +
- description + "\n");
+ myCategoryList->top10List.append("<tr><td>" + position + "</td><td>" +
+ user + "</td><td>" +
+ value + " " +
+ unit + "</td><td>" +
+ date + "</td></tr>");
}
+ /* Old way, no highlight
+ myCategoryList->top10List.append(position + "\t" +
+ user + "\t" +
+ value + " " +
+ unit + "\t" +
+ date + "\n");*/
- if(category == "top10gforce")
- {
- top10GforceList.append(position + "\t" +
- user + "\t" +
- value +
- unit + "\t" +
- description + "\n");
- }
- qDebug() << position << user << value << unit;
+ qDebug() << position << user << value << unit << date;
+ i++;
+ receivedFlag = 1;
}
}
}
+ //Only change labelTopList if a new top10List has been received
+ if(receivedFlag)
+ {
+ qDebug() << "receivedTop10List() emitted";
+ emit receivedTop10List();
+ }
+}
+
+/**
+ *
+ *
+ */
+void XmlReader::xmlReadCategories(QNetworkReply *device)
+//void XmlReader::xmlReadCategories(QIODevice *device)
+{
+ qDebug() << "_xmlReadCategories";
+
+ int i = 0;
+ int receivedFlag = 0;
+
+ xmlreader.clear();
+ QByteArray array = device->readAll();
+ qDebug() << "array: " << array;
+ xmlreader.addData(array);
+ //xmlreader.addData(device->readAll());
+
+ myCategoryList->clearCats();
+
+ //Go trough the xml document
+ while(!xmlreader.atEnd())
+ {
+ //Read next node
+ xmlreader.readNext();
+
+ //Check if this element is starting element
+ if(xmlreader.isStartElement())
+ {
+ if(xmlreader.name() == "categories")
+ {
+ qDebug() << xmlreader.name();
+ }
+ if(xmlreader.name() == "category")
+ {
+ qDebug() << xmlreader.name();
+ attr = xmlreader.attributes();
+ description = attr.value("description").toString();
+ unit = attr.value("unit").toString();
+ category = xmlreader.readElementText();
+ myCategoryList->appendCats(i, description, unit, category);
+ qDebug() << "description: " << description << "unit: " << unit << "category: " << category;
+ i++;
+ receivedFlag = 1;
+ }
+ }
+ }
+ //Only change comboBoxTopCategory if a new list has been received
+ if(receivedFlag)
+ {
+ qDebug() << "receivedCategoryList() emitted";
+ myCategoryList->realSizeOfCats = i;
+ emit receivedCategoryList();
+ }
}
/**
*/
void XmlReader::xmlShow()
{
- QString filename = "results.xml";
+ //QString filename = "results.xml";
+ QString filename = "xmlcategoryfile.xml";
QFile file(filename);
if (!file.open(QFile::ReadOnly))
return;
}
- //xmlRead(&file);
+ //xmlReadTop10Results(&file);
+ //xmlReadCategories(&file);
+ //xmlReadProfile(&file);
file.close();
}
/**
- *This is return function.
- *@todo Read real category list
- *@return QStringList categoryList
+ * This function is used to read profile xml.
+ * @param QIODevice device: target of reading, here filename.
+ * @param ProfileDialog *profileDialog
*/
-QStringList XmlReader::getCategoryList()
+void XmlReader::xmlReadProfile(QIODevice *device, ProfileDialog *profileDialog)
{
- return categoryList;
-}
+ qDebug() << "_xmlReadProfile";
-/**
- *This is return function.
- *@todo Read real top 10 category list
- *@return QStringList top10List
- */
-QStringList XmlReader::getTop10List()
-{
- //During development is needed some values for categorylist
- top10List.clear();
- top10List << "acceleration-0-10" << "acceleration-0-60" << "acceleration-0-100";
+ profile = profileDialog;
- return top10List;
-}
+ xmlreader.clear();
+ QByteArray array = device->readAll();
-QString XmlReader::getTopList( QString category, int size)
-{
- if (category == "acceleration-0-100")
- {
- //return top10AccelerationList;
- return "acc-tulos1\nacc-tulos2\nacc-tulos3\nacc-tulos4\nacc-tulos5\nacc-tulos6\nacc-tulos7\nacc-tulos8\nacc-tulos9\nacc-tulos10";
- }
- else if (category == "acceleration-0-40")
- {
- //return top10AccelerationList;
- return "acc-40-tulos1\nacc-40-tulos2\nacc-40-tulos3\nacc-40-tulos4\nacc-40-tulos5\nacc-40-tulos6\nacc-40-tulos7\nacc-40-tulos8\nacc-40-tulos9\nacc-40-tulos10";
- }
- else if (category == "Speed")
- {
- //return top10SpeedList;
- return "speed-tulos1\nspeed-tulos2\nspeed-tulos3\nspeed-tulos4\nspeed-tulos5\nspeed-tulos6\nspeed-tulos7\nspeed-tulos8\nspeed-tulos9\nspeed-tulos10";
- }
- else if (category == "G-force")
+ xmlreader.addData(array);
+
+ QString login;
+ QString picture;
+
+ // Go trough the xml document
+ while(!xmlreader.atEnd())
{
- //return top10GforceList;
- return "g-tulos1\ng-tulos2\ng-tulos3\ng-tulos4\ng-tulos5\ng-tulos6\ng-tulos7\ng-tulos8\ng-tulos9\ng-tulos10";
+ // Read next node
+ xmlreader.readNext();
+
+ // Check if this element is starting element
+ if(xmlreader.isStartElement())
+ {
+ if(xmlreader.name() == "profile")
+ {
+ qDebug() << xmlreader.name();
+ attr = xmlreader.attributes();
+ login = attr.value("login").toString();
+ picture = attr.value("picture").toString();
+ }
+ if(xmlreader.name() == "manufacturer")
+ {
+ qDebug() << xmlreader.name();
+ profile->setManufacturer(xmlreader.readElementText());
+ }
+ if(xmlreader.name() == "type")
+ {
+ qDebug() << xmlreader.name();
+ profile->setType(xmlreader.readElementText());
+
+ }
+ if(xmlreader.name() == "model")
+ {
+ qDebug() << xmlreader.name();
+ profile->setModel(xmlreader.readElementText());
+
+ }
+ if(xmlreader.name() == "description")
+ {
+ qDebug() << xmlreader.name();
+ profile->setDescription(xmlreader.readElementText());
+ }
+ if(xmlreader.name() == "picture" && picture == "yes")
+ {
+ qDebug() << xmlreader.name();
+ profile->setPicture(xmlreader.readElementText());
+ }
+ }
}
+ profile = NULL;
}