Implement keyed coding for NSURL.

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@30853 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
rfm 2010-06-25 13:38:06 +00:00
parent 9f2c523921
commit e6166d58ba
3 changed files with 78 additions and 32 deletions

View file

@ -1,3 +1,10 @@
2010-06-25 Richard Frith-Macdonald <rfm@gnu.org>
* Source/NSURL.m: Implement keyed coding
* Source/NSBundle.m: When fetching a localised resource for a specific
language, ignore the language list in the user preferences, and just
look for that language asn for unlocalised data.
2010-06-25 Richard Frith-Macdonald <rfm@gnu.org> 2010-06-25 Richard Frith-Macdonald <rfm@gnu.org>
* Source/NSKeyedArchiver.m: * Source/NSKeyedArchiver.m:

View file

@ -1646,33 +1646,53 @@ IF_NO_GC(
<rootPath>/<bundlePath> <rootPath>/<bundlePath>
<rootPath>/<bundlePath>/<language.lproj> <rootPath>/<bundlePath>/<language.lproj>
*/ */
+ (NSArray *) _bundleResourcePathsWithRootPath: (NSString *)rootPath + (NSArray *) _bundleResourcePathsWithRootPath: (NSString*)rootPath
subPath: (NSString *)subPath subPath: (NSString*)subPath
localization: (NSString*)localization
{ {
NSString* primary; NSString *primary;
NSString* language; NSString *language;
NSArray* languages; NSArray *languages;
NSMutableArray* array; NSMutableArray *array;
NSEnumerator* enumerate; NSEnumerator *enumerate;
array = [NSMutableArray arrayWithCapacity: 8]; array = [NSMutableArray arrayWithCapacity: 8];
languages = [NSUserDefaults userLanguages]; languages = [NSUserDefaults userLanguages];
primary = [rootPath stringByAppendingPathComponent: @"Resources"]; primary = [rootPath stringByAppendingPathComponent: @"Resources"];
[array addObject: _bundle_resource_path(primary, subPath, nil)]; [array addObject: _bundle_resource_path(primary, subPath, nil)];
/* This matches OS X behavior, which only searches languages that /* If we have been asked for a specific localization, we add it.
are in the user's preference. Don't use -preferredLocalizations - */
that would cause a recursive loop. */ if (localization != nil)
enumerate = [languages objectEnumerator]; {
while ((language = [enumerate nextObject])) [array addObject: _bundle_resource_path(primary, subPath, localization)];
[array addObject: _bundle_resource_path(primary, subPath, language)]; }
else
{
/* This matches OS X behavior, which only searches languages that
* are in the user's preference. Don't use -preferredLocalizations -
* that would cause a recursive loop.
*/
enumerate = [languages objectEnumerator];
while ((language = [enumerate nextObject]))
{
[array addObject: _bundle_resource_path(primary, subPath, language)];
}
}
primary = rootPath; primary = rootPath;
[array addObject: _bundle_resource_path(primary, subPath, nil)]; [array addObject: _bundle_resource_path(primary, subPath, nil)];
enumerate = [languages objectEnumerator]; if (localization != nil)
while ((language = [enumerate nextObject])) {
[array addObject: _bundle_resource_path(primary, subPath, language)]; [array addObject: _bundle_resource_path(primary, subPath, localization)];
}
else
{
enumerate = [languages objectEnumerator];
while ((language = [enumerate nextObject]))
{
[array addObject: _bundle_resource_path(primary, subPath, language)];
}
}
return array; return array;
} }
@ -1695,7 +1715,7 @@ IF_NO_GC(
} }
pathlist = [[self _bundleResourcePathsWithRootPath: rootPath pathlist = [[self _bundleResourcePathsWithRootPath: rootPath
subPath: subPath] objectEnumerator]; subPath: subPath localization: nil] objectEnumerator];
while ((path = [pathlist nextObject]) != nil) while ((path = [pathlist nextObject]) != nil)
{ {
if (bundle_directory_readable(path)) if (bundle_directory_readable(path))
@ -1767,7 +1787,8 @@ IF_NO_GC(
+ (NSArray*) _pathsForResourcesOfType: (NSString*)extension + (NSArray*) _pathsForResourcesOfType: (NSString*)extension
inRootDirectory: (NSString*)bundlePath inRootDirectory: (NSString*)bundlePath
inSubDirectory: (NSString *)subPath inSubDirectory: (NSString*)subPath
localization: (NSString*)localization
{ {
BOOL allfiles; BOOL allfiles;
NSString *path; NSString *path;
@ -1776,7 +1797,7 @@ IF_NO_GC(
NSFileManager *mgr = manager(); NSFileManager *mgr = manager();
pathlist = [[NSBundle _bundleResourcePathsWithRootPath: bundlePath pathlist = [[NSBundle _bundleResourcePathsWithRootPath: bundlePath
subPath: subPath] objectEnumerator]; subPath: subPath localization: localization] objectEnumerator];
resources = [NSMutableArray arrayWithCapacity: 2]; resources = [NSMutableArray arrayWithCapacity: 2];
allfiles = (extension == nil || [extension length] == 0); allfiles = (extension == nil || [extension length] == 0);
@ -1801,15 +1822,17 @@ IF_NO_GC(
{ {
return [self _pathsForResourcesOfType: extension return [self _pathsForResourcesOfType: extension
inRootDirectory: bundlePath inRootDirectory: bundlePath
inSubDirectory: nil]; inSubDirectory: nil
localization: nil];
} }
- (NSArray *) pathsForResourcesOfType: (NSString *)extension - (NSArray *) pathsForResourcesOfType: (NSString *)extension
inDirectory: (NSString *)subPath inDirectory: (NSString *)subPath
{ {
return [[self class] _pathsForResourcesOfType: extension return [[self class] _pathsForResourcesOfType: extension
inRootDirectory: [self bundlePath] inRootDirectory: [self bundlePath]
inSubDirectory: subPath]; inSubDirectory: subPath
localization: nil];
} }
- (NSArray*) pathsForResourcesOfType: (NSString*)extension - (NSArray*) pathsForResourcesOfType: (NSString*)extension
@ -1822,8 +1845,10 @@ IF_NO_GC(
NSString *path = nil; NSString *path = nil;
result = [NSMutableArray array]; result = [NSMutableArray array];
paths = [self pathsForResourcesOfType: extension paths = [[self class] _pathsForResourcesOfType: extension
inDirectory: subPath]; inRootDirectory: [self bundlePath]
inSubDirectory: subPath
localization: localizationName];
enumerator = [paths objectEnumerator]; enumerator = [paths objectEnumerator];
while ((path = [enumerator nextObject]) != nil) while ((path = [enumerator nextObject]) != nil)

View file

@ -1149,8 +1149,16 @@ static unsigned urlAlign;
- (void) encodeWithCoder: (NSCoder*)aCoder - (void) encodeWithCoder: (NSCoder*)aCoder
{ {
[aCoder encodeObject: _urlString]; if ([aCoder allowsKeyedCoding])
[aCoder encodeObject: _baseURL]; {
[aCoder encodeObject: _baseURL forKey: @"NS.base"];
[aCoder encodeObject: _urlString forKey: @"NS.relative"];
}
else
{
[aCoder encodeObject: _urlString];
[aCoder encodeObject: _baseURL];
}
} }
- (NSUInteger) hash - (NSUInteger) hash
@ -1163,15 +1171,21 @@ static unsigned urlAlign;
NSURL *base; NSURL *base;
NSString *rel; NSString *rel;
[aCoder decodeValueOfObjCType: @encode(id) at: &rel]; if ([aCoder allowsKeyedCoding])
[aCoder decodeValueOfObjCType: @encode(id) at: &base]; {
base = [aCoder decodeObjectForKey: @"NS.base"];
rel = [aCoder decodeObjectForKey: @"NS.relative"];
}
else
{
rel = [aCoder decodeObject];
base = [aCoder decodeObject];
}
if (nil == rel) if (nil == rel)
{ {
rel = @""; rel = @"";
} }
self = [self initWithString: rel relativeToURL: base]; self = [self initWithString: rel relativeToURL: base];
RELEASE(rel);
RELEASE(base);
return self; return self;
} }