/* Copyright (C) 2004 SKYRIX Software AG This file is part of OpenGroupware.org. OGo is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. OGo is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with OGo; see the file COPYING. If not, write to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include "OCSiCalFieldExtractor.h" #include "common.h" #include #include @implementation OCSiCalFieldExtractor static id parser = nil; static SaxObjectDecoder *sax = nil; static OCSiCalFieldExtractor *extractor = nil; + (void)initialize { if (parser == nil) { parser = [[[SaxXMLReaderFactory standardXMLReaderFactory] createXMLReaderForMimeType:@"text/calendar"] retain]; if (parser == nil) NSLog(@"ERROR: did not find a parser for text/calendar!"); } if (sax == nil) { sax = [[SaxObjectDecoder alloc] initWithMappingNamed:@"NGiCal"]; if (sax == nil) NSLog(@"ERROR: could not create the iCal SAX handler!"); } [parser setContentHandler:sax]; [parser setErrorHandler:sax]; } + (id)sharedICalFieldExtractor { if (extractor == nil) extractor = [[self alloc] init]; return extractor; } - (void)dealloc { [super dealloc]; } /* operations */ - (NSNumber *)numberForDate:(NSCalendarDate *)_date { return [NSNumber numberWithUnsignedInt:[_date timeIntervalSince1970]]; } - (NSMutableDictionary *)extractQuickFieldsFromEvent:(iCalEvent *)_event { NSMutableDictionary *row; NSCalendarDate *startDate, *endDate; NSArray *attendees; NSString *uid, *title, *location, *status, *accessClass; NSNumber *sequence; id organizer; id participants, partmails; NSMutableString *partstates; unsigned i, count; if (_event == nil) return nil; /* extract values */ startDate = [_event startDate]; endDate = [_event endDate]; uid = [_event uid]; title = [_event summary]; location = [_event location]; sequence = [_event sequence]; accessClass = [[_event accessClass] uppercaseString]; status = [[_event status] uppercaseString]; attendees = [_event attendees]; partmails = [attendees valueForKey:@"rfc822Email"]; partmails = [partmails componentsJoinedByString:@"\n"]; participants = [attendees valueForKey:@"cn"]; participants = [participants componentsJoinedByString:@"\n"]; /* build row */ row = [NSMutableDictionary dictionaryWithCapacity:8]; if ([uid isNotNull]) [row setObject:uid forKey:@"uid"]; else [self logWithFormat:@"WARNING: could not extract a uid from event!"]; [row setObject:[NSNumber numberWithBool:[_event isAllDay]] forKey:@"isallday"]; [row setObject:[NSNumber numberWithBool:[_event isRecurrent]] forKey:@"iscycle"]; [row setObject:[NSNumber numberWithBool:[_event isOpaque]] forKey:@"isopaque"]; if ([title isNotNull]) [row setObject:title forKey:@"title"]; if ([location isNotNull]) [row setObject:location forKey:@"location"]; if ([sequence isNotNull]) [row setObject:sequence forKey:@"sequence"]; if ([startDate isNotNull]) [row setObject:[self numberForDate:startDate] forKey:@"startdate"]; if ([endDate isNotNull]) [row setObject:[self numberForDate:endDate] forKey:@"enddate"]; if ([participants length] > 0) [row setObject:participants forKey:@"participants"]; if ([partmails length] > 0) [row setObject:partmails forKey:@"partmails"]; if ([status isNotNull]) { int code = 1; if ([status isEqualToString:@"TENTATIVE"]) code = 0; else if ([status isEqualToString:@"CANCELLED"]) code = 2; [row setObject:[NSNumber numberWithInt:code] forKey:@"status"]; } else { /* confirmed by default */ [row setObject:[NSNumber numberWithInt:1] forKey:@"status"]; } if([accessClass isNotNull] && ![accessClass isEqualToString:@"PUBLIC"]) { [row setObject:[NSNumber numberWithBool:NO] forKey:@"ispublic"]; } else { [row setObject:[NSNumber numberWithBool:YES] forKey:@"ispublic"]; } organizer = [_event organizer]; if (organizer) { NSString *email; email = [organizer valueForKey:@"rfc822Email"]; if (email) [row setObject:email forKey:@"orgmail"]; } /* construct partstates */ count = [attendees count]; partstates = [[NSMutableString alloc] initWithCapacity:count * 2]; for ( i = 0; i < count; i++) { iCalPerson *p; iCalPersonPartStat stat; p = [attendees objectAtIndex:i]; stat = [p participationStatus]; if(i != 0) [partstates appendString:@"\n"]; [partstates appendFormat:@"%d", stat]; } [row setObject:partstates forKey:@"partstates"]; [partstates release]; return row; } - (NSMutableDictionary *)extractQuickFieldsFromCalendar:(iCalCalendar *)_cal { id event; if (_cal == nil) return nil; event = [[_cal events] lastObject]; return [self extractQuickFieldsFromEvent:event]; } - (NSMutableDictionary *)extractQuickFieldsFromContent:(NSString *)_content { NSAutoreleasePool *pool; NSDictionary *fields; id cal; if (parser == nil || sax == nil) return nil; if ([_content length] == 0) return nil; pool = [[NSAutoreleasePool alloc] init]; [parser parseFromSource:_content]; cal = [sax rootObject]; fields = [[self extractQuickFieldsFromCalendar:cal] retain]; [sax reset]; [pool release]; return [fields autorelease]; } @end /* OCSiCalFieldExtractor */