author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Tue, 24 Nov 2009 08:56:33 +0200 | |
changeset 33 | 50974a8b132e |
parent 19 | 989d2f495d90 |
permissions | -rw-r--r-- |
19 | 1 |
/* |
2 |
* Copyright (c) 2007-2007 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 the License "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: This Class provides the core functionality to thumbnail generation |
|
15 |
* |
|
16 |
* |
|
17 |
*/ |
|
18 |
||
19 |
||
20 |
#include<e32cmn.h> |
|
21 |
||
22 |
#include<f32file.h> |
|
33
50974a8b132e
Revision: 200945
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
23 |
#include<apgcli.h> |
50974a8b132e
Revision: 200945
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
24 |
#include<apmrec.h> // For TDataRecognitionResult |
50974a8b132e
Revision: 200945
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
25 |
#include<apmstd.h> |
19 | 26 |
#include<thumbnailmanager.h> |
27 |
#include"mgthumbnailgeneration.h" |
|
28 |
||
29 |
#include"mgmresolution.h" |
|
30 |
#include"mgthumbobserver.h" |
|
31 |
_LIT(KImage,"image"); |
|
32 |
_LIT(KVideo,"video"); |
|
33 |
_LIT(KThumbFolder,"_PAlbTN_"); |
|
34 |
const TInt KHeight = 74 ; |
|
35 |
const TInt KWidth = 104; |
|
36 |
const TInt KThumbnailPathMaxLen = 255; |
|
37 |
const TInt KBufLen = 20; |
|
38 |
const TInt KZero = 0; |
|
39 |
||
40 |
// ----------------------------------------------------------------------------- |
|
41 |
// CThumbnailGeneration::NewL |
|
42 |
// Returns the instance of CThumbnailGeneration class. |
|
43 |
// ----------------------------------------------------------------------------- |
|
44 |
CThumbnailGeneration* CThumbnailGeneration::NewL() |
|
45 |
{ |
|
46 |
CThumbnailGeneration* self = new(ELeave)CThumbnailGeneration(); |
|
47 |
CleanupStack::PushL(self); |
|
48 |
self->ConstructL(); |
|
49 |
CleanupStack::Pop(self); |
|
50 |
return self; |
|
51 |
} |
|
52 |
||
53 |
// ----------------------------------------------------------------------------- |
|
54 |
// CThumbnailGeneration::ConstructL |
|
55 |
// It creates the instance of TNM and Observer |
|
56 |
// ----------------------------------------------------------------------------- |
|
57 |
void CThumbnailGeneration::ConstructL() |
|
58 |
{ |
|
59 |
// Creating instance of observer class |
|
60 |
iThumbnailObserver = CThumbnailObserver::NewL(); |
|
61 |
//Creating instance of ThumbnailManger |
|
62 |
iThumbnailManager = CThumbnailManager::NewL(*iThumbnailObserver); |
|
63 |
iThumbnailObserver->SetThumbnailMangager(iThumbnailManager); |
|
64 |
||
65 |
} |
|
66 |
||
67 |
// ----------------------------------------------------------------------------- |
|
68 |
// CThumbnailGeneration::~CThumbnailGeneration |
|
69 |
// Destructor |
|
70 |
// ----------------------------------------------------------------------------- |
|
71 |
CThumbnailGeneration::~CThumbnailGeneration() |
|
72 |
{ |
|
73 |
if(iThumbnailObserver) |
|
74 |
{ |
|
75 |
delete iThumbnailObserver; |
|
76 |
} |
|
77 |
if(iThumbnailManager) |
|
78 |
{ |
|
79 |
delete iThumbnailManager; |
|
80 |
} |
|
81 |
||
82 |
} |
|
83 |
||
84 |
// ----------------------------------------------------------------------------- |
|
85 |
// CThumbnailGeneration::CThumbnailGeneration |
|
86 |
// C++ Constructor |
|
87 |
// ----------------------------------------------------------------------------- |
|
88 |
CThumbnailGeneration::CThumbnailGeneration():iThumbnailObserver(NULL),iThumbnailManager(NULL) |
|
89 |
{ |
|
90 |
||
91 |
} |
|
92 |
||
93 |
// ----------------------------------------------------------------------------- |
|
94 |
// CThumbnailGeneration::Cancel |
|
95 |
// Cancels the thumbnail generation request |
|
96 |
// ----------------------------------------------------------------------------- |
|
97 |
TInt CThumbnailGeneration::Cancel(TInt32 aTransactionId) |
|
98 |
{ |
|
99 |
return iThumbnailObserver->Cancel(aTransactionId); |
|
100 |
} |
|
101 |
||
102 |
||
103 |
// ----------------------------------------------------------------------------- |
|
104 |
// CThumbnailGeneration::GetThumbnailL |
|
105 |
// This method makes request for thumbnail generation |
|
106 |
// ----------------------------------------------------------------------------- |
|
107 |
||
108 |
||
109 |
void CThumbnailGeneration::GetThumbnailL(MThumbnailObserver* aObserver,TInt32 aTransactionID, TDesC& aUrl, TInt aHeight, TInt aWidth) |
|
110 |
{ |
|
111 |
||
112 |
// Check whether uri is valid |
|
113 |
TBuf<KThumbnailPathMaxLen> thumbnailpath; |
|
114 |
TBuf<KThumbnailPathMaxLen> refDir; |
|
115 |
||
116 |
// Checking validity of media file name |
|
117 |
IsUrlValidL( aUrl); |
|
118 |
||
119 |
// getting thumbnail size |
|
120 |
TSize thumbsize = GetthumbsizeL(aUrl,aHeight,aWidth); |
|
121 |
||
122 |
//creating thumbnail path |
|
123 |
CreateThumbnailPathL(aUrl,thumbsize,refDir,thumbnailpath); |
|
124 |
||
125 |
// If thumbnail already exists then give a call to users callback |
|
126 |
if(IsThumbnailExistL(thumbnailpath,refDir)) |
|
127 |
{ |
|
128 |
iThumbnailObserver->AddToObserverL(aObserver,aTransactionID,thumbnailpath,KZero); |
|
129 |
iThumbnailObserver->ThumbnailAlreadyExists(); |
|
130 |
return ; |
|
131 |
} |
|
132 |
||
133 |
||
134 |
iThumbnailManager->SetFlagsL(CThumbnailManager::ECropToAspectRatio); |
|
135 |
||
136 |
||
137 |
// Setting thumbnail size |
|
138 |
iThumbnailManager->SetThumbnailSizeL( thumbsize ); |
|
139 |
||
140 |
// Prefer thumbnails in the highest quality possible disregarding |
|
141 |
// any negative impact on performance. |
|
142 |
iThumbnailManager->SetQualityPreferenceL(CThumbnailManager::EOptimizeForQuality ); |
|
143 |
||
144 |
//setting display mode EColor16M for both video and image it works |
|
145 |
iThumbnailManager->SetDisplayModeL( EColor16M ); |
|
146 |
||
147 |
// Create an object source representing a path to a file on local |
|
148 |
// file system. |
|
149 |
CThumbnailObjectSource* source = CThumbnailObjectSource::NewLC(aUrl,KNullDesC); |
|
150 |
||
151 |
// Issue the request using default priority |
|
152 |
TInt thumbnailRequestId = iThumbnailManager->GetThumbnailL( *source ); |
|
153 |
||
154 |
CleanupStack::PopAndDestroy(source); |
|
155 |
||
156 |
||
157 |
||
158 |
// Adding thumbanilrequestinfo to observer |
|
159 |
||
160 |
iThumbnailObserver->AddToObserverL(aObserver,aTransactionID,thumbnailpath,thumbnailRequestId); |
|
161 |
||
162 |
} |
|
163 |
||
164 |
// ----------------------------------------------------------------------------- |
|
165 |
// CThumbnailGeneration::getthumbsizeL |
|
166 |
// This method gets the size for thumbnail |
|
167 |
// ----------------------------------------------------------------------------- |
|
168 |
TSize CThumbnailGeneration::GetthumbsizeL(TDesC& aUrl,TInt aHeight, TInt aWidth) |
|
169 |
{ |
|
170 |
// Creating media resolution object |
|
171 |
TSize mediaRes(0,0); |
|
172 |
TSize thumbsize(0,0); |
|
173 |
MResolution* mediaResObj= GetResolutionobjL(aUrl); |
|
174 |
CleanupStack::PushL(mediaResObj); |
|
175 |
// Getting resolution for media file |
|
176 |
mediaResObj->GetresolutionL(mediaRes); |
|
177 |
||
178 |
// calculating size for thumbnail |
|
179 |
thumbsize = CalculateSize(mediaRes,aHeight,aWidth); |
|
180 |
CleanupStack::Pop(mediaResObj); |
|
181 |
delete mediaResObj; |
|
182 |
return thumbsize; |
|
183 |
||
184 |
} |
|
185 |
||
186 |
// ----------------------------------------------------------------------------- |
|
187 |
// CThumbnailGeneration::CalculateSize |
|
188 |
// This method calculates the size for thumbnail |
|
189 |
// ----------------------------------------------------------------------------- |
|
190 |
TSize CThumbnailGeneration::CalculateSize(TSize aRes, TInt aHeight, TInt aWidth) |
|
191 |
{ |
|
192 |
TSize thumbnailsize; |
|
193 |
||
194 |
if(KNegVal == aHeight && KNegVal == aWidth ) |
|
195 |
{ |
|
196 |
if( (aRes.iWidth < KWidth) || (aRes.iHeight< KHeight) ) // if the resolution of media file is less than that of plaformspecific |
|
197 |
{ |
|
198 |
thumbnailsize.iWidth = aRes.iWidth; |
|
199 |
||
200 |
thumbnailsize.iHeight =aRes.iHeight; |
|
201 |
} |
|
202 |
else // if resolution of video file is greater that platform specific |
|
203 |
{ |
|
204 |
thumbnailsize.iWidth = KWidth; |
|
205 |
||
206 |
thumbnailsize.iHeight = KHeight; |
|
207 |
||
208 |
} |
|
209 |
return thumbnailsize; |
|
210 |
} |
|
211 |
||
212 |
if(TSize(0,0) == aRes ) |
|
213 |
{ |
|
214 |
// If both height and width are not given or either of them are having value than that of platformspecific resolution |
|
215 |
// then we will use platform specific size |
|
216 |
if( (aHeight >KZero && (aHeight>KHeight) ) || (aWidth >KZero && (aWidth >KWidth )) ) |
|
217 |
{ |
|
218 |
||
219 |
thumbnailsize.iWidth = KWidth; |
|
220 |
||
221 |
thumbnailsize.iHeight = KHeight; |
|
222 |
||
223 |
} |
|
224 |
else if(KNegVal == aWidth ) // if height is given and width not given |
|
225 |
{ |
|
226 |
thumbnailsize.iWidth = (TInt)((TReal)(KWidth/KHeight)* aHeight); |
|
227 |
||
228 |
thumbnailsize.iHeight = aHeight ; |
|
229 |
||
230 |
} |
|
231 |
else if(KNegVal == aHeight)// if width is given and height not given |
|
232 |
{ |
|
233 |
thumbnailsize.iHeight = (TInt)(((TReal)KHeight/KWidth ) * aWidth) ; |
|
234 |
thumbnailsize.iWidth = aWidth ; |
|
235 |
} |
|
236 |
else // when both are given |
|
237 |
{ |
|
238 |
thumbnailsize.iWidth = aWidth; |
|
239 |
||
240 |
thumbnailsize.iHeight = aHeight; |
|
241 |
||
242 |
} |
|
243 |
||
244 |
} |
|
245 |
else // When we get the resolution of media file |
|
246 |
{ |
|
247 |
||
248 |
// If both height and width are not given or either of them are having value greater than that of media file |
|
249 |
// then we will use platform specific size |
|
250 |
if( (aHeight >KZero && (aHeight > aRes.iHeight) ) || (aWidth >KZero && (aWidth > aRes.iWidth )) ) |
|
251 |
{ |
|
252 |
||
253 |
thumbnailsize.iWidth = aRes.iWidth ; |
|
254 |
thumbnailsize.iHeight = aRes.iHeight ; |
|
255 |
||
256 |
} |
|
257 |
else if(KNegVal == aWidth )// if height is given and width is not given |
|
258 |
{ |
|
259 |
thumbnailsize.iWidth = (TInt)((TReal)(aRes.iWidth/aRes.iHeight)* aHeight); |
|
260 |
||
261 |
thumbnailsize.iHeight = aHeight ; |
|
262 |
||
263 |
} |
|
264 |
else if(KNegVal == aHeight)// if width is given and height not given |
|
265 |
{ |
|
266 |
thumbnailsize.iHeight = (TInt)(((TReal)aRes.iHeight/aRes.iWidth ) * aWidth) ; |
|
267 |
thumbnailsize.iWidth = aWidth ; |
|
268 |
} |
|
269 |
else // when both are given |
|
270 |
{ |
|
271 |
thumbnailsize.iWidth = aWidth; |
|
272 |
||
273 |
thumbnailsize.iHeight = aHeight; |
|
274 |
} |
|
275 |
||
276 |
||
277 |
||
278 |
} |
|
279 |
return thumbnailsize; |
|
280 |
} |
|
281 |
||
282 |
// ----------------------------------------------------------------------------- |
|
283 |
// CThumbnailGeneration::GetResolutionobj |
|
284 |
// This method gets the MResolution object |
|
285 |
// ----------------------------------------------------------------------------- |
|
286 |
MResolution* CThumbnailGeneration::GetResolutionobjL(TDesC& aUrl) |
|
287 |
{ |
|
288 |
||
289 |
RFs fs; |
|
290 |
TBuf8<KThumbnailPathMaxLen> filebuf; |
|
291 |
TInt fileSize = KThumbnailPathMaxLen; |
|
292 |
User::LeaveIfError(fs.Connect()); |
|
293 |
CleanupClosePushL(fs); |
|
294 |
||
295 |
User::LeaveIfError(fs.ReadFileSection(aUrl,KZero,filebuf,fileSize)); |
|
296 |
||
297 |
||
298 |
// Opening RApaLsSession to get mimetype of media file |
|
299 |
RApaLsSession aPaSession; |
|
300 |
User::LeaveIfError(aPaSession.Connect()); |
|
301 |
CleanupClosePushL(aPaSession); |
|
302 |
||
303 |
TDataRecognitionResult mimeTypeResult; |
|
304 |
TBuf<KBufLen> media; |
|
305 |
if(KErrNone == aPaSession.RecognizeData(aUrl,filebuf,*&mimeTypeResult)) |
|
306 |
{ |
|
307 |
// If media type is of image then creating image resolution object |
|
308 |
if(KZero == mimeTypeResult.iDataType.Des().Find(KImage) ) |
|
309 |
{ |
|
310 |
media.Append(KImage); |
|
311 |
} |
|
312 |
else if(KZero == mimeTypeResult.iDataType.Des().Find(KVideo)) |
|
313 |
{ |
|
314 |
media.Append(KVideo); |
|
315 |
} |
|
316 |
else |
|
317 |
{ |
|
318 |
User::Leave(KErrNotSupported); |
|
319 |
} |
|
320 |
} |
|
321 |
else |
|
322 |
{ |
|
323 |
// Need to leave from here |
|
324 |
User::Leave(KErrNotSupported); |
|
325 |
} |
|
326 |
||
327 |
CleanupStack::PopAndDestroy(&aPaSession); // aPaSession |
|
328 |
CleanupStack::PopAndDestroy(&fs); // fs |
|
329 |
||
330 |
||
331 |
return MediaResolutionFactory::CreateMediaResolutionobjL(media,aUrl); |
|
332 |
} |
|
333 |
||
334 |
||
335 |
// ----------------------------------------------------------------------------- |
|
336 |
// CThumbnailGeneration::IsThumbnailExist |
|
337 |
// This method checks if the requested thumbnail already exists |
|
338 |
// ----------------------------------------------------------------------------- |
|
339 |
TBool CThumbnailGeneration::IsThumbnailExistL(const TDesC& aThumbpath,const TDesC& aRefDir) |
|
340 |
{ |
|
341 |
// Need to check whether thumbnail exists . |
|
342 |
||
343 |
RFs fs; |
|
344 |
User::LeaveIfError(fs.Connect()); |
|
345 |
CleanupClosePushL(fs); |
|
346 |
RFile file; |
|
347 |
||
348 |
if(KErrNone == file.Open(fs,aThumbpath,EFileRead)) |
|
349 |
{ |
|
350 |
// thumbnail exist so return ETrue |
|
351 |
file.Close(); |
|
352 |
CleanupStack::PopAndDestroy(&fs); |
|
353 |
return ETrue; |
|
354 |
} |
|
355 |
||
356 |
||
357 |
||
358 |
// Need to check whether dir panltn exist |
|
359 |
TInt ret = fs.MkDirAll(aRefDir); |
|
360 |
||
361 |
if( !(KErrNone == ret || KErrAlreadyExists == ret) ) |
|
362 |
{ |
|
363 |
User::Leave(ret); |
|
364 |
} |
|
365 |
||
366 |
||
367 |
||
368 |
CleanupStack::PopAndDestroy(&fs); |
|
369 |
return EFalse; |
|
370 |
||
371 |
||
372 |
} |
|
373 |
||
374 |
// ----------------------------------------------------------------------------- |
|
375 |
// CThumbnailGeneration::IsUrlValid |
|
376 |
// This method checks the validity of Url |
|
377 |
// ----------------------------------------------------------------------------- |
|
378 |
void CThumbnailGeneration::IsUrlValidL(TDesC& aUrl) |
|
379 |
{ |
|
380 |
RFs fs; |
|
381 |
User::LeaveIfError(fs.Connect()); |
|
382 |
||
383 |
// If the url is not in correct filepath format |
|
384 |
if(!fs.IsValidName(aUrl)) |
|
385 |
{ |
|
386 |
fs.Close(); |
|
387 |
User::Leave(KErrBadName); |
|
388 |
||
389 |
} |
|
390 |
||
391 |
// If file is not present |
|
392 |
RFile file; |
|
393 |
||
394 |
if(KErrNone != file.Open(fs,aUrl,EFileRead)) |
|
395 |
{ |
|
396 |
file.Close(); |
|
397 |
fs.Close(); |
|
398 |
User::Leave(KErrNotFound); |
|
399 |
||
400 |
} |
|
401 |
||
402 |
file.Close(); |
|
403 |
fs.Close(); |
|
404 |
} |
|
405 |
||
406 |
// ----------------------------------------------------------------------------- |
|
407 |
// CThumbnailGeneration::createThumbnailPath |
|
408 |
// This method creates the thumbnail path |
|
409 |
// ----------------------------------------------------------------------------- |
|
410 |
void CThumbnailGeneration::CreateThumbnailPathL(TDesC& aUri, TSize aThumbsize,TDes& aRefDir ,TDes& aThumbnailPath) |
|
411 |
{ |
|
412 |
||
413 |
||
414 |
TInt strlength = aUri.Length(); |
|
415 |
TChar chr('\\'); |
|
416 |
TInt len = aUri.LocateReverseF(chr); |
|
417 |
||
418 |
if(KErrNotFound == len) |
|
419 |
{ |
|
420 |
User::Leave(KErrBadName); |
|
421 |
} |
|
422 |
||
423 |
||
424 |
TBuf<KThumbnailPathMaxLen> refpathbuf; |
|
425 |
TBuf<KThumbnailPathMaxLen> buf; |
|
426 |
buf.Append(aUri.Left(len+1)); |
|
427 |
buf.Append(KThumbFolder); |
|
428 |
refpathbuf.Append(buf); |
|
429 |
refpathbuf.Append(_L("\\")); |
|
430 |
aRefDir.Append(refpathbuf); // setting refdir argument |
|
431 |
buf.Append(aUri.Right(strlength-len)); |
|
432 |
buf.Append('_'); |
|
433 |
buf.AppendNum(aThumbsize.iWidth); |
|
434 |
buf.Append('x'); |
|
435 |
buf.AppendNum(aThumbsize.iHeight); |
|
436 |
aThumbnailPath.Append(buf); |
|
437 |
||
438 |
return; |
|
439 |
||
440 |
||
441 |
} |
|
442 |
||
443 |