2 Copyright (C) 2000-2004 SKYRIX Software AG
4 This file is part of OpenGroupware.org.
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
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.
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
22 #include "WOHyperlink.h"
26 [WOHTMLDynamicElement]
32 _WODirectActionHyperlink
35 @interface _WOComplexHyperlink : WOHyperlink
37 /* superclass of most hyperlink classes */
39 WOAssociation *fragmentIdentifier;
40 WOAssociation *string;
41 WOAssociation *target;
42 WOAssociation *disabled;
46 WOAssociation *queryDictionary;
47 NSDictionary *queryParameters; /* associations beginning with ? */
49 /* non WO, image stuff */
50 WOAssociation *filename; /* path relative to WebServerResources */
51 WOAssociation *framework;
52 WOAssociation *src; /* absolute URL */
53 WOAssociation *disabledFilename; /* icon for 'disabled' state */
56 - (NSString *)associationDescription;
60 @interface _WOHrefHyperlink : _WOComplexHyperlink
66 @interface _WOActionHyperlink : _WOComplexHyperlink
68 WOAssociation *action;
72 @interface _WOPageHyperlink : _WOComplexHyperlink
74 WOAssociation *pageName;
78 @interface _WODirectActionHyperlink : _WOComplexHyperlink
80 WOAssociation *actionClass;
81 WOAssociation *directActionName;
82 BOOL sidInUrl; /* include session-id in wa URL ? */
86 #include "WOElement+private.h"
87 #include "WOHyperlinkInfo.h"
88 #include "WOCompoundElement.h"
89 #include <NGObjWeb/WOApplication.h>
90 #include <NGObjWeb/WOResourceManager.h>
91 #include <NGExtensions/NSString+Ext.h>
94 static Class NSURLClass = Nil;
96 @implementation _WOComplexHyperlink
99 return [super version] /* v4 */;
102 NSAssert2([super version] == 4,
103 @"invalid superclass (%@) version %i !",
104 NSStringFromClass([self superclass]), [super version]);
105 if (NSURLClass == Nil)
106 NSURLClass = [NSURL class];
109 - (id)initWithName:(NSString *)_name
110 hyperlinkInfo:(WOHyperlinkInfo *)_info
111 template:(WOElement *)_t
113 if ((self = [super initWithName:_name hyperlinkInfo:_info template:_t])) {
114 self->template = [_t retain];
116 self->fragmentIdentifier = _info->fragmentIdentifier;
117 self->string = _info->string;
118 self->target = _info->target;
119 self->disabled = _info->disabled;
120 self->queryDictionary = _info->queryDictionary;
121 self->queryParameters = _info->queryParameters;
124 self->filename = _info->filename;
125 self->framework = _info->framework;
126 self->src = _info->src;
127 self->disabledFilename = _info->disabledFilename;
129 self->containsForm = self->queryParameters ? YES : NO;
135 [self->template release];
136 [self->queryDictionary release];
137 [self->queryParameters release];
138 [self->disabledFilename release];
139 [self->filename release];
140 [self->framework release];
142 [self->fragmentIdentifier release];
143 [self->string release];
144 [self->target release];
145 [self->disabled release];
151 - (WOElement *)template {
152 return self->template;
155 // ******************** responder ********************
157 - (void)takeValuesFromRequest:(WORequest *)_req inContext:(WOContext *)_ctx {
158 /* links can take form values !!!! (for query-parameters) */
160 if (self->queryParameters) {
161 /* apply values to ?style parameters */
162 WOComponent *sComponent = [_ctx component];
166 keys = [self->queryParameters keyEnumerator];
167 while ((key = [keys nextObject])) {
170 assoc = [self->queryParameters objectForKey:key];
172 if ([assoc isValueSettable]) {
173 value = [_req formValueForKey:key];
174 [assoc setValue:value inComponent:sComponent];
179 [self->template takeValuesFromRequest:_req inContext:_ctx];
182 - (id)invokeActionForRequest:(WORequest *)_request
183 inContext:(WOContext *)_ctx
185 if (self->disabled) {
186 if ([self->disabled boolValueInComponent:[_ctx component]])
190 if (![[_ctx elementID] isEqualToString:[_ctx senderID]])
191 /* link is not the active element */
192 return [self->template invokeActionForRequest:_request inContext:_ctx];
195 [[_ctx session] logWithFormat:@"%@[0x%08X]: no action/page set !",
196 NSStringFromClass([self class]), self];
200 - (BOOL)_appendHrefToResponse:(WOResponse *)_resp inContext:(WOContext *)_ctx {
201 [self subclassResponsibility:_cmd];
205 - (void)_addImageToResponse:(WOResponse *)_resp inContext:(WOContext *)_ctx {
206 WOComponent *sComponent = [_ctx component];
211 uUri = [[self->src valueInContext:_ctx] stringValue];
213 if ([self->disabled boolValueInComponent:sComponent]) {
214 uFi = [self->disabledFilename stringValueInComponent:sComponent];
216 uFi = [self->filename stringValueInComponent:sComponent];
219 uFi = [self->filename stringValueInComponent:sComponent];
221 if (!((uFi != nil) || (uUri != nil)))
224 languages = [_ctx hasSession]
225 ? [[_ctx session] languages]
226 : [[_ctx request] browserLanguages];
228 WOResponse_AddCString(_resp, "<img src=\"");
231 WOResourceManager *rm;
233 if ((rm = [[_ctx component] resourceManager]) == nil)
234 rm = [[_ctx application] resourceManager];
236 uFi = [rm urlForResourceNamed:uFi
238 [self->framework stringValueInComponent:sComponent]
240 request:[_ctx request]];
242 NSLog(@"%@: did not find resource %@", sComponent,
243 [self->filename stringValueInComponent:sComponent]);
246 [_resp appendContentHTMLAttributeValue:uFi];
249 [_resp appendContentHTMLAttributeValue:uUri];
251 WOResponse_AddChar(_resp, '"');
253 [self appendExtraAttributesToResponse:_resp inContext:_ctx];
254 WOResponse_AddCString(_resp, " />");
257 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
258 WOComponent *sComponent = [_ctx component];
260 NSString *targetView;
261 NSString *queryString = nil;
263 if ([[_ctx request] isFromClientComponent])
266 content = [self->string valueInContext:_ctx];
267 targetView = [self->target stringValueInComponent:sComponent];
269 WOResponse_AddCString(_response, "<a href=\"");
271 if ([self _appendHrefToResponse:_response inContext:_ctx]) {
272 queryString = [self queryStringForQueryDictionary:
273 [self->queryDictionary valueInComponent:sComponent]
274 andQueryParameters:self->queryParameters
278 if (self->fragmentIdentifier) {
279 [_response appendContentCharacter:'#'];
280 WOResponse_AddString(_response,
281 [self->fragmentIdentifier stringValueInComponent:sComponent]);
284 [_response appendContentCharacter:'?'];
285 WOResponse_AddString(_response, queryString);
287 [_response appendContentCharacter:'"'];
290 WOResponse_AddCString(_response, " target=\"");
291 WOResponse_AddString(_response, targetView);
292 [_response appendContentCharacter:'"'];
295 [self appendExtraAttributesToResponse:_response inContext:_ctx];
297 if (self->otherTagString) {
298 WOResponse_AddChar(_response, ' ');
299 WOResponse_AddString(_response,
300 [self->otherTagString stringValueInComponent:
303 [_response appendContentCharacter:'>'];
306 [self->template appendToResponse:_response inContext:_ctx];
307 if (content) [_response appendContentHTMLString:content];
310 if ((self->src != nil) || (self->filename != nil))
311 [self _addImageToResponse:_response inContext:_ctx];
314 WOResponse_AddCString(_response, "</a>");
319 - (NSString *)associationDescription {
320 NSMutableString *str = [NSMutableString stringWithCapacity:256];
322 if (self->fragmentIdentifier)
323 [str appendFormat:@" fragment=%@", self->fragmentIdentifier];
324 if (self->string) [str appendFormat:@" string=%@", self->string];
325 if (self->target) [str appendFormat:@" target=%@", self->target];
326 if (self->disabled) [str appendFormat:@" disabled=%@", self->disabled];
329 if (self->filename) [str appendFormat:@" filename=%@", self->filename];
330 if (self->framework) [str appendFormat:@" framework=%@", self->framework];
331 if (self->src) [str appendFormat:@" src=%@", self->src];
336 @end /* _WOComplexHyperlink */
338 @implementation _WOHrefHyperlink
340 static BOOL debugStaticLinks = NO;
343 NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
345 debugStaticLinks = [ud boolForKey:@"WODebugStaticLinkProcessing"];
348 - (id)initWithName:(NSString *)_name
349 hyperlinkInfo:(WOHyperlinkInfo *)_info
350 template:(WOElement *)_t
352 if ((self = [super initWithName:_name hyperlinkInfo:_info template:_t])) {
353 self->href = _info->href;
359 [self->href release];
365 - (BOOL)shouldRewriteURLString:(NSString *)_s inContext:(WOContext *)_ctx {
366 // TODO: we need a binding to disable rewriting!
367 if ([_s hasPrefix:@"mailto:"])
369 if ([_s hasPrefix:@"javascript:"])
374 - (BOOL)_appendHrefToResponse:(WOResponse *)_r inContext:(WOContext *)_ctx {
379 base = [_ctx baseURL];
380 hrefValue = [self->href valueInContext:_ctx];
383 if (hrefValue == nil)
386 if ([hrefValue isKindOfClass:NSURLClass]) {
387 s = [hrefValue stringValueRelativeToURL:base];
390 s = [hrefValue stringValue];
392 if ([self shouldRewriteURLString:s inContext:_ctx]) {
393 if ([s isAbsoluteURL]) {
394 // TODO: why are we doing this? we could just pass through the string?
395 // => probably to generate relative links
396 url = [NSURLClass URLWithString:s];
398 else if (base != nil) {
399 /* avoid creating a new URL for ".", just return the base */
400 url = [s isEqualToString:@"."]
402 : [NSURLClass URLWithString:s relativeToURL:base];
405 [self logWithFormat:@"WARNING: missing base URL in context ..."];
406 WOResponse_AddString(_r, s);
412 @"couldn't construct URL from 'href' string '%@' (base=%@)",
417 s = [url stringValueRelativeToURL:base];
423 if (debugStaticLinks) {
424 [self logWithFormat:@"static links based on 'href': '%@'", hrefValue];
425 [self logWithFormat:@" base %@", base];
426 [self logWithFormat:@" base-abs %@", [base absoluteString]];
427 [self logWithFormat:@" url %@", url];
428 [self logWithFormat:@" url-abs %@", [url absoluteString]];
429 [self logWithFormat:@" string %@", s];
431 WOResponse_AddString(_r, s);
437 - (NSString *)associationDescription {
438 NSMutableString *str = [NSMutableString stringWithCapacity:256];
440 [str appendFormat:@" href=%@", self->href];
441 [str appendString:[super associationDescription]];
446 @end /* _WOHrefHyperlink */
448 @implementation _WOActionHyperlink
450 - (id)initWithName:(NSString *)_name
451 hyperlinkInfo:(WOHyperlinkInfo *)_info
452 template:(WOElement *)_t
454 if ((self = [super initWithName:_name hyperlinkInfo:_info template:_t])) {
455 self->action = _info->action;
461 [self->action release];
465 /* dynamic invocation */
467 - (id)invokeActionForRequest:(WORequest *)_request
468 inContext:(WOContext *)_ctx
470 if (self->disabled) {
471 if ([self->disabled boolValueInComponent:[_ctx component]])
475 if (![[_ctx elementID] isEqualToString:[_ctx senderID]])
476 /* link is not the active element */
477 return [self->template invokeActionForRequest:_request inContext:_ctx];
480 return [self executeAction:self->action inContext:_ctx];
483 - (BOOL)_appendHrefToResponse:(WOResponse *)_response
484 inContext:(WOContext *)_ctx
486 WOResponse_AddString(_response, [_ctx componentActionURL]);
492 - (NSString *)associationDescription {
493 NSMutableString *str = [NSMutableString stringWithCapacity:256];
495 [str appendFormat:@" action=%@", self->action];
496 [str appendString:[super associationDescription]];
500 @end /* _WOActionHyperlink */
502 @implementation _WOPageHyperlink
504 - (id)initWithName:(NSString *)_name
505 hyperlinkInfo:(WOHyperlinkInfo *)_info
506 template:(WOElement *)_t
508 if ((self = [super initWithName:_name hyperlinkInfo:_info template:_t])) {
509 self->pageName = _info->pageName;
515 [self->pageName release];
521 - (id)invokeActionForRequest:(WORequest *)_request
522 inContext:(WOContext *)_ctx
527 if (self->disabled) {
528 if ([self->disabled boolValueInComponent:[_ctx component]])
532 if (![[_ctx elementID] isEqualToString:[_ctx senderID]])
533 /* link is not the active element */
534 return [self->template invokeActionForRequest:_request inContext:_ctx];
536 /* link is the active element */
538 name = [self->pageName stringValueInComponent:[_ctx component]];
539 page = [[_ctx application] pageWithName:name inContext:_ctx];
542 [[_ctx session] logWithFormat:
543 @"%@[0x%08X]: did not find page with name %@ !",
544 NSStringFromClass([self class]), self, name];
549 - (BOOL)_appendHrefToResponse:(WOResponse *)_response
550 inContext:(WOContext *)_ctx
554 87% -componentActionURL
555 13% NSString dataUsingEncoding(appendContentString!)
556 TODO(prof): use addcstring
558 WOResponse_AddString(_response, [_ctx componentActionURL]);
564 - (NSString *)associationDescription {
565 NSMutableString *str = [NSMutableString stringWithCapacity:256];
567 [str appendFormat:@" pageName=%@", self->pageName];
568 [str appendString:[super associationDescription]];
572 @end /* _WOPageHyperlink */
574 @implementation _WODirectActionHyperlink
576 - (id)initWithName:(NSString *)_name
577 hyperlinkInfo:(WOHyperlinkInfo *)_info
578 template:(WOElement *)_t
580 if ((self = [super initWithName:_name hyperlinkInfo:_info template:_t])) {
581 self->actionClass = _info->actionClass;
582 self->directActionName = _info->directActionName;
583 self->sidInUrl = _info->sidInUrl;
585 self->containsForm = NO; /* direct actions are never form stuff ... */
591 [self->actionClass release];
592 [self->directActionName release];
598 - (void)takeValuesFromRequest:(WORequest *)_req inContext:(WOContext *)_ctx {
599 /* DA links can *never* take form values !!!! */
600 [self->template takeValuesFromRequest:_req inContext:_ctx];
603 - (id)invokeActionForRequest:(WORequest *)_request
604 inContext:(WOContext *)_ctx
606 /* DA links can *never* invoke an action !!!! */
607 return [self->template invokeActionForRequest:_request inContext:_ctx];
610 - (BOOL)_appendHrefToResponse:(WOResponse *)_response
611 inContext:(WOContext *)_ctx
613 WOComponent *sComponent;
616 NSMutableDictionary *qd;
619 sComponent = [_ctx component];
620 daClass = [self->actionClass stringValueInComponent:sComponent];
621 daName = [self->directActionName stringValueInComponent:sComponent];
625 if (![daClass isEqualToString:@"DirectAction"])
626 daName = [NSString stringWithFormat:@"%@/%@", daClass, daName];
632 qd = [NSMutableDictionary dictionaryWithCapacity:16];
634 /* add query dictionary */
636 if (self->queryDictionary) {
637 if ((tmp = [self->queryDictionary valueInComponent:sComponent]))
638 [qd addEntriesFromDictionary:tmp];
641 /* add ?style parameters */
643 if (self->queryParameters) {
647 keys = [self->queryParameters keyEnumerator];
648 while ((key = [keys nextObject])) {
651 assoc = [self->queryParameters objectForKey:key];
652 value = [assoc stringValueInComponent:sComponent];
654 [qd setObject:(value ? value : @"") forKey:key];
660 if (self->sidInUrl) {
661 if ([_ctx hasSession]) {
665 [qd setObject:[sn sessionID] forKey:WORequestValueSessionID];
667 if (![sn isDistributionEnabled]) {
668 [qd setObject:[[WOApplication application] number]
669 forKey:WORequestValueInstance];
674 WOResponse_AddString(_response,
675 [_ctx directActionURLForActionNamed:daName
676 queryDictionary:qd]);
682 - (NSString *)associationDescription {
683 NSMutableString *str = [NSMutableString stringWithCapacity:256];
685 if (self->actionClass)
686 [str appendFormat:@" actionClass=%@", self->actionClass];
687 if (self->directActionName)
688 [str appendFormat:@" directAction=%@", self->directActionName];
690 [str appendString:[super associationDescription]];
694 @end /* _WODirectActionHyperlink */