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