]> err.no Git - sope/blob - sope-ical/NGiCal/NGVCardOrg.m
added class version checks
[sope] / sope-ical / NGiCal / NGVCardOrg.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 "NGVCardOrg.h"
23 #include "common.h"
24
25 @implementation NGVCardOrg
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)initWithName:(NSString *)_name units:(NSArray *)_units
37   group:(NSString *)_grp types:(NSArray *)_tps arguments:(NSDictionary *)_a
38 {
39   if ((self = [super initWithGroup:_grp types:_tps arguments:_a]) != nil) {
40     self->orgnam   = [_name  copy];
41     self->orgunits = [_units copy];
42   }
43   return self;
44 }
45
46 - (id)initWithGroup:(NSString *)_group types:(NSArray *)_types
47   arguments:(NSDictionary *)_a
48 {
49   return [self initWithName:nil units:nil
50                group:_group types:_types arguments:_a];
51 }
52 - (id)init {
53   return [self initWithName:nil units:nil group:nil types:nil arguments:nil];
54 }
55
56 - (void)dealloc {
57   [self->orgnam   release];
58   [self->orgunits release];
59   [super dealloc];
60 }
61
62 /* accessors */
63
64 - (NSString *)orgnam {
65   return self->orgnam;
66 }
67 - (NSArray *)orgunits {
68   return self->orgunits;
69 }
70
71 - (NSString *)orgunit {
72   return [self->orgunits count] > 0 ? [self->orgunits objectAtIndex:0] : nil;
73 }
74
75 /* fake being an array */
76
77 - (id)objectAtIndex:(unsigned)_idx {
78   NSString *s;
79
80   if (_idx == 0)
81     return (s = [self orgnam]) ? s : (NSString *)[NSNull null];
82   
83   return [self->orgunits objectAtIndex:(_idx - 1)];
84 }
85 - (unsigned)count {
86   return 1 + [self->orgunits count];
87 }
88
89 /* values */
90
91 - (NSString *)stringValue {
92   return [self vCardString];
93 }
94
95 - (NSString *)xmlString {
96   NSMutableString *ms;
97   NSString *s;
98   unsigned i;
99   
100   ms = [[NSMutableString alloc] initWithCapacity:256];
101   [self appendXMLTag:@"orgnam" value:[self orgnam] to:ms];
102   
103   for (i = 0; i < [self->orgunits count]; i++) {
104     [self appendXMLTag:@"orgunit" value:[self->orgunits objectAtIndex:i]
105           to:ms];
106   }
107   s = [[ms copy] autorelease];
108   [ms release];
109   return s;
110 }
111
112 - (NSString *)vCardString {
113   NSMutableString *ms;
114   NSString *s;
115   unsigned i;
116   
117   ms = [[NSMutableString alloc] initWithCapacity:64];
118   [self appendVCardValue:[self orgnam] to:ms];
119   for (i = 0; i < [self->orgunits count]; i++) {
120     [ms appendString:@";"];
121     [self appendVCardValue:[self->orgunits objectAtIndex:i] to:ms];
122   }
123   s = [[ms copy] autorelease];
124   [ms release];
125   return s;
126 }
127
128 - (NSDictionary *)asDictionary {
129   static NSString *keys[2] = { @"orgnam", @"orgunits" };
130   id values[2];
131   
132   values[0] = [self orgnam];
133   values[1] = [self orgunits];
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->orgnam];
155   [_coder encodeObject:self->orgunits];
156 }
157 - (id)initWithCoder:(NSCoder *)_coder {
158   if ((self = [super initWithCoder:_coder]) != nil) {
159     self->orgnam   = [[_coder decodeObject] copy];
160     self->orgunits = [[_coder decodeObject] copy];
161   }
162   return self;
163 }
164
165 /* description */
166
167 - (void)appendAttributesToDescription:(NSMutableString *)_ms {
168   if (self->orgnam) [_ms appendFormat:@" %@", self->orgnam];
169   if ([self->orgunits count] > 0) {
170     [_ms appendFormat:@" units=%@",
171          [self->orgunits componentsJoinedByString:@","]];
172   }
173   [super appendAttributesToDescription:_ms];
174 }
175
176 @end /* NGVCardOrg */