#include <sys/stat.h> // for file and dir stat calls
#include <CoreFoundation/CFDictionary.h>
#include "CPluginConfig.h"
#include "CFile.h"
#include "CLog.h"
CPluginConfig::CPluginConfig ( void )
{
fPlistRef = nil;
fDictRef = nil;
}
CPluginConfig::~CPluginConfig()
{
if ( fPlistRef != nil )
{
::CFRelease( fPlistRef );
fPlistRef = nil;
}
}
sInt32 CPluginConfig::Initialize ( void )
{
bool bSuccess = false;
int iResult = 0;
uInt32 uiDataSize = 0;
char *pData = nil;
CFile *pFile = nil;
struct stat statbuf;
CFDataRef dataRef = nil;
try
{
iResult = ::stat( kConfigFilePath, &statbuf );
if ( iResult == 0 )
{
pFile = new CFile( kConfigFilePath );
if ( (pFile != nil) && (pFile->FileSize() > 0) )
{
pData = (char *)::malloc( pFile->FileSize() + 1 );
if ( pData != nil )
{
uiDataSize = pFile->ReadBlock( pData, pFile->FileSize() );
dataRef = ::CFDataCreate( nil, (const uInt8 *)pData, uiDataSize );
if ( dataRef != nil )
{
fPlistRef = ::CFPropertyListCreateFromXMLData( kCFAllocatorDefault, dataRef, kCFPropertyListMutableContainersAndLeaves, nil );
if ( fPlistRef != nil )
{
if ( ::CFDictionaryGetTypeID() == ::CFGetTypeID( fPlistRef ) )
{
fDictRef = (CFMutableDictionaryRef)fPlistRef;
bSuccess = true;
}
}
CFRelease( dataRef );
dataRef = nil;
}
free( pData );
pData = nil;
}
delete( pFile );
pFile = nil;
}
}
if ( bSuccess == false )
{
uiDataSize = ::strlen( kDefaultConfig );
dataRef = ::CFDataCreate( nil, (const uInt8 *)kDefaultConfig, uiDataSize );
if ( dataRef != nil )
{
fPlistRef = (CFMutableDictionaryRef)::CFPropertyListCreateFromXMLData( kCFAllocatorDefault, dataRef, kCFPropertyListMutableContainersAndLeaves, nil );
if ( fPlistRef != nil )
{
if ( ::CFDictionaryGetTypeID() == ::CFGetTypeID( fPlistRef ) )
{
fDictRef = (CFMutableDictionaryRef)fPlistRef;
bSuccess = true;
}
}
CFRelease( dataRef );
dataRef = nil;
}
}
}
catch ( ... )
{
}
return( noErr );
}
ePluginState CPluginConfig::GetPluginState ( const char *inPluginName )
{
ePluginState epsResult = kUnknownState;
bool bFound = false;
CFStringRef cfStringRef = nil;
char *pValue = nil;
CFStringRef keyStrRef = nil;
if ( (fDictRef != nil) && (inPluginName != nil) )
{
keyStrRef = ::CFStringCreateWithCString( NULL, inPluginName, kCFStringEncodingMacRoman );
if ( keyStrRef != nil )
{
bFound = ::CFDictionaryContainsKey( fDictRef, keyStrRef );
if ( bFound == true )
{
cfStringRef = (CFStringRef)::CFDictionaryGetValue( fDictRef, keyStrRef );
if ( cfStringRef != nil )
{
if ( ::CFGetTypeID( cfStringRef ) == ::CFStringGetTypeID() )
{
pValue = (char *)::CFStringGetCStringPtr( cfStringRef, kCFStringEncodingMacRoman );
if ( pValue != nil )
{
if ( ::strcmp( pValue, kActiveValue ) == 0 )
{
epsResult = kActive;
}
else if ( ::strcmp( pValue, kInactiveValue ) == 0 )
{
epsResult = kInactive;
}
}
}
}
}
::CFRelease( keyStrRef );
keyStrRef = nil;
}
}
return( epsResult );
}
sInt32 CPluginConfig::SetPluginState ( const char *inPluginName, const ePluginState inPluginState )
{
CFStringRef keyStrRef = nil;
if ( (fDictRef != nil) && (inPluginName != nil) )
{
keyStrRef = ::CFStringCreateWithCString( NULL, inPluginName, kCFStringEncodingMacRoman );
if ( keyStrRef != nil )
{
if ( inPluginState == kActive )
{
::CFDictionarySetValue( fDictRef, keyStrRef, CFSTR( kActiveValue ) );
}
else if ( inPluginState == kInactive )
{
::CFDictionarySetValue( fDictRef, keyStrRef, CFSTR( kInactiveValue ) );
}
::CFRelease( keyStrRef );
keyStrRef = nil;
}
}
return( noErr );
}
sInt32 CPluginConfig::SaveConfigData ( void )
{
CFDataRef dataRef = nil;
int result = 0;;
struct stat statResult;
if ( fDictRef != nil )
{
dataRef = ::CFPropertyListCreateXMLData( NULL, fDictRef );
if ( dataRef != nil )
{
result = ::stat( kConfigFilePath, &statResult );
if (result != eDSNoErr)
{
result = ::stat( "/Library/Preferences", &statResult );
if (result != eDSNoErr)
{
::mkdir( "/Library/Preferences", 0775 );
::chmod( "/Library/Preferences", 0775 ); }
result = ::stat( "/Library/Preferences/DirectoryService", &statResult );
if (result != eDSNoErr)
{
::mkdir( "/Library/Preferences/DirectoryService", 0775 );
::chmod( "/Library/Preferences/DirectoryService", 0775 ); }
}
UInt8 *pData = (UInt8*)::calloc( CFDataGetLength(dataRef), 1 );
CFDataGetBytes( dataRef, CFRangeMake(0,CFDataGetLength(dataRef)), pData );
if ( (pData != nil) && (pData[0] != 0) )
{
try
{
CFile *pFile = new CFile( kConfigFilePath, true );
if ( pFile != nil )
{
pFile->seteof( 0 );
pFile->write( pData, CFDataGetLength(dataRef) );
delete( pFile );
pFile = nil;
}
}
catch ( ... )
{
}
}
CFRelease( dataRef );
dataRef = nil;
}
}
return( noErr );
}