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