cb9745782dacd11487257307d8855fa93094d749
[speedfreak] / Client / xmlwriter.cpp
1 /*
2  * Xml writer
3  *
4  * @author     Tiina Kivilinna-Korhola
5  * @author     Toni Jussila <toni.jussila@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 "xmlwriter.h"
11
12 /**
13   *@brief Constructor, connects object to GUI
14   *@param Pointer to carmainwindow, which is temporarily used during development
15   */
16 XmlWriter::XmlWriter()
17 {
18
19 }
20
21 /**
22   *@brief Destructor
23   */
24 XmlWriter::~XmlWriter()
25 {
26
27 }
28
29 /**
30   *@brief Writes registration items into tags.
31   *@param netbuf a buffer where xmlstreamwriter writes to.
32   *@param usr for user name.
33   *@param psswd for password.
34   *@param email.
35   */
36 void XmlWriter::writeRegistering(QBuffer *netbuf, QString usr, QString psswd, QString email, QString description)
37 {
38     qDebug() << "_writeRegistering";
39
40     xmlwriter.setDevice(netbuf);
41
42     xmlwriter.writeStartDocument();
43     xmlwriter.writeStartElement("user");
44
45     xmlwriter.writeStartElement("login");
46     xmlwriter.writeCharacters(usr);
47     xmlwriter.writeEndElement();
48
49     xmlwriter.writeStartElement("password");
50     xmlwriter.writeCharacters(psswd);
51     xmlwriter.writeEndElement();
52
53     xmlwriter.writeStartElement("email");
54     xmlwriter.writeCharacters(email);
55     xmlwriter.writeEndElement();
56
57     xmlwriter.writeStartElement("description");
58     xmlwriter.writeCharacters(description);
59     xmlwriter.writeEndElement();
60
61     xmlwriter.writeEndElement();
62     xmlwriter.writeEndDocument();
63 }
64
65 /**
66   *@brief Writes Speed Freek results items as tags and contents into a buffer.
67   *@todo Consider looping when writing many values.
68   *@todo Replace test value to finally used variables.
69   */
70 void XmlWriter::writeResult(QBuffer *netbuf, double result)
71 {
72     qDebug() << "_writeResult";
73
74     xmlwriter.setDevice(netbuf);
75
76     xmlwriter.writeStartDocument();
77     xmlwriter.writeStartElement("result");
78     xmlwriter.writeAttribute("value", QString::number(result));
79     xmlwriter.writeEndElement();
80     xmlwriter.writeEndDocument();
81 }
82
83 /**
84   *@brief Write track to server.
85   *@param netbuf where to write.
86   *@param counter is GPSData::roundCounter.
87   *@todo Decide suitable attributes.
88   */
89 void XmlWriter::writeGpsTrack(QBuffer *netbuf, int counter, int start, int stop, int lat, int lon, int alt, int speed, int time)
90 {
91     qDebug() << "_writeGpsTrack";
92
93     double *ptrValue;
94     //ptrValue = ptrTable;
95     double tmp = 0;
96
97     xmlwriter.setDevice(netbuf);
98
99     xmlwriter.writeStartDocument();
100
101     xmlwriter.writeStartElement("Route");
102     xmlwriter.writeAttribute("starttime", QDateTime::currentDateTime().toString());
103     xmlwriter.writeAttribute("endtime", QDateTime::currentDateTime().toString());
104     xmlwriter.writeAttribute("points", QDateTime::currentDateTime().toString());
105     for(int i = 0; i < counter; i++)
106     {
107         xmlwriter.writeStartElement("point");
108         xmlwriter.writeAttribute("lat", QString::number(lat));
109         xmlwriter.writeAttribute("lon", QString::number(lon));
110         xmlwriter.writeAttribute("alt", QString::number(alt));
111         xmlwriter.writeAttribute("speed", QString::number(speed));
112         xmlwriter.writeAttribute("time", QString::number(time));
113         xmlwriter.writeEndElement();
114     }
115     xmlwriter.writeEndElement();
116     xmlwriter.writeEndDocument();
117 }
118
119 /**
120   *@brief Opens and closes a file, when xml information is written into a file,
121   *and passes file to writeXmlFile()
122   *@param usr for user name.
123   *@param psswd for password.
124   *@param email.
125   *@todo Replace hardcoced filename to finally GUI entry widget.
126   */
127 //void XmlWriter::writeXml(QString usr, QString psswd, QString email)
128 void XmlWriter::writeXml()
129 {
130     QString filename = "xmlcategoryfile.xml";
131     QFile file(filename);
132     if (!file.open(QFile::WriteOnly | QFile::Text)) {
133         qDebug() << "_xmlWrite fail";
134         return;
135     }
136
137     writeXmlFile(&file);
138     //writeRegistering(&file, usr, psswd, email);
139     //writeResult(&file);
140     file.close();
141 }
142
143 /**
144   *@brief Writes general xml information.
145   *Calls other functions to insert login and result information.
146   *@param device: target of writing, here filename.
147   *@param usr for user name.
148   *@param psswd for password.
149   *@param email.
150   */
151 bool XmlWriter::writeXmlFile(QIODevice *device)
152 {
153     xmlwriter.setDevice(device);
154     xmlwriter.writeStartDocument();
155     writeItems();
156     xmlwriter.writeEndDocument();
157
158     return true;
159 }
160
161 /**
162   *@brief Writes Speed Freek results items as tags and contents to earlier defined target.
163   *@todo Consider looping when writing many values.
164   *@todo Replace testing values to finally used variabls.
165   */
166 void XmlWriter::writeItems()
167 {
168     xmlwriter.writeStartElement("result");
169     xmlwriter.writeAttribute("value", QString::number(14)); //tmp testing value
170     xmlwriter.writeAttribute("unit", "seconds");
171     xmlwriter.writeAttribute("date", QDateTime::currentDateTime().toString());
172     xmlwriter.writeEndElement();
173 }
174
175 /**
176   * Write profile xml
177   * @param QIODevice device: target of writing, here filename.
178   * @param QString user name
179   * @param QString manufacturer
180   * @param QString type
181   * @param QString model
182   * @param QString description
183   * @param QString picture: filename.
184   */
185 void XmlWriter::writeProfileXmlFile(QIODevice *device, QString userName, QString manufacturer,
186                                     QString type, QString model, QString description, QString picture)
187 {
188     QString pictureStatus;
189     if(picture != "")
190         pictureStatus = "yes";
191     else
192         pictureStatus = "no";
193
194     /*example of XML:
195     <?xml version="1.0" encoding="UTF-8"?>
196     <profile login="test827" picture="yes">
197             <manufacturer>toyota</manufacturer>
198             <type>corolla</type>
199             <model>1983</model>
200             <description>Fuel efficient, GPS system, only one owner</description>
201     </profile>*/
202
203     xmlwriter.setDevice(device);
204     xmlwriter.writeStartDocument();
205         xmlwriter.writeStartElement("profile");
206         xmlwriter.writeAttribute("login", userName);
207         xmlwriter.writeAttribute("picture", pictureStatus);
208             xmlwriter.writeStartElement("manufacturer");
209                 xmlwriter.writeCharacters(manufacturer);
210             xmlwriter.writeEndElement();
211             xmlwriter.writeStartElement("type");
212                 xmlwriter.writeCharacters(type);
213             xmlwriter.writeEndElement();
214             xmlwriter.writeStartElement("model");
215                 xmlwriter.writeCharacters(model);
216             xmlwriter.writeEndElement();
217             xmlwriter.writeStartElement("description");
218                 xmlwriter.writeCharacters(description);
219             xmlwriter.writeEndElement();
220             xmlwriter.writeStartElement("picture");
221                 xmlwriter.writeCharacters(picture);
222             xmlwriter.writeEndElement();
223         xmlwriter.writeEndElement();
224     xmlwriter.writeEndDocument();
225 }
226
227 /**
228   * Write profile xml
229   * @param QIODevice device: target of writing, here netbuf.
230   * @param QString user name
231   * @param QString manufacturer
232   * @param QString type
233   * @param QString model
234   * @param QString description
235   * @param QString picture: filename.
236   */
237 void XmlWriter::writeProfileXmlFile(QBuffer *netbuf, QString userName, QString manufacturer,
238                                     QString type, QString model, QString description, QString picture)
239 {
240     QString pictureStatus;
241     if(picture != "")
242         pictureStatus = "yes";
243     else
244         pictureStatus = "no";
245
246     /*example of XML:
247     <?xml version="1.0" encoding="UTF-8"?>
248     <profile login="test827" picture="yes">
249             <manufacturer>toyota</manufacturer>
250             <type>corolla</type>
251             <model>1983</model>
252             <description>Fuel efficient, GPS system, only one owner</description>
253     </profile>*/
254
255     xmlwriter.setDevice(netbuf);
256     xmlwriter.writeStartDocument();
257         xmlwriter.writeStartElement("profile");
258         xmlwriter.writeAttribute("login", userName);
259         xmlwriter.writeAttribute("picture", pictureStatus);
260             xmlwriter.writeStartElement("manufacturer");
261                 xmlwriter.writeCharacters(manufacturer);
262             xmlwriter.writeEndElement();
263             xmlwriter.writeStartElement("type");
264                 xmlwriter.writeCharacters(type);
265             xmlwriter.writeEndElement();
266             xmlwriter.writeStartElement("model");
267                 xmlwriter.writeCharacters(model);
268             xmlwriter.writeEndElement();
269             xmlwriter.writeStartElement("description");
270                 xmlwriter.writeCharacters(description);
271             xmlwriter.writeEndElement();
272             xmlwriter.writeStartElement("picture");
273                 xmlwriter.writeCharacters(picture);
274             xmlwriter.writeEndElement();
275         xmlwriter.writeEndElement();
276     xmlwriter.writeEndDocument();
277 }