use visible notifications of errors
[toast/confclerk.git] / src / sql / schedulexmlparser.cpp
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 #include "../gui/errormessage.h"
26
27 #include <QDebug>
28
29 ScheduleXmlParser::ScheduleXmlParser(QObject *aParent)
30     : QObject(aParent)
31 {
32 }
33
34 int ScheduleXmlParser::parseData(const QByteArray &aData, const QString& url)
35 {
36     QDomDocument document;
37     QString xml_error;
38     if (!document.setContent (aData, false, &xml_error)) {
39         error_message("Could not parse schedule: " + xml_error);
40         return -1;
41     }
42
43     QDomElement scheduleElement = document.firstChildElement("schedule");
44
45     SqlEngine::beginTransaction();
46
47     int confId = 0;
48     if (!scheduleElement.isNull())
49     {
50         QDomElement conferenceElement = scheduleElement.firstChildElement("conference");
51         if (!conferenceElement.isNull())
52         {
53             QHash<QString,QString> conference;
54             conference["id"] = QString::number(0); // conference ID is assigned automatically, or obtained from the DB
55             conference["title"] = conferenceElement.firstChildElement("title").text();
56             conference["subtitle"] = conferenceElement.firstChildElement("subtitle").text();
57             conference["venue"] = conferenceElement.firstChildElement("venue").text();
58             conference["city"] = conferenceElement.firstChildElement("city").text();
59             conference["start"] = conferenceElement.firstChildElement("start").text(); // date
60             conference["end"] = conferenceElement.firstChildElement("end").text(); // date
61             conference["days"] = conferenceElement.firstChildElement("days").text(); // int
62             conference["day_change"] = conferenceElement.firstChildElement("day_change").text(); // time
63             conference["timeslot_duration"] = conferenceElement.firstChildElement("timeslot_duration").text(); // time
64             conference["url"] = url;
65             SqlEngine::addConferenceToDB(conference);
66             confId = conference["id"].toInt();
67             emit(parsingSchedule(conference["title"]));
68         }
69
70         // we need to get count of all events in order to emit 'progressStatus' signal
71         int totalEventsCount = scheduleElement.elementsByTagName("event").count();
72
73         // parsing day elements
74         int currentEvent = 0; // hold global idx of processed event
75         QDomNodeList dayList = scheduleElement.elementsByTagName("day");
76         for (int i=0; i<dayList.count(); i++)
77         {
78             QDomElement dayElement = dayList.at(i).toElement();
79             //QDate dayDate = QDate::fromString(dayElement.attribute("date"),DATE_FORMAT);
80             //int dayIndex = dayElement.attribute("index").toInt();
81
82             // parsing room elements
83             QDomNodeList roomList = dayElement.elementsByTagName("room");
84             for (int i=0; i<roomList.count(); i++)
85             {
86                 QDomElement roomElement = roomList.at(i).toElement();
87                 // roomElement has to be 'Element' and it has to have 'name' attribute
88                 // TODO: 'event' has also 'room' node, so it can be unstable if that node has also 'name' attribute
89                 if(roomElement.hasAttribute("name"))
90                 {
91                     // parsing event elements
92                     QDomNodeList eventList = roomElement.elementsByTagName("event");
93                     for (int i=0; i<eventList.count(); i++)
94                     {
95                         currentEvent++;
96                         QDomElement eventElement = eventList.at(i).toElement();
97
98                         // now we have all info to create ROOM/EVENT_ROOM record(s)
99                         QHash<QString,QString> room;
100                         room["name"] = roomElement.attribute("name");
101                         room["event_id"] = eventElement.attribute("id");
102                         room["conference_id"] = QString::number(confId,10);
103                         room["picture"] = "NOT DEFINED YET"; // TODO: implement some mapping to assign correct picture to specified room_name
104                         SqlEngine::addRoomToDB(room);
105
106                         // process event's nodes
107                         QHash<QString,QString> event;
108                         event["id"] = eventElement.attribute("id");;
109                         event["conference_id"] = QString::number(confId, 10);
110                         event["start"] = eventElement.firstChildElement("start").text(); // time eg. 10:00
111                         event["date"] = dayElement.attribute("date"); // date eg. 2009-02-07
112                         event["duration"] = eventElement.firstChildElement("duration").text(); // time eg. 00:30
113                         event["room_name"] = eventElement.firstChildElement("room").text(); // string eg. "Janson"
114                         event["tag"] = eventElement.firstChildElement("tag").text(); // string eg. "welcome"
115                         event["title"] = eventElement.firstChildElement("title").text(); // string eg. "Welcome"
116                         event["subtitle"] = eventElement.firstChildElement("subtitle").text(); // string
117                         event["track"] = eventElement.firstChildElement("track").text(); // string eg. "Keynotes"
118                         event["type"] = eventElement.firstChildElement("type").text(); // string eg. "Podium"
119                         event["language"] = eventElement.firstChildElement("language").text(); // language eg. "English"
120                         event["abstract"] = eventElement.firstChildElement("abstract").text(); // string
121                         event["description"] = eventElement.firstChildElement("description").text(); // string
122                         SqlEngine::addEventToDB(event);
123                         // process persons' nodes
124                         QList<QString> persons;
125                         QDomElement personsElement = eventElement.firstChildElement("persons");
126                         QDomNodeList personList = personsElement.elementsByTagName("person");
127                         for(int i = 0;i < personList.count();i++){
128                             QHash<QString,QString> person;
129                             person["id"] = personList.at(i).toElement().attribute("id");
130                             person["name"] = personList.at(i).toElement().text();
131                             person["event_id"] = eventElement.attribute("id");
132                             person["conference_id"] = QString::number(confId, 10);
133                             //qDebug() << "adding Person: " << person["name"];
134                             SqlEngine::addPersonToDB(person);
135                         }
136                         // process links' nodes
137                         QDomElement linksElement = eventElement.firstChildElement("links");
138                         QDomNodeList linkList = linksElement.elementsByTagName("link");
139                         for(int i = 0;i < linkList.count();i++){
140                             QHash<QString,QString> link;
141                             link["name"] = linkList.at(i).toElement().text();
142                             link["url"] = linkList.at(i).toElement().attribute("href");
143                             link["event_id"] = eventElement.attribute("id");
144                             link["conference_id"] = QString::number(confId, 10);
145                             SqlEngine::addLinkToDB(link);
146                         }
147                         // emit signal to inform the user about the current status (how many events are parsed so far - expressed in %)
148                         int status = currentEvent * 100 / totalEventsCount;
149                         progressStatus(status);
150                     } // parsing event elements
151                 }
152             } // parsing room elements
153         } // parsing day elements
154     } // schedule element
155     SqlEngine::commitTransaction();
156
157     return confId;
158 }
159