Main Page | Alphabetical List | Class List | Directories | File List | Class Members | Related Pages

DAPreferences.m

00001 //
00002 //  DAPreferences.m
00003 //  DownAndOutª
00004 //
00005 //  Created by Fritz Anderson on Wed Aug 27 2003.
00006 //  Copyright (c) 2003 Trustees of the University of Chicago. All rights reserved.
00007 //  This program is free software; you can redistribute it and/or modify
00008 //  it under the terms of the GNU General Public License as published by
00009 //  the Free Software Foundation; either version 2 of the License, or
00010 //  (at your option) any later version.
00011 //  
00012 //  This program is distributed in the hope that it will be useful,
00013 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 //  GNU General Public License for more details.
00016 //  
00017 //  You should have received a copy of the GNU General Public License
00018 //  along with this program; if not, write to the Free Software
00019 //  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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

Generated on Wed Jan 25 12:04:27 2006 for DownAndOut by  doxygen 1.4.4