34c165d038c3385177b0b51008ce1ef0f9b25984
[toast/confclerk.git] / src / alarm / calendar.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 #include "calendar.h"
20 #include <qdebug.h>
21 #include <qvariant.h>
22
23 #include <calendar-backend/CMulticalendar.h>
24 #include <calendar-backend/CCalendar.h>
25 #include <calendar-backend/CEvent.h>
26 #include <calendar-backend/CAlarm.h>
27
28 Calendar::Calendar():
29 m_pMultiCalendar(0),
30 m_pCalendar(0)
31 {
32         m_pMultiCalendar = CMulticalendar::MCInstance();
33
34         if(m_pMultiCalendar != NULL)
35                 m_pCalendar  = m_pMultiCalendar->getSynchronizedCalendar();
36
37 }
38
39 Calendar::~Calendar()
40 {
41         delete m_pCalendar;
42         delete m_pMultiCalendar;
43 }
44
45 int Calendar::addCalendarEvent(QString summary, QString description, QString location, uint dateStart, uint  dateEnd, int eventId)
46 {
47         int errorCode = 0;
48
49         if(m_pMultiCalendar == NULL)
50                 return 0;
51
52         if(m_pCalendar == NULL)
53                 return 0;
54
55         // create event
56         CEvent event(summary.toStdString(), description.toStdString(), location.toStdString(),  dateStart, dateEnd);
57
58         // create alarm
59         CAlarm alarm;
60         alarm.setTrigger(dateStart - 15 * 60);
61         alarm.setRepeat(1);
62         alarm.setDuration(3);
63         alarm.setAction(0);
64         alarm.setAttach(QString::number(eventId).toLocal8Bit().data());
65
66         // add alarm to event
67         event.setAlarm(&alarm);
68
69         // register event into calendar
70         m_pMultiCalendar->addEvent(&event,m_pCalendar->getCalendarId(),errorCode);
71
72     return errorCode;
73 }
74
75 void Calendar::deleteCalendarEvent(uint dateStart, uint dateEnd, int eventId)
76 {
77         int errorCode = 0;
78
79         vector< CComponent * > components;
80
81         // get all events
82         components = m_pMultiCalendar->getComponents(m_pCalendar->getCalendarId(), 1, dateStart, dateEnd, errorCode);
83
84         for (vector<CComponent *>::iterator it = components.begin();it!=components.end(); ++it)
85         {
86                 // get alarm properties
87                 if( (*it)->getAlarmProperties() == true)
88                 {
89                         CAlarm * pAlarm = (*it)->getAlarm();
90
91                         bool converted = false;
92                         int tempId = QVariant(pAlarm->getAttach().data()).toInt(&converted);
93
94                         // check event id
95                         if(converted == true)
96                                 if(tempId == eventId)
97                                 {
98                                         //delete event
99                                         m_pCalendar->deleteComponent((*it)->getId(),errorCode);
100                                         break;
101                                 }
102                 }
103         }
104 }
105
106 bool Calendar::hasCalendarEvent(int eventId)
107 {
108         bool ret = false;
109         int errorCode = 0;
110
111         vector< CEvent * > events;
112
113         // get all events
114         events = m_pCalendar->getEvents(errorCode);
115
116         if(events.size() > 0)
117         {
118                 for (vector<CEvent *>::iterator it = events.begin(); it!=events.end(); ++it)
119                 {
120                         // get alarm properties
121                         if( (*it)->getAlarmProperties() == true)
122                         {
123                                 CAlarm * pAlarm = (*it)->getAlarm();
124
125                                 bool converted = false;
126                                 int tempId = QVariant(pAlarm->getAttach().data()).toInt(&converted);
127
128                                 // check event id
129                                 if(converted == true)
130                                         if(tempId == eventId)
131                                         {
132                                                 ret = true;
133                                                 break;
134                                         }
135                         }
136                 }
137         }
138 }