00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #import "DAPreferences.h"
00023
00025 #define PREFS_PATH @"~/Library/Preferences/edu.uchicago.DownAndOut.plist"
00026
00027 @implementation DAPreferences
00028
00029 - (id) initWithDictionary: (NSDictionary *) initial forPath: (NSString *) fullPath
00030 {
00031 path = [fullPath retain];
00032 prefs = [initial mutableCopy];
00033 dirty = NO;
00034 return self;
00035 }
00036
00037 - (id) initWithPath: (NSString *) fullPath
00038 {
00039 path = [fullPath retain];
00040 prefs = [[NSMutableDictionary dictionaryWithContentsOfFile: path] retain];
00041 dirty = NO;
00042 if (!prefs) {
00043 [self release];
00044 return nil;
00045 }
00046 return self;
00047 }
00048
00049 - (id) initWithDefaults: (NSDictionary *) initial;
00050 {
00051 id retval = [self initWithPath: [PREFS_PATH stringByExpandingTildeInPath]];
00052 if (!retval)
00053 retval = [[DAPreferences alloc] initWithDictionary: initial forPath: [PREFS_PATH stringByExpandingTildeInPath]];
00054 return retval;
00055 }
00056
00058 - (void) dealloc
00059 {
00060 [path release];
00061 [prefs release];
00062 [super dealloc];
00063 }
00064
00065 - (void) flushToFile
00066 {
00067 if (dirty) {
00068 [prefs writeToFile: path atomically: YES];
00069 dirty = NO;
00070 }
00071 }
00072
00073 - (int) integerForKey: (NSString *) aKey
00074 {
00075 id obj = [self objectForKey: aKey];
00076 return [obj intValue];
00077 }
00078
00079 - (void) setInteger: (int) anInt forKey: (NSString *) aKey
00080 {
00081 [self setObject: [NSNumber numberWithInt: anInt] forKey: aKey];
00082 }
00083
00084 - (id) objectForKey: (NSString *) aKey
00085 {
00086 return [prefs objectForKey: aKey];
00087 }
00088
00089 - (void) setObject: (id) value forKey: (NSString *) aKey
00090 {
00091 [prefs setObject: value forKey: aKey];
00092 dirty = YES;
00093 }
00094
00095 - (BOOL) boolForKey: (NSString *) aKey
00096 {
00097 id obj = [self objectForKey: aKey];
00098 return [obj boolValue];
00099 }
00100
00101 - (void) setBool: (BOOL) aBool forKey: (NSString *) aKey
00102 {
00103 [self setObject: [NSNumber numberWithBool: aBool] forKey: aKey];
00104 }
00105
00106 - (NSData *) dataForKey: (NSString *) aKey
00107 {
00108 id retval = [self objectForKey: aKey];
00109 return [retval isKindOfClass: [NSData class]]? retval: nil;
00110 }
00111
00112 - (NSString *) stringForKey: (NSString *) aKey
00113 {
00114 id retval = [self objectForKey: aKey];
00115 return [retval isKindOfClass: [NSString class]]? retval: nil;
00116 }
00117
00118 - (void) removeObjectForKey: (NSString *) aKey
00119 {
00120 [prefs removeObjectForKey: aKey];
00121 dirty = YES;
00122 }
00123
00124 @end