Improved functions that pack info into xml tags.
[speedfreak] / Client / xmlwriter.cpp
1 #include <QtGui>
2 #include <QNetworkRequest>
3 #include <QIODevice>
4 #include <QFile>
5 #include <QMessageBox>
6 #include <QDebug>
7 #include <QDateTime>
8 #include <QDate>
9 #include <QTime>
10 #include <QApplication>
11 #include "xmlwriter.h"
12
13
14
15 /**
16   *@brief Constructor, connects object to GUI
17   *@param Pointer to carmainwindow, which is temporarily used during development
18   */
19 XmlWriter::XmlWriter()
20 {
21
22 }
23
24 /**
25   *@brief Destructor
26   */
27 XmlWriter::~XmlWriter()
28 {
29
30 }
31 /**
32   *@brief Writes registration items into tags.
33   *@param netbuf a buffer where xmlstreamwriter writes to.
34   *@param usr for user name.
35   *@param psswd for password.
36   *@param email.
37   */
38 void XmlWriter::writeRegistering(QBuffer *netbuf, QString usr, QString psswd, QString email)
39 //void XmlWriter::writeRegistering(QIODevice *netbuf, QString usr, QString psswd, QString email)
40 {
41     xmlwriter.setDevice(netbuf);
42
43     xmlwriter.writeStartDocument();
44     xmlwriter.writeStartElement("user");
45
46     xmlwriter.writeStartElement("login");
47     xmlwriter.writeCharacters(usr);
48     xmlwriter.writeEndElement();
49
50     xmlwriter.writeStartElement("password");
51     xmlwriter.writeCharacters(psswd);
52     xmlwriter.writeEndElement();
53
54     xmlwriter.writeStartElement("email");
55     xmlwriter.writeCharacters(email);
56     xmlwriter.writeEndElement();
57
58     xmlwriter.writeEndElement();
59     xmlwriter.writeEndDocument();
60 }
61
62
63 /**
64   *@brief Writes Speed Freek results items as tags and contents into a buffer.
65   *@todo Consider looping when writing many values.
66   *@todo Replace test value to finally used variables.
67   */
68 void XmlWriter::writeResult(QBuffer *netbuf)
69 //void XmlWriter::writeResult(QIODevice *netbuf)
70 {
71     xmlwriter.setDevice(netbuf);
72
73     xmlwriter.writeStartDocument();
74     xmlwriter.writeStartElement("result");
75     xmlwriter.writeAttribute("value", QString::number(14));
76     xmlwriter.writeAttribute("unit", "seconds");
77     xmlwriter.writeAttribute("date", QDateTime::currentDateTime().toString());
78     xmlwriter.writeEndElement();
79     xmlwriter.writeEndDocument();
80 }
81
82
83 /**
84   *@brief Opens and closes a file, when xml information is written into a file,
85   *and passes file to writeXmlFile()
86   *@param usr for user name.
87   *@param psswd for password.
88   *@param email.
89   *@todo Replace hardcoced filename to finally GUI entry widget.
90   */
91 void XmlWriter::writeXml(QString usr, QString psswd, QString email)
92 {
93     QString filename = "xmlfile.xml";
94     QFile file(filename);
95     if (!file.open(QFile::WriteOnly | QFile::Text)) {
96         qDebug() << "_xmlWrite fail";
97         return;
98     }
99
100     writeXmlFile(&file);
101     //writeRegistering(&file, usr, psswd, email);
102     //writeResult(&file);
103     file.close();
104 }
105
106 /**
107   *@brief Writes general xml information.
108   *Calls other functions to insert login and result information.
109   *@param device: target of writing, here filename.
110   *@param usr for user name.
111   *@param psswd for password.
112   *@param email.
113   */
114 bool XmlWriter::writeXmlFile(QIODevice *device)
115 {
116     xmlwriter.setDevice(device);
117     xmlwriter.writeStartDocument();
118     writeItems();
119     xmlwriter.writeEndDocument();
120
121     return true;
122 }
123
124
125 /**
126   *@brief Writes Speed Freek results items as tags and contents to earlier defined target.
127   *@todo Consider looping when writing many values.
128   *@todo Replace testing values to finally used variabls.
129   */
130 void XmlWriter::writeItems()
131 {
132     xmlwriter.writeStartElement("result");
133     xmlwriter.writeAttribute("value", QString::number(14)); //tmp testing value
134     xmlwriter.writeAttribute("unit", "seconds");
135     xmlwriter.writeAttribute("date", QDateTime::currentDateTime().toString());
136     xmlwriter.writeEndElement();
137 }
138
139
140 /**
141   *@brief A temp function during development, used to create a "serverfile".
142   */
143 void XmlWriter::serverWritesTop()
144 {
145     int i = 0;
146     int n = 5;
147
148     /* Server sends to client */
149     xmlwriter.writeStartElement("results");
150     xmlwriter.writeAttribute("category", "acceleration-0-40");
151     xmlwriter.writeAttribute("unit", "seconds");
152     xmlwriter.writeAttribute("description", "Acceleration from 0 to 100 km/h");
153
154     for (i = 0; i < n; i++) {
155         xmlwriter.writeStartElement("result");
156         xmlwriter.writeAttribute("position", QString::number(i));
157         xmlwriter.writeAttribute("user", "test123");
158         xmlwriter.writeAttribute("date", QDateTime::currentDateTime().toString());
159         xmlwriter.writeAttribute("value", QString::number(i+i+1));
160         xmlwriter.writeEndElement();
161     }
162 }