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;
29 - (NSMutableDictionary *)_queryParameters;
32 @implementation UIxComponent
34 static NSTimeZone *MET = nil;
35 static NSTimeZone *GMT = nil;
37 static NSMutableArray *dayLabelKeys = nil;
38 static NSMutableArray *abbrDayLabelKeys = nil;
39 static NSMutableArray *monthLabelKeys = nil;
40 static NSMutableArray *abbrMonthLabelKeys = nil;
42 static BOOL uixDebugEnabled = NO;
45 NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
47 uixDebugEnabled = [ud boolForKey:@"SOGoUIxDebugEnabled"];
50 MET = [[NSTimeZone timeZoneWithAbbreviation:@"MET"] retain];
51 GMT = [[NSTimeZone timeZoneWithAbbreviation:@"GMT"] retain];
53 if (dayLabelKeys == nil) {
54 dayLabelKeys = [[NSMutableArray alloc] initWithCapacity:7];
55 [dayLabelKeys addObject:@"Sunday"];
56 [dayLabelKeys addObject:@"Monday"];
57 [dayLabelKeys addObject:@"Tuesday"];
58 [dayLabelKeys addObject:@"Wednesday"];
59 [dayLabelKeys addObject:@"Thursday"];
60 [dayLabelKeys addObject:@"Friday"];
61 [dayLabelKeys addObject:@"Saturday"];
63 abbrDayLabelKeys = [[NSMutableArray alloc] initWithCapacity:7];
64 [abbrDayLabelKeys addObject:@"a2_Sunday"];
65 [abbrDayLabelKeys addObject:@"a2_Monday"];
66 [abbrDayLabelKeys addObject:@"a2_Tuesday"];
67 [abbrDayLabelKeys addObject:@"a2_Wednesday"];
68 [abbrDayLabelKeys addObject:@"a2_Thursday"];
69 [abbrDayLabelKeys addObject:@"a2_Friday"];
70 [abbrDayLabelKeys addObject:@"a2_Saturday"];
72 monthLabelKeys = [[NSMutableArray alloc] initWithCapacity:12];
73 [monthLabelKeys addObject:@"January"];
74 [monthLabelKeys addObject:@"February"];
75 [monthLabelKeys addObject:@"March"];
76 [monthLabelKeys addObject:@"April"];
77 [monthLabelKeys addObject:@"May"];
78 [monthLabelKeys addObject:@"June"];
79 [monthLabelKeys addObject:@"July"];
80 [monthLabelKeys addObject:@"August"];
81 [monthLabelKeys addObject:@"September"];
82 [monthLabelKeys addObject:@"October"];
83 [monthLabelKeys addObject:@"November"];
84 [monthLabelKeys addObject:@"December"];
86 abbrMonthLabelKeys = [[NSMutableArray alloc] initWithCapacity:12];
87 [abbrMonthLabelKeys addObject:@"a3_January"];
88 [abbrMonthLabelKeys addObject:@"a3_February"];
89 [abbrMonthLabelKeys addObject:@"a3_March"];
90 [abbrMonthLabelKeys addObject:@"a3_April"];
91 [abbrMonthLabelKeys addObject:@"a3_May"];
92 [abbrMonthLabelKeys addObject:@"a3_June"];
93 [abbrMonthLabelKeys addObject:@"a3_July"];
94 [abbrMonthLabelKeys addObject:@"a3_August"];
95 [abbrMonthLabelKeys addObject:@"a3_September"];
96 [abbrMonthLabelKeys addObject:@"a3_October"];
97 [abbrMonthLabelKeys addObject:@"a3_November"];
98 [abbrMonthLabelKeys addObject:@"a3_December"];
103 [self->queryParameters release];
107 /* query parameters */
109 - (void)_parseQueryString:(NSString *)_s {
113 e = [[_s componentsSeparatedByString:@"&"] objectEnumerator];
114 while ((part = [e nextObject])) {
116 NSString *key, *value;
118 r = [part rangeOfString:@"="];
120 /* missing value of query parameter */
121 key = [part stringByUnescapingURL];
125 key = [[part substringToIndex:r.location] stringByUnescapingURL];
126 value = [[part substringFromIndex:(r.location + r.length)]
127 stringByUnescapingURL];
129 [self->queryParameters setObject:value forKey:key];
133 - (NSString *)queryParameterForKey:(NSString *)_key {
134 return [[self _queryParameters] objectForKey:_key];
137 - (void)setQueryParameter:(NSString *)_param forKey:(NSString *)_key {
142 [[self _queryParameters] setObject:_param forKey:_key];
144 [[self _queryParameters] removeObjectForKey:_key];
147 - (NSMutableDictionary *)_queryParameters {
148 if(!self->queryParameters) {
153 self->queryParameters = [[NSMutableDictionary alloc] initWithCapacity:8];
155 req = [[self context] request];
157 r = [uri rangeOfString:@"?" options:NSBackwardsSearch];
161 qs = [uri substringFromIndex:NSMaxRange(r)];
162 [self _parseQueryString:qs];
165 return self->queryParameters;
168 - (NSDictionary *)queryParameters {
169 return [self _queryParameters];
172 - (NSDictionary *)queryParametersBySettingSelectedDate:(NSCalendarDate *)_date{
173 NSMutableDictionary *qp;
175 qp = [[self queryParameters] mutableCopy];
176 [self setSelectedDateQueryParameter:_date inDictionary:qp];
177 return [qp autorelease];
180 - (void)setSelectedDateQueryParameter:(NSCalendarDate *)_newDate
181 inDictionary:(NSMutableDictionary *)_qp;
184 [_qp setObject:[self dateStringForDate:_newDate] forKey:@"day"];
186 [_qp removeObjectForKey:@"day"];
189 - (NSString *)completeHrefForMethod:(NSString *)_method {
193 qp = [self queryParameters];
197 qs = [[self context] queryStringFromDictionary:qp];
198 return [_method stringByAppendingFormat:@"?%@", qs];
201 - (NSString *)ownMethodName {
205 uri = [[[self context] request] uri];
207 /* first: cut off query parameters */
209 r = [uri rangeOfString:@"?" options:NSBackwardsSearch];
211 uri = [uri substringToIndex:r.location];
213 /* next: strip trailing slash */
215 if ([uri hasSuffix:@"/"]) uri = [uri substringToIndex:([uri length] - 1)];
216 r = [uri rangeOfString:@"/" options:NSBackwardsSearch];
218 /* then: cut of last path component */
220 if (r.length == 0) // no slash? are we at root?
223 return [uri substringFromIndex:(r.location + 1)];
226 - (NSString *)userFolderPath {
228 NSArray *traversalObjects;
231 ctx = [self context];
232 traversalObjects = [ctx objectTraversalStack];
233 url = [[traversalObjects objectAtIndex:1]
234 baseURLInContext:ctx];
235 return [[NSURL URLWithString:url] path];
238 - (NSString *)ownPath {
242 uri = [[[self context] request] uri];
244 /* first: cut off query parameters */
246 r = [uri rangeOfString:@"?" options:NSBackwardsSearch];
248 uri = [uri substringToIndex:r.location];
252 - (NSString *)relativePathToUserFolderSubPath:(NSString *)_sub {
255 dst = [[self userFolderPath] stringByAppendingPathComponent:_sub];
256 rel = [dst urlPathRelativeToPath:[self ownPath]];
262 - (NSTimeZone *)viewTimeZone {
263 // Note: also in the folder, should be based on a cookie?
267 - (NSTimeZone *)backendTimeZone {
271 - (NSCalendarDate *)selectedDate {
273 NSCalendarDate *cdate;
275 s = [self queryParameterForKey:@"day"];
276 cdate = ([s length] > 0)
277 ? [self dateForDateString:s]
278 : [NSCalendarDate date];
279 [cdate setTimeZone:[self viewTimeZone]];
280 s = [self queryParameterForKey:@"hm"];
281 if([s length] == 4) {
282 unsigned hour, minute;
284 hour = [[s substringToIndex:2] unsignedIntValue];
285 minute = [[s substringFromIndex:2] unsignedIntValue];
286 cdate = [cdate hour:hour minute:minute];
289 cdate = [cdate hour:12 minute:0];
294 - (NSString *)dateStringForDate:(NSCalendarDate *)_date {
295 [_date setTimeZone:[self viewTimeZone]];
296 return [_date descriptionWithCalendarFormat:@"%Y%m%d"];
299 - (NSCalendarDate *)dateForDateString:(NSString *)_dateString {
300 return [NSCalendarDate dateWithString:_dateString
301 calendarFormat:@"%Y%m%d"];
310 ctx = [self context];
311 return [[[self clientObject] authenticatorInContext:ctx] userInContext:ctx];
314 - (NSString *)shortUserNameForDisplay {
315 // TODO: better use a SoUser formatter?
319 #warning !! USE USER MANAGER INSTEAD!
321 s = [[self user] login];
325 // TODO: algorithm might be inappropriate, depends on the actual UID
327 r = [s rangeOfString:@"."];
331 return [s substringToIndex:r.location];
336 - (NSString *)labelForKey:(NSString *)_str {
337 WOResourceManager *rm;
341 NSString *lKey, *lTable, *lVal;
344 if ([_str length] == 0) {
348 /* lookup languages */
350 ctx = [self context];
351 languages = [ctx hasSession]
352 ? [[ctx session] languages]
353 : [[ctx request] browserLanguages];
355 /* find resource manager */
357 if ((rm = [self resourceManager]) == nil)
358 rm = [[WOApplication application] resourceManager];
360 [self debugWithFormat:@"WARNING: missing resource manager!"];
364 r = [_str rangeOfString:@"/"];
366 lTable = [_str substringToIndex:r.location];
367 lKey = [_str substringFromIndex:(r.location + r.length)];
375 if([lKey hasPrefix:@"$"]) {
376 lKey = [self valueForKeyPath:[lKey substringFromIndex:1]];
378 if([lTable hasPrefix:@"$"]) {
379 lTable = [self valueForKeyPath:[lTable substringFromIndex:1]];
382 if([lVal hasPrefix:@"$"]) {
383 lVal = [self valueForKeyPath:[lVal substringFromIndex:1]];
388 label = [rm stringForKey:lKey inTableNamed:lTable withDefaultValue:lVal
389 languages:languages];
393 - (NSString *)localizedNameForDayOfWeek:(unsigned)_dayOfWeek {
394 NSString *key = [dayLabelKeys objectAtIndex:_dayOfWeek % 7];
395 return [self labelForKey:key];
398 - (NSString *)localizedAbbreviatedNameForDayOfWeek:(unsigned)_dayOfWeek {
399 NSString *key = [abbrDayLabelKeys objectAtIndex:_dayOfWeek % 7];
400 return [self labelForKey:key];
403 - (NSString *)localizedNameForMonthOfYear:(unsigned)_monthOfYear {
404 NSString *key = [monthLabelKeys objectAtIndex:(_monthOfYear - 1) % 12];
405 return [self labelForKey:key];
408 - (NSString *)localizedAbbreviatedNameForMonthOfYear:(unsigned)_monthOfYear {
409 NSString *key = [abbrMonthLabelKeys objectAtIndex:(_monthOfYear - 1) % 12];
410 return [self labelForKey:key];
415 - (NSDictionary *)locale {
416 /* we need no fallback here, as locale is guaranteed to be set by sogod */
417 return [[self context] valueForKey:@"locale"];
422 - (BOOL)isUIxDebugEnabled {
423 return uixDebugEnabled;
426 @end /* UIxComponent */