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