1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/persistentdata/traceservices/tracefw/ulogger/src/sysconfig/sysconfigimpl.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,361 @@
1.4 +// Copyright (c) 2007-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 "sysconfigimpl.h"
1.20 +#include <hal.h>
1.21 +#include <hal_data.h>
1.22 +
1.23 +using namespace BSUL;
1.24 +
1.25 +namespace Ulogger
1.26 +{
1.27 +
1.28 +//////////////////////////////////////////////////////////////////////////////
1.29 +CConfigSettingsImpl* CConfigSettingsImpl::NewL()
1.30 + {
1.31 + CConfigSettingsImpl* self=new (ELeave) CConfigSettingsImpl();
1.32 + return self;
1.33 + }
1.34 +
1.35 +TBool CConfigSettingsImpl::Next(TPtrC8& aSetting,TPtrC8& aSettingValue)
1.36 + {
1.37 + if (!iIniSecIter)
1.38 + {
1.39 + return EFalse;
1.40 + }
1.41 + return iIniSecIter->Next(aSetting,aSettingValue);
1.42 + }
1.43 +
1.44 +void CConfigSettingsImpl::Reset()
1.45 + {
1.46 + if (iIniSecIter)
1.47 + {
1.48 + iIniSecIter->Reset();
1.49 + }
1.50 + }
1.51 +
1.52 +CConfigSettingsImpl::~CConfigSettingsImpl()
1.53 + {
1.54 + if (iIniSecIter)
1.55 + {
1.56 + delete iIniSecIter;
1.57 + iIniSecIter=NULL;
1.58 + }
1.59 + }
1.60 +
1.61 +CConfigSettingsImpl::CConfigSettingsImpl():iIniSecIter(NULL)
1.62 + {
1.63 + }
1.64 +
1.65 +///////////////////////////////////////////////////////////////////////////////
1.66 +
1.67 +
1.68 +CConfigImpl* CConfigImpl::NewL(RHeap* aHeap,TFileName& aFileName)
1.69 + {
1.70 + CConfigImpl* self=new (ELeave)CConfigImpl(aHeap,aFileName);
1.71 + CleanupStack::PushL(self);
1.72 + self->ConstructL();
1.73 + CleanupStack::Pop();
1.74 + return self;
1.75 + }
1.76 +
1.77 +void CConfigImpl::ConstructL()
1.78 + {
1.79 + // Create/open a handle onto global mutex used to protect
1.80 + // access to the config file.
1.81 + TInt ret = KErrAlreadyExists;
1.82 + for ( ; ret==KErrAlreadyExists; )
1.83 + {
1.84 + ret = iWriteMutex.OpenGlobal(KSysConfigMutex, EOwnerProcess);
1.85 + if (ret == KErrNotFound)
1.86 + ret = iWriteMutex.CreateGlobal(KSysConfigMutex, EOwnerProcess);
1.87 + }
1.88 + User::LeaveIfError(ret);
1.89 +
1.90 + //establishing connection to file server
1.91 + User::LeaveIfError(iFs.Connect());
1.92 + // allow this connection to be shared with other threads
1.93 + // - this facilitates sharing of config objects between threads
1.94 + User::LeaveIfError(iFs.ShareProtected());
1.95 +
1.96 + // load config
1.97 +// iIniFileWatcher = NULL;
1.98 + LoadConfigFileL();
1.99 + }
1.100 +
1.101 +CConfigImpl::~CConfigImpl()
1.102 + {
1.103 + if (iIniFileDocument)
1.104 + {
1.105 + delete iIniFileDocument;
1.106 + iIniFileDocument=NULL;
1.107 + }
1.108 + //closing the mutex
1.109 + if (iWriteMutex.Handle() && iWriteMutex.IsHeld())
1.110 + {
1.111 + iWriteMutex.Signal();
1.112 + }
1.113 + iWriteMutex.Close();
1.114 + //closing the file handle
1.115 + iFs.Close();
1.116 + }
1.117 +
1.118 +
1.119 +
1.120 +
1.121 +/**
1.122 +Load the Ulogger config file contents.
1.123 +The existing iIniFileDocument and caches are cleared.
1.124 +This method assumes that iFs is connected.
1.125 +*/
1.126 +void CConfigImpl::LoadConfigFileL()
1.127 + {
1.128 + //switch to private heap
1.129 + RHeap* threadHeap = NULL;
1.130 + if (iHeap)
1.131 + threadHeap = User::SwitchHeap(iHeap);
1.132 +
1.133 + // delete existing document
1.134 + if (iIniFileDocument)
1.135 + {
1.136 + delete iIniFileDocument;
1.137 + iIniFileDocument=NULL;
1.138 +
1.139 + }
1.140 +
1.141 + TEntry dummy;
1.142 + TInt ret=iFs.Entry(iFileName,dummy);
1.143 +
1.144 + // grab the mutex
1.145 + iWriteMutex.Wait();
1.146 +
1.147 + //Creating an instance of IniDocument from default
1.148 + iIniFileDocument=CIniDocument8::NewL(iFs,iFileName);
1.149 +
1.150 + // release the mutex
1.151 + iWriteMutex.Signal();
1.152 + //switch back to user heap
1.153 + if (threadHeap)
1.154 + User::SwitchHeap(threadHeap);
1.155 + }
1.156 +
1.157 +
1.158 +TInt CConfigImpl::PersistIniFile()
1.159 + {
1.160 + // grab the mutex
1.161 + iWriteMutex.Wait();
1.162 +
1.163 + TInt ret=iIniFileDocument->Externalise(iFileName);
1.164 +
1.165 + // release the mutex
1.166 + iWriteMutex.Signal();
1.167 + return ret;
1.168 + }
1.169 +
1.170 +
1.171 +TInt CConfigImpl::GetKeyValue(const TDesC8& aSectionName,const TDesC8& aKeyName ,TPtrC8& aValue)
1.172 + {
1.173 + return iIniFileDocument->GetKeyValue(aSectionName,aKeyName,aValue);
1.174 + }
1.175 +
1.176 +TInt CConfigImpl::RemoveKey(const TDesC8& aSectionName,const TDesC8& aKeyName)
1.177 + {
1.178 + return iIniFileDocument->RemoveKey(aSectionName,aKeyName);
1.179 + }
1.180 +
1.181 +TInt CConfigImpl::SetKeyValue(const TDesC8& aSectionName,const TDesC8& aKey, const TDesC8& aValue)
1.182 + {
1.183 + return iIniFileDocument->SetKey(aSectionName,aKey,aValue);
1.184 + }
1.185 +
1.186 +/*TInt CConfigImpl::AddSection(const TDesC8& aSectionName)
1.187 + {
1.188 + return iIniFileDocument->AddSection(aSectionName);
1.189 + }*/
1.190 +
1.191 +TInt CConfigImpl::RemoveSection(const TDesC8& aSectionName)
1.192 + {
1.193 + return iIniFileDocument->RemoveSection(aSectionName);
1.194 + }
1.195 +
1.196 +TInt CConfigImpl::GetSection(const TDesC8& aSectionName,CConfigSettingsIter& aSection)
1.197 + {
1.198 + //clear any previous internal iIniSecIterator
1.199 + if (aSection.iImpl->iIniSecIter)
1.200 + {
1.201 + delete (aSection.iImpl->iIniSecIter);
1.202 + aSection.iImpl->iIniSecIter=NULL;
1.203 + }
1.204 + //initializing its internal iIniSecIterator
1.205 + TRAPD(ret,aSection.iImpl->iIniSecIter=CIniSecIter8::NewL(aSectionName,iIniFileDocument));
1.206 + return ret;
1.207 + }
1.208 +
1.209 +
1.210 +
1.211 +//util to check any key within that section holds that value
1.212 +//if not found simply return the number of keys within that section
1.213 +TInt CConfigImpl::CheckValueExist(const TDesC8& aSectionName,const TDesC8& aValue,TInt& aKeyCount)
1.214 + {
1.215 + CIniSecIter8* iter=NULL;
1.216 + TRAPD(err,iter=CIniSecIter8::NewL(aSectionName,iIniFileDocument));
1.217 + //if section not found it might be the first time we are creating
1.218 + //the document so it is fine to return KErrNone
1.219 + if (err==KErrNotFound)
1.220 + {
1.221 + return KErrNone;
1.222 + }
1.223 + //return the error code if else than KErrNotFound e.g. KErrNoMemory;
1.224 + if (err!=KErrNone)
1.225 + {
1.226 + return err;
1.227 + }
1.228 + TPtrC8 key;
1.229 + TPtrC8 value;
1.230 + TInt keyCount=0;
1.231 + while (iter->Next(key,value))
1.232 + {
1.233 + if (value.Compare(aValue)==0)
1.234 + {
1.235 + delete iter;
1.236 + keyCount=0;
1.237 + return KErrAlreadyExists;
1.238 + }
1.239 + keyCount++;
1.240 + }
1.241 + aKeyCount=keyCount;
1.242 + delete iter;
1.243 + return KErrNone;
1.244 + }
1.245 +
1.246 +
1.247 +TInt CConfigImpl::GenerateInternalKey(const TDesC8& aSection,TBuf8<15>& aKeyName)
1.248 + {
1.249 + TPtrC8 lastKey;
1.250 + TInt ret=GetKeyCount(aSection,lastKey);
1.251 + if (ret<0)
1.252 + {
1.253 + return ret;
1.254 + }
1.255 + //either "mediaX" or "X"
1.256 + //TInt key=ret;
1.257 +
1.258 + if(aSection.Compare(KPrimaryFilterSection)== 0 )
1.259 + {
1.260 + TInt lastKeyValue=0;
1.261 + if(lastKey.Length())
1.262 + {
1.263 + TLex8 lex(lastKey);
1.264 + TInt err = lex.Val(lastKeyValue);
1.265 + if(err != KErrNone)
1.266 + return err;
1.267 + }
1.268 + aKeyName.Format(_L8("%03d"),++lastKeyValue);
1.269 + }
1.270 + else if(aSection.Compare(KSecondaryFilterSection) == 0)
1.271 + {
1.272 + TInt lastKeyValue=0;
1.273 + if(lastKey.Length())
1.274 + {
1.275 + TLex8 lex(lastKey);
1.276 + TInt err = lex.Val(lastKeyValue);
1.277 + if(err != KErrNone)
1.278 + return err;
1.279 + }
1.280 + aKeyName.Format(_L8("%04d"),++lastKeyValue);
1.281 + }
1.282 + else
1.283 + {
1.284 + TInt lastKeyValue=0;
1.285 + if(lastKey.Length())
1.286 + {
1.287 + TLex8 lex(lastKey);
1.288 + TInt err = lex.Val(lastKeyValue);
1.289 + if(err != KErrNone)
1.290 + return err;
1.291 + }
1.292 + aKeyName.Format(_L8("%d"),++lastKeyValue);
1.293 + }
1.294 + return KErrNone;
1.295 +}
1.296 +
1.297 +/*TInt CConfigImpl::GenerateInternalSessionName(const TDesC8& aSection,TBuf8<15>& aKeyName)
1.298 + {
1.299 + TPtrC8 lastKey;
1.300 + TInt ret=GetKeyCount(aSection,lastKey);
1.301 + if (ret<0)
1.302 + {
1.303 + return ret;
1.304 + }
1.305 + //either "mediaX" or "X"
1.306 + TInt index=(aSection.Compare(KActiveSection)==0?5:0);
1.307 + TInt key=0;
1.308 + if (lastKey.Length()!=0)
1.309 + {
1.310 + TLex8 lex;
1.311 + lex.Assign(lastKey.Mid(index));
1.312 + lex.Val(key);
1.313 + }
1.314 + aKeyName.Format((aSection.Compare(KActiveSection)==0?_L8("Session%d"):_L8("%d")),++key);
1.315 + return KErrNone;
1.316 + }*/
1.317 +
1.318 +
1.319 +TInt CConfigImpl::GetKeyCount(const TDesC8& aSectionName,TPtrC8& aLastKey)
1.320 + {
1.321 + CIniSecIter8* iter=NULL;
1.322 + TRAPD(err,iter=CIniSecIter8::NewL(aSectionName,iIniFileDocument));
1.323 + //if section not found it indicates the keycount is 0;
1.324 + if (err==KErrNotFound)
1.325 + {
1.326 + return 0;
1.327 + }
1.328 + //return any error code e.g KErrNoMemory here;
1.329 + if (err!=KErrNone)
1.330 + {
1.331 + return err;
1.332 + }
1.333 + TPtrC8 key;
1.334 + TInt keyCount=0x0000;
1.335 + while (iter->Next(aLastKey,key))
1.336 + {
1.337 + keyCount++;
1.338 + }
1.339 + delete iter;
1.340 + return keyCount;
1.341 + }
1.342 +
1.343 +TInt CConfigImpl::GetPointerToKeyName(const TDesC8& aSectionName,const TDesC8& aKeyName,TPtrC8& aKeyPointer)
1.344 + {
1.345 + CIniSecIter8* iter=NULL;
1.346 + TRAPD(err,iter=CIniSecIter8::NewL(aSectionName,iIniFileDocument));
1.347 + if (err!=KErrNone)
1.348 + {
1.349 + return err;
1.350 + }
1.351 + TPtrC8 value;
1.352 + while (iter->Next(aKeyPointer,value))
1.353 + {
1.354 + if (aKeyName.Compare(aKeyPointer)==0)
1.355 + {
1.356 + delete iter;
1.357 + return KErrNone;
1.358 + }
1.359 + }
1.360 + delete iter;
1.361 + return KErrNotFound;
1.362 + }
1.363 +}
1.364 +