/
IcalElements.m
209 lines (169 loc) · 5.45 KB
/
IcalElements.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
Copyright (C) 2000-2005 SKYRIX Software AG
This file is part of SOPE.
SOPE is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.
SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public
License along with SOPE; see the file COPYING. If not, write to the
Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.
*/
@interface IcalComponent : WODynamicElement
{
WOAssociation *cname;
WOElement *template;
}
@end
@interface IcalProperty : WODynamicElement
{
WOAssociation *pname;
WOElement *template;
NSDictionary *parameters;
WOAssociation *value;
WOAssociation *valueType;
}
@end
static inline NSDictionary *ExtractParameters(NSDictionary *_set) {
/* extracts ? parameters */
NSMutableDictionary *paras = nil;
NSMutableArray *paraKeys = nil;
NSEnumerator *keys;
NSString *key;
// locate query parameters
keys = [_set keyEnumerator];
while ((key = [keys nextObject])) {
if ([key hasPrefix:@"?"]) {
WOAssociation *value;
if ([key isEqualToString:@"?wosid"])
continue;
value = [_set objectForKey:key];
if (paraKeys == nil)
paraKeys = [NSMutableArray arrayWithCapacity:8];
if (paras == nil)
paras = [NSMutableDictionary dictionaryWithCapacity:8];
[paraKeys addObject:key];
[paras setObject:value forKey:[key substringFromIndex:1]];
}
}
// remove query parameters
if (paraKeys) {
unsigned cnt, count;
for (cnt = 0, count = [paraKeys count]; cnt < count; cnt++) {
[(NSMutableDictionary *)_set removeObjectForKey:
[paraKeys objectAtIndex:cnt]];
}
}
// assign parameters
return [paras copy];
}
static inline id GetProperty(NSDictionary *_set, NSString *_name) {
id propValue = [_set objectForKey:_name];
if (propValue) {
propValue = RETAIN(propValue);
[(id)_set removeObjectForKey:_name];
}
return propValue;
}
@implementation IcalComponent
- (id)initWithName:(NSString *)_name
associations:(NSDictionary *)_config
template:(WOElement *)_t
{
if ((self = [super initWithName:_name associations:_config template:_t])) {
self->cname = GetProperty(_config, @"name");
self->template = RETAIN(_t);
}
return self;
}
- (void)dealloc {
RELEASE(self->template);
RELEASE(self->cname);
[super dealloc];
}
- (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
NSString *n;
n = [self->cname stringValueInComponent:[_ctx component]];
[_response appendContentString:@"BEGIN:"];
[_response appendContentString:n];
[self->template appendToResponse:_response inContext:_ctx];
[_response appendContentString:@"END:"];
[_response appendContentString:n];
}
@end /* IcalComponent */
@implementation IcalProperty
- (id)initWithName:(NSString *)_name
associations:(NSDictionary *)_config
template:(WOElement *)_t
{
if ((self = [super initWithName:_name associations:_config template:_t])) {
self->pname = GetProperty(_config, @"name");
self->value = GetProperty(_config, @"value");
self->valueType = GetProperty(_config, @"valueType");
self->template = RETAIN(_t);
self->parameters = ExtractParameters(_config);
}
return self;
}
- (void)dealloc {
RELEASE(self->value);
RELEASE(self->valueType);
RELEASE(self->parameters);
RELEASE(self->template);
RELEASE(self->pname);
[super dealloc];
}
- (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
WOComponent *sComponent;
NSString *n;
NSEnumerator *keys;
NSString *key;
id val;
NSString *valType;
sComponent = [_ctx component];
n = [self->pname stringValueInComponent:sComponent];
val = [self->value valueInComponent:sComponent];
valType = [self->valueType stringValueInComponent:sComponent];
/* add name */
[_response appendContentString:n];
/* add parameters */
keys = [self->parameters keyEnumerator];
while ((key = [keys nextObject])) {
WOAssociation *val;
NSString *s;
val = [self->parameters objectForKey:key];
s = [val stringValueInComponent:sComponent];
if ([s length] > 0) {
[_response appendContentString:@";"];
[_response appendContentString:key];
[_response appendContentString:@"="];
[_response appendContentString:s];
}
}
/* add value */
[_response appendContentString:@":"];
if ([valType length] == 0) {
val = [val stringValue];
}
else if ([valType isEqualToString:@"datetime"]) {
static NSString *calfmt = @"%Y%m%dT%H%M00Z";
if ([val respondsToSelector:@selector(descriptionWithCalendarFormat:)]) {
static NSTimeZone *gmt = nil;
if (gmt == nil) gmt = [NSTimeZone timeZoneForSecondsFromGMT: 0];
[val setTimeZone:gmt];
val = [val descriptionWithCalendarFormat:calfmt];
}
else
val = [val stringValue];
}
else
val = [val stringValue];
[_response appendContentString:val];
[self->template appendToResponse:_response inContext:_ctx];
}
@end /* IcalProperty */