|
Wired Foundation
2.0
A foundation framework for the Wired implementation on Mac OS X
|
00001 /* $Id$ */ 00002 00003 /* 00004 * Copyright (c) 2003-2009 Axel Andersson 00005 * All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions 00009 * are met: 00010 * 1. Redistributions of source code must retain the above copyright 00011 * notice, this list of conditions and the following disclaimer. 00012 * 2. Redistributions in binary form must reproduce the above copyright 00013 * notice, this list of conditions and the following disclaimer in the 00014 * documentation and/or other materials provided with the distribution. 00015 * 00016 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 00017 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00018 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00019 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 00020 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00021 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00022 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00023 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 00024 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00025 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00026 * POSSIBILITY OF SUCH DAMAGE. 00027 */ 00028 00029 #import <WiredFoundation/NSObject-WIFoundation.h> 00030 #import <WiredFoundation/NSNumber-WIFoundation.h> 00031 #import <WiredFoundation/WISettings.h> 00032 00033 @interface WISettings(Private) 00034 00035 - (id)_initWithIdentifier:(NSString *)identifier; 00036 00037 @end 00038 00039 00040 @implementation WISettings(Private) 00041 00042 - (id)_initWithIdentifier:(NSString *)identifier { 00043 self = [super init]; 00044 00045 if([identifier length] > 0) { 00046 _identifier = [identifier retain]; 00047 _defaults = [[[NSUserDefaults standardUserDefaults] persistentDomainForName:_identifier] mutableCopy]; 00048 00049 if(!_defaults) 00050 _defaults = [[NSMutableDictionary alloc] init]; 00051 } else { 00052 _defaults = [[NSUserDefaults standardUserDefaults] retain]; 00053 } 00054 00055 _defaultValues = [[self defaults] retain]; 00056 00057 return self; 00058 } 00059 00060 @end 00061 00062 00063 00064 @implementation WISettings 00065 00066 + (id)settings { 00067 return [self settingsWithIdentifier:@""]; 00068 } 00069 00070 00071 00072 + (id)settingsWithIdentifier:(NSString *)identifier { 00073 static NSMutableDictionary *dictionary; 00074 id settings; 00075 00076 if(!dictionary) 00077 dictionary = [[NSMutableDictionary alloc] init]; 00078 00079 settings = [dictionary objectForKey:identifier]; 00080 00081 if(!settings) { 00082 settings = [[[self alloc] _initWithIdentifier:identifier] autorelease]; 00083 00084 [dictionary setObject:settings forKey:identifier]; 00085 } 00086 00087 return settings; 00088 } 00089 00090 00091 00092 #pragma mark - 00093 00094 - (NSDictionary *)defaults { 00095 return [NSDictionary dictionary]; 00096 } 00097 00098 00099 00100 #pragma mark - 00101 00102 - (BOOL)synchronize { 00103 NSUserDefaults *defaults; 00104 00105 defaults = [NSUserDefaults standardUserDefaults]; 00106 00107 if(_identifier) { 00108 [defaults removePersistentDomainForName:_identifier]; 00109 [defaults setPersistentDomain:_defaults forName:_identifier]; 00110 } 00111 00112 return [defaults synchronize]; 00113 } 00114 00115 00116 00117 #pragma mark - 00118 00119 - (void)dealloc { 00120 [_identifier release]; 00121 [_defaultValues release]; 00122 [_defaults release]; 00123 00124 [super dealloc]; 00125 } 00126 00127 00128 00129 #pragma mark - 00130 00131 - (void)setObject:(id)object forKey:(id)key { 00132 [_defaults setObject:object forKey:key]; 00133 00134 [self performSelectorOnce:@selector(synchronize) withObject:NULL afterDelay:1.0]; 00135 } 00136 00137 00138 00139 - (id)objectForKey:(id)key { 00140 id object; 00141 00142 object = [_defaults objectForKey:key]; 00143 00144 if(object) 00145 return object; 00146 00147 return [_defaultValues objectForKey:key]; 00148 } 00149 00150 00151 00152 - (void)removeObjectForKey:(id)key { 00153 [_defaults removeObjectForKey:key]; 00154 } 00155 00156 00157 00158 - (void)setString:(NSString *)object forKey:(id)key { 00159 [self setObject:object forKey:key]; 00160 } 00161 00162 00163 00164 - (NSString *)stringForKey:(id)key { 00165 return [self objectForKey:key]; 00166 } 00167 00168 00169 00170 - (void)setBool:(BOOL)value forKey:(id)key { 00171 [self setObject:[NSNumber numberWithBool:value] forKey:key]; 00172 } 00173 00174 00175 00176 - (BOOL)boolForKey:(id)key { 00177 return [[self objectForKey:key] boolValue]; 00178 } 00179 00180 00181 00182 - (void)setInt:(int)value forKey:(id)key { 00183 [self setObject:[NSNumber numberWithInt:value] forKey:key]; 00184 } 00185 00186 00187 00188 - (int)intForKey:(id)key { 00189 return [[self objectForKey:key] intValue]; 00190 } 00191 00192 00193 00194 - (void)setInteger:(NSInteger)value forKey:(id)key { 00195 [self setObject:[NSNumber numberWithInteger:value] forKey:key]; 00196 } 00197 00198 00199 00200 - (NSInteger)integerForKey:(id)key { 00201 return [[self objectForKey:key] integerValue]; 00202 } 00203 00204 00205 00206 - (void)setFloat:(float)value forKey:(id)key { 00207 [self setObject:[NSNumber numberWithFloat:value] forKey:key]; 00208 } 00209 00210 00211 00212 - (float)floatForKey:(id)key { 00213 return [[self objectForKey:key] floatValue]; 00214 } 00215 00216 00217 00218 - (void)setDouble:(double)value forKey:(id)key { 00219 [self setObject:[NSNumber numberWithDouble:value] forKey:key]; 00220 } 00221 00222 00223 00224 - (double)doubleForKey:(id)key { 00225 return [[self objectForKey:key] doubleValue]; 00226 } 00227 00228 00229 00230 #pragma mark - 00231 00232 - (void)addObject:(id)object toArrayForKey:(id)arrayKey { 00233 NSMutableArray *array; 00234 00235 array = [[[self objectForKey:arrayKey] mutableCopy] autorelease]; 00236 [array addObject:object]; 00237 [self setObject:array forKey:arrayKey]; 00238 } 00239 00240 00241 00242 - (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)object inArrayForKey:(id)arrayKey { 00243 NSMutableArray *array; 00244 00245 array = [[[self objectForKey:arrayKey] mutableCopy] autorelease]; 00246 [array replaceObjectAtIndex:index withObject:object]; 00247 [self setObject:array forKey:arrayKey]; 00248 } 00249 00250 00251 00252 - (void)removeObjectAtIndex:(NSUInteger)index fromArrayForKey:(id)arrayKey { 00253 NSMutableArray *array; 00254 00255 array = [[[self objectForKey:arrayKey] mutableCopy] autorelease]; 00256 [array removeObjectAtIndex:index]; 00257 [self setObject:array forKey:arrayKey]; 00258 } 00259 00260 00261 00262 #pragma mark - 00263 00264 - (void)setObject:(id)object forKey:(id)key inDictionaryForKey:(id)dictionaryKey { 00265 NSMutableDictionary *dictionary; 00266 00267 dictionary = [[[self objectForKey:dictionaryKey] mutableCopy] autorelease]; 00268 [dictionary setObject:object forKey:key]; 00269 [self setObject:dictionary forKey:dictionaryKey]; 00270 } 00271 00272 00273 00274 - (void)removeObjectForKey:(id)key inDictionaryForKey:(id)dictionaryKey { 00275 NSMutableDictionary *dictionary; 00276 00277 dictionary = [[[self objectForKey:dictionaryKey] mutableCopy] autorelease]; 00278 [dictionary removeObjectForKey:key]; 00279 [self setObject:dictionary forKey:dictionaryKey]; 00280 } 00281 00282 @end