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