]> err.no Git - scalable-opengroupware.org/blob - SOPE/NGCards/iCalEntityObject.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1178 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / SOPE / NGCards / iCalEntityObject.m
1 /*
2   Copyright (C) 2000-2005 SKYRIX Software AG
3
4   This file is part of SOPE.
5
6   SOPE 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
9   later version.
10
11   SOPE 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.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with SOPE; see the file COPYING.  If not, write to the
18   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19   02111-1307, USA.
20 */
21
22 #import <Foundation/NSArray.h>
23 #import <Foundation/NSString.h>
24 #import <Foundation/NSURL.h>
25 #import <Foundation/NSValue.h>
26
27 #import "NSCalendarDate+NGCards.h"
28
29 #import "iCalAlarm.h"
30 #import "iCalDateTime.h"
31 #import "iCalEntityObject.h"
32 #import "iCalPerson.h"
33
34 @interface iCalEntityObject (PrivateAPI)
35 - (NSArray *)_filteredAttendeesThinkingOfPersons:(BOOL)_persons;
36 @end
37
38 @implementation iCalEntityObject
39
40 - (Class) classForTag: (NSString *) classTag
41 {
42   Class tagClass;
43
44   if ([classTag isEqualToString: @"ATTENDEE"]
45       || [classTag isEqualToString: @"ORGANIZER"])
46     tagClass = [iCalPerson class];
47   else if ([classTag isEqualToString: @"VALARM"])
48     tagClass = [iCalAlarm class];
49   else if ([classTag isEqualToString: @"SUMMARY"]
50            || [classTag isEqualToString: @"UID"]
51            || [classTag isEqualToString: @"COMMENT"]
52            || [classTag isEqualToString: @"DESCRIPTION"]
53            || [classTag isEqualToString: @"CLASS"]
54            || [classTag isEqualToString: @"STATUS"]
55            || [classTag isEqualToString: @"SEQUENCE"]
56            || [classTag isEqualToString: @"URL"]
57            || [classTag isEqualToString: @"PRIORITY"]
58            || [classTag isEqualToString: @"CATEGORIES"]
59            || [classTag isEqualToString: @"LOCATION"])
60     tagClass = [CardElement class];
61   else if ([classTag isEqualToString: @"DTSTAMP"]
62            || [classTag isEqualToString: @"DTSTART"]
63            || [classTag isEqualToString: @"CREATED"]
64            || [classTag isEqualToString: @"LAST-MODIFIED"])
65     tagClass = [iCalDateTime class];
66   else
67     tagClass = [super classForTag: classTag];
68
69   return tagClass;
70 }
71
72 /* accessors */
73
74 - (void) setUid: (NSString *) _uid
75 {
76   [[self uniqueChildWithTag: @"uid"] setValue: 0 to: _uid];
77 }
78
79 - (NSString *) uid
80 {
81   return [[self uniqueChildWithTag: @"uid"] value: 0];
82 }
83
84 - (void) setSummary: (NSString *) _value
85 {
86   [[self uniqueChildWithTag: @"summary"] setValue: 0 to: _value];
87 }
88
89 - (NSString *) summary
90 {
91   return [[self uniqueChildWithTag: @"summary"] value: 0];
92 }
93
94 - (void) setLocation: (NSString *) _value
95 {
96   [[self uniqueChildWithTag: @"location"] setValue: 0 to: _value];
97 }
98
99 - (NSString *) location
100 {
101   return [[self uniqueChildWithTag: @"location"] value: 0];
102 }
103
104 - (void) setComment: (NSString *) _value
105 {
106   [[self uniqueChildWithTag: @"description"] setValue: 0 to: _value];
107 }
108
109 - (NSString *) comment
110 {
111   return [[self uniqueChildWithTag: @"description"] value: 0];
112 }
113
114 - (void) setAccessClass: (NSString *) _value
115 {
116   [[self uniqueChildWithTag: @"class"] setValue: 0 to: _value];
117 }
118
119 - (NSString *) accessClass
120 {
121   return [[self uniqueChildWithTag: @"class"] value: 0];
122 }
123
124 - (iCalAccessClass) symbolicAccessClass
125 {
126   iCalAccessClass symbolicAccessClass;
127   NSString *accessClass;
128
129   accessClass = [[self accessClass] uppercaseString];
130   if ([accessClass isEqualToString: @"PRIVATE"])
131     symbolicAccessClass = iCalAccessPrivate;
132   else if ([accessClass isEqualToString: @"CONFIDENTIAL"])
133     symbolicAccessClass = iCalAccessConfidential;
134   else
135     symbolicAccessClass = iCalAccessPublic;
136
137   return symbolicAccessClass;
138 }
139
140 - (BOOL) isPublic
141 {
142   return ([self symbolicAccessClass] == iCalAccessPublic);
143 }
144
145 - (void) setPriority: (NSString *) _value
146 {
147   [[self uniqueChildWithTag: @"priority"] setValue: 0 to: _value];
148 }
149
150 - (NSString *) priority
151 {
152   return [[self uniqueChildWithTag: @"priority"] value: 0];
153 }
154
155 - (void) setCategories: (NSString *) _value
156 {
157   [[self uniqueChildWithTag: @"categories"] setValue: 0 to: _value];
158 }
159
160 - (NSString *) categories
161 {
162   return [[self uniqueChildWithTag: @"categories"] value: 0];
163 }
164
165 - (void) setUserComment: (NSString *) _value
166 {
167   [[self uniqueChildWithTag: @"usercomment"] setValue: 0 to: _value];
168 }
169
170 - (NSString *) userComment
171 {
172   return [[self uniqueChildWithTag: @"usercomment"] value: 0];
173 }
174
175 - (void) setStatus: (NSString *) _value
176 {
177   [[self uniqueChildWithTag: @"status"] setValue: 0 to: _value];
178 }
179
180 - (NSString *) status
181 {
182   return [[self uniqueChildWithTag: @"status"] value: 0];
183 }
184
185 - (void) setSequence: (NSNumber *)_value
186 {
187   NSString *sequence;
188
189   sequence = [NSString stringWithFormat: @"%@", _value];
190   [[self uniqueChildWithTag: @"sequence"] setValue: 0
191                                           to: sequence];;
192 }
193
194 - (NSNumber *) sequence
195 {
196   NSString *sequence;
197
198   sequence = [[self uniqueChildWithTag: @"sequence"] value: 0];
199
200   return [NSNumber numberWithInt: [sequence intValue]];
201 }
202
203 - (void) increaseSequence
204 {
205   int seq;
206   
207   seq = [[self sequence] intValue];
208   seq += 1;
209   [self setSequence: [NSNumber numberWithInt: seq]];
210 }
211
212 - (void) setCreated: (NSCalendarDate *) newCreated
213 {
214   [(iCalDateTime *) [self uniqueChildWithTag: @"created"]
215                     setDateTime: newCreated];
216 }
217
218 - (NSCalendarDate *) created
219 {
220   return [(iCalDateTime *) [self uniqueChildWithTag: @"created"]
221                            dateTime];
222 }
223
224 - (void) setLastModified: (NSCalendarDate *) newLastModified
225 {
226   [(iCalDateTime *) [self uniqueChildWithTag: @"last-modified"]
227                     setDateTime: newLastModified];
228 }
229
230 - (NSCalendarDate *) lastModified
231 {
232   return [(iCalDateTime *) [self uniqueChildWithTag: @"last-modified"]
233                            dateTime];
234 }
235
236 - (void) setTimeStampAsDate: (NSCalendarDate *) newTimeStamp
237 {
238   [(iCalDateTime *) [self uniqueChildWithTag: @"dtstamp"]
239                     setDateTime: newTimeStamp];
240 }
241
242 - (NSCalendarDate *) timeStampAsDate
243 {
244   return [(iCalDateTime *) [self uniqueChildWithTag: @"dtstamp"]
245                            dateTime];
246 }
247
248 - (void) setStartDate: (NSCalendarDate *) newStartDate
249 {
250   [(iCalDateTime *) [self uniqueChildWithTag: @"dtstart"]
251                     setDateTime: newStartDate];
252 }
253
254 - (NSCalendarDate *) startDate
255 {
256   return [(iCalDateTime *) [self uniqueChildWithTag: @"dtstart"]
257                            dateTime];
258 }
259
260 - (BOOL) hasStartDate
261 {
262   return ([[self childrenWithTag: @"dtstart"] count] > 0);
263 }
264
265 - (void) setOrganizer: (iCalPerson *) _organizer
266 {
267   [_organizer setTag: @"organizer"];
268   [self setUniqueChild: _organizer];
269 }
270
271 - (iCalPerson *) organizer
272 {
273   return (iCalPerson *) [self uniqueChildWithTag: @"organizer"];
274 }
275
276 - (void) removeAllAttendees
277 {
278   [children removeObjectsInArray: [self attendees]];
279 }
280
281 - (void) addToAttendees: (iCalPerson *) _person
282 {
283   [_person setTag: @"attendee"];
284   [self addChild: _person];
285 }
286
287 - (void) setAttendees: (NSArray *) attendees
288 {
289   [self removeAllAttendees];
290   [self addChildren: attendees];
291 }
292
293 - (NSArray *) attendees
294 {
295   return [self childrenWithTag: @"attendee"];
296 }
297
298 - (void) removeAllAlarms
299 {
300   [children removeObjectsInArray: [self alarms]];
301 }
302
303 - (void) addToAlarms: (id) _alarm
304 {
305   if (_alarm)
306     {
307       [_alarm setTag: @"valarm"];
308       [self addChild: _alarm];
309     }
310 }
311
312 - (BOOL) hasAlarms
313 {
314   return ([[self childrenWithTag: @"valarm"] count] > 0);
315 }
316
317 - (NSArray *) alarms
318 {
319   return [self childrenWithTag: @"valarm"];
320 }
321
322 - (void) setUrl: (id) _value
323 {
324   NSString *asString;
325
326   if ([_value isKindOfClass: [NSString class]])
327     asString = _value;
328   else if ([_value isKindOfClass: [NSURL class]])
329     asString = [_value absoluteString];
330   else
331     asString = @"";
332
333   [[self uniqueChildWithTag: @"url"] setValue: 0 to: asString];
334 }
335
336 - (NSURL *) url
337 {
338   NSString *stringUrl;
339
340   stringUrl = [[self uniqueChildWithTag: @"url"] value: 0];
341
342   return [NSURL URLWithString: stringUrl];
343 }
344
345 /* stuff */
346
347 - (NSArray *) participants
348 {
349   return [self _filteredAttendeesThinkingOfPersons: YES];
350 }
351
352 - (NSArray *) resources
353 {
354   return [self _filteredAttendeesThinkingOfPersons: NO];
355 }
356
357 - (NSArray *) _filteredAttendeesThinkingOfPersons: (BOOL) _persons
358 {
359   NSArray *list;
360   NSMutableArray *filtered;
361   unsigned count, max;
362   iCalPerson *person;
363   NSString *role;
364
365   if (_persons)
366     {
367       list = [self attendees];
368       max = [list count];
369       filtered = [NSMutableArray arrayWithCapacity: max];
370       for (count = 0; count < max; count++)
371         {
372           person = (iCalPerson *) [list objectAtIndex: count];
373           role = [[person role] uppercaseString];
374           if (![role hasPrefix: @"NON-PART"])
375             [filtered addObject: person];
376         }
377
378       list = filtered;
379     }
380   else
381     list = [self childrenWithTag: @"attendee"
382                  andAttribute: @"role"
383                  havingValue: @"non-part"];
384
385   return list;
386 }
387
388 - (BOOL) isOrganizer: (id) _email
389 {
390   NSString *organizerMail;
391
392   organizerMail = [[self organizer] rfc822Email];
393
394   return [[organizerMail lowercaseString]
395            isEqualToString: [_email lowercaseString]];
396 }
397
398 - (BOOL) isParticipant: (id) _email
399 {
400   NSArray *partEmails;
401   
402   _email     = [_email lowercaseString];
403   partEmails = [[self participants] valueForKey:@"rfc822Email"];
404   partEmails = [partEmails valueForKey: @"lowercaseString"];
405   return [partEmails containsObject:_email];
406 }
407
408 - (iCalPerson *) findParticipantWithEmail: (id) _email
409 {
410   NSArray  *ps;
411   unsigned i, count;
412   
413   _email = [_email lowercaseString];
414   ps     = [self participants];
415   count  = [ps count];
416
417   for (i = 0; i < count; i++) {
418     iCalPerson *p;
419     
420     p = [ps objectAtIndex:i];
421     if ([[[p rfc822Email] lowercaseString] isEqualToString:_email])
422       return p;
423   }
424
425   return nil; /* not found */
426 }
427
428 @end /* iCalEntityObject */