sl@0: // Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // SettingsManage.cpp sl@0: // Part of the MVS Application for TechView sl@0: // sl@0: sl@0: #include "SettingsManager.h" sl@0: #include "MVSAppUI.h" sl@0: #include "MVSConfigAudioFormatDialog.h" sl@0: #include "MVSConfigVideoFormatDialog.h" sl@0: sl@0: _LIT(KFullPathOfFileStore,"C:\\private\\102737E8\\MvsSettings.dat"); sl@0: sl@0: CSettingsManager::CSettingsManager() sl@0: :iStore(0),iHasSettings(1) sl@0: { sl@0: sl@0: } sl@0: sl@0: CSettingsManager::~CSettingsManager() sl@0: { sl@0: iFsSession.Close(); //close the file session sl@0: iArrUid.Close(); sl@0: iArrStreamId.Close(); sl@0: if(iStore) sl@0: { sl@0: sl@0: } sl@0: } sl@0: sl@0: void CSettingsManager::ConstructL() sl@0: { sl@0: User::LeaveIfError(iFsSession.Connect()); // start a file session sl@0: } sl@0: sl@0: CSettingsManager* CSettingsManager::NewL() sl@0: { sl@0: CSettingsManager* self = new(ELeave) CSettingsManager(); sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(); sl@0: CleanupStack::Pop(self); sl@0: return self; sl@0: } sl@0: sl@0: sl@0: TBool CSettingsManager::OpenStore2ReadLC() sl@0: { sl@0: TParse fileStoreName; sl@0: iFsSession.Parse(KFullPathOfFileStore,fileStoreName); sl@0: sl@0: iStore = CPermanentFileStore::OpenLC(iFsSession, sl@0: fileStoreName.FullName(), sl@0: EFileRead ); sl@0: iHasSettings = ETrue; sl@0: return iHasSettings; sl@0: } sl@0: sl@0: void CSettingsManager::OpenStore2WriteLC() sl@0: { sl@0: TPath privatePath(KFullPathOfFileStore); sl@0: iFsSession.PrivatePath(privatePath); sl@0: TParse fileStoreName; sl@0: iFsSession.MkDirAll(KFullPathOfFileStore); sl@0: iFsSession.Parse(KFullPathOfFileStore,fileStoreName); sl@0: sl@0: if(iHasSettings) sl@0: { sl@0: iStore = CPermanentFileStore::OpenLC(iFsSession, sl@0: fileStoreName.FullName(), sl@0: EFileWrite ); sl@0: } sl@0: else sl@0: { sl@0: iStore = CPermanentFileStore::CreateLC(iFsSession, sl@0: fileStoreName.FullName(), sl@0: EFileWrite ); sl@0: } sl@0: sl@0: iStore->SetTypeL(KPermanentFileStoreLayoutUid); sl@0: } sl@0: sl@0: sl@0: TBool CSettingsManager::HasSettings() sl@0: { sl@0: return iHasSettings; sl@0: } sl@0: sl@0: void CSettingsManager::ReadGeneralSettingsL(CMVSAppUi* aAppUI) sl@0: { sl@0: if(!FileExists()) sl@0: return; sl@0: OpenStore2ReadLC(); sl@0: RStoreReadStream instream; sl@0: iRootId = iStore->Root(); sl@0: instream.OpenLC(*iStore,iRootId); //open root stream for reading sl@0: instream >> iGenSettingsId; sl@0: CleanupStack::PopAndDestroy(); sl@0: sl@0: //check for the validity of the streamid sl@0: if(!iGenSettingsId.Value()) sl@0: { sl@0: CleanupStack::PopAndDestroy(); sl@0: return; sl@0: } sl@0: sl@0: //open the stream for general settings sl@0: instream.OpenLC(*iStore,iGenSettingsId); sl@0: aAppUI->InternalizeL(instream); sl@0: CleanupStack::PopAndDestroy(2); sl@0: } sl@0: sl@0: sl@0: void CSettingsManager::WriteGeneralSettingsL(CMVSAppUi* aAppUI) sl@0: { sl@0: OpenStore2WriteLC(); sl@0: sl@0: RStoreWriteStream outstream; sl@0: /* sl@0: if already there is stream id for general settings open the existing sl@0: stream; otherwise create new stream id sl@0: */ sl@0: TBool updation = iGenSettingsId.Value(); sl@0: if(updation) sl@0: { sl@0: outstream.ReplaceLC(*iStore,iGenSettingsId); sl@0: } sl@0: else sl@0: { sl@0: iGenSettingsId = outstream.CreateLC(*iStore); sl@0: } sl@0: sl@0: //write the general settings sl@0: aAppUI->ExternalizeL(outstream); sl@0: outstream.CommitL(); sl@0: CleanupStack::PopAndDestroy(); sl@0: sl@0: if(!updation) sl@0: { sl@0: outstream.ReplaceLC(*iStore,iStore->Root()); sl@0: TUid tempUid; sl@0: TStreamId tempStreamId(0); sl@0: WriteIndexL(outstream,tempUid,tempStreamId); sl@0: CleanupStack::PopAndDestroy(); sl@0: } sl@0: sl@0: iStore->CommitL();// commit the changes to the store sl@0: CleanupStack::PopAndDestroy(); //for iStore sl@0: iFsSession.Close(); //close the file session sl@0: } sl@0: sl@0: /* sl@0: *This function assumes that the index doesn't exist sl@0: */ sl@0: void CSettingsManager::MakeSeedIndexL() sl@0: { sl@0: OpenStore2WriteLC(); sl@0: RStoreWriteStream outstream; sl@0: TStreamId invalidId(0); //caution: confirm the reliability of this value sl@0: iRootId = outstream.CreateLC(*iStore); sl@0: sl@0: //write an invalid stream index for general settings i.e.iGenSettingsId sl@0: outstream << invalidId; sl@0: sl@0: //write no. of controllers as 0 sl@0: outstream.WriteInt8L(0); sl@0: sl@0: outstream.CommitL(); //commit stream changes sl@0: CleanupStack::PopAndDestroy(); sl@0: sl@0: iStore->SetRootL(iRootId); sl@0: iStore->CommitL(); //commit changes to store sl@0: sl@0: CleanupStack::PopAndDestroy(); //for iStore sl@0: } sl@0: sl@0: // the stream should be in the beginning of controller section sl@0: void CSettingsManager::ReadControllerTableL(RReadStream& aStream) sl@0: { sl@0: iControllerCnt = aStream.ReadInt8L(); sl@0: TStreamId tempId; sl@0: iArrUid.Reset(); sl@0: iArrStreamId.Reset(); sl@0: //read the available pairs of Uid - Streamd Ids. sl@0: for(TInt8 i = 0; i < iControllerCnt;i++) sl@0: { sl@0: iArrUid.AppendL(TUid::Uid(aStream.ReadInt32L())); sl@0: aStream >> tempId; sl@0: iArrStreamId.AppendL(tempId); sl@0: } sl@0: } sl@0: sl@0: sl@0: TBool CSettingsManager::IsControllerAvailableL(const TUid& aUid, sl@0: TStreamId& aStreamId) sl@0: { sl@0: // if there is no Store fiel return false sl@0: if(!FileExists()) sl@0: return 0; sl@0: OpenStore2ReadLC(); sl@0: RStoreReadStream instream; sl@0: iRootId = iStore->Root(); sl@0: instream.OpenLC(*iStore,iRootId); //open root stream for reading sl@0: instream >> iGenSettingsId; //read this to move to controller section sl@0: ReadControllerTableL(instream); sl@0: CleanupStack::PopAndDestroy(2); sl@0: for(TUint8 i = 0; i < iControllerCnt; i++) sl@0: { sl@0: if(iArrUid[i] == aUid) sl@0: { sl@0: aStreamId = iArrStreamId[i]; sl@0: return 1; sl@0: } sl@0: } sl@0: return 0; sl@0: } sl@0: sl@0: sl@0: TInt CSettingsManager::ReadAudioDataL(CMVSConfigAudioFormatDialog* aAudioFormatDlg, sl@0: const TUid& aUid) sl@0: { sl@0: TStreamId controllerId; sl@0: // if the controller settings is not available return sl@0: if(!IsControllerAvailableL(aUid,controllerId)) sl@0: return 0; sl@0: sl@0: //open the store to read sl@0: if(!FileExists()) sl@0: return 0; sl@0: OpenStore2ReadLC(); sl@0: //open the stream of the given controller for reading sl@0: RStoreReadStream instream; sl@0: instream.OpenLC(*iStore,controllerId); sl@0: aAudioFormatDlg->InternalizeL(instream); sl@0: CleanupStack::PopAndDestroy(2); sl@0: return 1; sl@0: } sl@0: sl@0: void CSettingsManager::WriteAudioDataL(CMVSConfigAudioFormatDialog* aAudioFormat, sl@0: const TUid& aUid) sl@0: { sl@0: TStreamId controllerId; sl@0: RStoreWriteStream outstream; sl@0: TBool existingController = IsControllerAvailableL(aUid,controllerId); sl@0: OpenStore2WriteLC(); sl@0: if(!existingController) sl@0: { sl@0: //if controller settings is not available create new stream sl@0: controllerId = outstream.CreateLC(*iStore); sl@0: } sl@0: else //open the existing for updation sl@0: { sl@0: outstream.ReplaceLC(*iStore,controllerId); sl@0: } sl@0: aAudioFormat->ExternalizeL(outstream); sl@0: outstream.CommitL(); sl@0: CleanupStack::PopAndDestroy(); sl@0: sl@0: /* sl@0: if there is no updation for controller i.e. new controller settings sl@0: is entered sl@0: */ sl@0: if(!existingController) sl@0: { sl@0: outstream.ReplaceLC(*iStore,iRootId); sl@0: WriteIndexL(outstream,aUid,controllerId); sl@0: CleanupStack::PopAndDestroy(); sl@0: } sl@0: iStore->CommitL(); sl@0: CleanupStack::PopAndDestroy(); //for iStore sl@0: } sl@0: sl@0: void CSettingsManager::WriteIndexL(RWriteStream& aStream, sl@0: const TUid& aUid, sl@0: TStreamId& aStreamId) sl@0: { sl@0: TBool bNewPlugin = aStreamId.Value(); sl@0: TUint8 uchExistingPluginCnt = iControllerCnt; sl@0: //write root index sl@0: aStream << iGenSettingsId; sl@0: sl@0: if(bNewPlugin) sl@0: { sl@0: iControllerCnt++; sl@0: } sl@0: sl@0: aStream.WriteInt8L(iControllerCnt); sl@0: sl@0: //write the uid-streamid for existing plugins sl@0: for(TUint8 i = 0; i < uchExistingPluginCnt; i++) sl@0: { sl@0: aStream.WriteInt32L(iArrUid[i].iUid); //write uid sl@0: aStream << iArrStreamId[i]; //write streamid sl@0: } sl@0: sl@0: if(!bNewPlugin) sl@0: { sl@0: aStream.CommitL(); sl@0: return; sl@0: } sl@0: sl@0: //write uid-streamId for new plugin sl@0: aStream.WriteInt32L(aUid.iUid); //write uid sl@0: aStream << aStreamId; //write streamid sl@0: sl@0: iArrUid.AppendL(aUid); sl@0: iArrStreamId.AppendL(aStreamId); sl@0: aStream.CommitL(); sl@0: } sl@0: sl@0: sl@0: TBool CSettingsManager::ReadVideoDataL(CMVSConfigVideoFormatDialog* aVideoFormatDlg, sl@0: const TUid& aUid) sl@0: { sl@0: TStreamId controllerId; sl@0: // if the controller settings is not available return sl@0: if(!IsControllerAvailableL(aUid,controllerId)) sl@0: return 0; sl@0: sl@0: //open the store to read sl@0: if(!FileExists()) sl@0: return 0; sl@0: OpenStore2ReadLC(); sl@0: //open the stream of the given controller for reading sl@0: RStoreReadStream instream; sl@0: instream.OpenLC(*iStore,controllerId); sl@0: aVideoFormatDlg->InternalizeL(instream); sl@0: CleanupStack::PopAndDestroy(2); sl@0: return 1; sl@0: } sl@0: sl@0: sl@0: void CSettingsManager::WriteVideoDataL( CMVSConfigVideoFormatDialog* aVideoFormatDlg, sl@0: const TUid& aUid) sl@0: { sl@0: TStreamId controllerId; sl@0: RStoreWriteStream outstream; sl@0: TBool existingController = IsControllerAvailableL(aUid,controllerId); sl@0: OpenStore2WriteLC(); sl@0: if(!existingController) sl@0: { sl@0: //if controller settings is not available create new stream sl@0: controllerId = outstream.CreateLC(*iStore); sl@0: } sl@0: else //open the existing for updation sl@0: { sl@0: outstream.ReplaceLC(*iStore,controllerId); sl@0: } sl@0: aVideoFormatDlg->ExternalizeL(outstream); sl@0: outstream.CommitL(); sl@0: CleanupStack::PopAndDestroy(); sl@0: sl@0: /* sl@0: if there is no updation for controller i.e. new controller settings sl@0: is entered sl@0: */ sl@0: if(!existingController) sl@0: { sl@0: outstream.ReplaceLC(*iStore,iRootId); sl@0: WriteIndexL(outstream,aUid,controllerId); sl@0: CleanupStack::PopAndDestroy(); sl@0: } sl@0: iStore->CommitL(); sl@0: CleanupStack::PopAndDestroy(); //for iStore sl@0: } sl@0: sl@0: TBool CSettingsManager::FileExists() sl@0: { sl@0: TParse fileStoreName; sl@0: iFsSession.Parse(KFullPathOfFileStore,fileStoreName); sl@0: //check whether a settings file already exists sl@0: RFile file; sl@0: if(file.Open(iFsSession,fileStoreName.FullName(),EFileRead)!= KErrNone) // if the file doesn't exist already sl@0: { sl@0: iHasSettings = 0; sl@0: return EFalse; sl@0: } sl@0: file.Close(); sl@0: return ETrue; sl@0: }