Add copyright to source.
[toast/confclerk.git] / src / alarm / calendar.cpp
1 /*
2  * Copyright (C) 2010 Ixonos Plc.
3  * Copyright (C) 2011 Philipp Spitzer, gregor herrmann
4  *
5  * This file is part of ConfClerk.
6  *
7  * ConfClerk is free software: you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the Free
9  * Software Foundation, either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * ConfClerk is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15  * more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * ConfClerk.  If not, see <http://www.gnu.org/licenses/>.
19  */
20 #include "calendar.h"
21 #include <qdebug.h>
22 #include <qvariant.h>
23
24 #include <calendar-backend/CMulticalendar.h>
25 #include <calendar-backend/CCalendar.h>
26 #include <calendar-backend/CEvent.h>
27 #include <calendar-backend/CAlarm.h>
28
29 Calendar::Calendar():
30 m_pMultiCalendar(0),
31 m_pCalendar(0)
32 {
33         m_pMultiCalendar = CMulticalendar::MCInstance();
34
35         if(m_pMultiCalendar != NULL)
36                 m_pCalendar  = m_pMultiCalendar->getSynchronizedCalendar();
37
38 }
39
40 Calendar::~Calendar()
41 {
42         delete m_pCalendar;
43         delete m_pMultiCalendar;
44 }
45
46 int Calendar::addCalendarEvent(QString summary, QString description, QString location, uint dateStart, uint  dateEnd, int eventId)
47 {
48         int errorCode = 0;
49
50         if(m_pMultiCalendar == NULL)
51                 return 0;
52
53         if(m_pCalendar == NULL)
54                 return 0;
55
56         // create event
57         CEvent event(summary.toStdString(), description.toStdString(), location.toStdString(),  dateStart, dateEnd);
58
59         // create alarm
60         CAlarm alarm;
61         alarm.setTrigger(dateStart - 15 * 60);
62         alarm.setRepeat(1);
63         alarm.setDuration(3);
64         alarm.setAction(0);
65         alarm.setAttach(QString::number(eventId).toLocal8Bit().data());
66
67         // add alarm to event
68         event.setAlarm(&alarm);
69
70         // register event into calendar
71         m_pMultiCalendar->addEvent(&event,m_pCalendar->getCalendarId(),errorCode);
72
73     return errorCode;
74 }
75
76 void Calendar::deleteCalendarEvent(uint dateStart, uint dateEnd, int eventId)
77 {
78         int errorCode = 0;
79
80         vector< CComponent * > components;
81
82         // get all events
83         components = m_pMultiCalendar->getComponents(m_pCalendar->getCalendarId(), 1, dateStart, dateEnd, errorCode);
84
85         for (vector<CComponent *>::iterator it = components.begin();it!=components.end(); ++it)
86         {
87                 // get alarm properties
88                 if( (*it)->getAlarmProperties() == true)
89                 {
90                         CAlarm * pAlarm = (*it)->getAlarm();
91
92                         bool converted = false;
93                         int tempId = QVariant(pAlarm->getAttach().data()).toInt(&converted);
94
95                         // check event id
96                         if(converted == true)
97                                 if(tempId == eventId)
98                                 {
99                                         //delete event
100                                         m_pCalendar->deleteComponent((*it)->getId(),errorCode);
101                                         break;
102                                 }
103                 }
104         }
105 }
106
107 bool Calendar::hasCalendarEvent(int eventId)
108 {
109         bool ret = false;
110         int errorCode = 0;
111
112         vector< CEvent * > events;
113
114         // get all events
115         events = m_pCalendar->getEvents(errorCode);
116
117         if(events.size() > 0)
118         {
119                 for (vector<CEvent *>::iterator it = events.begin(); it!=events.end(); ++it)
120                 {
121                         // get alarm properties
122                         if( (*it)->getAlarmProperties() == true)
123                         {
124                                 CAlarm * pAlarm = (*it)->getAlarm();
125
126                                 bool converted = false;
127                                 int tempId = QVariant(pAlarm->getAttach().data()).toInt(&converted);
128
129                                 // check event id
130                                 if(converted == true)
131                                         if(tempId == eventId)
132                                         {
133                                                 ret = true;
134                                                 break;
135                                         }
136                         }
137                 }
138         }
139 }