mmlibs/mmfw/tsrc/mmvalidationsuite/mmvalidationsuiteapp/src/SettingsManager.cpp
changeset 0 b8ed18f6c07b
equal deleted inserted replaced
-1:000000000000 0:b8ed18f6c07b
       
     1 // Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // SettingsManage.cpp
       
    15 // Part of the MVS Application for TechView
       
    16 //
       
    17 
       
    18 #include "SettingsManager.h"
       
    19 #include "MVSAppUI.h"
       
    20 #include "MVSConfigAudioFormatDialog.h"
       
    21 #include "MVSConfigVideoFormatDialog.h"
       
    22 
       
    23 _LIT(KFullPathOfFileStore,"C:\\private\\102737E8\\MvsSettings.dat");	
       
    24 
       
    25 CSettingsManager::CSettingsManager()
       
    26 :iStore(0),iHasSettings(1)
       
    27 	{	
       
    28 	
       
    29 	}
       
    30 
       
    31 CSettingsManager::~CSettingsManager()
       
    32 	{
       
    33 	iFsSession.Close();  //close the file session
       
    34 	iArrUid.Close();
       
    35 	iArrStreamId.Close();
       
    36 	if(iStore)
       
    37 		{
       
    38 		
       
    39 		}
       
    40 	}
       
    41 
       
    42 void CSettingsManager::ConstructL()
       
    43 	{
       
    44 	User::LeaveIfError(iFsSession.Connect()); // start a file session
       
    45 	}
       
    46 	
       
    47 CSettingsManager* CSettingsManager::NewL()
       
    48 	{
       
    49 	CSettingsManager* self = new(ELeave) CSettingsManager();
       
    50 	CleanupStack::PushL(self);
       
    51 	self->ConstructL();
       
    52 	CleanupStack::Pop(self);
       
    53 	return self;
       
    54 	}
       
    55 
       
    56 
       
    57 TBool CSettingsManager::OpenStore2ReadLC()
       
    58 	{
       
    59 	TParse	fileStoreName;
       
    60 	iFsSession.Parse(KFullPathOfFileStore,fileStoreName);
       
    61 	
       
    62 	iStore = CPermanentFileStore::OpenLC(iFsSession,
       
    63 										fileStoreName.FullName(),
       
    64 										EFileRead );
       
    65 	iHasSettings = ETrue;
       
    66 	return iHasSettings;
       
    67 	}
       
    68 
       
    69 void CSettingsManager::OpenStore2WriteLC()
       
    70 	{
       
    71 	TPath privatePath(KFullPathOfFileStore);
       
    72 	iFsSession.PrivatePath(privatePath);
       
    73 	TParse	fileStoreName;
       
    74 	iFsSession.MkDirAll(KFullPathOfFileStore);
       
    75 	iFsSession.Parse(KFullPathOfFileStore,fileStoreName);
       
    76 	
       
    77 	if(iHasSettings)
       
    78 		{
       
    79 		iStore = CPermanentFileStore::OpenLC(iFsSession,
       
    80 											fileStoreName.FullName(),
       
    81 											EFileWrite );
       
    82 		}
       
    83 	else
       
    84 		{
       
    85 		iStore = CPermanentFileStore::CreateLC(iFsSession,
       
    86 											fileStoreName.FullName(),
       
    87 											EFileWrite );
       
    88 		}
       
    89 	
       
    90 	iStore->SetTypeL(KPermanentFileStoreLayoutUid);	 	
       
    91 	}
       
    92 
       
    93 
       
    94 TBool CSettingsManager::HasSettings()
       
    95 	{
       
    96 	return iHasSettings;
       
    97 	}
       
    98 
       
    99 void CSettingsManager::ReadGeneralSettingsL(CMVSAppUi* aAppUI)
       
   100 	{
       
   101 	if(!FileExists())
       
   102 		return;
       
   103 	OpenStore2ReadLC();
       
   104 	RStoreReadStream instream;
       
   105 	iRootId = iStore->Root();
       
   106 	instream.OpenLC(*iStore,iRootId); //open root stream for reading
       
   107 	instream >> iGenSettingsId;
       
   108 	CleanupStack::PopAndDestroy();			
       
   109 	
       
   110 	//check for the validity of the streamid
       
   111 	if(!iGenSettingsId.Value())
       
   112 		{
       
   113 		CleanupStack::PopAndDestroy();
       
   114 		return;
       
   115 		}
       
   116 	
       
   117 	//open the stream for general settings
       
   118 	instream.OpenLC(*iStore,iGenSettingsId); 
       
   119 	aAppUI->InternalizeL(instream);
       
   120 	CleanupStack::PopAndDestroy(2);	
       
   121 	}
       
   122 
       
   123 
       
   124 void CSettingsManager::WriteGeneralSettingsL(CMVSAppUi* aAppUI)
       
   125 	{	
       
   126 	OpenStore2WriteLC();
       
   127 	
       
   128 	RStoreWriteStream outstream;
       
   129 	/*
       
   130 	if already there is stream id for general settings open the existing
       
   131 	stream; otherwise create new stream id 
       
   132 	*/
       
   133 	TBool updation = iGenSettingsId.Value();
       
   134 	if(updation)
       
   135 		{
       
   136 		outstream.ReplaceLC(*iStore,iGenSettingsId);
       
   137 		}
       
   138 	else
       
   139 		{
       
   140 		iGenSettingsId = outstream.CreateLC(*iStore);
       
   141 		}
       
   142 	
       
   143 	//write the general settings
       
   144 	aAppUI->ExternalizeL(outstream);
       
   145 	outstream.CommitL();
       
   146 	CleanupStack::PopAndDestroy();
       
   147 	
       
   148 	if(!updation)
       
   149 		{
       
   150 		outstream.ReplaceLC(*iStore,iStore->Root());
       
   151 		TUid tempUid;
       
   152 		TStreamId tempStreamId(0);
       
   153 		WriteIndexL(outstream,tempUid,tempStreamId);
       
   154 		CleanupStack::PopAndDestroy();	
       
   155 		}		
       
   156 						
       
   157 	iStore->CommitL();// commit the changes to the store
       
   158 	CleanupStack::PopAndDestroy(); //for iStore 
       
   159 	iFsSession.Close(); //close the file session
       
   160 	}	  
       
   161 
       
   162 /*
       
   163  *This function assumes that the index doesn't exist
       
   164  */
       
   165 void CSettingsManager::MakeSeedIndexL()
       
   166 	{
       
   167 	OpenStore2WriteLC();
       
   168 	RStoreWriteStream outstream;
       
   169 	TStreamId invalidId(0); //caution: confirm the reliability of this value
       
   170 	iRootId = outstream.CreateLC(*iStore);		
       
   171 	
       
   172 	//write an invalid  stream index for general settings i.e.iGenSettingsId
       
   173 	outstream << invalidId;	 
       
   174 		
       
   175 	//write no. of controllers as 0
       
   176 	outstream.WriteInt8L(0);
       
   177 	
       
   178 	outstream.CommitL(); //commit stream changes
       
   179 	CleanupStack::PopAndDestroy();
       
   180 	
       
   181 	iStore->SetRootL(iRootId);
       
   182 	iStore->CommitL(); //commit changes to store
       
   183 	
       
   184 	CleanupStack::PopAndDestroy(); //for iStore	
       
   185 	}
       
   186 
       
   187 // the stream should be in the beginning of controller section
       
   188 void CSettingsManager::ReadControllerTableL(RReadStream& aStream)
       
   189 	{
       
   190 	iControllerCnt = aStream.ReadInt8L();	
       
   191 	TStreamId tempId;
       
   192 	iArrUid.Reset();
       
   193 	iArrStreamId.Reset();
       
   194 	//read the available pairs of Uid - Streamd Ids.
       
   195 	for(TInt8 i = 0; i < iControllerCnt;i++)
       
   196 		{
       
   197 		iArrUid.AppendL(TUid::Uid(aStream.ReadInt32L()));
       
   198 		aStream >> tempId;
       
   199 		iArrStreamId.AppendL(tempId);
       
   200 		}
       
   201 	}
       
   202 
       
   203 
       
   204 TBool CSettingsManager::IsControllerAvailableL(const TUid& aUid,
       
   205 													  TStreamId&  aStreamId)
       
   206 	{
       
   207 	 // if there is no Store fiel return false
       
   208 	if(!FileExists())
       
   209 		return 0;
       
   210 	OpenStore2ReadLC();
       
   211 	RStoreReadStream instream;
       
   212 	iRootId = iStore->Root();
       
   213 	instream.OpenLC(*iStore,iRootId); //open root stream for reading
       
   214 	instream >> iGenSettingsId;	 //read this to move to controller section
       
   215 	ReadControllerTableL(instream);
       
   216 	CleanupStack::PopAndDestroy(2);			
       
   217 	for(TUint8 i = 0; i < iControllerCnt; i++)
       
   218 		{
       
   219 		if(iArrUid[i] == aUid)
       
   220 			{
       
   221 		  	aStreamId = iArrStreamId[i];
       
   222 		  	return 1;	
       
   223 			}					
       
   224 	 	}
       
   225 	return 0;
       
   226 	}
       
   227 
       
   228 
       
   229 TInt CSettingsManager::ReadAudioDataL(CMVSConfigAudioFormatDialog* aAudioFormatDlg,
       
   230  							                 const TUid& aUid)
       
   231 	{
       
   232 	TStreamId controllerId;
       
   233 	// if the controller settings is not available return
       
   234 	if(!IsControllerAvailableL(aUid,controllerId))
       
   235 		return 0;
       
   236 	
       
   237 	//open the store to read
       
   238 	if(!FileExists())
       
   239 		return 0;
       
   240 	OpenStore2ReadLC();
       
   241 	//open the stream of the given controller for reading
       
   242 	RStoreReadStream instream;
       
   243 	instream.OpenLC(*iStore,controllerId);
       
   244 	aAudioFormatDlg->InternalizeL(instream);
       
   245 	CleanupStack::PopAndDestroy(2);
       
   246 	return 1;
       
   247 	}
       
   248 
       
   249 void CSettingsManager::WriteAudioDataL(CMVSConfigAudioFormatDialog* aAudioFormat,
       
   250 					  				const TUid& aUid)
       
   251 	{
       
   252 	TStreamId controllerId;		
       
   253 	RStoreWriteStream outstream;
       
   254 	TBool existingController = IsControllerAvailableL(aUid,controllerId);
       
   255 	OpenStore2WriteLC();	
       
   256 	if(!existingController)
       
   257 		{
       
   258 		//if controller settings is not available create new stream
       
   259 		controllerId = outstream.CreateLC(*iStore);		
       
   260 		}
       
   261 	else //open the existing for updation
       
   262 		{
       
   263 		outstream.ReplaceLC(*iStore,controllerId); 				
       
   264 		}
       
   265 	aAudioFormat->ExternalizeL(outstream);
       
   266 	outstream.CommitL();
       
   267 	CleanupStack::PopAndDestroy();
       
   268 	
       
   269     /*
       
   270     if there is no updation for controller i.e. new controller settings 
       
   271     is entered
       
   272     */
       
   273 	if(!existingController)
       
   274 		{
       
   275 		outstream.ReplaceLC(*iStore,iRootId); 					
       
   276 		WriteIndexL(outstream,aUid,controllerId);
       
   277 		CleanupStack::PopAndDestroy();		
       
   278 		}
       
   279 	iStore->CommitL();
       
   280 	CleanupStack::PopAndDestroy(); //for iStore
       
   281 	}	
       
   282 
       
   283 void CSettingsManager::WriteIndexL(RWriteStream& aStream,
       
   284 								  const TUid& aUid,
       
   285 								  TStreamId& aStreamId)
       
   286 	{
       
   287 	TBool bNewPlugin = aStreamId.Value();
       
   288 	TUint8 uchExistingPluginCnt = iControllerCnt;
       
   289 	//write root index
       
   290 	aStream << iGenSettingsId;
       
   291 	
       
   292 	if(bNewPlugin)
       
   293 		{
       
   294 		iControllerCnt++;		
       
   295 		}
       
   296 	
       
   297 	aStream.WriteInt8L(iControllerCnt);
       
   298 	
       
   299 	//write the uid-streamid for existing plugins
       
   300 	for(TUint8 i = 0; i < uchExistingPluginCnt; i++)
       
   301 		{
       
   302 		aStream.WriteInt32L(iArrUid[i].iUid); //write uid
       
   303 		aStream << 	iArrStreamId[i];	 //write streamid
       
   304 		}
       
   305 	
       
   306 	if(!bNewPlugin)
       
   307 		{
       
   308 		aStream.CommitL();
       
   309 		return;	
       
   310 		}
       
   311 	
       
   312 	//write uid-streamId for new plugin
       
   313 	aStream.WriteInt32L(aUid.iUid); //write uid
       
   314 	aStream << aStreamId;			//write streamid
       
   315 		
       
   316 	iArrUid.AppendL(aUid);
       
   317 	iArrStreamId.AppendL(aStreamId);	
       
   318 	aStream.CommitL();		
       
   319 	}
       
   320 
       
   321 
       
   322 TBool CSettingsManager::ReadVideoDataL(CMVSConfigVideoFormatDialog* aVideoFormatDlg,
       
   323  							          const TUid& aUid)
       
   324 	{
       
   325 	TStreamId controllerId;
       
   326 	// if the controller settings is not available return
       
   327 	if(!IsControllerAvailableL(aUid,controllerId))
       
   328 		return 0;
       
   329 	
       
   330 	//open the store to read
       
   331 	if(!FileExists())
       
   332 		return 0;
       
   333 	OpenStore2ReadLC();
       
   334 	//open the stream of the given controller for reading
       
   335 	RStoreReadStream instream;
       
   336 	instream.OpenLC(*iStore,controllerId);
       
   337 	aVideoFormatDlg->InternalizeL(instream);
       
   338 	CleanupStack::PopAndDestroy(2); 
       
   339 	return 1;
       
   340 	}
       
   341 
       
   342 
       
   343 void  CSettingsManager::WriteVideoDataL( CMVSConfigVideoFormatDialog* aVideoFormatDlg,
       
   344 						 				const TUid& aUid)
       
   345 	{
       
   346 	TStreamId controllerId;		
       
   347 	RStoreWriteStream outstream;
       
   348 	TBool existingController = IsControllerAvailableL(aUid,controllerId);
       
   349 	OpenStore2WriteLC();	
       
   350 	if(!existingController)
       
   351 		{
       
   352 		//if controller settings is not available create new stream
       
   353 		controllerId = outstream.CreateLC(*iStore);		
       
   354 		}
       
   355 	else //open the existing for updation
       
   356 		{
       
   357 		outstream.ReplaceLC(*iStore,controllerId); 				
       
   358 		}
       
   359 	aVideoFormatDlg->ExternalizeL(outstream);
       
   360 	outstream.CommitL();
       
   361 	CleanupStack::PopAndDestroy();
       
   362 	
       
   363     /*
       
   364     if there is no updation for controller i.e. new controller settings 
       
   365     is entered
       
   366     */
       
   367 	if(!existingController)
       
   368 		{
       
   369 		outstream.ReplaceLC(*iStore,iRootId); 					
       
   370 		WriteIndexL(outstream,aUid,controllerId);
       
   371 		CleanupStack::PopAndDestroy();		
       
   372 		}
       
   373 	iStore->CommitL();
       
   374 	CleanupStack::PopAndDestroy(); //for iStore	
       
   375 	}
       
   376 
       
   377 TBool CSettingsManager::FileExists()
       
   378 	{
       
   379 	TParse fileStoreName;
       
   380 	iFsSession.Parse(KFullPathOfFileStore,fileStoreName);
       
   381 	//check whether a settings file already exists
       
   382 	RFile file;
       
   383 	if(file.Open(iFsSession,fileStoreName.FullName(),EFileRead)!= KErrNone) // if the file doesn't exist already
       
   384 		{
       
   385 		iHasSettings = 0;
       
   386 		return EFalse;
       
   387 		}
       
   388 	file.Close();
       
   389 	return ETrue;
       
   390 	}