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