|
1 /* |
|
2 * Copyright (c) 2010 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: Resource loader for About in order to asynchronous load resource. |
|
15 * |
|
16 */ |
|
17 |
|
18 #include "AboutResourceLoader.h" |
|
19 #include "MResourceLoaderObserver.h" // For MResourceLoaderObserver |
|
20 #include "AboutResource.h" // For CAboutResource |
|
21 #include <StringLoader.h> // For StringLoader |
|
22 #include <coneresloader.h> // For RConeResourceLoader |
|
23 #include <eikenv.h> // For CEikonEnv |
|
24 #include <StringLoader.h> // For StringLoader |
|
25 #include <f32file.h> |
|
26 #include <BAUTILS.H> |
|
27 |
|
28 // CONSTANTS |
|
29 const TInt KDefaultResourceId( -1 ); // Default resource id. |
|
30 // The count for the resouce that to be loaded in one time. |
|
31 const TInt KDefaultItemLoadingCount( 10 ); |
|
32 |
|
33 // --------------------------------------------------------- |
|
34 // CAboutResourceLoader::CAboutResourceLoader() |
|
35 // |
|
36 // --------------------------------------------------------- |
|
37 CAboutResourceLoader::CAboutResourceLoader( CEikonEnv* aEikonEnv, |
|
38 MResourceLoaderObserver* aObserver ) |
|
39 : CActive( EPriorityLow ), |
|
40 iEnv( aEikonEnv ), |
|
41 iResourceId( KDefaultResourceId ), |
|
42 iObserver( aObserver ) |
|
43 { |
|
44 |
|
45 } |
|
46 |
|
47 // --------------------------------------------------------- |
|
48 // CAboutResourceLoader::NewLC() |
|
49 // |
|
50 // --------------------------------------------------------- |
|
51 CAboutResourceLoader* CAboutResourceLoader::NewLC( CEikonEnv* aEikonEnv, |
|
52 MResourceLoaderObserver* aObserver ) |
|
53 { |
|
54 CAboutResourceLoader* self = new ( ELeave ) CAboutResourceLoader( |
|
55 aEikonEnv, aObserver ); |
|
56 CleanupStack::PushL(self); |
|
57 self->ConstructL(); |
|
58 return self; |
|
59 } |
|
60 |
|
61 // --------------------------------------------------------- |
|
62 // CAboutResourceLoader::NewL() |
|
63 // |
|
64 // --------------------------------------------------------- |
|
65 CAboutResourceLoader* CAboutResourceLoader::NewL( CEikonEnv* aEikonEnv, |
|
66 MResourceLoaderObserver* aObserver ) |
|
67 { |
|
68 CAboutResourceLoader* self = CAboutResourceLoader::NewLC( |
|
69 aEikonEnv, aObserver ); |
|
70 CleanupStack::Pop(); // self; |
|
71 return self; |
|
72 } |
|
73 |
|
74 // --------------------------------------------------------- |
|
75 // CAboutResourceLoader::ConstructL() |
|
76 // |
|
77 // --------------------------------------------------------- |
|
78 void CAboutResourceLoader::ConstructL() |
|
79 { |
|
80 CActiveScheduler::Add(this); // Add to scheduler |
|
81 iLoadedItem = new(ELeave) RPointerArray<CAboutResource>(); |
|
82 iLoader = new(ELeave) RConeResourceLoader( *iEnv ); |
|
83 } |
|
84 |
|
85 // --------------------------------------------------------- |
|
86 // CAboutResourceLoader::CAboutResourceLoader() |
|
87 // |
|
88 // --------------------------------------------------------- |
|
89 CAboutResourceLoader::~CAboutResourceLoader() |
|
90 { |
|
91 Cancel(); // Cancel any request, if outstanding |
|
92 if ( iLoadedItem ) |
|
93 { |
|
94 iLoadedItem->ResetAndDestroy(); |
|
95 delete iLoadedItem; |
|
96 iLoadedItem = NULL; |
|
97 } |
|
98 if ( iResourceBuffer ) |
|
99 { |
|
100 delete iResourceBuffer; |
|
101 iResourceBuffer = NULL; |
|
102 } |
|
103 if ( iLoader ) |
|
104 { |
|
105 iLoader->Close(); |
|
106 delete iLoader; |
|
107 iLoader = NULL; |
|
108 } |
|
109 } |
|
110 |
|
111 // --------------------------------------------------------- |
|
112 // CAboutResourceLoader::DoCancel() |
|
113 // |
|
114 // --------------------------------------------------------- |
|
115 void CAboutResourceLoader::DoCancel() |
|
116 { |
|
117 iItemIterator = 0; |
|
118 // Delete the resource buffer. |
|
119 if ( iResourceBuffer ) |
|
120 { |
|
121 delete iResourceBuffer; |
|
122 iResourceBuffer = NULL; |
|
123 } |
|
124 |
|
125 if ( iLoader ) |
|
126 { |
|
127 iLoader->Close(); |
|
128 } |
|
129 } |
|
130 |
|
131 // --------------------------------------------------------- |
|
132 // CAboutResourceLoader::StartL() |
|
133 // |
|
134 // --------------------------------------------------------- |
|
135 void CAboutResourceLoader::StartL() |
|
136 { |
|
137 // Leave with KErrGeneral if the resource id is not set. |
|
138 if ( iResourceId == KDefaultResourceId ) |
|
139 { |
|
140 User::Leave( KErrGeneral ); |
|
141 } |
|
142 |
|
143 Cancel(); |
|
144 |
|
145 iItemIterator = 0; |
|
146 ReleaseLoadedItems(); |
|
147 |
|
148 if ( !iLoaded ) |
|
149 { |
|
150 // Init the rsc file path |
|
151 TFileName filePath( iResourcePath ); |
|
152 BaflUtils::NearestLanguageFile( iEnv->FsSession(), filePath ); |
|
153 iLoader->Close(); |
|
154 iLoader->OpenL( filePath ); |
|
155 } |
|
156 |
|
157 if ( iResourceBuffer ) |
|
158 { |
|
159 delete iResourceBuffer; |
|
160 iResourceBuffer = NULL; |
|
161 } |
|
162 |
|
163 iResourceBuffer = iEnv->AllocReadResourceAsDes8LC( iResourceId ); |
|
164 iReader.SetBuffer( iResourceBuffer ); |
|
165 iItemCount = iReader.ReadInt16(); |
|
166 CleanupStack::Pop(); // iResourceBuffer |
|
167 |
|
168 Activate(); |
|
169 } |
|
170 |
|
171 // --------------------------------------------------------- |
|
172 // CAboutResourceLoader::RunL() |
|
173 // |
|
174 // --------------------------------------------------------- |
|
175 void CAboutResourceLoader::RunL() |
|
176 { |
|
177 TInt err = KErrNone; |
|
178 for ( ; iItemIterator < iItemCount; iItemIterator++ ) |
|
179 { |
|
180 CAboutResource* aboutResource = CAboutResource::NewL(); |
|
181 CleanupStack::PushL( aboutResource ); |
|
182 TInt type = iReader.ReadInt8(); |
|
183 |
|
184 // Load Text |
|
185 if ( type == EAboutTextItem ) |
|
186 { |
|
187 HBufC* resourceItem( NULL ); |
|
188 // There are two different text, one is direct output and another needs format |
|
189 if ( iReader.ReadInt8() == EAboutUpdateTime ) |
|
190 { |
|
191 // About text needs format |
|
192 TTime time; |
|
193 time.UniversalTime(); |
|
194 TDateTime currentTime = time.DateTime(); |
|
195 resourceItem = StringLoader::LoadLC( iReader.ReadInt32(), |
|
196 currentTime.Year(), CEikonEnv::Static() ); |
|
197 } |
|
198 else |
|
199 { |
|
200 // Direct output about text |
|
201 resourceItem = iEnv->AllocReadResourceLC( iReader.ReadInt32() ); |
|
202 } |
|
203 // Set the loaded item |
|
204 aboutResource->SetResourceItem( resourceItem ); |
|
205 // Set the loaded item |
|
206 aboutResource->SetResourceIndex( iItemIterator ); |
|
207 iLoadedItem->AppendL( aboutResource ); |
|
208 CleanupStack::Pop( 2 ); // resourceItem and aboutResource |
|
209 } |
|
210 // Load image. |
|
211 else if ( type == EAboutImageItem ) |
|
212 { |
|
213 TPtrC imageFile = iReader.ReadTPtrC(); |
|
214 aboutResource->SetResourceItem( imageFile.AllocL() ); |
|
215 aboutResource->SetBitmapId( iReader.ReadInt16() ); |
|
216 aboutResource->SetResourceType( EAboutImageItem ); |
|
217 iLoadedItem->AppendL( aboutResource ); |
|
218 CleanupStack::Pop(); // aboutResource |
|
219 } |
|
220 else |
|
221 { |
|
222 err = KErrNotSupported; |
|
223 CleanupStack::PopAndDestroy(); // aboutResource |
|
224 break; |
|
225 } |
|
226 |
|
227 // Ten items per loading and calculate the number of loaded |
|
228 if ( ( iItemIterator % KDefaultItemLoadingCount ) |
|
229 == ( KDefaultItemLoadingCount - 1 ) ) |
|
230 { |
|
231 // Once the value of iItemIterator > 10, we should break |
|
232 iItemIterator++; |
|
233 break; |
|
234 } |
|
235 } |
|
236 |
|
237 // Notify observer. |
|
238 if ( err == KErrNone && iItemIterator < iItemCount ) |
|
239 { |
|
240 // Ten items loading over, Notify HandleItemsLoadedL observer |
|
241 iObserver->HandleItemsLoadedL( err ); |
|
242 Activate(); |
|
243 } |
|
244 else |
|
245 { |
|
246 // Once there is not enough ten items, need run this function again |
|
247 iObserver->HandleItemsLoadedL( err ); |
|
248 // One resource loading over, Notify HandleResourceLoadCompletedL observer |
|
249 iObserver->HandleResourceLoadCompletedL( iResourceId, err ); |
|
250 } |
|
251 } |
|
252 |
|
253 // --------------------------------------------------------- |
|
254 // CAboutResourceLoader::RunError() |
|
255 // |
|
256 // --------------------------------------------------------- |
|
257 TInt CAboutResourceLoader::RunError( TInt aError ) |
|
258 { |
|
259 return aError; |
|
260 } |
|
261 |
|
262 // --------------------------------------------------------- |
|
263 // CAboutResourceLoader::Activate() |
|
264 // |
|
265 // --------------------------------------------------------- |
|
266 void CAboutResourceLoader::Activate() |
|
267 { |
|
268 TRequestStatus* status = &iStatus; |
|
269 *status = KRequestPending; |
|
270 User::RequestComplete( status, KErrNone ); |
|
271 SetActive(); |
|
272 } |
|
273 |
|
274 // --------------------------------------------------------- |
|
275 // CAboutResourceLoader::SetResourceId() |
|
276 // |
|
277 // --------------------------------------------------------- |
|
278 void CAboutResourceLoader::SetResourceId( const TInt aResourceId, |
|
279 const TBool aLoaded, const TDesC& aResourcePath ) |
|
280 { |
|
281 iResourceId = aResourceId; |
|
282 iLoaded = aLoaded; |
|
283 iResourcePath.Copy( aResourcePath ); |
|
284 } |
|
285 |
|
286 // --------------------------------------------------------- |
|
287 // CAboutResourceLoader::CurrentResourceId() |
|
288 // |
|
289 // --------------------------------------------------------- |
|
290 TInt CAboutResourceLoader::CurrentResourceId() |
|
291 { |
|
292 return iResourceId; |
|
293 } |
|
294 |
|
295 // --------------------------------------------------------- |
|
296 // CAboutResourceLoader::LoadedResourceItems() |
|
297 // |
|
298 // --------------------------------------------------------- |
|
299 RPointerArray<CAboutResource>* CAboutResourceLoader::LoadedResourceItems() const |
|
300 { |
|
301 return iLoadedItem; |
|
302 } |
|
303 |
|
304 // --------------------------------------------------------- |
|
305 // CAboutResourceLoader::RleaseLoadedItems() |
|
306 // |
|
307 // --------------------------------------------------------- |
|
308 void CAboutResourceLoader::ReleaseLoadedItems() |
|
309 { |
|
310 iLoadedItem->ResetAndDestroy(); |
|
311 } |
|
312 |
|
313 // --------------------------------------------------------- |
|
314 // CAboutResourceLoader::ItemCount() |
|
315 // |
|
316 // --------------------------------------------------------- |
|
317 TInt CAboutResourceLoader::ItemCount() const |
|
318 { |
|
319 return iItemCount; |
|
320 } |