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