Retired Document
Important: This sample code may not represent best practices for current development. The project may use deprecated symbols and illustrate technologies and techniques that are no longer recommended.
sources/TPGPUAMPrefs.cp
// TPGPUAMPrefs.cp - Preference Object |
// |
// Apple Macintosh Developer Technical Support |
// Written by: Vinnie Moscaritolo |
// |
// Copyright (work in progress) Apple Computer, Inc All rights reserved. |
// |
// You may incorporate this sample code into your applications without |
// restriction, though the sample code has been provided "AS IS" and the |
// responsibility for its operation is 100% yours. However, what you are |
// not permitted to do is to redistribute the source as "DSC Sample Code" |
// after having made changes. If you're going to re-distribute the source, |
// we require that you make it clear in the source that the code was |
// descended from Apple Sample Code, but that you've made changes. |
// |
#include <PLStringFuncs.h> |
#include <string.h> |
#include <Files.h> |
#include "TPGPUAMPrefs.h" |
#include "TMacException.h" |
//------------------------------------------------------------------------------------ |
#pragma mark Local-Prototypes |
//------------------------------------------------------------------------------------ |
static OSErr get_prefs_name (StringPtr prefName); |
//------------------------------------------------------------------------------------ |
#pragma mark Local-Defines |
//------------------------------------------------------------------------------------ |
#define kPrefsNameStrID 128 /* name of prefs file */ |
//#define kPrefFolderName "\pPretty Good Preferences" /* name of Pref folder */ |
#define kPrefFolderName "\pPretty Mediocre Preferences" /* name of Pref folder */ |
#define kPrefsResourceName "\pPGPuam Prefs" |
//------------------------------------------------------------------------------------ |
#pragma mark - |
// --------------------------------------------------------------------------- |
static OSErr get_prefs_name (StringPtr prefName) |
// --------------------------------------------------------------------------- |
// |
// get the name of the preferences file from a resource in the application |
// |
// |
{ |
StringHandle prefStrHandle; |
OSErr ErrNo = noErr; |
prefStrHandle = GetString(kPrefsNameStrID); |
if (prefStrHandle != nil) |
BlockMove(*prefStrHandle, prefName, (Size) (**prefStrHandle) + 1); |
else ErrNo = resNotFound; |
return ErrNo; |
} |
// --------------------------------------------------------------------------- |
OSErr TPGPUAMPrefs::GetPrefSpec( FSSpec *fss ) |
// --------------------------------------------------------------------------- |
// |
{ |
Str255 prefsNameStr; |
Str255 prefsFolderStr; |
FSSpec prefsFSS; |
CInfoPBRec cpb; |
OSErr err = noErr; |
short prefsVRefNum; |
long prefsDirID; |
// get the name of the prefs file |
ThrowIfMacErr( get_prefs_name(prefsNameStr)); |
// find the pref folder |
ThrowIfMacErr( FindFolder(kOnSystemDisk, kPreferencesFolderType, kCreateFolder, &prefsFSS.vRefNum, &prefsFSS.parID )); |
// look for subfolder |
prefsVRefNum = prefsFSS.vRefNum; |
PLstrcpy(prefsFSS.name, kPrefFolderName); |
memset(&cpb, 0, sizeof cpb); |
cpb.hFileInfo.ioVRefNum = prefsFSS.vRefNum; |
cpb.hFileInfo.ioDirID = prefsFSS.parID; |
cpb.hFileInfo.ioNamePtr = prefsFSS.name; |
if (PBGetCatInfoSync( &cpb ) == noErr) |
{ |
if( (cpb.hFileInfo.ioFlAttrib & ioDirMask ) != 0 ) |
{ |
prefsDirID = cpb.dirInfo.ioDrDirID; |
} |
else |
{ |
return (dupFNErr); // "Pretty Good Preferences is a file!" |
} |
} |
else |
{ |
ThrowIfMacErr( FSpDirCreate( &prefsFSS, smSystemScript, &prefsDirID )); |
} |
// make a file spec for the prefs file |
err = FSMakeFSSpec(prefsVRefNum, prefsDirID, prefsNameStr, fss); |
return ( err ); |
} |
#define kFinderMessageStrID -16397 /* ID of STR for default finder message */ |
#define kStrType 'STR ' |
// --------------------------------------------------------------------------- |
OSErr TPGPUAMPrefs::CreatePrefFile(FSSpec *fss) |
// --------------------------------------------------------------------------- |
// |
{ |
// write pref file |
short prefsResRefNum; |
Handle versHandle; |
Handle finderMessageHandle; |
OSErr ErrNo = noErr; |
FSpCreateResFile(fss, '????' ,kPrefsFileType ,smSystemScript); |
if(!(ErrNo = ResError())) |
{ |
prefsResRefNum = FSpOpenResFile(fss, fsWrPerm); |
/* add the message to be displayed if the user tries |
to open the prefs file in the Finder (but don't add it |
if it's already in the preferences file) */ |
finderMessageHandle = (Handle) GetString(kFinderMessageStrID); |
if (finderMessageHandle != nil && |
HomeResFile((Handle) finderMessageHandle) != prefsResRefNum) { |
// copy the resource into the prefs file |
DetachResource(finderMessageHandle); |
AddResource(finderMessageHandle, kStrType, kFinderMessageStrID,"\pFinder message"); |
// if AddResource failed, dispose of the handle |
ErrNo = ResError(); |
if (ErrNo != noErr) DisposeHandle(finderMessageHandle); |
} |
versHandle = (Handle) GetResource('vers', 1); |
if (versHandle != nil && |
HomeResFile((Handle) versHandle) != prefsResRefNum) |
{ |
// copy the resource into the prefs file |
DetachResource(versHandle); |
AddResource(versHandle, 'vers', 1, nil); |
// if AddResource failed, dispose of the handle |
ErrNo = ResError(); |
if (ErrNo != noErr) DisposeHandle(versHandle); |
} |
versHandle = (Handle) GetResource('vers', 2); |
if (versHandle != nil && |
HomeResFile((Handle) versHandle) != prefsResRefNum) |
{ |
// copy the resource into the prefs file |
DetachResource(versHandle); |
AddResource(versHandle, 'vers', 2, nil); |
// if AddResource failed, dispose of the handle |
ErrNo = ResError(); |
if (ErrNo != noErr) DisposeHandle(versHandle); |
} |
// update and close the preference resource file |
CloseResFile(prefsResRefNum); |
} |
return ErrNo; |
} |
// --------------------------------------------------------------------------- |
void TPGPUAMPrefs::Finalize() |
// --------------------------------------------------------------------------- |
// |
{ |
if(fDirty) |
{ |
Set( 'disl', 0, sizeof( Boolean), &fDisclosureState); |
Set( 'cach', 0, sizeof( Boolean), &fCachePassPhrase); |
Set( 'ctim', 0, sizeof( SInt16), &fCachePassPhraseTimeLimit); |
Set( 'asrv', 0, sizeof( Boolean), &fAuthenticateServer); |
Set( 'opt1', 0, sizeof( Boolean), &fOption1); |
Set( 'opt2', 0, sizeof( Boolean), &fOption2); |
Write(); |
} |
} |
// --------------------------------------------------------------------------- |
void TPGPUAMPrefs::Initialize() |
// --------------------------------------------------------------------------- |
// |
{ |
Read(); |
if( Get('disl',0, 0, &fDisclosureState) != noErr) fDisclosureState = false; |
if( Get('cach',0, 0, &fCachePassPhrase) != noErr) fCachePassPhrase = false; |
if( Get('ctim',0, 0, &fCachePassPhraseTimeLimit) != noErr) fCachePassPhraseTimeLimit = 0; |
if( Get('asrv',0, 0, &fAuthenticateServer) != noErr) fAuthenticateServer = true; |
if( Get('opt1',0, 0, &fOption1) != noErr) fOption1 = true; |
if( Get('opt2',0, 0, &fOption2) != noErr) fOption2 = true; |
} |
// --------------------------------------------------------------------------- |
Boolean TPGPUAMPrefs::GetDisclosureState() |
// --------------------------------------------------------------------------- |
// |
{ |
return fDisclosureState; |
} |
// --------------------------------------------------------------------------- |
void TPGPUAMPrefs::SetDisclosureState(Boolean flag) |
// --------------------------------------------------------------------------- |
// |
{ |
fDisclosureState = flag ; |
fDirty= true; |
} |
// --------------------------------------------------------------------------- |
Boolean TPGPUAMPrefs::GetCachePassPhrase() |
// --------------------------------------------------------------------------- |
// |
{ |
return fCachePassPhrase; |
} |
// --------------------------------------------------------------------------- |
void TPGPUAMPrefs::SetCachePassPhrase(Boolean flag) |
// --------------------------------------------------------------------------- |
// |
{ |
fCachePassPhrase = flag ; |
fDirty= true; |
} |
// --------------------------------------------------------------------------- |
Boolean TPGPUAMPrefs::GetAuthenticateServer() |
// --------------------------------------------------------------------------- |
// |
{ |
return fAuthenticateServer; |
} |
// --------------------------------------------------------------------------- |
void TPGPUAMPrefs::SetAuthenticateServer(Boolean flag) |
// --------------------------------------------------------------------------- |
// |
{ |
fAuthenticateServer = flag ; |
fDirty= true; |
} |
// --------------------------------------------------------------------------- |
Boolean TPGPUAMPrefs::GetOption1() |
// --------------------------------------------------------------------------- |
// |
{ |
return fOption1; |
} |
// --------------------------------------------------------------------------- |
void TPGPUAMPrefs::SetOption1(Boolean flag) |
// --------------------------------------------------------------------------- |
// |
{ |
fOption1 = flag ; |
fDirty= true; |
} |
// --------------------------------------------------------------------------- |
Boolean TPGPUAMPrefs::GetOption2() |
// --------------------------------------------------------------------------- |
// |
{ |
return fOption2; |
} |
// --------------------------------------------------------------------------- |
void TPGPUAMPrefs::SetOption2(Boolean flag) |
// --------------------------------------------------------------------------- |
// |
{ |
fOption2 = flag ; |
fDirty= true; |
} |
// --------------------------------------------------------------------------- |
SInt16 TPGPUAMPrefs::GetCachePassPhraseTimeLimit() |
// --------------------------------------------------------------------------- |
// |
{ |
return fCachePassPhraseTimeLimit; |
} |
// --------------------------------------------------------------------------- |
void TPGPUAMPrefs::SetCachePassPhraseTimeLimit(SInt16 value) |
// --------------------------------------------------------------------------- |
// |
{ |
fCachePassPhraseTimeLimit = value ; |
fDirty= true; |
} |
Copyright © 2003 Apple Computer, Inc. All Rights Reserved. Terms of Use | Privacy Policy | Updated: 2003-07-22