]> err.no Git - sope/blob - sope-appserver/NGObjWeb/DynamicElements/WOEmbeddedObject.m
7edefac1bca10ba48fea86515b439c7ecf759157
[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 "decommon.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:
110                 (atype != nil ? atype :(NSString *)@"application/octet-stream")
111               forKey:@"content-type"];
112     
113     return response;
114   }
115   else {
116     [[_ctx session] debugWithFormat:
117                       @"no value configured for WOEmbeddedObject %@", self];
118     return nil;
119   }
120 }
121
122 #define StrVal(__x__) [self->__x__ stringValueInComponent:sComponent]
123
124 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
125   if (![[_ctx request] isFromClientComponent]) {
126     WOComponent *sComponent = [_ctx component];
127     NSString *uUri = [self->src      stringValueInComponent:sComponent];
128     NSString *uFi  = [self->filename stringValueInComponent:sComponent];
129     NSArray  *languages;
130
131     sComponent = [_ctx component];
132     uUri = [self->src      stringValueInComponent:sComponent];
133     uFi  = [self->filename stringValueInComponent:sComponent];
134     
135     WOResponse_AddCString(_response, "<embed src=\"");
136     
137     if ((self->data != nil) || (self->value != nil)) {
138       /* a component action link */
139       uUri = [_ctx componentActionURL];
140       if (uFi) {
141         uUri = [uUri stringByAppendingString:@"/"];
142         uUri = [uUri stringByAppendingString:uFi];
143       }
144       WOResponse_AddString(_response, uUri);
145     }
146     else if (uFi) {
147       WOResourceManager *rm;
148       NSString  *frameworkName;
149       
150       if ((rm = [[_ctx component] resourceManager]) == nil)
151         rm = [[_ctx application] resourceManager];
152       
153       /* If 'framework' binding is not set, use parent component's framework */
154       if (self->framework){
155         frameworkName = [self->framework stringValueInComponent:[_ctx component]];
156         if (frameworkName != nil && [frameworkName isEqualToString:@"app"])
157           frameworkName = nil;
158       }
159       else
160         frameworkName = [[_ctx component] frameworkName];
161       
162       languages = [_ctx resourceLookupLanguages];
163       uFi       = [rm urlForResourceNamed:uFi
164                       inFramework:frameworkName
165                       languages:languages
166                       request:[_ctx request]];
167       if (uFi == nil) {
168         NSLog(@"%@: did not find resource '%@'", sComponent,
169               [self->filename stringValueInComponent:sComponent]);
170         uFi = uUri;
171       }
172       [_response appendContentHTMLAttributeValue:uFi];
173     }
174     else if (uUri) {
175       [_response appendContentHTMLAttributeValue:uUri];
176     }
177     else {
178       [sComponent logWithFormat:@"missing resource URL for element %@", self];
179     }
180     
181     WOResponse_AddChar(_response, '"');
182   
183     [self appendExtraAttributesToResponse:_response inContext:_ctx];
184     if (self->otherTagString) {
185       WOResponse_AddChar(_response, ' ');
186       WOResponse_AddString(_response,
187                            [self->otherTagString stringValueInComponent:
188                                                    [_ctx component]]);
189     }
190     WOResponse_AddEmptyCloseParens(_response, _ctx);
191   }
192 }
193
194 // description
195
196 - (NSString *)associationDescription {
197   NSMutableString *str = [[NSMutableString alloc] init];
198
199   if (self->filename)  [str appendFormat:@" filename=%@",  self->filename];
200   if (self->framework) [str appendFormat:@" framework=%@", self->framework];
201   if (self->src)       [str appendFormat:@" src=%@",       self->src];
202   if (self->value)     [str appendFormat:@" value=%@",     self->value];
203   
204   return AUTORELEASE(str);
205 }
206
207 @end /* WOEmbeddedObject */