forked from tidev/titanium-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalendarModule.m
300 lines (242 loc) · 9.44 KB
/
CalendarModule.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
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2009-2013 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
#ifdef USE_TI_CALENDAR
#import "CalendarModule.h"
#import "TiCalendarCalendar.h"
#pragma mark - Backwards compatibility for pre-iOS 6.0
#if __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_6_0
//TODO: Should we warn that they need to update to the latest XCode if this is happening?
#define EKAuthorizationStatusNotDetermined 0
#define EKAuthorizationStatusRestricted 1
#define EKAuthorizationStatusDenied 2
#define EKAuthorizationStatusAuthorized 3
enum {
EKEntityTypeEvent,
EKEntityTypeReminder
};
typedef NSUInteger EKEntityType;
typedef void(^EKEventStoreRequestAccessCompletionHandler)(BOOL granted, NSError *error);
@protocol EKEventStoreIOS6Support <NSObject>
@optional
+ (NSInteger)authorizationStatusForEntityType:(EKEntityType)entityType;
- (void)requestAccessToEntityType:(EKEntityType)entityType completion:(EKEventStoreRequestAccessCompletionHandler)completion;
@end
#endif
@implementation CalendarModule
#pragma mark - internal methods
-(EKEventStore*)store
{
if (store == nil) {
store = [[EKEventStore alloc] init];
}
if (store == NULL) {
DebugLog(@"[WARN] Could not access EventStore. ");
} else {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(eventStoreChanged:) name:EKEventStoreChangedNotification object:nil];
}
return store;
}
-(NSArray*)allEventKitCalendars
{
if (![NSThread isMainThread]) {
__block id result = nil;
TiThreadPerformOnMainThread(^{result = [[self allEventKitCalendars] retain];}, YES);
return [result autorelease];
}
EKEventStore* ourStore = [self store];
if (ourStore == NULL) {
DebugLog(@"Could not instantiate an event of the event store.");
return nil;
}
return [ourStore calendars];
}
-(void)startup
{
[super startup];
store = NULL;
if ([EKEventStore respondsToSelector:@selector(authorizationStatusForEntityType:)]) {
iOS6API = YES;
}
}
-(void) eventStoreChanged:(NSNotification*)notification
{
if([self _hasListeners:@"change"]) {
[self fireEvent:@"change" withObject:nil];
}
}
#pragma mark - Internal Memory Management
-(void)dealloc
{
[super dealloc];
[store release];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
-(void)didReceiveMemoryWarning:(NSNotification*)notification
{
[super didReceiveMemoryWarning:notification];
}
#pragma mark - Public API's
-(NSArray*)allCalendars
{
if (![NSThread isMainThread]) {
__block id result = nil;
TiThreadPerformOnMainThread(^{result = [[self allCalendars] retain];}, YES);
return [result autorelease];
}
NSArray* calendars_ = [self allEventKitCalendars];
NSMutableArray* calendars = [NSMutableArray arrayWithCapacity:[calendars_ count]];
for (EKCalendar* calendar_ in calendars_) {
TiCalendarCalendar* calendar = [[[TiCalendarCalendar alloc] _initWithPageContext:[self executionContext] calendar:calendar_ module:self] autorelease];
[calendars addObject:calendar];
}
return calendars;
}
-(NSArray*)allEditableCalendars
{
if (![NSThread isMainThread]) {
__block id result = nil;
TiThreadPerformOnMainThread(^{result = [[self allEditableCalendars] retain];}, YES);
return [result autorelease];
}
NSArray* calendars_ = [self allEventKitCalendars];
NSMutableArray* editableCalendars = [NSMutableArray array];
for (EKCalendar* calendar_ in calendars_) {
if ([calendar_ allowsContentModifications]) {
TiCalendarCalendar* calendar = [[TiCalendarCalendar alloc] _initWithPageContext:[self executionContext] calendar:calendar_ module:self];
[editableCalendars addObject:calendar];
}
}
return editableCalendars;
}
-(TiCalendarCalendar*)getCalendarById:(id)arg
{
if ([TiUtils isIOS5OrGreater]) {
ENSURE_SINGLE_ARG(arg, NSString);
if (![NSThread isMainThread]) {
__block id result = nil;
TiThreadPerformOnMainThread(^{result = [[self getCalendarById:arg] retain];}, YES);
return [result autorelease];
}
EKEventStore* ourStore = [self store];
if (ourStore == NULL) {
DebugLog(@"Could not instantiate an event of the event store.");
return nil;
}
EKCalendar* calendar_ = [ourStore calendarWithIdentifier:arg];
if (calendar_ == NULL) {
return NULL;
}
TiCalendarCalendar* calendar = [[[TiCalendarCalendar alloc] _initWithPageContext:[self executionContext] calendar:calendar_ module:self] autorelease];
return calendar;
}
else {
DebugLog(@"Ti.Calendar.getCalendarById is only supported in iOS 5.0 and above.");
return nil;
}
}
-(TiCalendarCalendar*)defaultCalendar
{
if (![NSThread isMainThread]) {
__block id result = nil;
TiThreadPerformOnMainThread(^{result = [[self defaultCalendar] retain];}, YES);
return [result autorelease];
}
EKEventStore* ourStore = [self store];
if (ourStore == NULL) {
DebugLog(@"Could not instantiate an event of the event store.");
return nil;
}
EKCalendar* calendar_ = [ourStore defaultCalendarForNewEvents];
if (calendar_ == NULL) {
return nil;
}
TiCalendarCalendar* calendar = [[[TiCalendarCalendar alloc] _initWithPageContext:[self executionContext] calendar:calendar_ module:self] autorelease]; return calendar;
}
-(void) requestAuthorization:(id)args forEntityType:(EKEntityType)entityType
{
ENSURE_SINGLE_ARG(args, KrollCallback);
KrollCallback * callback = args;
NSString * errorStr = nil;
int code = 0;
bool doPrompt = NO;
if (iOS6API) {
long int permissions = [EKEventStore authorizationStatusForEntityType:entityType];
switch (permissions) {
case EKAuthorizationStatusNotDetermined:
doPrompt = YES;
break;
case EKAuthorizationStatusAuthorized:
break;
case EKAuthorizationStatusDenied:
code = EKAuthorizationStatusDenied;
errorStr = @"The user has denied access to events in Calendar.";
case EKAuthorizationStatusRestricted:
code = EKAuthorizationStatusRestricted;
errorStr = @"The user is unable to allow access to events in Calendar.";
default:
break;
}
}
if (!doPrompt) {
NSDictionary * propertiesDict = [TiUtils dictionaryWithCode:code message:errorStr];
NSArray * invocationArray = [[NSArray alloc] initWithObjects:&propertiesDict count:1];
[callback call:invocationArray thisObject:self];
[invocationArray release];
return;
}
TiThreadPerformOnMainThread(^(){
EKEventStore* ourstore = [self store];
[ourstore requestAccessToEntityType:EKEntityTypeEvent
completion:^(BOOL granted, NSError *error){
NSDictionary *propertiesDict = [TiUtils dictionaryWithCode:[error code]
message:[TiUtils messageFromError:error]];
KrollEvent * invocationEvent = [[KrollEvent alloc] initWithCallback:callback eventObject:propertiesDict thisObject:self];
[[callback context] enqueue:invocationEvent];
}];
}, NO);
}
#pragma mark - Public API
-(void) requestEventsAuthorization:(id)args
{
ENSURE_SINGLE_ARG(args, KrollCallback);
[self requestAuthorization:args forEntityType:EKEntityTypeEvent];
}
-(void) requestRemindersAuthorization:(id)args
{
ENSURE_SINGLE_ARG(args, KrollCallback);
[self requestAuthorization:args forEntityType:EKEntityTypeReminder];
}
-(NSNumber*) eventsAuthorization
{
long int result = EKAuthorizationStatusAuthorized;
if (iOS6API) { //in iOS 5.1 and below: no need to check for authorization.
result = [EKEventStore authorizationStatusForEntityType:EKEntityTypeEvent];
}
return [NSNumber numberWithLong:result];
}
#pragma mark - Properties
MAKE_SYSTEM_PROP(STATUS_NONE,EKEventStatusNone);
MAKE_SYSTEM_PROP(STATUS_CONFIRMED,EKEventStatusConfirmed);
MAKE_SYSTEM_PROP(STATUS_TENTATIVE,EKEventStatusNone);
MAKE_SYSTEM_PROP(STATUS_CANCELED,EKEventStatusNone);
MAKE_SYSTEM_PROP(AVAILABILITY_NOTSUPPORTED, EKEventAvailabilityNotSupported);
MAKE_SYSTEM_PROP(AVAILABILITY_BUSY, EKEventAvailabilityBusy);
MAKE_SYSTEM_PROP(AVAILABILITY_FREE, EKEventAvailabilityFree);
MAKE_SYSTEM_PROP(AVAILABILITY_TENTATIVE, EKEventAvailabilityTentative);
MAKE_SYSTEM_PROP(AVAILABILITY_UNAVAILABLE, EKEventAvailabilityUnavailable);
MAKE_SYSTEM_PROP(SPAN_THISEVENT, EKSpanThisEvent);
MAKE_SYSTEM_PROP(SPAN_FUTUREEVENTS, EKSpanFutureEvents);
MAKE_SYSTEM_PROP(RECURRENCEFREQUENCY_DAILY, EKRecurrenceFrequencyDaily);
MAKE_SYSTEM_PROP(RECURRENCEFREQUENCY_WEEKLY, EKRecurrenceFrequencyWeekly);
MAKE_SYSTEM_PROP(RECURRENCEFREQUENCY_MONTHLY, EKRecurrenceFrequencyMonthly);
MAKE_SYSTEM_PROP(RECURRENCEFREQUENCY_YEARLY, EKRecurrenceFrequencyYearly);
MAKE_SYSTEM_PROP(AUTHORIZATION_UNKNOWN, EKAuthorizationStatusNotDetermined);
MAKE_SYSTEM_PROP(AUTHORIZATION_RESTRICTED, EKAuthorizationStatusRestricted);
MAKE_SYSTEM_PROP(AUTHORIZATION_DENIED, EKAuthorizationStatusDenied);
MAKE_SYSTEM_PROP(AUTHORIZATION_AUTHORIZED, EKAuthorizationStatusAuthorized);
@end
#endif