home *** CD-ROM | disk | FTP | other *** search
- /*****
- *
- * PrefUtil.c
- *
- * This is a support file for "Grant's CGI Framework".
- * Please see the license agreement that accompanies the distribution package
- * for licensing details.
- *
- * Copyright ©1996 by Grant Neufeld
- * grant@acm.com
- * http://arpp.carleton.ca/cgi/framework/
- *
- *****/
-
- #include "MyConfiguration.h"
- #if kCompileWithPreferences
-
- #include "constants.h"
- #include "globals.h"
-
- #include "CGI.h"
- #include "DebugUtil.h"
- #include "FileUtil.h"
- #include "MemoryUtil.h"
- #include "ProcessUtil.h"
-
- #include "PrefUtil.h"
-
-
- /*** LOCAL VARIABLES ***/
-
- static FSSpec vPrefFSSpec;
- static SInt16 vPrefFileRef; /* reference of the open preference file */
- static SInt16 vSavedFileRef;
-
- static UInt16 vPrefBusyTotal; /* total # of resources used from the prefs file */
-
-
- /*** LOCAL PROTOTYPES ***/
-
- static OSErr prefFileCreate ( void );
- static OSErr prefFileOpen ( Boolean );
- static OSErr prefFileClose ( void );
-
-
- /*** FUNCTIONS ***/
-
- /* */
- void
- PrefStartup ( void )
- {
- /* initialize module variables */
- vPrefFSSpec.vRefNum = nil;
- vPrefFSSpec.parID = nil;
- (vPrefFSSpec.name)[0] = nil;
- vPrefFileRef = nil;
- vSavedFileRef = nil;
- vPrefBusyTotal = nil;
- } /* PrefStartup */
-
-
- /** Preference Item Access **/
- #pragma mark -
-
- /* when loading preference resources from CGI functions, you should try to
- just get the data and release the resource very quickly (unless your
- resources are purgable - which requires extra caution on your part).
- Otherwise, you may end up clogging the host application's memory when
- running as a plugin */
-
- /* Get the resource from the preferences file.
- If the prefs file doesn't exist or doesn't contain the resource,
- the resource will be accessed from the application fork.
- If the app fork doesn't contain the resource, returns NULL.
- You must use PrefItemRelease to release the resource when done with it. */
- p_export
- Handle
- PrefItemGet ( short theResID, ResType theResType )
- {
- OSErr theErr;
- Handle thePref;
-
- theErr = prefFileOpen ( false );
-
- thePref = GetResource ( theResType, theResID );
- if ( (thePref != NULL) && (theErr == noErr) )
- {
- /* since we presumably got the resource from the prefs file,
- increment the counter so that we can track how many rsrcs have
- been pulled from the file so we can know whether it should be
- closed or not at a later point */
- vPrefBusyTotal++;
- }
-
- return thePref;
- } /* PrefItemGet */
-
-
- /* Releases a resource that was accessed via PrefItemGet. */
- p_export
- void
- PrefItemRelease ( Handle thePref )
- {
- my_assert ( thePref != NULL, "\pPreferenceReleaseRsrc: thePref is NULL" );
-
- if ( thePref != NULL )
- {
- ReleaseResource ( thePref );
-
- if ( vPrefFileRef > nil )
- {
- my_assert ( vPrefBusyTotal > nil, "\pPrefItemRelease: vPrefBusyTotal is too small" );
-
- vPrefBusyTotal--;
- }
- }
-
- if ( (vPrefBusyTotal == nil) && (vPrefFileRef > nil) )
- {
- prefFileClose ();
- }
- } /* PrefItemRelease */
-
-
- /* Save the data as the specified resource in the preferences file.
- If the file doesn't exist yet, create it.
- theName is a Pascal format string - can be a NULL ptr. */
- p_export
- OSErr
- PrefItemSave ( short theResID, ResType theResType, void *theData, short theSize, StringPtr theName )
- {
- OSErr theErr;
- Handle theOldResource;
- Handle prefHandle;
-
- my_assert ( theData != NULL, "\pPrefItemSave: theData is NULL" );
- my_assert ( theSize > nil, "\pPrefItemSave: theSize is < nil" );
-
- theErr = prefFileOpen ( true );
- if ( theErr == noErr )
- {
- /* prefs file is open and writable */
-
- /* delete existing resource in prefs file, if there is one */
- theOldResource = Get1Resource ( theResType, theResID );
- if ( theOldResource != NULL )
- {
- if ( theName == NULL )
- {
- /* caller didn't supply a name, so use the old one */
- //•••
- }
-
- RemoveResource ( theOldResource );
- DisposeHandle ( theOldResource );
- }
-
- prefHandle = MemoryNewHandle ( theSize, &theErr );
- if ( prefHandle != NULL )
- {
- /* copy the data into the new handle */
- HLock ( prefHandle );
- BlockMove ( theData, *prefHandle, theSize );
- HUnlock ( prefHandle );
-
- AddResource ( prefHandle, theResType, theResID, theName );
- UpdateResFile ( CurResFile() );
- ReleaseResource ( prefHandle );
- }
-
- prefFileClose ();
- }
-
- return theErr;
- } /* PrefItemSave */
-
- /* same as PrefItemSave except uses CGI memory calls
- Use this when calling from a CGI function. */
- p_export
- OSErr
- PrefItemCGISave ( CGIHdl theCGIHdl, short theResID, ResType theResType, void *theData, short theSize, StringPtr theName )
- {
- #if !(kCompilingForWSAPI || kCompileWithAssertions)
- #pragma unused(theCGIHdl)
- #endif
-
- OSErr theErr;
- Handle theOldResource;
- Handle prefHandle;
-
- my_assert ( theCGIHdl != NULL, "\pPrefItemCGISave: theCGIHdl is NULL" );
- my_assert ( theData != NULL, "\pPrefItemCGISave: theData is NULL" );
- my_assert ( theSize > nil, "\pPrefItemCGISave: theSize is < nil" );
-
- theErr = prefFileOpen ( true );
- if ( theErr != noErr )
- {
- /* prefs file is open and writable */
-
- /* delete existing resource in prefs file, if there is one */
- theOldResource = Get1Resource ( theResType, theResID );
- if ( theOldResource != NULL )
- {
- if ( theName == NULL )
- {
- /* caller didn't supply a name, so use the old one */
- //•••
- }
-
- RemoveResource ( theOldResource );
- DisposeHandle ( theOldResource );
- }
-
- prefHandle = CGINewHandle ( theCGIHdl, theSize, &theErr );
- if ( prefHandle != NULL )
- {
- /* copy the data into the new handle */
- HLock ( prefHandle );
- BlockMove ( theData, *prefHandle, theSize );
- HUnlock ( prefHandle );
-
- AddResource ( prefHandle, theResType, theResID, theName );
- UpdateResFile ( CurResFile() );
- ReleaseResource ( prefHandle );
- }
-
- prefFileClose ();
- }
-
- return theErr;
- } /* PrefItemCGISave */
-
-
- #pragma mark -
-
- /* Open the preferences file and set it as the current resource file. */
- static OSErr
- prefFileOpen ( Boolean createIfDoesntExist )
- {
- #if kCompileWithPreferencesFile
-
- OSErr theErr;
- // SInt16 prefVolRef;
- // SInt32 prefDir;
- Str255 prefFilename;
- Boolean isFolder; /* used for Finder alias resolving */
- Boolean wasAliased; /* used for Finder alias resolving */
-
- if ( vPrefFileRef > nil )
- {
- return noErr;
- }
-
- if ( (vPrefFSSpec.name)[0] == '\0' )
- {
- /* fsspec is empty, so the file hasn't been previously located - find it */
-
- /* we locate the preferences file in the same folder as the application */
-
- /* get the name of the preferences file */
- GetIndString ( prefFilename, krFilenamesStrs, kriFilenamePref );
-
- /* try to make the FSSpec */
- theErr = FSMakeFSSpec ( gProcessFSSpec.vRefNum, gProcessFSSpec.parID, prefFilename, &vPrefFSSpec );
- if ( (theErr == fnfErr) && createIfDoesntExist )
- {
- /* File Not Found: need to create the file */
- theErr = prefFileCreate ();
- }
- else if ( theErr == noErr )
- {
- /* we've located the file */
- /* handle any Finder aliasing without user prompts if on an unmounted
- network volume */
- theErr = ResolveAliasFileMountOption ( &vPrefFSSpec, true, &isFolder, &wasAliased, false );
- if ( isFolder )
- {
- theErr = true; //• need error to indicate folder found when file expected
- }
- }
-
- if ( theErr != noErr )
- {
- /* clear out the fsspec since we failed to set it correctly */
- (vPrefFSSpec.name)[0] = nil;
-
- return theErr;
- }
- }
-
- vSavedFileRef = CurResFile ();
-
- /* Try opening the Preferences file */
- vPrefFileRef = FSpOpenResFile ( &vPrefFSSpec, fsRdWrPerm );
- if ( vPrefFileRef == -1 )
- {
- /* the file wasn't opened */
- theErr = ResError ();
- vPrefFileRef = nil;
- }
- else
- {
- /* if the prefs file is open now, use it */
- UseResFile ( vPrefFileRef );
- theErr = noErr;
- }
-
- return theErr;
-
- #else
- #pragma unused(createIfDoesntExist)
-
- return noErr;
-
- #endif /* kCompileWithPreferencesFile */
- } /* prefFileOpen */
-
-
- /* Close the preferences file. */
- static OSErr
- prefFileClose ( void )
- {
- #if kCompileWithPreferencesFile
- if ( (vPrefBusyTotal == nil) && (vPrefFileRef != nil) )
- {
- CloseResFile ( vPrefFileRef );
- UseResFile ( vSavedFileRef );
-
- vPrefFileRef = nil;
- }
- #endif
-
- return noErr;
- } /* prefFileClose */
-
-
- /* Create the preferences file. */
- static OSErr
- prefFileCreate ( void )
- {
- OSErr theErr;
-
- my_assert ( (vPrefFSSpec.name)[0] != '\0', "\pprefFileCreate: vPrefFSSpec is incomplete" );
-
- FSpCreateResFile ( &vPrefFSSpec, kMyCreatorType, kTypePref, smSystemScript );
- theErr = ResError ();
-
- //••• add copying of default resources here
-
- return theErr;
- } /* prefFileCreate */
-
-
- /** INDIVIDUAL PREFERENCES **/
- #pragma mark -
-
- /* */
- #if 0
- void
- PrefSaveCurrentSettings ( void )
- {
-
- } /* PrefSaveCurrentSettings */
- #endif
-
-
- /* */
- void
- PrefSaveSleepTicks ( UInt32 theSleepTicks )
- {
- OSErr theErr;
- UInt32 oldSleepTicks;
-
- oldSleepTicks = ProcessSleepGetDefault ();
- if ( theSleepTicks != oldSleepTicks )
- {
- theErr = PrefItemSave ( krPrefSleepTicks, kPrefResType, &theSleepTicks, sizeof(UInt32), "\pSleepTicks" );
- }
- } /* PrefSaveSleepTicks */
-
- /* */
- void
- PrefSaveSleepTicksBusy ( UInt32 theSleepTicks )
- {
- OSErr theErr;
- UInt32 oldSleepTicks;
-
- oldSleepTicks = ProcessSleepGetBusy ();
- if ( theSleepTicks != oldSleepTicks )
- {
- theErr = PrefItemSave ( krPrefSleepTicksBusy, kPrefResType, &theSleepTicks, sizeof(UInt32), "\pSleepTicksWhenBusy" );
- }
- } /* PrefSaveSleepTicksBusy */
-
- /* */
- void
- PrefSaveDoIdleQuit ( SInt16 theBoolean )
- {
- #if kCompileWithQuitOnLongIdle
-
- OSErr theErr;
-
- if ( (gDoIdleQuit && !theBoolean) || (!gDoIdleQuit && theBoolean) )
- {
- theErr = PrefItemSave ( krPrefDoIdleQuit, kPrefResType, &theBoolean, sizeof(SInt16), "\pDoIdleQuit" );
- }
-
- #else
- #pragma unused(theBoolean)
- #endif /* kCompileWithQuitOnLongIdle */
- } /* PrefSaveDoIdleQuit */
-
- /* */
- void
- PrefSaveDoIdleQuitOnOpenApp ( SInt16 theBoolean )
- {
- #if kCompileWithQuitOnLongIdle
-
- OSErr theErr;
-
- if ( (gDoIdleQuitOnOpenApp && !theBoolean) || (!gDoIdleQuitOnOpenApp && theBoolean) )
- {
- theErr = PrefItemSave ( krPrefDoIdleQuitOnOpenApp, kPrefResType, &theBoolean, sizeof(SInt16), "\pDoIdleQuitOnOpenApp" );
- }
-
- #else
- #pragma unused(theBoolean)
- #endif /* kCompileWithQuitOnLongIdle */
- } /* PrefSaveDoIdleQuitOnOpenApp */
-
- /* */
- void
- PrefSaveIdleTimeToQuit ( UInt32 idleTime )
- {
- #if kCompileWithQuitOnLongIdle
-
- OSErr theErr;
-
- if ( idleTime != gIdleTimeToQuit )
- {
- theErr = PrefItemSave ( krPrefIdleTimeToQuit, kPrefResType, &idleTime, sizeof(UInt32), "\pIdleTimeToQuit" );
- }
-
- #else
- #pragma unused(idleTime)
- #endif /* kCompileWithQuitOnLongIdle */
- } /* PrefSaveIdleTimeToQuit */
-
-
- #endif /* kCompileWithPreferences */
- /***** EOF *****/
-