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;
40 static NSMutableArray *dayLabelKeys = nil;
41 static NSMutableArray *abbrDayLabelKeys = nil;
42 static NSMutableArray *monthLabelKeys = nil;
43 static NSMutableArray *abbrMonthLabelKeys = nil;
47 MET = [[NSTimeZone timeZoneWithAbbreviation:@"MET"] retain];
48 GMT = [[NSTimeZone timeZoneWithAbbreviation:@"GMT"] retain];
50 if (dayLabelKeys == nil) {
51 dayLabelKeys = [[NSMutableArray alloc] initWithCapacity:7];
52 [dayLabelKeys addObject:@"Sunday"];
53 [dayLabelKeys addObject:@"Monday"];
54 [dayLabelKeys addObject:@"Tuesday"];
55 [dayLabelKeys addObject:@"Wednesday"];
56 [dayLabelKeys addObject:@"Thursday"];
57 [dayLabelKeys addObject:@"Friday"];
58 [dayLabelKeys addObject:@"Saturday"];
60 abbrDayLabelKeys = [[NSMutableArray alloc] initWithCapacity:7];
61 [abbrDayLabelKeys addObject:@"Sun"];
62 [abbrDayLabelKeys addObject:@"Mon"];
63 [abbrDayLabelKeys addObject:@"Tue"];
64 [abbrDayLabelKeys addObject:@"Wed"];
65 [abbrDayLabelKeys addObject:@"Thu"];
66 [abbrDayLabelKeys addObject:@"Fri"];
67 [abbrDayLabelKeys addObject:@"Sat"];
69 monthLabelKeys = [[NSMutableArray alloc] initWithCapacity:12];
70 [monthLabelKeys addObject:@"January"];
71 [monthLabelKeys addObject:@"February"];
72 [monthLabelKeys addObject:@"March"];
73 [monthLabelKeys addObject:@"April"];
74 [monthLabelKeys addObject:@"May"];
75 [monthLabelKeys addObject:@"June"];
76 [monthLabelKeys addObject:@"July"];
77 [monthLabelKeys addObject:@"August"];
78 [monthLabelKeys addObject:@"September"];
79 [monthLabelKeys addObject:@"October"];
80 [monthLabelKeys addObject:@"November"];
81 [monthLabelKeys addObject:@"December"];
83 abbrMonthLabelKeys = [[NSMutableArray alloc] initWithCapacity:12];
84 [abbrMonthLabelKeys addObject:@"Jan"];
85 [abbrMonthLabelKeys addObject:@"Feb"];
86 [abbrMonthLabelKeys addObject:@"Mar"];
87 [abbrMonthLabelKeys addObject:@"Apr"];
88 [abbrMonthLabelKeys addObject:@"May"];
89 [abbrMonthLabelKeys addObject:@"Jun"];
90 [abbrMonthLabelKeys addObject:@"Jul"];
91 [abbrMonthLabelKeys addObject:@"Aug"];
92 [abbrMonthLabelKeys addObject:@"Sep"];
93 [abbrMonthLabelKeys addObject:@"Oct"];
94 [abbrMonthLabelKeys addObject:@"Nov"];
95 [abbrMonthLabelKeys addObject:@"Dec"];
100 if ((self = [super init])) {
101 self->queryParameters = [[NSMutableDictionary alloc] init];
107 [self->queryParameters release];
119 req = [[self context] request];
121 r = [uri rangeOfString:@"?"];
125 qs = [uri substringFromIndex:(r.location + r.length)];
126 [self->queryParameters removeAllObjects];
127 [self _parseQueryString:qs];
131 - (void)_parseQueryString:(NSString *)_s {
135 e = [[_s componentsSeparatedByString:@"&"] objectEnumerator];
136 while ((part = [e nextObject])) {
138 NSString *key, *value;
140 r = [part rangeOfString:@"="];
142 /* missing value of query parameter */
143 key = [part stringByUnescapingURL];
147 key = [[part substringToIndex:r.location] stringByUnescapingURL];
148 value = [[part substringFromIndex:(r.location + r.length)]
149 stringByUnescapingURL];
151 [self->queryParameters setObject:value forKey:key];
155 - (NSString *)queryParameterForKey:(NSString *)_key {
156 return [self->queryParameters objectForKey:_key];
159 - (void)setQueryParameter:(NSString *)_param forKey:(NSString *)_key {
164 [self->queryParameters setObject:_param forKey:_key];
166 [self->queryParameters removeObjectForKey:_key];
169 - (NSDictionary *)queryParameters {
170 return self->queryParameters;
173 - (NSString *)completeHrefForMethod:(NSString *)_method {
177 qp = [self queryParameters];
181 qs = [[self context] queryStringFromDictionary:qp];
182 return [_method stringByAppendingFormat:@"?%@", qs];
185 - (NSString *)ownMethodName {
189 uri = [[[self context] request] uri];
191 /* first: cut off query parameters */
193 r = [uri rangeOfString:@"?" options:NSBackwardsSearch];
195 uri = [uri substringToIndex:r.location];
197 /* next: strip trailing slash */
199 if ([uri hasSuffix:@"/"]) uri = [uri substringToIndex:([uri length] - 1)];
200 r = [uri rangeOfString:@"/" options:NSBackwardsSearch];
202 /* then: cut of last path component */
204 if (r.length == 0) // no slash? are we at root?
207 return [uri substringFromIndex:(r.location + 1)];
212 - (NSTimeZone *)viewTimeZone {
213 // Note: also in the folder, should be based on a cookie?
217 - (NSTimeZone *)backendTimeZone {
221 - (NSCalendarDate *)selectedDate {
223 NSCalendarDate *cdate;
225 s = [self queryParameterForKey:@"day"];
226 cdate = ([s length] > 0)
227 ? [self dateForDateString:s]
228 : [NSCalendarDate date];
229 [cdate setTimeZone:[self viewTimeZone]];
233 - (NSString *)dateStringForDate:(NSCalendarDate *)_date {
234 [_date setTimeZone:[self viewTimeZone]];
235 return [_date descriptionWithCalendarFormat:@"%Y%m%d"];
238 - (NSCalendarDate *)dateForDateString:(NSString *)_dateString {
241 tz = [self viewTimeZone];
242 /* Note: we should give a time, best is noon to avoid edge conditions */
243 _dateString = [_dateString stringByAppendingFormat:@"12:00:00 %@",
245 return [NSCalendarDate dateWithString:_dateString
246 calendarFormat:@"%Y%m%d %H:%M:%S %Z"];
252 - (NSString *)labelForKey:(NSString *)_str {
253 WOResourceManager *rm;
257 NSString *lKey, *lTable, *lVal;
260 if ([_str length] == 0) {
264 /* lookup languages */
266 ctx = [self context];
267 languages = [ctx hasSession]
268 ? [[ctx session] languages]
269 : [[ctx request] browserLanguages];
271 /* find resource manager */
273 if ((rm = [self resourceManager]) == nil)
274 rm = [[WOApplication application] resourceManager];
276 [self debugWithFormat:@"WARNING: missing resource manager!"];
280 r = [_str rangeOfString:@"/"];
282 lTable = [_str substringToIndex:r.location];
283 lKey = [_str substringFromIndex:(r.location + r.length)];
291 if([lKey hasPrefix:@"$"]) {
292 lKey = [self valueForKeyPath:[lKey substringFromIndex:1]];
294 if([lTable hasPrefix:@"$"]) {
295 lTable = [self valueForKeyPath:[lTable substringFromIndex:1]];
298 if([lVal hasPrefix:@"$"]) {
299 lVal = [self valueForKeyPath:[lVal substringFromIndex:1]];
304 label = [rm stringForKey:lKey inTableNamed:lTable withDefaultValue:lVal
305 languages:languages];
309 - (NSString *)localizedNameForDayOfWeek:(unsigned)_dayOfWeek {
310 NSString *key = [dayLabelKeys objectAtIndex:_dayOfWeek % 7];
311 return [self labelForKey:key];
314 - (NSString *)localizedAbbreviatedNameForDayOfWeek:(unsigned)_dayOfWeek {
315 NSString *key = [abbrDayLabelKeys objectAtIndex:_dayOfWeek % 7];
316 return [self labelForKey:key];
319 - (NSString *)localizedNameForMonthOfYear:(unsigned)_monthOfYear {
320 NSString *key = [monthLabelKeys objectAtIndex:(_monthOfYear - 1) % 12];
321 return [self labelForKey:key];
324 - (NSString *)localizedAbbreviatedNameForMonthOfYear:(unsigned)_monthOfYear {
325 NSString *key = [abbrMonthLabelKeys objectAtIndex:(_monthOfYear - 1) % 12];
326 return [self labelForKey:key];
331 - (NSDictionary *)locale {
332 /* we need no fallback here, as locale is guaranteed to be set by sogod */
333 return [[self context] valueForKey:@"locale"];
336 @end /* UIxComponent */