#include "parse.h"
#include "ui_taxi.h"
#include <QtGui>
#include <QtCore>
{
}
void parse::parseXML() {
/* We'll parse the xml file that was made from the sync application */
/* If we can't open it, let's show an error message. */
// QMessageBox::critical(this,
// "QXSRimpl::parseXML",
// "Couldn't open strings.xml",
// QMessageBox::Ok);
//If Cant open file
qDebug() << "Couldn't Open /ttm/myXML.xml";
return;
}
/* QXmlStreamReader takes any QIODevice. */
QXmlStreamReader xml(file);
QList< QMap<QString,QString> > clients;
/* We'll parse the XML until we reach end of it.*/
while(!xml.atEnd() &&
!xml.hasError()) {
/* Read next element.*/
QXmlStreamReader::TokenType token = xml.readNext();
/* If token is just StartDocument, we'll go to next.*/
if(token == QXmlStreamReader::StartDocument) {
continue;
}
/* If token is StartElement, we'll see if we can read it.*/
if(token == QXmlStreamReader::StartElement) {
/* If it's named Clients, we'll go to the next.*/
if(xml.name() == "Clients") {
continue;
}
/* If it's named Client, we'll dig the information from there.*/
if(xml.name() == "Client") {
clients.append(this->parseClient(xml));
}
}
}
/* Error handling. */
if(xml.hasError()) {
// QMessageBox::critical(this,
// "parse::parseXML",
// xml.errorString(),
// QMessageBox::Ok);
//IF ERROR
qDebug() << xml.errorString();
}
/* Removes any device() or data from the reader
* and resets its internal state to the initial state. */
xml.clear();
this->addClientsToUI(clients);
}
QMap<QString, QString> parse::parseClient(QXmlStreamReader& xml) {
QMap<QString, QString> client;
/* Let's check that we're really getting a person. */
if(xml.tokenType() != QXmlStreamReader::StartElement &&
xml.name() == "Client") {
return client;
}
// /* Let's get the attributes for person */
// QXmlStreamAttributes attributes = xml.attributes();
// /* Let's check that person has id attribute. */
// if(attributes.hasAttribute("Client_ID")) {
// /* We'll add it to the map. */
// client["Client_ID"] = attributes.value("Client_ID").toString();
// }
/* Next element... */
xml.readNext();
/*
* We're going to loop over the things because the order might change.
* We'll continue the loop until we hit an EndElement named person.
*/
while(!(xml.tokenType() == QXmlStreamReader::EndElement &&
xml.name() == "Client")) {
if(xml.tokenType() == QXmlStreamReader::StartElement) {
/* We've found Client ID. */
if(xml.name() == "Client_ID") {
this->addElementDataToMap(xml, client);
}
/* We've found Network ID. */
if(xml.name() == "Network_ID") {
this->addElementDataToMap(xml, client);
}
/* We've found Video. */
if(xml.name() == "Video") {
this->addElementDataToMap(xml, client);
}
/* We've found Description. */
if(xml.name() == "Description") {
this->addElementDataToMap(xml, client);
}
/* We've found Zone6. */
if(xml.name() == "Zone6") {
this->addElementDataToMap(xml, client);
}
/* We've found status. */
if(xml.name() == "status") {
this->addElementDataToMap(xml, client);
}
}
/* ...and next... */
xml.readNext();
}
return client;
}
void parse::addElementDataToMap(QXmlStreamReader& xml,
QMap<QString, QString>& map) const {
/* We need a start element, like <foo> */
if(xml.tokenType() != QXmlStreamReader::StartElement) {
return;
}
/* Let's read the name... */
QString elementName
= xml.
name().
toString();
/* ...go to the next. */
xml.readNext();
/*
* This elements needs to contain Characters so we know it's
* actually data, if it's not we'll leave.
*/
if(xml.tokenType() != QXmlStreamReader::Characters) {
return;
}
/* Now we can add it to the map.*/
map.insert(elementName, xml.text().toString());
}
void parse::addClientsToUI(QList< QMap<QString,QString> >& clients) {
while(!clients.isEmpty()) {
QFormLayout* layout = new QFormLayout;
QMap<QString,QString> client = clients.takeFirst();
layout
->addRow
("Client ID",
new QLineEdit(client
["Client_ID"]));
layout
->addRow
("Network ID",
new QLineEdit(client
["Network_ID"]));
layout
->addRow
("Video",
new QLineEdit(client
["Video"]));
layout
->addRow
("Description",
new QLineEdit(client
["Description"]));
layout
->addRow
("Zone6",
new QLineEdit(client
["Zone6"]));
layout
->addRow
("status",
new QLineEdit(client
["status"]));
clientGB->setLayout(layout);
this->_layout->addWidget(clientGB);
}
}