]> err.no Git - scalable-opengroupware.org/blob - SOPE/NGCards/IcalElements.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1013 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / SOPE / NGCards / IcalElements.m
1 /*
2   Copyright (C) 2000-2005 SKYRIX Software AG
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 <NGObjWeb/WODynamicElement.h>
23
24 @interface IcalComponent : WODynamicElement
25 {
26   WOAssociation *cname;
27   WOElement     *template;
28 }
29 @end
30
31 @interface IcalProperty : WODynamicElement
32 {
33   WOAssociation *pname;
34   WOElement     *template;
35   NSDictionary  *parameters;
36   WOAssociation *value;
37   WOAssociation *valueType;
38 }
39 @end
40
41 #include "common.h"
42
43 static inline NSDictionary *ExtractParameters(NSDictionary *_set) {
44   /* extracts ? parameters */
45   NSMutableDictionary *paras = nil;
46   NSMutableArray      *paraKeys = nil;
47   NSEnumerator        *keys;
48   NSString            *key;
49   
50   // locate query parameters
51   keys = [_set keyEnumerator];
52   while ((key = [keys nextObject])) {
53     if ([key hasPrefix:@"?"]) {
54       WOAssociation *value;
55
56       if ([key isEqualToString:@"?wosid"])
57         continue;
58
59       value = [_set objectForKey:key];
60           
61       if (paraKeys == nil)
62         paraKeys = [NSMutableArray arrayWithCapacity:8];
63       if (paras == nil)
64         paras = [NSMutableDictionary dictionaryWithCapacity:8];
65           
66       [paraKeys addObject:key];
67       [paras setObject:value forKey:[key substringFromIndex:1]];
68     }
69   }
70
71   // remove query parameters
72   if (paraKeys) {
73     unsigned cnt, count;
74     for (cnt = 0, count = [paraKeys count]; cnt < count; cnt++) {
75       [(NSMutableDictionary *)_set removeObjectForKey:
76                                      [paraKeys objectAtIndex:cnt]];
77     }
78   }
79
80   // assign parameters
81   return [paras copy];
82 }
83
84 static inline id GetProperty(NSDictionary *_set, NSString *_name) {
85   id propValue = [_set objectForKey:_name];
86
87   if (propValue) {
88     propValue = RETAIN(propValue);
89     [(id)_set removeObjectForKey:_name];
90   }
91   return propValue;
92 }
93
94 @implementation IcalComponent
95
96 - (id)initWithName:(NSString *)_name
97   associations:(NSDictionary *)_config
98   template:(WOElement *)_t
99 {
100   if ((self = [super initWithName:_name associations:_config template:_t])) {
101     self->cname = GetProperty(_config, @"name");
102     self->template = RETAIN(_t);
103   }
104   return self;
105 }
106
107 - (void)dealloc {
108   RELEASE(self->template);
109   RELEASE(self->cname);
110   [super dealloc];
111 }
112
113 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
114   NSString *n;
115   
116   n = [self->cname stringValueInComponent:[_ctx component]];
117   
118   [_response appendContentString:@"BEGIN:"];
119   [_response appendContentString:n];
120   [self->template appendToResponse:_response inContext:_ctx];
121   [_response appendContentString:@"END:"];
122   [_response appendContentString:n];
123 }
124
125 @end /* IcalComponent */
126
127 @implementation IcalProperty
128
129 - (id)initWithName:(NSString *)_name
130   associations:(NSDictionary *)_config
131   template:(WOElement *)_t
132 {
133   if ((self = [super initWithName:_name associations:_config template:_t])) {
134     self->pname      = GetProperty(_config, @"name");
135     self->value      = GetProperty(_config, @"value");
136     self->valueType  = GetProperty(_config, @"valueType");
137     self->template   = RETAIN(_t);
138     self->parameters = ExtractParameters(_config);
139   }
140   return self;
141 }
142
143 - (void)dealloc {
144   RELEASE(self->value);
145   RELEASE(self->valueType);
146   RELEASE(self->parameters);
147   RELEASE(self->template);
148   RELEASE(self->pname);
149   [super dealloc];
150 }
151
152 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
153   WOComponent  *sComponent;
154   NSString     *n;
155   NSEnumerator *keys;
156   NSString     *key;
157   id           val;
158   NSString     *valType;
159
160   sComponent = [_ctx component];
161   n       = [self->pname     stringValueInComponent:sComponent];
162   val     = [self->value     valueInComponent:sComponent];
163   valType = [self->valueType stringValueInComponent:sComponent];
164
165   /* add name */
166   [_response appendContentString:n];
167
168   /* add parameters */
169   keys = [self->parameters keyEnumerator];
170   while ((key = [keys nextObject])) {
171     WOAssociation *val;
172     NSString *s;
173     
174     val = [self->parameters objectForKey:key];
175     s   = [val stringValueInComponent:sComponent];
176     
177     if ([s length] > 0) {
178       [_response appendContentString:@";"];
179       [_response appendContentString:key];
180       [_response appendContentString:@"="];
181       [_response appendContentString:s];
182     }
183   }
184   
185   /* add value */
186   [_response appendContentString:@":"];
187
188   if ([valType length] == 0) {
189     val = [val stringValue];
190   }
191   else if ([valType isEqualToString:@"datetime"]) {
192     static NSString *calfmt = @"%Y%m%dT%H%M00Z";
193     
194     if ([val respondsToSelector:@selector(descriptionWithCalendarFormat:)]) {
195       static NSTimeZone *gmt = nil;
196       if (gmt == nil) gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
197       [val setTimeZone:gmt];
198       val = [val descriptionWithCalendarFormat:calfmt];
199     }
200     else
201       val = [val stringValue];
202   }
203   else
204     val = [val stringValue];
205   
206   [_response appendContentString:val];
207   [self->template appendToResponse:_response inContext:_ctx];
208 }
209
210 @end /* IcalProperty */