]> err.no Git - scalable-opengroupware.org/blob - SOGo/UI/SOGoUI/UIxComponent.m
more code for day overview and the usual fixes
[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 /* labels */
256
257 - (NSString *)labelForKey:(NSString *)_str {
258     WOResourceManager *rm;
259     NSArray           *languages;
260     WOContext         *ctx;
261     NSString          *label;
262     NSString          *lKey, *lTable, *lVal;
263     NSRange r;
264
265     if ([_str length] == 0) {
266         return nil;
267     }
268     
269     /* lookup languages */
270     
271     ctx = [self context];
272     languages = [ctx hasSession]
273         ? [[ctx session] languages]
274         : [[ctx request] browserLanguages];
275     
276     /* find resource manager */
277     
278     if ((rm = [self resourceManager]) == nil)
279         rm = [[WOApplication application] resourceManager];
280     if (rm == nil)
281         [self debugWithFormat:@"WARNING: missing resource manager!"];
282     
283     /* get parameters */
284     
285     r = [_str rangeOfString:@"/"];
286     if (r.length > 0) {
287         lTable = [_str substringToIndex:r.location];
288         lKey   = [_str substringFromIndex:(r.location + r.length)];
289     }
290     else {
291         lTable = nil;
292         lKey   = _str;
293     }
294     lVal = lKey;
295
296     if([lKey hasPrefix:@"$"]) {
297         lKey = [self valueForKeyPath:[lKey substringFromIndex:1]];
298     }
299     if([lTable hasPrefix:@"$"]) {
300         lTable = [self valueForKeyPath:[lTable substringFromIndex:1]];
301     }
302 #if 0
303     if([lVal hasPrefix:@"$"]) {
304         lVal = [self valueForKeyPath:[lVal substringFromIndex:1]];
305     }
306 #endif
307     /* lookup string */
308     
309     label = [rm stringForKey:lKey inTableNamed:lTable withDefaultValue:lVal
310                 languages:languages];
311     return label;
312 }
313
314 - (NSString *)localizedNameForDayOfWeek:(unsigned)_dayOfWeek {
315     NSString *key =  [dayLabelKeys objectAtIndex:_dayOfWeek % 7];
316     return [self labelForKey:key];
317 }
318
319 - (NSString *)localizedAbbreviatedNameForDayOfWeek:(unsigned)_dayOfWeek {
320     NSString *key =  [abbrDayLabelKeys objectAtIndex:_dayOfWeek % 7];
321     return [self labelForKey:key];
322 }
323
324 - (NSString *)localizedNameForMonthOfYear:(unsigned)_monthOfYear {
325     NSString *key =  [monthLabelKeys objectAtIndex:(_monthOfYear - 1) % 12];
326     return [self labelForKey:key];
327 }
328
329 - (NSString *)localizedAbbreviatedNameForMonthOfYear:(unsigned)_monthOfYear {
330     NSString *key =  [abbrMonthLabelKeys objectAtIndex:(_monthOfYear - 1) % 12];
331     return [self labelForKey:key];
332 }
333
334 /* locale */
335
336 - (NSDictionary *)locale {
337     /* we need no fallback here, as locale is guaranteed to be set by sogod */
338     return [[self context] valueForKey:@"locale"];
339 }
340
341 @end /* UIxComponent */