2 Copyright (C) 2004 SKYRIX Software AG
4 This file is part of OpenGroupware.org.
6 OGo is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 OGo is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with OGo; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 #include "SOGoAppointment.h"
24 #include "SOGoAppointmentICalRenderer.h"
25 #include <SaxObjC/SaxObjC.h>
26 #include <NGiCal/NGiCal.h>
27 #include <EOControl/EOControl.h>
30 @interface SOGoAppointment (PrivateAPI)
31 - (NSArray *)_filteredAttendeesThinkingOfPersons:(BOOL)_persons;
34 @implementation SOGoAppointment
36 static id<NSObject,SaxXMLReader> parser = nil;
37 static SaxObjectDecoder *sax = nil;
41 parser = [[[SaxXMLReaderFactory standardXMLReaderFactory]
42 createXMLReaderForMimeType:@"text/calendar"]
45 NSLog(@"ERROR: did not find a parser for text/calendar!");
48 sax = [[SaxObjectDecoder alloc] initWithMappingNamed:@"NGiCal"];
50 NSLog(@"ERROR: could not create the iCal SAX handler!");
53 [parser setContentHandler:sax];
54 [parser setErrorHandler:sax];
57 - (id)initWithICalRootObject:(id)_root {
58 if ((self = [super init])) {
60 [self logWithFormat:@"root is: %@", root];
63 if ([_root isKindOfClass:[iCalEvent class]]) {
64 self->event = [_root retain];
66 else if ([_root isKindOfClass:[NSDictionary class]]) {
67 /* multiple vevents in the calendar */
69 @"ERROR(%s): tried to initialize with multiple records: %@",
70 __PRETTY_FUNCTION__, _root];
75 self->calendar = [_root retain];
76 self->event = [[[self->calendar events] lastObject] retain];
81 - (id)initWithICalString:(NSString *)_iCal {
84 if ([_iCal length] == 0) {
85 [self logWithFormat:@"ERROR: tried to init SOGoAppointment without iCal"];
89 if (parser == nil || sax == nil) {
90 [self logWithFormat:@"ERROR: iCal parser not properly set up!"];
95 if ([_iCal length] > 0) {
96 [parser parseFromSource:_iCal];
97 root = [[sax rootObject] retain]; /* retain to keep it around */
103 self = [self initWithICalRootObject:root];
109 [self->calendar release];
110 [self->event release];
117 return self->calendar;
124 - (NSString *)iCalString {
125 return [[SOGoAppointmentICalRenderer sharedAppointmentRenderer]
126 stringForAppointment:self];
129 /* forwarded methods */
131 - (void)setUid:(NSString *)_value {
132 [self->event setUid:_value];
135 return [self->event uid];
138 - (void)setSummary:(NSString *)_value {
139 [self->event setSummary:_value];
141 - (NSString *)summary {
142 return [self->event summary];
145 - (void)setLocation:(NSString *)_value {
146 [self->event setLocation:_value];
148 - (NSString *)location {
149 return [self->event location];
151 - (BOOL)hasLocation {
152 if (![[self location] isNotNull])
154 return [[self location] length] > 0 ? YES : NO;
157 - (void)setComment:(NSString *)_value {
158 if([_value length] == 0)
160 [self->event setComment:_value];
162 - (NSString *)comment {
163 return [self->event comment];
166 NSString *s = [self comment];
167 if(!s || [s length] == 0)
172 - (void)setPriority:(NSString *)_value {
173 [self->event setPriority:_value];
175 - (NSString *)priority {
176 return [self->event priority];
178 - (BOOL)hasPriority {
179 NSString *prio = [self priority];
185 r = [prio rangeOfString:@";"];
187 prio = [prio substringToIndex:r.location];
189 return [prio isEqualToString:@"0"] ? NO : YES;
192 - (void)setCategories:(NSArray *)_value {
195 if(!_value || [_value count] == 0) {
196 [self->event setCategories:nil];
199 _value = [_value sortedArrayUsingSelector:@selector(compareAscending:)];
200 catString = [_value componentsJoinedByString:@","];
201 [self->event setCategories:catString];
204 - (NSArray *)categories {
209 catString = [self->event categories];
211 return [NSArray array];
213 r = [catString rangeOfString:@";"];
215 catString = [catString substringToIndex:r.location];
217 cats = [catString componentsSeparatedByString:@","];
220 - (BOOL)hasCategories {
221 return [self->event categories] != nil ? YES : NO;
224 - (void)setStatus:(NSString *)_value {
225 [self->event setStatus:_value];
227 - (NSString *)status {
228 return [self->event status];
231 - (void)setStartDate:(NSCalendarDate *)_date {
232 [self->event setStartDate:_date];
234 - (NSCalendarDate *)startDate {
235 return [self->event startDate];
238 - (void)setEndDate:(NSCalendarDate *)_date {
239 [self->event setEndDate:_date];
241 - (NSCalendarDate *)endDate {
242 return [self->event endDate];
245 return [self->event hasEndDate];
248 - (void)setDuration:(NSTimeInterval)_duration {
250 [self logWithFormat:@"WARNING: could not apply duration!"];
252 - (BOOL)hasDuration {
253 return [self->event hasDuration];
255 - (NSTimeInterval)duration {
256 return [self->event durationAsTimeInterval];
259 - (void)setOrganizer:(iCalPerson *)_organizer {
260 [self->event setOrganizer:_organizer];
262 - (iCalPerson *)organizer {
263 return [self->event organizer];
266 - (void)removeAllAttendees {
267 [self setAttendees:nil];
269 - (void)addToAttendees:(iCalPerson *)_person {
270 [self->event addToAttendees:_person];
272 - (void)appendAttendees:(NSArray *)_persons {
275 count = [_persons count];
276 for (i = 0; i < count; i++)
277 [self addToAttendees:[_persons objectAtIndex:i]];
279 - (void)setAttendees:(NSArray *)_persons {
280 [self->event removeAllAttendees];
281 if ([_persons count] > 0)
282 [self appendAttendees:_persons];
284 - (NSArray *)attendees {
285 return [self->event attendees];
288 - (NSArray *)participants {
289 if (self->participants)
290 return self->participants;
292 self->participants = [[self _filteredAttendeesThinkingOfPersons:YES] retain];
293 return self->participants;
295 - (BOOL)hasParticipants {
296 return [[self participants] count] != 0;
299 - (NSArray *)resources {
300 return [self _filteredAttendeesThinkingOfPersons:NO];
303 - (NSArray *)_filteredAttendeesThinkingOfPersons:(BOOL)_persons {
305 NSMutableArray *filtered;
308 list = [self attendees];
309 count = [list count];
310 filtered = [NSMutableArray arrayWithCapacity:count];
311 for (i = 0; i < count; i++) {
315 p = [list objectAtIndex:i];
318 if (role == nil || ![role isEqualToString:@"NON-PART"])
319 [filtered addObject:p];
322 if ([role isEqualToString:@"NON-PART"])
323 [filtered addObject:p];
329 @end /* SOGoAppointment */