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