]> err.no Git - scalable-opengroupware.org/blob - SOGo/UI/SOGoUI/UIxComponent.m
Working AnaisUidSelector, working redirects, some refactoring.
[scalable-opengroupware.org] / SOGo / UI / SOGoUI / UIxComponent.m
1 /*
2   Copyright (C) 2004 SKYRIX Software AG
3
4   This file is part of OpenGroupware.org.
5
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
9   later version.
10
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.
15
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
19   02111-1307, USA.
20 */
21 // $Id$
22
23
24 #include "UIxComponent.h"
25 #include <Foundation/Foundation.h>
26 #include <NGObjWeb/NGObjWeb.h>
27 #include <NGExtensions/NGExtensions.h>
28
29
30 @interface UIxComponent (PrivateAPI)
31 - (void)_parseQueryString:(NSString *)_s;
32 @end
33
34
35 @implementation UIxComponent
36
37 static NSTimeZone *MET = nil;
38 static NSTimeZone *GMT = nil;
39
40 static NSMutableArray *dayLabelKeys = nil;
41 static NSMutableArray *abbrDayLabelKeys = nil;
42 static NSMutableArray *monthLabelKeys = nil;
43 static NSMutableArray *abbrMonthLabelKeys = nil;
44
45 + (void)initialize {
46     if (MET == nil) {
47         MET = [[NSTimeZone timeZoneWithAbbreviation:@"MET"] retain];
48         GMT = [[NSTimeZone timeZoneWithAbbreviation:@"GMT"] retain];
49     }
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"];
59
60         abbrDayLabelKeys = [[NSMutableArray alloc] initWithCapacity:7];
61         [abbrDayLabelKeys addObject:@"a2_Sunday"];
62         [abbrDayLabelKeys addObject:@"a2_Monday"];
63         [abbrDayLabelKeys addObject:@"a2_Tuesday"];
64         [abbrDayLabelKeys addObject:@"a2_Wednesday"];
65         [abbrDayLabelKeys addObject:@"a2_Thursday"];
66         [abbrDayLabelKeys addObject:@"a2_Friday"];
67         [abbrDayLabelKeys addObject:@"a2_Saturday"];
68
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"];
82
83         abbrMonthLabelKeys = [[NSMutableArray alloc] initWithCapacity:12];
84         [abbrMonthLabelKeys addObject:@"a3_January"];
85         [abbrMonthLabelKeys addObject:@"a3_February"];
86         [abbrMonthLabelKeys addObject:@"a3_March"];
87         [abbrMonthLabelKeys addObject:@"a3_April"];
88         [abbrMonthLabelKeys addObject:@"a3_May"];
89         [abbrMonthLabelKeys addObject:@"a3_June"];
90         [abbrMonthLabelKeys addObject:@"a3_July"];
91         [abbrMonthLabelKeys addObject:@"a3_August"];
92         [abbrMonthLabelKeys addObject:@"a3_September"];
93         [abbrMonthLabelKeys addObject:@"a3_October"];
94         [abbrMonthLabelKeys addObject:@"a3_November"];
95         [abbrMonthLabelKeys addObject:@"a3_December"];
96     }
97 }
98
99 - (id)init {
100   if ((self = [super init])) {
101     self->queryParameters = [[NSMutableDictionary alloc] init];
102   }
103   return self;
104 }
105
106 - (void)dealloc {
107   [self->queryParameters release];
108   [super dealloc];
109 }
110
111
112 - (void)awake {
113   WORequest *req;
114   NSString  *uri;
115   NSRange   r;
116
117   [super awake];
118
119   req = [[self context] request];
120   uri = [req uri];
121   r   = [uri rangeOfString:@"?"];
122   if (r.length > 0) {
123     NSString *qs;
124     
125     qs = [uri substringFromIndex:(r.location + r.length)];
126     [self->queryParameters removeAllObjects];
127     [self _parseQueryString:qs];
128   }    
129 }
130
131 - (void)_parseQueryString:(NSString *)_s {
132   NSEnumerator *e;
133   NSString *part;
134     
135   e = [[_s componentsSeparatedByString:@"&"] objectEnumerator];
136   while ((part = [e nextObject])) {
137     NSRange  r;
138     NSString *key, *value;
139         
140     r = [part rangeOfString:@"="];
141     if (r.length == 0) {
142       /* missing value of query parameter */
143       key   = [part stringByUnescapingURL];
144       value = @"1";
145     }
146     else {
147       key   = [[part substringToIndex:r.location] stringByUnescapingURL];
148       value = [[part substringFromIndex:(r.location + r.length)] 
149                 stringByUnescapingURL];
150     }
151     [self->queryParameters setObject:value forKey:key];
152   }
153 }
154
155 - (NSString *)queryParameterForKey:(NSString *)_key {
156   return [self->queryParameters objectForKey:_key];
157 }
158
159 - (void)setQueryParameter:(NSString *)_param forKey:(NSString *)_key {
160   if(_key == nil)
161     return;
162
163   if(_param != nil)
164     [self->queryParameters setObject:_param forKey:_key];
165   else
166     [self->queryParameters removeObjectForKey:_key];
167 }
168
169 - (NSDictionary *)queryParameters {
170   return self->queryParameters;
171 }
172
173 - (NSString *)completeHrefForMethod:(NSString *)_method {
174   NSDictionary *qp;
175   NSString *qs;
176     
177   qp = [self queryParameters];
178   if([qp count] == 0)
179     return _method;
180     
181   qs = [[self context] queryStringFromDictionary:qp];
182   return [_method stringByAppendingFormat:@"?%@", qs];
183 }
184
185 - (NSString *)ownMethodName {
186   NSString *uri;
187   NSRange  r;
188     
189   uri = [[[self context] request] uri];
190     
191   /* first: cut off query parameters */
192     
193   r = [uri rangeOfString:@"?" options:NSBackwardsSearch];
194   if (r.length > 0)
195     uri = [uri substringToIndex:r.location];
196     
197   /* next: strip trailing slash */
198     
199   if ([uri hasSuffix:@"/"]) uri = [uri substringToIndex:([uri length] - 1)];
200   r = [uri rangeOfString:@"/" options:NSBackwardsSearch];
201     
202   /* then: cut of last path component */
203     
204   if (r.length == 0) // no slash? are we at root?
205     return @"/";
206     
207   return [uri substringFromIndex:(r.location + 1)];
208 }
209
210 /* date */
211
212 - (NSTimeZone *)viewTimeZone {
213   // Note: also in the folder, should be based on a cookie?
214   return MET;
215 }
216
217 - (NSTimeZone *)backendTimeZone {
218   return GMT;
219 }
220
221 - (NSCalendarDate *)selectedDate {
222   NSString       *s;
223   NSCalendarDate *cdate;
224
225   s = [self queryParameterForKey:@"day"];
226   cdate = ([s length] > 0)
227     ? [self dateForDateString:s]
228     : [NSCalendarDate date];
229   [cdate setTimeZone:[self viewTimeZone]];
230   s = [self queryParameterForKey:@"hm"];
231   if([s length] == 4) {
232       unsigned hour, minute;
233       
234       hour = [[s substringToIndex:2] unsignedIntValue];
235       minute = [[s substringFromIndex:2] unsignedIntValue];
236       cdate = [cdate hour:hour minute:minute];
237   }
238   else {
239       cdate = [cdate hour:12 minute:0];
240   }
241   return cdate;
242 }
243
244 - (NSString *)dateStringForDate:(NSCalendarDate *)_date {
245   [_date setTimeZone:[self viewTimeZone]];
246   return [_date descriptionWithCalendarFormat:@"%Y%m%d"];
247 }
248
249 - (NSCalendarDate *)dateForDateString:(NSString *)_dateString {
250   return [NSCalendarDate dateWithString:_dateString 
251                          calendarFormat:@"%Y%m%d"];
252 }
253
254
255 /* SoUser */
256
257 - (SoUser *)user {
258     WOContext *ctx;
259     
260     ctx = [self context];
261     return [[[self clientObject] authenticatorInContext:ctx]
262                                userInContext:ctx];
263 }
264
265 - (NSString *)shortUserNameForDisplay {
266     // TODO: better use a SoUser formatter?
267     NSString *s;
268     NSRange  r;
269     
270     s = [[self user] login];
271     if ([s length] < 10)
272         return s;
273     
274     // TODO: algorithm might be inappropriate, depends on the actual UID
275     
276     r = [s rangeOfString:@"."];
277     if (r.length == 0)
278         return s;
279     
280     return [s substringToIndex:r.location];
281 }
282
283 /* labels */
284
285 - (NSString *)labelForKey:(NSString *)_str {
286     WOResourceManager *rm;
287     NSArray           *languages;
288     WOContext         *ctx;
289     NSString          *label;
290     NSString          *lKey, *lTable, *lVal;
291     NSRange r;
292
293     if ([_str length] == 0) {
294         return nil;
295     }
296     
297     /* lookup languages */
298     
299     ctx = [self context];
300     languages = [ctx hasSession]
301         ? [[ctx session] languages]
302         : [[ctx request] browserLanguages];
303     
304     /* find resource manager */
305     
306     if ((rm = [self resourceManager]) == nil)
307         rm = [[WOApplication application] resourceManager];
308     if (rm == nil)
309         [self debugWithFormat:@"WARNING: missing resource manager!"];
310     
311     /* get parameters */
312     
313     r = [_str rangeOfString:@"/"];
314     if (r.length > 0) {
315         lTable = [_str substringToIndex:r.location];
316         lKey   = [_str substringFromIndex:(r.location + r.length)];
317     }
318     else {
319         lTable = nil;
320         lKey   = _str;
321     }
322     lVal = lKey;
323
324     if([lKey hasPrefix:@"$"]) {
325         lKey = [self valueForKeyPath:[lKey substringFromIndex:1]];
326     }
327     if([lTable hasPrefix:@"$"]) {
328         lTable = [self valueForKeyPath:[lTable substringFromIndex:1]];
329     }
330 #if 0
331     if([lVal hasPrefix:@"$"]) {
332         lVal = [self valueForKeyPath:[lVal substringFromIndex:1]];
333     }
334 #endif
335     /* lookup string */
336     
337     label = [rm stringForKey:lKey inTableNamed:lTable withDefaultValue:lVal
338                 languages:languages];
339     return label;
340 }
341
342 - (NSString *)localizedNameForDayOfWeek:(unsigned)_dayOfWeek {
343     NSString *key =  [dayLabelKeys objectAtIndex:_dayOfWeek % 7];
344     return [self labelForKey:key];
345 }
346
347 - (NSString *)localizedAbbreviatedNameForDayOfWeek:(unsigned)_dayOfWeek {
348     NSString *key =  [abbrDayLabelKeys objectAtIndex:_dayOfWeek % 7];
349     return [self labelForKey:key];
350 }
351
352 - (NSString *)localizedNameForMonthOfYear:(unsigned)_monthOfYear {
353     NSString *key =  [monthLabelKeys objectAtIndex:(_monthOfYear - 1) % 12];
354     return [self labelForKey:key];
355 }
356
357 - (NSString *)localizedAbbreviatedNameForMonthOfYear:(unsigned)_monthOfYear {
358     NSString *key =  [abbrMonthLabelKeys objectAtIndex:(_monthOfYear - 1) % 12];
359     return [self labelForKey:key];
360 }
361
362 /* locale */
363
364 - (NSDictionary *)locale {
365     /* we need no fallback here, as locale is guaranteed to be set by sogod */
366     return [[self context] valueForKey:@"locale"];
367 }
368
369 @end /* UIxComponent */