]> err.no Git - scalable-opengroupware.org/blob - SOGo/UI/SOGoUI/UIxComponent.m
modified build process for SOGo
[scalable-opengroupware.org] / SOGo / UI / SOGoUI / UIxComponent.m
1 /*
2   Copyright (C) 2000-2004 SKYRIX Software AG
3
4   This file is part of OGo
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 - (id)init {
38     if ((self = [super init])) {
39         self->queryParameters = [[NSMutableDictionary alloc] init];
40     }
41     return self;
42 }
43
44 - (void)dealloc {
45     [self->queryParameters release];
46     [super dealloc];
47 }
48
49
50 - (void)awake {
51     WORequest *req;
52     NSString *uri;
53     NSRange r;
54
55     [super awake];
56
57     req = [[self context] request];
58     uri = [req uri];
59     r = [uri rangeOfString:@"?"];
60     if(r.length > 0) {
61         NSString *qs;
62         
63         qs = [uri substringFromIndex:(r.location + r.length)];
64         [self->queryParameters removeAllObjects];
65         [self _parseQueryString:qs];
66     }    
67 }
68
69 - (void)_parseQueryString:(NSString *)_s {
70     NSEnumerator *e;
71     NSString *part;
72     
73     e = [[_s componentsSeparatedByString:@"&"] objectEnumerator];
74     while ((part = [e nextObject])) {
75         NSRange  r;
76         NSString *key, *value;
77         
78         r = [part rangeOfString:@"="];
79         if (r.length == 0) {
80             /* missing value of query parameter */
81             key   = [part stringByUnescapingURL];
82             value = @"1";
83         }
84         else {
85             key   = [[part substringToIndex:r.location] stringByUnescapingURL];
86             value = [[part substringFromIndex:(r.location + r.length)] 
87                 stringByUnescapingURL];
88         }
89         [self->queryParameters setObject:value forKey:key];
90     }
91 }
92
93 - (NSString *)queryParameterForKey:(NSString *)_key {
94     return [self->queryParameters objectForKey:_key];
95 }
96
97 - (void)setQueryParameter:(NSString *)_param forKey:(NSString *)_key {
98     if(_key == nil)
99         return;
100
101     if(_param != nil)
102         [self->queryParameters setObject:_param forKey:_key];
103     else
104         [self->queryParameters removeObjectForKey:_key];
105 }
106
107 - (NSDictionary *)queryParameters {
108     return self->queryParameters;
109 }
110
111 - (NSString *)completeHrefForMethod:(NSString *)_method {
112     NSDictionary *qp;
113     NSString *qs;
114     
115     qp = [self queryParameters];
116     if([qp count] == 0)
117         return _method;
118     
119     qs = [[self context] queryStringFromDictionary:qp];
120     return [_method stringByAppendingFormat:@"?%@", qs];
121 }
122
123 - (NSString *)ownMethodName {
124     NSString *uri;
125     NSRange  r;
126     
127     uri = [[[self context] request] uri];
128     
129     /* first: cut off query parameters */
130     
131     r = [uri rangeOfString:@"?" options:NSBackwardsSearch];
132     if (r.length > 0)
133         uri = [uri substringToIndex:r.location];
134     
135     /* next: strip trailing slash */
136     
137     if ([uri hasSuffix:@"/"]) uri = [uri substringToIndex:([uri length] - 1)];
138     r = [uri rangeOfString:@"/" options:NSBackwardsSearch];
139     
140     /* then: cut of last path component */
141     
142     if (r.length == 0) // no slash? are we at root?
143         return @"/";
144     
145     return [uri substringFromIndex:(r.location + 1)];
146 }
147
148 /* date */
149
150 - (NSCalendarDate *)selectedDate {
151     NSString *s;
152     
153     s = [self queryParameterForKey:@"day"];
154     if(s) {
155         return [self dateForDateString:s];
156     }
157     return [NSCalendarDate date];
158 }
159
160 - (NSString *)dateStringForDate:(NSCalendarDate *)_date {
161     return [_date descriptionWithCalendarFormat:@"%Y%m%d"];
162 }
163
164 - (NSCalendarDate *)dateForDateString:(NSString *)_dateString {
165     return [NSCalendarDate dateWithString:_dateString calendarFormat:@"%Y%m%d"];
166 }
167
168 @end