Send result button added in result dialog.
[speedfreak] / Client / xmlwriter.cpp
1 #include <QtGui>
2 #include <QNetworkRequest>
3 //#include <QNetworkReply>
4 #include <QIODevice>
5 #include <QFile>
6 #include <QMessageBox>
7 #include <QDebug>
8 #include <QDateTime>
9 #include <QDate>
10 #include <QTime>
11 #include <QApplication>
12 #include "xmlwriter.h"
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(Ui_CarMainWindow* myMainWindow)
20 {
21     ui = myMainWindow;
22 }
23
24
25 /**
26   *@brief Destructor
27   */
28 XmlWriter::~XmlWriter()
29 {
30
31 }
32
33 /**
34   *@brief Opens and closes a file, when xml information is written into a file,
35   *and passes file to writeXmlFile()
36   *@note Partly harcoded and commented for git.
37   *@todo Replace hardcoced filename and GUI elements to finally used widgets.
38   */
39 void XmlWriter::writeXml()
40 {
41     QString filename = "xmlfile.xml";
42     QFile file(filename);
43     if (!file.open(QFile::WriteOnly | QFile::Text)) {
44         qDebug() << "_xmlWrite fail";
45         return;
46     }
47
48     writeXmlFile(&file);
49     file.close();
50 }
51
52 /**
53   *@brief Writes general xml information.
54   *Calls other functions to insert login and result information.
55   *@todo Check API connection to QBuffer, when Speed Freek network client has been written.
56   */
57 bool XmlWriter::writeXmlFile(QIODevice *device)
58 //bool XmlWriter::writeXmlFile(QBuffer *device)
59 {
60     xmlwriter.setDevice(device);
61     xmlwriter.writeStartDocument();
62     xmlwriter.writeStartElement("xml");
63     xmlwriter.writeAttribute("version", "1.0");
64     writeRegister();
65     writeItems();
66     xmlwriter.writeEndDocument();
67
68     return true;
69 }
70
71 /**
72   *@brief Writes Speed Freek application specific items as tags and contents.
73   *@brief Results of speed/ direction/ acceleration into QMap are calculated elsewhere
74   *@todo Replace hardcoced user, password and email to finally used GUI elements.
75   */
76 void XmlWriter::writeRegister()
77 {
78     xmlwriter.writeStartElement("user");
79
80     xmlwriter.writeStartElement("login");
81     xmlwriter.writeCharacters("test123");
82     xmlwriter.writeEndElement();
83
84     xmlwriter.writeStartElement("password");
85     xmlwriter.writeCharacters("thisisaveryinsecurepassword");
86     xmlwriter.writeEndElement();
87
88     //Is this neacessary when sending results
89     xmlwriter.writeStartElement("email");
90     xmlwriter.writeCharacters("test@example.com");
91     xmlwriter.writeEndElement();
92
93     xmlwriter.writeEndElement();
94 }
95
96 /**
97   *@brief Writes Speed Freek results items as tags and contents.
98   *@brief Results of speed/ direction/ acceleration into QMap are calculated elsewhere
99   *@todo Consider looping of writing QMap values.
100   *@todo Replace hardcoced names to finally used values.
101   */
102 void XmlWriter::writeItems()
103 {
104     //During development
105     this->fillResultmap();
106
107     xmlwriter.writeStartElement("result");
108     xmlwriter.writeAttribute("value", QString::number(resultmap.value("speed")));
109     xmlwriter.writeAttribute("unit", "seconds");
110     xmlwriter.writeAttribute("date", QDateTime::currentDateTime().toString());
111     xmlwriter.writeEndElement();
112 }
113
114
115 /**
116   *@brief A temp function during development, used until real QMap available.
117   */
118 void XmlWriter::fillResultmap()
119 {
120     resultmap["acceleration"] = 9;
121     resultmap["speed"] = 48;
122     resultmap["distance"] = 600;
123 }
124
125 /**
126   *@brief A temp function during development, used to create a "serverfile".
127   */
128 void XmlWriter::serverWritesTop()
129 {
130     int i = 0;
131     int n = 5;
132
133     /* Server sends to client */
134     xmlwriter.writeStartElement("results");
135     xmlwriter.writeAttribute("category", "acceleration-0-100");
136     xmlwriter.writeAttribute("unit", "seconds");
137     xmlwriter.writeAttribute("description", "Acceleration from 0 to 100 km/h");
138
139     for (i = 0; i < n; i++) {
140         xmlwriter.writeStartElement("result");
141         xmlwriter.writeAttribute("position", QString::number(i));
142         xmlwriter.writeAttribute("user", "test123");
143         xmlwriter.writeAttribute("date", QDateTime::currentDateTime().toString());
144         xmlwriter.writeAttribute("value", QString::number(i+i+1));
145         xmlwriter.writeEndElement();
146     }
147 }