]> err.no Git - sope/blob - sope-appserver/NGObjWeb/DynamicElements/WOHTMLDynamicElement.m
major Xcode build overhaul
[sope] / sope-appserver / NGObjWeb / DynamicElements / WOHTMLDynamicElement.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 <NGObjWeb/WOHTMLDynamicElement.h>
23 #include "WOElement+private.h"
24 #include "decommon.h"
25
26 @implementation WOHTMLDynamicElement
27
28 static BOOL debugActionExecute = NO;
29
30 + (int)version {
31   return [super version] + 0 /* v2 */;
32 }
33
34 + (void)initialize {
35   NSUserDefaults *ud;
36     
37   NSAssert2([super version] == 2,
38             @"invalid superclass (%@) version %i !",
39             NSStringFromClass([self superclass]), [super version]);
40   
41   ud = [NSUserDefaults standardUserDefaults];
42   debugActionExecute = [ud boolForKey:@"WODebugActions"];
43 }
44
45 /* active element */
46
47 - (id)executeAction:(WOAssociation *)_action inContext:(WOContext *)_ctx {
48   // TODO: I think this is deprectated?
49   id  object;
50   SEL act;
51   
52   if (_action == nil) {
53     if (debugActionExecute) {
54       [self logWithFormat:@"%@(%@): got no action to execute ..",
55               NSStringFromClass([self class]), _ctx];
56     }
57     return nil;
58   }
59   
60   if ((object = [_ctx component]) == nil) {
61     if (debugActionExecute) {
62       [self logWithFormat:@"%@(%@): got no object to execute action: %@",
63               NSStringFromClass([self class]), _ctx, _action];
64     }
65     return nil;
66   }
67   
68   if (![_action isValueConstant]) {
69     /* action specified like this: action = doIt; */
70     id result;
71     
72     result = [_action valueInComponent:object];
73     if (debugActionExecute) {
74       [self logWithFormat:@"%@(%@): executed dynamic action, got: %@",
75               NSStringFromClass([self class]), _ctx, result];
76     }
77     
78     if ([result respondsToSelector:@selector(ensureAwakeInContext:)]) {
79       if (debugActionExecute) {
80         [self logWithFormat:@"%@(%@): ensure result is awake in ctx",
81                 NSStringFromClass([self class]), _ctx];
82       }
83       [result ensureAwakeInContext:_ctx];
84     }
85     return result;
86   }
87     
88   /* action specified like this: action = "doIt"; */
89
90   [[_ctx component]
91          debugWithFormat:@"WARNING: %@ used with 'string' action !", self];
92
93   act = NSSelectorFromString([_action stringValueInComponent:object]);
94   if ([object respondsToSelector:act])
95     return [object performSelector:act];
96   
97   [[_ctx component] logWithFormat:
98                       @"%@[0x%p]: %@ does not respond to action @%@",
99                       NSStringFromClass([self class]), self,
100                       object,
101                       NSStringFromSelector(act)];
102   return nil;
103 }
104
105 @end /* WOHTMLDynamicElement */
106
107 NSDictionary *OWExtractQueryParameters(NSDictionary *_set) {
108   NSMutableDictionary *paras = nil;
109   NSMutableArray      *paraKeys = nil;
110   NSEnumerator        *keys;
111   NSString            *key;
112
113   /* locate query parameters */
114   keys = [_set keyEnumerator];
115   while ((key = [keys nextObject])) {
116     if ([key hasPrefix:@"?"]) {
117       WOAssociation *value;
118
119       if ([key isEqualToString:@"?wosid"])
120         continue;
121
122       value = [_set objectForKey:key];
123           
124       if (paraKeys == nil) {
125         paraKeys = [NSMutableArray arrayWithCapacity:8];
126         paras    = [NSMutableDictionary dictionaryWithCapacity:8];
127       }
128
129       [paraKeys addObject:key];
130       [paras setObject:value forKey:[key substringFromIndex:1]];
131     }
132   }
133
134   // remove query parameters
135   if (paraKeys) {
136     unsigned cnt, count;
137     for (cnt = 0, count = [paraKeys count]; cnt < count; cnt++) {
138       [(NSMutableDictionary *)_set removeObjectForKey:
139                                      [paraKeys objectAtIndex:cnt]];
140     }
141   }
142
143   // assign parameters
144   return [paras copy];
145 }