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