]> err.no Git - scalable-opengroupware.org/blob - SOGo/UI/SOGoUI/UIxComponent.m
Ongoing localization
[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:@"Sun"];
62         [abbrDayLabelKeys addObject:@"Mon"];
63         [abbrDayLabelKeys addObject:@"Tue"];
64         [abbrDayLabelKeys addObject:@"Wed"];
65         [abbrDayLabelKeys addObject:@"Thu"];
66         [abbrDayLabelKeys addObject:@"Fri"];
67         [abbrDayLabelKeys addObject:@"Sat"];
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:@"Jan"];
85         [abbrMonthLabelKeys addObject:@"Feb"];
86         [abbrMonthLabelKeys addObject:@"Mar"];
87         [abbrMonthLabelKeys addObject:@"Apr"];
88         [abbrMonthLabelKeys addObject:@"May"];
89         [abbrMonthLabelKeys addObject:@"Jun"];
90         [abbrMonthLabelKeys addObject:@"Jul"];
91         [abbrMonthLabelKeys addObject:@"Aug"];
92         [abbrMonthLabelKeys addObject:@"Sep"];
93         [abbrMonthLabelKeys addObject:@"Oct"];
94         [abbrMonthLabelKeys addObject:@"Nov"];
95         [abbrMonthLabelKeys addObject:@"Dec"];
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   return cdate;
231 }
232
233 - (NSString *)dateStringForDate:(NSCalendarDate *)_date {
234   [_date setTimeZone:[self viewTimeZone]];
235   return [_date descriptionWithCalendarFormat:@"%Y%m%d"];
236 }
237
238 - (NSCalendarDate *)dateForDateString:(NSString *)_dateString {
239   NSTimeZone *tz;
240   
241   tz = [self viewTimeZone];
242   /* Note: we should give a time, best is noon to avoid edge conditions */
243   _dateString = [_dateString stringByAppendingFormat:@"12:00:00 %@",
244                                [tz abbreviation]];
245   return [NSCalendarDate dateWithString:_dateString 
246                          calendarFormat:@"%Y%m%d %H:%M:%S %Z"];
247 }
248
249
250 /* labels */
251
252 - (NSString *)labelForKey:(NSString *)_str {
253     WOResourceManager *rm;
254     NSArray           *languages;
255     WOContext         *ctx;
256     NSString          *label;
257     NSString          *lKey, *lTable, *lVal;
258     NSRange r;
259
260     if ([_str length] == 0) {
261         return nil;
262     }
263     
264     /* lookup languages */
265     
266     ctx = [self context];
267     languages = [ctx hasSession]
268         ? [[ctx session] languages]
269         : [[ctx request] browserLanguages];
270     
271     /* find resource manager */
272     
273     if ((rm = [self resourceManager]) == nil)
274         rm = [[WOApplication application] resourceManager];
275     if (rm == nil)
276         [self debugWithFormat:@"WARNING: missing resource manager!"];
277     
278     /* get parameters */
279     
280     r = [_str rangeOfString:@"/"];
281     if (r.length > 0) {
282         lTable = [_str substringToIndex:r.location];
283         lKey   = [_str substringFromIndex:(r.location + r.length)];
284     }
285     else {
286         lTable = nil;
287         lKey   = _str;
288     }
289     lVal = lKey;
290
291     if([lKey hasPrefix:@"$"]) {
292         lKey = [self valueForKeyPath:[lKey substringFromIndex:1]];
293     }
294     if([lTable hasPrefix:@"$"]) {
295         lTable = [self valueForKeyPath:[lTable substringFromIndex:1]];
296     }
297 #if 0
298     if([lVal hasPrefix:@"$"]) {
299         lVal = [self valueForKeyPath:[lVal substringFromIndex:1]];
300     }
301 #endif
302     /* lookup string */
303     
304     label = [rm stringForKey:lKey inTableNamed:lTable withDefaultValue:lVal
305                 languages:languages];
306     return label;
307 }
308
309 - (NSString *)localizedNameForDayOfWeek:(unsigned)_dayOfWeek {
310     NSString *key =  [dayLabelKeys objectAtIndex:_dayOfWeek % 7];
311     return [self labelForKey:key];
312 }
313
314 - (NSString *)localizedAbbreviatedNameForDayOfWeek:(unsigned)_dayOfWeek {
315     NSString *key =  [abbrDayLabelKeys objectAtIndex:_dayOfWeek % 7];
316     return [self labelForKey:key];
317 }
318
319 - (NSString *)localizedNameForMonthOfYear:(unsigned)_monthOfYear {
320     NSString *key =  [monthLabelKeys objectAtIndex:(_monthOfYear - 1) % 12];
321     return [self labelForKey:key];
322 }
323
324 - (NSString *)localizedAbbreviatedNameForMonthOfYear:(unsigned)_monthOfYear {
325     NSString *key =  [abbrMonthLabelKeys objectAtIndex:(_monthOfYear - 1) % 12];
326     return [self labelForKey:key];
327 }
328
329 /* locale */
330
331 - (NSDictionary *)locale {
332     /* we need no fallback here, as locale is guaranteed to be set by sogod */
333     return [[self context] valueForKey:@"locale"];
334 }
335
336 @end /* UIxComponent */