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