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