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