/*============================================================*/
/*============================================================*/
/*==														==*/
/*==				Preferences Handling File				==*/
/*==														==*/
/*============================================================*/
/*============================================================*/


/*========================================================  Includes  */

#include "Globals.h"
#include "UnivUtilities.h"
#include "Prefs.h"
#include <Folders.h>
#include <GestaltEqu.h>
#include <Script.h>
#include "FileError.h"


/*========================================================  Globals  */

extern macEnvironment	thisMac;


/*========================================================  CanUseFindFolder  */

Boolean CanUseFindFolder (void)

{
	OSErr			theErr;
	long			theFeature;
	
	if (!thisMac.hasGestalt)
		return(FALSE);
	theErr = Gestalt(gestaltFindFolderAttr, &theFeature);
	if (theErr != noErr)
		return(FALSE);
	
	if (!BitTst(&theFeature, 31 - gestaltFindFolderPresent))
		return(FALSE);
	return(TRUE);
}


/*========================================================  GetPrefsFPathSyst7  */

Boolean GetPrefsFPathSyst7 (long *prefDirID, short *systemVolRef)

{
	OSErr			theErr;
	
	theErr = FindFolder(kOnSystemDisk, kPreferencesFolderType, kCreateFolder, 
		systemVolRef, prefDirID);
	if (theErr != noErr)
		return(FALSE);
	
	return(TRUE);
}


/*========================================================  CreatePrefsFolder  */

Boolean CreatePrefsFolder (short *systemVolRef)

{
	HFileParam		fileParamBlock;
	Str255			folderName;
	OSErr			theErr;
	
	GetIndString(folderName, rPrefsStringsID, kPrefsFNameIndex);
	if (folderName[0] == 0)
		PasStringCopy((StringPtr)&folderName, (StringPtr)kDefaultPrefFName);
	
	fileParamBlock.ioVRefNum = *systemVolRef;
	fileParamBlock.ioDirID = 0;
	fileParamBlock.ioNamePtr = folderName;
	fileParamBlock.ioCompletion = kNilPointer;
	
	theErr = PBDirCreate((HParmBlkPtr)&fileParamBlock, FALSE);
	if (theErr != noErr)
	{
		CheckFileError(theErr, "\pPreferences");
		return(FALSE);
	}
	return(TRUE);
}


/*========================================================  GetPrefsFPathSyst6  */

Boolean GetPrefsFPathSyst6 (short *systemVolRef)

{
	Str255			folderName;
	SysEnvRec		thisWorld;
	CInfoPBRec		catalogInfoPB;
	DirInfo			*directoryInfo = (DirInfo *) &catalogInfoPB;
	HFileInfo		*fileInfo = (HFileInfo *) &catalogInfoPB;
	WDPBRec			workingDirPB;
	long			prefDirID;
	OSErr			theErr;
	
	GetIndString(folderName, rPrefsStringsID, kPrefsFNameIndex);
	if (folderName[0] == 0)
		PasStringCopy((StringPtr)&folderName, (StringPtr)kDefaultPrefFName);
	
	theErr = SysEnvirons(2, &thisWorld);
	if (theErr != noErr)
		return(FALSE);
		
	*systemVolRef = thisWorld.sysVRefNum;
	fileInfo->ioVRefNum = *systemVolRef;
	fileInfo->ioDirID  = 0;
	fileInfo->ioFDirIndex = 0;
	fileInfo->ioNamePtr = folderName;
	fileInfo->ioCompletion = kNilPointer;
	theErr = PBGetCatInfo(&catalogInfoPB, FALSE);
	if (theErr != noErr)
	{
		if (theErr != fnfErr)
		{
			CheckFileError(theErr, "\pPreferences");
			return(FALSE);
		}
		if (!CreatePrefsFolder(systemVolRef))
			return(FALSE);
	
		directoryInfo->ioVRefNum = *systemVolRef;
		directoryInfo->ioFDirIndex = 0;
		directoryInfo->ioNamePtr = folderName;
		theErr = PBGetCatInfo(&catalogInfoPB, FALSE);
		if (theErr != noErr)
		{
			CheckFileError(theErr, "\pPreferences");
			return(FALSE);
		}
	}
	prefDirID = directoryInfo->ioDrDirID;
	
	workingDirPB.ioNamePtr = nil;
	workingDirPB.ioVRefNum = *systemVolRef;
	workingDirPB.ioWDIndex = 0;
	workingDirPB.ioWDProcID = 0;
	workingDirPB.ioWDVRefNum = 0;
	workingDirPB.ioCompletion = kNilPointer;
	theErr = PBGetWDInfo(&workingDirPB, FALSE);
	if (theErr != noErr)
	{
		CheckFileError(theErr, "\pPreferences");
		return(FALSE);
	}
		
	*systemVolRef = workingDirPB.ioWDVRefNum;
	
	workingDirPB.ioNamePtr = nil;
	workingDirPB.ioWDDirID = prefDirID;
	workingDirPB.ioVRefNum = *systemVolRef;
	workingDirPB.ioWDProcID = 0;
	workingDirPB.ioCompletion = kNilPointer;
	theErr = PBOpenWD(&workingDirPB, FALSE);
	if (theErr != noErr)
	{
		CheckFileError(theErr, "\pPreferences");
		return(FALSE);
	}
	
	*systemVolRef = workingDirPB.ioVRefNum;
	
	return(TRUE);
}


/*========================================================  WritePrefsFileSyst7  */

Boolean WritePrefsFileSyst7 (long *prefDirID, short *systemVolRef, prefsInfo *thePrefs)

{
	OSErr			theErr;
	short			fileRefNum;
	long			byteCount;
	FSSpec			theSpecs;
	Str255			fileName = kPrefFileName;
	
	theErr = FSMakeFSSpec(*systemVolRef, *prefDirID, fileName, &theSpecs);
	if (theErr != noErr)
	{
		if (theErr != fnfErr)
		{
			CheckFileError(theErr, "\pPreferences");
			return(FALSE);
		}
		theErr = FSpCreate(&theSpecs, kPrefCreatorType, kPrefFileType, smSystemScript);
		if (theErr != noErr)
		{
			CheckFileError(theErr, "\pPreferences");
			return(FALSE);
		}
	}
	theErr = FSpOpenDF(&theSpecs, fsRdWrPerm, &fileRefNum);
	if (theErr != noErr)
	{
		CheckFileError(theErr, "\pPreferences");
		return(FALSE);
	}
	
	byteCount = sizeof(*thePrefs);
	
	theErr = FSWrite(fileRefNum, &byteCount, thePrefs);
	if (theErr != noErr)
	{
		CheckFileError(theErr, "\pPreferences");
		return(FALSE);
	}
	
	theErr = FSClose(fileRefNum);
	if (theErr != noErr)
	{
		CheckFileError(theErr, "\pPreferences");
		return(FALSE);
	}
	
	return(TRUE);
}


/*========================================================  WritePrefsFileSyst6  */

Boolean WritePrefsFileSyst6 (short *systemVolRef, prefsInfo *thePrefs)

{
	OSErr			theErr;
	short			fileRefNum;
	long			byteCount;
	Str255			fileName = kPrefFileName;
	
	theErr = FSOpen(fileName, *systemVolRef, &fileRefNum);
	if (theErr != noErr)
	{
		if (theErr != fnfErr)
		{
			CheckFileError(theErr, "\pPreferences");
			return(FALSE);
		}
		theErr = Create(fileName, *systemVolRef, kPrefCreatorType, kPrefFileType);
		if (theErr != noErr)
		{
			CheckFileError(theErr, "\pPreferences");
			return(FALSE);
		}
		theErr = FSOpen(fileName, *systemVolRef, &fileRefNum);
		if (theErr != noErr)
		{
			CheckFileError(theErr, "\pPreferences");
			return(FALSE);
		}
	}
	
	byteCount = sizeof(*thePrefs);
	
	theErr = FSWrite(fileRefNum, &byteCount, thePrefs);
	if (theErr != noErr)
	{
		CheckFileError(theErr, "\pPreferences");
		return(FALSE);
	}
	
	theErr = FSClose(fileRefNum);
	if (theErr != noErr)
	{
		CheckFileError(theErr, "\pPreferences");
		return(FALSE);
	}
	
	return(TRUE);
}


/*========================================================  SavePrefs  */

Boolean SavePrefs (prefsInfo *thePrefs)

{
	long			prefDirID, systemDirID;
	short			systemVolRef;
	Boolean			canUseFSSpecs, noProblems;
	
	canUseFSSpecs = thisMac.hasSystem7;
	if (canUseFSSpecs)
	{
		canUseFSSpecs = CanUseFindFolder();
		if (canUseFSSpecs)
			noProblems = GetPrefsFPathSyst7(&prefDirID, &systemVolRef);
		else
			noProblems = GetPrefsFPathSyst6(&systemVolRef);
		if (!noProblems)
			return(FALSE);
	}
	else
	{
		noProblems = GetPrefsFPathSyst6(&systemVolRef);
		if (!noProblems)
			return(FALSE);
	}
	
	if (canUseFSSpecs)
	{
		if (!WritePrefsFileSyst7(&prefDirID, &systemVolRef, thePrefs))
			return(FALSE);
	}
	else
	{
		if (!WritePrefsFileSyst6(&systemVolRef, thePrefs))
			return(FALSE);
	}
	
	return(TRUE);
}

/*========================================================  ReadPrefsFileSyst7  */

Boolean ReadPrefsFileSyst7 (long *prefDirID, short *systemVolRef, prefsInfo *thePrefs)

{
	OSErr			theErr;
	short			fileRefNum;
	long			byteCount;
	FSSpec			theSpecs;
	Str255			fileName = kPrefFileName;
	
	theErr = FSMakeFSSpec(*systemVolRef, *prefDirID, fileName, &theSpecs);
	if (theErr != noErr)
	{
		if (theErr == fnfErr)
			return(FALSE);
		else
		{
			CheckFileError(theErr, "\pPreferences");
			return(FALSE);
		}
	}
	
	theErr = FSpOpenDF(&theSpecs, fsRdWrPerm, &fileRefNum);
	if (theErr != noErr)
	{
		CheckFileError(theErr, "\pPreferences");
		return(FALSE);
	}
	
	byteCount = sizeof(*thePrefs);
	
	theErr = FSRead(fileRefNum, &byteCount, thePrefs);
	if (theErr != noErr)
	{
		CheckFileError(theErr, "\pPreferences");
		theErr = FSClose(fileRefNum);
		return(FALSE);
	}
	
	theErr = FSClose(fileRefNum);
	if (theErr != noErr)
	{
		CheckFileError(theErr, "\pPreferences");
		return(FALSE);
	}
	
	return(TRUE);
}


/*========================================================  ReadPrefsFileSyst6  */

Boolean ReadPrefsFileSyst6 (short *systemVolRef, prefsInfo *thePrefs)

{
	OSErr			theErr;
	short			fileRefNum;
	long			byteCount;
	Str255			fileName = kPrefFileName;
	
	theErr = FSOpen(fileName, *systemVolRef, &fileRefNum);
	if (theErr != noErr)
	{
		if (theErr == fnfErr)
			return(FALSE);
		else
		{
			CheckFileError(theErr, "\pPreferences");
			return(FALSE);
		}
	}
	
	byteCount = sizeof(*thePrefs);
	
	theErr = FSRead(fileRefNum, &byteCount, thePrefs);
	if (theErr != noErr)
	{
		CheckFileError(theErr, "\pPreferences");
		theErr = FSClose(fileRefNum);
		return(FALSE);
	}
	
	theErr = FSClose(fileRefNum);
	if (theErr != noErr)
	{
		CheckFileError(theErr, "\pPreferences");
		return(FALSE);
	}
	
	return(TRUE);
}


/*========================================================  LoadPrefs  */

Boolean LoadPrefs (prefsInfo *thePrefs)

{
	long			prefDirID, systemDirID;
	short			systemVolRef;
	Boolean			canUseFSSpecs, noProblems;
	
	canUseFSSpecs = thisMac.hasSystem7;
	if (canUseFSSpecs)
	{
		canUseFSSpecs = CanUseFindFolder();
		if (canUseFSSpecs)
			noProblems = GetPrefsFPathSyst7(&prefDirID, &systemVolRef);
		else
			noProblems = GetPrefsFPathSyst6(&systemVolRef);
		if (!noProblems)
			return(FALSE);
	}
	else
	{
		noProblems = GetPrefsFPathSyst6(&systemVolRef);
		if (!noProblems)
			return(FALSE);
	}
	
	if (canUseFSSpecs)
	{
		noProblems = ReadPrefsFileSyst7(&prefDirID, &systemVolRef, thePrefs);
		if (!noProblems)
			return(FALSE);
	}
	else
	{
		noProblems = ReadPrefsFileSyst6(&systemVolRef, thePrefs);
		if (!noProblems)
			return(FALSE);
	}
	
	return(TRUE);
}


