Initial commit
[golf-scorecard] / src / xmlparser.cpp
1 #include "xmlparser.h"\r
2 #include "datahandling.h"\r
3 \r
4 #include <QFileDialog>\r
5 #include <QMessageBox>\r
6 #include <QDomNode>\r
7 #include <QCoreApplication>\r
8 \r
9 #include <QDebug>\r
10 \r
11 #define N900\r
12 \r
13 xmlparser::xmlparser()\r
14 {\r
15     fileName = (QCoreApplication::applicationDirPath() + "/xml/players/playerdata.xml");\r
16     fileNameCourses = (QCoreApplication::applicationDirPath() + "/xml/courses/coursedata.xml");\r
17 \r
18     #ifdef N900\r
19         fileName = "/opt/GolfScorecard/xml/players/playerdata.xml";\r
20         fileNameCourses = "/opt/GolfScorecard/xml/courses/coursedata.xml";\r
21     #endif\r
22 }\r
23 \r
24 void xmlparser::loadPlayerXMLFile()\r
25 {\r
26     datahandling *playerdata = datahandling::instance();\r
27 \r
28     if (fileName.isEmpty()){\r
29         QMessageBox::warning(this, tr("Player file"),\r
30                              tr("EMPTY! Cannot read file."));\r
31 \r
32     }\r
33 \r
34     QFile file(fileName);\r
35 \r
36     if (!file.open(QFile::ReadOnly | QFile::Text)) {\r
37         QMessageBox::warning(this, tr("Player file"),\r
38                              tr("Cannot read file %1:\n%2.")\r
39                              .arg(fileName)\r
40                              .arg(file.errorString()));\r
41 \r
42     }\r
43 \r
44     QDomDocument doc("Players");\r
45 \r
46     if( !doc.setContent( &file ) )\r
47     {\r
48       file.close();\r
49       QMessageBox::warning(this, tr("Parsing failed"), tr("Parsing failed totally"));\r
50     }\r
51     file.close();\r
52 \r
53     QDomElement root = doc.documentElement();\r
54     if( root.tagName() != "players" )\r
55       QMessageBox::warning(this, tr("No root found!"), tr("There was no root in XML found!"));\r
56 \r
57     QDomElement child = root.firstChildElement("player");\r
58     while (!child.isNull())\r
59     {\r
60         playerdata->addPlayer(child.attribute("name",""),child.attribute("hcp",""),child.attribute("gender", ""));\r
61         child = child.nextSiblingElement("player");\r
62     }\r
63 }\r
64 \r
65 void xmlparser::loadCourseXMLFile()\r
66 {\r
67     datahandling *coursedata = datahandling::instance();\r
68 \r
69     if (fileNameCourses.isEmpty()){\r
70         QMessageBox::warning(this, tr("Course file"),\r
71                              tr("EMPTY! Cannot read file."));\r
72 \r
73     }\r
74 \r
75     QFile file(fileNameCourses);\r
76 \r
77     if (!file.open(QFile::ReadOnly | QFile::Text)) {\r
78         QMessageBox::warning(this, tr("Course file"),\r
79                              tr("Cannot read file %1:\n%2.")\r
80                              .arg(fileNameCourses)\r
81                              .arg(file.errorString()));\r
82 \r
83     }\r
84 \r
85     QDomDocument doc("Courses");\r
86 \r
87     if( !doc.setContent( &file ) )\r
88     {\r
89       file.close();\r
90       QMessageBox::warning(this, tr("Parsing failed"), tr("Parsing failed totally"));\r
91     }\r
92     file.close();\r
93 \r
94     QDomElement root = doc.documentElement();\r
95     if( root.tagName() != "courses" )\r
96       QMessageBox::warning(this, tr("No root found!"), tr("There was no root in XML found!"));\r
97 \r
98     QDomElement child = root.firstChildElement("course");\r
99     while (!child.isNull())\r
100     {\r
101         coursedata->addCourses(child.attribute("name", ""), child.attribute("course_file", ""));\r
102         child = child.nextSiblingElement("course");\r
103     }\r
104 }\r
105 \r
106 void xmlparser::parseSelectedCourse()\r
107 {\r
108     datahandling *coursedata = datahandling::instance();\r
109     QString selectedCourse = (QCoreApplication::applicationDirPath() + "/xml/courses/" + coursedata->selectedCourse);\r
110 \r
111     #ifdef N900\r
112         selectedCourse = ("/opt/GolfScorecard/xml/courses/" + coursedata->selectedCourse);\r
113     #endif\r
114 \r
115     if (selectedCourse.isEmpty()){\r
116         QMessageBox::warning(this, tr("Course file"),\r
117                              tr("EMPTY! Cannot read file."));\r
118 \r
119     }\r
120 \r
121     QFile file(selectedCourse);\r
122 \r
123     if (!file.open(QFile::ReadOnly | QFile::Text)) {\r
124         QMessageBox::warning(this, tr("Course file"),\r
125                              tr("Cannot read file %1:\n%2.")\r
126                              .arg(selectedCourse)\r
127                              .arg(file.errorString()));\r
128     }\r
129 \r
130     QDomDocument doc("Courses");\r
131 \r
132     if( !doc.setContent( &file ) )\r
133     {\r
134       file.close();\r
135       QMessageBox::warning(this, tr("Parsing failed"), tr("Parsing failed totally"));\r
136     }\r
137     file.close();\r
138 \r
139     QDomElement root = doc.documentElement();\r
140     if( root.tagName() != "courses" )\r
141       QMessageBox::warning(this, tr("No root found!"), tr("There was no root in XML found!"));\r
142 \r
143     QDomElement course = root.firstChildElement("course");\r
144     QDomElement cr = course.firstChildElement("cr");\r
145     QDomElement male = cr.firstChildElement("male");\r
146     QDomElement female = cr.firstChildElement("female");\r
147     QDomElement slope = course.firstChildElement("slope");\r
148     QDomElement hole = course.firstChildElement("hole");\r
149 \r
150     QDomElement white = hole.firstChildElement("white");\r
151     QDomElement yellow = hole.firstChildElement("yellow");\r
152     QDomElement blue = hole.firstChildElement("blue");\r
153     QDomElement red = hole.firstChildElement("red");\r
154     QDomElement green = hole.firstChildElement("green");\r
155 \r
156 \r
157 \r
158     while (!course.isNull())\r
159     {\r
160         coursedata->addCourseData(course.attribute("name", ""), course.attribute("par", ""),\r
161                                   course.attribute("latitude", "").toDouble(), course.attribute("longitude", "").toDouble(),\r
162                                   course.attribute("country", ""), course.attribute("city", ""));\r
163         while (!cr.isNull())\r
164         {\r
165             while (!male.isNull())\r
166             {\r
167                 coursedata->addCourseCrMale(male.attribute("white", "").toDouble(), male.attribute("yellow", "").toDouble(),\r
168                                             male.attribute("blue", "").toDouble(), male.attribute("red", "").toDouble());\r
169                 male = male.nextSiblingElement("male");\r
170             }\r
171 \r
172             while (!female.isNull())\r
173             {\r
174                 coursedata->addCourseCrFemale(female.attribute("yellow", "").toDouble(),\r
175                                             female.attribute("blue", "").toDouble(), female.attribute("red", "").toDouble());\r
176                 female = female.nextSiblingElement("female");\r
177             }\r
178             cr = cr.nextSiblingElement("cr");\r
179         }\r
180 \r
181         male = slope.firstChildElement("male");\r
182         female = slope.firstChildElement("female");\r
183 \r
184         while (!slope.isNull())\r
185         {\r
186             while (!male.isNull())\r
187             {\r
188                 coursedata->addCourseSlopeMale(male.attribute("white", "").toDouble(), male.attribute("yellow", "").toDouble(),\r
189                                             male.attribute("blue", "").toDouble(), male.attribute("red", "").toDouble());\r
190                 male = male.nextSiblingElement("male");\r
191             }\r
192 \r
193             while (!female.isNull())\r
194             {\r
195                 coursedata->addCourseSlopeFemale(female.attribute("yellow", "").toDouble(),\r
196                                             female.attribute("blue", "").toDouble(), female.attribute("red", "").toDouble());\r
197                 female = female.nextSiblingElement("female");\r
198             }\r
199             slope = slope.nextSiblingElement("slope");\r
200         }\r
201 \r
202         while (!hole.isNull())\r
203         {\r
204             coursedata->addHoleData(hole.attribute("id", ""), hole.attribute("hcp", ""),\r
205                                     hole.attribute("par", ""), hole.attribute("map_uri", ""));\r
206             while (!white.isNull())\r
207             {\r
208                 coursedata->addWhiteTee(white.attribute("length", ""),\r
209                                         white.attribute("latitude", "").toDouble(), white.attribute("longitude", "").toDouble());\r
210                 white = white.nextSiblingElement("white");\r
211             }\r
212             while (!yellow.isNull())\r
213             {\r
214                 coursedata->addYellowTee(yellow.attribute("length", ""),\r
215                                         yellow.attribute("latitude", "").toDouble(), yellow.attribute("longitude", "").toDouble());\r
216                 yellow = yellow.nextSiblingElement("yellow");\r
217             }\r
218             while (!blue.isNull())\r
219             {\r
220                 coursedata->addBlueTee(blue.attribute("length", ""),\r
221                                         blue.attribute("latitude", "").toDouble(), blue.attribute("longitude", "").toDouble());\r
222                 blue = blue.nextSiblingElement("blue");\r
223             }\r
224             while (!red.isNull())\r
225             {\r
226                 coursedata->addRedTee(red.attribute("length", ""),\r
227                                         red.attribute("latitude", "").toDouble(), red.attribute("longitude", "").toDouble());\r
228                 red = red.nextSiblingElement("red");\r
229             }\r
230             while (!green.isNull())\r
231             {\r
232                 coursedata->addGreenInfo(green.attribute("latitude", "").toDouble(), green.attribute("longitude", "").toDouble());\r
233                 green = green.nextSiblingElement("green");\r
234             }\r
235 \r
236             hole = hole.nextSiblingElement("hole");\r
237             white = hole.firstChildElement("white");\r
238             yellow = hole.firstChildElement("yellow");\r
239             blue = hole.firstChildElement("blue");\r
240             red = hole.firstChildElement("red");\r
241             green = hole.firstChildElement("green");\r
242         }\r
243         course = course.nextSiblingElement("course");\r
244     }\r
245 }\r