]> err.no Git - sope/blob - sope-ical/NGiCal/iCalEntityObject.m
6f78e6c53e66b2c3de50b14b5834025fdf82f861
[sope] / sope-ical / NGiCal / 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 #include "iCalEntityObject.h"
23 #include "iCalPerson.h"
24 #include "common.h"
25
26 @interface iCalEntityObject (PrivateAPI)
27 - (NSArray *)_filteredAttendeesThinkingOfPersons:(BOOL)_persons;
28 @end
29
30 @implementation iCalEntityObject
31
32 + (int)version {
33   return [super version] + 0 /* v0 */;
34 }
35 + (void)initialize {
36   NSAssert2([super version] == 0,
37             @"invalid superclass (%@) version %i !",
38             NSStringFromClass([self superclass]), [super version]);
39 }
40
41 - (void)dealloc {
42   [self->uid          release];
43   [self->summary      release];
44   [self->created      release];
45   [self->lastModified release];
46   [self->startDate    release];
47   [self->accessClass  release];
48   [self->priority     release];
49   [self->alarms       release];
50   [self->organizer    release];
51   [self->attendees    release];
52   [self->comment      release];
53   [self->sequence     release];
54   [self->location     release];
55   [self->status       release];
56   [self->categories   release];
57   [self->userComment  release];
58   [super dealloc];
59 }
60
61 /* NSCopying */
62
63 - (id)copyWithZone:(NSZone *)_zone {
64   iCalEntityObject *new;
65
66   new = [super copyWithZone:_zone];
67
68   ASSIGNCOPY(new->uid,          self->uid);
69   ASSIGNCOPY(new->summary,      self->summary);
70   new->timestamp = self->timestamp;
71   ASSIGNCOPY(new->created,      self->created);
72   ASSIGNCOPY(new->lastModified, self->lastModified);
73   ASSIGNCOPY(new->startDate,    self->startDate);
74   ASSIGNCOPY(new->accessClass,  self->accessClass);
75   ASSIGNCOPY(new->priority,     self->priority);
76   ASSIGNCOPY(new->alarms,       self->alarms);
77   ASSIGNCOPY(new->organizer,    self->organizer);
78   ASSIGNCOPY(new->attendees,    self->attendees);
79   ASSIGNCOPY(new->comment,      self->comment);
80   ASSIGNCOPY(new->sequence,     self->sequence);
81   ASSIGNCOPY(new->location,     self->location);
82   ASSIGNCOPY(new->status,       self->status);
83   ASSIGNCOPY(new->categories,   self->categories);
84   ASSIGNCOPY(new->userComment,  self->userComment);
85
86   return new;
87 }
88
89 /* accessors */
90
91 - (void)setUid:(NSString *)_value {
92   if (self->uid != _value) {
93     [self->uid autorelease];
94     self->uid = [_value retain];
95   }
96 }
97 - (NSString *)uid {
98   return self->uid;
99 }
100
101 - (void)setSummary:(NSString *)_value {
102   if (self->summary != _value) {
103     [self->summary autorelease];
104     self->summary = [_value retain];
105   }
106 }
107 - (NSString *)summary {
108   return self->summary;
109 }
110
111 - (void)setLocation:(NSString *)_value {
112   if (self->location != _value) {
113     [self->location autorelease];
114     self->location = [_value retain];
115   }
116 }
117 - (NSString *)location {
118   return self->location;
119 }
120
121 - (void)setComment:(NSString *)_value {
122   if (self->comment != _value) {
123     [self->comment autorelease];
124     self->comment = [_value retain];
125   }
126 }
127 - (NSString *)comment {
128   return self->comment;
129 }
130
131 - (void)setAccessClass:(NSString *)_value {
132   if (self->accessClass != _value) {
133     [self->accessClass autorelease];
134     self->accessClass = [_value retain];
135   }
136 }
137 - (NSString *)accessClass {
138   return self->accessClass;
139 }
140
141 - (void)setPriority:(NSString *)_value {
142   if (self->priority != _value) {
143     [self->priority autorelease];
144     self->priority = [_value retain];
145   }
146 }
147 - (NSString *)priority {
148   return self->priority;
149 }
150
151 - (void)setCategories:(NSString *)_value {
152   ASSIGN(self->categories, _value);
153 }
154 - (NSString *)categories {
155   return self->categories;
156 }
157
158 - (void)setSequence:(NSNumber *)_value {
159   if (![_value isNotNull]) _value = nil;
160   if (self->sequence != _value) {
161     if (_value != nil && ![_value isKindOfClass:[NSNumber class]])
162       _value = [NSNumber numberWithInt:[_value intValue]];
163     [self->sequence autorelease];
164     self->sequence = [_value retain];
165   }
166 }
167 - (NSNumber *)sequence {
168   return self->sequence;
169 }
170 - (void)increaseSequence {
171   int seq;
172   
173   seq = [[self sequence] intValue];
174   seq += 1;
175   [self setSequence:[NSNumber numberWithInt:seq]];
176 }
177
178 - (void)setStatus:(NSString *)_value {
179   ASSIGNCOPY(self->status, _value);
180 }
181 - (NSString *)status {
182   // eg: STATUS:CONFIRMED
183   return self->status;
184 }
185
186 - (void)setCreated:(NSCalendarDate *)_value {
187   if (self->created != _value) {
188     [self->created autorelease];
189     self->created = [_value retain];
190   }
191 }
192 - (NSCalendarDate *)created {
193   return self->created;
194 }
195 - (void)setLastModified:(NSCalendarDate *)_value {
196   if (self->lastModified != _value) {
197     [self->lastModified autorelease];
198     self->lastModified = [_value retain];
199   }
200 }
201 - (NSCalendarDate *)lastModified {
202   return self->lastModified;
203 }
204
205 - (void)setTimeStampAsDate:(NSCalendarDate *)_date {
206   /* TODO: too be completed */
207 }
208 - (NSCalendarDate *)timeStampAsDate {
209   return [NSDate dateWithTimeIntervalSince1970:self->timestamp];
210 }
211
212 - (void)setStartDate:(NSCalendarDate *)_date {
213   if (self->startDate != _date) {
214     [self->startDate autorelease];
215     self->startDate = [_date retain];
216   }
217 }
218 - (NSCalendarDate *)startDate {
219   return self->startDate;
220 }
221
222 - (void)setOrganizer:(iCalPerson *)_organizer {
223   if (self->organizer != _organizer) {
224     [self->organizer autorelease];
225     self->organizer = [_organizer retain];
226   }
227 }
228 - (iCalPerson *)organizer {
229   return self->organizer;
230 }
231
232 - (void)removeAllAttendees {
233     [self->attendees removeAllObjects];
234 }
235 - (void)addToAttendees:(iCalPerson *)_person {
236   if (_person == nil) return;
237   if (self->attendees == nil)
238     self->attendees = [[NSMutableArray alloc] initWithCapacity:4];
239   [self->attendees addObject:_person];
240 }
241 - (NSArray *)attendees {
242   return self->attendees;
243 }
244
245 - (void)removeAllAlarms {
246     [self->alarms removeAllObjects];
247 }
248 - (void)addToAlarms:(id)_alarm {
249   if (_alarm == nil) return;
250   if (self->alarms == nil)
251     self->alarms = [[NSMutableArray alloc] initWithCapacity:1];
252   [self->alarms addObject:_alarm];
253 }
254 - (BOOL)hasAlarms {
255   return [self->alarms count] > 0 ? YES : NO;
256 }
257 - (NSArray *)alarms {
258   return self->alarms;
259 }
260
261 - (void)setUserComment:(NSString *)_userComment {
262   ASSIGN(self->userComment, _userComment);
263 }
264 - (NSString *)userComment {
265   return self->userComment;
266 }
267
268 /* stuff */
269
270 - (NSArray *)participants {
271   return [self _filteredAttendeesThinkingOfPersons:YES];
272 }
273 - (NSArray *)resources {
274   return [self _filteredAttendeesThinkingOfPersons:NO];
275 }
276
277 - (NSArray *)_filteredAttendeesThinkingOfPersons:(BOOL)_persons {
278   NSArray        *list;
279   NSMutableArray *filtered;
280   unsigned       i, count;
281   
282   list     = [self attendees];
283   count    = [list count];
284   filtered = [NSMutableArray arrayWithCapacity:count];
285   for (i = 0; i < count; i++) {
286     iCalPerson *p;
287     NSString   *role;
288     
289     p = [list objectAtIndex:i];
290     role = [p role];
291     if (_persons) {
292       if (role == nil || ![role hasPrefix:@"NON-PART"])
293         [filtered addObject:p];
294     }
295     else {
296       if ([role hasPrefix:@"NON-PART"])
297         [filtered addObject:p];
298     }
299   }
300   return filtered;
301 }
302
303 - (BOOL)isOrganizer:(id)_email {
304   _email = [_email lowercaseString];
305   return [[[[self organizer] rfc822Email] lowercaseString]
306                                           isEqualToString:_email];
307 }
308
309 - (BOOL)isParticipant:(id)_email {
310   NSArray *partEmails;
311   
312   _email     = [_email lowercaseString];
313   partEmails = [[self participants] valueForKey:@"rfc822Email"];
314   partEmails = [partEmails valueForKey:@"lowercaseString"];
315   return [partEmails containsObject:_email];
316 }
317
318 - (iCalPerson *)findParticipantWithEmail:(id)_email {
319   NSArray  *ps;
320   unsigned i, count;
321   
322   _email = [_email lowercaseString];
323   ps     = [self participants];
324   count  = [ps count];
325
326   for (i = 0; i < count; i++) {
327     iCalPerson *p;
328     
329     p = [ps objectAtIndex:i];
330     if ([[[p rfc822Email] lowercaseString] isEqualToString:_email])
331       return p;
332   }
333   return nil; /* not found */
334 }
335
336 @end /* iCalEntityObject */