-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcalaccess.cc
357 lines (282 loc) · 7.53 KB
/
calaccess.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#include <CCalendar.h>
#include <CMulticalendar.h>
#include <CComponent.h>
#include <CRecurrence.h>
#include <CEvent.h>
#include <CAlarm.h>
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
struct event_t {
const char *id;
int start;
int end;
const char *title;
const char *where;
const char *descr;
int allday;
int ctime;
int mtime;
int tzOffset;
const char *rrule;
int alarm;
};
extern "C" const char *getAllLocalCalendarsIDs()
{
vector<CCalendar *>::iterator it;
stringstream calIDs;
CMulticalendar *mc = CMulticalendar::MCInstance();
vector<CCalendar *> list = mc->getListCalFromMc();
it = list.begin();
while (it != list.end()) {
CCalendar *cal = *it;
calIDs << cal->getCalendarId() << ":";
++it;
}
mc->releaseListCalendars(list);
string calIDsString(calIDs.str());
if (calIDsString.size() == 0) {
return NULL;
} else {
return (const char *)strdup(calIDsString.erase(calIDsString.size()-1).c_str());
}
}
extern "C" const char *getLocalCalendarName(int id)
{
int error(0);
CCalendar *cal = NULL;
CMulticalendar *mc = CMulticalendar::MCInstance();
cal = mc->getCalendarById(id, error);
if (error == 0) {
return NULL;
} else {
string calName(cal->getCalendarName());
delete(cal);
return (const char *)strdup(calName.c_str());
}
}
extern "C" int createLocalCalendar(char *title)
{
CCalendar *newCal;
int error;
CMulticalendar *mc = CMulticalendar::MCInstance();
newCal = mc->addCalendar(string(title), COLOUR_NEXT_FREE, false, true, LOCAL_CALENDAR, "", "1.0", error);
if (newCal == NULL) {
return -1;
} else {
int newCalId = newCal->getCalendarId();
delete newCal;
return newCalId;
}
}
extern "C" const char *addLocalCalendarEntry(int calId, int start, int end,
char *title, char *where, char *descr,
int fullday, int cdate, char *rrule, int rtype, int alarm)
{
bool newEvtRes;
int error;
string newEvtId;
CCalendar *cal;
CEvent *newEvt;
CMulticalendar *mc = CMulticalendar::MCInstance();
newEvt = new CEvent(string(title), string(descr), string(where),
start, end);
newEvt->setCreatedTime(cdate);
newEvt->setLastModified(cdate);
newEvt->setAllDay(fullday);
newEvt->setTzOffset(CMulticalendar::MCInstance()->getSystemTimeShift());
newEvt->setTzid(CMulticalendar::MCInstance()->getSystemTimeZone());
cal = mc->getCalendarById(calId, error);
if (cal == NULL) {
delete newEvt;
return (const char *)strdup(newEvtId.c_str());
}
// Recurrence handling
string rrule_s = string(rrule);
if (rrule_s.size() > 0) {
CRecurrence *rec = new CRecurrence();
rec->setRtype(rtype);
vector<string> rrules;
rrules.push_back(rrule_s);
rec->setRrule(rrules);
newEvt->setRecurrence(rec);
}
if (alarm >= 0)
newEvt->setAlarm(new CAlarm(alarm*60, 1));
else
newEvt->removeAlarm();
newEvtRes = cal->addEvent(newEvt, error);
if (newEvtRes) {
newEvtId = newEvt->getId();
}
delete newEvt;
return (const char *)strdup(newEvtId.c_str());
}
extern "C" int updateLocalEvent(int cid, char *localId, char *summary,
char *descr, char *where, int start, int end, char *rrule,
int rtype, int until, int alarm)
{
int error;
CMulticalendar *mc = CMulticalendar::MCInstance();
CCalendar *cal = mc->getCalendarById(cid, error);
if (cal == NULL)
return -1;
CEvent *evt = cal->getEvent(string(localId), error);
if (evt == NULL) {
delete cal;
return -1;
}
evt->setSummary(string(summary));
evt->setDescription(string(descr));
evt->setLocation(string(where));
evt->setDateStart(start);
evt->setDateEnd(end);
// Recurrence handling
string rrule_s = string(rrule);
if (rrule_s.size() > 0) {
CRecurrence *rec = new CRecurrence();
rec->setRtype(rtype);
vector<string> rrules;
rrules.push_back(rrule_s);
rec->setRrule(rrules);
evt->setRecurrence(rec);
evt->setUntil(until);
}
if (alarm >= 0)
evt->setAlarm(new CAlarm(alarm*60, 1));
else
evt->removeAlarm();
bool res = cal->modifyEvent(evt, error);
delete cal;
delete evt;
if (!res) {
return -1;
}
return 0;
}
extern "C" int removeCancelledEventLocally(int cid, char *lid)
{
int error;
bool ret;
CMulticalendar *mc = CMulticalendar::MCInstance();
CCalendar *cal = mc->getCalendarById(cid, error);
if (cal == NULL)
return -1;
ret = cal->deleteEvent(string(lid), error);
delete cal;
return ret;
}
extern "C" const char *queryNewLocalEvents(int cid, int lastSync)
{
int error;
bool ret;
vector<CEvent *>::iterator it;
stringstream evtsString;
CMulticalendar *mc = CMulticalendar::MCInstance();
CCalendar *cal = mc->getCalendarById(cid, error);
if (cal == NULL)
return NULL;
vector<CEvent*> newEvts = cal->getAllAddedEvents(lastSync, error);
it = newEvts.begin();
while (it != newEvts.end()) {
CEvent *evt = *it;
evtsString << evt->getId() << ":";
delete evt;
++it;
}
string evtsIDsString(evtsString.str());
if (evtsIDsString.size() == 0) {
return NULL;
} else {
return (const char *)strdup(evtsIDsString.erase(evtsIDsString.size()-1).c_str());
}
}
extern "C" const char *queryUpdatedLocalEvents(int cid, int lastSync)
{
int error;
bool ret;
vector<CEvent *>::iterator it;
stringstream evtsString;
CMulticalendar *mc = CMulticalendar::MCInstance();
CCalendar *cal = mc->getCalendarById(cid, error);
if (cal == NULL)
return (const char *)strdup(evtsString.str().c_str());
vector<CEvent*> modEvts = cal->getAllModifiedEvents(lastSync, error);
it = modEvts.begin();
while (it != modEvts.end()) {
CEvent *evt = *it;
evtsString << evt->getId() << ":";
delete evt;
++it;
}
string evtsIDsString(evtsString.str());
if (evtsIDsString.size() == 0) {
return NULL;
} else {
return (const char*)strdup(evtsIDsString.erase(evtsIDsString.size()-1).c_str());
}
}
extern "C" const char *getAllDeletedEvents(int cid, int lastSync)
{
int error;
bool ret;
vector<string>::iterator it;
stringstream evtsString;
CMulticalendar *mc = CMulticalendar::MCInstance();
CCalendar *cal = mc->getCalendarById(cid, error);
if (cal == NULL)
return (const char*)strdup(evtsString.str().c_str());
vector<string> delEvts = cal->getAllDeletedEvents(lastSync, error);
it = delEvts.begin();
while (it != delEvts.end()) {
string evt = *it;
evtsString << evt << ":";
++it;
}
string evtsIDsString(evtsString.str());
if (evtsIDsString.size() == 0) {
return NULL;
} else {
return (const char*)strdup(evtsIDsString.erase(evtsIDsString.size()-1).c_str());
}
}
extern "C" int getEventById(int cid, char *lid, event_t *e)
{
int error;
CMulticalendar *mc = CMulticalendar::MCInstance();
CCalendar *cal = mc->getCalendarById(cid, error);
if (cal == NULL)
return -1;
CEvent *evt = cal->getEvent(string(lid), error);
if (evt == NULL)
return -1;
e->id = lid;
e->start = evt->getDateStart();
e->end = evt->getDateEnd();
e->title = (const char*)strdup(evt->getSummary().c_str());
e->where = (const char*)strdup(evt->getLocation().c_str());
e->descr = (const char*)strdup(evt->getDescription().c_str());
e->allday = evt->getAllDay();
e->ctime = evt->getCreatedTime();
e->mtime = evt->getLastModified();
e->tzOffset = evt->getTzOffset();
CRecurrence *recurrence = evt->getRecurrence();
if (recurrence == NULL)
e->rrule = "";
else {
vector<string> rrules = recurrence->getRrule();
stringstream rruleStream;
for (unsigned int i=0; i < rrules.size(); ++i)
rruleStream << rrules[i] << ";";
string rrule(rruleStream.str());
e->rrule = (const char*)strdup(rrule.erase(rrule.size()-1).c_str());
}
CAlarm *alrm = evt->getAlarm();
if (alrm != NULL) {
int alarm = alrm->getTrigger();
e->alarm = (alarm == -1 ? -1 : alarm/60);
}
delete evt;
return 0;
}