Fully implement the class NSUserDefaultsController.

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@27522 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
fredkiefer 2009-01-05 18:15:53 +00:00
parent 432f5358af
commit fe8a97bae9
5 changed files with 267 additions and 23 deletions

View file

@ -1,3 +1,10 @@
2009-01-05 Fred Kiefer <FredKiefer@gmx.de>
* Headers/AppKit/NSController.h,
* Source/NSController.m: Add one 10.4 method.
* Headers/AppKit/NSUserDefaultsController.h: Add one 10.4 method.
* Source/NSUserDefaultsController.m: Fully implement class.
2009-01-05 Richard Frith-Macdonald <rfm@gnu.org> 2009-01-05 Richard Frith-Macdonald <rfm@gnu.org>
* Source/NSScroller.m: Fixup/reorganisation of setting names for * Source/NSScroller.m: Fixup/reorganisation of setting names for

View file

@ -30,7 +30,7 @@
#include <Foundation/NSObject.h> #include <Foundation/NSObject.h>
#if OS_API_VERSION(100300,GS_API_LATEST) #if OS_API_VERSION(MAC_OS_X_VERSION_10_3, GS_API_LATEST)
@class NSMutableArray; @class NSMutableArray;
@ -50,6 +50,12 @@
- (void) objectDidBeginEditing: (id)editor; - (void) objectDidBeginEditing: (id)editor;
- (void) objectDidEndEditing: (id)editor; - (void) objectDidEndEditing: (id)editor;
#if OS_API_VERSION(MAC_OS_X_VERSION_10_4, GS_API_LATEST)
- (void) commitEditingWithDelegate: (id)delegate
didCommitSelector: (SEL)didCommitSelector
contextInfo: (void*)contextInfo;
#endif
@end @end
#endif // OS_API_VERSION #endif // OS_API_VERSION

View file

@ -31,7 +31,7 @@
#include <AppKit/NSController.h> #include <AppKit/NSController.h>
#if OS_API_VERSION(100300,GS_API_LATEST) #if OS_API_VERSION(MAC_OS_X_VERSION_10_3, GS_API_LATEST)
@class NSUserDefaults; @class NSUserDefaults;
@class NSDictionary; @class NSDictionary;
@ -41,6 +41,7 @@
{ {
NSUserDefaults* _defaults; NSUserDefaults* _defaults;
NSDictionary* _initial_values; NSDictionary* _initial_values;
id _values;
BOOL _applies_immediately; BOOL _applies_immediately;
} }
@ -58,6 +59,9 @@
- (void) revert: (id)sender; - (void) revert: (id)sender;
- (void) revertToInitialValues: (id)sender; - (void) revertToInitialValues: (id)sender;
- (void) save: (id)sender; - (void) save: (id)sender;
#if OS_API_VERSION(MAC_OS_X_VERSION_10_4, GS_API_LATEST)
- (BOOL) hasUnappliedChanges;
#endif
@end @end

View file

@ -68,13 +68,13 @@
if((self = [super init]) != nil) if((self = [super init]) != nil)
{ {
if([aDecoder allowsKeyedCoding]) if([aDecoder allowsKeyedCoding])
{ {
ASSIGN(_declared_keys,[aDecoder decodeObjectForKey: @"NSDeclaredKeys"]); ASSIGN(_declared_keys,[aDecoder decodeObjectForKey: @"NSDeclaredKeys"]);
} }
else else
{ {
ASSIGN(_declared_keys,[aDecoder decodeObject]); ASSIGN(_declared_keys,[aDecoder decodeObject]);
} }
} }
return self; return self;
} }
@ -93,13 +93,34 @@
{ {
if (![[_editors objectAtIndex: i] commitEditing]) if (![[_editors objectAtIndex: i] commitEditing])
{ {
return NO; return NO;
} }
} }
return YES; return YES;
} }
- (void) commitEditingWithDelegate: (id)delegate
didCommitSelector: (SEL)didCommitSelector
contextInfo: (void*)contextInfo
{
unsigned c = [_editors count];
unsigned i;
for (i = 0; i < c; i++)
{
BOOL didCommit = [[_editors objectAtIndex: i] commitEditing];
if (delegate != nil && didCommitSelector != NULL)
{
void (*meth)(id, SEL, id, BOOL, void*);
meth = (void (*)(id, SEL, id, BOOL, void*))[delegate methodForSelector:
didCommitSelector];
if (meth)
meth(delegate, didCommitSelector, self, didCommit, contextInfo);
}
}
}
- (void) discardEditing - (void) discardEditing
{ {
[_editors makeObjectsPerformSelector: @selector(discardEditing)]; [_editors makeObjectsPerformSelector: @selector(discardEditing)];

View file

@ -26,21 +26,149 @@
Boston, MA 02110-1301, USA. Boston, MA 02110-1301, USA.
*/ */
#include <Foundation/NSCoder.h>
#include <Foundation/NSDictionary.h> #include <Foundation/NSDictionary.h>
#include <Foundation/NSEnumerator.h>
#include <Foundation/NSKeyValueObserving.h>
#include <Foundation/NSNotification.h>
#include <Foundation/NSUserDefaults.h> #include <Foundation/NSUserDefaults.h>
#include <AppKit/NSUserDefaultsController.h> #include <AppKit/NSUserDefaultsController.h>
static id shared = nil; static id shared = nil;
@interface GSUserDefaultsHelper : NSObject
{
@public
NSUserDefaultsController *controller;
NSMutableDictionary *values;
}
- (id) initWithController: (NSUserDefaultsController*)udc;
- (id) valueForKey: (NSString*)key;
- (void) setValue: (id)value forKey: (NSString*)key;
- (void) revert;
- (void) revertToInitialValues: (NSDictionary *)initial;
- (void) defaultsDidChange: (NSUserDefaults *)defaults;
@end
@implementation GSUserDefaultsHelper
- (id) initWithController: (NSUserDefaultsController*)udc
{
if ((self = [super init]) != nil)
{
// We are retained by the controller
controller = udc;
values = [[NSMutableDictionary alloc] init];
}
return self;
}
- (void) dealloc
{
RELEASE(values);
[super dealloc];
}
- (id) valueForKey: (NSString*)key
{
id value = [values objectForKey: key];
if (!value)
{
// If the value isn't cached, get it from the controller and cache it.
value = [[controller defaults] objectForKey: key];
if (!value)
{
value = [[controller initialValues] objectForKey: key];
}
// We need to cache the values we return to be able to
// report changes to them when the defaults change.
if (value)
[values setObject: value forKey: key];
}
NSLog(@"returning %@ for key %@", value, key);
return value;
}
- (void) setValue: (id)value forKey: (NSString*)key
{
[self willChangeValueForKey: key];
[values setObject: value forKey: key];
if ([controller appliesImmediately])
[[controller defaults] setObject: value forKey: key];
[self didChangeValueForKey: key];
}
- (void) revert
{
NSEnumerator *e;
NSString *key;
e = [values keyEnumerator];
while ((key = (NSString *)[e nextObject]))
{
[self willChangeValueForKey: key];
[values removeObjectForKey: key];
[self didChangeValueForKey: key];
}
}
- (void) revertToInitialValues: (NSDictionary *)initial
{
NSEnumerator *e;
NSString *key;
e = [values keyEnumerator];
while ((key = (NSString *)[e nextObject]))
{
id val = [values objectForKey: key];
id oldVal = [initial objectForKey: key];
if (oldVal && ![val isEqual: oldVal])
{
[self willChangeValueForKey: key];
[values setObject: oldVal forKey: key];
// When appliesImmediately is YES, should we save these values?
[self didChangeValueForKey: key];
}
}
}
- (void) defaultsDidChange: (NSUserDefaults *)defaults
{
NSEnumerator *e;
NSString *key;
e = [values keyEnumerator];
while ((key = (NSString *)[e nextObject]))
{
id val = [values objectForKey: key];
id newVal = [defaults objectForKey: key];
if (newVal && ![val isEqual: newVal])
{
[self willChangeValueForKey: key];
[values setObject: newVal forKey: key];
[self didChangeValueForKey: key];
}
}
}
@end
@implementation NSUserDefaultsController @implementation NSUserDefaultsController
+ (id) sharedUserDefaultsController + (id) sharedUserDefaultsController
{ {
if (shared == nil) if (shared == nil)
{ {
shared = [[NSUserDefaultsController alloc] shared = [[NSUserDefaultsController alloc]
initWithDefaults: nil initWithDefaults: nil
initialValues: nil]; initialValues: nil];
} }
return shared; return shared;
} }
@ -51,17 +179,35 @@ static id shared = nil;
if ((self = [super init]) != nil) if ((self = [super init]) != nil)
{ {
if (defaults == nil) if (defaults == nil)
{ {
defaults = [NSUserDefaults standardUserDefaults]; defaults = [NSUserDefaults standardUserDefaults];
} }
ASSIGN(_defaults, defaults); ASSIGN(_defaults, defaults);
[self setAppliesImmediately: YES];
[self setInitialValues: initialValues]; [self setInitialValues: initialValues];
_values = [[GSUserDefaultsHelper alloc]
initWithController: self];
// Watch for user default change notifications
[[NSNotificationCenter defaultCenter]
addObserver: self
selector: @selector(defaultsDidChange:)
name: NSUserDefaultsDidChangeNotification
object: _defaults];
} }
return self; return self;
} }
- (void) dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver: self];
RELEASE(_values);
RELEASE(_defaults);
RELEASE(_initial_values);
[super dealloc];
}
- (NSUserDefaults*) defaults - (NSUserDefaults*) defaults
{ {
return _defaults; return _defaults;
@ -69,8 +215,7 @@ static id shared = nil;
- (id) values - (id) values
{ {
// TODO return _values;
return nil;
} }
- (NSDictionary*) initialValues - (NSDictionary*) initialValues
@ -80,7 +225,7 @@ static id shared = nil;
- (void) setInitialValues: (NSDictionary*)values - (void) setInitialValues: (NSDictionary*)values
{ {
ASSIGN(_initial_values, values); ASSIGNCOPY(_initial_values, values);
} }
- (BOOL) appliesImmediately - (BOOL) appliesImmediately
@ -98,18 +243,79 @@ static id shared = nil;
[self discardEditing]; [self discardEditing];
if (![self appliesImmediately]) if (![self appliesImmediately])
{ {
// TODO [_values revert];
} }
} }
- (void) revertToInitialValues: (id)sender - (void) revertToInitialValues: (id)sender
{ {
// TODO [self discardEditing];
[_values revertToInitialValues: _initial_values];
} }
- (void) save: (id)sender - (void) save: (id)sender
{ {
// TODO if (![self appliesImmediately])
{
NSDictionary *values = ((GSUserDefaultsHelper*)_values)->values;
NSEnumerator *e;
NSString *key;
e = [values keyEnumerator];
while ((key = (NSString *)[e nextObject]))
{
[_defaults setObject: [values objectForKey: key] forKey: key];
}
}
}
- (BOOL) hasUnappliedChanges
{
NSDictionary *values = ((GSUserDefaultsHelper*)_values)->values;
NSEnumerator *e;
NSString *key;
e = [values keyEnumerator];
while ((key = (NSString *)[e nextObject]))
{
id val = [values objectForKey: key];
id newVal = [_defaults objectForKey: key];
if (![val isEqual: newVal])
{
return YES;
}
}
return NO;
}
- (void) defaultsDidChange: (NSNotification*)notification
{
[_values defaultsDidChange: _defaults];
}
- (void) encodeWithCoder: (NSCoder *)aCoder
{
if ([aCoder allowsKeyedCoding])
if (self == [NSUserDefaultsController sharedUserDefaultsController])
{
[aCoder encodeBool: YES forKey: @"NSSharedInstance"];
return;
}
[super encodeWithCoder: aCoder];
}
- (id) initWithCoder: (NSCoder *)aDecoder
{
if ([aDecoder allowsKeyedCoding])
if ([aDecoder decodeBoolForKey: @"NSSharedInstance"])
{
RELEASE(self);
return [NSUserDefaultsController sharedUserDefaultsController];
}
return [super initWithCoder: aDecoder];
} }
@end @end