|
1 /* |
|
2 * Copyright (c) 2007-2009 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: ESMR policymmanager implementation |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include "emailtrace.h" |
|
20 #include "cesmrpolicymanager.h" |
|
21 //<cmail> |
|
22 #include "cesmrpolicy.h" |
|
23 #include <esmrpolicies.rsg> |
|
24 #include "esmrdef.h" |
|
25 //</cmail> |
|
26 #include "esmrinternaluid.h" |
|
27 #include "cesmrcalimportexporter.h" |
|
28 #include "tesmrentryfield.h" |
|
29 #include "cesmrpolicychecker.h" |
|
30 #include "cesmrpolicyextensionmanager.h" |
|
31 |
|
32 #include <coemain.h>// CCoeEnv |
|
33 #include <barsread.h> |
|
34 |
|
35 |
|
36 /// Unnamed namespace for local definitions |
|
37 namespace { |
|
38 |
|
39 // Definition for resource id array granularity |
|
40 const TInt KESMRPolicyIdArrayGranularity = 4; |
|
41 |
|
42 /** |
|
43 * Reads ESMR policy resource id table from resource. |
|
44 * |
|
45 * @param aReader Reference to resource reader. |
|
46 * @param aResourceIdTable Reference to resource id table. |
|
47 */ |
|
48 void ReadResourceIdArrayL( |
|
49 TResourceReader& aReader, |
|
50 RArray<TInt>& aResourceIdTable ) |
|
51 { |
|
52 TInt numOfFields = aReader.ReadInt16(); |
|
53 for (TInt i(0); i < numOfFields; i++ ) |
|
54 { |
|
55 TInt resourceId= aReader.ReadInt32(); |
|
56 aResourceIdTable.AppendL(resourceId); |
|
57 } |
|
58 } |
|
59 |
|
60 |
|
61 #ifdef _DEBUG |
|
62 |
|
63 _LIT( KESMRPolicyMgrPanic, "ESMRPolicyManager" ); |
|
64 |
|
65 enum TESMRPolicyManagerPanic |
|
66 { |
|
67 EESMRPolicyMgrNoPolicyFound = 1, |
|
68 EESMRPolicyMgrPopWithoutPush |
|
69 }; |
|
70 |
|
71 void Panic( TESMRPolicyManagerPanic aPanic ) |
|
72 { |
|
73 User::Panic( KESMRPolicyMgrPanic, aPanic ); |
|
74 } |
|
75 |
|
76 /** |
|
77 * Logs policy information. |
|
78 * |
|
79 * @param aPolicy Reference to policy. |
|
80 */ |
|
81 void LogPolicy( |
|
82 const CESMRPolicy& aPolicy ) |
|
83 { |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 const RArray<TESMREntryField>& fieldArray = aPolicy.Fields(); |
|
90 TInt fieldCount( fieldArray.Count() ); |
|
91 |
|
92 |
|
93 for ( TInt i(0); i < fieldCount; ++i ) |
|
94 { |
|
95 const TESMREntryField& field = fieldArray[i]; |
|
96 |
|
97 } |
|
98 } |
|
99 |
|
100 #define LOG_POLICY( x ) { LogPolicy( x ); } |
|
101 |
|
102 #else |
|
103 |
|
104 #define LOG_POLICY( x ) |
|
105 |
|
106 #endif // _DEBUG |
|
107 |
|
108 } // namespace |
|
109 |
|
110 // ======== MEMBER FUNCTIONS ======== |
|
111 |
|
112 // --------------------------------------------------------------------------- |
|
113 // CESMRPolicyManager::CESMRPolicyManager |
|
114 // --------------------------------------------------------------------------- |
|
115 // |
|
116 inline CESMRPolicyManager::CESMRPolicyManager() |
|
117 : iPolicyResourceIds( KESMRPolicyIdArrayGranularity ) |
|
118 { |
|
119 FUNC_LOG; |
|
120 //do nothing |
|
121 } |
|
122 |
|
123 // --------------------------------------------------------------------------- |
|
124 // CESMRPolicyManager::~CESMRPolicyManager |
|
125 // --------------------------------------------------------------------------- |
|
126 // |
|
127 EXPORT_C CESMRPolicyManager::~CESMRPolicyManager() |
|
128 { |
|
129 FUNC_LOG; |
|
130 delete iDefaultPolicyChecker; |
|
131 delete iCurrentPolicy; |
|
132 delete iExtension; |
|
133 iPolicyResourceFile.Close(); |
|
134 iPolicyResourceIds.Close(); |
|
135 iPolicyStack.ResetAndDestroy(); |
|
136 } |
|
137 |
|
138 // --------------------------------------------------------------------------- |
|
139 // CESMRPolicyManager::NewL |
|
140 // --------------------------------------------------------------------------- |
|
141 // |
|
142 EXPORT_C CESMRPolicyManager* CESMRPolicyManager::NewL() |
|
143 { |
|
144 FUNC_LOG; |
|
145 |
|
146 CESMRPolicyManager* self = new (ELeave) CESMRPolicyManager; |
|
147 CleanupStack::PushL( self ); |
|
148 self->ConstructL(); |
|
149 CleanupStack::Pop(self); |
|
150 |
|
151 return self; |
|
152 } |
|
153 |
|
154 // --------------------------------------------------------------------------- |
|
155 // CESMRPolicyManager::ConstructL |
|
156 // --------------------------------------------------------------------------- |
|
157 // |
|
158 void CESMRPolicyManager::ConstructL() |
|
159 { |
|
160 FUNC_LOG; |
|
161 iDefaultPolicyChecker = CESMRPolicyChecker::NewL(); |
|
162 iCoeEnv = CCoeEnv::Static(); |
|
163 iExtension = CESMRPolicyExtensionManager::NewL( *iCoeEnv ); |
|
164 } |
|
165 |
|
166 // --------------------------------------------------------------------------- |
|
167 // CESMRPolicyManager::ExtensionUid |
|
168 // --------------------------------------------------------------------------- |
|
169 // |
|
170 EXPORT_C TUid CESMRPolicyManager::ExtensionUid() |
|
171 { |
|
172 FUNC_LOG; |
|
173 return TUid::Uid( KESMRPolicyMgrUid ); |
|
174 } |
|
175 |
|
176 // --------------------------------------------------------------------------- |
|
177 // CESMRPolicyManager::ResolvePolicyL |
|
178 // --------------------------------------------------------------------------- |
|
179 // |
|
180 EXPORT_C void CESMRPolicyManager::ResolvePolicyL( |
|
181 const TESMRScenarioData& aScenarioData, |
|
182 MESMRCalEntry& aESMREntry, |
|
183 MESMRPolicyChecker* aPolicyChecker ) |
|
184 { |
|
185 FUNC_LOG; |
|
186 |
|
187 MESMRPolicyChecker* policyChecker = iDefaultPolicyChecker; |
|
188 if ( aPolicyChecker ) |
|
189 { |
|
190 policyChecker = aPolicyChecker; |
|
191 } |
|
192 |
|
193 delete iCurrentPolicy; |
|
194 iCurrentPolicy = NULL; |
|
195 |
|
196 TInt policyCount( iPolicyResourceIds.Count() ); |
|
197 for ( TInt i( 0 ); |
|
198 ( i < policyCount) && !iCurrentPolicy; |
|
199 ++i ) |
|
200 { |
|
201 ReadPolicyL( iPolicyResourceIds[i], aESMREntry ); |
|
202 if ( !policyChecker->MatchesL( *iCurrentPolicy, aScenarioData ) ) |
|
203 { |
|
204 // Policy did not match --> Reading next |
|
205 delete iCurrentPolicy; |
|
206 iCurrentPolicy = NULL; |
|
207 } |
|
208 } |
|
209 |
|
210 // Matching policy not found --> Panic |
|
211 __ASSERT_DEBUG( iCurrentPolicy, Panic(EESMRPolicyMgrNoPolicyFound) ); |
|
212 |
|
213 if ( !iCurrentPolicy ) |
|
214 { |
|
215 User::Leave( KErrNotFound ); |
|
216 } |
|
217 |
|
218 // Logging policy for debugging purposes |
|
219 LOG_POLICY( *iCurrentPolicy ) |
|
220 } |
|
221 |
|
222 // --------------------------------------------------------------------------- |
|
223 // CESMRPolicyManager::PushPolicyL |
|
224 // --------------------------------------------------------------------------- |
|
225 // |
|
226 EXPORT_C void CESMRPolicyManager::PushPolicyL() |
|
227 { |
|
228 if ( iCurrentPolicy ) |
|
229 { |
|
230 iPolicyStack.AppendL( iCurrentPolicy ); |
|
231 iCurrentPolicy = NULL; |
|
232 } |
|
233 } |
|
234 |
|
235 // --------------------------------------------------------------------------- |
|
236 // CESMRPolicyManager::PopPolicy |
|
237 // --------------------------------------------------------------------------- |
|
238 // |
|
239 EXPORT_C CESMRPolicy* CESMRPolicyManager::PopPolicy() |
|
240 { |
|
241 delete iCurrentPolicy; |
|
242 iCurrentPolicy = NULL; |
|
243 |
|
244 TInt stackCount = iPolicyStack.Count(); |
|
245 if ( stackCount ) |
|
246 { |
|
247 iCurrentPolicy = iPolicyStack[ --stackCount ]; |
|
248 iPolicyStack.Remove( stackCount ); |
|
249 } |
|
250 |
|
251 __ASSERT_DEBUG( iCurrentPolicy, Panic( EESMRPolicyMgrPopWithoutPush ) ); |
|
252 |
|
253 return iCurrentPolicy; |
|
254 } |
|
255 |
|
256 // --------------------------------------------------------------------------- |
|
257 // CESMRPolicyManager::ReadPolicyFromResourceL |
|
258 // --------------------------------------------------------------------------- |
|
259 // |
|
260 EXPORT_C void CESMRPolicyManager::ReadPolicyFromResourceL( |
|
261 const TDesC& aPolicyFile, |
|
262 TInt aPolicyArrayResourceId ) |
|
263 { |
|
264 FUNC_LOG; |
|
265 |
|
266 |
|
267 iPolicyResourceFile.Close(); |
|
268 iPolicyResourceFile.OpenL( iCoeEnv->FsSession(), aPolicyFile ); |
|
269 iPolicyResourceFile.ConfirmSignatureL(); |
|
270 |
|
271 // R_ESMR_POLICIES |
|
272 HBufC8* resourceIdBuffer = |
|
273 iPolicyResourceFile.AllocReadLC( aPolicyArrayResourceId ); |
|
274 |
|
275 // Construct resource reader |
|
276 TResourceReader reader; |
|
277 reader.SetBuffer(resourceIdBuffer); |
|
278 |
|
279 // Read policy resource ids |
|
280 iPolicyResourceIds.Reset(); |
|
281 ReadResourceIdArrayL(reader, iPolicyResourceIds ); |
|
282 |
|
283 CleanupStack::PopAndDestroy( resourceIdBuffer ); |
|
284 |
|
285 // Load extension resources |
|
286 iExtension->ReadResourcesFromExtensionsL(); |
|
287 |
|
288 } |
|
289 |
|
290 |
|
291 // --------------------------------------------------------------------------- |
|
292 // CESMRPolicyManager::ReadPolicyL |
|
293 // --------------------------------------------------------------------------- |
|
294 // |
|
295 void CESMRPolicyManager::ReadPolicyL( |
|
296 TInt aResourceId, |
|
297 MESMRCalEntry& /*aESMREntry */) |
|
298 { |
|
299 FUNC_LOG; |
|
300 |
|
301 HBufC8* resourceBuffer = NULL; |
|
302 // First try to find policy from static policies and then from extensions |
|
303 TRAPD( error, resourceBuffer = iPolicyResourceFile.AllocReadL( aResourceId ) ); |
|
304 if ( resourceBuffer ) |
|
305 { |
|
306 CleanupStack::PushL( resourceBuffer ); |
|
307 } |
|
308 |
|
309 TResourceReader reader; |
|
310 CESMRPolicy* policy = NULL; |
|
311 if ( error == KErrNone && resourceBuffer ) |
|
312 { |
|
313 reader.SetBuffer(resourceBuffer); |
|
314 policy = CESMRPolicy::NewL( iPolicyResourceFile, reader ); |
|
315 } |
|
316 else |
|
317 { |
|
318 resourceBuffer = iExtension->ReadBufferL( aResourceId ); |
|
319 CESMRExtensionResourceFile* file = NULL; |
|
320 if ( resourceBuffer ) |
|
321 { |
|
322 CleanupStack::PushL( resourceBuffer ); |
|
323 file = iExtension->ExtensionResourceFile( aResourceId ); |
|
324 } |
|
325 |
|
326 if ( resourceBuffer && file ) |
|
327 { |
|
328 reader.SetBuffer(resourceBuffer); |
|
329 policy = CESMRPolicy::NewL( file->ResFile(), reader ); |
|
330 } |
|
331 } |
|
332 |
|
333 // Logging read policy |
|
334 LOG_POLICY( *policy ) |
|
335 |
|
336 delete iCurrentPolicy; |
|
337 iCurrentPolicy = policy; |
|
338 |
|
339 if ( resourceBuffer ) |
|
340 { |
|
341 CleanupStack::PopAndDestroy( resourceBuffer ); |
|
342 } |
|
343 |
|
344 } |
|
345 |
|
346 // EOF |
|
347 |