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