First public contribution.
1 // Copyright (c) 2007-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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
16 #include "sysconfigimpl.h"
25 //////////////////////////////////////////////////////////////////////////////
26 CConfigSettingsImpl* CConfigSettingsImpl::NewL()
28 CConfigSettingsImpl* self=new (ELeave) CConfigSettingsImpl();
32 TBool CConfigSettingsImpl::Next(TPtrC8& aSetting,TPtrC8& aSettingValue)
38 return iIniSecIter->Next(aSetting,aSettingValue);
41 void CConfigSettingsImpl::Reset()
49 CConfigSettingsImpl::~CConfigSettingsImpl()
58 CConfigSettingsImpl::CConfigSettingsImpl():iIniSecIter(NULL)
62 ///////////////////////////////////////////////////////////////////////////////
65 CConfigImpl* CConfigImpl::NewL(RHeap* aHeap,TFileName& aFileName)
67 CConfigImpl* self=new (ELeave)CConfigImpl(aHeap,aFileName);
68 CleanupStack::PushL(self);
74 void CConfigImpl::ConstructL()
76 // Create/open a handle onto global mutex used to protect
77 // access to the config file.
78 TInt ret = KErrAlreadyExists;
79 for ( ; ret==KErrAlreadyExists; )
81 ret = iWriteMutex.OpenGlobal(KSysConfigMutex, EOwnerProcess);
82 if (ret == KErrNotFound)
83 ret = iWriteMutex.CreateGlobal(KSysConfigMutex, EOwnerProcess);
85 User::LeaveIfError(ret);
87 //establishing connection to file server
88 User::LeaveIfError(iFs.Connect());
89 // allow this connection to be shared with other threads
90 // - this facilitates sharing of config objects between threads
91 User::LeaveIfError(iFs.ShareProtected());
94 // iIniFileWatcher = NULL;
98 CConfigImpl::~CConfigImpl()
100 if (iIniFileDocument)
102 delete iIniFileDocument;
103 iIniFileDocument=NULL;
106 if (iWriteMutex.Handle() && iWriteMutex.IsHeld())
108 iWriteMutex.Signal();
111 //closing the file handle
119 Load the Ulogger config file contents.
120 The existing iIniFileDocument and caches are cleared.
121 This method assumes that iFs is connected.
123 void CConfigImpl::LoadConfigFileL()
125 //switch to private heap
126 RHeap* threadHeap = NULL;
128 threadHeap = User::SwitchHeap(iHeap);
130 // delete existing document
131 if (iIniFileDocument)
133 delete iIniFileDocument;
134 iIniFileDocument=NULL;
139 TInt ret=iFs.Entry(iFileName,dummy);
144 //Creating an instance of IniDocument from default
145 iIniFileDocument=CIniDocument8::NewL(iFs,iFileName);
148 iWriteMutex.Signal();
149 //switch back to user heap
151 User::SwitchHeap(threadHeap);
155 TInt CConfigImpl::PersistIniFile()
160 TInt ret=iIniFileDocument->Externalise(iFileName);
163 iWriteMutex.Signal();
168 TInt CConfigImpl::GetKeyValue(const TDesC8& aSectionName,const TDesC8& aKeyName ,TPtrC8& aValue)
170 return iIniFileDocument->GetKeyValue(aSectionName,aKeyName,aValue);
173 TInt CConfigImpl::RemoveKey(const TDesC8& aSectionName,const TDesC8& aKeyName)
175 return iIniFileDocument->RemoveKey(aSectionName,aKeyName);
178 TInt CConfigImpl::SetKeyValue(const TDesC8& aSectionName,const TDesC8& aKey, const TDesC8& aValue)
180 return iIniFileDocument->SetKey(aSectionName,aKey,aValue);
183 /*TInt CConfigImpl::AddSection(const TDesC8& aSectionName)
185 return iIniFileDocument->AddSection(aSectionName);
188 TInt CConfigImpl::RemoveSection(const TDesC8& aSectionName)
190 return iIniFileDocument->RemoveSection(aSectionName);
193 TInt CConfigImpl::GetSection(const TDesC8& aSectionName,CConfigSettingsIter& aSection)
195 //clear any previous internal iIniSecIterator
196 if (aSection.iImpl->iIniSecIter)
198 delete (aSection.iImpl->iIniSecIter);
199 aSection.iImpl->iIniSecIter=NULL;
201 //initializing its internal iIniSecIterator
202 TRAPD(ret,aSection.iImpl->iIniSecIter=CIniSecIter8::NewL(aSectionName,iIniFileDocument));
208 //util to check any key within that section holds that value
209 //if not found simply return the number of keys within that section
210 TInt CConfigImpl::CheckValueExist(const TDesC8& aSectionName,const TDesC8& aValue,TInt& aKeyCount)
212 CIniSecIter8* iter=NULL;
213 TRAPD(err,iter=CIniSecIter8::NewL(aSectionName,iIniFileDocument));
214 //if section not found it might be the first time we are creating
215 //the document so it is fine to return KErrNone
216 if (err==KErrNotFound)
220 //return the error code if else than KErrNotFound e.g. KErrNoMemory;
228 while (iter->Next(key,value))
230 if (value.Compare(aValue)==0)
234 return KErrAlreadyExists;
244 TInt CConfigImpl::GenerateInternalKey(const TDesC8& aSection,TBuf8<15>& aKeyName)
247 TInt ret=GetKeyCount(aSection,lastKey);
252 //either "mediaX" or "X"
255 if(aSection.Compare(KPrimaryFilterSection)== 0 )
261 TInt err = lex.Val(lastKeyValue);
265 aKeyName.Format(_L8("%03d"),++lastKeyValue);
267 else if(aSection.Compare(KSecondaryFilterSection) == 0)
273 TInt err = lex.Val(lastKeyValue);
277 aKeyName.Format(_L8("%04d"),++lastKeyValue);
285 TInt err = lex.Val(lastKeyValue);
289 aKeyName.Format(_L8("%d"),++lastKeyValue);
294 /*TInt CConfigImpl::GenerateInternalSessionName(const TDesC8& aSection,TBuf8<15>& aKeyName)
297 TInt ret=GetKeyCount(aSection,lastKey);
302 //either "mediaX" or "X"
303 TInt index=(aSection.Compare(KActiveSection)==0?5:0);
305 if (lastKey.Length()!=0)
308 lex.Assign(lastKey.Mid(index));
311 aKeyName.Format((aSection.Compare(KActiveSection)==0?_L8("Session%d"):_L8("%d")),++key);
316 TInt CConfigImpl::GetKeyCount(const TDesC8& aSectionName,TPtrC8& aLastKey)
318 CIniSecIter8* iter=NULL;
319 TRAPD(err,iter=CIniSecIter8::NewL(aSectionName,iIniFileDocument));
320 //if section not found it indicates the keycount is 0;
321 if (err==KErrNotFound)
325 //return any error code e.g KErrNoMemory here;
331 TInt keyCount=0x0000;
332 while (iter->Next(aLastKey,key))
340 TInt CConfigImpl::GetPointerToKeyName(const TDesC8& aSectionName,const TDesC8& aKeyName,TPtrC8& aKeyPointer)
342 CIniSecIter8* iter=NULL;
343 TRAPD(err,iter=CIniSecIter8::NewL(aSectionName,iIniFileDocument));
349 while (iter->Next(aKeyPointer,value))
351 if (aKeyName.Compare(aKeyPointer)==0)