Allow loading from an absolute path

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/branches/themes@23667 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Richard Frith-MacDonald 2006-09-29 09:12:49 +00:00
parent 1a87c5ff11
commit 19a0eab892

View file

@ -161,8 +161,9 @@ typedef enum {
@implementation GSTheme @implementation GSTheme
static GSTheme *defaultTheme = nil; static GSTheme *defaultTheme = nil;
static GSTheme *theTheme = nil; static NSString *defaultThemeName = nil;
static GSTheme *theTheme = nil;
static NSString *theThemeName = nil; static NSString *theThemeName = nil;
static NSMutableDictionary *themes = nil; static NSMutableDictionary *themes = nil;
static NSNull *null = nil; static NSNull *null = nil;
@ -174,8 +175,9 @@ static NSNull *null = nil;
defs = [NSUserDefaults standardUserDefaults]; defs = [NSUserDefaults standardUserDefaults];
name = [defs stringForKey: @"GSTheme"]; name = [defs stringForKey: @"GSTheme"];
if (name != theThemeName && [name isEqual: theThemeName] == NO) if (name != defaultThemeName && [name isEqual: defaultThemeName] == NO)
{ {
ASSIGN(defaultThemeName, name); // Don't try to load again.
[self loadThemeNamed: name]; [self loadThemeNamed: name];
} }
} }
@ -185,7 +187,6 @@ static NSNull *null = nil;
if (themes == nil) if (themes == nil)
{ {
themes = [NSMutableDictionary new]; themes = [NSMutableDictionary new];
[self theme]; // Initialise/create the default theme
[[NSNotificationCenter defaultCenter] [[NSNotificationCenter defaultCenter]
addObserver: self addObserver: self
selector: @selector(defaultsDidChange:) selector: @selector(defaultsDidChange:)
@ -203,6 +204,9 @@ static NSNull *null = nil;
defaultTheme = [[self alloc] initWithBundle: aBundle]; defaultTheme = [[self alloc] initWithBundle: aBundle];
ASSIGN(theTheme, defaultTheme); ASSIGN(theTheme, defaultTheme);
} }
/* Establish the theme specified by the user defaults (if any);
*/
[self defaultsDidChange: nil];
} }
+ (BOOL) loadThemeNamed: (NSString*)aName + (BOOL) loadThemeNamed: (NSString*)aName
@ -214,15 +218,14 @@ static NSNull *null = nil;
if ([aName length] == 0) if ([aName length] == 0)
{ {
DESTROY(theThemeName);
[self setTheme: nil]; [self setTheme: nil];
[self theme]; [self theme];
return YES; return YES;
} }
/* Ensure that the theme name does not contain path components /* Ensure that the theme name has the 'theme' extension.
* and has the 'theme' extension.
*/ */
aName = [aName lastPathComponent];
if ([[aName pathExtension] isEqualToString: @"theme"] == YES) if ([[aName pathExtension] isEqualToString: @"theme"] == YES)
{ {
theme = aName; theme = aName;
@ -236,20 +239,34 @@ static NSNull *null = nil;
if (bundle == nil) if (bundle == nil)
{ {
NSString *path; NSString *path;
NSEnumerator *enumerator;
NSFileManager *mgr = [NSFileManager defaultManager]; NSFileManager *mgr = [NSFileManager defaultManager];
BOOL isDir;
enumerator = [NSSearchPathForDirectoriesInDomains /* A theme may be either an absolute path or a filename to be located
(NSAllLibrariesDirectory, NSAllDomainsMask, YES) objectEnumerator]; * in the Themes subdirectory of one of the standard Library directories.
while ((path = [enumerator nextObject]) != nil) */
{ if ([theme isAbsolutePath] == YES)
BOOL isDir; {
if ([mgr fileExistsAtPath: theme isDirectory: &isDir] == YES
path = [path stringByAppendingPathComponent: @"Themes"]; && isDir == YES)
path = [path stringByAppendingPathComponent: theme];
if ([mgr fileExistsAtPath: path isDirectory: &isDir])
{ {
break; path = theme;
}
}
else
{
NSEnumerator *enumerator;
enumerator = [NSSearchPathForDirectoriesInDomains
(NSAllLibrariesDirectory, NSAllDomainsMask, YES) objectEnumerator];
while ((path = [enumerator nextObject]) != nil)
{
path = [path stringByAppendingPathComponent: @"Themes"];
path = [path stringByAppendingPathComponent: theme];
if ([mgr fileExistsAtPath: path isDirectory: &isDir])
{
break;
}
} }
} }
@ -271,6 +288,7 @@ static NSNull *null = nil;
{ {
cls = self; cls = self;
} }
ASSIGN(theThemeName, theme);
instance = [[cls alloc] initWithBundle: bundle]; instance = [[cls alloc] initWithBundle: bundle];
[self setTheme: instance]; [self setTheme: instance];
RELEASE(instance); RELEASE(instance);