]> err.no Git - sope/blob - sope-ical/NGiCal/iCalPerson.m
use %p for pointer formats
[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   new->cn       = [self->cn       copyWithZone:_zone];
54   new->email    = [self->email    copyWithZone:_zone];
55   new->rsvp     = [self->rsvp     copyWithZone:_zone];
56   new->partStat = [self->partStat copyWithZone:_zone];
57   new->role     = [self->role     copyWithZone:_zone];
58   new->xuid     = [self->xuid     copyWithZone:_zone];
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 - (NSString *)partStatWithDefault {
134   NSString *s;
135   
136   s = [self partStat];
137   if ([s length] > 0)
138     return s;
139   
140   return @"NEEDS-ACTION";
141 }
142
143 - (void)setParticipationStatus:(iCalPersonPartStat)_status {
144   NSString *stat;
145
146   switch (_status) {
147     case iCalPersonPartStatAccepted:
148       stat = @"ACCEPTED";
149       break;
150     case iCalPersonPartStatDeclined:
151       stat = @"DECLINED";
152       break;
153     case iCalPersonPartStatTentative:
154       stat = @"TENTATIVE";
155       break;
156     case iCalPersonPartStatDelegated:
157       stat = @"DELEGATED";
158       break;
159     case iCalPersonPartStatCompleted:
160       stat = @"COMPLETED";
161       break;
162     case iCalPersonPartStatInProcess:
163       stat = @"IN-PROCESS";
164       break;
165     case iCalPersonPartStatExperimental:
166     case iCalPersonPartStatOther:
167       [NSException raise:NSInternalInconsistencyException
168                    format:@"Attempt to set meaningless "
169                           @"participationStatus (%d)!", _status];
170       stat = nil; /* keep compiler happy */
171       break;
172     default:
173       stat = @"NEEDS-ACTION";
174       break;
175   }
176   [self setPartStat:stat];
177 }
178
179 - (iCalPersonPartStat)participationStatus {
180   NSString *stat;
181   
182   stat = [[self partStat] uppercaseString];
183   if (![stat isNotNull] || [stat isEqualToString:@"NEEDS-ACTION"])
184     return iCalPersonPartStatNeedsAction;
185   else if ([stat isEqualToString:@"ACCEPTED"])
186     return iCalPersonPartStatAccepted;
187   else if ([stat isEqualToString:@"DECLINED"])
188     return iCalPersonPartStatDeclined;
189   else if ([stat isEqualToString:@"TENTATIVE"])
190     return iCalPersonPartStatTentative;
191   else if ([stat isEqualToString:@"DELEGATED"])
192     return iCalPersonPartStatDelegated;
193   else if ([stat isEqualToString:@"COMPLETED"])
194     return iCalPersonPartStatCompleted;
195   else if ([stat isEqualToString:@"IN-PROCESS"])
196     return iCalPersonPartStatInProcess;
197   else if ([stat hasPrefix:@"X-"])
198     return iCalPersonPartStatExperimental;
199   return iCalPersonPartStatOther;
200 }
201
202
203 /* comparison */
204
205 - (unsigned)hash {
206   if([self email])
207     return [[self email] hash];
208   return [super hash];
209 }
210
211 - (BOOL)isEqual:(id)_other {
212   if(_other == nil)
213     return NO;
214   if([_other class] != self->isa)
215     return NO;
216   if([_other hash] != [self hash])
217     return NO;
218   return [self isEqualToPerson:_other];
219 }
220
221 - (BOOL)isEqualToPerson:(iCalPerson *)_other {
222   if(![self hasSameEmailAddress:_other])
223     return NO;
224   if(!IS_EQUAL([self cn], [_other cn], isEqualToString:))
225     return NO;
226   if(!IS_EQUAL([self rsvp], [_other rsvp], isEqualToString:))
227     return NO;
228   if(!IS_EQUAL([self partStat], [_other partStat], isEqualToString:))
229     return NO;
230   if(!IS_EQUAL([self role], [_other role], isEqualToString:))
231     return NO;
232   if(!IS_EQUAL([self xuid], [_other xuid], isEqualToString:))
233     return NO;
234   return YES;
235 }
236
237 - (BOOL)hasSameEmailAddress:(iCalPerson *)_other {
238   return IS_EQUAL([[self email] lowercaseString],
239                   [[_other email] lowercaseString],
240                   isEqualToString:);
241 }
242
243 /* descriptions */
244
245 - (NSString *)description {
246   NSMutableString *ms;
247
248   ms = [NSMutableString stringWithCapacity:128];
249   [ms appendFormat:@"<0x%p[%@]:", self, NSStringFromClass([self class])];
250
251   if (self->cn)       [ms appendFormat:@" cn=%@",     self->cn];
252   if (self->email)    [ms appendFormat:@" email=%@",  self->email];
253   if (self->role)     [ms appendFormat:@" role=%@",   self->role];
254   if (self->xuid)     [ms appendFormat:@" uid=%@",    self->xuid];
255   if (self->partStat) [ms appendFormat:@" status=%@", self->partStat];
256   if (self->rsvp)     [ms appendFormat:@" rsvp=%@",   self->rsvp];
257
258   [ms appendString:@">"];
259   return ms;
260 }
261
262 @end /* iCalPerson */