]> err.no Git - scalable-opengroupware.org/blob - SOGo/UI/SOGoUI/UIxComponent.m
minor additions
[scalable-opengroupware.org] / SOGo / UI / SOGoUI / UIxComponent.m
1 /*
2   Copyright (C) 2004 SKYRIX Software AG
3
4   This file is part of OpenGroupware.org.
5
6   OGo 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   OGo 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 OGo; 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 // $Id$
22
23
24 #include "UIxComponent.h"
25 #include <Foundation/Foundation.h>
26 #include <NGObjWeb/NGObjWeb.h>
27 #include <NGExtensions/NGExtensions.h>
28
29
30 @interface UIxComponent (PrivateAPI)
31 - (void)_parseQueryString:(NSString *)_s;
32 @end
33
34
35 @implementation UIxComponent
36
37 static NSTimeZone *MET = nil;
38 static NSTimeZone *GMT = nil;
39
40 + (void)initialize {
41   if (MET == nil) {
42     MET = [[NSTimeZone timeZoneWithAbbreviation:@"MET"] retain];
43     GMT = [[NSTimeZone timeZoneWithAbbreviation:@"GMT"] retain];
44   }
45 }
46
47 - (id)init {
48   if ((self = [super init])) {
49     self->queryParameters = [[NSMutableDictionary alloc] init];
50   }
51   return self;
52 }
53
54 - (void)dealloc {
55   [self->queryParameters release];
56   [super dealloc];
57 }
58
59
60 - (void)awake {
61   WORequest *req;
62   NSString  *uri;
63   NSRange   r;
64
65   [super awake];
66
67   req = [[self context] request];
68   uri = [req uri];
69   r   = [uri rangeOfString:@"?"];
70   if (r.length > 0) {
71     NSString *qs;
72     
73     qs = [uri substringFromIndex:(r.location + r.length)];
74     [self->queryParameters removeAllObjects];
75     [self _parseQueryString:qs];
76   }    
77 }
78
79 - (void)_parseQueryString:(NSString *)_s {
80   NSEnumerator *e;
81   NSString *part;
82     
83   e = [[_s componentsSeparatedByString:@"&"] objectEnumerator];
84   while ((part = [e nextObject])) {
85     NSRange  r;
86     NSString *key, *value;
87         
88     r = [part rangeOfString:@"="];
89     if (r.length == 0) {
90       /* missing value of query parameter */
91       key   = [part stringByUnescapingURL];
92       value = @"1";
93     }
94     else {
95       key   = [[part substringToIndex:r.location] stringByUnescapingURL];
96       value = [[part substringFromIndex:(r.location + r.length)] 
97                 stringByUnescapingURL];
98     }
99     [self->queryParameters setObject:value forKey:key];
100   }
101 }
102
103 - (NSString *)queryParameterForKey:(NSString *)_key {
104   return [self->queryParameters objectForKey:_key];
105 }
106
107 - (void)setQueryParameter:(NSString *)_param forKey:(NSString *)_key {
108   if(_key == nil)
109     return;
110
111   if(_param != nil)
112     [self->queryParameters setObject:_param forKey:_key];
113   else
114     [self->queryParameters removeObjectForKey:_key];
115 }
116
117 - (NSDictionary *)queryParameters {
118   return self->queryParameters;
119 }
120
121 - (NSString *)completeHrefForMethod:(NSString *)_method {
122   NSDictionary *qp;
123   NSString *qs;
124     
125   qp = [self queryParameters];
126   if([qp count] == 0)
127     return _method;
128     
129   qs = [[self context] queryStringFromDictionary:qp];
130   return [_method stringByAppendingFormat:@"?%@", qs];
131 }
132
133 - (NSString *)ownMethodName {
134   NSString *uri;
135   NSRange  r;
136     
137   uri = [[[self context] request] uri];
138     
139   /* first: cut off query parameters */
140     
141   r = [uri rangeOfString:@"?" options:NSBackwardsSearch];
142   if (r.length > 0)
143     uri = [uri substringToIndex:r.location];
144     
145   /* next: strip trailing slash */
146     
147   if ([uri hasSuffix:@"/"]) uri = [uri substringToIndex:([uri length] - 1)];
148   r = [uri rangeOfString:@"/" options:NSBackwardsSearch];
149     
150   /* then: cut of last path component */
151     
152   if (r.length == 0) // no slash? are we at root?
153     return @"/";
154     
155   return [uri substringFromIndex:(r.location + 1)];
156 }
157
158 /* date */
159
160 - (NSTimeZone *)viewTimeZone {
161   // Note: also in the folder, should be based on a cookie?
162   return MET;
163 }
164
165 - (NSTimeZone *)backendTimeZone {
166   return GMT;
167 }
168
169 - (NSCalendarDate *)selectedDate {
170   NSString       *s;
171   NSCalendarDate *cdate;
172   
173   s = [self queryParameterForKey:@"day"];
174   cdate = ([s length] > 0)
175     ? [self dateForDateString:s]
176     : [NSCalendarDate date];
177   [cdate setTimeZone:[self viewTimeZone]];
178   return cdate;
179 }
180
181 - (NSString *)dateStringForDate:(NSCalendarDate *)_date {
182   [_date setTimeZone:[self viewTimeZone]];
183   return [_date descriptionWithCalendarFormat:@"%Y%m%d"];
184 }
185
186 - (NSCalendarDate *)dateForDateString:(NSString *)_dateString {
187   NSTimeZone *tz;
188   
189   tz = [self viewTimeZone];
190   /* Note: we should give a time, best is noon to avoid edge conditions */
191   _dateString = [_dateString stringByAppendingFormat:@"12:00:00 %@",
192                                [tz abbreviation]];
193   return [NSCalendarDate dateWithString:_dateString 
194                          calendarFormat:@"%Y%m%d %H:%M:%S %Z"];
195 }
196
197 @end /* UIxComponent */