]> err.no Git - sope/blob - sope-ical/NGiCal/iCalPerson.m
added class version checks
[sope] / sope-ical / NGiCal / iCalPerson.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 "iCalPerson.h"
23 #include "common.h"
24
25 @implementation iCalPerson
26
27 + (int)version {
28   return [super version] + 0 /* v0 */;
29 }
30 + (void)initialize {
31   NSAssert2([super version] == 0,
32             @"invalid superclass (%@) version %i !",
33             NSStringFromClass([self superclass]), [super version]);
34 }
35
36 - (void)dealloc {
37   [self->cn       release];
38   [self->email    release];
39   [self->rsvp     release];
40   [self->partStat release];
41   [self->role     release];
42   [self->xuid     release];
43   [super dealloc];
44 }
45
46 /* NSCopying */
47
48 - (id)copyWithZone:(NSZone *)_zone {
49   iCalPerson *new;
50   
51   new = [super copyWithZone:_zone];
52   
53   ASSIGNCOPY(new->cn,       self->cn);
54   ASSIGNCOPY(new->email,    self->email);
55   ASSIGNCOPY(new->rsvp,     self->rsvp);
56   ASSIGNCOPY(new->partStat, self->partStat);
57   ASSIGNCOPY(new->role,     self->role);
58   ASSIGNCOPY(new->xuid,     self->xuid);
59   
60   return new;
61 }
62
63 /* accessors */
64
65 - (void)setCn:(NSString *)_s {
66   ASSIGNCOPY(self->cn, _s);
67 }
68 - (NSString *)cn {
69   return self->cn;
70 }
71 - (NSString *)cnWithoutQuotes {
72   /* remove quotes around a CN */
73   NSString *_cn;
74   
75   _cn = [self cn];
76   if ([_cn length] <= 2)
77     return _cn;
78   if ([_cn characterAtIndex:0] != '"')
79     return _cn;
80   if (![_cn hasSuffix:@"\""])
81     return _cn;
82   
83   return [_cn substringWithRange:NSMakeRange(1, [_cn length] - 2)];
84 }
85
86 - (void)setEmail:(NSString *)_s {
87   ASSIGNCOPY(self->email, _s);
88 }
89 - (NSString *)email {
90   return self->email;
91 }
92
93 - (NSString *)rfc822Email {
94   NSString *_email;
95   unsigned idx;
96
97   _email = [self email];
98   idx    = NSMaxRange([_email rangeOfString:@":"]);
99
100   if ((idx > 0) && ([_email length] > idx))
101     return [_email substringFromIndex:idx];
102
103   return _email;
104 }
105
106 - (void)setRsvp:(NSString *)_s {
107   ASSIGNCOPY(self->rsvp, _s);
108 }
109 - (NSString *)rsvp {
110   return self->rsvp;
111 }
112
113 - (void)setXuid:(NSString *)_s {
114   ASSIGNCOPY(self->xuid, _s);
115 }
116 - (NSString *)xuid {
117   return self->xuid;
118 }
119
120 - (void)setRole:(NSString *)_s {
121   ASSIGNCOPY(self->role, _s);
122 }
123 - (NSString *)role {
124   return self->role;
125 }
126
127 - (void)setPartStat:(NSString *)_s {
128   ASSIGNCOPY(self->partStat, _s);
129 }
130 - (NSString *)partStat {
131   return self->partStat;
132 }
133
134 - (void)setParticipationStatus:(iCalPersonPartStat)_status {
135   NSString *stat;
136
137   switch (_status) {
138     case iCalPersonPartStatAccepted:
139       stat = @"ACCEPTED";
140       break;
141     case iCalPersonPartStatDeclined:
142       stat = @"DECLINED";
143       break;
144     case iCalPersonPartStatTentative:
145       stat = @"TENTATIVE";
146       break;
147     case iCalPersonPartStatDelegated:
148       stat = @"DELEGATED";
149       break;
150     case iCalPersonPartStatCompleted:
151       stat = @"COMPLETED";
152       break;
153     case iCalPersonPartStatInProcess:
154       stat = @"IN-PROCESS";
155       break;
156     case iCalPersonPartStatExperimental:
157     case iCalPersonPartStatOther:
158       [NSException raise:NSInternalInconsistencyException
159                    format:@"Attempt to set meaningless "
160                           @"participationStatus (%d)!", _status];
161       stat = nil; /* keep compiler happy */
162       break;
163     default:
164       stat = @"NEEDS-ACTION";
165       break;
166   }
167   [self setPartStat:stat];
168 }
169
170 - (iCalPersonPartStat)participationStatus {
171   NSString *stat;
172   
173   stat = [[self partStat] uppercaseString];
174   if (![stat isNotNull] || [stat isEqualToString:@"NEEDS-ACTION"])
175     return iCalPersonPartStatNeedsAction;
176   else if ([stat isEqualToString:@"ACCEPTED"])
177     return iCalPersonPartStatAccepted;
178   else if ([stat isEqualToString:@"DECLINED"])
179     return iCalPersonPartStatDeclined;
180   else if ([stat isEqualToString:@"TENTATIVE"])
181     return iCalPersonPartStatTentative;
182   else if ([stat isEqualToString:@"DELEGATED"])
183     return iCalPersonPartStatDelegated;
184   else if ([stat isEqualToString:@"COMPLETED"])
185     return iCalPersonPartStatCompleted;
186   else if ([stat isEqualToString:@"IN-PROCESS"])
187     return iCalPersonPartStatInProcess;
188   else if ([stat hasPrefix:@"X-"])
189     return iCalPersonPartStatExperimental;
190   return iCalPersonPartStatOther;
191 }
192
193
194 /* comparison */
195
196 - (unsigned)hash {
197   if([self email])
198     return [[self email] hash];
199   return [super hash];
200 }
201
202 - (BOOL)isEqual:(id)_other {
203   if(_other == nil)
204     return NO;
205   if([_other class] != self->isa)
206     return NO;
207   if([_other hash] != [self hash])
208     return NO;
209   return [self isEqualToPerson:_other];
210 }
211
212 - (BOOL)isEqualToPerson:(iCalPerson *)_other {
213   if(![self hasSameEmailAddress:_other])
214     return NO;
215   if(!IS_EQUAL([self cn], [_other cn], isEqualToString:))
216     return NO;
217   if(!IS_EQUAL([self rsvp], [_other rsvp], isEqualToString:))
218     return NO;
219   if(!IS_EQUAL([self partStat], [_other partStat], isEqualToString:))
220     return NO;
221   if(!IS_EQUAL([self role], [_other role], isEqualToString:))
222     return NO;
223   if(!IS_EQUAL([self xuid], [_other xuid], isEqualToString:))
224     return NO;
225   return YES;
226 }
227
228 - (BOOL)hasSameEmailAddress:(iCalPerson *)_other {
229   return IS_EQUAL([[self email] lowercaseString],
230                   [[_other email] lowercaseString],
231                   isEqualToString:);
232 }
233
234 /* descriptions */
235
236 - (NSString *)description {
237   NSMutableString *ms;
238
239   ms = [NSMutableString stringWithCapacity:128];
240   [ms appendFormat:@"<0x%08X[%@]:", self, NSStringFromClass([self class])];
241
242   if (self->cn)       [ms appendFormat:@" cn=%@",     self->cn];
243   if (self->email)    [ms appendFormat:@" email=%@",  self->email];
244   if (self->role)     [ms appendFormat:@" role=%@",   self->role];
245   if (self->xuid)     [ms appendFormat:@" uid=%@",    self->xuid];
246   if (self->partStat) [ms appendFormat:@" status=%@", self->partStat];
247   if (self->rsvp)     [ms appendFormat:@" rsvp=%@",   self->rsvp];
248
249   [ms appendString:@">"];
250   return ms;
251 }
252
253 @end /* iCalPerson */