2009-11-29 05:19:32 +01:00
|
|
|
/* SOGoDefaultsSource.m - this file is part of SOGo
|
|
|
|
*
|
|
|
|
* Copyright (C) 2009 Inverse inc.
|
|
|
|
*
|
|
|
|
* Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
|
|
|
|
*
|
|
|
|
* This file is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This file is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import <Foundation/NSArray.h>
|
|
|
|
#import <Foundation/NSDictionary.h>
|
|
|
|
#import <Foundation/NSException.h>
|
|
|
|
#import <Foundation/NSString.h>
|
|
|
|
#import <Foundation/NSValue.h>
|
|
|
|
|
|
|
|
#import <NGExtensions/NSObject+Logs.h>
|
|
|
|
|
|
|
|
#define NEEDS_DEFAULTS_SOURCE_INTERNAL 1
|
|
|
|
#import "SOGoDefaultsSource.h"
|
|
|
|
|
|
|
|
NSString *SOGoDefaultsSourceInvalidSource = @"SOGoDefaultsSourceInvalidSource";
|
|
|
|
NSString *SOGoDefaultsSourceUnmutableSource = @"SOGoDefaultsSourceUnmutableSource";
|
|
|
|
|
2009-12-01 15:29:56 +01:00
|
|
|
static Class NSArrayKlass = Nil;
|
|
|
|
static Class NSDataKlass = Nil;
|
|
|
|
static Class NSDictionaryKlass = Nil;
|
|
|
|
static Class NSStringKlass = Nil;
|
|
|
|
|
2009-11-29 05:19:32 +01:00
|
|
|
@implementation SOGoDefaultsSource
|
|
|
|
|
2009-12-01 15:29:56 +01:00
|
|
|
+ (void) initialize
|
|
|
|
{
|
|
|
|
if (!NSArrayKlass)
|
|
|
|
NSArrayKlass = [NSArray class];
|
|
|
|
if (!NSDataKlass)
|
|
|
|
NSDataKlass = [NSData class];
|
|
|
|
if (!NSDictionaryKlass)
|
|
|
|
NSDictionaryKlass = [NSDictionary class];
|
|
|
|
if (!NSStringKlass)
|
|
|
|
NSStringKlass = [NSString class];
|
|
|
|
}
|
|
|
|
|
2009-11-29 05:19:32 +01:00
|
|
|
+ (id) defaultsSourceWithSource: (id) newSource
|
|
|
|
andParentSource: (SOGoDefaultsSource *) newParentSource
|
|
|
|
{
|
|
|
|
SOGoDefaultsSource *sogoDefaultsSource;
|
|
|
|
|
|
|
|
sogoDefaultsSource = [self new];
|
|
|
|
[sogoDefaultsSource autorelease];
|
|
|
|
[sogoDefaultsSource setSource: newSource];
|
|
|
|
[sogoDefaultsSource setParentSource: newParentSource];
|
|
|
|
|
|
|
|
if ([sogoDefaultsSource migrate])
|
|
|
|
[sogoDefaultsSource synchronize];
|
|
|
|
|
|
|
|
return sogoDefaultsSource;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) init
|
|
|
|
{
|
|
|
|
if ((self = [super init]))
|
|
|
|
{
|
|
|
|
source = nil;
|
|
|
|
parentSource = nil;
|
|
|
|
isMutable = NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
[source release];
|
|
|
|
[parentSource release];
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setSource: (id) newSource
|
|
|
|
{
|
|
|
|
if ([newSource respondsToSelector: @selector (objectForKey:)])
|
|
|
|
{
|
|
|
|
ASSIGN (source, newSource);
|
|
|
|
isMutable = [source respondsToSelector: @selector (setObject:forKey:)];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
[NSException raise: SOGoDefaultsSourceInvalidSource
|
|
|
|
format: @"UserDefaults source '%@'"
|
|
|
|
@" does not respond to 'object:forKey:'", newSource];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) source
|
|
|
|
{
|
|
|
|
return source;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setParentSource: (SOGoDefaultsSource *) newParentSource
|
|
|
|
{
|
|
|
|
ASSIGN (parentSource, newParentSource);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setObject: (id) value
|
|
|
|
forKey: (NSString *) key
|
|
|
|
{
|
|
|
|
if (isMutable)
|
|
|
|
[source setObject: value forKey: key];
|
|
|
|
else
|
|
|
|
[NSException raise: SOGoDefaultsSourceUnmutableSource
|
|
|
|
format: @"UserDefaults source '%@' is not mutable", source];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) objectForKey: (NSString *) objectKey
|
|
|
|
{
|
|
|
|
id objectForKey;
|
|
|
|
|
|
|
|
objectForKey = [source objectForKey: objectKey];
|
|
|
|
if (!objectForKey)
|
|
|
|
objectForKey = [parentSource objectForKey: objectKey];
|
|
|
|
|
|
|
|
return objectForKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) removeObjectForKey: (NSString *) key
|
|
|
|
{
|
|
|
|
[source removeObjectForKey: key];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setBool: (BOOL) value
|
|
|
|
forKey: (NSString *) key
|
|
|
|
{
|
2009-11-30 19:23:29 +01:00
|
|
|
[self setInteger: (value ? 1: 0) forKey: key];
|
2009-11-29 05:19:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) boolForKey: (NSString *) key
|
|
|
|
{
|
2009-12-01 15:29:56 +01:00
|
|
|
id boolForKey;
|
|
|
|
BOOL value;
|
|
|
|
|
|
|
|
boolForKey = [self objectForKey: key];
|
|
|
|
if (boolForKey)
|
|
|
|
{
|
|
|
|
if ([boolForKey respondsToSelector: @selector (boolValue)])
|
|
|
|
value = [boolForKey boolValue];
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[self warnWithFormat: @"expected a boolean for '%@' (ignored)",
|
|
|
|
key];
|
|
|
|
value = NO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
value = NO;
|
|
|
|
|
|
|
|
return value;
|
2009-11-29 05:19:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setFloat: (float) value
|
|
|
|
forKey: (NSString *) key
|
|
|
|
{
|
|
|
|
[self setObject: [NSNumber numberWithFloat: value]
|
|
|
|
forKey: key];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (float) floatForKey: (NSString *) key
|
|
|
|
{
|
2009-12-01 15:29:56 +01:00
|
|
|
id floatForKey;
|
|
|
|
float value;
|
|
|
|
|
|
|
|
floatForKey = [self objectForKey: key];
|
|
|
|
if (floatForKey)
|
|
|
|
{
|
|
|
|
if ([floatForKey respondsToSelector: @selector (floatValue)])
|
|
|
|
value = [floatForKey floatValue];
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[self warnWithFormat: @"expected a float for '%@' (ignored)",
|
|
|
|
key];
|
|
|
|
value = 0.0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
value = 0.0;
|
|
|
|
|
|
|
|
return value;
|
2009-11-29 05:19:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setInteger: (int) value
|
|
|
|
forKey: (NSString *) key
|
|
|
|
{
|
|
|
|
[self setObject: [NSString stringWithFormat: @"%d", value]
|
|
|
|
forKey: key];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (int) integerForKey: (NSString *) key
|
|
|
|
{
|
2009-12-01 15:29:56 +01:00
|
|
|
id intForKey;
|
|
|
|
int value;
|
2009-11-29 05:19:32 +01:00
|
|
|
|
2009-12-01 15:29:56 +01:00
|
|
|
intForKey = [self objectForKey: key];
|
|
|
|
if (intForKey)
|
|
|
|
{
|
|
|
|
if ([intForKey respondsToSelector: @selector (intValue)])
|
|
|
|
value = [intForKey intValue];
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[self warnWithFormat: @"expected an integer for '%@' (ignored)",
|
|
|
|
key];
|
|
|
|
value = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
value = 0;
|
2009-11-29 05:19:32 +01:00
|
|
|
|
2009-12-01 15:29:56 +01:00
|
|
|
return value;
|
2009-11-29 05:19:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSData *) dataForKey: (NSString *) key
|
|
|
|
{
|
2009-12-01 15:29:56 +01:00
|
|
|
NSData *dataForKey;
|
|
|
|
|
|
|
|
dataForKey = [self objectForKey: key];
|
|
|
|
if (dataForKey && ![dataForKey isKindOfClass: NSDataKlass])
|
|
|
|
{
|
|
|
|
[self warnWithFormat: @"expected an NSData for '%@' (ignored)",
|
|
|
|
key];
|
|
|
|
dataForKey = nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
return dataForKey;
|
2009-11-29 05:19:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) stringForKey: (NSString *) key
|
|
|
|
{
|
2009-12-01 15:29:56 +01:00
|
|
|
NSString *stringForKey;
|
|
|
|
|
|
|
|
stringForKey = [self objectForKey: key];
|
|
|
|
if (stringForKey && ![stringForKey isKindOfClass: NSStringKlass])
|
|
|
|
{
|
|
|
|
[self warnWithFormat: @"expected an NSString for '%@' (ignored)",
|
|
|
|
key];
|
|
|
|
stringForKey = nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
return stringForKey;
|
2009-11-29 05:19:32 +01:00
|
|
|
}
|
|
|
|
|
2009-12-01 15:29:56 +01:00
|
|
|
- (NSDictionary *) dictionaryForKey: (NSString *) key
|
2009-11-29 05:19:32 +01:00
|
|
|
{
|
2009-12-01 15:29:56 +01:00
|
|
|
NSDictionary *dictionaryForKey;
|
|
|
|
|
|
|
|
/* Dictionaries are a special case for which we don't allow searches in the
|
|
|
|
parent source. Each level can thus have its own set of dictionary
|
|
|
|
values. */
|
|
|
|
dictionaryForKey = [source objectForKey: key];
|
|
|
|
if (dictionaryForKey
|
|
|
|
&& ![dictionaryForKey isKindOfClass: NSDictionaryKlass])
|
|
|
|
{
|
|
|
|
[self warnWithFormat: @"expected an NSDictionary for '%@' (ignored)",
|
|
|
|
key];
|
|
|
|
dictionaryForKey = nil;
|
|
|
|
}
|
2009-11-29 05:19:32 +01:00
|
|
|
|
2009-12-01 15:29:56 +01:00
|
|
|
return dictionaryForKey;
|
|
|
|
}
|
2009-11-29 05:19:32 +01:00
|
|
|
|
2009-12-01 15:29:56 +01:00
|
|
|
- (NSArray *) arrayForKey: (NSString *) key
|
|
|
|
{
|
|
|
|
NSArray *arrayForKey;
|
|
|
|
|
|
|
|
arrayForKey = [self objectForKey: key];
|
|
|
|
if (arrayForKey && ![arrayForKey isKindOfClass: NSArrayKlass])
|
|
|
|
{
|
|
|
|
[self warnWithFormat: @"expected an NSArray for '%@' (ignored)",
|
|
|
|
key];
|
|
|
|
arrayForKey = nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
return arrayForKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSArray *) stringArrayForKey: (NSString *) key
|
|
|
|
{
|
|
|
|
NSArray *stringArray;
|
|
|
|
int count, max;
|
|
|
|
|
|
|
|
stringArray = [self arrayForKey: key];
|
2009-12-01 17:09:26 +01:00
|
|
|
max = [stringArray count];
|
2009-12-01 15:29:56 +01:00
|
|
|
for (count = 0; stringArray && count < max; count++)
|
|
|
|
if (![[stringArray objectAtIndex: count] isKindOfClass: NSStringKlass])
|
|
|
|
{
|
2009-12-01 15:46:55 +01:00
|
|
|
[self warnWithFormat: @"expected string values in array for '%@'"
|
2009-12-01 15:29:56 +01:00
|
|
|
@", value %d is not a string (ignored)",
|
|
|
|
key, count];
|
|
|
|
stringArray = nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
return stringArray;
|
2009-11-29 05:19:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) migrate
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) migrateOldDefaultsWithDictionary: (NSDictionary *) migratedKeys
|
|
|
|
{
|
|
|
|
NSArray *allKeys;
|
|
|
|
id currentValue, existingValue;
|
|
|
|
NSString *oldName, *newName;
|
|
|
|
int count, max;
|
|
|
|
BOOL requireSync;
|
|
|
|
|
|
|
|
requireSync = NO;
|
|
|
|
|
|
|
|
allKeys = [migratedKeys allKeys];
|
|
|
|
max = [allKeys count];
|
|
|
|
for (count = 0; count < max; count++)
|
|
|
|
{
|
|
|
|
oldName = [allKeys objectAtIndex: count];
|
|
|
|
currentValue = [source objectForKey: oldName];
|
|
|
|
if (currentValue)
|
|
|
|
{
|
|
|
|
newName = [migratedKeys objectForKey: oldName];
|
|
|
|
existingValue = [source objectForKey: newName];
|
2009-11-30 16:42:16 +01:00
|
|
|
requireSync = YES;
|
|
|
|
[source setObject: currentValue forKey: newName];
|
|
|
|
[source removeObjectForKey: oldName];
|
|
|
|
[self warnWithFormat: @"defaults key '%@' was renamed to '%@'",
|
|
|
|
oldName, newName];
|
2009-11-29 05:19:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return requireSync;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) synchronize
|
|
|
|
{
|
|
|
|
BOOL rc;
|
|
|
|
|
|
|
|
if ([source respondsToSelector: @selector (synchronize)])
|
|
|
|
rc = [source synchronize];
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[self errorWithFormat: @"current source cannot synchronize defaults"];
|
|
|
|
rc = NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|