1 | /* |
---|
2 | * Copyright (C) 2010 Ixonos Plc. |
---|
3 | * |
---|
4 | * This file is part of fosdem-schedule. |
---|
5 | * |
---|
6 | * fosdem-schedule is free software: you can redistribute it and/or modify it |
---|
7 | * under the terms of the GNU General Public License as published by the Free |
---|
8 | * Software Foundation, either version 2 of the License, or (at your option) |
---|
9 | * any later version. |
---|
10 | * |
---|
11 | * fosdem-schedule is distributed in the hope that it will be useful, but |
---|
12 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
---|
13 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
---|
14 | * more details. |
---|
15 | * |
---|
16 | * You should have received a copy of the GNU General Public License along with |
---|
17 | * fosdem-schedule. If not, see <http://www.gnu.org/licenses/>. |
---|
18 | */ |
---|
19 | |
---|
20 | #include <QDomDocument> |
---|
21 | #include <QHash> |
---|
22 | |
---|
23 | #include "schedulexmlparser.h" |
---|
24 | #include "sqlengine.h" |
---|
25 | |
---|
26 | #include <QDebug> |
---|
27 | |
---|
28 | ScheduleXmlParser::ScheduleXmlParser(QObject *aParent) |
---|
29 | : QObject(aParent) |
---|
30 | { |
---|
31 | } |
---|
32 | |
---|
33 | int ScheduleXmlParser::parseData(const QByteArray &aData, const QString& url) |
---|
34 | { |
---|
35 | QDomDocument document; |
---|
36 | document.setContent (aData, false); |
---|
37 | |
---|
38 | QDomElement scheduleElement = document.firstChildElement("schedule"); |
---|
39 | |
---|
40 | SqlEngine::beginTransaction(); |
---|
41 | |
---|
42 | int confId = 0; |
---|
43 | if (!scheduleElement.isNull()) |
---|
44 | { |
---|
45 | QDomElement conferenceElement = scheduleElement.firstChildElement("conference"); |
---|
46 | if (!conferenceElement.isNull()) |
---|
47 | { |
---|
48 | QHash<QString,QString> conference; |
---|
49 | conference["id"] = QString::number(0); // conference ID is assigned automatically, or obtained from the DB |
---|
50 | conference["title"] = conferenceElement.firstChildElement("title").text(); |
---|
51 | conference["subtitle"] = conferenceElement.firstChildElement("subtitle").text(); |
---|
52 | conference["venue"] = conferenceElement.firstChildElement("venue").text(); |
---|
53 | conference["city"] = conferenceElement.firstChildElement("city").text(); |
---|
54 | conference["start"] = conferenceElement.firstChildElement("start").text(); // date |
---|
55 | conference["end"] = conferenceElement.firstChildElement("end").text(); // date |
---|
56 | conference["days"] = conferenceElement.firstChildElement("days").text(); // int |
---|
57 | conference["day_change"] = conferenceElement.firstChildElement("day_change").text(); // time |
---|
58 | conference["timeslot_duration"] = conferenceElement.firstChildElement("timeslot_duration").text(); // time |
---|
59 | conference["url"] = url; |
---|
60 | SqlEngine::addConferenceToDB(conference); |
---|
61 | confId = conference["id"].toInt(); |
---|
62 | emit(parsingSchedule(conference["title"])); |
---|
63 | } |
---|
64 | |
---|
65 | // we need to get count of all events in order to emit 'progressStatus' signal |
---|
66 | int totalEventsCount = scheduleElement.elementsByTagName("event").count(); |
---|
67 | |
---|
68 | // parsing day elements |
---|
69 | int currentEvent = 0; // hold global idx of processed event |
---|
70 | QDomNodeList dayList = scheduleElement.elementsByTagName("day"); |
---|
71 | for (int i=0; i<dayList.count(); i++) |
---|
72 | { |
---|
73 | QDomElement dayElement = dayList.at(i).toElement(); |
---|
74 | //QDate dayDate = QDate::fromString(dayElement.attribute("date"),DATE_FORMAT); |
---|
75 | //int dayIndex = dayElement.attribute("index").toInt(); |
---|
76 | |
---|
77 | // parsing room elements |
---|
78 | QDomNodeList roomList = dayElement.elementsByTagName("room"); |
---|
79 | for (int i=0; i<roomList.count(); i++) |
---|
80 | { |
---|
81 | QDomElement roomElement = roomList.at(i).toElement(); |
---|
82 | // roomElement has to be 'Element' and it has to have 'name' attribute |
---|
83 | // TODO: 'event' has also 'room' node, so it can be unstable if that node has also 'name' attribute |
---|
84 | if(roomElement.hasAttribute("name")) |
---|
85 | { |
---|
86 | // parsing event elements |
---|
87 | QDomNodeList eventList = roomElement.elementsByTagName("event"); |
---|
88 | for (int i=0; i<eventList.count(); i++) |
---|
89 | { |
---|
90 | currentEvent++; |
---|
91 | QDomElement eventElement = eventList.at(i).toElement(); |
---|
92 | |
---|
93 | // now we have all info to create ROOM/EVENT_ROOM record(s) |
---|
94 | QHash<QString,QString> room; |
---|
95 | room["name"] = roomElement.attribute("name"); |
---|
96 | room["event_id"] = eventElement.attribute("id"); |
---|
97 | room["conference_id"] = QString::number(confId,10); |
---|
98 | room["picture"] = "NOT DEFINED YET"; // TODO: implement some mapping to assign correct picture to specified room_name |
---|
99 | SqlEngine::addRoomToDB(room); |
---|
100 | |
---|
101 | // process event's nodes |
---|
102 | QHash<QString,QString> event; |
---|
103 | event["id"] = eventElement.attribute("id");; |
---|
104 | event["conference_id"] = QString::number(confId, 10); |
---|
105 | event["start"] = eventElement.firstChildElement("start").text(); // time eg. 10:00 |
---|
106 | event["date"] = dayElement.attribute("date"); // date eg. 2009-02-07 |
---|
107 | event["duration"] = eventElement.firstChildElement("duration").text(); // time eg. 00:30 |
---|
108 | event["room_name"] = eventElement.firstChildElement("room").text(); // string eg. "Janson" |
---|
109 | event["tag"] = eventElement.firstChildElement("tag").text(); // string eg. "welcome" |
---|
110 | event["title"] = eventElement.firstChildElement("title").text(); // string eg. "Welcome" |
---|
111 | event["subtitle"] = eventElement.firstChildElement("subtitle").text(); // string |
---|
112 | event["track"] = eventElement.firstChildElement("track").text(); // string eg. "Keynotes" |
---|
113 | event["type"] = eventElement.firstChildElement("type").text(); // string eg. "Podium" |
---|
114 | event["language"] = eventElement.firstChildElement("language").text(); // language eg. "English" |
---|
115 | event["abstract"] = eventElement.firstChildElement("abstract").text(); // string |
---|
116 | event["description"] = eventElement.firstChildElement("description").text(); // string |
---|
117 | SqlEngine::addEventToDB(event); |
---|
118 | // process persons' nodes |
---|
119 | QList<QString> persons; |
---|
120 | QDomElement personsElement = eventElement.firstChildElement("persons"); |
---|
121 | QDomNodeList personList = personsElement.elementsByTagName("person"); |
---|
122 | for(int i = 0;i < personList.count();i++){ |
---|
123 | QHash<QString,QString> person; |
---|
124 | person["id"] = personList.at(i).toElement().attribute("id"); |
---|
125 | person["name"] = personList.at(i).toElement().text(); |
---|
126 | person["event_id"] = eventElement.attribute("id"); |
---|
127 | person["conference_id"] = QString::number(confId, 10); |
---|
128 | //qDebug() << "adding Person: " << person["name"]; |
---|
129 | SqlEngine::addPersonToDB(person); |
---|
130 | } |
---|
131 | // process links' nodes |
---|
132 | QDomElement linksElement = eventElement.firstChildElement("links"); |
---|
133 | QDomNodeList linkList = linksElement.elementsByTagName("link"); |
---|
134 | for(int i = 0;i < linkList.count();i++){ |
---|
135 | QHash<QString,QString> link; |
---|
136 | link["name"] = linkList.at(i).toElement().text(); |
---|
137 | link["url"] = linkList.at(i).toElement().attribute("href"); |
---|
138 | link["event_id"] = eventElement.attribute("id"); |
---|
139 | link["conference_id"] = QString::number(confId, 10); |
---|
140 | SqlEngine::addLinkToDB(link); |
---|
141 | } |
---|
142 | // emit signal to inform the user about the current status (how many events are parsed so far - expressed in %) |
---|
143 | int status = currentEvent * 100 / totalEventsCount; |
---|
144 | progressStatus(status); |
---|
145 | } // parsing event elements |
---|
146 | } |
---|
147 | } // parsing room elements |
---|
148 | } // parsing day elements |
---|
149 | } // schedule element |
---|
150 | SqlEngine::commitTransaction(); |
---|
151 | |
---|
152 | return confId; |
---|
153 | } |
---|
154 | |
---|