-
Notifications
You must be signed in to change notification settings - Fork 30
/
calendarPlugin.m
executable file
·325 lines (257 loc) · 12.2 KB
/
calendarPlugin.m
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
//
// calendarPlugin.m
// Author: Felix Montanez
// Date: 01-17-2011
// Notes:
//
// Contributors : Sean Bedford
#import "calendarPlugin.h"
#import <EventKitUI/EventKitUI.h>
#import <EventKit/EventKit.h>
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
@implementation calendarPlugin
@synthesize eventStore;
#pragma mark Initialisation functions
- (CDVPlugin*) initWithWebView:(UIWebView*)theWebView
{
self = (calendarPlugin*)[super initWithWebView:theWebView];
if (self) {
//[self setup];
[self initEventStoreWithCalendarCapabilities];
}
return self;
}
- (void)initEventStoreWithCalendarCapabilities {
__block BOOL accessGranted = NO;
eventStore= [[EKEventStore alloc] init];
if([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)]) {
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
accessGranted = granted;
dispatch_semaphore_signal(sema);
}];
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
} else { // we're on iOS 5 or older
accessGranted = YES;
}
if (accessGranted) {
self.eventStore = eventStore;
}
}
#pragma mark Helper Functions
-(NSArray*)findEKEventsWithTitle: (NSString *)title
location: (NSString *)location
message: (NSString *)message
startDate: (NSDate *)startDate
endDate: (NSDate *)endDate {
// Build up a predicateString - this means we only query a parameter if we actually had a value in it
NSMutableString *predicateString= [[NSMutableString alloc] initWithString:@""];
if (title.length > 0) {
[predicateString appendString:[NSString stringWithFormat:@"title == '%@'" , title]];
}
if (location.length > 0) {
[predicateString appendString:[NSString stringWithFormat:@" AND location == '%@'" , location]];
}
if (message.length > 0) {
[predicateString appendString:[NSString stringWithFormat:@" AND notes == '%@'" , message]];
}
NSPredicate *matches = [NSPredicate predicateWithFormat:predicateString];
NSArray *datedEvents = [self.eventStore eventsMatchingPredicate:[eventStore predicateForEventsWithStartDate:startDate endDate:endDate calendars:nil]];
NSArray *matchingEvents = [datedEvents filteredArrayUsingPredicate:matches];
return matchingEvents;
}
#pragma mark Cordova functions
- (void)createEvent:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options {
// Import arguments
NSString *callbackId = [arguments pop];
NSString* title = [arguments objectAtIndex:0];
NSString* location = [arguments objectAtIndex:1];
NSString* message = [arguments objectAtIndex:2];
NSString *startDate = [arguments objectAtIndex:3];
NSString *endDate = [arguments objectAtIndex:4];
//creating the dateformatter object
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *myStartDate = [df dateFromString:startDate];
NSDate *myEndDate = [df dateFromString:endDate];
EKEvent *myEvent = [EKEvent eventWithEventStore: self.eventStore];
myEvent.title = title;
myEvent.location = location;
myEvent.notes = message;
myEvent.startDate = myStartDate;
myEvent.endDate = myEndDate;
myEvent.calendar = self.eventStore.defaultCalendarForNewEvents;
EKAlarm *reminder = [EKAlarm alarmWithRelativeOffset:-2*60*60];
[myEvent addAlarm:reminder];
NSError *error = nil;
[self.eventStore saveEvent:myEvent span:EKSpanThisEvent error:&error];
BOOL saved = [self.eventStore saveEvent:myEvent span:EKSpanThisEvent
error:&error];
if (saved) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message:@"Saved to Calendar" delegate:self
cancelButtonTitle:@"Thank you!"
otherButtonTitles:nil];
[alert show];
[alert release];
}
// Check error code + return result
if (error) {
CDVPluginResult * pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR
messageAsString:error.userInfo.description];
[self writeJavascript:[pluginResult toErrorCallbackString:callbackId]];
}
else {
NSLog(@"Reached Success");
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self writeJavascript:[pluginResult toSuccessCallbackString:callbackId]];
}
}
-(void)deleteEvent:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options {
// Import arguments
NSString *callbackId = [arguments pop];
NSString* title = [arguments objectAtIndex:0];
NSString* location = [arguments objectAtIndex:1];
NSString* message = [arguments objectAtIndex:2];
NSString *startDate = [arguments objectAtIndex:3];
NSString *endDate = [arguments objectAtIndex:4];
bool delAll = [arguments objectAtIndex:5];
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *myStartDate = [df dateFromString:startDate];
NSDate *myEndDate = [df dateFromString:endDate];
NSArray *matchingEvents = [self findEKEventsWithTitle:title location:location message:message startDate:myStartDate endDate:myEndDate];
if (delAll || matchingEvents.count == 1) {
// Definitive single match - delete it!
NSError *error = NULL;
bool hadErrors = false;
if (delAll) {
for (EKEvent * event in matchingEvents) {
[self.eventStore removeEvent:event span:EKSpanThisEvent error:&error];
// Check for error codes and return result
if (error) {
hadErrors = true;
}
}
}
else {
[self.eventStore removeEvent:[matchingEvents lastObject] span:EKSpanThisEvent error:&error];
}
// Check for error codes and return result
if (error || hadErrors) {
NSString *messageString;
if (hadErrors) {
messageString = @"Error deleting events";
}
else {
messageString = error.userInfo.description;
}
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR
messageAsString:messageString];
[self writeJavascript:[pluginResult toErrorCallbackString:callbackId]];
}
else {
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self writeJavascript:[pluginResult toSuccessCallbackString:callbackId]];
}
}
}
-(void)findEvent:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options {
// Import arguments
NSString *callbackId = [arguments pop];
NSString* title = [arguments objectAtIndex:0];
NSString* location = [arguments objectAtIndex:1];
NSString* message = [arguments objectAtIndex:2];
NSString *startDate = [arguments objectAtIndex:3];
NSString *endDate = [arguments objectAtIndex:4];
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *myStartDate = [df dateFromString:startDate];
NSDate *myEndDate = [df dateFromString:endDate];
NSArray *matchingEvents = [self findEKEventsWithTitle:title location:location message:message startDate:myStartDate endDate:myEndDate];
NSMutableArray *finalResults = [[NSMutableArray alloc] initWithCapacity:matchingEvents.count];
// Stringify the results - Cordova can't deal with Obj-C objects
for (EKEvent * event in matchingEvents) {
NSMutableDictionary *entry = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
event.title, @"title",
event.location, @"location",
event.notes, @"message",
[df stringFromDate:event.startDate], @"startDate",
[df stringFromDate:event.endDate], @"endDate", nil];
[finalResults addObject:entry];
}
if (finalResults.count > 0) {
// Return the results we got
CDVPluginResult* result = [CDVPluginResult
resultWithStatus: CDVCommandStatus_OK
messageAsArray:finalResults
];
[self writeJavascript:[result toSuccessCallbackString:callbackId]];
}
else {
CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_NO_RESULT];
[self writeJavascript:[result toErrorCallbackString:callbackId]];
}
}
-(void)modifyEvent:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options {
// Import arguments
NSString *callbackId = [arguments pop];
NSString* title = [arguments objectAtIndex:0];
NSString* location = [arguments objectAtIndex:1];
NSString* message = [arguments objectAtIndex:2];
NSString *startDate = [arguments objectAtIndex:3];
NSString *endDate = [arguments objectAtIndex:4];
NSString* ntitle = [arguments objectAtIndex:5];
NSString* nlocation = [arguments objectAtIndex:6];
NSString* nmessage = [arguments objectAtIndex:7];
NSString *nstartDate = [arguments objectAtIndex:8];
NSString *nendDate = [arguments objectAtIndex:9];
// Make NSDates from our strings
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *myStartDate = [df dateFromString:startDate];
NSDate *myEndDate = [df dateFromString:endDate];
// Find matches
NSArray *matchingEvents = [self findEKEventsWithTitle:title location:location message:message startDate:myStartDate endDate:myEndDate];
if (matchingEvents.count == 1) {
// Presume we have to have an exact match to modify it!
// Need to load this event from an EKEventStore so we can edit it
EKEvent *theEvent = [self.eventStore eventWithIdentifier:((EKEvent*)[matchingEvents lastObject]).eventIdentifier];
if (ntitle) {
theEvent.title = ntitle;
}
if (nlocation) {
theEvent.location = nlocation;
}
if (nmessage) {
theEvent.notes = nmessage;
}
if (nstartDate) {
NSDate *newMyStartDate = [df dateFromString:nstartDate];
theEvent.startDate = newMyStartDate;
}
if (nendDate) {
NSDate *newMyEndDate = [df dateFromString:nendDate];
theEvent.endDate = newMyEndDate;
}
// Now save the new details back to the store
NSError *error = nil;
[self.eventStore saveEvent:theEvent span:EKSpanThisEvent error:&error];
// Check error code + return result
if (error) {
CDVPluginResult * pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR
messageAsString:error.userInfo.description];
[self writeJavascript:[pluginResult toErrorCallbackString:callbackId]];
}
else {
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self writeJavascript:[pluginResult toSuccessCallbackString:callbackId]];
}
}
else {
// Otherwise return a no result error
CDVPluginResult * pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_NO_RESULT];
[self writeJavascript:[pluginResult toErrorCallbackString:callbackId]];
}
}
@end