]> err.no Git - sope/blob - sope-appserver/NGObjWeb/DynamicElements/WOMetaRefresh.m
started TAL element builder
[sope] / sope-appserver / NGObjWeb / DynamicElements / WOMetaRefresh.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 "WOHTMLDynamicElement.h"
23 #include "WOElement+private.h"
24 #include <NGObjWeb/WOAssociation.h>
25 #include <NGObjWeb/WOContext.h>
26 #include <NGObjWeb/WOApplication.h>
27 #include <NGObjWeb/WOSession.h>
28 #include <NGObjWeb/WORequest.h>
29 #include <NGObjWeb/WOResponse.h>
30 #include "decommon.h"
31
32 /*
33   WOMetaRefresh associations:
34
35     href | pageName | action | (directActionName & actionClass)
36     fragmentIdentifier
37     disabled
38     timeout/seconds
39 */
40
41 @interface WOMetaRefresh : WOHTMLDynamicElement
42 {
43   // WODynamicElement: extraAttributes
44   // WODynamicElement: otherTagString
45 @protected
46   WOAssociation *action;
47   WOAssociation *href;
48   WOAssociation *pageName;
49   WOAssociation *directActionName;
50   WOAssociation *actionClass;
51   WOAssociation *disabled;
52   WOAssociation *fragmentIdentifier;
53   WOAssociation *timeout;
54
55   WOAssociation *queryDictionary;
56   NSDictionary  *queryParameters;  /* associations beginning with ? */
57   BOOL          sidInUrl;
58 }
59
60 @end
61
62 @implementation WOMetaRefresh
63
64 - (id)initWithName:(NSString *)_name
65   associations:(NSDictionary *)_config
66   template:(WOElement *)_t
67 {
68   if ((self = [super initWithName:_name associations:_config template:_t])) {
69     WOAssociation *sidInUrlAssoc;
70     
71     sidInUrlAssoc            = OWGetProperty(_config, @"?wosid");
72     self->action             = OWGetProperty(_config, @"action");
73     self->href               = OWGetProperty(_config, @"href");
74     self->pageName           = OWGetProperty(_config, @"pageName");
75     self->fragmentIdentifier = OWGetProperty(_config, @"fragmentIdentifier");
76     self->disabled           = OWGetProperty(_config, @"disabled"); 
77     self->timeout            = OWGetProperty(_config, @"timeout"); 
78     self->directActionName   = OWGetProperty(_config, @"directActionName"); 
79     self->actionClass        = OWGetProperty(_config, @"actionClass"); 
80     
81     self->sidInUrl = (sidInUrlAssoc)
82       ? [sidInUrlAssoc boolValueInComponent:nil]
83       : YES;
84     
85     if (self->timeout == nil)
86       self->timeout = OWGetProperty(_config, @"seconds");
87     else if ([OWGetProperty(_config, @"seconds") autorelease] != nil) {
88       [self logWithFormat:
89               @"WARNING: got both, 'timeout' and 'seconds' bindings!"];
90     }
91     
92     self->queryDictionary = OWGetProperty(_config, @"queryDictionary");
93     self->queryParameters = OWExtractQueryParameters(_config);
94   }
95   return self;
96 }
97
98 - (void)dealloc {
99   [self->queryParameters    release];
100   [self->queryDictionary    release];
101   [self->directActionName   release];
102   [self->actionClass        release];
103   [self->action             release];
104   [self->href               release];
105   [self->pageName           release];
106   [self->fragmentIdentifier release];
107   [self->disabled           release];
108   [self->timeout            release];
109   [super dealloc];
110 }
111
112 /* handling requests */
113
114 - (id)invokeActionForRequest:(WORequest *)_request
115   inContext:(WOContext *)_ctx
116 {
117   if ([self->disabled boolValueInComponent:[_ctx component]])
118     return nil;
119   
120   if (self->action)
121     return [self executeAction:self->action inContext:_ctx];
122
123   if (self->pageName) {
124     NSString    *name;
125     WOComponent *page;
126
127     name = [self->pageName stringValueInComponent:[_ctx component]];
128     page = [[_ctx application] pageWithName:name inContext:_ctx];
129       
130     if (page == nil) {
131       [[_ctx component] logWithFormat:
132                           @"%@[0x%08X]: did not find page with name %@ !",
133                           NSStringFromClass([self class]), self, name];
134     }
135     [self debugWithFormat:@"showing page %@", page];
136     return page;
137   }
138   
139   [[_ctx component] 
140          logWithFormat:@"%@[0x%08X]: no action/page set !",
141            NSStringFromClass([self class]), self];
142   return nil;
143 }
144
145 /* generating response */
146
147 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
148   WOComponent *sComponent;
149   int         to;
150   NSString    *url;
151   NSString    *queryString = nil;
152   BOOL addSID;
153   
154   if ([[_ctx request] isFromClientComponent])
155     return;
156   
157   sComponent = [_ctx component];
158   to = [self->timeout intValueInComponent:sComponent];
159   WOResponse_AddCString(_response, "<meta http-equiv=\"refresh\" content=\"");
160   WOResponse_AddInt(_response, to);
161   WOResponse_AddCString(_response, "; url=");
162
163   if (self->href) {
164     /* a href was explicitly assigned */
165     url = [self->href stringValueInComponent:sComponent];
166     addSID = self->sidInUrl;
167   }
168   else if (self->directActionName) {
169     /* a direct action */
170     NSString *daClass;
171     NSString *daName;
172
173     daClass = [self->actionClass      stringValueInComponent:sComponent];
174     daName  = [self->directActionName stringValueInComponent:sComponent];
175     
176     if (daClass) {
177       if (daName) {
178         if (![daClass isEqualToString:@"DirectAction"])
179           daName = [NSString stringWithFormat:@"%@/%@", daClass, daName];
180       }
181       else
182         daName = daClass;
183     }
184
185     url = [_ctx directActionURLForActionNamed:daName queryDictionary:nil];
186     addSID = self->sidInUrl;
187   }
188   else {
189     url = [_ctx componentActionURL];
190     addSID = NO;
191   }
192   WOResponse_AddString(_response, url);
193   
194   queryString = [self queryStringForQueryDictionary:
195                         [self->queryDictionary valueInComponent:sComponent]
196                       andQueryParameters:self->queryParameters
197                       inContext:_ctx];
198   if (addSID && [sComponent hasSession]) {
199     WOSession *sn = [sComponent session];
200     
201     if ([queryString length] == 0) {
202       queryString = [NSString stringWithFormat:@"%@=%@", 
203                               WORequestValueSessionID, [sn sessionID]];
204     }
205     else {
206       queryString = [queryString stringByAppendingFormat:@"&%@=%@", 
207                               WORequestValueSessionID, [sn sessionID]];
208     }
209   }
210   
211   if (self->fragmentIdentifier) {
212     [_response appendContentCharacter:'#'];
213     WOResponse_AddString(_response,
214                          [self->fragmentIdentifier stringValueInComponent:
215                               sComponent]);
216   }
217   
218   if (queryString) {
219     [_response appendContentCharacter:'?'];
220     WOResponse_AddString(_response, queryString);
221   }
222   
223   [_response appendContentCharacter:'"']; // close CONTENT attribute
224   
225   [self appendExtraAttributesToResponse:_response inContext:_ctx];
226   if (self->otherTagString) {
227     WOResponse_AddChar(_response, ' ');
228     WOResponse_AddString(_response,
229                          [self->otherTagString stringValueInComponent:
230                               [_ctx component]]);
231   }
232   WOResponse_AddEmptyCloseParens(_response, _ctx);
233 }
234
235 /* description */
236
237 - (NSString *)associationDescription {
238   NSMutableString *str = [NSMutableString stringWithCapacity:256];
239   
240   if (self->action)   [str appendFormat:@" action=%@",   self->action];
241   if (self->href)     [str appendFormat:@" href=%@",     self->href];
242   if (self->pageName) [str appendFormat:@" pageName=%@", self->pageName];
243   if (self->fragmentIdentifier)
244     [str appendFormat:@" fragment=%@", self->fragmentIdentifier];
245   if (self->disabled) [str appendFormat:@" disabled=%@", self->disabled];
246   
247   return str;
248 }
249
250 @end /* WOMetaRefresh */