mmappcomponents/mmmtpdataprovider/mmmtpdpplugins/abstractmediamtpdataprovider/src/cabstractmediamtpdataproviderenumerator.cpp
changeset 0 a2952bb97e68
child 9 bee149131e4b
child 25 d881023c13eb
equal deleted inserted replaced
-1:000000000000 0:a2952bb97e68
       
     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:  Enumerator objects
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <bautils.h>
       
    20 #include <mtp/cmtpobjectmetadata.h>
       
    21 #include <mtp/mmtpdataproviderframework.h>
       
    22 #include <mtp/mmtpobjectmgr.h>
       
    23 #include <mtp/mmtpstoragemgr.h>
       
    24 #include <mtp/mmtpreferencemgr.h>
       
    25 #include <mpxmediaarray.h>
       
    26 #include <mpxmedia.h>
       
    27 
       
    28 #include "abstractmediamtpdataproviderconst.h"
       
    29 #include "cabstractmediamtpdataproviderenumerator.h"
       
    30 #include "cabstractmediamtpdataprovider.h"
       
    31 #include "mmmtpdplogger.h"
       
    32 #include "mmmtpdputility.h"
       
    33 #include "cmmmtpdpmetadataaccesswrapper.h"
       
    34 #include "cmmmtpdpmetadatampxaccess.h"
       
    35 
       
    36 
       
    37 /** Number of objects to insert into the object manager in one go*/
       
    38 const TInt KMTPDriveGranularity = 5;
       
    39 
       
    40 _LIT( KPlaylistFilePath, "Playlists\\" );
       
    41 
       
    42 // -----------------------------------------------------------------------------
       
    43 // CAbstractMediaMtpDataProviderEnumerator::NewL
       
    44 // Two phase constructor
       
    45 // -----------------------------------------------------------------------------
       
    46 //
       
    47 CAbstractMediaMtpDataProviderEnumerator* CAbstractMediaMtpDataProviderEnumerator::NewL( MMTPDataProviderFramework& aFramework,
       
    48     CAbstractMediaMtpDataProvider& aDataProvider )
       
    49     {
       
    50     PRINT( _L( "MM MTP => CAbstractMediaMtpDataProviderEnumerator::NewL" ) );
       
    51     CAbstractMediaMtpDataProviderEnumerator* self = new ( ELeave ) CAbstractMediaMtpDataProviderEnumerator( aFramework,
       
    52         aDataProvider );
       
    53     CleanupStack::PushL( self );
       
    54     self->ConstructL();
       
    55     CleanupStack::Pop( self );
       
    56     PRINT( _L( "MM MTP <= CAbstractMediaMtpDataProviderEnumerator::NewL" ) );
       
    57     return self;
       
    58     }
       
    59 
       
    60 // -----------------------------------------------------------------------------
       
    61 // CAbstractMediaMtpDataProviderEnumerator::CAbstractMediaMtpDataProviderEnumerator
       
    62 // Standard C++ Constructor
       
    63 // -----------------------------------------------------------------------------
       
    64 //
       
    65 CAbstractMediaMtpDataProviderEnumerator::CAbstractMediaMtpDataProviderEnumerator( MMTPDataProviderFramework& aFramework,
       
    66     CAbstractMediaMtpDataProvider& aDataProvider ) :
       
    67     CActive( EPriorityLow ),
       
    68     iFramework( aFramework ),
       
    69     iObjectMgr( aFramework.ObjectMgr() ),
       
    70     iDataProviderId( aFramework.DataProviderId() ),
       
    71     iDataProvider( aDataProvider ),
       
    72     iStorages( 2 ),
       
    73     iAbstractMedias( NULL ),
       
    74     iCount( 0 ),
       
    75     iCurrentIndex( 0 )
       
    76     {
       
    77     PRINT1( _L( "MM MTP <> CAbstractMediaMtpDataProviderEnumerator::CAbstractMediaMtpDataProviderEnumerator, iDataProviderId = %d" ), iDataProviderId );
       
    78     }
       
    79 
       
    80 // -----------------------------------------------------------------------------
       
    81 // CAbstractMediaMtpDataProviderEnumerator::ConstructL
       
    82 //
       
    83 // -----------------------------------------------------------------------------
       
    84 //
       
    85 void CAbstractMediaMtpDataProviderEnumerator::ConstructL()
       
    86     {
       
    87     CActiveScheduler::Add( this );
       
    88 
       
    89 #if defined(_DEBUG) || defined(MMMTPDP_PERFLOG)
       
    90     iPerfLog = CMmMtpDpPerfLog::NewL( _L( "CAbstractMediaMtpDataProviderEnumerator" ) );
       
    91 #endif
       
    92     }
       
    93 
       
    94 // -----------------------------------------------------------------------------
       
    95 // CAbstractMediaMtpDataProviderEnumerator::~CAbstractMediaMtpDataProviderEnumerator
       
    96 // destructor
       
    97 // -----------------------------------------------------------------------------
       
    98 //
       
    99 CAbstractMediaMtpDataProviderEnumerator::~CAbstractMediaMtpDataProviderEnumerator()
       
   100     {
       
   101     PRINT( _L( "MM MTP => CAbstractMediaMtpDataProviderEnumerator::~CAbstractMediaMtpDataProviderEnumerator" ) );
       
   102 
       
   103     Cancel();
       
   104     iStorages.Close();
       
   105 
       
   106     delete iAbstractMedias;
       
   107     iAbstractMedias = NULL;
       
   108 #if defined(_DEBUG) || defined(MMMTPDP_PERFLOG)
       
   109     delete iPerfLog;
       
   110 #endif // _DEBUG
       
   111     PRINT( _L( "MM MTP <= CAbstractMediaMtpDataProviderEnumerator::~CAbstractMediaMtpDataProviderEnumerator" ) );
       
   112     }
       
   113 
       
   114 // -----------------------------------------------------------------------------
       
   115 // AbstractMediaDpMtpEnumerator::StartL
       
   116 // Kick off the enumeration on the specified storage
       
   117 // -----------------------------------------------------------------------------
       
   118 //
       
   119 void CAbstractMediaMtpDataProviderEnumerator::StartL( TUint32 aStorageId )
       
   120     {
       
   121     PRINT1( _L( "MM MTP => CAbstractMediaMtpDataProviderEnumerator::StartL aStorageId = 0x%x" ), aStorageId );
       
   122 
       
   123     MMTPStorageMgr& storageMgr( iFramework.StorageMgr() );
       
   124     if ( aStorageId == KMTPStorageAll )
       
   125         {
       
   126         // Retrieve the available logical StorageIDs
       
   127         RPointerArray<const CMTPStorageMetaData> storages;
       
   128         CleanupClosePushL( storages );      // + storages
       
   129         TMTPStorageMgrQueryParams params( KNullDesC,
       
   130             CMTPStorageMetaData::ESystemTypeDefaultFileSystem );
       
   131 
       
   132         storageMgr.GetLogicalStoragesL( params, storages );
       
   133 
       
   134         // Construct the StorageIDs list.
       
   135         for ( TInt i = 0; i < storages.Count(); i++ )
       
   136             {
       
   137             iStorages.AppendL( storages[i]->Uint(
       
   138                 CMTPStorageMetaData::EStorageId ) );
       
   139             }
       
   140         CleanupStack::PopAndDestroy( &storages );   // - storages
       
   141         }
       
   142     else if ( aStorageId != KMTPNotSpecified32 )
       
   143         {
       
   144         __ASSERT_DEBUG( storageMgr.ValidStorageId( aStorageId ), User::Invariant() );
       
   145         const CMTPStorageMetaData& storage(
       
   146              storageMgr.StorageL( aStorageId ) );
       
   147         if ( storage.Uint( CMTPStorageMetaData::EStorageSystemType ) ==
       
   148                 CMTPStorageMetaData::ESystemTypeDefaultFileSystem )
       
   149             {
       
   150             if ( storageMgr.LogicalStorageId( aStorageId ) != KMTPNotSpecified32 )
       
   151                 {
       
   152                 // Logical StorageID.
       
   153                 iStorages.AppendL( aStorageId );
       
   154                 }
       
   155             else
       
   156                 {
       
   157                 // Physical StorageID. Enumerate all eligible logical storages.
       
   158                 const RArray<TUint>& logicalIds( storage.UintArray(
       
   159                     CMTPStorageMetaData::EStorageLogicalIds ) );
       
   160 
       
   161                 TInt countLogicalIds = logicalIds.Count();
       
   162 
       
   163                 for ( TInt i = 0; i < countLogicalIds; i++ )
       
   164                     {
       
   165                     iStorages.AppendL( logicalIds[i] );
       
   166                     }
       
   167                 }
       
   168             }
       
   169         }
       
   170 
       
   171     // keep in mind for notification when enumeration complete
       
   172     iStorageId = aStorageId;
       
   173 
       
   174     if ( iStorages.Count() > 0 )
       
   175         {
       
   176         ScanStorageL( iStorages[0] );
       
   177         }
       
   178     else
       
   179         {
       
   180         iStorages.Reset();
       
   181         SignalCompleteL( iDataProvider );
       
   182         }
       
   183 
       
   184     PRINT( _L( "MM MTP <= CAbstractMediaMtpDataProviderEnumerator::StartL" ) );
       
   185     }
       
   186 
       
   187 // -----------------------------------------------------------------------------
       
   188 // CAbstractMediaMtpDataProviderEnumerator::ScanStorageL
       
   189 // Find out all AbstractMedia file according to storage id
       
   190 // -----------------------------------------------------------------------------
       
   191 //
       
   192 void CAbstractMediaMtpDataProviderEnumerator::ScanStorageL( TUint32 aStorageId )
       
   193     {
       
   194     PRINT1( _L( "MM MTP => CAbstractMediaMtpDataProviderEnumerator::ScanStorageL aStorageId = 0x%x" ), aStorageId );
       
   195     const CMTPStorageMetaData& storage(
       
   196         iFramework.StorageMgr().StorageL( aStorageId ) );
       
   197 
       
   198     __ASSERT_DEBUG( ( storage.Uint( CMTPStorageMetaData::EStorageSystemType ) ==
       
   199         CMTPStorageMetaData::ESystemTypeDefaultFileSystem ), User::Invariant() );
       
   200 
       
   201     TFileName root( storage.DesC( CMTPStorageMetaData::EStorageSuid ) );
       
   202     PRINT1( _L( "MM MTP <> CAbstractMediaMtpDataProviderEnumerator::ScanStorageL StorageSuid = %S" ), &root );
       
   203 
       
   204     // created by windows media player, or else return responsecode is Access denied
       
   205     // Create abstract media directory if it does not exist
       
   206     HBufC* tempBuf = HBufC::NewLC( KMaxFileName );  // + tempBuf
       
   207     TPtr folder = tempBuf->Des();
       
   208     folder.Zero();
       
   209     folder.Append( root );
       
   210     folder.Append( KPlaylistFilePath );
       
   211     TBool ret = BaflUtils::FileExists( iFramework.Fs(), folder );
       
   212     PRINT2( _L( "MM MTP <> CAbstractMediaMtpDataProviderEnumerator::ScanStorageL ret = %d, folder = %S" ), ret, &folder );
       
   213     if( !ret )
       
   214         {
       
   215         TInt err = iFramework.Fs().MkDirAll( folder );
       
   216         PRINT2( _L( "MM MTP <> CAbstractMediaMtpDataProviderEnumerator::ScanStorageL Creating folder (%S) returned error %d" ), tempBuf, err );
       
   217 
       
   218         // add this new folder to framework metadata DB
       
   219         CMTPObjectMetaData* object = CMTPObjectMetaData::NewLC( 0, // Dev Dp hard code
       
   220                 EMTPFormatCodeAssociation,
       
   221                 aStorageId,
       
   222                 folder );  // + object
       
   223         object->SetUint( CMTPObjectMetaData::EParentHandle, KMTPHandleNoParent );
       
   224         object->SetUint( CMTPObjectMetaData::EFormatSubCode, EMTPAssociationTypeGenericFolder );
       
   225         PERFLOGSTART( KObjectManagerInsert );
       
   226         iObjectMgr.InsertObjectL( *object );
       
   227         PERFLOGSTOP( KObjectManagerInsert );
       
   228         CleanupStack::PopAndDestroy( object ); // - object
       
   229         }
       
   230     CleanupStack::PopAndDestroy( tempBuf ); // - tempBuf
       
   231 
       
   232     // find all abstract medias stored in MPX
       
   233     delete iAbstractMedias;
       
   234     iAbstractMedias = NULL;
       
   235     PERFLOGSTART( KMpxGetAllPlaylist );
       
   236     TRAPD( err, iDataProvider.GetWrapperL().GetAllPlaylistL( root, &iAbstractMedias ) );
       
   237     PERFLOGSTOP( KMpxGetAllPlaylist );
       
   238 
       
   239     if ( iAbstractMedias != NULL && err == KErrNone )
       
   240         {
       
   241         iCount = iAbstractMedias->Count();
       
   242         iCurrentIndex = 0;
       
   243 
       
   244         TRequestStatus* status = &iStatus;
       
   245         User::RequestComplete( status, iStatus.Int() );
       
   246         SetActive();
       
   247         }
       
   248     else
       
   249         {
       
   250         iCount = 0;
       
   251         iCurrentIndex = 0;
       
   252         ScanNextL();
       
   253         }
       
   254 
       
   255     PRINT( _L( "MM MTP <= CAbstractMediaMtpDataProviderEnumerator::ScanStorageL" ) );
       
   256     }
       
   257 
       
   258 // -----------------------------------------------------------------------------
       
   259 // CAbstractMediaMtpDataProviderEnumerator::ScanNextStorageL
       
   260 //
       
   261 // -----------------------------------------------------------------------------
       
   262 //
       
   263 void CAbstractMediaMtpDataProviderEnumerator::ScanNextL()
       
   264     {
       
   265     PRINT1( _L( "MM MTP = > CAbstractMediaMtpDataProviderEnumerator::ScanNextStorageL iStorages.Count = %d" ), iStorages.Count() );
       
   266     if ( iCurrentIndex < iCount )
       
   267         {
       
   268         TRequestStatus* status = &iStatus;
       
   269         User::RequestComplete( status, iStatus.Int() );
       
   270         SetActive();
       
   271 
       
   272         PRINT2( _L( "MM MTP <> Current storage is still being scanned, current index = %d, total AbstractMedia count = %d" ),
       
   273             iCurrentIndex,
       
   274             iCount );
       
   275         }
       
   276     // If there are one or more unscanned storages left
       
   277     // (the currently scanned one is still on the list)
       
   278     else if ( iStorages.Count() > 1 )
       
   279         {
       
   280         iStorages.Remove( 0 );
       
   281         ScanStorageL( iStorages[0] );
       
   282         }
       
   283     else
       
   284         {
       
   285         // We are done
       
   286         PRINT( _L( "MM MTP <> Objects enumeration completed 2" ) );
       
   287         iStorages.Reset();
       
   288         SignalCompleteL( iDataProvider );
       
   289         }
       
   290     }
       
   291 
       
   292 // -----------------------------------------------------------------------------
       
   293 // CAbstractMediaMtpDataProviderEnumerator::RunL
       
   294 // NOTE: preserve for performance improvement
       
   295 // -----------------------------------------------------------------------------
       
   296 //
       
   297 void CAbstractMediaMtpDataProviderEnumerator::RunL()
       
   298     {
       
   299     TBuf<KMaxFileName> playlist;
       
   300 
       
   301     // insert all playlists into handle db of framework
       
   302     CMPXMedia* media = ( *iAbstractMedias )[iCurrentIndex];
       
   303     PERFLOGSTART( KMpxGetPlaylistName );
       
   304     iDataProvider.GetWrapperL().GetPlaylistNameL( media, playlist );
       
   305     PERFLOGSTOP( KMpxGetPlaylistName );
       
   306     AddEntryL( playlist );
       
   307 
       
   308     // find all reference of each playlist and create dummy files for each playlist
       
   309     CDesCArray* references = new ( ELeave ) CDesCArrayFlat( KMTPDriveGranularity );
       
   310     CleanupStack::PushL( references ); // + references
       
   311 
       
   312     PERFLOGSTART( KMpxQueryPlaylistReference );
       
   313     iDataProvider.GetWrapperL().GetAllReferenceL( media, *references );
       
   314     PERFLOGSTOP( KMpxQueryPlaylistReference );
       
   315 
       
   316     // insert references into reference db
       
   317     AddReferencesL( playlist, *references );
       
   318 
       
   319     CleanupStack::PopAndDestroy( references ); // - references
       
   320     iCurrentIndex++;
       
   321 
       
   322     ScanNextL();
       
   323     }
       
   324 
       
   325 // -----------------------------------------------------------------------------
       
   326 // CAbstractMediaMtpDataProviderEnumerator::RunError
       
   327 // NOTE: preserve for performance improvement
       
   328 // -----------------------------------------------------------------------------
       
   329 //
       
   330 TInt CAbstractMediaMtpDataProviderEnumerator::RunError( TInt aError )
       
   331     {
       
   332     if ( aError != KErrNone )
       
   333         PRINT1( _L( "MM MTP <> CAbstractMediaMtpDataProviderEnumerator::RunError with error %d" ), aError );
       
   334 
       
   335     return KErrNone;
       
   336     }
       
   337 
       
   338 // -----------------------------------------------------------------------------
       
   339 // CAbstractMediaMtpDataProviderEnumerator::DoCancel()
       
   340 // Cancel the enumeration process
       
   341 // NOTE: preserve for performance improvement
       
   342 // -----------------------------------------------------------------------------
       
   343 //
       
   344 void CAbstractMediaMtpDataProviderEnumerator::DoCancel()
       
   345     {
       
   346 
       
   347     }
       
   348 
       
   349 // -----------------------------------------------------------------------------
       
   350 // CAbstractMediaMtpDataProviderEnumerator::SignalCompleteL
       
   351 // Called when the enumeration is completed
       
   352 // -----------------------------------------------------------------------------
       
   353 //
       
   354 void CAbstractMediaMtpDataProviderEnumerator::SignalCompleteL( MMTPEnumerationCallback& aCallback,
       
   355     TInt aError )
       
   356     {
       
   357     PRINT( _L( "MM MTP <> CAbstractMediaMtpDataProviderEnumerator::SignalCompleteL" ) );
       
   358     // Enumeration completed on this drive
       
   359     aCallback.NotifyEnumerationCompleteL( iStorageId, aError );
       
   360     }
       
   361 
       
   362 // -----------------------------------------------------------------------------
       
   363 // CAbstractMediaMtpDataProviderEnumerator::AddEntryL
       
   364 // Add a file entry to the object store
       
   365 // -----------------------------------------------------------------------------
       
   366 //
       
   367 void CAbstractMediaMtpDataProviderEnumerator::AddEntryL( const TDesC& aSuid )
       
   368     {
       
   369     PRINT1( _L("MM MTP => CAbstractMediaMtpDataProviderEnumerator::AddEntryL AbstractMedia name = %S"), &aSuid );
       
   370 
       
   371     TMTPFormatCode format = MmMtpDpUtility::FormatFromFilename( aSuid );
       
   372     CMTPObjectMetaData* object = CMTPObjectMetaData::NewLC( iDataProviderId,
       
   373         format,
       
   374         iStorages[0],
       
   375         aSuid );  // + object
       
   376 
       
   377     TParsePtrC parser( aSuid );
       
   378     PERFLOGSTART( KObjectManagerObjectUid );
       
   379     TUint32 parentHandle = iFramework.ObjectMgr().HandleL( parser.DriveAndPath() );
       
   380     PERFLOGSTOP( KObjectManagerObjectUid );
       
   381     object->SetUint( CMTPObjectMetaData::EParentHandle, parentHandle );
       
   382 
       
   383     PERFLOGSTART(KObjectManagerInsert);
       
   384     iObjectMgr.InsertObjectL( *object );
       
   385     PERFLOGSTOP(KObjectManagerInsert);
       
   386 
       
   387     CleanupStack::PopAndDestroy( object );// - object
       
   388 
       
   389     iDataProvider.GetWrapperL().CreateDummyFile( aSuid );
       
   390 
       
   391     // remember the abstract media file for clean up
       
   392     iDataProvider.GetWrapperL().AddDummyFileL( aSuid );
       
   393 
       
   394     PRINT( _L( "MM MTP <= CAbstractMediaMtpDataProviderEnumerator::AddEntryL" ) );
       
   395     }
       
   396 
       
   397 // -----------------------------------------------------------------------------
       
   398 // CAbstractMediaMtpDataProviderEnumerator::AddReferencesL
       
   399 // Add references into reference db according to abstract media name
       
   400 // -----------------------------------------------------------------------------
       
   401 //
       
   402 void CAbstractMediaMtpDataProviderEnumerator::AddReferencesL( const TDesC& aAbstractMediaName,
       
   403     CDesCArray& aReferences )
       
   404     {
       
   405     TInt count = aReferences.Count();
       
   406     PRINT2( _L("MM MTP => CAbstractMediaMtpDataProviderEnumerator::AddReferencesL AbstractMedia name = %S, ref count = %d"), &aAbstractMediaName, count );
       
   407 
       
   408     // check if references are valid
       
   409     CMTPObjectMetaData* object = CMTPObjectMetaData::NewLC();   // + object
       
   410     MMTPObjectMgr& objectMgr = iFramework.ObjectMgr();
       
   411 
       
   412     TInt removeCount = 0;
       
   413     for ( TInt i = 0; i < count; i++ )
       
   414         {
       
   415         TInt index = i - removeCount;
       
   416         PRINT2( _L( "MM MTP <> CAbstractMediaMtpDataProviderEnumerator::AddReferencesL ref[%d]'s name = %S" ), index, &( aReferences[index] ) );
       
   417         PERFLOGSTART( KObjectManagerHandle );
       
   418         TUint32 handle = iFramework.ObjectMgr().HandleL( aReferences[index] );
       
   419         PERFLOGSTOP( KObjectManagerHandle );
       
   420         if ( handle == KMTPHandleNone ) // object doesn't exist
       
   421             {
       
   422             TPtrC temp( aReferences[index] );
       
   423             PRINT1( _L( "MM MTP <> CAbstractMediaMtpDataProviderEnumerator::AddReferencesL, [%S] doesn't existed in handle db, remove this from reference array" ), &temp );
       
   424 
       
   425             // if handle is invalid, remove from reference array
       
   426             aReferences.Delete( index, 1 );
       
   427             removeCount++;
       
   428             }
       
   429         }
       
   430     CleanupStack::PopAndDestroy( object );  // - object
       
   431 
       
   432     // add all references into references db
       
   433     MMTPReferenceMgr& referenceMgr = iFramework.ReferenceMgr();
       
   434     PERFLOGSTART( KReferenceManagerSetReference );
       
   435     referenceMgr.SetReferencesL( aAbstractMediaName, aReferences );
       
   436     PERFLOGSTOP( KReferenceManagerSetReference );
       
   437 
       
   438     PRINT( _L( "MM MTP <= CAbstractMediaMtpDataProviderEnumerator::AddReferencesL" ) );
       
   439     }
       
   440 
       
   441 //end of file