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