]> err.no Git - sope/blob - sope-appserver/WEExtensions/WEContextConditional.m
new Xcode projects, removed old Xcode project
[sope] / sope-appserver / WEExtensions / WEContextConditional.m
1 /*
2   Copyright (C) 2000-2003 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 #import "common.h"
24 #import "WEContextConditional.h"
25
26 @implementation WEContextConditional
27
28 - (id)initWithName:(NSString *)_name
29   associations:(NSDictionary *)_config
30   template:(WOElement *)_c
31 {
32   if ((self = [super initWithName:_name associations:_config template:_c])) {
33     self->negate     = WOExtGetProperty(_config, @"negate");
34     self->contextKey = WOExtGetProperty(_config, @"contextKey");
35     self->didMatch   = WOExtGetProperty(_config, @"didMatch");
36
37     self->template  = RETAIN(_c);
38   }
39   return self;
40 }
41
42 #if !LIB_FOUNDATION_BOEHM_GC
43 - (void)dealloc {
44   RELEASE(self->template);
45   RELEASE(self->negate);
46   RELEASE(self->contextKey);
47   RELEASE(self->didMatch);
48   [super dealloc];
49 }
50 #endif
51
52 // accessors
53
54 - (WOElement *)template {
55   return self->template;
56 }
57
58 - (NSString *)_contextKey {
59   return nil;
60 }
61
62 - (NSString *)_didMatchKey {
63   return nil;
64 }
65
66 // state
67
68 static inline BOOL _doShow(WEContextConditional *self, WOContext *_ctx) {
69   BOOL doShow   = NO;
70   BOOL doNegate = [self->negate boolValueInComponent:[_ctx component]];
71
72   if ([self _contextKey])
73     doShow = ([_ctx objectForKey:[self _contextKey]] != nil);
74   else if (self->contextKey) {
75     id cKey = [self->contextKey valueInComponent:[_ctx component]];
76
77     doShow = ([_ctx objectForKey:cKey] != nil);
78   }
79   doShow = doNegate ? !doShow : doShow;
80   
81   if (doShow && [self->didMatch isValueSettable])
82     [self->didMatch setBoolValue:YES inComponent:[_ctx component]];
83   
84   if (doShow && [self _didMatchKey] != nil)
85     [_ctx setObject:@"YES" forKey:[self _didMatchKey]];
86   
87   return doShow;
88 }
89
90 // ******************** responder ********************
91
92 - (void)takeValuesFromRequest:(WORequest *)_request
93   inContext:(WOContext *)_ctx
94 {
95   if (_doShow(self, _ctx)) {
96     [_ctx appendElementIDComponent:@"1"];
97     [self->template takeValuesFromRequest:_request inContext:_ctx];
98     [_ctx deleteLastElementIDComponent];
99   }
100 }
101
102 - (id)invokeActionForRequest:(WORequest *)_request inContext:(WOContext *)_ctx {
103   NSString *state;
104
105   state = [[_ctx currentElementID] stringValue];
106   
107   if (state) {
108     [_ctx consumeElementID]; // consume state-id (on or off)
109
110     if ([state isEqualToString:@"1"]) {
111       id result;
112       
113       [_ctx appendElementIDComponent:state];
114       result = [self->template invokeActionForRequest:_request inContext:_ctx];
115       [_ctx deleteLastElementIDComponent];
116
117       return result;
118     }
119   }
120   return nil;
121 }
122
123 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
124   if (_doShow(self, _ctx)) {
125     [_ctx appendElementIDComponent:@"1"];
126     [self->template appendToResponse:_response inContext:_ctx];
127     [_ctx deleteLastElementIDComponent];
128   }
129 }
130
131 @end /* WEContextConditional */