]> err.no Git - sope/blob - sope-appserver/NGObjWeb/DynamicElements/WOEmbeddedObject.m
fixed copyrights for 2005
[sope] / sope-appserver / NGObjWeb / DynamicElements / WOEmbeddedObject.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/WOResourceManager.h>
25 #include <NGObjWeb/WOApplication.h>
26 #include "common.h"
27
28 @interface WOEmbeddedObject : WOHTMLDynamicElement
29 {
30   // WODynamicElement: extraAttributes
31   // WODynamicElement: otherTagString
32 @protected
33   WOAssociation *filename;  // path relative to WebServerResources
34   WOAssociation *framework;
35   WOAssociation *src;       // absolute URL
36   WOAssociation *value;     // data (eg from a database)
37
38   /* new in WO4 */
39   WOAssociation *data;
40   WOAssociation *mimeType;
41   WOAssociation *key;
42 }
43
44 @end /* WOEmbeddedObject */
45
46 @implementation WOEmbeddedObject
47
48 - (id)initWithName:(NSString *)_name
49   associations:(NSDictionary *)_config
50   template:(WOElement *)_tmpl
51 {
52   if ((self = [super initWithName:_name associations:_config template:_tmpl])) {
53     self->filename  = OWGetProperty(_config, @"filename");
54     self->framework = OWGetProperty(_config, @"framework");
55     self->src       = OWGetProperty(_config, @"src");
56     self->value     = OWGetProperty(_config, @"value");
57
58     self->data      = OWGetProperty(_config, @"data");
59     self->mimeType  = OWGetProperty(_config, @"mimeType");
60     self->key       = OWGetProperty(_config, @"key");
61
62     if (self->key)
63       NSLog(@"WARNING: 'key' association in WOEmbeddedObject not supported !");
64   }
65   return self;
66 }
67
68 #if !LIB_FOUNDATION_BOEHM_GC
69 - (void)dealloc {
70   RELEASE(self->key);
71   RELEASE(self->data);
72   RELEASE(self->mimeType);
73   RELEASE(self->framework);
74   RELEASE(self->filename);
75   RELEASE(self->src);
76   RELEASE(self->value);
77   [super dealloc];
78 }
79 #endif
80
81 // ******************** responder ********************
82
83 - (id)invokeActionForRequest:(WORequest *)_request inContext:(WOContext *)_ctx {
84   if (self->value) {
85     WOElement *element;
86
87     if ((element = [self->value valueInComponent:[_ctx component]]) == nil) {
88       [[_ctx session] debugWithFormat:
89           @"WARNING: missing element value for WOEmbeddedObject %@", self];
90       return nil;
91     }
92
93     [element appendToResponse:[_ctx response] inContext:_ctx];
94     return [_ctx response];
95   }
96   else if (self->data) {
97     WOComponent *sComponent;
98     NSData     *adata;
99     NSString   *atype;
100     WOResponse *response;
101     
102     sComponent = [_ctx component];
103     adata = [self->data     valueInComponent:sComponent];
104     atype = [self->mimeType stringValueInComponent:sComponent];
105     
106     response = [_ctx response];
107     
108     [response setContent:adata];
109     [response setHeader:atype ? atype : @"application/octet-stream"
110               forKey:@"content-type"];
111     
112     return response;
113   }
114   else {
115     [[_ctx session] debugWithFormat:
116                       @"no value configured for WOEmbeddedObject %@", self];
117     return nil;
118   }
119 }
120
121 #define StrVal(__x__) [self->__x__ stringValueInComponent:sComponent]
122
123 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
124   if (![[_ctx request] isFromClientComponent]) {
125     WOComponent *sComponent = [_ctx component];
126     NSString *uUri = [self->src      stringValueInComponent:sComponent];
127     NSString *uFi  = [self->filename stringValueInComponent:sComponent];
128     NSArray  *languages;
129
130     sComponent = [_ctx component];
131     uUri = [self->src      stringValueInComponent:sComponent];
132     uFi  = [self->filename stringValueInComponent:sComponent];
133     
134     WOResponse_AddCString(_response, "<embed src=\"");
135     
136     if ((self->data != nil) || (self->value != nil)) {
137       /* a component action link */
138       uUri = [_ctx componentActionURL];
139       if (uFi) {
140         uUri = [uUri stringByAppendingString:@"/"];
141         uUri = [uUri stringByAppendingString:uFi];
142       }
143       WOResponse_AddString(_response, uUri);
144     }
145     else if (uFi) {
146       WOResourceManager *rm;
147       NSString  *frameworkName;
148       
149       if ((rm = [[_ctx component] resourceManager]) == nil)
150         rm = [[_ctx application] resourceManager];
151       
152       /* If 'framework' binding is not set, use parent component's framework */
153       if (self->framework){
154         frameworkName = [self->framework stringValueInComponent:[_ctx component]];
155         if (frameworkName != nil && [frameworkName isEqualToString:@"app"])
156           frameworkName = nil;
157       }
158       else
159         frameworkName = [[_ctx component] frameworkName];
160       
161       languages = [_ctx hasSession]
162         ? [[_ctx session] languages]
163         : [[_ctx request] browserLanguages];
164       uFi = [rm urlForResourceNamed:uFi
165                 inFramework:frameworkName
166                 languages:languages
167                 request:[_ctx request]];
168       if (uFi == nil) {
169         NSLog(@"%@: did not find resource '%@'", sComponent,
170               [self->filename stringValueInComponent:sComponent]);
171         uFi = uUri;
172       }
173       [_response appendContentHTMLAttributeValue:uFi];
174     }
175     else if (uUri) {
176       [_response appendContentHTMLAttributeValue:uUri];
177     }
178     else {
179       [sComponent logWithFormat:@"missing resource URL for element %@", self];
180     }
181     
182     WOResponse_AddChar(_response, '"');
183   
184     [self appendExtraAttributesToResponse:_response inContext:_ctx];
185     if (self->otherTagString) {
186       WOResponse_AddChar(_response, ' ');
187       WOResponse_AddString(_response,
188                            [self->otherTagString stringValueInComponent:
189                                                    [_ctx component]]);
190     }
191     WOResponse_AddCString(_response, " />");
192   }
193 }
194
195 // description
196
197 - (NSString *)associationDescription {
198   NSMutableString *str = [[NSMutableString alloc] init];
199
200   if (self->filename)  [str appendFormat:@" filename=%@",  self->filename];
201   if (self->framework) [str appendFormat:@" framework=%@", self->framework];
202   if (self->src)       [str appendFormat:@" src=%@",       self->src];
203   if (self->value)     [str appendFormat:@" value=%@",     self->value];
204   
205   return AUTORELEASE(str);
206 }
207
208 @end /* WOEmbeddedObject */