#include "PlistPreferences.h" #include "ACFString.h" #include "ACFNumber.h" #include "CFObjDel.h" PlistPreferences::PlistPreferences(CFStringRef inPrefsIdentifier) : mPrefsIdentifier(inPrefsIdentifier) { } PlistPreferences::~PlistPreferences() { } void PlistPreferences::Init() { } void PlistPreferences::Read() { Init(); ::CFPreferencesAppSynchronize( mPrefsIdentifier ); } void PlistPreferences::Save() { ::CFPreferencesAppSynchronize( mPrefsIdentifier ); } //sets the output value only if the key exists in prefs. otherwise value remains unchanged (presumably inited to default value) Boolean PlistPreferences::GetBoolValueForKey(CFStringRef inKey, Boolean &outBoolValue) { Boolean isValid = false; Boolean boolValue = ::CFPreferencesGetAppBooleanValue(inKey, mPrefsIdentifier, &isValid ); if( isValid ) outBoolValue = boolValue; return isValid; } void PlistPreferences::SetBoolValueForKey(CFStringRef inKey, Boolean inBoolValue) { ::CFPreferencesSetAppValue( inKey, inBoolValue ? kCFBooleanTrue : kCFBooleanFalse, mPrefsIdentifier ); } //sets the output value only if the key exists in prefs. otherwise value remains unchanged (presumably inited to default value) Boolean PlistPreferences::GetIntegerValueForKey(CFStringRef inKey, CFIndex &outIntValue) { Boolean isValid = false; CFIndex intValue = ::CFPreferencesGetAppIntegerValue(inKey, mPrefsIdentifier, &isValid ); if( isValid ) outIntValue = intValue; return isValid; } void PlistPreferences::SetIntegerValueForKey(CFStringRef inKey, CFIndex inIntValue) { ACFNumber theNum(inIntValue); ::CFPreferencesSetAppValue( inKey, theNum, mPrefsIdentifier ); } //caller responslible for releasing non-null outString CFStringRef PlistPreferences::CopyStringForKey(CFStringRef inKey) { CFTypeRef resultRef = ::CFPreferencesCopyAppValue(inKey, mPrefsIdentifier); if(resultRef != NULL) { CFObjDel refDel(resultRef); if( ::CFGetTypeID(resultRef) == ::CFStringGetTypeID() ) { return (CFStringRef)refDel.Detach(); } } return NULL; } void PlistPreferences::SetStringForKey(CFStringRef inKey, CFStringRef inString) { ::CFPreferencesSetAppValue( inKey, inString, mPrefsIdentifier ); } //static helper FourCharCode PlistPreferences::CFStringToFourCharCode(CFStringRef inStrRef) { if(inStrRef == NULL) return 0; FourCharCode outValue = 0; Str31 pascalString = {0,0,0,0,0};//zero the fields we are interested in if( ::CFStringGetPascalString(inStrRef, pascalString, sizeof(pascalString), kCFStringEncodingMacRoman) ) { short theLen = pascalString[0]; if(theLen > 4) theLen = 4; else if(theLen < 4) { *(long *)(pascalString+theLen+1) = 0;//terminate with a couple of zeros } outValue = *(FourCharCode*)(pascalString+1); } return outValue; }