sl@0
|
1 |
// Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
|
sl@0
|
2 |
// All rights reserved.
|
sl@0
|
3 |
// This component and the accompanying materials are made available
|
sl@0
|
4 |
// under the terms of "Eclipse Public License v1.0"
|
sl@0
|
5 |
// which accompanies this distribution, and is available
|
sl@0
|
6 |
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
sl@0
|
7 |
//
|
sl@0
|
8 |
// Initial Contributors:
|
sl@0
|
9 |
// Nokia Corporation - initial contribution.
|
sl@0
|
10 |
//
|
sl@0
|
11 |
// Contributors:
|
sl@0
|
12 |
//
|
sl@0
|
13 |
// Description:
|
sl@0
|
14 |
//
|
sl@0
|
15 |
|
sl@0
|
16 |
#include "sysconfigimpl.h"
|
sl@0
|
17 |
#include <hal.h>
|
sl@0
|
18 |
#include <hal_data.h>
|
sl@0
|
19 |
|
sl@0
|
20 |
using namespace BSUL;
|
sl@0
|
21 |
|
sl@0
|
22 |
namespace Ulogger
|
sl@0
|
23 |
{
|
sl@0
|
24 |
|
sl@0
|
25 |
//////////////////////////////////////////////////////////////////////////////
|
sl@0
|
26 |
CConfigSettingsImpl* CConfigSettingsImpl::NewL()
|
sl@0
|
27 |
{
|
sl@0
|
28 |
CConfigSettingsImpl* self=new (ELeave) CConfigSettingsImpl();
|
sl@0
|
29 |
return self;
|
sl@0
|
30 |
}
|
sl@0
|
31 |
|
sl@0
|
32 |
TBool CConfigSettingsImpl::Next(TPtrC8& aSetting,TPtrC8& aSettingValue)
|
sl@0
|
33 |
{
|
sl@0
|
34 |
if (!iIniSecIter)
|
sl@0
|
35 |
{
|
sl@0
|
36 |
return EFalse;
|
sl@0
|
37 |
}
|
sl@0
|
38 |
return iIniSecIter->Next(aSetting,aSettingValue);
|
sl@0
|
39 |
}
|
sl@0
|
40 |
|
sl@0
|
41 |
void CConfigSettingsImpl::Reset()
|
sl@0
|
42 |
{
|
sl@0
|
43 |
if (iIniSecIter)
|
sl@0
|
44 |
{
|
sl@0
|
45 |
iIniSecIter->Reset();
|
sl@0
|
46 |
}
|
sl@0
|
47 |
}
|
sl@0
|
48 |
|
sl@0
|
49 |
CConfigSettingsImpl::~CConfigSettingsImpl()
|
sl@0
|
50 |
{
|
sl@0
|
51 |
if (iIniSecIter)
|
sl@0
|
52 |
{
|
sl@0
|
53 |
delete iIniSecIter;
|
sl@0
|
54 |
iIniSecIter=NULL;
|
sl@0
|
55 |
}
|
sl@0
|
56 |
}
|
sl@0
|
57 |
|
sl@0
|
58 |
CConfigSettingsImpl::CConfigSettingsImpl():iIniSecIter(NULL)
|
sl@0
|
59 |
{
|
sl@0
|
60 |
}
|
sl@0
|
61 |
|
sl@0
|
62 |
///////////////////////////////////////////////////////////////////////////////
|
sl@0
|
63 |
|
sl@0
|
64 |
|
sl@0
|
65 |
CConfigImpl* CConfigImpl::NewL(RHeap* aHeap,TFileName& aFileName)
|
sl@0
|
66 |
{
|
sl@0
|
67 |
CConfigImpl* self=new (ELeave)CConfigImpl(aHeap,aFileName);
|
sl@0
|
68 |
CleanupStack::PushL(self);
|
sl@0
|
69 |
self->ConstructL();
|
sl@0
|
70 |
CleanupStack::Pop();
|
sl@0
|
71 |
return self;
|
sl@0
|
72 |
}
|
sl@0
|
73 |
|
sl@0
|
74 |
void CConfigImpl::ConstructL()
|
sl@0
|
75 |
{
|
sl@0
|
76 |
// Create/open a handle onto global mutex used to protect
|
sl@0
|
77 |
// access to the config file.
|
sl@0
|
78 |
TInt ret = KErrAlreadyExists;
|
sl@0
|
79 |
for ( ; ret==KErrAlreadyExists; )
|
sl@0
|
80 |
{
|
sl@0
|
81 |
ret = iWriteMutex.OpenGlobal(KSysConfigMutex, EOwnerProcess);
|
sl@0
|
82 |
if (ret == KErrNotFound)
|
sl@0
|
83 |
ret = iWriteMutex.CreateGlobal(KSysConfigMutex, EOwnerProcess);
|
sl@0
|
84 |
}
|
sl@0
|
85 |
User::LeaveIfError(ret);
|
sl@0
|
86 |
|
sl@0
|
87 |
//establishing connection to file server
|
sl@0
|
88 |
User::LeaveIfError(iFs.Connect());
|
sl@0
|
89 |
// allow this connection to be shared with other threads
|
sl@0
|
90 |
// - this facilitates sharing of config objects between threads
|
sl@0
|
91 |
User::LeaveIfError(iFs.ShareProtected());
|
sl@0
|
92 |
|
sl@0
|
93 |
// load config
|
sl@0
|
94 |
// iIniFileWatcher = NULL;
|
sl@0
|
95 |
LoadConfigFileL();
|
sl@0
|
96 |
}
|
sl@0
|
97 |
|
sl@0
|
98 |
CConfigImpl::~CConfigImpl()
|
sl@0
|
99 |
{
|
sl@0
|
100 |
if (iIniFileDocument)
|
sl@0
|
101 |
{
|
sl@0
|
102 |
delete iIniFileDocument;
|
sl@0
|
103 |
iIniFileDocument=NULL;
|
sl@0
|
104 |
}
|
sl@0
|
105 |
//closing the mutex
|
sl@0
|
106 |
if (iWriteMutex.Handle() && iWriteMutex.IsHeld())
|
sl@0
|
107 |
{
|
sl@0
|
108 |
iWriteMutex.Signal();
|
sl@0
|
109 |
}
|
sl@0
|
110 |
iWriteMutex.Close();
|
sl@0
|
111 |
//closing the file handle
|
sl@0
|
112 |
iFs.Close();
|
sl@0
|
113 |
}
|
sl@0
|
114 |
|
sl@0
|
115 |
|
sl@0
|
116 |
|
sl@0
|
117 |
|
sl@0
|
118 |
/**
|
sl@0
|
119 |
Load the Ulogger config file contents.
|
sl@0
|
120 |
The existing iIniFileDocument and caches are cleared.
|
sl@0
|
121 |
This method assumes that iFs is connected.
|
sl@0
|
122 |
*/
|
sl@0
|
123 |
void CConfigImpl::LoadConfigFileL()
|
sl@0
|
124 |
{
|
sl@0
|
125 |
//switch to private heap
|
sl@0
|
126 |
RHeap* threadHeap = NULL;
|
sl@0
|
127 |
if (iHeap)
|
sl@0
|
128 |
threadHeap = User::SwitchHeap(iHeap);
|
sl@0
|
129 |
|
sl@0
|
130 |
// delete existing document
|
sl@0
|
131 |
if (iIniFileDocument)
|
sl@0
|
132 |
{
|
sl@0
|
133 |
delete iIniFileDocument;
|
sl@0
|
134 |
iIniFileDocument=NULL;
|
sl@0
|
135 |
|
sl@0
|
136 |
}
|
sl@0
|
137 |
|
sl@0
|
138 |
TEntry dummy;
|
sl@0
|
139 |
TInt ret=iFs.Entry(iFileName,dummy);
|
sl@0
|
140 |
|
sl@0
|
141 |
// grab the mutex
|
sl@0
|
142 |
iWriteMutex.Wait();
|
sl@0
|
143 |
|
sl@0
|
144 |
//Creating an instance of IniDocument from default
|
sl@0
|
145 |
iIniFileDocument=CIniDocument8::NewL(iFs,iFileName);
|
sl@0
|
146 |
|
sl@0
|
147 |
// release the mutex
|
sl@0
|
148 |
iWriteMutex.Signal();
|
sl@0
|
149 |
//switch back to user heap
|
sl@0
|
150 |
if (threadHeap)
|
sl@0
|
151 |
User::SwitchHeap(threadHeap);
|
sl@0
|
152 |
}
|
sl@0
|
153 |
|
sl@0
|
154 |
|
sl@0
|
155 |
TInt CConfigImpl::PersistIniFile()
|
sl@0
|
156 |
{
|
sl@0
|
157 |
// grab the mutex
|
sl@0
|
158 |
iWriteMutex.Wait();
|
sl@0
|
159 |
|
sl@0
|
160 |
TInt ret=iIniFileDocument->Externalise(iFileName);
|
sl@0
|
161 |
|
sl@0
|
162 |
// release the mutex
|
sl@0
|
163 |
iWriteMutex.Signal();
|
sl@0
|
164 |
return ret;
|
sl@0
|
165 |
}
|
sl@0
|
166 |
|
sl@0
|
167 |
|
sl@0
|
168 |
TInt CConfigImpl::GetKeyValue(const TDesC8& aSectionName,const TDesC8& aKeyName ,TPtrC8& aValue)
|
sl@0
|
169 |
{
|
sl@0
|
170 |
return iIniFileDocument->GetKeyValue(aSectionName,aKeyName,aValue);
|
sl@0
|
171 |
}
|
sl@0
|
172 |
|
sl@0
|
173 |
TInt CConfigImpl::RemoveKey(const TDesC8& aSectionName,const TDesC8& aKeyName)
|
sl@0
|
174 |
{
|
sl@0
|
175 |
return iIniFileDocument->RemoveKey(aSectionName,aKeyName);
|
sl@0
|
176 |
}
|
sl@0
|
177 |
|
sl@0
|
178 |
TInt CConfigImpl::SetKeyValue(const TDesC8& aSectionName,const TDesC8& aKey, const TDesC8& aValue)
|
sl@0
|
179 |
{
|
sl@0
|
180 |
return iIniFileDocument->SetKey(aSectionName,aKey,aValue);
|
sl@0
|
181 |
}
|
sl@0
|
182 |
|
sl@0
|
183 |
/*TInt CConfigImpl::AddSection(const TDesC8& aSectionName)
|
sl@0
|
184 |
{
|
sl@0
|
185 |
return iIniFileDocument->AddSection(aSectionName);
|
sl@0
|
186 |
}*/
|
sl@0
|
187 |
|
sl@0
|
188 |
TInt CConfigImpl::RemoveSection(const TDesC8& aSectionName)
|
sl@0
|
189 |
{
|
sl@0
|
190 |
return iIniFileDocument->RemoveSection(aSectionName);
|
sl@0
|
191 |
}
|
sl@0
|
192 |
|
sl@0
|
193 |
TInt CConfigImpl::GetSection(const TDesC8& aSectionName,CConfigSettingsIter& aSection)
|
sl@0
|
194 |
{
|
sl@0
|
195 |
//clear any previous internal iIniSecIterator
|
sl@0
|
196 |
if (aSection.iImpl->iIniSecIter)
|
sl@0
|
197 |
{
|
sl@0
|
198 |
delete (aSection.iImpl->iIniSecIter);
|
sl@0
|
199 |
aSection.iImpl->iIniSecIter=NULL;
|
sl@0
|
200 |
}
|
sl@0
|
201 |
//initializing its internal iIniSecIterator
|
sl@0
|
202 |
TRAPD(ret,aSection.iImpl->iIniSecIter=CIniSecIter8::NewL(aSectionName,iIniFileDocument));
|
sl@0
|
203 |
return ret;
|
sl@0
|
204 |
}
|
sl@0
|
205 |
|
sl@0
|
206 |
|
sl@0
|
207 |
|
sl@0
|
208 |
//util to check any key within that section holds that value
|
sl@0
|
209 |
//if not found simply return the number of keys within that section
|
sl@0
|
210 |
TInt CConfigImpl::CheckValueExist(const TDesC8& aSectionName,const TDesC8& aValue,TInt& aKeyCount)
|
sl@0
|
211 |
{
|
sl@0
|
212 |
CIniSecIter8* iter=NULL;
|
sl@0
|
213 |
TRAPD(err,iter=CIniSecIter8::NewL(aSectionName,iIniFileDocument));
|
sl@0
|
214 |
//if section not found it might be the first time we are creating
|
sl@0
|
215 |
//the document so it is fine to return KErrNone
|
sl@0
|
216 |
if (err==KErrNotFound)
|
sl@0
|
217 |
{
|
sl@0
|
218 |
return KErrNone;
|
sl@0
|
219 |
}
|
sl@0
|
220 |
//return the error code if else than KErrNotFound e.g. KErrNoMemory;
|
sl@0
|
221 |
if (err!=KErrNone)
|
sl@0
|
222 |
{
|
sl@0
|
223 |
return err;
|
sl@0
|
224 |
}
|
sl@0
|
225 |
TPtrC8 key;
|
sl@0
|
226 |
TPtrC8 value;
|
sl@0
|
227 |
TInt keyCount=0;
|
sl@0
|
228 |
while (iter->Next(key,value))
|
sl@0
|
229 |
{
|
sl@0
|
230 |
if (value.Compare(aValue)==0)
|
sl@0
|
231 |
{
|
sl@0
|
232 |
delete iter;
|
sl@0
|
233 |
keyCount=0;
|
sl@0
|
234 |
return KErrAlreadyExists;
|
sl@0
|
235 |
}
|
sl@0
|
236 |
keyCount++;
|
sl@0
|
237 |
}
|
sl@0
|
238 |
aKeyCount=keyCount;
|
sl@0
|
239 |
delete iter;
|
sl@0
|
240 |
return KErrNone;
|
sl@0
|
241 |
}
|
sl@0
|
242 |
|
sl@0
|
243 |
|
sl@0
|
244 |
TInt CConfigImpl::GenerateInternalKey(const TDesC8& aSection,TBuf8<15>& aKeyName)
|
sl@0
|
245 |
{
|
sl@0
|
246 |
TPtrC8 lastKey;
|
sl@0
|
247 |
TInt ret=GetKeyCount(aSection,lastKey);
|
sl@0
|
248 |
if (ret<0)
|
sl@0
|
249 |
{
|
sl@0
|
250 |
return ret;
|
sl@0
|
251 |
}
|
sl@0
|
252 |
//either "mediaX" or "X"
|
sl@0
|
253 |
//TInt key=ret;
|
sl@0
|
254 |
|
sl@0
|
255 |
if(aSection.Compare(KPrimaryFilterSection)== 0 )
|
sl@0
|
256 |
{
|
sl@0
|
257 |
TInt lastKeyValue=0;
|
sl@0
|
258 |
if(lastKey.Length())
|
sl@0
|
259 |
{
|
sl@0
|
260 |
TLex8 lex(lastKey);
|
sl@0
|
261 |
TInt err = lex.Val(lastKeyValue);
|
sl@0
|
262 |
if(err != KErrNone)
|
sl@0
|
263 |
return err;
|
sl@0
|
264 |
}
|
sl@0
|
265 |
aKeyName.Format(_L8("%03d"),++lastKeyValue);
|
sl@0
|
266 |
}
|
sl@0
|
267 |
else if(aSection.Compare(KSecondaryFilterSection) == 0)
|
sl@0
|
268 |
{
|
sl@0
|
269 |
TInt lastKeyValue=0;
|
sl@0
|
270 |
if(lastKey.Length())
|
sl@0
|
271 |
{
|
sl@0
|
272 |
TLex8 lex(lastKey);
|
sl@0
|
273 |
TInt err = lex.Val(lastKeyValue);
|
sl@0
|
274 |
if(err != KErrNone)
|
sl@0
|
275 |
return err;
|
sl@0
|
276 |
}
|
sl@0
|
277 |
aKeyName.Format(_L8("%04d"),++lastKeyValue);
|
sl@0
|
278 |
}
|
sl@0
|
279 |
else
|
sl@0
|
280 |
{
|
sl@0
|
281 |
TInt lastKeyValue=0;
|
sl@0
|
282 |
if(lastKey.Length())
|
sl@0
|
283 |
{
|
sl@0
|
284 |
TLex8 lex(lastKey);
|
sl@0
|
285 |
TInt err = lex.Val(lastKeyValue);
|
sl@0
|
286 |
if(err != KErrNone)
|
sl@0
|
287 |
return err;
|
sl@0
|
288 |
}
|
sl@0
|
289 |
aKeyName.Format(_L8("%d"),++lastKeyValue);
|
sl@0
|
290 |
}
|
sl@0
|
291 |
return KErrNone;
|
sl@0
|
292 |
}
|
sl@0
|
293 |
|
sl@0
|
294 |
/*TInt CConfigImpl::GenerateInternalSessionName(const TDesC8& aSection,TBuf8<15>& aKeyName)
|
sl@0
|
295 |
{
|
sl@0
|
296 |
TPtrC8 lastKey;
|
sl@0
|
297 |
TInt ret=GetKeyCount(aSection,lastKey);
|
sl@0
|
298 |
if (ret<0)
|
sl@0
|
299 |
{
|
sl@0
|
300 |
return ret;
|
sl@0
|
301 |
}
|
sl@0
|
302 |
//either "mediaX" or "X"
|
sl@0
|
303 |
TInt index=(aSection.Compare(KActiveSection)==0?5:0);
|
sl@0
|
304 |
TInt key=0;
|
sl@0
|
305 |
if (lastKey.Length()!=0)
|
sl@0
|
306 |
{
|
sl@0
|
307 |
TLex8 lex;
|
sl@0
|
308 |
lex.Assign(lastKey.Mid(index));
|
sl@0
|
309 |
lex.Val(key);
|
sl@0
|
310 |
}
|
sl@0
|
311 |
aKeyName.Format((aSection.Compare(KActiveSection)==0?_L8("Session%d"):_L8("%d")),++key);
|
sl@0
|
312 |
return KErrNone;
|
sl@0
|
313 |
}*/
|
sl@0
|
314 |
|
sl@0
|
315 |
|
sl@0
|
316 |
TInt CConfigImpl::GetKeyCount(const TDesC8& aSectionName,TPtrC8& aLastKey)
|
sl@0
|
317 |
{
|
sl@0
|
318 |
CIniSecIter8* iter=NULL;
|
sl@0
|
319 |
TRAPD(err,iter=CIniSecIter8::NewL(aSectionName,iIniFileDocument));
|
sl@0
|
320 |
//if section not found it indicates the keycount is 0;
|
sl@0
|
321 |
if (err==KErrNotFound)
|
sl@0
|
322 |
{
|
sl@0
|
323 |
return 0;
|
sl@0
|
324 |
}
|
sl@0
|
325 |
//return any error code e.g KErrNoMemory here;
|
sl@0
|
326 |
if (err!=KErrNone)
|
sl@0
|
327 |
{
|
sl@0
|
328 |
return err;
|
sl@0
|
329 |
}
|
sl@0
|
330 |
TPtrC8 key;
|
sl@0
|
331 |
TInt keyCount=0x0000;
|
sl@0
|
332 |
while (iter->Next(aLastKey,key))
|
sl@0
|
333 |
{
|
sl@0
|
334 |
keyCount++;
|
sl@0
|
335 |
}
|
sl@0
|
336 |
delete iter;
|
sl@0
|
337 |
return keyCount;
|
sl@0
|
338 |
}
|
sl@0
|
339 |
|
sl@0
|
340 |
TInt CConfigImpl::GetPointerToKeyName(const TDesC8& aSectionName,const TDesC8& aKeyName,TPtrC8& aKeyPointer)
|
sl@0
|
341 |
{
|
sl@0
|
342 |
CIniSecIter8* iter=NULL;
|
sl@0
|
343 |
TRAPD(err,iter=CIniSecIter8::NewL(aSectionName,iIniFileDocument));
|
sl@0
|
344 |
if (err!=KErrNone)
|
sl@0
|
345 |
{
|
sl@0
|
346 |
return err;
|
sl@0
|
347 |
}
|
sl@0
|
348 |
TPtrC8 value;
|
sl@0
|
349 |
while (iter->Next(aKeyPointer,value))
|
sl@0
|
350 |
{
|
sl@0
|
351 |
if (aKeyName.Compare(aKeyPointer)==0)
|
sl@0
|
352 |
{
|
sl@0
|
353 |
delete iter;
|
sl@0
|
354 |
return KErrNone;
|
sl@0
|
355 |
}
|
sl@0
|
356 |
}
|
sl@0
|
357 |
delete iter;
|
sl@0
|
358 |
return KErrNotFound;
|
sl@0
|
359 |
}
|
sl@0
|
360 |
}
|
sl@0
|
361 |
|