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;
45 static BOOL uixDebugEnabled = NO;
48 NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
50 uixDebugEnabled = [ud boolForKey:@"SOGoUIxDebugEnabled"];
53 MET = [[NSTimeZone timeZoneWithAbbreviation:@"MET"] retain];
54 GMT = [[NSTimeZone timeZoneWithAbbreviation:@"GMT"] retain];
56 if (dayLabelKeys == nil) {
57 dayLabelKeys = [[NSMutableArray alloc] initWithCapacity:7];
58 [dayLabelKeys addObject:@"Sunday"];
59 [dayLabelKeys addObject:@"Monday"];
60 [dayLabelKeys addObject:@"Tuesday"];
61 [dayLabelKeys addObject:@"Wednesday"];
62 [dayLabelKeys addObject:@"Thursday"];
63 [dayLabelKeys addObject:@"Friday"];
64 [dayLabelKeys addObject:@"Saturday"];
66 abbrDayLabelKeys = [[NSMutableArray alloc] initWithCapacity:7];
67 [abbrDayLabelKeys addObject:@"a2_Sunday"];
68 [abbrDayLabelKeys addObject:@"a2_Monday"];
69 [abbrDayLabelKeys addObject:@"a2_Tuesday"];
70 [abbrDayLabelKeys addObject:@"a2_Wednesday"];
71 [abbrDayLabelKeys addObject:@"a2_Thursday"];
72 [abbrDayLabelKeys addObject:@"a2_Friday"];
73 [abbrDayLabelKeys addObject:@"a2_Saturday"];
75 monthLabelKeys = [[NSMutableArray alloc] initWithCapacity:12];
76 [monthLabelKeys addObject:@"January"];
77 [monthLabelKeys addObject:@"February"];
78 [monthLabelKeys addObject:@"March"];
79 [monthLabelKeys addObject:@"April"];
80 [monthLabelKeys addObject:@"May"];
81 [monthLabelKeys addObject:@"June"];
82 [monthLabelKeys addObject:@"July"];
83 [monthLabelKeys addObject:@"August"];
84 [monthLabelKeys addObject:@"September"];
85 [monthLabelKeys addObject:@"October"];
86 [monthLabelKeys addObject:@"November"];
87 [monthLabelKeys addObject:@"December"];
89 abbrMonthLabelKeys = [[NSMutableArray alloc] initWithCapacity:12];
90 [abbrMonthLabelKeys addObject:@"a3_January"];
91 [abbrMonthLabelKeys addObject:@"a3_February"];
92 [abbrMonthLabelKeys addObject:@"a3_March"];
93 [abbrMonthLabelKeys addObject:@"a3_April"];
94 [abbrMonthLabelKeys addObject:@"a3_May"];
95 [abbrMonthLabelKeys addObject:@"a3_June"];
96 [abbrMonthLabelKeys addObject:@"a3_July"];
97 [abbrMonthLabelKeys addObject:@"a3_August"];
98 [abbrMonthLabelKeys addObject:@"a3_September"];
99 [abbrMonthLabelKeys addObject:@"a3_October"];
100 [abbrMonthLabelKeys addObject:@"a3_November"];
101 [abbrMonthLabelKeys addObject:@"a3_December"];
106 if ((self = [super init])) {
107 self->queryParameters = [[NSMutableDictionary alloc] init];
113 [self->queryParameters release];
125 req = [[self context] request];
127 r = [uri rangeOfString:@"?"];
131 qs = [uri substringFromIndex:(r.location + r.length)];
132 [self->queryParameters removeAllObjects];
133 [self _parseQueryString:qs];
137 - (void)_parseQueryString:(NSString *)_s {
141 e = [[_s componentsSeparatedByString:@"&"] objectEnumerator];
142 while ((part = [e nextObject])) {
144 NSString *key, *value;
146 r = [part rangeOfString:@"="];
148 /* missing value of query parameter */
149 key = [part stringByUnescapingURL];
153 key = [[part substringToIndex:r.location] stringByUnescapingURL];
154 value = [[part substringFromIndex:(r.location + r.length)]
155 stringByUnescapingURL];
157 [self->queryParameters setObject:value forKey:key];
161 - (NSString *)queryParameterForKey:(NSString *)_key {
162 return [self->queryParameters objectForKey:_key];
165 - (void)setQueryParameter:(NSString *)_param forKey:(NSString *)_key {
170 [self->queryParameters setObject:_param forKey:_key];
172 [self->queryParameters removeObjectForKey:_key];
175 - (NSDictionary *)queryParameters {
176 return self->queryParameters;
179 - (NSString *)completeHrefForMethod:(NSString *)_method {
183 qp = [self queryParameters];
187 qs = [[self context] queryStringFromDictionary:qp];
188 return [_method stringByAppendingFormat:@"?%@", qs];
191 - (NSString *)ownMethodName {
195 uri = [[[self context] request] uri];
197 /* first: cut off query parameters */
199 r = [uri rangeOfString:@"?" options:NSBackwardsSearch];
201 uri = [uri substringToIndex:r.location];
203 /* next: strip trailing slash */
205 if ([uri hasSuffix:@"/"]) uri = [uri substringToIndex:([uri length] - 1)];
206 r = [uri rangeOfString:@"/" options:NSBackwardsSearch];
208 /* then: cut of last path component */
210 if (r.length == 0) // no slash? are we at root?
213 return [uri substringFromIndex:(r.location + 1)];
218 - (NSTimeZone *)viewTimeZone {
219 // Note: also in the folder, should be based on a cookie?
223 - (NSTimeZone *)backendTimeZone {
227 - (NSCalendarDate *)selectedDate {
229 NSCalendarDate *cdate;
231 s = [self queryParameterForKey:@"day"];
232 cdate = ([s length] > 0)
233 ? [self dateForDateString:s]
234 : [NSCalendarDate date];
235 [cdate setTimeZone:[self viewTimeZone]];
236 s = [self queryParameterForKey:@"hm"];
237 if([s length] == 4) {
238 unsigned hour, minute;
240 hour = [[s substringToIndex:2] unsignedIntValue];
241 minute = [[s substringFromIndex:2] unsignedIntValue];
242 cdate = [cdate hour:hour minute:minute];
245 cdate = [cdate hour:12 minute:0];
250 - (NSString *)dateStringForDate:(NSCalendarDate *)_date {
251 [_date setTimeZone:[self viewTimeZone]];
252 return [_date descriptionWithCalendarFormat:@"%Y%m%d"];
255 - (NSCalendarDate *)dateForDateString:(NSString *)_dateString {
256 return [NSCalendarDate dateWithString:_dateString
257 calendarFormat:@"%Y%m%d"];
266 ctx = [self context];
267 return [[[self clientObject] authenticatorInContext:ctx]
271 - (NSString *)shortUserNameForDisplay {
272 // TODO: better use a SoUser formatter?
276 #warning !! USE USER MANAGER INSTEAD!
278 s = [[self user] login];
282 // TODO: algorithm might be inappropriate, depends on the actual UID
284 r = [s rangeOfString:@"."];
288 return [s substringToIndex:r.location];
293 - (NSString *)labelForKey:(NSString *)_str {
294 WOResourceManager *rm;
298 NSString *lKey, *lTable, *lVal;
301 if ([_str length] == 0) {
305 /* lookup languages */
307 ctx = [self context];
308 languages = [ctx hasSession]
309 ? [[ctx session] languages]
310 : [[ctx request] browserLanguages];
312 /* find resource manager */
314 if ((rm = [self resourceManager]) == nil)
315 rm = [[WOApplication application] resourceManager];
317 [self debugWithFormat:@"WARNING: missing resource manager!"];
321 r = [_str rangeOfString:@"/"];
323 lTable = [_str substringToIndex:r.location];
324 lKey = [_str substringFromIndex:(r.location + r.length)];
332 if([lKey hasPrefix:@"$"]) {
333 lKey = [self valueForKeyPath:[lKey substringFromIndex:1]];
335 if([lTable hasPrefix:@"$"]) {
336 lTable = [self valueForKeyPath:[lTable substringFromIndex:1]];
339 if([lVal hasPrefix:@"$"]) {
340 lVal = [self valueForKeyPath:[lVal substringFromIndex:1]];
345 label = [rm stringForKey:lKey inTableNamed:lTable withDefaultValue:lVal
346 languages:languages];
350 - (NSString *)localizedNameForDayOfWeek:(unsigned)_dayOfWeek {
351 NSString *key = [dayLabelKeys objectAtIndex:_dayOfWeek % 7];
352 return [self labelForKey:key];
355 - (NSString *)localizedAbbreviatedNameForDayOfWeek:(unsigned)_dayOfWeek {
356 NSString *key = [abbrDayLabelKeys objectAtIndex:_dayOfWeek % 7];
357 return [self labelForKey:key];
360 - (NSString *)localizedNameForMonthOfYear:(unsigned)_monthOfYear {
361 NSString *key = [monthLabelKeys objectAtIndex:(_monthOfYear - 1) % 12];
362 return [self labelForKey:key];
365 - (NSString *)localizedAbbreviatedNameForMonthOfYear:(unsigned)_monthOfYear {
366 NSString *key = [abbrMonthLabelKeys objectAtIndex:(_monthOfYear - 1) % 12];
367 return [self labelForKey:key];
372 - (NSDictionary *)locale {
373 /* we need no fallback here, as locale is guaranteed to be set by sogod */
374 return [[self context] valueForKey:@"locale"];
379 - (BOOL)isUIxDebugEnabled {
380 return uixDebugEnabled;
383 @end /* UIxComponent */