]> err.no Git - sope/blob - sope-ical/NGiCal/NGVCardOrg.m
minor changes to Xcode project layout
[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 /* fake being an array */
63
64 - (id)objectAtIndex:(unsigned)_idx {
65   NSString *s;
66
67   if (_idx == 0)
68     return (s = [self orgnam]) ? s : (NSString *)[NSNull null];
69   
70   return [self->orgunits objectAtIndex:(_idx - 1)];
71 }
72 - (unsigned)count {
73   return 1 + [self->orgunits count];
74 }
75
76 /* values */
77
78 - (NSString *)stringValue {
79   return [self vCardString];
80 }
81
82 - (NSString *)xmlString {
83   NSMutableString *ms;
84   NSString *s;
85   unsigned i;
86   
87   ms = [[NSMutableString alloc] initWithCapacity:256];
88   [self appendXMLTag:@"orgnam" value:[self orgnam] to:ms];
89   
90   for (i = 0; i < [self->orgunits count]; i++) {
91     [self appendXMLTag:@"orgunit" value:[self->orgunits objectAtIndex:i]
92           to:ms];
93   }
94   s = [[ms copy] autorelease];
95   [ms release];
96   return s;
97 }
98
99 - (NSString *)vCardString {
100   NSMutableString *ms;
101   NSString *s;
102   unsigned i;
103   
104   ms = [[NSMutableString alloc] initWithCapacity:64];
105   [self appendVCardValue:[self orgnam] to:ms];
106   for (i = 0; i < [self->orgunits count]; i++) {
107     [ms appendString:@";"];
108     [self appendVCardValue:[self->orgunits objectAtIndex:i] to:ms];
109   }
110   s = [[ms copy] autorelease];
111   [ms release];
112   return s;
113 }
114
115 - (NSDictionary *)asDictionary {
116   static NSString *keys[2] = { @"orgnam", @"orgunits" };
117   id values[2];
118   
119   values[0] = [self orgnam];
120   values[1] = [self orgunits];
121   
122   return [NSDictionary dictionaryWithObjects:values forKeys:keys 
123                        count:[self count]];
124 }
125
126 - (NSArray *)asArray {
127   id values[[self count] + 1];
128   unsigned i;
129
130   for (i = 0; i < [self count]; i++)
131     values[i] = [self objectAtIndex:i];
132   
133   return [NSArray arrayWithObjects:values count:[self count]];
134 }
135
136 /* NSCoding */
137
138 - (void)encodeWithCoder:(NSCoder *)_coder {
139   [super encodeWithCoder:_coder];
140   
141   [_coder encodeObject:self->orgnam];
142   [_coder encodeObject:self->orgunits];
143 }
144 - (id)initWithCoder:(NSCoder *)_coder {
145   if ((self = [super initWithCoder:_coder]) != nil) {
146     self->orgnam   = [[_coder decodeObject] copy];
147     self->orgunits = [[_coder decodeObject] copy];
148   }
149   return self;
150 }
151
152 /* description */
153
154 - (void)appendAttributesToDescription:(NSMutableString *)_ms {
155   if (self->orgnam) [_ms appendFormat:@" %@", self->orgnam];
156   if ([self->orgunits count] > 0) {
157     [_ms appendFormat:@" units=%@",
158          [self->orgunits componentsJoinedByString:@","]];
159   }
160   [super appendAttributesToDescription:_ms];
161 }
162
163 @end /* NGVCardOrg */