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
23 #include "UIxComponent.h"
25 #include <NGObjWeb/SoHTTPAuthenticator.h>
27 @interface UIxComponent (PrivateAPI)
28 - (void)_parseQueryString:(NSString *)_s;
31 @implementation UIxComponent
33 static NSTimeZone *MET = nil;
34 static NSTimeZone *GMT = nil;
36 static NSMutableArray *dayLabelKeys = nil;
37 static NSMutableArray *abbrDayLabelKeys = nil;
38 static NSMutableArray *monthLabelKeys = nil;
39 static NSMutableArray *abbrMonthLabelKeys = nil;
41 static BOOL uixDebugEnabled = NO;
44 NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
46 uixDebugEnabled = [ud boolForKey:@"SOGoUIxDebugEnabled"];
49 MET = [[NSTimeZone timeZoneWithAbbreviation:@"MET"] retain];
50 GMT = [[NSTimeZone timeZoneWithAbbreviation:@"GMT"] retain];
52 if (dayLabelKeys == nil) {
53 dayLabelKeys = [[NSMutableArray alloc] initWithCapacity:7];
54 [dayLabelKeys addObject:@"Sunday"];
55 [dayLabelKeys addObject:@"Monday"];
56 [dayLabelKeys addObject:@"Tuesday"];
57 [dayLabelKeys addObject:@"Wednesday"];
58 [dayLabelKeys addObject:@"Thursday"];
59 [dayLabelKeys addObject:@"Friday"];
60 [dayLabelKeys addObject:@"Saturday"];
62 abbrDayLabelKeys = [[NSMutableArray alloc] initWithCapacity:7];
63 [abbrDayLabelKeys addObject:@"a2_Sunday"];
64 [abbrDayLabelKeys addObject:@"a2_Monday"];
65 [abbrDayLabelKeys addObject:@"a2_Tuesday"];
66 [abbrDayLabelKeys addObject:@"a2_Wednesday"];
67 [abbrDayLabelKeys addObject:@"a2_Thursday"];
68 [abbrDayLabelKeys addObject:@"a2_Friday"];
69 [abbrDayLabelKeys addObject:@"a2_Saturday"];
71 monthLabelKeys = [[NSMutableArray alloc] initWithCapacity:12];
72 [monthLabelKeys addObject:@"January"];
73 [monthLabelKeys addObject:@"February"];
74 [monthLabelKeys addObject:@"March"];
75 [monthLabelKeys addObject:@"April"];
76 [monthLabelKeys addObject:@"May"];
77 [monthLabelKeys addObject:@"June"];
78 [monthLabelKeys addObject:@"July"];
79 [monthLabelKeys addObject:@"August"];
80 [monthLabelKeys addObject:@"September"];
81 [monthLabelKeys addObject:@"October"];
82 [monthLabelKeys addObject:@"November"];
83 [monthLabelKeys addObject:@"December"];
85 abbrMonthLabelKeys = [[NSMutableArray alloc] initWithCapacity:12];
86 [abbrMonthLabelKeys addObject:@"a3_January"];
87 [abbrMonthLabelKeys addObject:@"a3_February"];
88 [abbrMonthLabelKeys addObject:@"a3_March"];
89 [abbrMonthLabelKeys addObject:@"a3_April"];
90 [abbrMonthLabelKeys addObject:@"a3_May"];
91 [abbrMonthLabelKeys addObject:@"a3_June"];
92 [abbrMonthLabelKeys addObject:@"a3_July"];
93 [abbrMonthLabelKeys addObject:@"a3_August"];
94 [abbrMonthLabelKeys addObject:@"a3_September"];
95 [abbrMonthLabelKeys addObject:@"a3_October"];
96 [abbrMonthLabelKeys addObject:@"a3_November"];
97 [abbrMonthLabelKeys addObject:@"a3_December"];
102 if ((self = [super init])) {
103 self->queryParameters = [[NSMutableDictionary alloc] initWithCapacity:8];
109 [self->queryParameters release];
122 req = [[self context] request];
124 r = [uri rangeOfString:@"?"];
128 qs = [uri substringFromIndex:(r.location + r.length)];
129 [self->queryParameters removeAllObjects];
130 [self _parseQueryString:qs];
134 /* query parameters */
136 - (void)_parseQueryString:(NSString *)_s {
140 e = [[_s componentsSeparatedByString:@"&"] objectEnumerator];
141 while ((part = [e nextObject])) {
143 NSString *key, *value;
145 r = [part rangeOfString:@"="];
147 /* missing value of query parameter */
148 key = [part stringByUnescapingURL];
152 key = [[part substringToIndex:r.location] stringByUnescapingURL];
153 value = [[part substringFromIndex:(r.location + r.length)]
154 stringByUnescapingURL];
156 [self->queryParameters setObject:value forKey:key];
160 - (NSString *)queryParameterForKey:(NSString *)_key {
161 return [self->queryParameters objectForKey:_key];
164 - (void)setQueryParameter:(NSString *)_param forKey:(NSString *)_key {
169 [self->queryParameters setObject:_param forKey:_key];
171 [self->queryParameters removeObjectForKey:_key];
174 - (NSDictionary *)queryParameters {
175 return self->queryParameters;
178 - (NSDictionary *)queryParametersBySettingSelectedDate:(NSCalendarDate *)_date{
179 NSMutableDictionary *qp;
181 qp = [[self queryParameters] mutableCopy];
182 [self setSelectedDateQueryParameter:_date inDictionary:qp];
183 return [qp autorelease];
186 - (void)setSelectedDateQueryParameter:(NSCalendarDate *)_newDate
187 inDictionary:(NSMutableDictionary *)_qp;
190 [_qp setObject:[self dateStringForDate:_newDate] forKey:@"day"];
192 [_qp removeObjectForKey:@"day"];
195 - (NSString *)completeHrefForMethod:(NSString *)_method {
199 qp = [self queryParameters];
203 qs = [[self context] queryStringFromDictionary:qp];
204 return [_method stringByAppendingFormat:@"?%@", qs];
207 - (NSString *)ownMethodName {
211 uri = [[[self context] request] uri];
213 /* first: cut off query parameters */
215 r = [uri rangeOfString:@"?" options:NSBackwardsSearch];
217 uri = [uri substringToIndex:r.location];
219 /* next: strip trailing slash */
221 if ([uri hasSuffix:@"/"]) uri = [uri substringToIndex:([uri length] - 1)];
222 r = [uri rangeOfString:@"/" options:NSBackwardsSearch];
224 /* then: cut of last path component */
226 if (r.length == 0) // no slash? are we at root?
229 return [uri substringFromIndex:(r.location + 1)];
234 - (NSTimeZone *)viewTimeZone {
235 // Note: also in the folder, should be based on a cookie?
239 - (NSTimeZone *)backendTimeZone {
243 - (NSCalendarDate *)selectedDate {
245 NSCalendarDate *cdate;
247 s = [self queryParameterForKey:@"day"];
248 cdate = ([s length] > 0)
249 ? [self dateForDateString:s]
250 : [NSCalendarDate date];
251 [cdate setTimeZone:[self viewTimeZone]];
252 s = [self queryParameterForKey:@"hm"];
253 if([s length] == 4) {
254 unsigned hour, minute;
256 hour = [[s substringToIndex:2] unsignedIntValue];
257 minute = [[s substringFromIndex:2] unsignedIntValue];
258 cdate = [cdate hour:hour minute:minute];
261 cdate = [cdate hour:12 minute:0];
266 - (NSString *)dateStringForDate:(NSCalendarDate *)_date {
267 [_date setTimeZone:[self viewTimeZone]];
268 return [_date descriptionWithCalendarFormat:@"%Y%m%d"];
271 - (NSCalendarDate *)dateForDateString:(NSString *)_dateString {
272 return [NSCalendarDate dateWithString:_dateString
273 calendarFormat:@"%Y%m%d"];
282 ctx = [self context];
283 return [[[self clientObject] authenticatorInContext:ctx] userInContext:ctx];
286 - (NSString *)shortUserNameForDisplay {
287 // TODO: better use a SoUser formatter?
291 #warning !! USE USER MANAGER INSTEAD!
293 s = [[self user] login];
297 // TODO: algorithm might be inappropriate, depends on the actual UID
299 r = [s rangeOfString:@"."];
303 return [s substringToIndex:r.location];
308 - (NSString *)labelForKey:(NSString *)_str {
309 WOResourceManager *rm;
313 NSString *lKey, *lTable, *lVal;
316 if ([_str length] == 0) {
320 /* lookup languages */
322 ctx = [self context];
323 languages = [ctx hasSession]
324 ? [[ctx session] languages]
325 : [[ctx request] browserLanguages];
327 /* find resource manager */
329 if ((rm = [self resourceManager]) == nil)
330 rm = [[WOApplication application] resourceManager];
332 [self debugWithFormat:@"WARNING: missing resource manager!"];
336 r = [_str rangeOfString:@"/"];
338 lTable = [_str substringToIndex:r.location];
339 lKey = [_str substringFromIndex:(r.location + r.length)];
347 if([lKey hasPrefix:@"$"]) {
348 lKey = [self valueForKeyPath:[lKey substringFromIndex:1]];
350 if([lTable hasPrefix:@"$"]) {
351 lTable = [self valueForKeyPath:[lTable substringFromIndex:1]];
354 if([lVal hasPrefix:@"$"]) {
355 lVal = [self valueForKeyPath:[lVal substringFromIndex:1]];
360 label = [rm stringForKey:lKey inTableNamed:lTable withDefaultValue:lVal
361 languages:languages];
365 - (NSString *)localizedNameForDayOfWeek:(unsigned)_dayOfWeek {
366 NSString *key = [dayLabelKeys objectAtIndex:_dayOfWeek % 7];
367 return [self labelForKey:key];
370 - (NSString *)localizedAbbreviatedNameForDayOfWeek:(unsigned)_dayOfWeek {
371 NSString *key = [abbrDayLabelKeys objectAtIndex:_dayOfWeek % 7];
372 return [self labelForKey:key];
375 - (NSString *)localizedNameForMonthOfYear:(unsigned)_monthOfYear {
376 NSString *key = [monthLabelKeys objectAtIndex:(_monthOfYear - 1) % 12];
377 return [self labelForKey:key];
380 - (NSString *)localizedAbbreviatedNameForMonthOfYear:(unsigned)_monthOfYear {
381 NSString *key = [abbrMonthLabelKeys objectAtIndex:(_monthOfYear - 1) % 12];
382 return [self labelForKey:key];
387 - (NSDictionary *)locale {
388 /* we need no fallback here, as locale is guaranteed to be set by sogod */
389 return [[self context] valueForKey:@"locale"];
394 - (BOOL)isUIxDebugEnabled {
395 return uixDebugEnabled;
398 @end /* UIxComponent */