From ca6ec91a49d27af1a33cf81d13bded4e02a7a9f1 Mon Sep 17 00:00:00 2001 From: fredkiefer Date: Sun, 1 Dec 2013 12:26:12 +0000 Subject: [PATCH] * Headers/Additions/GNUstepGUI/GSModelLoaderFactory.h, * Source/GSModelLoaderFactory.m: Add new method +supportedTypes. * Source/NSBundleAdditions.m: Use this new method to implement NIB resource detection on top of the base methods instead of doing it here. git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@37421 72102866-910b-0410-8b05-ffd578937521 --- ChangeLog | 7 ++ .../GNUstepGUI/GSModelLoaderFactory.h | 8 ++- Source/GSModelLoaderFactory.m | 21 +++++- Source/NSBundleAdditions.m | 70 ++++++++----------- 4 files changed, 61 insertions(+), 45 deletions(-) diff --git a/ChangeLog b/ChangeLog index 82b17f0a7..babe0465f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2013-12-01 Fred Kiefer + + * Headers/Additions/GNUstepGUI/GSModelLoaderFactory.h, + * Source/GSModelLoaderFactory.m: Add new method +supportedTypes. + * Source/NSBundleAdditions.m: Use this new method to implement NIB + resource detection on top of the base methods instead of doing it here. + 2013-11-24 Fred Kiefer * Source/NSGradient.m: Rearrange code to avoid duplicate diff --git a/Headers/Additions/GNUstepGUI/GSModelLoaderFactory.h b/Headers/Additions/GNUstepGUI/GSModelLoaderFactory.h index bca3c8a78..157720402 100644 --- a/Headers/Additions/GNUstepGUI/GSModelLoaderFactory.h +++ b/Headers/Additions/GNUstepGUI/GSModelLoaderFactory.h @@ -32,6 +32,7 @@ #import #import +@class NSArray; @class NSData; @class NSDictionary; @class NSString; @@ -51,10 +52,11 @@ @interface GSModelLoaderFactory : NSObject + (void) registerModelLoaderClass: (Class)aClass; -+ (Class)classForType: (NSString *)type; ++ (Class) classForType: (NSString *)type; ++ (NSArray *) supportedTypes; + (NSString *) supportedModelFileAtPath: (NSString *)modelPath; -+ (GSModelLoader *)modelLoaderForFileType: (NSString *)type; -+ (GSModelLoader *)modelLoaderForFileName: (NSString *)modelPath; ++ (GSModelLoader *) modelLoaderForFileType: (NSString *)type; ++ (GSModelLoader *) modelLoaderForFileName: (NSString *)modelPath; @end #endif diff --git a/Source/GSModelLoaderFactory.m b/Source/GSModelLoaderFactory.m index 3965e5f38..4d979dae4 100644 --- a/Source/GSModelLoaderFactory.m +++ b/Source/GSModelLoaderFactory.m @@ -130,6 +130,23 @@ static NSMutableDictionary *_modelMap = nil; return [_modelMap objectForKey: type]; } ++ (NSArray *) supportedTypes +{ + NSArray *objectArray = [_modelMap allValues]; + NSArray *sortedArray = [objectArray sortedArrayUsingSelector: + @selector(_comparePriority:)]; + NSEnumerator *oen = [sortedArray objectEnumerator]; + Class cls = nil; + NSMutableArray *types = [[NSMutableArray alloc] init]; + + while ((cls = [oen nextObject]) != nil) + { + [types addObject: [cls type]]; + } + + return AUTORELEASE(types); +} + + (NSString *) supportedModelFileAtPath: (NSString *)modelPath { NSString *result = nil; @@ -168,7 +185,7 @@ static NSMutableDictionary *_modelMap = nil; return result; } -+ (GSModelLoader *)modelLoaderForFileType: (NSString *)type ++ (GSModelLoader *) modelLoaderForFileType: (NSString *)type { Class aClass = [GSModelLoaderFactory classForType: type]; GSModelLoader *loader = nil; @@ -186,7 +203,7 @@ static NSMutableDictionary *_modelMap = nil; return loader; } -+ (GSModelLoader *)modelLoaderForFileName: (NSString *)modelPath ++ (GSModelLoader *) modelLoaderForFileName: (NSString *)modelPath { NSString *path = [GSModelLoaderFactory supportedModelFileAtPath: modelPath]; GSModelLoader *result = nil; diff --git a/Source/NSBundleAdditions.m b/Source/NSBundleAdditions.m index 3c803e294..edb5ba5df 100644 --- a/Source/NSBundleAdditions.m +++ b/Source/NSBundleAdditions.m @@ -34,6 +34,7 @@ #import #import #import +#import #import #import #import @@ -281,53 +282,42 @@ - (NSString *) pathForNibResource: (NSString *)fileName { - NSMutableArray *array = [NSMutableArray arrayWithCapacity: 8]; - NSArray *languages; - NSString *rootPath = [self bundlePath]; - NSString *primary; - NSString *language; - NSEnumerator *enumerator; + NSEnumerator *enumerator; + NSArray *types = [GSModelLoaderFactory supportedTypes]; + NSString *ext = [fileName pathExtension]; - languages = [[NSUserDefaults standardUserDefaults] - stringArrayForKey: @"NSLanguages"]; - - /* - * Build an array of resource paths that differs from the normal order - - * we want a localized file in preference to a generic one. - */ - primary = [rootPath stringByAppendingPathComponent: @"Resources"]; - enumerator = [languages objectEnumerator]; - while ((language = [enumerator nextObject])) + NSDebugLLog(@"NIB", @"Path for NIB file %@", fileName); + if ((ext == nil) || [ext isEqualToString:@""]) { - NSString *langDir; + NSString *type; - langDir = [NSString stringWithFormat: @"%@.lproj", language]; - [array addObject: [primary stringByAppendingPathComponent: langDir]]; + enumerator = [types objectEnumerator]; + while ((type = [enumerator nextObject])) + { + NSDebugLLog(@"NIB", @"Checking type %@", fileName); + NSString *path = [self pathForResource: fileName + ofType: type]; + if (path != nil) + { + return path; + } + } } - [array addObject: primary]; - primary = rootPath; - enumerator = [languages objectEnumerator]; - while ((language = [enumerator nextObject])) + else { - NSString *langDir; - - langDir = [NSString stringWithFormat: @"%@.lproj", language]; - [array addObject: [primary stringByAppendingPathComponent: langDir]]; - } - [array addObject: primary]; - - enumerator = [array objectEnumerator]; - while ((rootPath = [enumerator nextObject]) != nil) - { - NSString *modelPath = [rootPath stringByAppendingPathComponent: fileName]; - NSString *path = [GSModelLoaderFactory supportedModelFileAtPath: modelPath]; - - if (path != nil) - { - return path; - } + if ([types containsObject: ext]) + { + NSString *path = [self pathForResource: + [fileName stringByDeletingPathExtension] + ofType: ext]; + if (path != nil) + { + return path; + } + } } + NSDebugLLog(@"NIB", @"Did not find NIB resource %@", fileName); return nil; }