#include <NGExtensions/NSString+Ext.h>
#include "common.h"
+/*
+ How is resource lookup supposed to work?
+
+ First, we need to determine whether the resource being asked for is a
+ resource contained in the product bundle. If thats the case, this resource
+ manager will take of it.
+
+ If not, there are two options:
+ a) the resource is from a different product, so we direct lookup to that
+ b) the resource is a "global" resource, so we ask the WOApplication manager
+*/
+
@interface WOResourceManager(UsedPrivates)
- (NSString *)webServerResourcesPath;
- (NSString *)resourcesPath;
return bundle;
}
-- (NSString *)pathForResourceNamed:(NSString *)_name
+- (WOResourceManager *)resourceManagerForResourceNamed:(NSString *)_name
inFramework:(NSString *)_frameworkName
- languages:(NSArray *)_languages
{
- // TODO: should we do acquisition? (hm, don't think so!, done in lookup)
- // but maybe we should not fall back to WOApplication resources
- WOResourceManager *rm;
SoProduct *bproduct;
NSBundle *bundle;
- NSString *path;
-
- if (debugOn) [self debugWithFormat:@"lookup resource: '%@'", _name];
/* determine product bundle or explicitly requested framework/bundle */
+
+ if ([_frameworkName length] == 0)
+ return self;
if ((bundle = [self bundleForFrameworkName:_frameworkName]) == nil)
- goto fallback;
+ return nil;
bproduct = [[SoProductRegistry sharedProductRegistry]
productForBundle:bundle];
- rm = [bproduct resourceManager];
if (debugOn) {
[self debugWithFormat:
- @" fwname %@\n bundle: %@\n product: %@",
+ @" fwname: '%@'\n bundle: '%@'\n product: '%@'",
_frameworkName, bundle, bproduct];
}
+ return (bproduct == self->product)
+ ? self : (id)[bproduct resourceManager];
+}
+
+- (NSString *)primaryLookupPathForResourceNamed:(NSString *)_name
+ languages:(NSArray *)_l
+{
+ NSString *path;
+
+ path = [[self->product bundle]
+ pathForResource:[_name stringByDeletingPathExtension]
+ ofType:[_name pathExtension]
+ inDirectory:@"Resources"
+ languages:_l];
+ if (debugOn && path == nil) {
+ [self debugWithFormat:@" resource %@/%@ not found.",
+ _name, [_l componentsJoinedByString:@","]];
+ }
+ return path;
+}
+
+- (NSString *)pathForResourceNamed:(NSString *)_name
+ inFramework:(NSString *)_frameworkName
+ languages:(NSArray *)_languages
+{
+ // TODO: should we do acquisition? (hm, don't think so!, done in lookup)
+ // but maybe we should not fall back to WOApplication resources
+ WOResourceManager *rm;
+ NSString *path;
- /* lookup resource in bundle */
+ if (debugOn) [self debugWithFormat:@"lookup resource: '%@'", _name];
- if (bproduct == self->product || rm == self) {
- path = [bundle pathForResource:[_name stringByDeletingPathExtension]
- ofType:[_name pathExtension]
- inDirectory:@"Resources"
- languages:_languages];
- if (debugOn && path == nil) {
- [self debugWithFormat:@" resource %@/%@ not found in bundle: %@",
- _name, [_languages componentsJoinedByString:@","], bundle];
- }
+ /* determine resource manager to be actually used */
+
+ rm = [self resourceManagerForResourceNamed:_name inFramework:_frameworkName];
+
+ /* lookup resource */
+
+ if (rm == self) {
+ path = [self primaryLookupPathForResourceNamed:_name languages:_languages];
}
- else {
- /* delegate lookup to resource manager of other product */
+ else if (rm != nil) {
+ /* delegate lookup to resource manager of other products */
path = [rm pathForResourceNamed:_name inFramework:_frameworkName
languages:_languages];
if (debugOn && path == nil)
[self debugWithFormat:@" resource %@ not found in rm: %@", _name, rm];
}
+ else
+ path = nil;
+
if (path != nil) {
if (debugOn) [self debugWithFormat:@" => found: %@", path];
return path;
}
- /* fall back to WOResourceManager lookup */
- fallback:
-
- rm = [self fallbackResourceManager];
- if (rm != nil) {
- return [rm pathForResourceNamed:_name inFramework:_frameworkName
- languages:_languages];
- }
- else {
- return [super pathForResourceNamed:_name inFramework:_frameworkName
- languages:_languages];
- }
+ /* fall back to global resource manager */
+
+ return [[self fallbackResourceManager]
+ pathForResourceNamed:_name inFramework:_frameworkName
+ languages:_languages];
}
/* generate URL for resources (eg filename binding in WOImage) */
return [[self fallbackResourceManager] webServerResourcesPath];
}
-- (NSString *)urlForResourceNamed:(NSString *)_name
- inFramework:(NSString *)_frameworkName
- languages:(NSArray *)_languages
- request:(WORequest *)_request
-{
- NSString *resource = nil, *tmp;
+- (NSString *)urlForProductRelativeResourcePath:(NSString *)resource {
+ NSString *tmp;
NSString *path = nil, *sbase;
unsigned len;
NSString *url;
- if (debugOn) [self debugWithFormat:@"lookup url: '%@'", _name];
-
- if (_languages == nil) _languages = [_request browserLanguages];
-
- resource = [self pathForResourceNamed:_name
- inFramework:_frameworkName
- languages:_languages];
-#if APPLE_Foundation_LIBRARY || NeXT_Foundation_LIBRARY
- if ([resource rangeOfString:@"/Contents/"].length > 0) {
- resource = [resource stringByReplacingString:@"/Contents"
- withString:@""];
- }
-#endif
-#if 0
- tmp = [resource stringByStandardizingPath];
- if (tmp) resource = tmp;
-#endif
-
- if (resource == nil) {
- if (debugOn) {
- [self debugWithFormat:@" => not found '%@' (fw=%@,langs=%@)",
- _name, _frameworkName, [_languages componentsJoinedByString:@","]];
- }
- return nil;
- }
-
sbase = self->base;
tmp = [sbase commonPrefixWithString:resource options:0];
if (![url hasSuffix:@"/"]) url = [url stringByAppendingString:@"/"];
url = [url stringByAppendingString:path];
+ return url;
+}
+
+- (NSString *)fixupResourcePath:(NSString *)resource {
+#if APPLE_Foundation_LIBRARY || NeXT_Foundation_LIBRARY
+ if ([resource rangeOfString:@"/Contents/"].length > 0) {
+ resource = [resource stringByReplacingString:@"/Contents"
+ withString:@""];
+ }
+#endif
+#if 0
+ if ((tmp = [resource stringByStandardizingPath]) != nil)
+ resource = tmp;
+#endif
+ return resource;
+}
+
+- (NSString *)urlForResourceNamed:(NSString *)_name
+ inFramework:(NSString *)_frameworkName
+ languages:(NSArray *)_languages
+ request:(WORequest *)_request
+{
+ WOResourceManager *rm;
+ NSString *url;
+
+ if (debugOn) [self debugWithFormat:@"lookup url: '%@'", _name];
+
+ if (_languages == nil) _languages = [_request browserLanguages];
+
+ /* determine resource manager to be actually used */
+
+ rm = [self resourceManagerForResourceNamed:_name inFramework:_frameworkName];
+
+ /* lookup resource */
+
+ url = nil;
+ if (rm == self) { /* handle it directly */
+ NSString *resource;
+
+ resource = [self primaryLookupPathForResourceNamed:_name
+ languages:_languages];
+ resource = [self fixupResourcePath:resource];
+ if (debugOn) [self debugWithFormat:@" resource to URL: %@", resource];
+
+ if (resource != nil) {
+ url = [self urlForProductRelativeResourcePath:resource];
+ }
+ else if (debugOn) {
+ [self debugWithFormat:@" => not found '%@' (fw=%@,langs=%@)",
+ _name, _frameworkName,
+ [_languages componentsJoinedByString:@","]];
+ }
+ }
+ else if (rm != nil) { /* delegate to other product */
+ if (debugOn) [self debugWithFormat:@" lookup with rm: %@", rm];
+ url = [rm urlForResourceNamed:_name inFramework:_frameworkName
+ languages:_languages request:_request];
+ }
+
+ if (url == nil) { /* fallback */
+ rm = [self fallbackResourceManager];
+ if (debugOn) [self debugWithFormat:@" fallback: %@", rm];
+ url = [rm urlForResourceNamed:_name inFramework:_frameworkName
+ languages:_languages request:_request];
+ }
if (debugOn) [self debugWithFormat:@" => '%@'", url];
return url;
}
+ (NSString *)gsTemplatesSubpath {
- static NSString *gsTemplatesSubpath = nil;
NSString *p;
- if (gsTemplatesSubpath != nil)
- return gsTemplatesSubpath;
-
p = [[WOApplication application] gsTemplatesDirectoryName];
p = [@"Library/" stringByAppendingString:p];
- gsTemplatesSubpath = [p copy];
- return gsTemplatesSubpath;
+ return p;
+}
++ (NSString *)gsWebSubpath {
+ NSString *p;
+
+ p = [[WOApplication application] gsWebDirectoryName];
+ p = [@"Library/" stringByAppendingString:p];
+ return p;
}
/* locate resource directories */
prefix = [[ud stringForKey:@"WOResourcePrefix"] copy];
wsPathes = [[self findResourceDirectoryPathesWithName:
- @"WebServerResources/" fhsName:@"www/"] copy];
+ [self gsWebSubpath] fhsName:@"www/"] copy];
if (debugOn)
NSLog(@"WebServerResources pathes: %@", wsPathes);
NSEnumerator *e;
NSString *path;
- // TODO: some kind of hack ... - need to decide how we want to deal with
- // the bundle less main component
- _appName = @"OpenGroupware10a";
-
if (debugOn) {
[self logWithFormat:@"lookup URL of resource: '%@'/%@/%@",
_name, _fwName, _lang];
[_langs componentsJoinedByString:@","]];
}
- appName = [(WOApplication *)[WOApplication application] name];
+ appName = [_request applicationName];
if (appName == nil)
- appName = [_request applicationName];
+ appName = [(WOApplication *)[WOApplication application] name];
/* check languages */
inFramework:_fwName
language:language
applicationName:appName];
- if (url) {
+ if (url != nil) {
if (debugOn) [self logWithFormat:@" FOUND: %@", url];
return url;
}
- (NSString *)gsTemplatesDirectoryName {
return [[self name] stringByAppendingString:@"/Templates/"];
}
+- (NSString *)gsWebDirectoryName {
+ return [[self name] stringByAppendingString:@"/WebServerResources/"];
+}
@end /* WOApplication(WEResourceManager) */