2 Copyright (C) 2004 SKYRIX Software AG
4 This file is part of OpenGroupware.org.
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
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.
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
24 #include "UIxComponent.h"
25 #include <Foundation/Foundation.h>
26 #include <NGObjWeb/NGObjWeb.h>
27 #include <NGExtensions/NGExtensions.h>
30 @interface UIxComponent (PrivateAPI)
31 - (void)_parseQueryString:(NSString *)_s;
35 @implementation UIxComponent
37 static NSTimeZone *MET = nil;
38 static NSTimeZone *GMT = nil;
42 MET = [[NSTimeZone timeZoneWithAbbreviation:@"MET"] retain];
43 GMT = [[NSTimeZone timeZoneWithAbbreviation:@"GMT"] retain];
48 if ((self = [super init])) {
49 self->queryParameters = [[NSMutableDictionary alloc] init];
55 [self->queryParameters release];
67 req = [[self context] request];
69 r = [uri rangeOfString:@"?"];
73 qs = [uri substringFromIndex:(r.location + r.length)];
74 [self->queryParameters removeAllObjects];
75 [self _parseQueryString:qs];
79 - (void)_parseQueryString:(NSString *)_s {
83 e = [[_s componentsSeparatedByString:@"&"] objectEnumerator];
84 while ((part = [e nextObject])) {
86 NSString *key, *value;
88 r = [part rangeOfString:@"="];
90 /* missing value of query parameter */
91 key = [part stringByUnescapingURL];
95 key = [[part substringToIndex:r.location] stringByUnescapingURL];
96 value = [[part substringFromIndex:(r.location + r.length)]
97 stringByUnescapingURL];
99 [self->queryParameters setObject:value forKey:key];
103 - (NSString *)queryParameterForKey:(NSString *)_key {
104 return [self->queryParameters objectForKey:_key];
107 - (void)setQueryParameter:(NSString *)_param forKey:(NSString *)_key {
112 [self->queryParameters setObject:_param forKey:_key];
114 [self->queryParameters removeObjectForKey:_key];
117 - (NSDictionary *)queryParameters {
118 return self->queryParameters;
121 - (NSString *)completeHrefForMethod:(NSString *)_method {
125 qp = [self queryParameters];
129 qs = [[self context] queryStringFromDictionary:qp];
130 return [_method stringByAppendingFormat:@"?%@", qs];
133 - (NSString *)ownMethodName {
137 uri = [[[self context] request] uri];
139 /* first: cut off query parameters */
141 r = [uri rangeOfString:@"?" options:NSBackwardsSearch];
143 uri = [uri substringToIndex:r.location];
145 /* next: strip trailing slash */
147 if ([uri hasSuffix:@"/"]) uri = [uri substringToIndex:([uri length] - 1)];
148 r = [uri rangeOfString:@"/" options:NSBackwardsSearch];
150 /* then: cut of last path component */
152 if (r.length == 0) // no slash? are we at root?
155 return [uri substringFromIndex:(r.location + 1)];
160 - (NSTimeZone *)viewTimeZone {
161 // Note: also in the folder, should be based on a cookie?
165 - (NSTimeZone *)backendTimeZone {
169 - (NSCalendarDate *)selectedDate {
171 NSCalendarDate *cdate;
173 s = [self queryParameterForKey:@"day"];
174 cdate = ([s length] > 0)
175 ? [self dateForDateString:s]
176 : [NSCalendarDate date];
177 [cdate setTimeZone:[self viewTimeZone]];
181 - (NSString *)dateStringForDate:(NSCalendarDate *)_date {
182 [_date setTimeZone:[self viewTimeZone]];
183 return [_date descriptionWithCalendarFormat:@"%Y%m%d"];
186 - (NSCalendarDate *)dateForDateString:(NSString *)_dateString {
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 %@",
193 return [NSCalendarDate dateWithString:_dateString
194 calendarFormat:@"%Y%m%d %H:%M:%S %Z"];
200 - (NSString *)labelForKey:(NSString *)_str {
201 WOResourceManager *rm;
205 NSString *lKey, *lTable, *lVal;
208 if ([_str length] == 0) {
212 /* lookup languages */
214 ctx = [self context];
215 languages = [ctx hasSession]
216 ? [[ctx session] languages]
217 : [[ctx request] browserLanguages];
219 /* find resource manager */
221 if ((rm = [self resourceManager]) == nil)
222 rm = [[WOApplication application] resourceManager];
224 [self debugWithFormat:@"WARNING: missing resource manager!"];
228 r = [_str rangeOfString:@"/"];
230 lTable = [_str substringToIndex:r.location];
231 lKey = [_str substringFromIndex:(r.location + r.length)];
239 if([lKey hasPrefix:@"$"]) {
240 lKey = [self valueForKeyPath:[lKey substringFromIndex:1]];
242 if([lTable hasPrefix:@"$"]) {
243 lTable = [self valueForKeyPath:[lTable substringFromIndex:1]];
246 if([lVal hasPrefix:@"$"]) {
247 lVal = [self valueForKeyPath:[lVal substringFromIndex:1]];
252 label = [rm stringForKey:lKey inTableNamed:lTable withDefaultValue:lVal
253 languages:languages];
259 - (NSDictionary *)locale {
260 /* we need no fallback here, as locale is guaranteed to be set by sogod */
261 NSLog(@"%s locale:%@",
263 [[self context] valueForKey:@"locale"]);
264 return [[self context] valueForKey:@"locale"];
267 @end /* UIxComponent */