/*
- Copyright (C) 2000-2004 SKYRIX Software AG
+ Copyright (C) 2000-2006 SKYRIX Software AG
+ Copyright (C) 2006 Helge Hess
- This file is part of OpenGroupware.org.
+ This file is part of SOPE.
- OGo is free software; you can redistribute it and/or modify it under
+ SOPE is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.
- OGo is distributed in the hope that it will be useful, but WITHOUT ANY
+ SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public
- License along with OGo; see the file COPYING. If not, write to the
+ License along with SOPE; see the file COPYING. If not, write to the
Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.
*/
#include <NGObjWeb/WORequest.h>
#include <NGObjWeb/WOResponse.h>
#include <NGObjWeb/WOSession.h>
-#import <EOControl/EONull.h>
+#include <Foundation/NSNull.h>
#include "WOElementID.h"
#include "common.h"
#include <time.h>
@implementation WOContext
+ (int)version {
- return 7;
-}
-
-static Class MutableStrClass = Nil;
-static int contextCount = 0;
-static int logComponents = -1;
-static int relativeURLs = -1;
-static BOOL debugOn = NO;
-static int debugCursor = -1;
-static BOOL debugComponentAwake = NO;
-static BOOL testNSURLs = NO;
-static BOOL newCURLStyle = NO;
+ return 8;
+}
+
+static Class WOContextClass = Nil;
+static Class MutableStrClass = Nil;
+static int contextCount = 0;
+static int logComponents = -1;
+static int relativeURLs = -1;
+static BOOL debugOn = NO;
+static int debugCursor = -1;
+static BOOL debugComponentAwake = NO;
+static BOOL testNSURLs = NO;
+static BOOL newCURLStyle = NO;
static NSString *WOApplicationSuffix = nil;
+ (void)initialize {
- NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
- static BOOL didInitialize = NO;
- if (didInitialize) return;
+ static BOOL didInit = NO;
+ NSUserDefaults *ud;
+ NSString *cn;
+
+ if (didInit) return;
+
+ didInit = YES;
+
+ ud = [NSUserDefaults standardUserDefaults];
if (WOAppClass == Nil)
WOAppClass = [WOApplication class];
if (MutableStrClass == Nil)
MutableStrClass = [NSMutableString class];
- didInitialize = YES;
-
+ cn = [ud stringForKey:@"WOContextClass"];
+ WOContextClass = NSClassFromString(cn);
+ NSAssert1(WOContextClass != Nil,
+ @"Couldn't instantiate WOContextClass (%@)!", cn);
+
logComponents = [[ud objectForKey:@"WOLogComponents"] boolValue] ? 1 : 0;
relativeURLs = [[ud objectForKey:@"WOUseRelativeURLs"] boolValue]? 1 : 0;
debugCursor = [ud boolForKey:@"WODebugCursor"] ? 1 : 0;
WOApplicationSuffix = [[ud stringForKey:@"WOApplicationSuffix"] copy];
}
-+ (id)contextWithRequest:(WORequest *)_request {
- return [[(WOContext *)[self alloc] initWithRequest:_request] autorelease];
++ (id)contextWithRequest:(WORequest *)_r {
+ return [[(WOContext *)[WOContextClass alloc] initWithRequest:_r] autorelease];
}
- (id)initWithRequest:(WORequest *)_request {
if ((self = [super init])) {
- unsigned char buf[24];
- self->qpJoin = @"&";
+ char buf[24];
+ self->qpJoin = @"&";
- sprintf(buf, "%03x%08x%08x", ++contextCount, (int)time(NULL), (int)self);
+ sprintf(buf, "%03x%08x%08x", ++contextCount, (int)time(NULL),
+ (unsigned int)(unsigned long)self);
self->ctxId = [[NSString alloc] initWithCString:buf];
+ /* per default close tags in XML style */
+ self->wcFlags.xmlStyleEmptyElements = 1;
+ self->wcFlags.allowEmptyAttributes = 0;
+
self->elementID = [[WOElementID alloc] init];
self->awakeComponents = [[NSMutableSet alloc] initWithCapacity:64];
[super dealloc];
}
+/* session */
+
- (void)setSession:(WOSession *)_session {
ASSIGN(self->session, _session);
}
+- (void)setNewSession:(WOSession *)_session {
+ [self setSession:_session];
+ self->wcFlags.hasNewSession = 1;
+}
-- (WOSession *)session {
- // in WO4 -session creates a new session if none is associated
+- (id)session {
+ /* in WO4 -session creates a new session if none is associated */
if (self->session == nil) {
[[self application] _initializeSessionInContext:self];
- if (self->session == nil)
+ if (self->session == nil) {
[self logWithFormat:@"%s: missing session for context ..",
__PRETTY_FUNCTION__];
+ }
}
return self->session;
- (BOOL)hasSession {
return (self->session != nil) ? YES : NO;
}
+- (BOOL)hasNewSession {
+ if (!self->wcFlags.hasNewSession)
+ return NO;
+ return [self hasSession];
+}
- (BOOL)savePageRequired {
- return self->savePageRequired;
+ return self->wcFlags.savePageRequired ? YES : NO;
}
/* cursors */
/* components */
-- (WOComponent *)component {
+- (id)component {
return (self->componentStackCount > 0)
? self->componentStack[self->componentStackCount - 1]
: nil;
[_page ensureAwakeInContext:self];
ASSIGN(self->page, _page);
}
-- (WOComponent *)page {
+- (id)page {
return self->page;
}
/* forms */
- (void)setInForm:(BOOL)_form {
- self->inForm = _form;
+ self->wcFlags.inForm = _form ? 1 : 0;
}
- (BOOL)isInForm {
- return self->inForm;
+ return self->wcFlags.inForm ? YES : NO;
}
- (void)addActiveFormElement:(WOElement *)_formElement {
return;
else if (WOGetKVCGetMethod(self, _key) == NULL) {
if (_value == nil)
- _value = [EONull null];
+ _value = [NSNull null];
if (self->variables == nil) {
self->variables =
sid = [[self session] sessionID];
return [NSString stringWithFormat:
- @"<0x%08X[%@]: %@ app=%@ sn=%@ eid=%@ rqeid=%@>",
- (unsigned)self, NSStringFromClass([self class]),
+ @"<0x%p[%@]: %@ app=%@ sn=%@ eid=%@ rqeid=%@>",
+ self, NSStringFromClass([self class]),
[self contextID],
[app name],
- sid ? sid : @"none",
+ sid != nil ? sid : (NSString *)@"none",
[self elementID],
[self senderID]];
}
/* described in Apple TIL article 70101 */
}
+- (void)setQueryPathSeparator:(NSString *)_sp {
+ ASSIGNCOPY(self->qpJoin, _sp);
+}
+- (NSString *)queryPathSeparator {
+ return self->qpJoin;
+}
+
+- (void)setGenerateXMLStyleEmptyElements:(BOOL)_flag {
+ self->wcFlags.xmlStyleEmptyElements = _flag ? 1 : 0;
+}
+- (BOOL)generateXMLStyleEmptyElements {
+ return self->wcFlags.xmlStyleEmptyElements ? YES : NO;
+}
+
+- (void)setGenerateEmptyAttributes:(BOOL)_flag {
+ self->wcFlags.allowEmptyAttributes = _flag ? 1 : 0;
+}
+- (BOOL)generateEmptyAttributes {
+ return self->wcFlags.allowEmptyAttributes ? YES : NO;
+}
+
- (NSString *)queryStringFromDictionary:(NSDictionary *)_queryDict {
NSEnumerator *keys;
NSString *key;
qs = [MutableStrClass stringWithCapacity:256];
keys = [_queryDict keyEnumerator];
- for (isFirst = YES; (key = [keys nextObject]); ) {
- NSString *value;
+ for (isFirst = YES; (key = [keys nextObject]) != nil; ) {
+ id value;
- if (isFirst)
- isFirst = NO;
- else
- [qs appendString:self->qpJoin];
+ value = [_queryDict objectForKey:key];
+
+ /* check for multi-value parameter */
+
+ if ([value isKindOfClass:[NSArray class]]) {
+ NSArray *a = value;
+ unsigned i, count;
+
+ for (i = 0, count = [a count]; i < count; i++) {
+ value = [a objectAtIndex:i];
+
+ if (isFirst) isFirst = NO;
+ else [qs appendString:self->qpJoin];
+
+ // TODO: code duplication ...
+ value = ![value isNotNull] ? (NSString *)nil : [value stringValue];
+ key = [key stringByEscapingURL];
+ value = [value stringByEscapingURL];
+
+ [qs appendString:key];
+ if (value != nil) {
+ [qs appendString:@"="];
+ [qs appendString:value];
+ }
+ }
+ continue;
+ }
+
+ /* regular, single-value parameter */
- value = [[_queryDict objectForKey:key] stringValue];
+ if (isFirst) isFirst = NO;
+ else [qs appendString:self->qpJoin];
+ value = ![value isNotNull] ? (NSString *)nil : [value stringValue];
key = [key stringByEscapingURL];
value = [value stringByEscapingURL];
[qs appendString:key];
- if (value) {
+ if (value != nil) {
[qs appendString:@"="];
[qs appendString:value];
}
// 26% -urlWithRequestHandler...
// 21% -elementID (was 40% !! :-)
// ~20% mutable string ops
+
+ /*
+ This makes the request handler save the page in the session at the
+ end of the request (only necessary if the page generates URLs which
+ refer the context).
+ */
+ self->wcFlags.savePageRequired = 1;
+
if (newCURLStyle) {
+ // TODO: who uses that? Its not enabled per default
+ // TODO: what does this do?
NSMutableString *qs;
NSString *p;
- self->savePageRequired = YES;
qs = [MutableStrClass stringWithCapacity:64];
[qs appendString:WORequestValueSenderID];
static NSMutableString *url = nil; // THREAD
static IMP addStr = NULL;
NSString *s;
-
- self->savePageRequired = YES;
+ NSString *coRqhKey;
+
+ coRqhKey = [WOAppClass componentRequestHandlerKey];
+
+ /*
+ Optimization: use relative URL if the request already was a component
+ action (with a valid session)
+ */
+ if (!self->wcFlags.hasNewSession) {
+ if ([[self->request requestHandlerKey] isEqualToString:coRqhKey])
+ return [self->elementID elementID];
+ }
+
if (url == nil) {
url = [[MutableStrClass alloc] initWithCapacity:256];
addStr = [url methodForSelector:@selector(appendString:)];
}
else
[url setString:@"/"];
-
+
/*
Note: component actions *always* require sessions to be able to locate
the request component !
addStr(url, @selector(appendString:), @"/");
addStr(url, @selector(appendString:), [self->elementID elementID]);
- s = [self urlWithRequestHandlerKey:
- [WOAppClass componentRequestHandlerKey]
+ s = [self urlWithRequestHandlerKey:coRqhKey
path:url queryString:nil];
return s;
}
#endif
}
+/* languages for resource lookup (non-WO) */
+
+- (NSArray *)resourceLookupLanguages {
+ return [self hasSession]
+ ? [[self session] languages]
+ : [[self request] browserLanguages];
+}
+
+
/* DeprecatedMethodsInWO4 */
-- (WOApplication *)application {
+- (id)application {
if (self->application == nil)
self->application = [WOAppClass application];
- if (self->application == nil)
- NSLog(@"%s: missing application for context %@", __PRETTY_FUNCTION__, self);
+ if (self->application == nil) {
+ [self logWithFormat:
+ @"%s: missing application for context %@",
+ __PRETTY_FUNCTION__, self];
+ }
return self->application;
}
return url;
}
-@end /* WOContext(DeprecatedMethodsInWO4) */
-
+@end /* WOContext */
@implementation WOComponent(Cursors)
- (void)pushCursor:(id)_obj {
- NSMutableArray *ctxStack;
-
if (debugCursor)
[self logWithFormat:@"enter cursor: %@", _obj];
- if ((ctxStack = self->cycleContext) == nil)
+ if (!self->cycleContext)
self->cycleContext = [[NSMutableArray alloc] initWithCapacity:8];
/* add to cursor stack */
- [ctxStack addObject:(_obj ? _obj : [NSNull null])];
+ [self->cycleContext addObject:(_obj != nil ? _obj : (id)[NSNull null])];
/* set active cursor */
- if (![_obj isNotNull]) _obj = nil;
[self setObject:_obj forKey:@"_"];
}