]> err.no Git - sope/blob - sope-ical/NGiCal/NGVCardName.m
fixed gcc 4.0 warnings
[sope] / sope-ical / NGiCal / NGVCardName.m
1 /*
2   Copyright (C) 2005 Helge Hess
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 "NGVCardName.h"
23 #include "common.h"
24
25 @implementation NGVCardName
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 - (id)initWithPropertyList:(id)_plist group:(NSString *)_group 
37   types:(NSArray *)_types arguments:(NSDictionary *)_a
38 {
39   if ((self = [super initWithGroup:_group types:_types arguments:_a]) != nil) {
40     self->family = [[(NSDictionary *)_plist objectForKey:@"family"] copy];
41     self->given  = [[(NSDictionary *)_plist objectForKey:@"given"]  copy];
42     self->other  = [[(NSDictionary *)_plist objectForKey:@"other"]  copy];
43     self->prefix = [[(NSDictionary *)_plist objectForKey:@"prefix"] copy];
44     self->suffix = [[(NSDictionary *)_plist objectForKey:@"suffix"] copy];
45   }
46   return self;
47 }
48
49 - (id)initWithGroup:(NSString *)_group types:(NSArray *)_types
50   arguments:(NSDictionary *)_a
51 {
52   return [self initWithPropertyList:nil 
53                group:_group types:_types arguments:_a];
54 }
55 - (id)init {
56   return [self initWithPropertyList:nil group:nil types:nil arguments:nil];
57 }
58
59 - (void)dealloc {
60   [self->family release];
61   [self->given  release];
62   [self->other  release];
63   [self->prefix release];
64   [self->suffix release];
65   [super dealloc];
66 }
67
68 /* accessors */
69
70 - (NSString *)family {
71   return self->family;
72 }
73 - (NSString *)given {
74   return self->given;
75 }
76 - (NSString *)other {
77   return self->other;
78 }
79 - (NSString *)prefix {
80   return self->prefix;
81 }
82 - (NSString *)suffix {
83   return self->suffix;
84 }
85
86 /* fake being an array */
87
88 - (id)objectAtIndex:(unsigned)_idx {
89   NSString *s;
90   
91   switch (_idx) {
92   case 0: return (s = [self family]) ? s : (NSString *)[NSNull null];
93   case 1: return (s = [self given])  ? s : (NSString *)[NSNull null];
94   case 2: return (s = [self other])  ? s : (NSString *)[NSNull null];
95   case 3: return (s = [self prefix]) ? s : (NSString *)[NSNull null];
96   case 4: return (s = [self suffix]) ? s : (NSString *)[NSNull null];
97   }
98   
99   // TODO: throw exception
100   return nil;
101 }
102 - (unsigned)count {
103   return 5;
104 }
105
106 /* values */
107
108 - (NSString *)stringValue {
109   return [self vCardString];
110 }
111
112 - (NSString *)xmlString {
113   NSMutableString *ms;
114   NSString *s;
115   
116   ms = [[NSMutableString alloc] initWithCapacity:256];
117   [self appendXMLTag:@"family" value:[self family] to:ms];
118   [self appendXMLTag:@"given"  value:[self given]  to:ms];
119   [self appendXMLTag:@"other"  value:[self other]  to:ms];
120   [self appendXMLTag:@"prefix" value:[self prefix] to:ms];
121   [self appendXMLTag:@"suffix" value:[self suffix] to:ms];
122   s = [[ms copy] autorelease];
123   [ms release];
124   return s;
125 }
126
127 - (NSString *)vCardString {
128   NSMutableString *ms;
129   NSString *s;
130   
131   ms = [[NSMutableString alloc] initWithCapacity:256];
132   [self appendVCardValue:[self family] to:ms]; [ms appendString:@";"];
133   [self appendVCardValue:[self given]  to:ms]; [ms appendString:@";"];
134   [self appendVCardValue:[self other]  to:ms]; [ms appendString:@";"];
135   [self appendVCardValue:[self prefix] to:ms]; [ms appendString:@";"];
136   [self appendVCardValue:[self suffix] to:ms];
137   s = [[ms copy] autorelease];
138   [ms release];
139   return s;
140 }
141
142 - (NSDictionary *)asDictionary {
143   static NSString *keys[] = {
144     @"family", @"given", @"other", @"prefix", @"suffix", nil
145   };
146   id values[[self count] + 1];
147   unsigned i;
148   
149   for (i = 0; i < [self count]; i++)
150     values[i] = [self objectAtIndex:i];
151   
152   return [NSDictionary dictionaryWithObjects:values forKeys:keys
153                        count:[self count]];
154 }
155
156 - (NSArray *)asArray {
157   id values[[self count] + 1];
158   unsigned i;
159
160   for (i = 0; i < [self count]; i++)
161     values[i] = [self objectAtIndex:i];
162   
163   return [NSArray arrayWithObjects:values count:[self count]];
164 }
165
166 - (id)propertyList {
167   return [self asDictionary];
168 }
169
170 /* NSCoding */
171
172 - (void)encodeWithCoder:(NSCoder *)_coder {
173   [super encodeWithCoder:_coder];
174   
175   [_coder encodeObject:self->family];
176   [_coder encodeObject:self->given];
177   [_coder encodeObject:self->other];
178   [_coder encodeObject:self->prefix];
179   [_coder encodeObject:self->suffix];
180 }
181 - (id)initWithCoder:(NSCoder *)_coder {
182   if ((self = [super initWithCoder:_coder]) != nil) {
183     self->family = [[_coder decodeObject] copy];
184     self->given  = [[_coder decodeObject] copy];
185     self->other  = [[_coder decodeObject] copy];
186     self->prefix = [[_coder decodeObject] copy];
187     self->suffix = [[_coder decodeObject] copy];
188   }
189   return self;
190 }
191
192 /* description */
193
194 - (void)appendAttributesToDescription:(NSMutableString *)_ms {
195   [super appendAttributesToDescription:_ms];
196   [_ms appendFormat:@" vcard=%@", [self vCardString]];
197 }
198
199 @end /* NGVCardName */