]> err.no Git - sope/blob - sope-appserver/NGObjWeb/DynamicElements/WOImageButton.m
improved query string handling
[sope] / sope-appserver / NGObjWeb / DynamicElements / WOImageButton.m
1 /*
2   Copyright (C) 2000-2005 SKYRIX Software AG
3
4   This file is part of SOPE.
5
6   SOPE 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   SOPE 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 SOPE; 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
22 #include "WOInput.h"
23
24 @interface WOImageButton : WOInput
25 {
26   // WODynamicElement: extraAttributes
27   // WODynamicElement: otherTagString
28   // WOInput:    name
29   // WOInput:    value
30   // WOInput:    disabled
31 @protected
32   WOAssociation *filename;  // image path relative to WebServerResources
33   WOAssociation *framework;
34   WOAssociation *src;       // absolute URL
35   WOAssociation *action;
36   WOAssociation *pageName;
37   WOAssociation *x;
38   WOAssociation *y;
39   
40   // new in WO4:
41   WOAssociation *queryDictionary;
42   NSDictionary  *queryParameters;  // associations beginning with ?
43   WOAssociation *actionClass;
44   WOAssociation *directActionName;
45   BOOL          sidInUrl;
46   
47   // non-WO
48   WOAssociation *disabledFilename; // image path to icon for 'disabled' state
49 }
50
51 @end /* WOImageButton */
52
53 #include <NGObjWeb/WOApplication.h>
54 #include <NGObjWeb/WOResourceManager.h>
55 #include "decommon.h"
56
57 @implementation WOImageButton
58
59 + (int)version {
60   return 2;
61 }
62
63 - (id)initWithName:(NSString *)_name
64   associations:(NSDictionary *)_config
65   template:(WOElement *)_t
66 {
67   if ((self = [super initWithName:_name associations:_config template:_t])) {
68     WOAssociation *sidInUrlAssoc;
69     
70     sidInUrlAssoc   = OWGetProperty(_config, @"?wosid");
71     self->action    = OWGetProperty(_config, @"action");
72     self->filename  = OWGetProperty(_config, @"filename");
73     self->framework = OWGetProperty(_config, @"framework");
74     self->pageName  = OWGetProperty(_config, @"pageName");
75     self->x         = OWGetProperty(_config, @"x");
76     self->y         = OWGetProperty(_config, @"y");
77
78     self->queryDictionary  = OWGetProperty(_config, @"queryDictionary");
79     self->queryParameters  = OWExtractQueryParameters(_config);
80     self->actionClass      = OWGetProperty(_config, @"actionClass");
81     self->directActionName = OWGetProperty(_config, @"directActionName");
82     
83     self->disabledFilename = OWGetProperty(_config, @"disabledFilename");
84     
85     self->sidInUrl = (sidInUrlAssoc)
86       ? [sidInUrlAssoc boolValueInComponent:nil]
87       : YES;
88   }
89   return self;
90 }
91
92 - (void)dealloc {
93   [self->actionClass      release];
94   [self->directActionName release];
95   [self->queryDictionary  release];
96   [self->queryParameters  release];
97   [self->action           release];
98   [self->disabledFilename release];
99   [self->framework        release];
100   [self->filename         release];
101   [self->src              release];
102   [self->pageName         release];
103   [self->x                release];
104   [self->y                release];
105   [super dealloc];
106 }
107
108 /* handling requests */
109
110 - (void)takeValuesFromRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
111   WOComponent *sComponent = [_ctx component];
112   NSString *baseId = nil;
113   id       xVal    = nil;
114   id       yVal    = nil;
115
116   //NSLog(@"%s: take values ...", __PRETTY_FUNCTION__);
117   
118   if (self->disabled != nil) {
119     if ([self->disabled boolValueInComponent:sComponent])
120       return;
121   }
122   
123   baseId = OWFormElementName(self, _ctx);
124   
125   xVal = [_rq formValueForKey:[baseId stringByAppendingString:@".x"]];
126   yVal = [_rq formValueForKey:[baseId stringByAppendingString:@".y"]];
127
128   if (xVal) {
129     if ([self->x isValueSettable]) {
130       [self->x setUnsignedIntValue:[xVal unsignedIntValue]
131            inComponent:sComponent];
132     }
133   }
134   if (yVal) {
135     if ([self->y isValueSettable]) {
136       [self->y setUnsignedIntValue:[yVal unsignedIntValue]
137            inComponent:sComponent];
138     }
139   }
140   
141   if (((xVal != nil) || (yVal != nil)) &&
142       ((self->action != nil) || (self->pageName != nil))) {
143     /* should perform action */
144     [_ctx addActiveFormElement:self];
145   }
146 }
147
148 - (id)invokeActionForRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
149   if (self->disabled != nil) {
150     if ([self->disabled boolValueInComponent:[_ctx component]])
151       return nil;
152   }
153   
154   /* check whether this is the active form element (determined above) */
155   if (![[_ctx elementID] isEqualToString:[_ctx senderID]]) {
156     NSLog(@"WOImageButton is not active (%@ vs %@) !",
157           [_ctx elementID], [_ctx senderID]);
158     return nil;
159   }
160   
161   if (self->action)
162     return [self executeAction:self->action inContext:_ctx];
163     
164   if (self->pageName) {
165     NSString    *pname = nil;
166     WOComponent *page = nil;
167
168     pname = [self->pageName stringValueInComponent:[_ctx component]];
169     page = [[_ctx application] pageWithName:pname inContext:_ctx];
170
171     if (page == nil) {
172       [[_ctx session] logWithFormat:
173                       @"%@[0x%08X]: did not find page with name %@ !",
174                       NSStringFromClass([self class]), self, pname];
175     }
176     NSLog(@"%@: showing page %@", self, page);
177     return page;
178   }
179   return nil;
180 }
181
182 /* generating response */
183
184 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
185   WOComponent *sComponent = [_ctx component];
186   NSString *uUri = nil;
187   NSString *uFi  = nil;
188   BOOL isDisabled;
189
190   uUri = [self->src stringValueInComponent:sComponent];
191   
192   if ((isDisabled = [self->disabled boolValueInComponent:sComponent])) {
193     uFi =  [self->disabledFilename stringValueInComponent:sComponent];
194     if (uFi == nil)
195       uFi = [self->filename stringValueInComponent:sComponent];
196   }
197   else
198     uFi = [self->filename stringValueInComponent:sComponent];
199
200   if (isDisabled) {
201     WOResponse_AddCString(_response, "<img");
202   }
203   else {
204     WOResponse_AddCString(_response, "<input type=\"image\" name=\"");
205     [_response appendContentHTMLAttributeValue:OWFormElementName(self, _ctx)];
206     WOResponse_AddChar(_response, '"');
207   }
208   
209   WOResponse_AddCString(_response, " src=\"");
210   if (uFi != nil) {
211     WOResourceManager *rm;
212     NSArray *langs;
213     NSString  *frameworkName;
214
215     if ((rm = [[_ctx component] resourceManager]) == nil)
216       rm = [[_ctx application] resourceManager];
217
218     langs = [_ctx resourceLookupLanguages];
219     
220     /* If 'framework' binding is not set, use parent component's framework */
221     if (self->framework){
222       frameworkName = [self->framework stringValueInComponent:sComponent];
223       if (frameworkName != nil && [frameworkName isEqualToString:@"app"])
224         frameworkName = nil;
225     }
226     else
227       frameworkName = [sComponent frameworkName];
228     
229     uFi = [rm urlForResourceNamed:uFi
230                inFramework:frameworkName
231                languages:langs
232                request:[_ctx request]];
233     if (uFi == nil) {
234       NSLog(@"%@: did not find resource '%@'", sComponent,
235             [self->filename stringValueInComponent:sComponent]);
236       uFi = uUri;
237     }
238     [_response appendContentHTMLAttributeValue:uFi];
239   }
240   else
241     [_response appendContentHTMLAttributeValue:uUri];
242   WOResponse_AddChar(_response, '"');
243   
244   [self appendExtraAttributesToResponse:_response inContext:_ctx];
245   if (self->otherTagString != nil) {
246     NSString *s;
247     
248     s = [self->otherTagString stringValueInComponent:sComponent];
249     WOResponse_AddChar(_response, ' ');
250     WOResponse_AddString(_response, s);
251   }
252   WOResponse_AddEmptyCloseParens(_response, _ctx);
253 }
254
255 /* description */
256
257 - (NSString *)associationDescription {
258   NSMutableString *str;
259   
260   str = [NSMutableString stringWithCapacity:128];
261   [str appendString:[super associationDescription]];
262   if (self->action)   [str appendFormat:@" action=%@", self->action];
263   if (self->pageName) [str appendFormat:@" page=%@", self->pageName];
264   if (self->filename) [str appendFormat:@" file=%@", self->filename];
265   if (self->src)      [str appendFormat:@" src=%@",  self->src];
266   if (self->x)        [str appendFormat:@" x=%@",    self->x];
267   if (self->y)        [str appendFormat:@" y=%@",    self->y];
268
269   if (self->actionClass)
270     [str appendFormat:@" actionClass=%@", self->actionClass];
271   if (self->directActionName)
272     [str appendFormat:@" directAction=%@", self->directActionName];
273   
274   return str;
275 }
276
277 @end /* WOImageButton */