mmappcomponents/harvester/metadataextractor/src/mpxmetadataextractor.cpp
branchRCL_3
changeset 13 4740b34b83ce
parent 12 51035f0751c2
child 14 f56ec6ce2732
equal deleted inserted replaced
12:51035f0751c2 13:4740b34b83ce
    10 * Nokia Corporation - initial contribution.
    10 * Nokia Corporation - initial contribution.
    11 *
    11 *
    12 * Contributors:
    12 * Contributors:
    13 *
    13 *
    14 * Description:  Extracts metadata from a file
    14 * Description:  Extracts metadata from a file
    15 *  Version     : %version: da1mmcf#38.1.4.2.6.1.9 % << Don't touch! Updated by Synergy at check-out.
    15 *  Version     : %version: da1mmcf#38.1.4.2.6.1.10 % << Don't touch! Updated by Synergy at check-out.
    16 *
    16 *
    17 */
    17 */
    18 
    18 
    19 #include <e32base.h>
    19 #include <e32base.h>
    20 #include <badesca.h>
    20 #include <badesca.h>
    39 #include <mpxmediamusicdefs.h>
    39 #include <mpxmediamusicdefs.h>
    40 #include <mpxmediageneraldefs.h>
    40 #include <mpxmediageneraldefs.h>
    41 #include <mpxmediaaudiodefs.h>
    41 #include <mpxmediaaudiodefs.h>
    42 #include <mpxmediadrmdefs.h>
    42 #include <mpxmediadrmdefs.h>
    43 #include <mpxmediamtpdefs.h>
    43 #include <mpxmediamtpdefs.h>
       
    44 #include <mpxmetadataextractorobserver.h>
    44 
    45 
    45 #include "mpxmetadataextractor.h"
    46 #include "mpxmetadataextractor.h"
    46 #include "mpxfileinfoutility.h"
    47 #include "mpxfileinfoutility.h"
    47 
    48 
    48 _LIT( KWmaMimeType, "audio/x-ms-wma" );
    49 _LIT( KWmaMimeType, "audio/x-ms-wma" );
    80                                   RApaLsSession& aAppArc,
    81                                   RApaLsSession& aAppArc,
    81                                   RPointerArray<CMPXCollectionType>& aTypes  )
    82                                   RPointerArray<CMPXCollectionType>& aTypes  )
    82                                   : iFs( aFs ),
    83                                   : iFs( aFs ),
    83                                     iAppArc( aAppArc ),
    84                                     iAppArc( aAppArc ),
    84                                     iSupportedTypes( aTypes ),
    85                                     iSupportedTypes( aTypes ),
    85                                     iOutstandingThumbnailRequest(0),
    86                                     iCancelled( EFalse ),
    86                                     iTNMBlockCount(0)
    87                                     iObs( NULL ),
       
    88                                     iMedia( NULL ),
       
    89                                     iMetadataOnly( EFalse ),
       
    90                                     iFileOpenError( KErrNone )
    87     {
    91     {
    88 
    92 
    89     }
    93     }
    90 
    94 
    91 
    95 
    96 void CMPXMetadataExtractor::ConstructL()
   100 void CMPXMetadataExtractor::ConstructL()
    97     {
   101     {
    98     iMetadataUtility = CMetaDataUtility::NewL();
   102     iMetadataUtility = CMetaDataUtility::NewL();
    99     iDrmMediaUtility = CMPXDrmMediaUtility::NewL();
   103     iDrmMediaUtility = CMPXDrmMediaUtility::NewL();
   100     iFileInfoUtil    = CMPXFileInfoUtility::NewL();
   104     iFileInfoUtil    = CMPXFileInfoUtility::NewL();
       
   105     iTaskTimer = CPeriodic::NewL( CActive::EPriorityIdle );
   101 
   106 
   102 #ifdef RD_MPX_TNM_INTEGRATION
   107 #ifdef RD_MPX_TNM_INTEGRATION
   103     // Create Thumbnail Manager instance. This object is the observer.
   108     // Create Thumbnail Manager instance. This object is the observer.
   104     iTNManager = CThumbnailManager::NewL( *this );
   109     iTNManager = CThumbnailManager::NewL( *this );
   105     iTNManager->SetFlagsL( CThumbnailManager::EDefaultFlags );
   110     iTNManager->SetFlagsL( CThumbnailManager::EDefaultFlags );
   106     iTNManager->SetQualityPreferenceL( CThumbnailManager::EOptimizeForQuality);
   111     iTNManager->SetQualityPreferenceL( CThumbnailManager::EOptimizeForQuality);
   107     // create wait loop
   112     // create wait loop
   108     iTNSyncWait = new (ELeave) CActiveSchedulerWait;
   113     iTNSyncWait = new (ELeave) CActiveSchedulerWait;
   109     iTimer = CPeriodic::NewL( CActive::EPriorityIdle );
   114     iTimer = CPeriodic::NewL( CActive::EPriorityIdle );
       
   115     iArrayTNRequestId.Reset();
       
   116     iArrayTasks.Reset();
   110 #endif //RD_MPX_TNM_INTEGRATION
   117 #endif //RD_MPX_TNM_INTEGRATION
   111     }
   118     }
   112 
   119 
   113 // ---------------------------------------------------------------------------
   120 // ---------------------------------------------------------------------------
   114 // Two-Phased Constructor
   121 // Two-Phased Constructor
   136 CMPXMetadataExtractor::~CMPXMetadataExtractor()
   143 CMPXMetadataExtractor::~CMPXMetadataExtractor()
   137     {
   144     {
   138     delete iMetadataUtility;
   145     delete iMetadataUtility;
   139     delete iFileInfoUtil;
   146     delete iFileInfoUtil;
   140     delete iDrmMediaUtility;
   147     delete iDrmMediaUtility;
       
   148     delete iTaskTimer;
       
   149     iFile.Close();
   141 #ifdef RD_MPX_TNM_INTEGRATION
   150 #ifdef RD_MPX_TNM_INTEGRATION
   142     delete iTNManager;
   151     delete iTNManager;
   143     if (iTNSyncWait && iTNSyncWait->IsStarted() )
   152     if (iTNSyncWait && iTNSyncWait->IsStarted() )
   144         {
   153         {
   145         iTNSyncWait->AsyncStop();
   154         iTNSyncWait->AsyncStop();
   146         }
   155         }
   147     delete iTNSyncWait;
   156     delete iTNSyncWait;
   148     delete iTimer;
   157     delete iTimer;
       
   158     iArrayTNRequestId.Close();
       
   159     iArrayTasks.Close();
   149 #endif //RD_MPX_TNM_INTEGRATION
   160 #endif //RD_MPX_TNM_INTEGRATION
   150     
   161     }
   151     MPX_DEBUG2("CMPXMetadataExtractor: TNM Block Count: %d ", iTNMBlockCount );
   162 
   152     }
   163 // ---------------------------------------------------------------------------
   153 
   164 // Constructs a media properties object : synchronous function
   154 // ---------------------------------------------------------------------------
       
   155 // Constructs a media properties object
       
   156 // ---------------------------------------------------------------------------
   165 // ---------------------------------------------------------------------------
   157 //
   166 //
   158 EXPORT_C void CMPXMetadataExtractor::CreateMediaL( const TDesC& aFile,
   167 EXPORT_C void CMPXMetadataExtractor::CreateMediaL( const TDesC& aFile,
   159                                                    CMPXMedia*& aNewProperty,
   168                                                    CMPXMedia*& aNewProperty,
   160                                                    TBool aMetadataOnly )
   169                                                    TBool aMetadataOnly )
   161     {
   170     {
   162     // make a copy of aFile
   171     MPX_FUNC("CMPXMetadataExtractor::CreateMediaL()");
   163     HBufC* fileName = HBufC::NewL(KMaxFileName);
   172     // check if we are still processing a request.
   164     CleanupStack::PushL( fileName );
   173     if ( iArrayTasks.Count() )
   165     fileName->Des().Append( aFile );
   174         {
   166     MPX_DEBUG2("CMPXMetadataExtractor::CreateMediaL %S <---", fileName );
   175         MPX_DEBUG1("CMPXMetadataExtractor::CreateMediaL Request ongoing. Abort!" );
   167 
   176         User::Leave( KErrAbort );
   168     RArray<TInt> contentIDs;
   177         }
   169     contentIDs.AppendL( KMPXMediaIdGeneral );
   178 
   170     contentIDs.AppendL( KMPXMediaIdAudio );
   179     iCancelled = EFalse;
   171     contentIDs.AppendL( KMPXMediaIdMusic );
   180     iFileOpenError = KErrNone;
   172     contentIDs.AppendL( KMPXMediaIdDrm );
   181     iObs = NULL;
   173     contentIDs.AppendL( KMPXMediaIdMTP );
       
   174     aNewProperty = NULL;
   182     aNewProperty = NULL;
   175     CMPXMedia* media = CMPXMedia::NewL( contentIDs.Array() );
   183     iFileName = aFile;
   176     CleanupStack::PushL( media );
   184     iMetadataOnly = aMetadataOnly;
   177     contentIDs.Close();
   185     
   178 
   186     // populate the task array
   179     // CMPXMedia default types
   187     AddTasksL();
   180 
   188     
   181     media->SetTObjectValueL<TMPXGeneralType>( KMPXMediaGeneralType,
   189     // execute all tasks in the array
   182                                               EMPXItem );
   190     while ( iArrayTasks.Count() )
   183     media->SetTObjectValueL<TMPXGeneralCategory>( KMPXMediaGeneralCategory,
   191         {
   184                                                   EMPXSong );
   192         // execute task at index 0
   185 
   193         TRAPD( error, ExecuteTaskL() );
   186     TParsePtrC parse( *fileName );
   194         if ( error || iCancelled )
   187 
   195             {
   188     // Title, default is file name
   196             // cleanup
   189     media->SetTextValueL( KMPXMediaGeneralTitle,
   197             if ( iMedia != NULL )
   190                           parse.Name() );
   198                 {
   191 
   199                 delete iMedia;
   192     // Default album track
   200                 iMedia = NULL;
   193     media->SetTextValueL( KMPXMediaMusicAlbumTrack,
   201                 }
   194                           KNullDesC );
   202             iArrayTasks.Reset();
   195 
   203             if ( error )
   196     // Set the Mime Type and collection UID
   204                 {
   197     //
   205                 CleanUp();
   198     if( !aMetadataOnly )
   206                 User::LeaveIfError( error );
   199         {
   207                 }
   200         TInt index(KErrNotFound);
   208             break;
   201         TInt count( iSupportedTypes.Count() );
   209             }
   202         for (TInt i=0; i <count; ++i)
       
   203             {
       
   204             TInt index2(KErrNotFound);
       
   205             const CDesCArray& exts = iSupportedTypes[i]->Extensions();
       
   206             const TDesC& ext = parse.Ext();
       
   207             if (!exts.FindIsq(ext, index2))
       
   208                 { // found
       
   209                 index = i;
       
   210                 break;
       
   211                 }
       
   212             }
       
   213         if( KErrNotFound != index )
       
   214             {
       
   215             MPX_DEBUG1("CMPXMetadataExtractor::CreateMediaPropertiesL apparc <---" );
       
   216             TInt mimeIndex = SupportedContainerTypeL( *fileName, index );
       
   217             User::LeaveIfError( mimeIndex );
       
   218             MPX_DEBUG1("CMPXMetadataExtractor::CreateMediaPropertiesL apparc --->" );
       
   219 
       
   220             media->SetTextValueL( KMPXMediaGeneralMimeType,
       
   221                                   iSupportedTypes[index]->Mimetypes()[mimeIndex] );
       
   222 
       
   223             media->SetTObjectValueL( KMPXMediaGeneralCollectionId,
       
   224                                      iSupportedTypes[index]->Uid() );
       
   225             }
       
   226         else
       
   227             {
       
   228             User::Leave(KErrNotSupported);
       
   229             }
       
   230         }
       
   231     else // other case use apparc to fetch and set mimetype
       
   232         {
       
   233         TDataType dataType;
       
   234         TUid dummyUid(KNullUid);
       
   235         iAppArc.AppForDocument(*fileName, dummyUid, dataType);
       
   236         media->SetTextValueL( KMPXMediaGeneralMimeType,dataType.Des() );
       
   237         }
       
   238         
   210         
   239     // Use file handle here
   211         iArrayTasks.Remove( 0 );
   240     //
   212         }
   241     RFile file;
   213     
   242     TInt err = file.Open( iFs, *fileName, EFileRead | EFileShareReadersOrWriters );
   214     aNewProperty = iMedia;
   243     CleanupClosePushL(file);
   215     CleanUp();
   244     
       
   245     // Initially set default tags.
       
   246     SetDefaultL( *media );
       
   247     // Metadata related
       
   248     //
       
   249     if( err == KErrNone )
       
   250         {
       
   251         const TDesC& mimeType = media->ValueText( KMPXMediaGeneralMimeType );
       
   252         HBufC8* mimeType8 = HBufC8::NewLC( mimeType.Length() );
       
   253         mimeType8->Des().Append( mimeType );
       
   254         TRAPD( metadataerror, iMetadataUtility->OpenFileL( file, *mimeType8 ) );
       
   255         MPX_DEBUG2("CMPXMetadataExtractor::CreateMediaL, error %d parsing metadata", 
       
   256             metadataerror );
       
   257         CleanupStack::PopAndDestroy( mimeType8 );
       
   258         
       
   259         // Even if there is error extracting metadata, fill extracted tags.
       
   260         TRAP( metadataerror, SetMediaPropertiesL( *media, *fileName ) ); 
       
   261         MPX_DEBUG2("CMPXMetadataExtractor::CreateMediaL, error %d setting tags", 
       
   262             metadataerror );
       
   263 
       
   264         // Reset the utility
       
   265         iMetadataUtility->ResetL();
       
   266         }
       
   267     
       
   268     // Common properties that we can extract
       
   269     //
       
   270     SetExtMediaPropertiesL( *media, *fileName, aMetadataOnly, file, err );
       
   271     CleanupStack::PopAndDestroy(&file);
       
   272 
       
   273     // Set the pointers now that the object is ready
       
   274     //
       
   275     CleanupStack::Pop( media );
       
   276     aNewProperty = media;
       
   277 
       
   278     CleanupStack::PopAndDestroy( fileName );
       
   279     MPX_DEBUG1("CMPXMetadataExtractor::CreateMediaPropertiesL --->");
       
   280     }
   216     }
   281 
   217 
   282 // ---------------------------------------------------------------------------
   218 // ---------------------------------------------------------------------------
   283 // Sets all of the default media properties
   219 // Sets all of the default media properties
   284 // ---------------------------------------------------------------------------
   220 // ---------------------------------------------------------------------------
   285 //
   221 //
   286 void CMPXMetadataExtractor::SetDefaultL( CMPXMedia& aMediaProp )
   222 void CMPXMetadataExtractor::SetDefaultL( CMPXMedia& aMediaProp )
   287     {
   223     {
       
   224     MPX_FUNC("CMPXMetadataExtractor::SetDefaultL()");
   288     // Comment
   225     // Comment
   289     aMediaProp.SetTextValueL( KMPXMediaGeneralComment,
   226     aMediaProp.SetTextValueL( KMPXMediaGeneralComment,
   290                               KNullDesC );
   227                               KNullDesC );
   291     // Artist
   228     // Artist
   292     aMediaProp.SetTextValueL( KMPXMediaMusicArtist,
   229     aMediaProp.SetTextValueL( KMPXMediaMusicArtist,
   316 
   253 
   317 // ---------------------------------------------------------------------------
   254 // ---------------------------------------------------------------------------
   318 // Sets media object attributes from metadata utilities
   255 // Sets media object attributes from metadata utilities
   319 // ---------------------------------------------------------------------------
   256 // ---------------------------------------------------------------------------
   320 //
   257 //
   321 void CMPXMetadataExtractor::SetMediaPropertiesL( CMPXMedia& aMedia,
   258 void CMPXMetadataExtractor::SetMediaPropertiesL()
   322                                                  const TDesC& aFile )
   259     {
   323     {
   260     MPX_FUNC("CMPXMetadataExtractor::SetMediaPropertiesL()");
   324     MPX_DEBUG1("CMPXMetadataExtractor::SetMediaPropertiesL <---" );
   261 
   325 
   262     const TDesC& mimeType = iMedia->ValueText( KMPXMediaGeneralMimeType );
   326     const CMetaDataFieldContainer& metaCont =
   263     HBufC8* mimeType8 = HBufC8::NewLC( mimeType.Length() );
   327                                           iMetadataUtility->MetaDataFieldsL();
   264     mimeType8->Des().Append( mimeType );
       
   265     
       
   266     // Continue to extract metadata even if fail.
       
   267     TRAPD( metadataerror, iMetadataUtility->OpenFileL( iFile, *mimeType8 ) );
       
   268     MPX_DEBUG2("CMPXMetadataExtractor::CreateMediaL, error %d parsing metadata", 
       
   269         metadataerror );
       
   270     CleanupStack::PopAndDestroy( mimeType8 );
       
   271     
       
   272     const CMetaDataFieldContainer& metaCont = iMetadataUtility->MetaDataFieldsL();
   328     TInt count( metaCont.Count() );
   273     TInt count( metaCont.Count() );
   329     for( TInt i=0; i<count; ++i )
   274     for( TInt i=0; i<count; ++i )
   330         {
   275         {
   331         TMetaDataFieldId fieldType;
   276         TMetaDataFieldId fieldType;
   332 
   277 
   335         
   280         
   336         // get the value, except for album art
   281         // get the value, except for album art
   337         if ( fieldType != EMetaDataJpeg )
   282         if ( fieldType != EMetaDataJpeg )
   338            {
   283            {
   339            TRAPD( err, value = metaCont.At( i, fieldType ).AllocL() );
   284            TRAPD( err, value = metaCont.At( i, fieldType ).AllocL() );
   340            CleanupStack::PushL( value );
       
   341            if ( KErrNone != err )
   285            if ( KErrNone != err )
   342                {
   286                {
   343                MPX_DEBUG2("CMPXMetadataExtractor::SetMediaPropertiesL - error = %i", err);           
   287                MPX_DEBUG2("CMPXMetadataExtractor::SetMediaPropertiesL - error = %i", err);           
   344                CleanupStack::PopAndDestroy( value );
       
   345                continue;
   288                continue;
   346                }     
   289                }     
       
   290            CleanupStack::PushL( value );
   347            }
   291            }
   348         
   292         
   349         switch( fieldType )
   293         switch( fieldType )
   350             {
   294             {
   351             case EMetaDataSongTitle:
   295             case EMetaDataSongTitle:
   354                 valptr.Trim();
   298                 valptr.Trim();
   355                 TInt vallen = value->Length();
   299                 TInt vallen = value->Length();
   356                 if (vallen>0)
   300                 if (vallen>0)
   357                     {
   301                     {
   358                     FindAndReplaceForbiddenChars(valptr, vallen);
   302                     FindAndReplaceForbiddenChars(valptr, vallen);
   359                     aMedia.SetTextValueL(KMPXMediaGeneralTitle, *value);
   303                     iMedia->SetTextValueL(KMPXMediaGeneralTitle, *value);
   360                     }
   304                     }
   361                 break;
   305                 break;
   362                 }
   306                 }
   363             case EMetaDataArtist:
   307             case EMetaDataArtist:
   364                 {
   308                 {
   366                 valptr.Trim();
   310                 valptr.Trim();
   367                 TInt vallen = value->Length();
   311                 TInt vallen = value->Length();
   368                 if (vallen>0)
   312                 if (vallen>0)
   369                     {
   313                     {
   370                     FindAndReplaceForbiddenChars(valptr, vallen);
   314                     FindAndReplaceForbiddenChars(valptr, vallen);
   371                     aMedia.SetTextValueL(KMPXMediaMusicArtist, *value);
   315                     iMedia->SetTextValueL(KMPXMediaMusicArtist, *value);
   372                     }
   316                     }
   373                 break;
   317                 break;
   374                 }
   318                 }
   375             case EMetaDataAlbum:
   319             case EMetaDataAlbum:
   376                 {
   320                 {
   378                 valptr.Trim();
   322                 valptr.Trim();
   379                 TInt vallen = value->Length();
   323                 TInt vallen = value->Length();
   380                 if (vallen>0)
   324                 if (vallen>0)
   381                     {
   325                     {
   382                     FindAndReplaceForbiddenChars(valptr, vallen);
   326                     FindAndReplaceForbiddenChars(valptr, vallen);
   383                     aMedia.SetTextValueL(KMPXMediaMusicAlbum, *value );
   327                     iMedia->SetTextValueL(KMPXMediaMusicAlbum, *value );
   384                     }
   328                     }
   385                 break;
   329                 break;
   386                 }
   330                 }
   387             case EMetaDataYear:
   331             case EMetaDataYear:
   388                 {
   332                 {
   393 
   337 
   394                 // Convert from TInt -> TDateTime -> TTime -> TInt64
   338                 // Convert from TInt -> TDateTime -> TTime -> TInt64
   395                 TDateTime dt;
   339                 TDateTime dt;
   396                 dt.SetYear( year );
   340                 dt.SetYear( year );
   397                 TTime time( dt );
   341                 TTime time( dt );
   398                 aMedia.SetTObjectValueL<TInt64>( KMPXMediaMusicYear,
   342                 iMedia->SetTObjectValueL<TInt64>( KMPXMediaMusicYear,
   399                                                  time.Int64() );
   343                                                  time.Int64() );
   400                 break;
   344                 break;
   401                 }
   345                 }
   402             case EMetaDataComment:
   346             case EMetaDataComment:
   403                 {
   347                 {
   404                 aMedia.SetTextValueL( KMPXMediaGeneralComment,
   348                 iMedia->SetTextValueL( KMPXMediaGeneralComment,
   405                                       *value );
   349                                       *value );
   406                 break;
   350                 break;
   407                 }
   351                 }
   408             case EMetaDataAlbumTrack:
   352             case EMetaDataAlbumTrack:
   409                 {
   353                 {
   410                 aMedia.SetTextValueL( KMPXMediaMusicAlbumTrack,
   354                 iMedia->SetTextValueL( KMPXMediaMusicAlbumTrack,
   411                                       *value );
   355                                       *value );
   412                 break;
   356                 break;
   413                 }
   357                 }
   414             case EMetaDataGenre:
   358             case EMetaDataGenre:
   415                 {
   359                 {
   417                 valptr.Trim();
   361                 valptr.Trim();
   418                 TInt vallen = value->Length();
   362                 TInt vallen = value->Length();
   419                 if (vallen>0)
   363                 if (vallen>0)
   420                     {
   364                     {
   421                     FindAndReplaceForbiddenChars(valptr, vallen);
   365                     FindAndReplaceForbiddenChars(valptr, vallen);
   422                     aMedia.SetTextValueL(KMPXMediaMusicGenre, *value);
   366                     iMedia->SetTextValueL(KMPXMediaMusicGenre, *value);
   423                     }
   367                     }
   424                 break;
   368                 break;
   425                 }
   369                 }
   426             case EMetaDataComposer:
   370             case EMetaDataComposer:
   427                 {
   371                 {
   429                 valptr.Trim();
   373                 valptr.Trim();
   430                 TInt vallen = value->Length();
   374                 TInt vallen = value->Length();
   431                 if (vallen>0)
   375                 if (vallen>0)
   432                     {
   376                     {
   433                     FindAndReplaceForbiddenChars(valptr, vallen);
   377                     FindAndReplaceForbiddenChars(valptr, vallen);
   434                     aMedia.SetTextValueL(KMPXMediaMusicComposer, *value);
   378                     iMedia->SetTextValueL(KMPXMediaMusicComposer, *value);
   435                     }
   379                     }
   436                 break;
   380                 break;
   437                 }
   381                 }
   438             case EMetaDataUrl:
   382             case EMetaDataUrl:
   439             case EMetaDataUserUrl:  // fall through
   383             case EMetaDataUserUrl:  // fall through
   440                 {
   384                 {
   441                 aMedia.SetTextValueL( KMPXMediaMusicURL,
   385                 iMedia->SetTextValueL( KMPXMediaMusicURL,
   442                                       *value );
   386                                       *value );
   443                 break;
   387                 break;
   444                 }
   388                 }
   445             case EMetaDataJpeg:
   389             case EMetaDataJpeg:
   446                 {
   390                 {
   447 #ifdef RD_MPX_TNM_INTEGRATION
   391                 // Album art handled in AddMediaAlbumArtL()
   448                 MPX_PERF_START(CMPXMetadataExtractor_SetMediaPropertiesL_JPEG_TNM);
       
   449                 TPtrC8 ptr8 = metaCont.Field8( EMetaDataJpeg );
       
   450                 HBufC8* value8; 
       
   451                 TRAPD( err, value8 = ptr8.AllocL() );
       
   452                 if ( KErrNone != err )
       
   453                     {
       
   454                     MPX_DEBUG2("CMPXMetadataExtractor::SetMediaPropertiesL - error jpeg = %i", err);           
       
   455                     }
       
   456                 else
       
   457                     {
       
   458                     CleanupStack::PushL( value8 );
       
   459                     TRAPD( err, AddMediaAlbumArtL( aMedia, aFile, *value8 ) );
       
   460                     if ( KErrNone != err )
       
   461                         {
       
   462                         MPX_DEBUG2("CMPXMetadataExtractor::SetMediaPropertiesL - error album art = %i", err);           
       
   463                         }
       
   464                     CleanupStack::Pop( value8 );
       
   465                     }
       
   466                 MPX_PERF_END(CMPXMetadataExtractor_SetMediaPropertiesL_JPEG_TNM);
       
   467 #else //RD_MPX_TNM_INTEGRATION
       
   468                 aMedia.SetTextValueL( KMPXMediaMusicAlbumArtFileName,
       
   469                                       aFile );
       
   470 #endif //RD_MPX_TNM_INTEGRATION
       
   471                 break;
   392                 break;
   472                 }
   393                 }
   473             case EMetaDataCopyright:
   394             case EMetaDataCopyright:
   474                 {
   395                 {
   475                 aMedia.SetTextValueL( KMPXMediaGeneralCopyright,
   396                 iMedia->SetTextValueL( KMPXMediaGeneralCopyright,
   476                                       *value );
   397                                       *value );
   477                 break;
   398                 break;
   478                 }
   399                 }
   479             case EMetaDataDuration:     
   400             case EMetaDataDuration:     
   480                 {                  
   401                 {                  
   481                 const TDesC& mimeType = aMedia.ValueText( KMPXMediaGeneralMimeType );
   402                 const TDesC& mimeType = iMedia->ValueText( KMPXMediaGeneralMimeType );
   482                 MPX_DEBUG2("CMPXMetadataExtractor::SetExtMediaPropertiesL, mimeType = %S", &mimeType);   
   403                 MPX_DEBUG2("CMPXMetadataExtractor::SetExtMediaPropertiesL, mimeType = %S", &mimeType);   
   483                 
   404                 
   484                 // Verify if WMA, get the duration
   405                 // Verify if WMA, get the duration
   485                 if( mimeType.Compare(KWmaMimeType) == 0 || mimeType.Compare(KWmaCafMimeType) == 0 )
   406                 if( mimeType.Compare(KWmaMimeType) == 0 || mimeType.Compare(KWmaCafMimeType) == 0 )
   486                     {
   407                     {
   490                     TLex lexer( *value );
   411                     TLex lexer( *value );
   491                     TInt32 duration ( 0 );
   412                     TInt32 duration ( 0 );
   492                     lexer.Val( duration );   // [second]      
   413                     lexer.Val( duration );   // [second]      
   493                     duration *= 1000;        // [msec]
   414                     duration *= 1000;        // [msec]
   494                 
   415                 
   495                     aMedia.SetTObjectValueL<TInt32>( KMPXMediaGeneralDuration,
   416                     iMedia->SetTObjectValueL<TInt32>( KMPXMediaGeneralDuration,
   496                                                 duration );      
   417                                                 duration );      
   497                 
   418                 
   498                     MPX_DEBUG2("CMPXMetadataExtractor::SetMediaPropertiesL- duration = %i", duration);  
   419                     MPX_DEBUG2("CMPXMetadataExtractor::SetMediaPropertiesL- duration = %i", duration);  
   499                     }
   420                     }
   500                 break;
   421                 break;
   517         if (fieldType != EMetaDataJpeg)
   438         if (fieldType != EMetaDataJpeg)
   518             {
   439             {
   519             CleanupStack::PopAndDestroy( value );       
   440             CleanupStack::PopAndDestroy( value );       
   520             }
   441             }
   521         }
   442         }
   522 
       
   523     MPX_DEBUG1("CMPXMetadataExtractor::SetMediaPropertiesL --->" );
       
   524     }
   443     }
   525 
   444 
   526 // ---------------------------------------------------------------------------
   445 // ---------------------------------------------------------------------------
   527 // Sets extra media properties not returned by metadata utilities
   446 // Sets extra media properties not returned by metadata utilities
   528 // ---------------------------------------------------------------------------
   447 // ---------------------------------------------------------------------------
   529 //
   448 //
   530 void CMPXMetadataExtractor::SetExtMediaPropertiesL( CMPXMedia& aProp, 
   449 void CMPXMetadataExtractor::SetExtMediaPropertiesL()
   531                                                     const TDesC& aFile,
   450     {
   532                                                     TBool aMetadataOnly,
   451     MPX_FUNC("CMPXMetadataExtractor::SetExtMediaPropertiesL()");
   533                                                     RFile& aFileHandle,
       
   534                                                     TInt aFileErr )
       
   535     {
       
   536     MPX_DEBUG1("CMPXMetadataExtractor::SetExtMediaPropertiesL <---");
       
   537 
   452 
   538     // DB Flags to set
   453     // DB Flags to set
   539     //
   454     //
   540     TUint dbFlags(KMPXMediaGeneralFlagsSetOrUnsetBit);
   455     TUint dbFlags(KMPXMediaGeneralFlagsSetOrUnsetBit);
   541 
   456 
   542     // File Path
   457     // File Path
   543     //
   458     //
   544     TParsePtrC parse( aFile );
   459     TParsePtrC parse( iFileName );
   545     aProp.SetTextValueL( KMPXMediaGeneralUri,
   460     iMedia->SetTextValueL( KMPXMediaGeneralUri, iFileName );
   546                          aFile );
   461     iMedia->SetTextValueL( KMPXMediaGeneralDrive, parse.Drive() );
   547     aProp.SetTextValueL( KMPXMediaGeneralDrive,
       
   548                          parse.Drive() );
       
   549 
   462 
   550     // DRM Rights
   463     // DRM Rights
   551     //
   464     //
   552     CMPXMedia* drm = NULL;
   465     CMPXMedia* drm = NULL;
   553     TRAPD( drmError, iDrmMediaUtility->InitL( aFile );
   466     TRAPD( drmError, iDrmMediaUtility->InitL( iFileName );
   554                      drm = CMPXMedia::NewL( *iDrmMediaUtility->GetMediaL( KMPXMediaDrmProtected.iAttributeId |
   467            drm = CMPXMedia::NewL( *iDrmMediaUtility->GetMediaL( KMPXMediaDrmProtected.iAttributeId |
   555                                                                           KMPXMediaDrmRightsStatus.iAttributeId ) );
   468                                                                 KMPXMediaDrmRightsStatus.iAttributeId ) );
   556          );
   469          );
   557 
   470 
   558     TBool prot(EFalse);
   471     TBool prot(EFalse);
   559     if( drm )
   472     if( drm )
   560         {
   473         {
   568 
   481 
   569         TMPXMediaDrmRightsStatus status(EMPXDrmRightsFull);
   482         TMPXMediaDrmRightsStatus status(EMPXDrmRightsFull);
   570         if( drm->IsSupported( KMPXMediaDrmRightsStatus ) )
   483         if( drm->IsSupported( KMPXMediaDrmRightsStatus ) )
   571             {
   484             {
   572             status = drm->ValueTObjectL<TMPXMediaDrmRightsStatus>(KMPXMediaDrmRightsStatus);
   485             status = drm->ValueTObjectL<TMPXMediaDrmRightsStatus>(KMPXMediaDrmRightsStatus);
   573             aProp.SetTObjectValueL<TInt>(KMPXMediaDrmRightsStatus, status );
   486             iMedia->SetTObjectValueL<TInt>(KMPXMediaDrmRightsStatus, status );
   574             MPX_DEBUG2("CMPXMetadataExtractor::SetExtMediaPropertiesL -- status %i", status);
   487             MPX_DEBUG2("CMPXMetadataExtractor::SetExtMediaPropertiesL -- status %i", status);
   575             }
   488             }
   576 
   489 
   577         // Set DB flags
   490         // Set DB flags
   578         dbFlags |= KMPXMediaGeneralFlagsIsDrmProtected;
   491         dbFlags |= KMPXMediaGeneralFlagsIsDrmProtected;
   592     else
   505     else
   593         {
   506         {
   594         User::LeaveIfError( drmError );
   507         User::LeaveIfError( drmError );
   595         }
   508         }
   596 
   509 
   597     aProp.SetTObjectValueL<TBool>( KMPXMediaDrmProtected, prot );
   510     iMedia->SetTObjectValueL<TBool>( KMPXMediaDrmProtected, prot );
   598     aProp.SetTObjectValueL<TUint16>( KMPXMediaMTPDrmStatus, (TUint16)prot );
   511     iMedia->SetTObjectValueL<TUint16>( KMPXMediaMTPDrmStatus, (TUint16)prot );
   599     
   512     
   600     iDrmMediaUtility->Close();
   513     iDrmMediaUtility->Close();
   601     
   514     
   602     //
   515     //
   603     // File Size --- The following needs MMF support
   516     // File Size --- The following needs MMF support
   604     //
   517     //
   605     TInt size( 0 );
   518     TInt size( 0 );
   606     if( aFileErr == KErrNone )
   519     if( iFileOpenError == KErrNone )
   607         {
   520         {
   608         const TDesC& mimeType = aProp.ValueText( KMPXMediaGeneralMimeType );
   521         const TDesC& mimeType = iMedia->ValueText( KMPXMediaGeneralMimeType );
   609         MPX_DEBUG2("CMPXMetadataExtractor::SetExtMediaPropertiesL, mimeType = %S", &mimeType);   
   522         MPX_DEBUG2("CMPXMetadataExtractor::SetExtMediaPropertiesL, mimeType = %S", &mimeType);   
   610         
   523         
   611         // Verify if WMA, skip getting info from MMF
   524         // Verify if WMA, skip getting info from MMF
   612         if( mimeType.Compare(KWmaMimeType) == 0 || mimeType.Compare(KWmaCafMimeType) == 0 )
   525         if( mimeType.Compare(KWmaMimeType) == 0 || mimeType.Compare(KWmaCafMimeType) == 0 )
   613             {
   526             {
   615             MPX_DEBUG1("CMPXMetadataExtractor::SetExtMediaPropertiesL, skip MMF ");   
   528             MPX_DEBUG1("CMPXMetadataExtractor::SetExtMediaPropertiesL, skip MMF ");   
   616             }
   529             }
   617         else
   530         else
   618             {
   531             {
   619             MPX_DEBUG1("CMPXMetadataExtractor::SetExtMediaPropertiesL, get MMF controller");   
   532             MPX_DEBUG1("CMPXMetadataExtractor::SetExtMediaPropertiesL, get MMF controller");   
   620             aFileHandle.Size( size );
   533             iFile.Size( size );
   621             aProp.SetTObjectValueL<TInt>( KMPXMediaGeneralSize, size );
   534             iMedia->SetTObjectValueL<TInt>( KMPXMediaGeneralSize, size );
   622 
   535 
   623             // Duration, bitrate, samplerate, etc
   536             // Duration, bitrate, samplerate, etc
   624             //
   537             //
   625             if( !aMetadataOnly )
   538             if( !iMetadataOnly )
   626             {
   539             {
   627                 TRAPD(err2, iFileInfoUtil->OpenFileL(
   540                 TRAPD(err2, iFileInfoUtil->OpenFileL(
   628                           aFileHandle, 
   541                           iFile, 
   629                           aProp.ValueText(KMPXMediaGeneralMimeType)));
   542                           iMedia->ValueText(KMPXMediaGeneralMimeType)));
   630                 MPX_DEBUG2("CMPXMetadataExtractor::SetExtMediaPropertiesL, file info util error %i", err2);
   543                 MPX_DEBUG2("CMPXMetadataExtractor::SetExtMediaPropertiesL, file info util error %i", err2);
   631                 if( KErrNone == err2 )
   544                 if( KErrNone == err2 )
   632                     {
   545                     {
   633                     aProp.SetTObjectValueL<TUint>( KMPXMediaAudioBitrate,
   546                     iMedia->SetTObjectValueL<TUint>( KMPXMediaAudioBitrate,
   634                                                    iFileInfoUtil->BitRate() );
   547                                                    iFileInfoUtil->BitRate() );
   635                     aProp.SetTObjectValueL<TUint>( KMPXMediaAudioSamplerate,
   548                     iMedia->SetTObjectValueL<TUint>( KMPXMediaAudioSamplerate,
   636                                                    iFileInfoUtil->SampleRate() );
   549                                                    iFileInfoUtil->SampleRate() );
   637                     TInt64 duration = (TInt64) iFileInfoUtil->Duration().Int64() / 1000; // ms
   550                     TInt64 duration = (TInt64) iFileInfoUtil->Duration().Int64() / 1000; // ms
   638                     aProp.SetTObjectValueL<TInt32>( KMPXMediaGeneralDuration,
   551                     iMedia->SetTObjectValueL<TInt32>( KMPXMediaGeneralDuration,
   639                                                     duration );
   552                                                     duration );
   640 
   553 
   641                     MPX_DEBUG2("CMPXMetadataExtractor::SetExtMediaPropertiesL -- duration %i", duration);
   554                     MPX_DEBUG2("CMPXMetadataExtractor::SetExtMediaPropertiesL -- duration %i", duration);
   642                     }
   555                     }
   643                 
   556                 
   644                 iFileInfoUtil->Reset();
   557                 iFileInfoUtil->Reset();
   645                 }
   558                 }
   646             }
   559             }
   647         }
   560         }
   648     else if( aFileErr == KErrNotFound || aFileErr == KErrPathNotFound )
   561     else if( iFileOpenError == KErrNotFound || iFileOpenError == KErrPathNotFound )
   649         {
   562         {
   650         dbFlags |= KMPXMediaGeneralFlagsIsInvalid;
   563         dbFlags |= KMPXMediaGeneralFlagsIsInvalid;
   651         }
   564         }
   652     // Finally set the db flag
   565     // Finally set the db flag
   653     //
   566     //
   654     aProp.SetTObjectValueL( KMPXMediaGeneralFlags,
   567     iMedia->SetTObjectValueL( KMPXMediaGeneralFlags,
   655                             dbFlags );
   568                             dbFlags );
   656     
       
   657     MPX_DEBUG1("CMPXMetadataExtractor::SetExtMediaPropertiesL --->");
       
   658     }
   569     }
   659 
   570 
   660 // ---------------------------------------------------------------------------
   571 // ---------------------------------------------------------------------------
   661 // Check to see if this file is a supported container
   572 // Check to see if this file is a supported container
   662 // ---------------------------------------------------------------------------
   573 // ---------------------------------------------------------------------------
   663 //
   574 //
   664 TInt CMPXMetadataExtractor::SupportedContainerTypeL( const TDesC& aFile,
   575 TInt CMPXMetadataExtractor::SupportedContainerTypeL( const TDesC& aFile,
   665                                                      TInt aIndex )
   576                                                      TInt aIndex )
   666     {
   577     {
       
   578     MPX_FUNC("CMPXMetadataExtractor::SupportedContainerTypeL()");
   667     TInt index(KErrNotFound);
   579     TInt index(KErrNotFound);
   668 
   580 
   669     TDataType dataType;
   581     TDataType dataType;
   670     TUid dummyUid(KNullUid);
   582     TUid dummyUid(KNullUid);
   671     iAppArc.AppForDocument(aFile, dummyUid, dataType);
   583     iAppArc.AppForDocument(aFile, dummyUid, dataType);
   695 // ---------------------------------------------------------------------------
   607 // ---------------------------------------------------------------------------
   696 // CMPXMetadataExtractor::ThumbnailReady
   608 // CMPXMetadataExtractor::ThumbnailReady
   697 // Callback but not used here
   609 // Callback but not used here
   698 // ---------------------------------------------------------------------------
   610 // ---------------------------------------------------------------------------
   699 void CMPXMetadataExtractor::ThumbnailReady( TInt /*aError*/, 
   611 void CMPXMetadataExtractor::ThumbnailReady( TInt /*aError*/, 
   700         MThumbnailData& /*aThumbnail*/, TThumbnailRequestId /*aId*/ )
   612         MThumbnailData& /*aThumbnail*/, TThumbnailRequestId aId )
   701     {
   613     {
   702     MPX_FUNC("CMPXMetadataExtractor::ThumbnailReady()");
   614     MPX_FUNC("CMPXMetadataExtractor::ThumbnailReady()");
   703     MPX_DEBUG2("CMPXMetadataExtractor::ThumbnailReady(): iOutstandingThumbnailRequest %d",
   615     
   704         iOutstandingThumbnailRequest);
   616     // Remove thumbnail id from array.
   705     iOutstandingThumbnailRequest--;
   617     TInt index = iArrayTNRequestId.Find( aId );
   706     if ( iOutstandingThumbnailRequest < KMPXMaxThumbnailRequest )
   618     if ( index >= 0 )
       
   619         {
       
   620         iArrayTNRequestId.Remove( index );
       
   621         }
       
   622     MPX_DEBUG2("CMPXMetadataExtractor::ThumbnailReady(): Outstanding Thumbnail Request = %d",
       
   623             iArrayTNRequestId.Count());
       
   624 
       
   625     if ( iArrayTNRequestId.Count() < KMPXMaxThumbnailRequest )
   707         {
   626         {
   708         StopWaitLoop();
   627         StopWaitLoop();
   709         }
   628         }
   710     }
   629     }
   711 
   630 
   760 // Extract album art from a file and add to thumbnail manager.
   679 // Extract album art from a file and add to thumbnail manager.
   761 // ----------------------------------------------------------------------------
   680 // ----------------------------------------------------------------------------
   762 EXPORT_C TInt CMPXMetadataExtractor::ExtractAlbumArtL( CMPXMedia* aMedia )
   681 EXPORT_C TInt CMPXMetadataExtractor::ExtractAlbumArtL( CMPXMedia* aMedia )
   763     {
   682     {
   764     MPX_FUNC("CMPXMetadataExtractor::ExtractAlbumArtL()");
   683     MPX_FUNC("CMPXMetadataExtractor::ExtractAlbumArtL()");
   765     TInt err = KErrNone; 
   684     TInt err = KErrNone;
       
   685     iCancelled = EFalse;
   766     
   686     
   767     if ( !aMedia->IsSupported(KMPXMediaGeneralUri) )
   687     if ( !aMedia->IsSupported(KMPXMediaGeneralUri) )
   768         {
   688         {
   769         return KErrArgument;
   689         return KErrArgument;
   770         }
   690         }
   776     TParsePtrC parse( path );
   696     TParsePtrC parse( path );
   777     TPtrC ext( parse.Ext() );
   697     TPtrC ext( parse.Ext() );
   778     if (ext.CompareF(KNonEmbeddedArtExt)== 0)
   698     if (ext.CompareF(KNonEmbeddedArtExt)== 0)
   779         {
   699         {
   780 		#ifdef RD_MPX_TNM_INTEGRATION
   700 		#ifdef RD_MPX_TNM_INTEGRATION
   781 
       
   782         //check if can send TN request, If thumbnail creation is ongoing, wait til it is done
   701         //check if can send TN request, If thumbnail creation is ongoing, wait til it is done
   783         CheckBeforeSendRequest();
   702         CheckBeforeSendRequest();
   784 
       
   785         CThumbnailObjectSource* source = CThumbnailObjectSource::NewLC(
   703         CThumbnailObjectSource* source = CThumbnailObjectSource::NewLC(
   786            path, KImageFileType  );
   704            path, KImageFileType  );
   787           
   705         TThumbnailRequestId tnId = iTNManager->CreateThumbnails( *source );
   788        
   706         iArrayTNRequestId.Append( tnId );
   789 
       
   790         iTNManager->CreateThumbnails( *source );
       
   791         
       
   792         iOutstandingThumbnailRequest++;
       
   793         CleanupStack::PopAndDestroy( source );
   707         CleanupStack::PopAndDestroy( source );
   794 
   708         #endif // RD_MPX_TNM_INTEGRATION
   795         #endif
       
   796         }
   709         }
   797     else
   710     else
   798         {
   711         {
   799 #endif
   712 #endif // ABSTRACTAUDIOALBUM_INCLUDED
   800     // create wanted fields array
   713     // create wanted fields array
   801     RArray<TMetaDataFieldId> wantedFields;
   714     RArray<TMetaDataFieldId> wantedFields;
   802     CleanupClosePushL( wantedFields );
   715     CleanupClosePushL( wantedFields );
   803     wantedFields.Append(EMetaDataJpeg);
   716     wantedFields.Append(EMetaDataJpeg);
   804     
   717     
   818         }
   731         }
   819     CleanupStack::PopAndDestroy( &wantedFields );
   732     CleanupStack::PopAndDestroy( &wantedFields );
   820     
   733     
   821     if ( !err )
   734     if ( !err )
   822         {
   735         {
   823         TRAP( err, GetMediaAlbumArtL( *aMedia, path ));
   736         //check if can send TN request, If thumbnail creation is ongoing, wait til it is done
       
   737         CheckBeforeSendRequest();
       
   738         TRAP( err, AddMediaAlbumArtL( *aMedia, path ));
   824         }
   739         }
   825 
   740 
   826     // Reset the utility
   741     // Reset the utility
   827     iMetadataUtility->ResetL();
   742     iMetadataUtility->ResetL();
   828 #ifdef ABSTRACTAUDIOALBUM_INCLUDED
   743 #ifdef ABSTRACTAUDIOALBUM_INCLUDED
   829       }
   744       }
   830 #endif
   745 #endif // ABSTRACTAUDIOALBUM_INCLUDED
   831     return err;
   746     return err;
   832     }
   747     }
   833 
   748 
   834 // ----------------------------------------------------------------------------
   749 // ----------------------------------------------------------------------------
   835 // Set album art.
   750 // Add album art to media object.
   836 // ----------------------------------------------------------------------------
   751 // ----------------------------------------------------------------------------
   837 TInt CMPXMetadataExtractor::GetMediaAlbumArtL( CMPXMedia& aMedia, 
   752 void CMPXMetadataExtractor::AddMediaAlbumArtL( CMPXMedia& aMedia, 
   838                                                const TDesC& aFile )
   753                                                const TDesC& aFile )
   839     {
   754     {
   840     MPX_FUNC("CMPXMetadataExtractor::GetMediaAlbumArtL()");
   755     MPX_FUNC("CMPXMetadataExtractor::AddMediaAlbumArtL()");
   841     TInt err = KErrNone;
   756     
   842     // get metadata container.
   757     // get metadata container.
   843     const CMetaDataFieldContainer& metaCont = iMetadataUtility->MetaDataFieldsL();
   758     const CMetaDataFieldContainer& metaCont = iMetadataUtility->MetaDataFieldsL();
   844 
       
   845     TPtrC8 data8 = metaCont.Field8( EMetaDataJpeg );
   759     TPtrC8 data8 = metaCont.Field8( EMetaDataJpeg );
   846     
   760    
   847     if ( data8.Length() )
   761     if ( data8.Length() )
   848         {
   762         {
   849         MPX_DEBUG1("CMPXMetadataExtractor::GetMediaAlbumArtL(): Album art exist.");
   763         MPX_DEBUG1("CMPXMetadataExtractor::GetMediaAlbumArtL(): Album art exist.");
   850 
       
   851 #ifdef RD_MPX_TNM_INTEGRATION
   764 #ifdef RD_MPX_TNM_INTEGRATION
   852         HBufC8* value8; 
   765         HBufC8* value8 = NULL; 
   853         TRAPD( err, value8 = data8.AllocL() );
   766         TRAPD( err, value8 = data8.AllocL() );
   854         if ( KErrNone != err )
   767         if ( KErrNone != err )
   855             {
   768             {
   856             MPX_DEBUG2("CMPXMetadataExtractor::GetMediaAlbumArtL - error jpeg = %i", err);           
   769             MPX_DEBUG2("CMPXMetadataExtractor::GetMediaAlbumArtL - error jpeg = %i", err);
   857             User::Leave( err );  
   770             return;
   858             }              
   771             }              
   859         CleanupStack::PushL( value8 );
   772         CleanupStack::PushL( value8 );
   860         AddMediaAlbumArtL( aMedia, aFile, *value8 );
   773     
       
   774         TBuf<256> mimeType;
       
   775         mimeType.Copy( KImageFileType );
       
   776         CThumbnailObjectSource* source = CThumbnailObjectSource::NewL(
       
   777                 value8, mimeType, aFile );
       
   778         TThumbnailRequestId tnId = iTNManager->CreateThumbnails( *source );
       
   779         iArrayTNRequestId.Append( tnId ); // add thumbnail id to array
   861         CleanupStack::Pop( value8 );
   780         CleanupStack::Pop( value8 );
   862 #else // RD_MPX_TNM_INTEGRATION
   781         aMedia.SetTextValueL( KMPXMediaMusicOriginalAlbumArtFileName, aFile );
       
   782 #endif // RD_MPX_TNM_INTEGRATION          
   863         aMedia.SetTextValueL( KMPXMediaMusicAlbumArtFileName, aFile );
   783         aMedia.SetTextValueL( KMPXMediaMusicAlbumArtFileName, aFile );
   864 #endif // RD_MPX_TNM_INTEGRATION          
   784         }
   865         }
   785     }
   866     else
   786 
   867         {
   787 // ----------------------------------------------------------------------------
   868         err = KErrNotFound;
   788 // Check if can send request to TNM or not.
   869         }
   789 // ----------------------------------------------------------------------------
   870     
       
   871     return err;
       
   872     }
       
   873 
       
   874 // ----------------------------------------------------------------------------
       
   875 // Add album art to media object.
       
   876 // ----------------------------------------------------------------------------
       
   877 void CMPXMetadataExtractor::AddMediaAlbumArtL( CMPXMedia& aMedia, 
       
   878                                                const TDesC& aFile,
       
   879                                                TDesC8& aValue )
       
   880     {
       
   881     MPX_FUNC("CMPXMetadataExtractor::AddMediaAlbumArtL()");
       
   882 #ifdef RD_MPX_TNM_INTEGRATION
       
   883     
       
   884     //check if can send TN request, If thumbnail creation is ongoing, wait til it is done
       
   885     CheckBeforeSendRequest();
       
   886 
       
   887     aMedia.SetTextValueL( KMPXMediaMusicAlbumArtFileName, aFile );
       
   888     
       
   889     TBuf<256> mimeType;
       
   890     mimeType.Copy( KImageFileType );
       
   891     CThumbnailObjectSource* source = CThumbnailObjectSource::NewLC(
       
   892         &aValue, mimeType, aFile );
       
   893     iTNManager->CreateThumbnails( *source );
       
   894     CleanupStack::PopAndDestroy( source );
       
   895     aMedia.SetTextValueL( KMPXMediaMusicOriginalAlbumArtFileName, aFile );
       
   896     iOutstandingThumbnailRequest++;
       
   897     
       
   898 #endif // RD_MPX_TNM_INTEGRATION          
       
   899     }
       
   900 
       
   901 void CMPXMetadataExtractor::CheckBeforeSendRequest()
   790 void CMPXMetadataExtractor::CheckBeforeSendRequest()
   902      {
   791      {
   903      MPX_FUNC("CMPXMetadataExtractor::CheckBeforeSendRequest()");
   792      MPX_FUNC("CMPXMetadataExtractor::CheckBeforeSendRequest()");
   904 #ifdef RD_MPX_TNM_INTEGRATION
   793 #ifdef RD_MPX_TNM_INTEGRATION
   905     MPX_DEBUG2("CMPXMetadataExtractor::CheckBeforeSendRequest(): iOutstandingThumbnailRequest %d",
   794     MPX_DEBUG2("CMPXMetadataExtractor::CheckBeforeSendRequest(): Outstanding Thumbnail Request = %d",
   906         iOutstandingThumbnailRequest);
   795             iArrayTNRequestId.Count());
   907 	// If thumbnail creation is ongoing, wait til it is done
   796 	// If thumbnail creation is ongoing, wait til it is done
   908     if ( iOutstandingThumbnailRequest >= KMPXMaxThumbnailRequest )
   797     if ( iArrayTNRequestId.Count() >= KMPXMaxThumbnailRequest )
   909         {
   798         {
   910         MPX_DEBUG1("CMPXMetadataExtractor::CheckBeforeSendRequest(): Thumbnail creation ongoing!");
   799         MPX_DEBUG1("CMPXMetadataExtractor::CheckBeforeSendRequest(): Thumbnail creation ongoing!");
   911         iTNMBlockCount++;
       
   912         // Cancel timer.
   800         // Cancel timer.
   913         CancelTimeoutTimer();
   801         CancelTimeoutTimer();
   914         // Start timer in case there is no callback from ThumbNail Manager.
   802         // Start timer in case there is no callback from ThumbNail Manager.
   915         iTimer->Start(
   803         iTimer->Start(
   916             KMPXTimeoutTimer,
   804             KMPXTimeoutTimer,
   923             iTNSyncWait->Start();
   811             iTNSyncWait->Start();
   924             }
   812             }
   925         }
   813         }
   926 #endif // RD_MPX_TNM_INTEGRATION
   814 #endif // RD_MPX_TNM_INTEGRATION
   927      }
   815      }
       
   816 
       
   817 // ----------------------------------------------------------------------------
       
   818 // Cancel request. This will empty the task array and stop the wait loop. This
       
   819 //                 will cause the CreateMediaL() to finish more quickly.
       
   820 // ----------------------------------------------------------------------------
       
   821 EXPORT_C void CMPXMetadataExtractor::CancelRequest()
       
   822     {
       
   823     MPX_FUNC("CMPXMetadataExtractor::CancelRequest()");
       
   824     iCancelled = ETrue;
       
   825     // Cancel all tasks
       
   826     iArrayTasks.Reset();
       
   827     // Cancel all thumbnail request
       
   828     CancelAllThumbnailRequests();
       
   829     StopWaitLoop();
       
   830     }
       
   831 
       
   832 // ----------------------------------------------------------------------------
       
   833 // Cancel all outstanding thumbnail requests.
       
   834 // ----------------------------------------------------------------------------
       
   835 void CMPXMetadataExtractor::CancelAllThumbnailRequests()
       
   836     {
       
   837     MPX_FUNC("CMPXMetadataExtractor::CancelAllThumbnailRequests()");
       
   838 #ifdef RD_MPX_TNM_INTEGRATION
       
   839     // TODO: remove comments when TNM make CancelRequest asynchronous.
       
   840     /*TInt count = iArrayTNRequestId.Count();
       
   841     for ( TInt i=0; i<count; i++ )
       
   842         {
       
   843         iTNManager->CancelRequest( iArrayTNRequestId[i] );
       
   844         }
       
   845     */
       
   846     iArrayTNRequestId.Reset();
       
   847 #endif // RD_MPX_TNM_INTEGRATION
       
   848     }
       
   849 
       
   850 // ----------------------------------------------------------------------------
       
   851 // Create media and set default data and mimetype.
       
   852 // ----------------------------------------------------------------------------
       
   853 void CMPXMetadataExtractor::DoCreateMediaL()
       
   854     {
       
   855     MPX_FUNC("CMPXMetadataExtractor::DoCreateMediaL()");
       
   856     RArray<TInt> contentIDs;
       
   857     contentIDs.AppendL( KMPXMediaIdGeneral );
       
   858     contentIDs.AppendL( KMPXMediaIdAudio );
       
   859     contentIDs.AppendL( KMPXMediaIdMusic );
       
   860     contentIDs.AppendL( KMPXMediaIdDrm );
       
   861     contentIDs.AppendL( KMPXMediaIdMTP );
       
   862     iMedia = CMPXMedia::NewL( contentIDs.Array() );
       
   863     contentIDs.Close();
       
   864 
       
   865     // CMPXMedia default types
       
   866     iMedia->SetTObjectValueL<TMPXGeneralType>( KMPXMediaGeneralType,
       
   867                                               EMPXItem );
       
   868     iMedia->SetTObjectValueL<TMPXGeneralCategory>( KMPXMediaGeneralCategory,
       
   869                                                   EMPXSong );
       
   870 
       
   871     TParsePtrC parse( iFileName );
       
   872     // Title, default is file name
       
   873     iMedia->SetTextValueL( KMPXMediaGeneralTitle,
       
   874                           parse.Name() );
       
   875     // Default album track
       
   876     iMedia->SetTextValueL( KMPXMediaMusicAlbumTrack,
       
   877                           KNullDesC );
       
   878 
       
   879     // Set the Mime Type and collection UID
       
   880     //
       
   881     if( !iMetadataOnly )
       
   882         {
       
   883         TInt index(KErrNotFound);
       
   884         TInt count( iSupportedTypes.Count() );
       
   885         for (TInt i=0; i <count; ++i)
       
   886             {
       
   887             TInt index2(KErrNotFound);
       
   888             const CDesCArray& exts = iSupportedTypes[i]->Extensions();
       
   889             const TDesC& ext = parse.Ext();
       
   890             if (!exts.FindIsq(ext, index2))
       
   891                 { // found
       
   892                 index = i;
       
   893                 break;
       
   894                 }
       
   895             }
       
   896         if( KErrNotFound != index )
       
   897             {
       
   898             MPX_DEBUG1("CMPXMetadataExtractor::DoCreateMediaL apparc <---" );
       
   899             TInt mimeIndex = SupportedContainerTypeL( iFileName, index );
       
   900             User::LeaveIfError( mimeIndex );
       
   901             MPX_DEBUG1("CMPXMetadataExtractor::DoCreateMediaL apparc --->" );
       
   902 
       
   903             iMedia->SetTextValueL( KMPXMediaGeneralMimeType,
       
   904                                   iSupportedTypes[index]->Mimetypes()[mimeIndex] );
       
   905 
       
   906             iMedia->SetTObjectValueL( KMPXMediaGeneralCollectionId,
       
   907                                      iSupportedTypes[index]->Uid() );
       
   908             }
       
   909         else
       
   910             {
       
   911             User::Leave(KErrNotSupported);
       
   912             }
       
   913         }
       
   914     else // other case use apparc to fetch and set mimetype
       
   915         {
       
   916         TDataType dataType;
       
   917         TUid dummyUid(KNullUid);
       
   918         iAppArc.AppForDocument(iFileName, dummyUid, dataType);
       
   919         iMedia->SetTextValueL( KMPXMediaGeneralMimeType,dataType.Des() );
       
   920         }
       
   921         
       
   922     // Initially set default tags.
       
   923     SetDefaultL( *iMedia );
       
   924     }
       
   925 
       
   926 // ----------------------------------------------------------------------------
       
   927 // Execute task at index 0.
       
   928 // ----------------------------------------------------------------------------
       
   929 void CMPXMetadataExtractor::ExecuteTaskL()
       
   930     {
       
   931     MPX_FUNC("CMPXMetadataExtractor::ExecuteTasksL()");
       
   932 
       
   933     if ( iArrayTasks.Count() )
       
   934         {
       
   935         switch ( iArrayTasks[0] )
       
   936             {
       
   937             case ETaskCreateMedia:
       
   938                 DoCreateMediaL();
       
   939                 break;
       
   940             case ETaskAddMetadata:
       
   941                 SetMediaPropertiesL();
       
   942                 break;
       
   943             case ETaskAddExtMetadata:
       
   944                 SetExtMediaPropertiesL();
       
   945                 break;
       
   946             case ETaskAddAlbumArt:
       
   947                 AddMediaAlbumArtL( *iMedia, iFileName );
       
   948                 break;
       
   949             case ETaskCheckBeforeSend:
       
   950                 CheckBeforeSendRequest();
       
   951                 break;
       
   952             default:
       
   953                 MPX_ASSERT(0); // Should never get here
       
   954             }
       
   955         }
       
   956     }
       
   957 
       
   958 // ---------------------------------------------------------------------------
       
   959 // Constructs a media properties object : asynchronous funcion
       
   960 // ---------------------------------------------------------------------------
       
   961 //
       
   962 EXPORT_C void CMPXMetadataExtractor::CreateMediaAsyncL( const TDesC& aFile,
       
   963                                                         MMPXMetadataExtractorObserver* aObs,
       
   964                                                         TBool aMetadataOnly )
       
   965     {
       
   966     MPX_FUNC("CMPXMetadataExtractor::CreateMediaAsyncL()");
       
   967     // check if we are still processing a request.
       
   968     if ( iArrayTasks.Count() )
       
   969         {
       
   970         MPX_DEBUG1("CMPXMetadataExtractor::CreateMediaAsyncL Request ongoing. Abort!" );
       
   971         User::Leave( KErrAbort );
       
   972         }
       
   973     
       
   974     iCancelled = EFalse;
       
   975     iFileOpenError = KErrNone;
       
   976     iFileName = aFile;
       
   977     iObs = aObs;
       
   978     iMetadataOnly = aMetadataOnly;
       
   979     
       
   980     // populate the task array
       
   981     AddTasksL();
       
   982     
       
   983     // Start task timer to execute task
       
   984     if ( iArrayTasks.Count() )
       
   985         {
       
   986         if ( iTaskTimer->IsActive() )
       
   987             {
       
   988             iTaskTimer->Cancel();
       
   989             }   
       
   990         iTaskTimer->Start( 0, 0, TCallBack(TaskTimerCallback, this ));
       
   991         }
       
   992     }
       
   993 
       
   994 // ---------------------------------------------------------------------------
       
   995 // Opens the file
       
   996 // ---------------------------------------------------------------------------
       
   997 //
       
   998 TInt CMPXMetadataExtractor::OpenFile()
       
   999     {
       
  1000     MPX_FUNC("CMPXMetadataExtractor::OpenFile()");
       
  1001     
       
  1002     // Open the file
       
  1003     iFile.Close();
       
  1004     TInt error = iFile.Open( iFs, iFileName, EFileRead | EFileShareReadersOrWriters );
       
  1005     MPX_DEBUG2("CMPXMetadataExtractor::OpenFile open error = %d", error );
       
  1006     return error;
       
  1007     }
       
  1008 
       
  1009 // ---------------------------------------------------------------------------
       
  1010 // Populat task array
       
  1011 // ---------------------------------------------------------------------------
       
  1012 //
       
  1013 void CMPXMetadataExtractor::AddTasksL()
       
  1014     {
       
  1015     MPX_FUNC("CMPXMetadataExtractor::AddTasks()");
       
  1016     iFileOpenError = OpenFile();
       
  1017     
       
  1018     // Do not change the order of the task below.
       
  1019     iArrayTasks.Reset();
       
  1020     if ( iFileOpenError == KErrNone )
       
  1021         {
       
  1022         iArrayTasks.AppendL(ETaskCreateMedia);
       
  1023         iArrayTasks.AppendL(ETaskAddMetadata);
       
  1024         iArrayTasks.AppendL(ETaskCheckBeforeSend);
       
  1025         iArrayTasks.AppendL(ETaskAddAlbumArt);
       
  1026         iArrayTasks.AppendL(ETaskAddExtMetadata);
       
  1027         }
       
  1028     else
       
  1029         {
       
  1030         iArrayTasks.AppendL(ETaskCreateMedia);
       
  1031         iArrayTasks.AppendL(ETaskAddExtMetadata);
       
  1032         }
       
  1033     }
       
  1034 
       
  1035 // ----------------------------------------------------------------------------
       
  1036 // Callback for timer.
       
  1037 // ----------------------------------------------------------------------------
       
  1038 TInt CMPXMetadataExtractor::TaskTimerCallback(TAny* aPtr)
       
  1039     {
       
  1040     MPX_FUNC("CMPXMetadataExtractor::TaskTimerCallback()");
       
  1041 
       
  1042     CMPXMetadataExtractor* ptr =
       
  1043         static_cast<CMPXMetadataExtractor*>(aPtr);
       
  1044 
       
  1045     ptr->HandleTaskTimerExpired();
       
  1046     return KErrNone;
       
  1047     }
       
  1048 
       
  1049 // ----------------------------------------------------------------------------
       
  1050 // Handle task timer expired
       
  1051 // ----------------------------------------------------------------------------
       
  1052 void CMPXMetadataExtractor::HandleTaskTimerExpired()
       
  1053     {
       
  1054     MPX_FUNC("CMPXMetadataExtractor::HandleTaskTimerExpired()");
       
  1055     
       
  1056     iTaskTimer->Cancel();
       
  1057     // execute task at index 0
       
  1058     TRAPD( error, ExecuteTaskL() );
       
  1059     if ( error || iCancelled )
       
  1060         {
       
  1061         // cleanup
       
  1062         if ( iMedia != NULL )
       
  1063             {
       
  1064             delete iMedia;
       
  1065             iMedia = NULL;
       
  1066             }
       
  1067         iArrayTasks.Reset();
       
  1068         }
       
  1069     
       
  1070     // Remove task at index 0.
       
  1071     if ( iArrayTasks.Count() )
       
  1072         {
       
  1073         iArrayTasks.Remove( 0 );
       
  1074         }
       
  1075     
       
  1076     // check if we have any more task to run
       
  1077     if ( iArrayTasks.Count() )
       
  1078         {
       
  1079         // start task timer
       
  1080         iTaskTimer->Start( 0, 0, TCallBack(TaskTimerCallback, this ));
       
  1081         }
       
  1082     else
       
  1083         {
       
  1084         // done
       
  1085         if ( iObs && !iCancelled )
       
  1086             {
       
  1087             iObs->HandleCreateMediaComplete( iMedia, error );
       
  1088             }
       
  1089         
       
  1090         CleanUp();
       
  1091         }
       
  1092     }
       
  1093 
       
  1094 // ----------------------------------------------------------------------------
       
  1095 // Callback for timer.
       
  1096 // ----------------------------------------------------------------------------
       
  1097 void CMPXMetadataExtractor::CleanUp()
       
  1098     {
       
  1099     MPX_FUNC("CMPXMetadataExtractor::CleanUp()");
       
  1100     // Reset the utility
       
  1101     TRAP_IGNORE( iMetadataUtility->ResetL() );
       
  1102     iFile.Close();
       
  1103     }
       
  1104