upnpframework/upnpthumbnail/src/upnpthumbnailcreator.cpp
branchIOP_Improvements
changeset 40 08b5eae9f9ff
equal deleted inserted replaced
39:6369bfd1b60d 40:08b5eae9f9ff
       
     1 /*
       
     2 * Copyright (c) 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:      
       
    15 *
       
    16 */
       
    17 
       
    18 //  Include Files  
       
    19 
       
    20 #include <f32file.h>
       
    21 #include <mdeproperty.h>
       
    22 #include <mdeinstanceitem.h>
       
    23 #include <mdeitem.h>
       
    24 #include <mdesession.h>
       
    25 #include <fbs.h>
       
    26 #include <thumbnaildata.h>
       
    27 #include <ImageConversion.h>
       
    28 #include "upnpthumbnailcreator.h"  // CUpnpThumbnailCreator
       
    29 #include "upnpthumbnailcreatorObserver.h"
       
    30 #include "upnpfileutilitytypes.h"
       
    31 
       
    32 _LIT( KComponentLogfile, "upnpthumbnailcreator.txt");
       
    33 _LIT(KSystemFilePath, "c:\\system\\temp\\");
       
    34 #include "upnplog.h"
       
    35 
       
    36 const TInt KSmallWidth = 640;
       
    37 const TInt KSmallHeight = 480;
       
    38 const TInt KMediumWidth = 1024;
       
    39 const TInt KMediumHeight = 768;
       
    40 const TInt KLargeWidth = 4096;
       
    41 const TInt KLargeHeight = 4096;
       
    42 const TInt KThumbnailWidth = 160;
       
    43 const TInt KThumbnailHeight = 160;
       
    44 const TInt KSmallIconWidth = 48;
       
    45 const TInt KSmallIconHeight = 48;
       
    46 const TInt KLargeIconWidth = 160;
       
    47 const TInt KLargeIconHeight = 160;
       
    48 //  Member Functions
       
    49 
       
    50 
       
    51 // --------------------------------------------------------------------------
       
    52 // CUpnpThumbnailCreator::NewLC
       
    53 // See upnpthumbnailcreator.h
       
    54 //---------------------------------------------------------------------------
       
    55 EXPORT_C CUpnpThumbnailCreator* CUpnpThumbnailCreator::NewLC(
       
    56         MUpnpThumbnailCreatorObserver& aMUpnpThumbnailCreatorObserver,
       
    57         const TDesC& aFilePath,
       
    58         TThumbnailDlnaSize aSize)
       
    59     {
       
    60     CUpnpThumbnailCreator* self = new (ELeave) CUpnpThumbnailCreator(
       
    61             aMUpnpThumbnailCreatorObserver, aSize);
       
    62     CleanupStack::PushL(self);
       
    63     self->ConstructL(aFilePath);
       
    64     return self;
       
    65     }
       
    66 
       
    67 // --------------------------------------------------------------------------
       
    68 // CUpnpThumbnailCreator::NewL
       
    69 // See upnpthumbnailcreator.h
       
    70 //---------------------------------------------------------------------------
       
    71 EXPORT_C CUpnpThumbnailCreator* CUpnpThumbnailCreator::NewL(
       
    72         MUpnpThumbnailCreatorObserver& aMUpnpThumbnailCreatorObserver,
       
    73         const TDesC& aFilePath,
       
    74         TThumbnailDlnaSize aSize)
       
    75     {
       
    76     __LOG1( "CUpnpThumbnailCreator:NewL() aFilePath: &s", &aFilePath);    
       
    77     CUpnpThumbnailCreator* self = CUpnpThumbnailCreator::NewLC(
       
    78             aMUpnpThumbnailCreatorObserver,aFilePath, aSize);
       
    79     CleanupStack::Pop(self);
       
    80     return self;
       
    81     }
       
    82 
       
    83 
       
    84 
       
    85 // --------------------------------------------------------------------------
       
    86 // CUpnpThumbnailCreator::MdeObjectId
       
    87 // See upnpthumbnailcreator.h
       
    88 //---------------------------------------------------------------------------
       
    89 EXPORT_C TInt CUpnpThumbnailCreator::MdeObjectId() const
       
    90     {
       
    91     return iObjectid;
       
    92     }
       
    93 
       
    94 // --------------------------------------------------------------------------
       
    95 // CUpnpThumbnailCreator::ThumbnailFilePath
       
    96 // See upnpthumbnailcreator.h // CI: RM return TDesC &
       
    97 //---------------------------------------------------------------------------
       
    98 EXPORT_C const TDesC& CUpnpThumbnailCreator::ThumbnailFilePath() const
       
    99     {
       
   100     __ASSERT( iThumbnailFilePath, __FILE__, __LINE__ );
       
   101     return *iThumbnailFilePath;
       
   102     }
       
   103 
       
   104 // --------------------------------------------------------------------------
       
   105 // CUpnpThumbnailCreator::Thumbnail
       
   106 // See upnpthumbnailcreator.h
       
   107 //---------------------------------------------------------------------------
       
   108 EXPORT_C CFbsBitmap& CUpnpThumbnailCreator::Thumbnail() const
       
   109     {
       
   110     __ASSERT( iThumbnail, __FILE__, __LINE__ );
       
   111     return *iThumbnail;
       
   112     }
       
   113 
       
   114 // --------------------------------------------------------------------------
       
   115 // CUpnpThumbnailCreator::~CUpnpThumbnailCreator
       
   116 // See upnpthumbnailcreator.h
       
   117 //---------------------------------------------------------------------------
       
   118 CUpnpThumbnailCreator::~CUpnpThumbnailCreator()
       
   119     {
       
   120     __LOG( "CUpnpThumbnailCreator:~CUpnpThumbnailCreator()");
       
   121     if (IsActive())
       
   122         {
       
   123         Cancel();
       
   124         }
       
   125     
       
   126     delete iImageFilePath;
       
   127     delete iMdEQuery;
       
   128     delete iMdESession;
       
   129     delete iThumbnail; 
       
   130     delete iThmbManager;
       
   131     delete iImageEncoder;
       
   132 	
       
   133     if(iThumbnailFilePath != NULL)
       
   134         {
       
   135         iFsSession.Delete(iThumbnailFilePath->Des());
       
   136         delete iThumbnailFilePath;
       
   137         }
       
   138     iFsSession.Close();
       
   139     
       
   140     }
       
   141  
       
   142 
       
   143 /********************************************************************
       
   144  * MMdESessionObserver  virtual methods
       
   145  ********************************************************************/ 
       
   146 
       
   147 // --------------------------------------------------------------------------
       
   148 // CUpnpThumbnailCreator::HandleSessionOpened
       
   149 // See mdesession.h
       
   150 //---------------------------------------------------------------------------
       
   151 void CUpnpThumbnailCreator::HandleSessionOpened(CMdESession& /*aSession*/, 
       
   152                                                             TInt aError)
       
   153     {
       
   154     __LOG1( "CUpnpThumbnailCreator:HandleSessionOpened() %d", aError);
       
   155     CompleteRequest(aError);
       
   156     }
       
   157 
       
   158 // --------------------------------------------------------------------------
       
   159 // CUpnpThumbnailCreator::HandleSessionError
       
   160 // See mdesession.h
       
   161 //---------------------------------------------------------------------------
       
   162 void CUpnpThumbnailCreator::HandleSessionError(CMdESession& /*aSession*/, 
       
   163                                                             TInt aError)
       
   164     {
       
   165      __LOG1( "CUpnpThumbnailCreator:HandleSessionError() %d", aError);
       
   166      CompleteRequest(aError);
       
   167     }
       
   168 /********************************************************************
       
   169  * MMdEQueryObserver virtual methods
       
   170  ********************************************************************/
       
   171 
       
   172 // --------------------------------------------------------------------------
       
   173 // CUpnpThumbnailCreator::HandleQueryNewResults
       
   174 // See mdequery.h
       
   175 //---------------------------------------------------------------------------
       
   176 void CUpnpThumbnailCreator::HandleQueryNewResults(CMdEQuery& aQuery, 
       
   177                                                 TInt aFirstNewItemIndex,
       
   178                                                 TInt aNewItemCount)
       
   179     {
       
   180     CMdEObjectQuery& query = (CMdEObjectQuery &) aQuery;
       
   181     if (aNewItemCount > 0)
       
   182         {
       
   183         TInt i( aFirstNewItemIndex);
       
   184         for ( ; i < (aFirstNewItemIndex+aNewItemCount); i++)
       
   185             {
       
   186             CMdEObject* object = (CMdEObject*)query.TakeOwnershipOfResult(i);
       
   187             if(object->Uri().CompareF(iImageFilePath->Des()) == 0)
       
   188                 {
       
   189                 iObjectid = object->Id();
       
   190                 CompleteRequest(KErrNone);
       
   191                 break;
       
   192                 }
       
   193             delete object;
       
   194             }
       
   195         if(i == (aFirstNewItemIndex + aNewItemCount))
       
   196             {
       
   197             CompleteRequest(KErrNotFound);
       
   198             }
       
   199         }
       
   200     }
       
   201 
       
   202 // --------------------------------------------------------------------------
       
   203 // CUpnpThumbnailCreator::HandleQueryCompleted
       
   204 // See mdequery.h
       
   205 //---------------------------------------------------------------------------
       
   206 void CUpnpThumbnailCreator::HandleQueryCompleted(CMdEQuery& /*aQuery*/, 
       
   207                                                                 TInt aError)
       
   208     {
       
   209      __LOG1( "CUpnpThumbnailCreator:HandleQueryCompleted() %d",aError);
       
   210      if(aError != KErrNone)
       
   211          {
       
   212          CompleteRequest(aError);
       
   213          }     
       
   214     }
       
   215 
       
   216 /********************************************************************
       
   217  * MThumbnailManagerObserver virtual methods
       
   218  ********************************************************************/
       
   219 
       
   220 // --------------------------------------------------------------------------
       
   221 // CUpnpThumbnailCreator::ThumbnailPreviewReady
       
   222 // See thumbnailmanagerobserver.h
       
   223 //---------------------------------------------------------------------------
       
   224 void CUpnpThumbnailCreator::ThumbnailPreviewReady(MThumbnailData& /*aThumbnail*/,
       
   225                                                 TThumbnailRequestId /*aId*/ )
       
   226     {    
       
   227     }
       
   228 
       
   229 // --------------------------------------------------------------------------
       
   230 // CUpnpThumbnailCreator::ThumbnailReady
       
   231 // See thumbnailmanagerobserver.h
       
   232 //---------------------------------------------------------------------------
       
   233 void CUpnpThumbnailCreator::ThumbnailReady(TInt aError, 
       
   234                                             MThumbnailData& aThumbnail, 
       
   235                                             TThumbnailRequestId /*aId*/ )
       
   236     {
       
   237     // This function must not leave.
       
   238      __LOG1( "CUpnpThumbnailCreator:ThumbnailReady() %d",aError);
       
   239     delete iThumbnail; iThumbnail = NULL;
       
   240     if ( aError == KErrNone )
       
   241         {
       
   242         // Claim ownership of the bitmap instance for later use
       
   243         iThumbnail = aThumbnail.DetachBitmap();
       
   244         }
       
   245     
       
   246     CompleteRequest(aError);
       
   247     }
       
   248 
       
   249 // --------------------------------------------------------------------------
       
   250 // CUpnpThumbnailCreator::NotifyCompletion
       
   251 // See upnpbitmapconverterobserver.h
       
   252 //---------------------------------------------------------------------------
       
   253 void CUpnpThumbnailCreator::ThumbnailReady(TInt aError)
       
   254     {
       
   255     if(iThumbnailFilePath)
       
   256         {
       
   257         TInt ret = iFsSession.SetAtt(iThumbnailFilePath->Des(),KEntryAttHidden,KEntryAttArchive); 
       
   258         __LOG1( "CUpnpThumbnailCreator:NotifyCompletion - thumbnail attribute hidden result %d",ret);
       
   259         }
       
   260     iMUpnpThumbnailCreatorObserver.ThumbnailCreatorReady(aError);    
       
   261     }
       
   262 
       
   263 
       
   264 // --------------------------------------------------------------------------
       
   265 // CUpnpThumbnailCreator::CUpnpThumbnailCreator
       
   266 // See upnpthumbnailcreator.h
       
   267 //---------------------------------------------------------------------------
       
   268 CUpnpThumbnailCreator::CUpnpThumbnailCreator(
       
   269         MUpnpThumbnailCreatorObserver& aMUpnpThumbnailCreatorObserver,
       
   270         TThumbnailDlnaSize aSize)
       
   271         :CActive( CActive::EPriorityStandard ),
       
   272         iMUpnpThumbnailCreatorObserver(aMUpnpThumbnailCreatorObserver),
       
   273         iThumbnailDlnaSize (aSize)
       
   274         
       
   275     {
       
   276     CActiveScheduler::Add( this );
       
   277     }
       
   278 
       
   279 // --------------------------------------------------------------------------
       
   280 // CUpnpThumbnailCreator::ConstructL
       
   281 // See upnpthumbnailcreator.h
       
   282 //---------------------------------------------------------------------------        
       
   283 void CUpnpThumbnailCreator::ConstructL(const TDesC& aFilePath)
       
   284     {
       
   285     // second phase constructor, anything that may leave must be constructed here
       
   286     __LOG( "CUpnpThumbnailCreator:ConstructL() ");
       
   287     iState = EIdle;
       
   288     User::LeaveIfError(iFsSession.Connect());
       
   289     iImageFilePath = aFilePath.AllocL();
       
   290     
       
   291     ConvertDlnaSizeToImageSize();
       
   292     
       
   293     iThmbManager = CThumbnailManager::NewL( *this );
       
   294         
       
   295     // Set flags: Keep aspect ratio and allow smaller image than requested
       
   296     iThmbManager->SetFlagsL(static_cast<CThumbnailManager::TThumbnailFlags>(
       
   297     CThumbnailManager::EDefaultFlags | CThumbnailManager::EAllowAnySize )); 
       
   298 
       
   299     // Preferred size is 160x120 (or less)
       
   300     iThmbManager->SetThumbnailSizeL( ConvertDlnaSizeToImageSize() );
       
   301     
       
   302     iState = EInitializeMde;
       
   303     iMdESession = CMdESession::NewL(*this);
       
   304     
       
   305     iStatus = KRequestPending;
       
   306     SetActive();
       
   307     }
       
   308 
       
   309 TSize CUpnpThumbnailCreator::ConvertDlnaSizeToImageSize()
       
   310     {
       
   311     TSize thumbnailSize;
       
   312     
       
   313     switch (iThumbnailDlnaSize)
       
   314         {
       
   315         case ESmall:
       
   316             {
       
   317             thumbnailSize = TSize (KSmallWidth, KSmallHeight);
       
   318             break;
       
   319             }
       
   320         case EMedium:
       
   321             {
       
   322             thumbnailSize = TSize (KMediumWidth, KMediumHeight);
       
   323             break;
       
   324             }
       
   325         case ELarge:
       
   326             {
       
   327             thumbnailSize = TSize (KLargeWidth, KLargeHeight);
       
   328             break;
       
   329             }
       
   330         case EThumbnail:
       
   331             {
       
   332             thumbnailSize = TSize (KThumbnailWidth, KThumbnailHeight);
       
   333             break;
       
   334             }
       
   335         case ESmallIcon:
       
   336             {
       
   337             thumbnailSize = TSize (KSmallIconWidth, KSmallIconHeight);
       
   338             break;
       
   339             }
       
   340         case ELargeIcon:
       
   341             {
       
   342             thumbnailSize = TSize (KLargeIconWidth, KLargeIconHeight);
       
   343             break;
       
   344             }
       
   345         default:
       
   346             {
       
   347             __LOG( "CUpnpThumbnailCreator::ConvertDlnaSizeToImageSize - Not Expected!" );
       
   348             __PANIC( __FILE__, __LINE__ );
       
   349             break;
       
   350             }
       
   351         }
       
   352     
       
   353     return thumbnailSize;
       
   354     }
       
   355 
       
   356 // --------------------------------------------------------------------------
       
   357 // CUpnpThumbnailCreator::QueryMdeL
       
   358 // See upnpthumbnailcreator.h
       
   359 //---------------------------------------------------------------------------
       
   360 void CUpnpThumbnailCreator::QueryMdeL()
       
   361     {
       
   362      __LOG( "CUpnpThumbnailCreator:QueryMdeL() ");
       
   363     User::LeaveIfNull(iMdESession); //CI: LeaveIfNull() leaves with KErrNoMemory is this the idea? or can this even happen??
       
   364     
       
   365     CMdENamespaceDef& namespacedef = iMdESession->GetDefaultNamespaceDefL();
       
   366     CMdEObjectDef& objectdef = namespacedef.GetObjectDefL(
       
   367                                     MdeConstants::Image::KImageObject);
       
   368     iMdEQuery = iMdESession->NewObjectQueryL(namespacedef, objectdef, this);
       
   369 
       
   370     iMdEQuery->SetResultMode(EQueryResultModeItem);
       
   371     
       
   372     iMdEQuery->FindL();
       
   373     
       
   374     iStatus = KRequestPending;
       
   375     SetActive();
       
   376     }
       
   377 
       
   378 // --------------------------------------------------------------------------
       
   379 // CUpnpThumbnailCreator::ParseImageMediaObjectL
       
   380 // See upnpthumbnailcreator.h
       
   381 //---------------------------------------------------------------------------
       
   382 
       
   383 void CUpnpThumbnailCreator::GetThumbnail()
       
   384     {
       
   385      __LOG( "CUpnpThumbnailCreator:GetThumbnail() ");
       
   386     // object id is available for the image,
       
   387     // thumbnail manager can give us the thumbnail.
       
   388     iThmbManager->GetThumbnailL(iObjectid);
       
   389     
       
   390     iStatus = KRequestPending;
       
   391     SetActive();
       
   392     }
       
   393 
       
   394 // --------------------------------------------------------------------------
       
   395 // CUpnpThumbnailCreator::PrepareThumbnailFilePathL
       
   396 // See upnpthumbnailcreator.h
       
   397 //---------------------------------------------------------------------------
       
   398 void CUpnpThumbnailCreator::PrepareThumbnailFilePathL()
       
   399     {
       
   400      __LOG( "CUpnpThumbnailCreator:PrepareThumbnailFilePathL() ");
       
   401     TFileName path(KSystemFilePath);    
       
   402     //User::LeaveIfError(iFsSession.SessionPath(path));
       
   403     TInt ret = iFsSession.MkDir(path);
       
   404     //handle when creation of the path fails.
       
   405     if(ret == KErrNone || ret == KErrAlreadyExists )
       
   406         {
       
   407         TParse p;
       
   408         p.Set(iImageFilePath->Des(),NULL,NULL);
       
   409         TFileName filenameandext(p.NameAndExt());
       
   410         path.Append(filenameandext);
       
   411         iThumbnailFilePath = path.AllocL();
       
   412         }
       
   413     else
       
   414         {
       
   415         User::Leave(ret);
       
   416         }
       
   417     }
       
   418 
       
   419 // --------------------------------------------------------------------------
       
   420 // CUpnpThumbnailCreator::SaveThumbnailL
       
   421 // See upnpthumbnailcreator.h
       
   422 //---------------------------------------------------------------------------
       
   423 void CUpnpThumbnailCreator::EncodeAndSaveThumbnailL()
       
   424     {
       
   425      __LOG( "CUpnpThumbnailCreator:SaveThumbnailL() ");
       
   426      PrepareThumbnailFilePathL();
       
   427      
       
   428      iImageEncoder = CImageEncoder::FileNewL(iFsSession,*iThumbnailFilePath,
       
   429              CImageEncoder::EOptionNone, KImageTypeJPGUid,KNullUid );
       
   430 
       
   431      iImageEncoder->Convert( &iStatus, *iThumbnail );
       
   432      SetActive();     
       
   433     }
       
   434 
       
   435 void CUpnpThumbnailCreator::CompleteRequest(TInt aError)
       
   436     {
       
   437     TRequestStatus* stat = &iStatus;
       
   438     User::RequestComplete( stat, aError );
       
   439     }
       
   440 
       
   441 
       
   442 // --------------------------------------------------------------------------
       
   443 // CUpnpBitmapConverter::RunL
       
   444 // See upnpbitmapconverter.h
       
   445 //---------------------------------------------------------------------------
       
   446 void CUpnpThumbnailCreator::RunL()
       
   447     {
       
   448     __LOG1( "CUpnpThumbnailCreator:RunL() iStatus=%d", iStatus.Int());
       
   449     
       
   450     if (iStatus.Int() < 0)
       
   451         {
       
   452         ThumbnailReady(iStatus.Int());
       
   453         }
       
   454     else
       
   455         {
       
   456         switch (iState)
       
   457             {
       
   458             case EIdle:
       
   459                 {
       
   460                 break;
       
   461                 }
       
   462             case EInitializeMde:
       
   463                 {
       
   464                 iState = EQueryingImage;
       
   465                 QueryMdeL();
       
   466                 break;
       
   467                 }
       
   468             case EQueryingImage:
       
   469                 {
       
   470                 iState = EFetchingThumbnail;
       
   471                 GetThumbnail();
       
   472                 break;
       
   473                 }
       
   474             case EFetchingThumbnail:
       
   475                 {
       
   476                 iState = EConvertingThumbnail;
       
   477                 EncodeAndSaveThumbnailL();
       
   478                 break;
       
   479                 }
       
   480             case EConvertingThumbnail:
       
   481                 {
       
   482                 ThumbnailReady(iStatus.Int());             
       
   483                 break;
       
   484                 }
       
   485             }
       
   486         }
       
   487     }
       
   488 
       
   489 TInt CUpnpThumbnailCreator::RunError( TInt aError )
       
   490     {
       
   491     iState = EIdle;
       
   492     ThumbnailReady(aError);
       
   493     return KErrNone;
       
   494     }
       
   495 
       
   496 
       
   497 // --------------------------------------------------------------------------
       
   498 // CUpnpBitmapConverter::DoCancel
       
   499 // See upnpbitmapconverter.h
       
   500 //---------------------------------------------------------------------------
       
   501 void CUpnpThumbnailCreator::DoCancel()
       
   502     {
       
   503     __LOG( "CUpnpThumbnailCreator:DoCancel()");
       
   504     if (iState == EConvertingThumbnail)
       
   505         {
       
   506         iImageEncoder->Cancel();
       
   507         }
       
   508     else
       
   509         {
       
   510         TRequestStatus* stat = &iStatus;
       
   511         User::RequestComplete( stat, KErrCancel );        
       
   512         }
       
   513     }