]> err.no Git - sope/blob - sope-ical/NGiCal/NGVCardValue.m
added class version checks
[sope] / sope-ical / NGiCal / NGVCardValue.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 "NGVCardValue.h"
23 #include <NGExtensions/NSString+misc.h>
24 #include "common.h"
25
26 @implementation NGVCardValue
27
28 + (int)version {
29   return [super version] + 0 /* v0 */;
30 }
31
32 - (id)initWithGroup:(NSString *)_group types:(NSArray *)_types
33   arguments:(NSDictionary *)_a
34 {
35   if ((self = [super init]) != nil) {
36     self->group     = [_group copy];
37     self->types     = [_types copy];
38     self->arguments = [_a copy];
39   }
40   return self;
41 }
42 - (id)init {
43   return [self initWithGroup:nil types:nil arguments:nil];
44 }
45
46 - (void)dealloc {
47   [self->group     release];
48   [self->types     release];
49   [self->arguments release];
50   [super dealloc];
51 }
52
53 /* accessors */
54
55 - (NSString *)group {
56   return self->group;
57 }
58 - (NSArray *)types {
59   return self->types;
60 }
61 - (NSDictionary *)arguments {
62   return self->arguments;
63 }
64 - (BOOL)isPreferred {
65   return [self->types containsObject:@"PREF"];
66 }
67
68 /* values */
69
70 - (NSString *)stringValue {
71   [self logWithFormat:@"ERROR(%s): subclasses should override this method!",
72         __PRETTY_FUNCTION__];
73   return nil;
74 }
75
76 - (id)propertyList {
77   return [self stringValue];
78 }
79
80 - (NSString *)xmlString {
81   return [[self stringValue] stringByEscapingXMLString];
82 }
83
84 - (NSString *)vCardString {
85   // TODO: apply proper escaping
86   return [self stringValue];
87 }
88
89 /* misc support methods */
90
91 - (void)appendXMLTag:(NSString *)_tag value:(NSString *)_val
92   to:(NSMutableString *)_ms
93 {
94   [_ms appendString:@"<"];
95   [_ms appendString:_tag];
96   [_ms appendString:@">"];
97   if ([_val isNotNull]) [_ms appendString:[_val stringByEscapingXMLString]];
98   [_ms appendString:@"</"];
99   [_ms appendString:_tag];
100   [_ms appendString:@">"];
101 }
102
103 - (void)appendVCardValue:(NSString *)_val to:(NSMutableString *)_ms {
104   // TODO: properly escape!
105   if ([_val isNotNull]) [_ms appendString:_val];
106 }
107
108 /* NSCopying */
109
110 - (id)copyWithZone:(NSZone *)_zone {
111   /* values are considered immutable */
112   return [self retain];
113 }
114
115 /* NSCoding */
116
117 - (void)encodeWithCoder:(NSCoder *)_coder {
118   [_coder encodeObject:self->group];
119   [_coder encodeObject:self->types];
120   [_coder encodeObject:self->arguments];
121 }
122 - (id)initWithCoder:(NSCoder *)_coder {
123   self->group     = [[_coder decodeObject] copy];
124   self->types     = [[_coder decodeObject] copy];
125   self->arguments = [[_coder decodeObject] copy];
126   return self;
127 }
128
129 /* description */
130
131 - (void)appendDictionary:(NSDictionary *)_d compactTo:(NSMutableString *)_s {
132   NSEnumerator *keys;
133   NSString *k;
134   
135   keys = [_d keyEnumerator];
136   while ((k = [keys nextObject]) != nil) {
137     NSString *v;
138     
139     v = [_d objectForKey:k];
140     [_s appendFormat:@"%@='%@';", k, v];
141   }
142 }
143
144 - (void)appendAttributesToDescription:(NSMutableString *)_ms {
145   if (self->group != nil) [_ms appendFormat:@" group='%@'", self->group];
146   if ([self->types count] > 0) {
147     [_ms appendFormat:@" types=%@", 
148            [self->types componentsJoinedByString:@","]];
149   }
150   if ([self->arguments count] > 0) {
151     [_ms appendString:@" args="];
152     [self appendDictionary:self->arguments compactTo:_ms];
153   }
154 }
155
156 - (NSString *)description {
157   NSMutableString *str = nil;
158   
159   str = [NSMutableString stringWithCapacity:64];
160   [str appendFormat:@"<0x%08X[%@]:", self, NSStringFromClass([self class])];
161   [self appendAttributesToDescription:str];
162   [str appendString:@">"];
163   return str;
164 }
165
166 @end /* NGVCardValue */