]> err.no Git - scalable-opengroupware.org/blob - SOGo/UI/SOGoUI/UIxComponent.m
git-svn-id: http://svn.opengroupware.org/SOGo/trunk@137 d1b88da0-ebda-0310-925b-ed51d...
[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
39 + (void)initialize {
40   if (MET == nil) MET = [[NSTimeZone timeZoneWithAbbreviation:@"MET"] retain];
41 }
42
43 - (id)init {
44   if ((self = [super init])) {
45     self->queryParameters = [[NSMutableDictionary alloc] init];
46   }
47   return self;
48 }
49
50 - (void)dealloc {
51   [self->queryParameters release];
52   [super dealloc];
53 }
54
55
56 - (void)awake {
57   WORequest *req;
58   NSString  *uri;
59   NSRange   r;
60
61   [super awake];
62
63   req = [[self context] request];
64   uri = [req uri];
65   r   = [uri rangeOfString:@"?"];
66   if (r.length > 0) {
67     NSString *qs;
68     
69     qs = [uri substringFromIndex:(r.location + r.length)];
70     [self->queryParameters removeAllObjects];
71     [self _parseQueryString:qs];
72   }    
73 }
74
75 - (void)_parseQueryString:(NSString *)_s {
76   NSEnumerator *e;
77   NSString *part;
78     
79   e = [[_s componentsSeparatedByString:@"&"] objectEnumerator];
80   while ((part = [e nextObject])) {
81     NSRange  r;
82     NSString *key, *value;
83         
84     r = [part rangeOfString:@"="];
85     if (r.length == 0) {
86       /* missing value of query parameter */
87       key   = [part stringByUnescapingURL];
88       value = @"1";
89     }
90     else {
91       key   = [[part substringToIndex:r.location] stringByUnescapingURL];
92       value = [[part substringFromIndex:(r.location + r.length)] 
93                 stringByUnescapingURL];
94     }
95     [self->queryParameters setObject:value forKey:key];
96   }
97 }
98
99 - (NSString *)queryParameterForKey:(NSString *)_key {
100   return [self->queryParameters objectForKey:_key];
101 }
102
103 - (void)setQueryParameter:(NSString *)_param forKey:(NSString *)_key {
104   if(_key == nil)
105     return;
106
107   if(_param != nil)
108     [self->queryParameters setObject:_param forKey:_key];
109   else
110     [self->queryParameters removeObjectForKey:_key];
111 }
112
113 - (NSDictionary *)queryParameters {
114   return self->queryParameters;
115 }
116
117 - (NSString *)completeHrefForMethod:(NSString *)_method {
118   NSDictionary *qp;
119   NSString *qs;
120     
121   qp = [self queryParameters];
122   if([qp count] == 0)
123     return _method;
124     
125   qs = [[self context] queryStringFromDictionary:qp];
126   return [_method stringByAppendingFormat:@"?%@", qs];
127 }
128
129 - (NSString *)ownMethodName {
130   NSString *uri;
131   NSRange  r;
132     
133   uri = [[[self context] request] uri];
134     
135   /* first: cut off query parameters */
136     
137   r = [uri rangeOfString:@"?" options:NSBackwardsSearch];
138   if (r.length > 0)
139     uri = [uri substringToIndex:r.location];
140     
141   /* next: strip trailing slash */
142     
143   if ([uri hasSuffix:@"/"]) uri = [uri substringToIndex:([uri length] - 1)];
144   r = [uri rangeOfString:@"/" options:NSBackwardsSearch];
145     
146   /* then: cut of last path component */
147     
148   if (r.length == 0) // no slash? are we at root?
149     return @"/";
150     
151   return [uri substringFromIndex:(r.location + 1)];
152 }
153
154 /* date */
155
156 - (NSTimeZone *)viewTimeZone {
157   // Note: also in the folder, should be based on a cookie?
158   return MET;
159 }
160
161 - (NSCalendarDate *)selectedDate {
162   NSString       *s;
163   NSCalendarDate *cdate;
164   
165   s = [self queryParameterForKey:@"day"];
166   cdate = ([s length] > 0)
167     ? [self dateForDateString:s]
168     : [NSCalendarDate date];
169   [cdate setTimeZone:[self viewTimeZone]];
170   return cdate;
171 }
172
173 - (NSString *)dateStringForDate:(NSCalendarDate *)_date {
174   [_date setTimeZone:[self viewTimeZone]];
175   return [_date descriptionWithCalendarFormat:@"%Y%m%d"];
176 }
177
178 - (NSCalendarDate *)dateForDateString:(NSString *)_dateString {
179   NSTimeZone *tz;
180   
181   tz = [self viewTimeZone];
182   /* Note: we should give a time, best is noon to avoid edge conditions */
183   _dateString = [_dateString stringByAppendingFormat:@"12:00:00 %@",
184                                [tz abbreviation]];
185   return [NSCalendarDate dateWithString:_dateString 
186                          calendarFormat:@"%Y%m%d %H:%M:%S %Z"];
187 }
188
189 @end /* UIxComponent */