os/mm/mmlibs/mmfw/tsrc/mmvalidationsuite/mmvalidationsuiteapp/src/SettingsManager.cpp
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/mm/mmlibs/mmfw/tsrc/mmvalidationsuite/mmvalidationsuiteapp/src/SettingsManager.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,390 @@
1.4 +// Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +// SettingsManage.cpp
1.18 +// Part of the MVS Application for TechView
1.19 +//
1.20 +
1.21 +#include "SettingsManager.h"
1.22 +#include "MVSAppUI.h"
1.23 +#include "MVSConfigAudioFormatDialog.h"
1.24 +#include "MVSConfigVideoFormatDialog.h"
1.25 +
1.26 +_LIT(KFullPathOfFileStore,"C:\\private\\102737E8\\MvsSettings.dat");
1.27 +
1.28 +CSettingsManager::CSettingsManager()
1.29 +:iStore(0),iHasSettings(1)
1.30 + {
1.31 +
1.32 + }
1.33 +
1.34 +CSettingsManager::~CSettingsManager()
1.35 + {
1.36 + iFsSession.Close(); //close the file session
1.37 + iArrUid.Close();
1.38 + iArrStreamId.Close();
1.39 + if(iStore)
1.40 + {
1.41 +
1.42 + }
1.43 + }
1.44 +
1.45 +void CSettingsManager::ConstructL()
1.46 + {
1.47 + User::LeaveIfError(iFsSession.Connect()); // start a file session
1.48 + }
1.49 +
1.50 +CSettingsManager* CSettingsManager::NewL()
1.51 + {
1.52 + CSettingsManager* self = new(ELeave) CSettingsManager();
1.53 + CleanupStack::PushL(self);
1.54 + self->ConstructL();
1.55 + CleanupStack::Pop(self);
1.56 + return self;
1.57 + }
1.58 +
1.59 +
1.60 +TBool CSettingsManager::OpenStore2ReadLC()
1.61 + {
1.62 + TParse fileStoreName;
1.63 + iFsSession.Parse(KFullPathOfFileStore,fileStoreName);
1.64 +
1.65 + iStore = CPermanentFileStore::OpenLC(iFsSession,
1.66 + fileStoreName.FullName(),
1.67 + EFileRead );
1.68 + iHasSettings = ETrue;
1.69 + return iHasSettings;
1.70 + }
1.71 +
1.72 +void CSettingsManager::OpenStore2WriteLC()
1.73 + {
1.74 + TPath privatePath(KFullPathOfFileStore);
1.75 + iFsSession.PrivatePath(privatePath);
1.76 + TParse fileStoreName;
1.77 + iFsSession.MkDirAll(KFullPathOfFileStore);
1.78 + iFsSession.Parse(KFullPathOfFileStore,fileStoreName);
1.79 +
1.80 + if(iHasSettings)
1.81 + {
1.82 + iStore = CPermanentFileStore::OpenLC(iFsSession,
1.83 + fileStoreName.FullName(),
1.84 + EFileWrite );
1.85 + }
1.86 + else
1.87 + {
1.88 + iStore = CPermanentFileStore::CreateLC(iFsSession,
1.89 + fileStoreName.FullName(),
1.90 + EFileWrite );
1.91 + }
1.92 +
1.93 + iStore->SetTypeL(KPermanentFileStoreLayoutUid);
1.94 + }
1.95 +
1.96 +
1.97 +TBool CSettingsManager::HasSettings()
1.98 + {
1.99 + return iHasSettings;
1.100 + }
1.101 +
1.102 +void CSettingsManager::ReadGeneralSettingsL(CMVSAppUi* aAppUI)
1.103 + {
1.104 + if(!FileExists())
1.105 + return;
1.106 + OpenStore2ReadLC();
1.107 + RStoreReadStream instream;
1.108 + iRootId = iStore->Root();
1.109 + instream.OpenLC(*iStore,iRootId); //open root stream for reading
1.110 + instream >> iGenSettingsId;
1.111 + CleanupStack::PopAndDestroy();
1.112 +
1.113 + //check for the validity of the streamid
1.114 + if(!iGenSettingsId.Value())
1.115 + {
1.116 + CleanupStack::PopAndDestroy();
1.117 + return;
1.118 + }
1.119 +
1.120 + //open the stream for general settings
1.121 + instream.OpenLC(*iStore,iGenSettingsId);
1.122 + aAppUI->InternalizeL(instream);
1.123 + CleanupStack::PopAndDestroy(2);
1.124 + }
1.125 +
1.126 +
1.127 +void CSettingsManager::WriteGeneralSettingsL(CMVSAppUi* aAppUI)
1.128 + {
1.129 + OpenStore2WriteLC();
1.130 +
1.131 + RStoreWriteStream outstream;
1.132 + /*
1.133 + if already there is stream id for general settings open the existing
1.134 + stream; otherwise create new stream id
1.135 + */
1.136 + TBool updation = iGenSettingsId.Value();
1.137 + if(updation)
1.138 + {
1.139 + outstream.ReplaceLC(*iStore,iGenSettingsId);
1.140 + }
1.141 + else
1.142 + {
1.143 + iGenSettingsId = outstream.CreateLC(*iStore);
1.144 + }
1.145 +
1.146 + //write the general settings
1.147 + aAppUI->ExternalizeL(outstream);
1.148 + outstream.CommitL();
1.149 + CleanupStack::PopAndDestroy();
1.150 +
1.151 + if(!updation)
1.152 + {
1.153 + outstream.ReplaceLC(*iStore,iStore->Root());
1.154 + TUid tempUid;
1.155 + TStreamId tempStreamId(0);
1.156 + WriteIndexL(outstream,tempUid,tempStreamId);
1.157 + CleanupStack::PopAndDestroy();
1.158 + }
1.159 +
1.160 + iStore->CommitL();// commit the changes to the store
1.161 + CleanupStack::PopAndDestroy(); //for iStore
1.162 + iFsSession.Close(); //close the file session
1.163 + }
1.164 +
1.165 +/*
1.166 + *This function assumes that the index doesn't exist
1.167 + */
1.168 +void CSettingsManager::MakeSeedIndexL()
1.169 + {
1.170 + OpenStore2WriteLC();
1.171 + RStoreWriteStream outstream;
1.172 + TStreamId invalidId(0); //caution: confirm the reliability of this value
1.173 + iRootId = outstream.CreateLC(*iStore);
1.174 +
1.175 + //write an invalid stream index for general settings i.e.iGenSettingsId
1.176 + outstream << invalidId;
1.177 +
1.178 + //write no. of controllers as 0
1.179 + outstream.WriteInt8L(0);
1.180 +
1.181 + outstream.CommitL(); //commit stream changes
1.182 + CleanupStack::PopAndDestroy();
1.183 +
1.184 + iStore->SetRootL(iRootId);
1.185 + iStore->CommitL(); //commit changes to store
1.186 +
1.187 + CleanupStack::PopAndDestroy(); //for iStore
1.188 + }
1.189 +
1.190 +// the stream should be in the beginning of controller section
1.191 +void CSettingsManager::ReadControllerTableL(RReadStream& aStream)
1.192 + {
1.193 + iControllerCnt = aStream.ReadInt8L();
1.194 + TStreamId tempId;
1.195 + iArrUid.Reset();
1.196 + iArrStreamId.Reset();
1.197 + //read the available pairs of Uid - Streamd Ids.
1.198 + for(TInt8 i = 0; i < iControllerCnt;i++)
1.199 + {
1.200 + iArrUid.AppendL(TUid::Uid(aStream.ReadInt32L()));
1.201 + aStream >> tempId;
1.202 + iArrStreamId.AppendL(tempId);
1.203 + }
1.204 + }
1.205 +
1.206 +
1.207 +TBool CSettingsManager::IsControllerAvailableL(const TUid& aUid,
1.208 + TStreamId& aStreamId)
1.209 + {
1.210 + // if there is no Store fiel return false
1.211 + if(!FileExists())
1.212 + return 0;
1.213 + OpenStore2ReadLC();
1.214 + RStoreReadStream instream;
1.215 + iRootId = iStore->Root();
1.216 + instream.OpenLC(*iStore,iRootId); //open root stream for reading
1.217 + instream >> iGenSettingsId; //read this to move to controller section
1.218 + ReadControllerTableL(instream);
1.219 + CleanupStack::PopAndDestroy(2);
1.220 + for(TUint8 i = 0; i < iControllerCnt; i++)
1.221 + {
1.222 + if(iArrUid[i] == aUid)
1.223 + {
1.224 + aStreamId = iArrStreamId[i];
1.225 + return 1;
1.226 + }
1.227 + }
1.228 + return 0;
1.229 + }
1.230 +
1.231 +
1.232 +TInt CSettingsManager::ReadAudioDataL(CMVSConfigAudioFormatDialog* aAudioFormatDlg,
1.233 + const TUid& aUid)
1.234 + {
1.235 + TStreamId controllerId;
1.236 + // if the controller settings is not available return
1.237 + if(!IsControllerAvailableL(aUid,controllerId))
1.238 + return 0;
1.239 +
1.240 + //open the store to read
1.241 + if(!FileExists())
1.242 + return 0;
1.243 + OpenStore2ReadLC();
1.244 + //open the stream of the given controller for reading
1.245 + RStoreReadStream instream;
1.246 + instream.OpenLC(*iStore,controllerId);
1.247 + aAudioFormatDlg->InternalizeL(instream);
1.248 + CleanupStack::PopAndDestroy(2);
1.249 + return 1;
1.250 + }
1.251 +
1.252 +void CSettingsManager::WriteAudioDataL(CMVSConfigAudioFormatDialog* aAudioFormat,
1.253 + const TUid& aUid)
1.254 + {
1.255 + TStreamId controllerId;
1.256 + RStoreWriteStream outstream;
1.257 + TBool existingController = IsControllerAvailableL(aUid,controllerId);
1.258 + OpenStore2WriteLC();
1.259 + if(!existingController)
1.260 + {
1.261 + //if controller settings is not available create new stream
1.262 + controllerId = outstream.CreateLC(*iStore);
1.263 + }
1.264 + else //open the existing for updation
1.265 + {
1.266 + outstream.ReplaceLC(*iStore,controllerId);
1.267 + }
1.268 + aAudioFormat->ExternalizeL(outstream);
1.269 + outstream.CommitL();
1.270 + CleanupStack::PopAndDestroy();
1.271 +
1.272 + /*
1.273 + if there is no updation for controller i.e. new controller settings
1.274 + is entered
1.275 + */
1.276 + if(!existingController)
1.277 + {
1.278 + outstream.ReplaceLC(*iStore,iRootId);
1.279 + WriteIndexL(outstream,aUid,controllerId);
1.280 + CleanupStack::PopAndDestroy();
1.281 + }
1.282 + iStore->CommitL();
1.283 + CleanupStack::PopAndDestroy(); //for iStore
1.284 + }
1.285 +
1.286 +void CSettingsManager::WriteIndexL(RWriteStream& aStream,
1.287 + const TUid& aUid,
1.288 + TStreamId& aStreamId)
1.289 + {
1.290 + TBool bNewPlugin = aStreamId.Value();
1.291 + TUint8 uchExistingPluginCnt = iControllerCnt;
1.292 + //write root index
1.293 + aStream << iGenSettingsId;
1.294 +
1.295 + if(bNewPlugin)
1.296 + {
1.297 + iControllerCnt++;
1.298 + }
1.299 +
1.300 + aStream.WriteInt8L(iControllerCnt);
1.301 +
1.302 + //write the uid-streamid for existing plugins
1.303 + for(TUint8 i = 0; i < uchExistingPluginCnt; i++)
1.304 + {
1.305 + aStream.WriteInt32L(iArrUid[i].iUid); //write uid
1.306 + aStream << iArrStreamId[i]; //write streamid
1.307 + }
1.308 +
1.309 + if(!bNewPlugin)
1.310 + {
1.311 + aStream.CommitL();
1.312 + return;
1.313 + }
1.314 +
1.315 + //write uid-streamId for new plugin
1.316 + aStream.WriteInt32L(aUid.iUid); //write uid
1.317 + aStream << aStreamId; //write streamid
1.318 +
1.319 + iArrUid.AppendL(aUid);
1.320 + iArrStreamId.AppendL(aStreamId);
1.321 + aStream.CommitL();
1.322 + }
1.323 +
1.324 +
1.325 +TBool CSettingsManager::ReadVideoDataL(CMVSConfigVideoFormatDialog* aVideoFormatDlg,
1.326 + const TUid& aUid)
1.327 + {
1.328 + TStreamId controllerId;
1.329 + // if the controller settings is not available return
1.330 + if(!IsControllerAvailableL(aUid,controllerId))
1.331 + return 0;
1.332 +
1.333 + //open the store to read
1.334 + if(!FileExists())
1.335 + return 0;
1.336 + OpenStore2ReadLC();
1.337 + //open the stream of the given controller for reading
1.338 + RStoreReadStream instream;
1.339 + instream.OpenLC(*iStore,controllerId);
1.340 + aVideoFormatDlg->InternalizeL(instream);
1.341 + CleanupStack::PopAndDestroy(2);
1.342 + return 1;
1.343 + }
1.344 +
1.345 +
1.346 +void CSettingsManager::WriteVideoDataL( CMVSConfigVideoFormatDialog* aVideoFormatDlg,
1.347 + const TUid& aUid)
1.348 + {
1.349 + TStreamId controllerId;
1.350 + RStoreWriteStream outstream;
1.351 + TBool existingController = IsControllerAvailableL(aUid,controllerId);
1.352 + OpenStore2WriteLC();
1.353 + if(!existingController)
1.354 + {
1.355 + //if controller settings is not available create new stream
1.356 + controllerId = outstream.CreateLC(*iStore);
1.357 + }
1.358 + else //open the existing for updation
1.359 + {
1.360 + outstream.ReplaceLC(*iStore,controllerId);
1.361 + }
1.362 + aVideoFormatDlg->ExternalizeL(outstream);
1.363 + outstream.CommitL();
1.364 + CleanupStack::PopAndDestroy();
1.365 +
1.366 + /*
1.367 + if there is no updation for controller i.e. new controller settings
1.368 + is entered
1.369 + */
1.370 + if(!existingController)
1.371 + {
1.372 + outstream.ReplaceLC(*iStore,iRootId);
1.373 + WriteIndexL(outstream,aUid,controllerId);
1.374 + CleanupStack::PopAndDestroy();
1.375 + }
1.376 + iStore->CommitL();
1.377 + CleanupStack::PopAndDestroy(); //for iStore
1.378 + }
1.379 +
1.380 +TBool CSettingsManager::FileExists()
1.381 + {
1.382 + TParse fileStoreName;
1.383 + iFsSession.Parse(KFullPathOfFileStore,fileStoreName);
1.384 + //check whether a settings file already exists
1.385 + RFile file;
1.386 + if(file.Open(iFsSession,fileStoreName.FullName(),EFileRead)!= KErrNone) // if the file doesn't exist already
1.387 + {
1.388 + iHasSettings = 0;
1.389 + return EFalse;
1.390 + }
1.391 + file.Close();
1.392 + return ETrue;
1.393 + }