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