1 /* |
|
2 * Copyright (c) 2006 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: Takes care of storing and serving the slides |
|
15 * to Slideshow plugin |
|
16 * |
|
17 */ |
|
18 |
|
19 |
|
20 #include <e32math.h> |
|
21 #include <eikenv.h> |
|
22 #include <bautils.h> |
|
23 |
|
24 #include "SlideshowModel.h" |
|
25 #include "SlideshowPluginUtils.h" |
|
26 |
|
27 //------------------------------------------------------------------------------ |
|
28 // CSlideshowModel::NewL |
|
29 //------------------------------------------------------------------------------ |
|
30 // |
|
31 CSlideshowModel* CSlideshowModel::NewL() |
|
32 { |
|
33 CSlideshowModel* self = new (ELeave) CSlideshowModel(); |
|
34 |
|
35 CleanupStack::PushL(self); |
|
36 self->ConstructL(); |
|
37 CleanupStack::Pop(self); |
|
38 |
|
39 return self; |
|
40 } |
|
41 |
|
42 |
|
43 //------------------------------------------------------------------------------ |
|
44 // CSlideshowModel::~CSlideshowModel |
|
45 //------------------------------------------------------------------------------ |
|
46 // |
|
47 CSlideshowModel::~CSlideshowModel() |
|
48 { |
|
49 // Delete slide array |
|
50 if (iSlides) |
|
51 { |
|
52 iSlides->ResetAndDestroy(); |
|
53 } |
|
54 delete iSlides; |
|
55 } |
|
56 |
|
57 |
|
58 //------------------------------------------------------------------------------ |
|
59 // CSlideshowModel::CSlideshowModel |
|
60 //------------------------------------------------------------------------------ |
|
61 // |
|
62 CSlideshowModel::CSlideshowModel() |
|
63 : iNextSlide(-1) |
|
64 { |
|
65 } |
|
66 |
|
67 |
|
68 //------------------------------------------------------------------------------ |
|
69 // CSlideshowModel::ConstructL |
|
70 //------------------------------------------------------------------------------ |
|
71 // |
|
72 void CSlideshowModel::ConstructL() |
|
73 { |
|
74 // Create slide array |
|
75 iSlides = new(ELeave) CArrayPtrFlat<CSlideshowSlide>( |
|
76 KSlideshowSlideArrayGranularity); |
|
77 |
|
78 //Get random seed |
|
79 TTime time; |
|
80 time.HomeTime(); |
|
81 iRandSeed = time.Int64(); |
|
82 } |
|
83 |
|
84 |
|
85 //------------------------------------------------------------------------------ |
|
86 // CSlideshowModel::NumberOfSlides |
|
87 //------------------------------------------------------------------------------ |
|
88 // |
|
89 TInt CSlideshowModel::NumberOfSlides() const |
|
90 { |
|
91 //if the slide array is not NULL, return it's size |
|
92 return iSlides ? iSlides->Count() : 0; |
|
93 } |
|
94 |
|
95 |
|
96 //------------------------------------------------------------------------------ |
|
97 // CSlideshowModel::AppendSlideL |
|
98 //------------------------------------------------------------------------------ |
|
99 // |
|
100 void CSlideshowModel::AppendSlideL(CSlideshowSlide* aSlide) |
|
101 { |
|
102 // Sanity check |
|
103 if (!iSlides) |
|
104 { |
|
105 ASSERT(EFalse); |
|
106 return; |
|
107 } |
|
108 |
|
109 // Stash it in |
|
110 iSlides->AppendL(aSlide); |
|
111 } |
|
112 |
|
113 |
|
114 //------------------------------------------------------------------------------ |
|
115 // CSlideshowModel::InsertSlideL |
|
116 //------------------------------------------------------------------------------ |
|
117 // |
|
118 void CSlideshowModel::InsertSlideL(TInt aIndex, CSlideshowSlide* aSlide) |
|
119 { |
|
120 if ((iSlides) && (aIndex >= 0) && (aIndex <= NumberOfSlides())) |
|
121 { |
|
122 // Squeeze it in |
|
123 iSlides->InsertL(aIndex, aSlide); |
|
124 } |
|
125 else |
|
126 { |
|
127 ASSERT(EFalse); |
|
128 } |
|
129 } |
|
130 |
|
131 |
|
132 //------------------------------------------------------------------------------ |
|
133 // CSlideshowModel::DeleteSlide |
|
134 //------------------------------------------------------------------------------ |
|
135 // |
|
136 void CSlideshowModel::DeleteSlide(TInt aIndex) |
|
137 { |
|
138 //Sanity check |
|
139 if ((iSlides) && (aIndex >= 0) && (aIndex <= NumberOfSlides())) |
|
140 { |
|
141 // Grab hold of the slide for deletion, remove from array |
|
142 // and compress the array |
|
143 CSlideshowSlide* ptr = iSlides->At(aIndex); |
|
144 |
|
145 iSlides->Delete(aIndex); |
|
146 delete ptr; |
|
147 iSlides->Compress(); |
|
148 |
|
149 } |
|
150 else |
|
151 { |
|
152 ASSERT(EFalse); |
|
153 } |
|
154 } |
|
155 |
|
156 |
|
157 //------------------------------------------------------------------------------ |
|
158 // CSlideshowModel::DeleteAll |
|
159 //------------------------------------------------------------------------------ |
|
160 // |
|
161 void CSlideshowModel::DeleteAll() |
|
162 { |
|
163 //Sanity check |
|
164 if (iSlides) |
|
165 { |
|
166 iSlides->ResetAndDestroy(); |
|
167 } |
|
168 |
|
169 iCurrentSlide = 0; |
|
170 iNextSlide = -1; |
|
171 } |
|
172 |
|
173 |
|
174 //------------------------------------------------------------------------------ |
|
175 // CSlideshowModel::SlideAt |
|
176 //------------------------------------------------------------------------------ |
|
177 // |
|
178 CSlideshowSlide* CSlideshowModel::SlideAt(TInt aIndex) const |
|
179 { |
|
180 if ((iSlides) && (aIndex >= 0) && (aIndex < NumberOfSlides())) |
|
181 { |
|
182 //return the postion object |
|
183 return iSlides->At(aIndex); |
|
184 } |
|
185 else |
|
186 { |
|
187 return NULL; |
|
188 } |
|
189 } |
|
190 |
|
191 |
|
192 //------------------------------------------------------------------------------ |
|
193 // CSlideshowModel::NextSlide |
|
194 //------------------------------------------------------------------------------ |
|
195 // |
|
196 CSlideshowSlide* CSlideshowModel::NextSlide(TBool /*aRandom */) |
|
197 { |
|
198 if (iNextSlide < 0) |
|
199 { |
|
200 // Next slide could not be prepared, or preparenextslide not called |
|
201 return NULL; |
|
202 } |
|
203 |
|
204 CSlideshowSlide* pSlide = SlideAt(iNextSlide); |
|
205 |
|
206 if (!pSlide) |
|
207 { |
|
208 // Apparently there are no more slides - wrap around |
|
209 iCurrentSlide = -1; |
|
210 iNextSlide = 0; |
|
211 pSlide = SlideAt(iNextSlide); |
|
212 } |
|
213 |
|
214 iCurrentSlide = iNextSlide; |
|
215 return pSlide; |
|
216 } |
|
217 |
|
218 |
|
219 //------------------------------------------------------------------------------ |
|
220 // CSlideshowModel::PrepareNextSlide |
|
221 //------------------------------------------------------------------------------ |
|
222 // |
|
223 void CSlideshowModel::PrepareNextSlide(TBool aRandom, const TSize& aTargetSize) |
|
224 { |
|
225 SSPLOGGER_WRITE("Model::PrepareNextSlide(), model busy"); |
|
226 |
|
227 SSPLOGGER_WRITEF(_L("SPP: iCurrentSlide %d, iNextSlide %d"), |
|
228 iCurrentSlide, iNextSlide); |
|
229 |
|
230 TBool slideOK = EFalse; |
|
231 while (!slideOK) |
|
232 { |
|
233 TInt nSlides = NumberOfSlides(); |
|
234 // Any point in continuing? |
|
235 if (nSlides == 0) |
|
236 { |
|
237 iNextSlide = -1; |
|
238 break; |
|
239 } |
|
240 |
|
241 // Special cases, 1 or 2 slides only |
|
242 if (nSlides == 1) |
|
243 { |
|
244 iNextSlide = 0; |
|
245 SSPLOGGER_WRITEF(_L("SPP: One slide, next slide %d"), iNextSlide); |
|
246 } |
|
247 else if (nSlides == 2) |
|
248 { |
|
249 // Switch between slides even if random |
|
250 iNextSlide = (iNextSlide == 0 ? 1 : 0); |
|
251 SSPLOGGER_WRITEF(_L("SPP: 2 slides, next slide %d"), iNextSlide); |
|
252 } |
|
253 else |
|
254 { |
|
255 // We got at least 3 slides, and can also do random |
|
256 if (aRandom) |
|
257 { |
|
258 // Make sure we won't pick the same slide |
|
259 do |
|
260 { |
|
261 iNextSlide = Random(nSlides - 1); |
|
262 } |
|
263 while (iNextSlide == iCurrentSlide); |
|
264 |
|
265 SSPLOGGER_WRITEF(_L("SPP: Random, next slide %d"), iNextSlide); |
|
266 } |
|
267 else |
|
268 { |
|
269 iNextSlide++; |
|
270 |
|
271 // Wrap? |
|
272 if (iNextSlide >= nSlides) |
|
273 { |
|
274 SSPLOGGER_WRITE("Wrapping..."); |
|
275 iNextSlide = 0; |
|
276 } |
|
277 SSPLOGGER_WRITEF(_L("SPP: Continuous, next slide %d"), |
|
278 iNextSlide); |
|
279 } |
|
280 } |
|
281 |
|
282 // Get the slide |
|
283 CSlideshowSlide* pSlide = SlideAt(iNextSlide); |
|
284 |
|
285 if (!pSlide) |
|
286 { |
|
287 // Should not have happened - start from beginning |
|
288 iNextSlide = 0; |
|
289 SSPLOGGER_WRITEF(_L("SPP: Wrapping, next slide %d"), |
|
290 iNextSlide); |
|
291 pSlide = SlideAt(iNextSlide); |
|
292 } |
|
293 |
|
294 if (pSlide) |
|
295 { |
|
296 // Try loading the image, if image does not already exist |
|
297 if (!pSlide->IsSlideDecoded()) |
|
298 { |
|
299 SSPLOGGER_WRITE("Preparing next slide"); |
|
300 if(iCurrentSlide != iNextSlide) |
|
301 { |
|
302 // relese currently deleted image from RAM.( We dont have caching) |
|
303 ReleaseCurrentSlide(); |
|
304 } |
|
305 if ((TInt)ETrue == pSlide->CouldNotDecodeFile()) |
|
306 { |
|
307 // could not decode this file last time. so delete this slide from the |
|
308 // list of slide and go to the next one. |
|
309 DeleteSlide(iNextSlide); |
|
310 iNextSlide--; |
|
311 continue; |
|
312 } |
|
313 TRAPD(err, pSlide->PrepareSlideL(aTargetSize)); |
|
314 if (err == KErrNone) |
|
315 { |
|
316 //wait for slide to load. |
|
317 iCurrentSlide = iNextSlide; |
|
318 SSPLOGGER_WRITE("Started Decoding Wait"); |
|
319 break; |
|
320 } |
|
321 else |
|
322 { |
|
323 // Apparently the file was not a loadable image - |
|
324 // remove from slideset and try again |
|
325 SSPLOGGER_WRITEF(_L("SSP: Deleting slide %d"), iNextSlide); |
|
326 DeleteSlide(iNextSlide); |
|
327 iNextSlide--; |
|
328 continue; // can not load this image, go to the next one. |
|
329 } |
|
330 } |
|
331 else |
|
332 { |
|
333 // Slide is already decoded. This is cool, if the file is |
|
334 // still there. Otherwise lose image and go on. |
|
335 // should not come here as we dont any caching |
|
336 SSPLOGGER_WRITEF(_L("Slide %d already has image"), iNextSlide); |
|
337 |
|
338 if (!BaflUtils::FileExists(CEikonEnv::Static()->FsSession(), |
|
339 *(pSlide->FileName()))) |
|
340 { |
|
341 SSPLOGGER_WRITE("Slide image file gone, dropping image"); |
|
342 pSlide->ReleaseImage(); |
|
343 continue; // continue with next image |
|
344 } |
|
345 else |
|
346 { |
|
347 if(iCurrentSlide != iNextSlide) |
|
348 { |
|
349 ReleaseCurrentSlide(); |
|
350 } |
|
351 break; |
|
352 } |
|
353 } |
|
354 } |
|
355 } |
|
356 SSPLOGGER_WRITE("Model::PrepareNextSlide() end, model no longer busy"); |
|
357 } |
|
358 |
|
359 |
|
360 //------------------------------------------------------------------------------ |
|
361 // CSlideshowModel::ReleaseCurrentSlide |
|
362 //------------------------------------------------------------------------------ |
|
363 // |
|
364 void CSlideshowModel::ReleaseCurrentSlide() |
|
365 { |
|
366 CSlideshowSlide* pSlide = SlideAt(iCurrentSlide); |
|
367 if(pSlide) |
|
368 { |
|
369 pSlide->ReleaseImage(); |
|
370 } |
|
371 } |
|
372 |
|
373 |
|
374 //------------------------------------------------------------------------------ |
|
375 // CSlideshowModel::ReleaseNextSlide |
|
376 //------------------------------------------------------------------------------ |
|
377 // |
|
378 void CSlideshowModel::ReleaseNextSlide() |
|
379 { |
|
380 CSlideshowSlide* pSlide = SlideAt(iNextSlide); |
|
381 if(pSlide) |
|
382 { |
|
383 pSlide->ReleaseImage(); |
|
384 } |
|
385 iNextSlide --; |
|
386 } |
|
387 |
|
388 |
|
389 //------------------------------------------------------------------------------ |
|
390 // CSlideshowModel::SetReserveL |
|
391 //------------------------------------------------------------------------------ |
|
392 // |
|
393 void CSlideshowModel::SetReserveL(TInt aCount) |
|
394 { |
|
395 if ((iSlides) && (aCount >= NumberOfSlides())) |
|
396 { |
|
397 iSlides->SetReserveL(aCount); |
|
398 } |
|
399 } |
|
400 |
|
401 //------------------------------------------------------------------------------ |
|
402 // CSlideshowModel::SlideStatus |
|
403 //------------------------------------------------------------------------------ |
|
404 // |
|
405 TInt CSlideshowModel::SlideStatus(CSlideshowSlide* aSlide) |
|
406 { |
|
407 if(!aSlide) |
|
408 { |
|
409 return KStatusNoSlide; |
|
410 } |
|
411 else if(aSlide->IsSlideDecoded()) |
|
412 { |
|
413 return KStatusSlideDecoded; |
|
414 } |
|
415 else if(aSlide->IsDecodingInProgress()) |
|
416 { |
|
417 return KStatusDecodingInProgress; |
|
418 } |
|
419 else if (aSlide->CouldNotDecodeFile()) |
|
420 { |
|
421 return KStatusSlideDecodeingFailed; |
|
422 } |
|
423 |
|
424 return KStatusSlideNotDecoding; |
|
425 } |
|
426 |
|
427 |
|
428 |
|
429 //------------------------------------------------------------------------------ |
|
430 // CSlideshowModel::Random |
|
431 //------------------------------------------------------------------------------ |
|
432 // |
|
433 TInt CSlideshowModel::Random(TInt aMax, TInt aMin /* = 0 */) |
|
434 { |
|
435 TInt range = aMax - aMin; |
|
436 TInt rand = Math::Rand(iRandSeed); |
|
437 return (rand % range); |
|
438 } |
|
439 |
|
440 |
|
441 // End of File |
|