-
Notifications
You must be signed in to change notification settings - Fork 8
/
EKEvent+Utilities.m
366 lines (234 loc) · 10.6 KB
/
EKEvent+Utilities.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
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
358
359
360
361
362
363
364
365
366
//
// Category.m
// EKEventToiCal
//
// Created by Dan Willoughby on 6/7/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "EKEvent+Utilities.h"
@implementation EKEvent (Utilities)
NSString *letters = @"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
-(NSString *) genRandStringLength {
int len = 36;
NSMutableString *randomString = [NSMutableString stringWithCapacity: len];
for (int i=0; i<len; i++) {
[randomString appendFormat: @"%c", [letters characterAtIndex:(rand() % [letters length])]];
}
NSString *c = [randomString substringWithRange:NSMakeRange(0, 8)];
NSString *d = [randomString substringWithRange:NSMakeRange(8, 4)];
NSString *e = [randomString substringWithRange:NSMakeRange(12, 4)];
NSString *f = [randomString substringWithRange:NSMakeRange(16, 4)];
NSString *g = [randomString substringWithRange:NSMakeRange(20, 12)];
NSMutableString *stringWithDashes = [NSMutableString string];
[stringWithDashes appendFormat:@"%@-%@-%@-%@-%@",c,d,e,f,g];
return stringWithDashes;
}
- (NSMutableString*)iCalString {
NSMutableString *iCalString = [NSMutableString string];
//The first line must be "BEGIN:VCALENDAR"
[iCalString appendString:@"BEGIN:VCALENDAR"];
[iCalString appendString:@"\r\nVERSION:2.0"];
//calendar
if (self.calendar.title) {
//[iCalString appendFormat:@"\r\nX-WR-CALNAME:%@",self.calendar.title];
}
// CGColorRef blah = self.calendar.CGColor;
// NSLog(@"********************* = %@",blah);
//X-WR-CALNAME:Untitled 2 -----calendar's Title ical
//X-APPLE-CALENDAR-COLOR:#F57802 -----calendar color ical
//Event Start Date
[iCalString appendString:@"\r\nBEGIN:VEVENT"];
//allDay
if (self.allDay) {
NSDateFormatter *format1 = [[NSDateFormatter alloc] init];
[format1 setDateFormat:@"yyyyMMdd"];
NSString *allDayDate = [format1 stringFromDate:self.startDate];
[iCalString appendFormat:@"\r\nDTSTART;VALUE=DATE:%@",allDayDate];
//get startdate and add 1 day for the end date.
NSDate *addDay = [self.startDate dateByAddingTimeInterval:86400];
NSString *allDayEnd = [format1 stringFromDate:addDay];
[iCalString appendFormat:@"\r\nDTEND;VALUE=DATE:%@",allDayEnd];
[format1 release];
}
else {
if (self.startDate && self.endDate) {
[iCalString appendString:@"\r\nDTSTART;TZID=America/Denver:"];
NSDateFormatter *format2 = [[NSDateFormatter alloc] init];
[format2 setDateFormat:@"yyyyMMdd'T'HHmmss"];
NSString *dateAsString = [format2 stringFromDate:self.startDate];
[iCalString appendString:dateAsString];
//end date
[iCalString appendString:@"\r\nDTEND;TZID=America/Denver:"];
NSString *dateAsString1 = [format2 stringFromDate:self.endDate];
[iCalString appendString:dateAsString1];
[format2 release];
}
else {
NSLog(@"****Error****Missing one of needed values: startDate or endDate");
}
}
[iCalString appendString:@"\r\nDTSTAMP:"]; //date the event was created
NSDateFormatter *format3 = [[NSDateFormatter alloc] init];
[format3 setDateFormat:@"yyyyMMdd'T'HHmmss'Z'"];
NSString *dateAsString2 = [format3 stringFromDate:self.lastModifiedDate];
[iCalString appendString:dateAsString2];
//lastModifiedDate
if (self.lastModifiedDate) {
[iCalString appendString:@"\r\nLAST-MODIFIED:"];
NSString *dateAsString2 = [format3 stringFromDate:self.lastModifiedDate];
[iCalString appendString:dateAsString2];
}
[format3 release];
//UID is generated randomly
NSString *a = [self genRandStringLength];
[iCalString appendFormat:@"\r\nUID:%@0000000000000000000",a];
//attendees @TODO: The property is read-only and cannot be modified so this is not complete or tested
for (EKParticipant *attend in self.attendees) {
[iCalString appendString:@"\r\nATTENDEE"];
if (attend.name) {
[iCalString appendFormat:@";CN=%@",attend.name];
}
//@TODO:this is not complete
if (attend.participantStatus) {
[iCalString appendFormat:@";PARTSTAT=%@",attend.participantStatus];
}
//@TODO:this is not complete
if (attend.participantType) {
[iCalString appendFormat:@";CUTYPE=%@",attend.participantType];
}
//@TODO:this is not complete
if (attend.participantRole) {
[iCalString appendFormat:@";ROLE=%@",attend.participantRole];
}
}
//ATTENDEE;CN="Dan Willoughby";CUTYPE=INDIVIDUAL;PARTSTAT=ACCEPTED:mailto:email
//availability @TODO: The property is read-only and cannot be modified so this is not complete or tested
if (self.availability == 1) {
[iCalString appendString:@"\r\nTRANSP:OPAQUE"]; //busy
}
else {
[iCalString appendString:@"\r\nTRANSP:TRANSPARENT"]; //free
}
//eventIdentifier @TODO: The property is read-only and cannot be modified so this is not complete or tested
//isDetached @TODO: The property is read-only and cannot be modified so this is not complete or tested
//location
if (self.location) {
[iCalString appendFormat:@"\r\nLOCATION:%@",self.location];
}
//organizer @TODO: The property is read-only and cannot be modified so this is not complete or tested
if (self.organizer != nil) {
[iCalString appendString:@"\r\nORGANIZER"];
if (self.organizer.name) {
[iCalString appendFormat:@";CN=%@",self.organizer.name];
}
//@TODO:this is not complete
if (self.organizer.participantStatus) {
[iCalString appendFormat:@";PARTSTAT=%@",self.organizer.participantStatus];
}
//@TODO:this is not complete
if (self.organizer.participantType) {
[iCalString appendFormat:@";CUTYPE=%@",self.organizer.participantType];
}
//@TODO:this is not complete
if (self.organizer.participantRole) {
[iCalString appendFormat:@";ROLE=%@",self.organizer.participantRole];
}
}
//recurrenceRule
NSString *recurrenceString = [NSString stringWithFormat:@"%@", self.recurrenceRule];
NSArray *partsArray = [recurrenceString componentsSeparatedByString:@"RRULE "];
if ([partsArray count] > 1) {
NSString *secondHalf = [partsArray objectAtIndex:1];
// int loc = [secondHalf rangeOfString:@"Z"].location;
//if (loc > 0) {
// return [secondHalf substringToIndex:loc];
[iCalString appendFormat:@"\r\nRRULE:%@",secondHalf];
}
//recurrenceRule-frequency(oldway)
/*
if (self.recurrenceRule.frequency == 0) {
[iCalString appendString:@"\r\nRRULE:FREQ=DAILY"];
}
if (self.recurrenceRule.frequency == 1) {
[iCalString appendString:@"\r\nRRULE:FREQ=WEEKLY"];
}
if (self.recurrenceRule.frequency == 2) {
[iCalString appendString:@"\r\nRRULE:FREQ=MONTHLY"];
}
if (self.recurrenceRule.frequency == 3) {
[iCalString appendString:@"\r\nRRULE:FREQ=YEARLY"];
}
if (self.recurrenceRule.interval) {
[iCalString appendFormat:@";INTERVAL=%d",self.recurrenceRule.interval];
}
*/
//When a calendar component is created, its sequence number is zero
[iCalString appendString:@"\r\nSEQUENCE:0"];
//status
if (self.status == 1) {
[iCalString appendString:@"\r\nSTATUS:CONFIRMED"];
}
if (self.status == 2) {
[iCalString appendString:@"\r\nSTATUS:TENTATIVE"];
}
if (self.status == 3) {
[iCalString appendString:@"\r\nSTATUS:CANCELLED"];
}
//Event Title
if (self.title) {
[iCalString appendFormat:@"\r\nSUMMARY:%@",self.title];
}
//Notes
if (self.notes) {
[iCalString appendFormat:@"\r\nDESCRIPTION:%@",self.notes];
}
//Alarm
for (EKAlarm *alarm in self.alarms) {
[iCalString appendString:@"\r\nBEGIN:VALARM"];
[iCalString appendString:@"\r\nACTION:DISPLAY"];//a message(usually the title of the event) will be displayed
[iCalString appendString:@"\r\nDESCRIPTION:event reminder"]; //notes with the alarm--not the message.
if (alarm.absoluteDate) {
NSDateFormatter *format3 = [[NSDateFormatter alloc] init];
[format3 setDateFormat:@"yyyyMMdd'T'HHmmss"];
NSString *dateAsString3 = [format3 stringFromDate:alarm.absoluteDate];
[format3 release];
[iCalString appendFormat:@"\r\nTRIGGER;VALUE=DATE-TIME:%@",dateAsString3];
}
if (alarm.relativeOffset) {
//converts offset to D H M S then appends it to iCalString
NSInteger offset = alarm.relativeOffset;
int i = offset * - 1;
int day = i / (24*60*60);
i = i % (24*60*60);
int hour = i / (60*60);
i = i % (60*60);
int minute = i / 60;
i = i % 60;
int second = i;
[iCalString appendFormat:@"\r\nTRIGGER:-P"];
if (day != 0) {
[iCalString appendFormat:@"%dD", day];
}
if (hour || minute || second != 0) {
[iCalString appendString:@"T"];
if (hour != 0) {
[iCalString appendFormat:@"%dH", hour];
}
if (minute != 0) {
[iCalString appendFormat:@"%dM", minute];
}
if (second != 0) {
[iCalString appendFormat:@"%dS", second];
}
}
}
NSString *b = [self genRandStringLength];
[iCalString appendFormat:@"\r\nX-WR-ALARMUID:%@",b];
[iCalString appendString:@"\r\nEND:VALARM"];
}
[iCalString appendString:@"\r\nEND:VEVENT"];
//The last line must be "END:VCALENDAR"
[iCalString appendString:@"\r\nEND:VCALENDAR"];
return iCalString;
}
@end