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