os/persistentdata/persistentstorage/centralrepository/pccenrep/src/pccenrepimpl.cpp
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/persistentdata/persistentstorage/centralrepository/pccenrep/src/pccenrepimpl.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,250 @@
1.4 +// Copyright (c) 2008-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 +//
1.18 +
1.19 +#include "pccenrepimpl.h"
1.20 +
1.21 +_LIT(KCreExt,".cre");
1.22 +_LIT(KTxtExt,".txt");
1.23 +_LIT(KOgnExt,".ogn");
1.24 +const TInt KExtLength=4;
1.25 +const TInt KMinReposFileLength=12;
1.26 +
1.27 +//dummy fail transaction cleanup operation
1.28 +void CPcRepImpl::FailTransactionCleanupOperation(TAny* /**aRepository*/)
1.29 + {
1.30 + //do nothing
1.31 + }
1.32 +
1.33 +CPcRepImpl* CPcRepImpl::NewL(TUid aRepositoryUid,const TDesC& aInFileName,const TDesC& aOutFileName,TBool aAutoLoading)
1.34 + {
1.35 + CPcRepImpl* self=new (ELeave)CPcRepImpl();
1.36 + CleanupStack::PushL(self);
1.37 + self->ConstructL(aRepositoryUid,aInFileName,aOutFileName,aAutoLoading);
1.38 + CleanupStack::Pop();
1.39 + return self;
1.40 + }
1.41 +
1.42 +void CPcRepImpl::ConstructL(TUid aRepositoryUid,const TDesC& aInFileName,const TDesC& aOutFileName,TBool aAutoLoading)
1.43 + {
1.44 + User::LeaveIfError(iFs.Connect());
1.45 + iRepository=CHeapRepository::NewL(aRepositoryUid);
1.46 +
1.47 + TFileName ognFile; // XXXXXXXX.ogn
1.48 + TBool isOriginal;
1.49 +
1.50 + IsOriginalL(aRepositoryUid, aOutFileName, aAutoLoading, ognFile, isOriginal);
1.51 + iRepository->SettingsArray().SetIsDefault(isOriginal);
1.52 + if (!aAutoLoading)
1.53 + {
1.54 + //verify file name must be in format of XXXXXXXX.<cre/txt> that is minimum length is 8+1+3=12
1.55 + if (aInFileName.Length()<KMinReposFileLength || aOutFileName.Length()<KMinReposFileLength)
1.56 + User::Leave(KErrArgument);
1.57 + //verify it is in the format of <path>XXXXXXXX.<cre/txt>
1.58 + TPtrC inFileName(aInFileName.Right(KMinReposFileLength));
1.59 + TPtrC outFileName(aOutFileName.Right(KMinReposFileLength));
1.60 + TUint inRepUid;
1.61 + TLex parser(inFileName.Left(8));
1.62 + TInt ret=parser.Val(inRepUid,EHex);
1.63 + if (ret!=KErrNone)
1.64 + (ret==KErrNoMemory)?User::Leave(ret):User::Leave(KErrArgument);
1.65 + parser.Assign(outFileName.Left(8));
1.66 + TUint outRepUid;
1.67 + ret=parser.Val(outRepUid,EHex);
1.68 + if (ret!=KErrNone)
1.69 + (ret==KErrNoMemory)?User::Leave(ret):User::Leave(KErrArgument);
1.70 +
1.71 + //now finally verify the extension of the output file
1.72 + if (aOutFileName.Right(KExtLength).CompareF(KCreExt())!=0)
1.73 + User::Leave(KErrArgument);
1.74 + if (aInFileName.Right(KExtLength).CompareF(KTxtExt())==0)
1.75 + {
1.76 + iRepository->SetUid(TUid::Uid(inRepUid));
1.77 + CIniFileIn* iniFile=NULL;
1.78 + ret=CIniFileIn::NewLC(iFs,iniFile,aInFileName);
1.79 + User::LeaveIfError(ret);
1.80 + iRepository->ReloadContentL(*iniFile);
1.81 + CleanupStack::PopAndDestroy();
1.82 + }
1.83 + else if (aInFileName.Right(KExtLength).CompareF(KCreExt())==0)
1.84 + {
1.85 + iRepository->SetUid(TUid::Uid(inRepUid));
1.86 + iRepository->CreateRepositoryFromCreFileL(iFs,aInFileName);
1.87 + }
1.88 + else
1.89 + User::Leave(KErrArgument);
1.90 + iOutFileName=aOutFileName.AllocL();
1.91 + }
1.92 + else
1.93 + {
1.94 + //auto mode look for CRE first then TXT
1.95 + TFileName crefile;
1.96 + crefile.AppendNumFixedWidth(aRepositoryUid.iUid,EHex,8);
1.97 + crefile.Append(KCreExt());
1.98 + TInt ret=KErrNone;
1.99 + TRAP(ret,iRepository->CreateRepositoryFromCreFileL(iFs,crefile));
1.100 + if (ret!=KErrNone)
1.101 + {
1.102 + if (ret!=KErrNotFound)
1.103 + User::Leave(ret);
1.104 + //look for TXT now
1.105 + TFileName file;
1.106 + file.AppendNumFixedWidth(aRepositoryUid.iUid,EHex,8);
1.107 + file.Append(KTxtExt());
1.108 + CIniFileIn* iniFile=NULL;
1.109 + ret=CIniFileIn::NewLC(iFs,iniFile,file);
1.110 + User::LeaveIfError(ret);
1.111 + iRepository->ReloadContentL(*iniFile);
1.112 + CleanupStack::PopAndDestroy();
1.113 + }
1.114 + //output is always cre
1.115 + iOutFileName=crefile.AllocL();
1.116 + }
1.117 + GetSingleMetaArrayL(iSingleMetaArray);
1.118 + if (isOriginal)
1.119 + {
1.120 + // An empty file is generated in the name of <Repository Uid>.ogn when
1.121 + // the repository is loaded for the first time. see IsOriginalL().
1.122 + RFile file;
1.123 + User::LeaveIfError(file.Create(iFs, ognFile, EFileWrite));
1.124 + file.Close();
1.125 + }
1.126 + iInitialised=ETrue;
1.127 + }
1.128 +
1.129 +CPcRepImpl::~CPcRepImpl()
1.130 + {
1.131 + /** persist to cre on destructor */
1.132 + if (iRepository && iInitialised)
1.133 + Flush();
1.134 +
1.135 + iFs.Close();
1.136 + iSingleMetaArray.Close();
1.137 + delete iRepository;
1.138 + delete iOutFileName;
1.139 + }
1.140 +
1.141 +TInt CPcRepImpl::Flush()
1.142 + {
1.143 +#ifdef SYMBIAN_CENTREP_SUPPORT_MULTIROFS
1.144 + return iRepository->CommitChanges(iFs,KPersistFormatSupportsIndMetaIndicator,*iOutFileName);
1.145 +#else
1.146 + return iRepository->CommitChanges(iFs,KPersistFormatVersion,*iOutFileName);
1.147 +#endif
1.148 + }
1.149 +
1.150 +void CPcRepImpl::MoveL(TUint32 aSourcePartialKey, TUint32 aTargetPartialKey,TUint32 aMask, TUint32& aErrorKey)
1.151 + {
1.152 + RSettingPointerArray sourceSettings;
1.153 + CleanupClosePushL(sourceSettings);
1.154 + TInt error=iRepository->SettingsArray().Find(aSourcePartialKey & aMask, aMask, sourceSettings);
1.155 +
1.156 + //dont fail transaction if source settings is empty
1.157 + if (error!=KErrNone)
1.158 + {
1.159 + aErrorKey = aSourcePartialKey;
1.160 + User::Leave(error);
1.161 + }
1.162 + else if (sourceSettings.Count() == 0)
1.163 + {
1.164 + aErrorKey = aSourcePartialKey;
1.165 + User::Leave(KErrNotFound);
1.166 + }
1.167 +
1.168 + for (TInt i=0;i<sourceSettings.Count();i++)
1.169 + {
1.170 + TServerSetting* ts=sourceSettings[i];
1.171 + TUint32 sourceKey = ts->Key();
1.172 + TUint32 targetKey = sourceKey ^ ((aSourcePartialKey & aMask) ^ (aTargetPartialKey & aMask));
1.173 + TServerSetting* targetSetting = GetSetting(targetKey);
1.174 + if (targetSetting && !targetSetting->IsDeleted())
1.175 + {
1.176 + aErrorKey=targetKey;
1.177 + User::Leave(KErrAlreadyExists);
1.178 + }
1.179 + }
1.180 +
1.181 + TRAPD(err,error = MOperationLogic::MoveL(aSourcePartialKey,aTargetPartialKey,aMask,aErrorKey, sourceSettings));
1.182 +
1.183 + //the Move operation logic only mark it as deleted, now remove it from the settings list
1.184 + RemoveAnyMarkDeleted();
1.185 +
1.186 + User::LeaveIfError(err);
1.187 + User::LeaveIfError(error);
1.188 +
1.189 + CleanupStack::PopAndDestroy(&sourceSettings);
1.190 + }
1.191 +
1.192 +void CPcRepImpl::DeleteRangeL(TUint32 aPartialKey, TUint32 aMask,TUint32& aErrorKey)
1.193 + {
1.194 + RSettingPointerArray sourceSettings;
1.195 + CleanupClosePushL(sourceSettings);
1.196 + TInt error=FindSettings(aPartialKey & aMask, aMask, sourceSettings);
1.197 + if (error==KErrNotFound)
1.198 + {
1.199 + aErrorKey=aPartialKey;
1.200 + }
1.201 + User::LeaveIfError(error);
1.202 + DeleteSettingsRangeL(sourceSettings,aPartialKey,aErrorKey);
1.203 + RemoveAnyMarkDeleted();
1.204 + CleanupStack::PopAndDestroy(&sourceSettings);
1.205 + }
1.206 +
1.207 +void CPcRepImpl::GetSingleMetaArrayL(RSingleMetaArray& aMetaArray)
1.208 + {
1.209 + TInt numSettings = iRepository->SettingsArray().Count();
1.210 + aMetaArray.Reset();
1.211 + aMetaArray.ReserveL(numSettings);
1.212 + for (TInt i = 0; i < numSettings; i++)
1.213 + {
1.214 + TUint32 key = iRepository->SettingsArray()[i].Key();
1.215 + TUint32 meta = iRepository->SettingsArray()[i].Meta();
1.216 + TSettingSingleMeta metaItem(key, meta);
1.217 + aMetaArray.AppendL(metaItem);
1.218 + }
1.219 + }
1.220 +
1.221 +// This function is used to identify wether the repository is loaded for the first
1.222 +// time or not. The purpose of this function is to determine whether entries of a
1.223 +// repository should be set clean during initializing process. At symbian side, this
1.224 +// flag is only set when load from ROM, but at PC side, there is no ROM, so the clean
1.225 +// flag is set when load for the first time to keep consistency with Symbian side Library.
1.226 +void CPcRepImpl::IsOriginalL(TUid aUid, const TDesC& aOutFile, TBool aAutoLoading, TFileName& aOgnFileName, TBool& aIsOriginal)
1.227 + {
1.228 + if (!aAutoLoading)
1.229 + {
1.230 + TInt len = aOutFile.Length();
1.231 + aOgnFileName = aOutFile.Left(len - KExtLength);
1.232 + }
1.233 + else
1.234 + {
1.235 + aOgnFileName.AppendNumFixedWidth(aUid.iUid,EHex,8);
1.236 + }
1.237 + aOgnFileName.Append(KOgnExt());
1.238 +
1.239 + RFile file;
1.240 + TInt err = file.Open(iFs,aOgnFileName,EFileRead|EFileShareReadersOnly);
1.241 + file.Close();
1.242 + if (err != KErrNone)
1.243 + {
1.244 + if (err == KErrNotFound || err == KErrPathNotFound)
1.245 + {
1.246 + aIsOriginal = ETrue;
1.247 + return;
1.248 + }
1.249 + else
1.250 + User::Leave(err);
1.251 + }
1.252 + aIsOriginal = EFalse;
1.253 + }