Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Support a custom user defaults domain

This is useful for situations like a background app that updates a
parent preference pane but wants to maintain defaults in the background
app's domain.
  • Loading branch information...
commit 29b7321094029882de3c35e6e2626ef1c39a1e79 1 parent 449b00e
@mattstevens mattstevens authored
Showing with 22 additions and 12 deletions.
  1. +1 −0  SUConstants.h
  2. +1 −0  SUConstants.m
  3. +2 −0  SUHost.h
  4. +18 −12 SUHost.m
View
1  SUConstants.h
@@ -59,6 +59,7 @@ extern NSString *const SULastProfileSubmitDateKey;
extern NSString *const SUPromptUserOnFirstLaunchKey;
extern NSString *const SUFixedHTMLDisplaySizeKey;
extern NSString *const SUKeepDownloadOnFailedInstallKey;
+extern NSString *const SUDefaultsDomainKey;
// -----------------------------------------------------------------------------
// Errors:
View
1  SUConstants.m
@@ -35,6 +35,7 @@
NSString *const SUPromptUserOnFirstLaunchKey = @"SUPromptUserOnFirstLaunch";
NSString *const SUFixedHTMLDisplaySizeKey = @"SUFixedHTMLDisplaySize";
NSString *const SUKeepDownloadOnFailedInstallKey = @"SUKeepDownloadOnFailedInstall";
+NSString *const SUDefaultsDomainKey = @"SUDefaultsDomain";
NSString *const SUSparkleErrorDomain = @"SUSparkleErrorDomain";
OSStatus SUAppcastParseError = 1000;
View
2  SUHost.h
@@ -14,6 +14,8 @@
{
@private
NSBundle *bundle;
+ NSString *defaultsDomain;
+ BOOL usesStandardUserDefaults;
}
+ (NSString *)systemVersionString;
View
30 SUHost.m
@@ -23,12 +23,20 @@ - (id)initWithBundle:(NSBundle *)aBundle
bundle = [aBundle retain];
if (![bundle bundleIdentifier])
SULog(@"Sparkle Error: the bundle being updated at %@ has no CFBundleIdentifier! This will cause preference read/write to not work properly.", bundle);
+
+ defaultsDomain = [[bundle objectForInfoDictionaryKey:SUDefaultsDomainKey] retain];
+ if (!defaultsDomain)
+ defaultsDomain = [[bundle bundleIdentifier] retain];
+
+ // If we're using the main bundle's defaults we'll use the standard user defaults mechanism, otherwise we have to get CF-y.
+ usesStandardUserDefaults = [defaultsDomain isEqualToString:[[NSBundle mainBundle] bundleIdentifier]];
}
return self;
}
- (void)dealloc
{
+ [defaultsDomain release];
[bundle release];
[super dealloc];
}
@@ -177,34 +185,33 @@ - (id)objectForUserDefaultsKey:(NSString *)defaultName
// Under Tiger, CFPreferencesCopyAppValue doesn't get values from NSRegistrationDomain, so anything
// passed into -[NSUserDefaults registerDefaults:] is ignored. The following line falls
// back to using NSUserDefaults, but only if the host bundle is the main bundle.
- if (bundle == [NSBundle mainBundle])
+ if (usesStandardUserDefaults)
return [[NSUserDefaults standardUserDefaults] objectForKey:defaultName];
- CFPropertyListRef obj = CFPreferencesCopyAppValue((CFStringRef)defaultName, (CFStringRef)[bundle bundleIdentifier]);
+ CFPropertyListRef obj = CFPreferencesCopyAppValue((CFStringRef)defaultName, (CFStringRef)defaultsDomain);
return [(id)CFMakeCollectable(obj) autorelease];
}
- (void)setObject:(id)value forUserDefaultsKey:(NSString *)defaultName;
{
- // If we're using a .app, we'll use the standard user defaults mechanism; otherwise, we have to get CF-y.
- if (bundle == [NSBundle mainBundle])
+ if (usesStandardUserDefaults)
{
[[NSUserDefaults standardUserDefaults] setObject:value forKey:defaultName];
}
else
{
- CFPreferencesSetValue((CFStringRef)defaultName, value, (CFStringRef)[bundle bundleIdentifier], kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
- CFPreferencesSynchronize((CFStringRef)[bundle bundleIdentifier], kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
+ CFPreferencesSetValue((CFStringRef)defaultName, value, (CFStringRef)defaultsDomain, kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
+ CFPreferencesSynchronize((CFStringRef)defaultsDomain, kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
}
}
- (BOOL)boolForUserDefaultsKey:(NSString *)defaultName
{
- if (bundle == [NSBundle mainBundle])
+ if (usesStandardUserDefaults)
return [[NSUserDefaults standardUserDefaults] boolForKey:defaultName];
BOOL value;
- CFPropertyListRef plr = CFPreferencesCopyAppValue((CFStringRef)defaultName, (CFStringRef)[bundle bundleIdentifier]);
+ CFPropertyListRef plr = CFPreferencesCopyAppValue((CFStringRef)defaultName, (CFStringRef)defaultsDomain);
if (plr == NULL)
value = NO;
else
@@ -217,15 +224,14 @@ - (BOOL)boolForUserDefaultsKey:(NSString *)defaultName
- (void)setBool:(BOOL)value forUserDefaultsKey:(NSString *)defaultName
{
- // If we're using a .app, we'll use the standard user defaults mechanism; otherwise, we have to get CF-y.
- if (bundle == [NSBundle mainBundle])
+ if (usesStandardUserDefaults)
{
[[NSUserDefaults standardUserDefaults] setBool:value forKey:defaultName];
}
else
{
- CFPreferencesSetValue((CFStringRef)defaultName, (CFBooleanRef)[NSNumber numberWithBool:value], (CFStringRef)[bundle bundleIdentifier], kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
- CFPreferencesSynchronize((CFStringRef)[bundle bundleIdentifier], kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
+ CFPreferencesSetValue((CFStringRef)defaultName, (CFBooleanRef)[NSNumber numberWithBool:value], (CFStringRef)defaultsDomain, kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
+ CFPreferencesSynchronize((CFStringRef)defaultsDomain, kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.