|
1 /* |
|
2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 #include <s32mem.h> |
|
25 #include <s32file.h> |
|
26 #include <sysutil.h> |
|
27 |
|
28 #include "senmultiuseridentitymanager.h" |
|
29 |
|
30 #include "senlogger.h" |
|
31 #include "SenXmlUtils.h" |
|
32 |
|
33 namespace |
|
34 { |
|
35 _LIT8(KUser, "User"); |
|
36 _LIT8(KUsers, "Users"); |
|
37 _LIT8(KUserName, "username"); |
|
38 _LIT8(KPIN, "PIN"); |
|
39 _LIT8(KActive, "active"); |
|
40 _LIT8(KTrue, "1"); |
|
41 _LIT8(KFalse, "0"); |
|
42 _LIT8(KUsersStart, "<Users>"); |
|
43 _LIT8(KUsersEnd, "</Users>"); |
|
44 _LIT8(KUserStartFmt, "<User username=\"%S\" PIN=\"%S\" active=\"%d\">"); |
|
45 _LIT8(KUserEnd, "</User>"); |
|
46 const TInt KFLATBUF_SIZE = 128; |
|
47 const TInt KActiveValueSize = 1; |
|
48 |
|
49 const TInt KStateParsingIdentity = 100; |
|
50 } |
|
51 |
|
52 |
|
53 EXPORT_C CSenMultiUserIdentityManager* CSenMultiUserIdentityManager::NewL( |
|
54 MSenCoreServiceManager& aManager) |
|
55 { |
|
56 CSenMultiUserIdentityManager* pNew = NewLC(aManager); |
|
57 CleanupStack::Pop(); |
|
58 return(pNew) ; |
|
59 } |
|
60 |
|
61 EXPORT_C CSenMultiUserIdentityManager* CSenMultiUserIdentityManager::NewLC( |
|
62 MSenCoreServiceManager& aManager) |
|
63 { |
|
64 CSenMultiUserIdentityManager* pNew = |
|
65 new (ELeave) CSenMultiUserIdentityManager(aManager); |
|
66 CleanupStack::PushL(pNew); |
|
67 pNew->ConstructL(); |
|
68 return pNew; |
|
69 } |
|
70 |
|
71 CSenMultiUserIdentityManager::CSenMultiUserIdentityManager( |
|
72 MSenCoreServiceManager& aManager) |
|
73 : CSenBaseIdentityManager(aManager) |
|
74 { |
|
75 } |
|
76 |
|
77 EXPORT_C CSenMultiUserIdentityManager::~CSenMultiUserIdentityManager() |
|
78 { |
|
79 iUsers.ResetAndDestroy(); |
|
80 TLSLOG_L(KSenCoreServiceManagerLogChannelBase , KMinLogLevel,"CSenMultiUserIdentityManager::~CSenMultiUserIdentityManager"); |
|
81 } |
|
82 |
|
83 void CSenMultiUserIdentityManager::ConstructL() |
|
84 { |
|
85 CSenBaseIdentityManager::ConstructL(); |
|
86 //db was empty, take default one from Base class(loaded during Base::Contructl) |
|
87 if (!iActiveIdentity) |
|
88 { |
|
89 iActiveIdentity = iIdentity; |
|
90 iActiveUser = CSenUser::NewL(KNullDesC8, KNullDesC8, ETrue); |
|
91 iActiveUser->Identites().Append(iActiveIdentity); |
|
92 iUsers.Append(iActiveUser); |
|
93 } |
|
94 else |
|
95 { |
|
96 //parsing from CSenBaseIdentityManager::ConstructL() |
|
97 // caused, that iActiveIdentity is already set (because has been set by below "StartElement" method). |
|
98 // So, orginal iIdentity should be replaced by iActiveIdentity (used by UpdateTouch functionality) |
|
99 delete iIdentity; |
|
100 iIdentity = NULL; |
|
101 iIdentity = iActiveIdentity; |
|
102 }; |
|
103 |
|
104 } |
|
105 |
|
106 void CSenMultiUserIdentityManager::StartElementL( |
|
107 const TDesC8& /*aNsUri*/, |
|
108 const TDesC8& aLocalName, |
|
109 const TDesC8& /*aQName*/, |
|
110 const RAttributeArray& aAttributes |
|
111 ) |
|
112 { |
|
113 switch (iState) |
|
114 { |
|
115 case KStateIgnore: |
|
116 { |
|
117 if (aLocalName == KUser) |
|
118 { |
|
119 TPtrC8 userName = SenXmlUtils::AttrValue(aAttributes, KUserName); |
|
120 TPtrC8 pin = SenXmlUtils::AttrValue(aAttributes, KPIN); |
|
121 TPtrC8 active = SenXmlUtils::AttrValue(aAttributes, KActive); |
|
122 if (active == KTrue) |
|
123 { |
|
124 iParsedUser = CSenUser::NewL(userName, pin, ETrue); |
|
125 } |
|
126 else |
|
127 { |
|
128 iParsedUser = CSenUser::NewL(userName, pin); |
|
129 } |
|
130 |
|
131 if (active == KTrue) |
|
132 { |
|
133 //2nd time active user?? within db |
|
134 __ASSERT_ALWAYS(!iActiveUser, User::Panic(KSenUser, KErrAlreadyExists)); |
|
135 iActiveUser = iParsedUser; |
|
136 } |
|
137 iUsers.Append(iParsedUser); |
|
138 iState = KStateParsingIdentity; |
|
139 } |
|
140 } |
|
141 break; |
|
142 |
|
143 case KStateParsingIdentity: |
|
144 { |
|
145 if (aLocalName == KIdentityElementLocalName) |
|
146 { |
|
147 CSenBaseIdentity* identity = CSenBaseIdentity::NewLC(ipDeviceID->Des()); |
|
148 identity->SetAttributesL(aAttributes); |
|
149 identity->SetReader(*Reader()); |
|
150 DelegateParsingL(*identity); |
|
151 __ASSERT_ALWAYS(iParsedUser, User::Panic(KSenUser, KErrAlreadyExists)); |
|
152 iParsedUser->Identites().Append(identity); |
|
153 |
|
154 //current parsed user is active |
|
155 if (iParsedUser == iActiveUser) |
|
156 { |
|
157 iActiveIdentity = identity; |
|
158 } |
|
159 CleanupStack::Pop(identity); |
|
160 } |
|
161 } |
|
162 break; |
|
163 |
|
164 default: |
|
165 break; |
|
166 } |
|
167 } |
|
168 void CSenMultiUserIdentityManager::EndElementL( |
|
169 const TDesC8& /*aNsUri*/, |
|
170 const TDesC8& aLocalName, |
|
171 const TDesC8& /*aQName*/) |
|
172 { |
|
173 switch (iState) |
|
174 { |
|
175 case KStateIgnore: |
|
176 { |
|
177 if (aLocalName == KUsers)//end parsing |
|
178 { |
|
179 if (iUsers.Count() == 1) |
|
180 { |
|
181 CSenUser* user = iUsers[0]; |
|
182 iActiveUser = user; |
|
183 iActiveIdentity = iActiveUser->Identites()[0]; |
|
184 } |
|
185 } |
|
186 break; |
|
187 } |
|
188 case KStateParsingIdentity: |
|
189 { |
|
190 if (aLocalName == KUser) |
|
191 { |
|
192 iState = KStateIgnore; |
|
193 } |
|
194 } |
|
195 break; |
|
196 } |
|
197 } |
|
198 |
|
199 |
|
200 MSenIdentity& CSenMultiUserIdentityManager::IdentityL() |
|
201 { |
|
202 return (MSenIdentity&)*iActiveIdentity; |
|
203 } |
|
204 |
|
205 TInt CSenMultiUserIdentityManager::UserNameL(HBufC8*& aUserName) |
|
206 { |
|
207 delete aUserName; |
|
208 aUserName = NULL; |
|
209 aUserName = iActiveUser->UserName().AllocL(); |
|
210 return KErrNone; |
|
211 } |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 TInt CSenMultiUserIdentityManager::WriteConfigurationToL( const TDesC& aFile ) |
|
218 { |
|
219 // First, collect everything into MEMORY |
|
220 CBufFlat *pBuf = CBufFlat::NewL(KFLATBUF_SIZE); |
|
221 CleanupStack::PushL(pBuf); |
|
222 |
|
223 RBufWriteStream bufWs(*pBuf); |
|
224 CleanupClosePushL(bufWs); |
|
225 bufWs.WriteL(KUsersStart); |
|
226 |
|
227 |
|
228 //############# main loop ######### |
|
229 TInt count = iUsers.Count(); |
|
230 for (TInt i=0; i<count; i++) |
|
231 { |
|
232 RBuf8 userTag; |
|
233 CSenUser* user = iUsers[i]; |
|
234 userTag.CreateL(KUserStartFmt().Length() + |
|
235 user->PIN().Length() + |
|
236 user->UserName().Length() + |
|
237 KActiveValueSize //1 |
|
238 ); |
|
239 |
|
240 if (user->Active()) |
|
241 { |
|
242 userTag.Format(KUserStartFmt, &(user->UserName()), &(user->PIN()), KTrue); |
|
243 } |
|
244 else |
|
245 { |
|
246 userTag.Format(KUserStartFmt, &(user->UserName()), &(user->PIN()), KFalse); |
|
247 } |
|
248 |
|
249 |
|
250 bufWs.WriteL(userTag); |
|
251 TInt count2 = iUsers[i]->Identites().Count(); |
|
252 for (TInt j=0; j<count2; j++) |
|
253 { |
|
254 CSenBaseIdentity* identity = iUsers[i]->Identites()[j]; |
|
255 identity->WriteAsXMLToL(bufWs); |
|
256 } |
|
257 bufWs.WriteL(KUserEnd); |
|
258 userTag.Close(); |
|
259 } |
|
260 |
|
261 //############# |
|
262 bufWs.WriteL(KUsersEnd); |
|
263 |
|
264 TPtrC8 p8 = pBuf->Ptr(0); |
|
265 |
|
266 CleanupStack::PopAndDestroy(1); // bufWs |
|
267 |
|
268 // Everything in MEMORY ok, prepare to write into file |
|
269 RFs fss; |
|
270 User::LeaveIfError(fss.Connect()); |
|
271 CleanupClosePushL(fss); |
|
272 |
|
273 RFileWriteStream fileOutStream; |
|
274 CleanupClosePushL(fileOutStream); |
|
275 |
|
276 |
|
277 if(!SysUtil::FFSSpaceBelowCriticalLevelL(&fss, p8.Length()) ) |
|
278 { |
|
279 // note, this will zero-length the file(!) |
|
280 // it is better to require that 2xfilesize is available and not to |
|
281 // dangerously zero the old file and find out |
|
282 // that there is no space left.. |
|
283 |
|
284 //Data caging 2 implementation |
|
285 #if defined( EKA2 ) || defined( RD_SECURE_PRIV_DATA ) |
|
286 TBuf<KMaxPath> file; |
|
287 fss.CreatePrivatePath(EDriveC); |
|
288 fss.PrivatePath(file); |
|
289 file.Append(aFile); |
|
290 fileOutStream.Replace(fss, file, EFileWrite); |
|
291 #else |
|
292 fileOutStream.Replace(fss, aFile, EFileWrite); |
|
293 #endif |
|
294 // finally write the UTF-8 into the file. |
|
295 fileOutStream.WriteL(p8); |
|
296 } |
|
297 CleanupStack::PopAndDestroy(3); |
|
298 return KErrNone; |
|
299 } |
|
300 |
|
301 |
|
302 |
|
303 // End of file |