-
Notifications
You must be signed in to change notification settings - Fork 6
/
Event.m
317 lines (280 loc) · 7.98 KB
/
Event.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
#import <Foundation/Foundation.h>
#import <string.h>
#import "Event.h"
#import "DateRange.h"
@implementation Event(NSCoding)
- (void)encodeWithCoder:(NSCoder *)coder
{
[super encodeWithCoder:coder];
[coder encodeObject:_startDate forKey:@"sdate"];
[coder encodeInt:_duration forKey:@"duration"];
[coder encodeObject:_location forKey:@"location"];
[coder encodeBool:_allDay forKey:@"allDay"];
[coder encodeObject:_rrule forKey:@"rrule"];
[coder encodeBool:_sticky forKey:@"sticky"];
}
- (id)initWithCoder:(NSCoder *)coder
{
[super initWithCoder:coder];
_startDate = [[coder decodeObjectForKey:@"sdate"] retain];
_duration = [coder decodeIntForKey:@"duration"];
_location = [[coder decodeObjectForKey:@"location"] retain];
if ([coder containsValueForKey:@"allDay"])
_allDay = [coder decodeBoolForKey:@"allDay"];
else
_allDay = NO;
if ([coder containsValueForKey:@"rrule"])
_rrule = [[coder decodeObjectForKey:@"rrule"] retain];
if ([coder containsValueForKey:@"sticky"])
_sticky = [coder decodeBoolForKey:@"sticky"];
else
_sticky = NO;
return self;
}
@end
@implementation Event
- (id)copy
{
Event *new = [NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:self]];
[new generateUID];
return new;
}
- (id)initWithStartDate:(Date *)start duration:(int)minutes title:(NSString *)aTitle
{
self = [super initWithSummary:aTitle];
if (self) {
[self setStartDate:start];
[self setDuration:minutes];
[self setSticky:NO];
}
return self;
}
- (void)dealloc
{
RELEASE(_location);
RELEASE(_startDate);
RELEASE(_rrule);
[super dealloc];
}
- (BOOL)isScheduledForDay:(Date *)day
{
return [self intersectionWithDay:day].length > 0;
}
/*
* Values are in seconds whereas time granularity in
* all other Event methods is minute.
* Worth changing ?
*/
- (NSRange)intersectionWithDay:(Date *)day
{
NSEnumerator *enumerator;
DateRange *range = AUTORELEASE([[DateRange alloc] initWithStart:_startDate duration:[self duration]*60]);
if (!_rrule)
return [range intersectionWithDay:day];
enumerator = [_rrule enumeratorFromDate:_startDate length:[self duration]*60];
while ((range = [enumerator nextObject])) {
if ([range intersectsWithDay:day])
return [range intersectionWithDay:day];
if ([[range start] compare:day withTime:NO] > 0)
break;
}
return NSMakeRange(0, 0);
}
- (Date *)nextActivationDate
{
if (!_rrule)
return _startDate;
return nil;
}
- (NSString *)location
{
return _location;
}
- (void)setLocation:(NSString *)location
{
ASSIGN(_location, location);
}
- (BOOL)allDay
{
return _allDay;
}
- (void)setAllDay:(BOOL)allDay
{
/*
* FIXME : why do we force startDate to being a date ?
* What is the relation with the appointment being an
* all day one ?
*/
[_startDate setIsDate:allDay];
_allDay = allDay;
}
- (NSString *)description
{
return [NSString stringWithFormat:@"<%@> from <%@> for <%d>", [self summary], [_startDate description], [self duration]];
}
- (NSString *)details
{
if (_allDay)
return @"all day";
int minute = [_startDate minuteOfDay];
return [NSString stringWithFormat:@"%dh%02d", minute / 60, minute % 60];
}
- (BOOL)contains:(NSString *)text
{
if ([self summary] && [[self summary] rangeOfString:text options:NSCaseInsensitiveSearch].length > 0)
return YES;
if (_location && [_location rangeOfString:text options:NSCaseInsensitiveSearch].length > 0)
return YES;
if ([self text] && [[[self text] string] rangeOfString:text options:NSCaseInsensitiveSearch].length > 0)
return YES;
return NO;
}
- (int)duration
{
if (_allDay)
return 1440;
return _duration;
}
- (Date *)startDate
{
return _startDate;
}
- (RecurrenceRule *)rrule
{
return _rrule;
}
- (BOOL)sticky
{
return _sticky;
}
- (void)setDuration:(int)newDuration
{
_duration = newDuration;
}
- (void)setStartDate:(Date *)newStartDate
{
ASSIGNCOPY(_startDate, newStartDate);
}
- (void)setRRule:(RecurrenceRule *)arule
{
ASSIGN(_rrule, arule);
}
- (void)setSticky:(BOOL)sticky
{
_sticky = sticky;
}
- (NSEnumerator *)dateEnumerator
{
return [_rrule enumeratorFromDate:_startDate];
}
- (NSEnumerator *)dateRangeEnumerator
{
return [_rrule enumeratorFromDate:_startDate length:_allDay?86400:_duration*60];
}
@end
@implementation Event(iCalendar)
- (id)initWithICalComponent:(icalcomponent *)ic
{
icalproperty *prop;
icalproperty *pstart;
icalproperty *pend;
icalproperty *pdur;
struct icaltimetype start;
struct icaltimetype end;
struct icaldurationtype diff;
Date *date;
const char *location;
self = [super initWithICalComponent:ic];
if (self == nil)
return nil;
pstart = icalcomponent_get_first_property(ic, ICAL_DTSTART_PROPERTY);
if (!pstart) {
NSLog(@"No start date");
goto init_error;
}
start = icalproperty_get_dtstart(pstart);
date = [[Date alloc] initWithICalTime:start];
[self setStartDate:date];
pend = icalcomponent_get_first_property(ic, ICAL_DTEND_PROPERTY);
pdur = icalcomponent_get_first_property(ic, ICAL_DURATION_PROPERTY);
if ((!pend && !pdur) || [date isDate])
[self setAllDay:YES];
else {
if (!pend)
diff = icalproperty_get_duration(pdur);
else {
end = icalproperty_get_dtend(pend);
diff = icaltime_subtract(end, start);
}
[self setDuration:icaldurationtype_as_int(diff) / 60];
}
prop = icalcomponent_get_first_property(ic, ICAL_RRULE_PROPERTY);
if (prop)
_rrule = [[RecurrenceRule alloc] initWithICalRRule:icalproperty_get_rrule(prop)];
[date release];
location = icalcomponent_get_location(ic);
if (location)
[self setLocation:[NSString stringWithUTF8String:location]];
prop = icalcomponent_get_first_property(ic, ICAL_X_PROPERTY);
while (prop) {
if (!strcmp(icalproperty_get_x_name(prop), "X-SIMPLEAGENDA-STICKY")) {
[self setSticky:strcmp("YES", icalproperty_get_value_as_string(prop))];
}
prop = icalcomponent_get_next_property(ic, ICAL_X_PROPERTY);
}
return self;
init_error:
NSLog(@"Error creating Event from iCal component");
[self release];
return nil;
}
- (BOOL)updateICalComponent:(icalcomponent *)ic
{
Date *end;
icalproperty *prop;
if (![super updateICalComponent:ic])
return NO;
[self deleteProperty:ICAL_LOCATION_PROPERTY fromComponent:ic];
if ([self location])
icalcomponent_add_property(ic, icalproperty_new_location([[self location] UTF8String]));
[self deleteProperty:ICAL_DTSTART_PROPERTY fromComponent:ic];
icalcomponent_add_property(ic, icalproperty_new_dtstart([_startDate UTCICalTime]));
[self deleteProperty:ICAL_DTEND_PROPERTY fromComponent:ic];
[self deleteProperty:ICAL_DURATION_PROPERTY fromComponent:ic];
if (![self allDay])
icalcomponent_add_property(ic, icalproperty_new_dtend(icaltime_add([_startDate UTCICalTime], icaldurationtype_from_int(_duration * 60))));
else {
end = [_startDate copy];
[end incrementDay];
icalcomponent_add_property(ic, icalproperty_new_dtend([end UTCICalTime]));
[end release];
/* OGo workaround ? */
prop = icalcomponent_get_first_property(ic, ICAL_X_PROPERTY);
while (prop) {
if (!strcmp(icalproperty_get_x_name(prop), "X-MICROSOFT-CDO-ALLDAYEVENT"))
icalcomponent_remove_property(ic, prop);
prop = icalcomponent_get_next_property(ic, ICAL_X_PROPERTY);
}
prop = icalproperty_new_from_string("X-MICROSOFT-CDO-ALLDAYEVENT:TRUE");
icalcomponent_add_property(ic, prop);
}
prop = icalcomponent_get_first_property(ic, ICAL_X_PROPERTY);
while (prop) {
if (!strcmp(icalproperty_get_x_name(prop), "X-SIMPLEAGENDA-STICKY"))
icalcomponent_remove_property(ic, prop);
prop = icalcomponent_get_next_property(ic, ICAL_X_PROPERTY);
}
if ([self sticky]) {
prop = icalproperty_new_from_string("X-SIMPLEAGENDA-STICKY:TRUE");
icalcomponent_add_property(ic, prop);
}
[self deleteProperty:ICAL_RRULE_PROPERTY fromComponent:ic];
if (_rrule)
icalcomponent_add_property(ic, icalproperty_new_rrule([_rrule iCalRRule]));
return YES;
}
- (int)iCalComponentType
{
return ICAL_VEVENT_COMPONENT;
}
@end