Route and Results buttons updated.
[speedfreak] / Client / xmlwriter.cpp
index f860975..3abe02a 100644 (file)
@@ -1,26 +1,22 @@
-#include <QtGui>
-#include <QNetworkRequest>
-//#include <QNetworkReply>
-#include <QIODevice>
-#include <QFile>
-#include <QMessageBox>
-#include <QDebug>
-#include <QDateTime>
-#include <QDate>
-#include <QTime>
-#include <QApplication>
-#include "xmlwriter.h"
+/*
+ * Xml writer
+ *
+ * @author     Tiina Kivilinna-Korhola
+ * @author     Toni Jussila <toni.jussila@fudeco.com>
+ * @copyright  (c) 2010 Speed Freak team
+ * license     http://opensource.org/licenses/gpl-license.php GNU Public License
+ */
 
+#include "xmlwriter.h"
 
 /**
   *@brief Constructor, connects object to GUI
   *@param Pointer to carmainwindow, which is temporarily used during development
   */
-XmlWriter::XmlWriter(Ui_CarMainWindow* myMainWindow)
+XmlWriter::XmlWriter()
 {
-    ui = myMainWindow;
-}
 
+}
 
 /**
   *@brief Destructor
@@ -31,117 +27,143 @@ XmlWriter::~XmlWriter()
 }
 
 /**
-  *@brief Opens and closes a file, when xml information is written into a file,
-  *and passes file to writeXmlFile()
-  *@note Partly harcoded and commented for git.
-  *@todo Replace hardcoced filename and GUI elements to finally used widgets.
+  *@brief Writes registration items into tags.
+  *@param netbuf a buffer where xmlstreamwriter writes to.
+  *@param usr for user name.
+  *@param psswd for password.
+  *@param email.
   */
-void XmlWriter::writeXml()
+void XmlWriter::writeRegistering(QBuffer *netbuf, QString usr, QString psswd, QString email)
 {
-    QString filename = "xmlfile.xml";
-    QFile file(filename);
-    if (!file.open(QFile::WriteOnly | QFile::Text)) {
-        qDebug() << "_xmlWrite fail";
-        return;
-    }
+    qDebug() << "_writeRegistering";
 
-    writeXmlFile(&file);
-    file.close();
-}
+    xmlwriter.setDevice(netbuf);
 
-/**
-  *@brief Writes general xml information.
-  *Calls other functions to insert login and result information.
-  *@todo Check API connection to QBuffer, when Speed Freek network client has been written.
-  */
-bool XmlWriter::writeXmlFile(QIODevice *device)
-//bool XmlWriter::writeXmlFile(QBuffer *device)
-{
-    xmlwriter.setDevice(device);
     xmlwriter.writeStartDocument();
-    xmlwriter.writeStartElement("xml");
-    xmlwriter.writeAttribute("version", "1.0");
-    writeRegister();
-    writeItems();
-    xmlwriter.writeEndDocument();
-
-    return true;
-}
-
-/**
-  *@brief Writes Speed Freek application specific items as tags and contents.
-  *@brief Results of speed/ direction/ acceleration into QMap are calculated elsewhere
-  *@todo Replace hardcoced user, password and email to finally used GUI elements.
-  */
-void XmlWriter::writeRegister()
-{
     xmlwriter.writeStartElement("user");
 
     xmlwriter.writeStartElement("login");
-    xmlwriter.writeCharacters("test123");
+    xmlwriter.writeCharacters(usr);
     xmlwriter.writeEndElement();
 
     xmlwriter.writeStartElement("password");
-    xmlwriter.writeCharacters("thisisaveryinsecurepassword");
+    xmlwriter.writeCharacters(psswd);
     xmlwriter.writeEndElement();
 
-    //Is this neacessary when sending results
     xmlwriter.writeStartElement("email");
-    xmlwriter.writeCharacters("test@example.com");
+    xmlwriter.writeCharacters(email);
     xmlwriter.writeEndElement();
 
     xmlwriter.writeEndElement();
+    xmlwriter.writeEndDocument();
 }
 
 /**
-  *@brief Writes Speed Freek results items as tags and contents.
-  *@brief Results of speed/ direction/ acceleration into QMap are calculated elsewhere
-  *@todo Consider looping of writing QMap values.
-  *@todo Replace hardcoced names to finally used values.
+  *@brief Writes Speed Freek results items as tags and contents into a buffer.
+  *@todo Consider looping when writing many values.
+  *@todo Replace test value to finally used variables.
   */
-void XmlWriter::writeItems()
+void XmlWriter::writeResult(QBuffer *netbuf, double result)
 {
-    //During development
-    this->fillResultmap();
+    qDebug() << "_writeResult";
+
+    xmlwriter.setDevice(netbuf);
 
+    xmlwriter.writeStartDocument();
     xmlwriter.writeStartElement("result");
-    xmlwriter.writeAttribute("value", QString::number(resultmap.value("speed")));
-    xmlwriter.writeAttribute("unit", "seconds");
-    xmlwriter.writeAttribute("date", QDateTime::currentDateTime().toString());
+    xmlwriter.writeAttribute("value", QString::number(result));
     xmlwriter.writeEndElement();
+    xmlwriter.writeEndDocument();
 }
 
+/**
+  *@brief Write track to server.
+  *@param netbuf where to write.
+  *@param counter is GPSData::roundCounter.
+  *@todo Decide suitable attributes.
+  */
+void XmlWriter::writeGpsTrack(QBuffer *netbuf, int counter, int start, int stop, int lat, int lon, int alt, int speed, int time)
+{
+    qDebug() << "_writeGpsTrack";
+
+    double *ptrValue;
+    //ptrValue = ptrTable;
+    double tmp = 0;
+
+    xmlwriter.setDevice(netbuf);
+
+    xmlwriter.writeStartDocument();
+
+    xmlwriter.writeStartElement("Route");
+    xmlwriter.writeAttribute("starttime", QDateTime::currentDateTime().toString());
+    xmlwriter.writeAttribute("endtime", QDateTime::currentDateTime().toString());
+    xmlwriter.writeAttribute("points", QDateTime::currentDateTime().toString());
+    for(int i = 0; i < counter; i++)
+    {
+        xmlwriter.writeStartElement("point");
+        xmlwriter.writeAttribute("lat", QString::number(lat));
+        xmlwriter.writeAttribute("lon", QString::number(lon));
+        xmlwriter.writeAttribute("alt", QString::number(alt));
+        xmlwriter.writeAttribute("speed", QString::number(speed));
+        xmlwriter.writeAttribute("time", QString::number(time));
+        xmlwriter.writeEndElement();
+    }
+    xmlwriter.writeEndElement();
+    xmlwriter.writeEndDocument();
+}
 
 /**
-  *@brief A temp function during development, used until real QMap available.
+  *@brief Opens and closes a file, when xml information is written into a file,
+  *and passes file to writeXmlFile()
+  *@param usr for user name.
+  *@param psswd for password.
+  *@param email.
+  *@todo Replace hardcoced filename to finally GUI entry widget.
   */
-void XmlWriter::fillResultmap()
+//void XmlWriter::writeXml(QString usr, QString psswd, QString email)
+void XmlWriter::writeXml()
 {
-    resultmap["acceleration"] = 9;
-    resultmap["speed"] = 48;
-    resultmap["distance"] = 600;
+    QString filename = "xmlcategoryfile.xml";
+    QFile file(filename);
+    if (!file.open(QFile::WriteOnly | QFile::Text)) {
+        qDebug() << "_xmlWrite fail";
+        return;
+    }
+
+    writeXmlFile(&file);
+    //writeRegistering(&file, usr, psswd, email);
+    //writeResult(&file);
+    file.close();
 }
 
 /**
-  *@brief A temp function during development, used to create a "serverfile".
+  *@brief Writes general xml information.
+  *Calls other functions to insert login and result information.
+  *@param device: target of writing, here filename.
+  *@param usr for user name.
+  *@param psswd for password.
+  *@param email.
   */
-void XmlWriter::serverWritesTop()
+bool XmlWriter::writeXmlFile(QIODevice *device)
 {
-    int i = 0;
-    int n = 5;
+    xmlwriter.setDevice(device);
+    xmlwriter.writeStartDocument();
+    writeItems();
+    xmlwriter.writeEndDocument();
+
+    return true;
+}
 
-    /* Server sends to client */
-    xmlwriter.writeStartElement("results");
-    xmlwriter.writeAttribute("category", "acceleration-0-100");
+/**
+  *@brief Writes Speed Freek results items as tags and contents to earlier defined target.
+  *@todo Consider looping when writing many values.
+  *@todo Replace testing values to finally used variabls.
+  */
+void XmlWriter::writeItems()
+{
+    xmlwriter.writeStartElement("result");
+    xmlwriter.writeAttribute("value", QString::number(14)); //tmp testing value
     xmlwriter.writeAttribute("unit", "seconds");
-    xmlwriter.writeAttribute("description", "Acceleration from 0 to 100 km/h");
-
-    for (i = 0; i < n; i++) {
-        xmlwriter.writeStartElement("result");
-        xmlwriter.writeAttribute("position", QString::number(i));
-        xmlwriter.writeAttribute("user", "test123");
-        xmlwriter.writeAttribute("date", QDateTime::currentDateTime().toString());
-        xmlwriter.writeAttribute("value", QString::number(i+i+1));
-        xmlwriter.writeEndElement();
-    }
+    xmlwriter.writeAttribute("date", QDateTime::currentDateTime().toString());
+    xmlwriter.writeEndElement();
 }