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