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 - (NSDictionary *)queryParametersBySettingSelectedDate:(NSCalendarDate *)_date {
180 NSMutableDictionary *qp;
182 qp = [[self queryParameters] mutableCopy];
183 [self setSelectedDateQueryParameter:_date inDictionary:qp];
184 return [qp autorelease];
187 - (void)setSelectedDateQueryParameter:(NSCalendarDate *)_newDate
188 inDictionary:(NSMutableDictionary *)_qp;
191 [_qp setObject:[self dateStringForDate:_newDate] forKey:@"day"];
193 [_qp removeObjectForKey:@"day"];
196 - (NSString *)completeHrefForMethod:(NSString *)_method {
200 qp = [self queryParameters];
204 qs = [[self context] queryStringFromDictionary:qp];
205 return [_method stringByAppendingFormat:@"?%@", qs];
208 - (NSString *)ownMethodName {
212 uri = [[[self context] request] uri];
214 /* first: cut off query parameters */
216 r = [uri rangeOfString:@"?" options:NSBackwardsSearch];
218 uri = [uri substringToIndex:r.location];
220 /* next: strip trailing slash */
222 if ([uri hasSuffix:@"/"]) uri = [uri substringToIndex:([uri length] - 1)];
223 r = [uri rangeOfString:@"/" options:NSBackwardsSearch];
225 /* then: cut of last path component */
227 if (r.length == 0) // no slash? are we at root?
230 return [uri substringFromIndex:(r.location + 1)];
235 - (NSTimeZone *)viewTimeZone {
236 // Note: also in the folder, should be based on a cookie?
240 - (NSTimeZone *)backendTimeZone {
244 - (NSCalendarDate *)selectedDate {
246 NSCalendarDate *cdate;
248 s = [self queryParameterForKey:@"day"];
249 cdate = ([s length] > 0)
250 ? [self dateForDateString:s]
251 : [NSCalendarDate date];
252 [cdate setTimeZone:[self viewTimeZone]];
253 s = [self queryParameterForKey:@"hm"];
254 if([s length] == 4) {
255 unsigned hour, minute;
257 hour = [[s substringToIndex:2] unsignedIntValue];
258 minute = [[s substringFromIndex:2] unsignedIntValue];
259 cdate = [cdate hour:hour minute:minute];
262 cdate = [cdate hour:12 minute:0];
267 - (NSString *)dateStringForDate:(NSCalendarDate *)_date {
268 [_date setTimeZone:[self viewTimeZone]];
269 return [_date descriptionWithCalendarFormat:@"%Y%m%d"];
272 - (NSCalendarDate *)dateForDateString:(NSString *)_dateString {
273 return [NSCalendarDate dateWithString:_dateString
274 calendarFormat:@"%Y%m%d"];
283 ctx = [self context];
284 return [[[self clientObject] authenticatorInContext:ctx]
288 - (NSString *)shortUserNameForDisplay {
289 // TODO: better use a SoUser formatter?
293 #warning !! USE USER MANAGER INSTEAD!
295 s = [[self user] login];
299 // TODO: algorithm might be inappropriate, depends on the actual UID
301 r = [s rangeOfString:@"."];
305 return [s substringToIndex:r.location];
310 - (NSString *)labelForKey:(NSString *)_str {
311 WOResourceManager *rm;
315 NSString *lKey, *lTable, *lVal;
318 if ([_str length] == 0) {
322 /* lookup languages */
324 ctx = [self context];
325 languages = [ctx hasSession]
326 ? [[ctx session] languages]
327 : [[ctx request] browserLanguages];
329 /* find resource manager */
331 if ((rm = [self resourceManager]) == nil)
332 rm = [[WOApplication application] resourceManager];
334 [self debugWithFormat:@"WARNING: missing resource manager!"];
338 r = [_str rangeOfString:@"/"];
340 lTable = [_str substringToIndex:r.location];
341 lKey = [_str substringFromIndex:(r.location + r.length)];
349 if([lKey hasPrefix:@"$"]) {
350 lKey = [self valueForKeyPath:[lKey substringFromIndex:1]];
352 if([lTable hasPrefix:@"$"]) {
353 lTable = [self valueForKeyPath:[lTable substringFromIndex:1]];
356 if([lVal hasPrefix:@"$"]) {
357 lVal = [self valueForKeyPath:[lVal substringFromIndex:1]];
362 label = [rm stringForKey:lKey inTableNamed:lTable withDefaultValue:lVal
363 languages:languages];
367 - (NSString *)localizedNameForDayOfWeek:(unsigned)_dayOfWeek {
368 NSString *key = [dayLabelKeys objectAtIndex:_dayOfWeek % 7];
369 return [self labelForKey:key];
372 - (NSString *)localizedAbbreviatedNameForDayOfWeek:(unsigned)_dayOfWeek {
373 NSString *key = [abbrDayLabelKeys objectAtIndex:_dayOfWeek % 7];
374 return [self labelForKey:key];
377 - (NSString *)localizedNameForMonthOfYear:(unsigned)_monthOfYear {
378 NSString *key = [monthLabelKeys objectAtIndex:(_monthOfYear - 1) % 12];
379 return [self labelForKey:key];
382 - (NSString *)localizedAbbreviatedNameForMonthOfYear:(unsigned)_monthOfYear {
383 NSString *key = [abbrMonthLabelKeys objectAtIndex:(_monthOfYear - 1) % 12];
384 return [self labelForKey:key];
389 - (NSDictionary *)locale {
390 /* we need no fallback here, as locale is guaranteed to be set by sogod */
391 return [[self context] valueForKey:@"locale"];
396 - (BOOL)isUIxDebugEnabled {
397 return uixDebugEnabled;
400 @end /* UIxComponent */