omads/omadsextensions/adapters/mediads/src/mediamanager.cpp
branchRCL_3
changeset 24 8e7494275d3a
equal deleted inserted replaced
23:2bb96f4ecad8 24:8e7494275d3a
       
     1 /*
       
     2 * Copyright (c) 2009-2010 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:  CMediaManager class implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include "mediamanager.h"
       
    21 
       
    22 #include <mpxmedia.h>
       
    23 #include <mpxmediageneraldefs.h>
       
    24 #include <mpxcollectionhelper.h>
       
    25 #include <mpxmediacontainerdefs.h>
       
    26 #include <mpxmediaarray.h>
       
    27 #include <mpxcollectionhelperfactory.h>
       
    28 #include <mpxcollectionhelper.h>
       
    29 #include <utf.h>
       
    30 #include <mpxplaylistenginedefs.h> // for EMPXPlaylistTypeM3U
       
    31 #include <mpxcollectiontype.h>
       
    32 #include <mpxmediamusicdefs.h>
       
    33 #include <mpxcollectionplugin.hrh>
       
    34 #include <PathInfo.h>
       
    35 #include <mpxmediaaudiodefs.h>
       
    36 #include <mpxmediamtpdefs.h>
       
    37 #include <driveinfo.h>
       
    38 
       
    39 #include "mediadsproviderdefs.h"
       
    40 #include "playlistitem.h"
       
    41 #include "logger.h"
       
    42 
       
    43 
       
    44 const TUid KMpxLocalCollectionUid = { 0x101FFC3A };
       
    45 
       
    46 _LIT( KPlaylistPath, "playlists\\" );
       
    47 _LIT( KMPXVirtualPlaylistExt, ".vir" );
       
    48 
       
    49 // Maximum amount of song items to be enumerated on one RunL.
       
    50 const TInt iMaxSongItemsToEnumerate(100);
       
    51 // Maximum amount of playlist items to be enumerated on one RunL.
       
    52 const TInt iMaxPlaylistItemsToEnumerate(15);
       
    53 
       
    54 
       
    55 CMediaManager::CMediaManager( RFs& aFs, MMediaManagerObserver* aObserver,
       
    56         TKeyArrayFix aKey, CMD5& aHasher  )
       
    57 : CActive( EPriorityStandard ),
       
    58 iObserver( aObserver ),
       
    59 iKey( aKey ),
       
    60 iHasher( aHasher ),
       
    61 iFs( aFs )
       
    62     {
       
    63     TRACE_FUNC;
       
    64     CActiveScheduler::Add(this);
       
    65     }
       
    66 
       
    67 
       
    68 void CMediaManager::ConstructL()
       
    69     {
       
    70     TRACE_FUNC_ENTRY;
       
    71     iHarvester = CMPXHarvesterFactory::NewL();
       
    72     LOGGER_WRITE("1");
       
    73     iCollection = MMPXCollectionUtility::NewL( iObserver );
       
    74     LOGGER_WRITE("2");
       
    75     iMediator = CMPXCollectionMediator::NewL( iCollection->Collection() );
       
    76     LOGGER_WRITE("3");
       
    77     iCollectionHelper = CMPXCollectionHelperFactory::NewCollectionCachedHelperL();   
       
    78     
       
    79     TRACE_FUNC_EXIT;
       
    80     }
       
    81 
       
    82 
       
    83 CMediaManager* CMediaManager::NewL( RFs& aFs, MMediaManagerObserver* aObserver, TKeyArrayFix aKey, CMD5& aHasher )
       
    84     {
       
    85     CMediaManager* self = new( ELeave ) CMediaManager( aFs, aObserver, aKey, aHasher );
       
    86     CleanupStack::PushL( self );
       
    87     self->ConstructL();
       
    88     CleanupStack::Pop( self );
       
    89     return self;
       
    90     }
       
    91 
       
    92 
       
    93 // ---------------------------------------------------------------------------
       
    94 // Destructor
       
    95 // ---------------------------------------------------------------------------
       
    96 //
       
    97 CMediaManager::~CMediaManager()
       
    98     {
       
    99     TRACE_FUNC_ENTRY;
       
   100     Cancel();
       
   101     delete iFoundedMedia;
       
   102     if ( iCollectionHelper )
       
   103         {
       
   104         iCollectionHelper->Close();
       
   105         iCollectionHelper = NULL;
       
   106         }
       
   107     
       
   108     delete iMediator;
       
   109     
       
   110     if ( iCollection )
       
   111         {
       
   112         iCollection->Close();
       
   113         iCollection = NULL;
       
   114         }
       
   115     
       
   116     if ( iHarvester )
       
   117         {
       
   118         // For some unknown reason this may sometimes leave with error code -38
       
   119         TRAP_IGNORE( iHarvester->Close() );
       
   120         iHarvester = NULL;
       
   121         }
       
   122     
       
   123     TRACE_FUNC_EXIT;
       
   124     }
       
   125 
       
   126 void CMediaManager::ScanL()
       
   127     {
       
   128     TRACE_FUNC_ENTRY;
       
   129     iHarvester->ScanL();
       
   130     TRACE_FUNC_EXIT;
       
   131     }
       
   132 
       
   133 void CMediaManager::CancelScanL()
       
   134     {
       
   135     TRACE_FUNC_ENTRY;
       
   136     iHarvester->CancelScanL();
       
   137     TRACE_FUNC_EXIT;
       
   138     }
       
   139 
       
   140 // --------------------------------------------------------------------------
       
   141 // Starts registering all playlists. Calls RegisterAllPlayListsCompleted when ready
       
   142 // --------------------------------------------------------------------------
       
   143 // 
       
   144 TInt CMediaManager::RegisterAllPlayLists()
       
   145     {
       
   146     TRACE_FUNC_ENTRY;
       
   147     if ( iCurrentState != EIdle || !iSnapshotArray)
       
   148         {
       
   149         LOGGER_WRITE("Not Idle or iSnapshotArray is NULL");
       
   150         return KErrNotReady;
       
   151         }
       
   152     iCurrentState = EReadingPlaylistCount;
       
   153     SetActive();
       
   154     TRequestStatus* status = &iStatus;
       
   155     User::RequestComplete( status, KErrNone );
       
   156     TRACE_FUNC_EXIT;
       
   157     return KErrNone;
       
   158     }
       
   159 
       
   160 // --------------------------------------------------------------------------
       
   161 // Starts registering all song items. Calls RegisterAllSongsCompleted when ready
       
   162 // --------------------------------------------------------------------------
       
   163 // 
       
   164 TInt CMediaManager::RegisterAllSongs()
       
   165     {
       
   166     TRACE_FUNC_ENTRY;
       
   167     if ( iCurrentState != EIdle || !iSnapshotArray)
       
   168         {
       
   169         LOGGER_WRITE("Not Idle or iSnapshotArray is NULL");
       
   170         return KErrNotReady;
       
   171         }
       
   172     iCurrentState = EReadingSongCount;
       
   173     SetActive();
       
   174     TRequestStatus* status = &iStatus;
       
   175     User::RequestComplete( status, KErrNone );
       
   176     TRACE_FUNC_EXIT;
       
   177     return KErrNone;
       
   178     }
       
   179 
       
   180 // --------------------------------------------------------------------------
       
   181 // Registers playlist.
       
   182 // --------------------------------------------------------------------------
       
   183 // 
       
   184 void CMediaManager::RegisterPlaylistL( const CMPXMedia& aMedia )
       
   185     {
       
   186     TRACE_FUNC_ENTRY;
       
   187     if ( !iSnapshotArray )
       
   188         {
       
   189         User::Leave( KErrGeneral );
       
   190         }
       
   191     
       
   192     if ( !aMedia.IsSupported(KMPXMediaGeneralId) )
       
   193         {
       
   194         LOGGER_WRITE("KMPXMediaGeneralId not supported");
       
   195         User::Leave(KErrNotSupported);
       
   196         }
       
   197      
       
   198      // Extract the playlist id from the found object
       
   199      TUint32 playlistId = *aMedia.Value<TMPXItemId>(KMPXMediaGeneralId);
       
   200      LOGGER_WRITE_1("KMPXMediaGeneralId: %d", playlistId);
       
   201      
       
   202      if ( !aMedia.IsSupported( KMPXMediaGeneralTitle ) )
       
   203           {
       
   204           LOGGER_WRITE("KMPXMediaGeneralTitle not supported");
       
   205           User::Leave(KErrNotSupported);
       
   206           }
       
   207      
       
   208      CPlaylistItem* playlist = CPlaylistItem::NewLC();
       
   209      // find the media object that contains a list of songs in the playlist
       
   210      
       
   211      RArray<TInt> contentIDs;
       
   212      CleanupClosePushL( contentIDs );
       
   213      contentIDs.AppendL(KMPXMediaIdGeneral);
       
   214      
       
   215      CMPXMedia* searchMedia = CMPXMedia::NewL(contentIDs.Array());
       
   216      CleanupStack::PopAndDestroy( &contentIDs );
       
   217      CleanupStack::PushL(searchMedia);
       
   218      
       
   219      searchMedia->SetTObjectValueL(KMPXMediaGeneralType, EMPXGroup);    
       
   220      searchMedia->SetTObjectValueL(KMPXMediaGeneralCategory, EMPXSong);
       
   221      searchMedia->SetTObjectValueL<TMPXItemId>(KMPXMediaGeneralId, playlistId);
       
   222      
       
   223      RArray<TMPXAttribute> songAttributes;
       
   224      CleanupClosePushL(songAttributes);
       
   225      songAttributes.AppendL(KMPXMediaGeneralId);
       
   226      songAttributes.AppendL(KMPXMediaGeneralUri);
       
   227      
       
   228      CMPXMedia* foundMedia = iCollectionHelper->FindAllL(*searchMedia, 
       
   229                                                          songAttributes.Array());
       
   230      CleanupStack::PopAndDestroy(&songAttributes);
       
   231      CleanupStack::PopAndDestroy(searchMedia);
       
   232      
       
   233      CleanupStack::PushL(foundMedia);
       
   234      
       
   235      
       
   236      if ( !foundMedia->IsSupported(KMPXMediaArrayCount) )
       
   237          {
       
   238          User::Leave(KErrNotSupported);
       
   239          }
       
   240      
       
   241      // Number of references
       
   242      TInt numOfRefs = *foundMedia->Value<TInt>(KMPXMediaArrayCount);
       
   243      if ( numOfRefs )
       
   244          {
       
   245          if (!foundMedia->IsSupported(KMPXMediaArrayContents))
       
   246              {
       
   247              User::Leave(KErrNotSupported);
       
   248              }
       
   249          
       
   250          const CMPXMediaArray* refArray =
       
   251              foundMedia->Value<CMPXMediaArray>(KMPXMediaArrayContents);
       
   252          
       
   253          // Import the references
       
   254          for (TInt i=0;i<numOfRefs;i++)
       
   255              {
       
   256              const CMPXMedia* refMedia = (*refArray)[i];
       
   257              
       
   258              if ( refMedia->IsSupported(KMPXMediaGeneralUri) )
       
   259                  {
       
   260                  const TDesC& uri = refMedia->ValueText(KMPXMediaGeneralUri);
       
   261                  playlist->AddItemL( uri );
       
   262                  }
       
   263              
       
   264              }
       
   265          }
       
   266     
       
   267     TSnapshotItem playlistItem(playlistId);
       
   268     
       
   269     const TDesC& title = aMedia.ValueText( KMPXMediaGeneralTitle );
       
   270     playlist->SetTitleL( title );
       
   271     
       
   272     const TDesC& uri = aMedia.ValueText( KMPXMediaGeneralUri );
       
   273     playlist->SetUrlL( uri );
       
   274     
       
   275     playlistItem.SetItemHash( *playlist, iHasher );
       
   276     
       
   277     playlistItem.SetParentId( KPlaylistsFolderId );
       
   278     TKeyArrayFix key( iKey );
       
   279     iSnapshotArray->InsertIsqL( playlistItem, key );
       
   280     
       
   281     CleanupStack::PopAndDestroy( foundMedia );
       
   282     CleanupStack::PopAndDestroy( playlist );
       
   283     
       
   284     TRACE_FUNC_EXIT;
       
   285     }
       
   286 
       
   287 // --------------------------------------------------------------------------
       
   288 // Populates CPlaylistItem
       
   289 // --------------------------------------------------------------------------
       
   290 // 
       
   291 void CMediaManager::GetPlayListL( TInt aPlaylistId, CPlaylistItem& aList )
       
   292     {
       
   293     TRACE_FUNC_ENTRY;
       
   294     
       
   295     LOGGER_WRITE_1("aPlaylistId: %d", aPlaylistId);
       
   296     
       
   297     CMPXMedia* playlistItem = FindPlaylistL( aPlaylistId );
       
   298     CleanupStack::PushL( playlistItem );
       
   299     if ( !playlistItem->IsSupported(KMPXMediaGeneralId) )
       
   300         {
       
   301         LOGGER_WRITE("KMPXMediaGeneralId not supported");
       
   302         User::Leave(KErrNotSupported);
       
   303         }
       
   304     
       
   305     // Extract the playlist id from the found object
       
   306     TUint32 playlistId = *playlistItem->Value<TMPXItemId>(KMPXMediaGeneralId);
       
   307     LOGGER_WRITE_1("KMPXMediaGeneralId: %d", playlistId);
       
   308     aList.SetId( playlistId );
       
   309     
       
   310     if ( !playlistItem->IsSupported(KMPXMediaGeneralTitle) )
       
   311         {
       
   312         LOGGER_WRITE("KMPXMediaGeneralTitle not supported");
       
   313         User::Leave( KErrNotSupported );
       
   314         }
       
   315     else
       
   316         {
       
   317         const TDesC& title = playlistItem->ValueText(KMPXMediaGeneralTitle);
       
   318         LOGGER_WRITE_1("KMPXMediaGeneralTitle: %S", &title);
       
   319         aList.SetTitleL( title );
       
   320         }
       
   321     
       
   322     if ( playlistItem->IsSupported( KMPXMediaGeneralUri ) )
       
   323         {
       
   324         const TDesC& uri = playlistItem->ValueText( KMPXMediaGeneralUri );
       
   325         aList.SetUrlL( uri );
       
   326         }
       
   327     
       
   328     CleanupStack::PopAndDestroy( playlistItem );
       
   329     
       
   330     CMPXMedia* playlist = FetchPlaylistContentL( aPlaylistId );
       
   331     CleanupStack::PushL( playlist );
       
   332     
       
   333     if ( !playlist->IsSupported(KMPXMediaArrayCount) )
       
   334          {
       
   335          User::Leave(KErrNotSupported);
       
   336          }
       
   337      
       
   338     // Number of references
       
   339     TInt numOfRefs = *playlist->Value<TInt>(KMPXMediaArrayCount);
       
   340     LOGGER_WRITE_1("numOfRefs: %d", numOfRefs);
       
   341     if ( numOfRefs )
       
   342         {
       
   343         if (!playlist->IsSupported(KMPXMediaArrayContents))
       
   344             {
       
   345             User::Leave(KErrNotSupported);
       
   346             }
       
   347         
       
   348         const CMPXMediaArray* refArray =
       
   349         playlist->Value<CMPXMediaArray>(KMPXMediaArrayContents);
       
   350         
       
   351         // Import the references
       
   352         for (TInt i=0;i<numOfRefs;i++)
       
   353             {
       
   354             const CMPXMedia* refMedia = (*refArray)[i];
       
   355             
       
   356             if ( refMedia->IsSupported(KMPXMediaGeneralUri) )
       
   357                 {
       
   358                 aList.AddItemL( refMedia->ValueText(KMPXMediaGeneralUri) );
       
   359                 }
       
   360             }
       
   361         }
       
   362  
       
   363     CleanupStack::PopAndDestroy( playlist );
       
   364     
       
   365     TRACE_FUNC_EXIT;
       
   366     }
       
   367 
       
   368 // --------------------------------------------------------------------------
       
   369 // Fetch a playlist content
       
   370 // --------------------------------------------------------------------------
       
   371 // 
       
   372 CMPXMedia* CMediaManager::FetchPlaylistContentL( TInt aId )
       
   373     {
       
   374     TRACE_FUNC_ENTRY;
       
   375     TMPXItemId id(aId);
       
   376 
       
   377     CMPXMedia* criteria = CMPXMedia::NewL();
       
   378     CleanupStack::PushL( criteria );
       
   379     criteria->SetTObjectValueL<TUid>(
       
   380             KMPXMediaGeneralCollectionId, KMpxLocalCollectionUid );
       
   381     criteria->SetTObjectValueL<TMPXGeneralType>(
       
   382             KMPXMediaGeneralType, EMPXGroup );
       
   383     
       
   384     // set search keys
       
   385     criteria->SetTObjectValueL<TMPXGeneralCategory>(
       
   386             KMPXMediaGeneralCategory, EMPXSong );    
       
   387     criteria->SetTObjectValueL<TMPXItemId>(
       
   388             KMPXMediaGeneralId, id );
       
   389     
       
   390     // define attributes fetched
       
   391     RArray<TMPXAttribute> songAttributes;
       
   392     CleanupClosePushL(songAttributes);
       
   393     songAttributes.AppendL(KMPXMediaGeneralId);
       
   394     songAttributes.AppendL(KMPXMediaGeneralUri);
       
   395     
       
   396     // now find
       
   397     CMPXMedia* result = iCollectionHelper->FindAllL( *criteria , songAttributes.Array() );
       
   398     CleanupStack::PopAndDestroy( &songAttributes );
       
   399     CleanupStack::PopAndDestroy( criteria );
       
   400     
       
   401     TRACE_FUNC_EXIT;
       
   402     return result;
       
   403     }
       
   404 
       
   405 
       
   406  // --------------------------------------------------------------------------
       
   407  // Find a playlist
       
   408  // --------------------------------------------------------------------------
       
   409  //
       
   410  CMPXMedia* CMediaManager::FindPlaylistL( TInt aUid )
       
   411      {
       
   412      TRACE_FUNC_ENTRY;
       
   413      CMPXMedia* criteria = CMPXMedia::NewL();
       
   414      CleanupStack::PushL( criteria );
       
   415      
       
   416      criteria->SetTObjectValueL<TMPXGeneralType>(
       
   417                  KMPXMediaGeneralType, EMPXItem );
       
   418      criteria->SetTObjectValueL<TMPXGeneralCategory>(
       
   419                  KMPXMediaGeneralCategory, EMPXPlaylist );
       
   420      criteria->SetTObjectValueL<TMPXItemId>(
       
   421                  KMPXMediaGeneralId, aUid );
       
   422          
       
   423      // define attributes fetched
       
   424      RArray<TMPXAttribute> playlistAttributes;
       
   425      CleanupClosePushL(playlistAttributes);
       
   426      playlistAttributes.AppendL(KMPXMediaGeneralId);
       
   427      playlistAttributes.AppendL(KMPXMediaGeneralCollectionId); //test
       
   428      playlistAttributes.AppendL(KMPXMediaGeneralTitle);
       
   429      playlistAttributes.AppendL(KMPXMediaGeneralUri);
       
   430      
       
   431      // now find
       
   432      CMPXMedia* foundList = 0;
       
   433      foundList = iCollectionHelper->FindAllL( *criteria , playlistAttributes.Array() );
       
   434      CleanupStack::PopAndDestroy( &playlistAttributes );
       
   435      CleanupStack::PopAndDestroy( criteria );
       
   436      
       
   437      CleanupStack::PushL( foundList );
       
   438      
       
   439      if ( !foundList->IsSupported(KMPXMediaArrayCount) )
       
   440          {
       
   441          LOGGER_WRITE("KMPXMediaArrayCount not supported");
       
   442          User::Leave( KErrNotSupported );
       
   443          }
       
   444      
       
   445      TInt foundItemCount = *foundList->Value<TInt>(KMPXMediaArrayCount);
       
   446      
       
   447      CMPXMedia* playlist(NULL);
       
   448      if ( foundItemCount )
       
   449          {
       
   450          if ( !foundList->IsSupported(KMPXMediaArrayContents) )
       
   451              {
       
   452              LOGGER_WRITE("KMPXMediaArrayContents not supported");
       
   453              User::Leave(KErrNotSupported);
       
   454              }
       
   455              
       
   456          const CMPXMediaArray* foundArray = foundList->Value<CMPXMediaArray>(KMPXMediaArrayContents);
       
   457          
       
   458          // get the first founded item
       
   459          playlist = CMPXMedia::NewL(*(*foundArray)[0]);
       
   460          }
       
   461      else
       
   462          {
       
   463          User::Leave( KErrGeneral );
       
   464          }
       
   465      
       
   466      CleanupStack::PopAndDestroy( foundList );
       
   467      
       
   468      TRACE_FUNC_EXIT;
       
   469      return playlist;
       
   470      }
       
   471 
       
   472 void CMediaManager::GetSongL( TInt aSongId, CSongItem& aSongItem )
       
   473     {
       
   474     TRACE_FUNC_ENTRY;
       
   475     TMPXItemId id(aSongId);
       
   476 
       
   477     RArray<TInt> contentIDs;
       
   478     CleanupClosePushL( contentIDs );
       
   479     contentIDs.AppendL( KMPXMediaIdMusic );
       
   480     contentIDs.AppendL( KMPXMediaIdGeneral );
       
   481     
       
   482     CMPXMedia* criteria = CMPXMedia::NewL(contentIDs.Array());
       
   483     CleanupStack::PopAndDestroy( &contentIDs );
       
   484     CleanupStack::PushL(criteria);
       
   485     
       
   486     // set search keys
       
   487     criteria->SetTObjectValueL<TMPXGeneralType>(
       
   488             KMPXMediaGeneralType, EMPXItem );
       
   489     criteria->SetTObjectValueL<TMPXGeneralCategory>(
       
   490             KMPXMediaGeneralCategory, EMPXSong );   
       
   491     criteria->SetTObjectValueL<TMPXItemId>(
       
   492             KMPXMediaGeneralId, id );
       
   493     
       
   494     // define attributes fetched
       
   495     RArray<TMPXAttribute> songAttributes;
       
   496     CleanupClosePushL(songAttributes);
       
   497     
       
   498     songAttributes.AppendL( KMPXMediaGeneralType );
       
   499     songAttributes.AppendL( KMPXMediaGeneralCategory );
       
   500     songAttributes.AppendL( KMPXMediaGeneralUri );
       
   501     songAttributes.AppendL( KMPXMediaGeneralId );
       
   502     songAttributes.AppendL( KMPXMediaGeneralTitle );
       
   503     songAttributes.AppendL( KMPXMediaGeneralMimeType );
       
   504     songAttributes.AppendL( KMPXMediaGeneralDuration );
       
   505     songAttributes.AppendL( KMPXMediaGeneralComment );
       
   506     
       
   507     songAttributes.AppendL( KMPXMediaMusicArtist );
       
   508     songAttributes.AppendL( KMPXMediaMusicAlbum );
       
   509     songAttributes.AppendL( KMPXMediaMusicYear );
       
   510     songAttributes.AppendL( KMPXMediaMusicAlbumTrack );
       
   511     songAttributes.AppendL( KMPXMediaMusicGenre );
       
   512     songAttributes.AppendL( KMPXMediaMusicComposer );
       
   513     
       
   514     // now find
       
   515     CMPXMedia* result = 0;
       
   516     TRAPD(err, result = iCollectionHelper->FindAllL( *criteria , songAttributes.Array() ));
       
   517     CleanupStack::PopAndDestroy( &songAttributes );
       
   518     CleanupStack::PopAndDestroy( criteria );
       
   519     
       
   520     if ( err )
       
   521         {
       
   522         LOGGER_WRITE_1("FindAllL err: %d", err);
       
   523         User::Leave( err );
       
   524         }
       
   525     
       
   526     CleanupStack::PushL( result );
       
   527 
       
   528     if ( !result->IsSupported(KMPXMediaArrayCount) )
       
   529          {
       
   530          LOGGER_WRITE("KMPXMediaArrayCount not supported");
       
   531          User::Leave(KErrNotSupported);
       
   532          }
       
   533     
       
   534     if ( !result->IsSupported( KMPXMediaArrayContents ) )
       
   535         {
       
   536         User::Leave(KErrNotSupported);
       
   537         }
       
   538     
       
   539     const CMPXMediaArray* songsArray =
       
   540     result->Value<CMPXMediaArray>(KMPXMediaArrayContents);
       
   541     if ( songsArray->Count() == 0 )
       
   542         {
       
   543         User::Leave( KErrNotFound );
       
   544         }
       
   545     const CMPXMedia* songMedia = (*songsArray)[0];
       
   546     
       
   547     ReadSongAttributesL( *songMedia, aSongItem, ETrue );
       
   548     
       
   549     CleanupStack::PopAndDestroy( result );
       
   550     
       
   551     TRACE_FUNC_EXIT;
       
   552     }
       
   553 
       
   554 CMPXMedia* CMediaManager::CreatePlaylistMediaL( const CPlaylistItem& aPlaylist )
       
   555     {
       
   556     TRACE_FUNC_ENTRY;
       
   557     RArray<TInt> contentIDs;
       
   558     contentIDs.AppendL(KMPXMediaIdGeneral);
       
   559     
       
   560     CMPXMedia* playlist = CMPXMedia::NewL( contentIDs.Array() );
       
   561     CleanupStack::PushL( playlist );
       
   562     contentIDs.Close();
       
   563     // add playlist info into the playlist media
       
   564     playlist->SetTObjectValueL<TUid>(
       
   565         KMPXMediaGeneralCollectionId, KMpxLocalCollectionUid );
       
   566     
       
   567     // set playlist title
       
   568     playlist->SetTextValueL( KMPXMediaGeneralTitle, aPlaylist.Title() );
       
   569     
       
   570     // set type
       
   571     playlist->SetTObjectValueL( KMPXMediaGeneralType, EMPXItem );
       
   572      
       
   573     // set category
       
   574     playlist->SetTObjectValueL( KMPXMediaGeneralCategory, EMPXPlaylist ); 
       
   575     
       
   576     
       
   577     TFileName playlistfile;
       
   578     
       
   579     TInt drive;
       
   580     User::LeaveIfError( DriveInfo::GetDefaultDrive( DriveInfo::EDefaultMassStorage, drive) );
       
   581     TUint driveStatus( 0 );
       
   582     TInt err = DriveInfo::GetDriveStatus( iFs, drive, driveStatus );
       
   583     if ( !err && (driveStatus & DriveInfo::EDrivePresent) )
       
   584         {
       
   585         User::LeaveIfError( PathInfo::GetRootPath( playlistfile, drive ) );
       
   586         LOGGER_WRITE_1("mmc playlistfile: %S", &playlistfile);
       
   587         }
       
   588     else
       
   589         {
       
   590         playlistfile = PathInfo::PhoneMemoryRootPath();
       
   591         LOGGER_WRITE_1("phone playlistfile: %S", &playlistfile);
       
   592         }
       
   593     
       
   594     playlistfile.Append( KPlaylistPath );
       
   595     
       
   596     // set location drive
       
   597     TDriveUnit driveUnit( playlistfile );
       
   598     TPtrC driveName = driveUnit.Name();
       
   599     playlist->SetTextValueL( KMPXMediaGeneralDrive, driveName );
       
   600     
       
   601     
       
   602     TTime time;
       
   603     time.HomeTime();
       
   604     playlistfile.AppendNum(time.Int64());
       
   605     playlistfile.Append(KMPXVirtualPlaylistExt);
       
   606     
       
   607     LOGGER_WRITE_1("playlistfile: %S", &playlistfile);
       
   608     playlist->SetTextValueL(
       
   609              KMPXMediaGeneralUri, playlistfile );
       
   610     
       
   611     // set DbFlags to indicate that this is a virtual playlist
       
   612     playlist->SetTObjectValueL<TUint>(KMPXMediaGeneralFlags,
       
   613        KMPXMediaGeneralFlagsSetOrUnsetBit | KMPXMediaGeneralFlagsIsVirtual);
       
   614 
       
   615     // create songs
       
   616     CMPXMediaArray* medias = CreateMediaArrayLC( aPlaylist );
       
   617     // set medias
       
   618     playlist->SetCObjectValueL( KMPXMediaArrayContents, medias );
       
   619     /// set media count        
       
   620     playlist->SetTObjectValueL( KMPXMediaArrayCount, medias->Count() );
       
   621     
       
   622     CleanupStack::PopAndDestroy(medias);
       
   623     CleanupStack::Pop(playlist);
       
   624     
       
   625     
       
   626     TRACE_FUNC_EXIT;
       
   627     return playlist;
       
   628     }
       
   629 
       
   630 CMPXMediaArray* CMediaManager::CreateMediaArrayLC( const CPlaylistItem& aList )
       
   631     {
       
   632     CMPXMediaArray* medias = CMPXMediaArray::NewL();
       
   633     CleanupStack::PushL( medias );
       
   634     //
       
   635     // create entries
       
   636     //
       
   637     TInt itemCount = aList.ItemCount();
       
   638     for ( TInt i=0; i<itemCount; i++)
       
   639         {
       
   640         TPtrC16 ptrUri = aList.ItemAt(i);
       
   641         LOGGER_WRITE_1("add uri: %S", &ptrUri);
       
   642         iCollectionHelper->AddL( ptrUri, this );
       
   643         iWaitOngoing = ETrue;
       
   644         LOGGER_WRITE("iWait.Start");
       
   645         iWait.Start();
       
   646         LOGGER_WRITE("continue..");
       
   647     
       
   648         RArray<TInt> contentIDs;
       
   649         contentIDs.AppendL(KMPXMediaIdGeneral);
       
   650         contentIDs.AppendL(KMPXMediaIdAudio);
       
   651         contentIDs.AppendL(KMPXMediaIdMusic);
       
   652         contentIDs.AppendL(KMPXMediaIdMTP); 
       
   653         
       
   654         CMPXMedia* entry = CMPXMedia::NewL( contentIDs.Array() );
       
   655         CleanupStack::PushL( entry );
       
   656         contentIDs.Close();
       
   657         
       
   658         entry->SetTObjectValueL<TUid>(
       
   659                 KMPXMediaGeneralCollectionId, KMpxLocalCollectionUid );
       
   660         
       
   661         entry->SetTObjectValueL<TMPXGeneralType>(
       
   662                      KMPXMediaGeneralType, EMPXItem );
       
   663         
       
   664         entry->SetTObjectValueL<TMPXGeneralCategory>(
       
   665                      KMPXMediaGeneralCategory, EMPXSong );
       
   666         
       
   667         
       
   668         entry->SetTextValueL( KMPXMediaGeneralUri, ptrUri );
       
   669         
       
   670         // set location drive
       
   671         TDriveUnit driveUnit( ptrUri );
       
   672         TPtrC driveName = driveUnit.Name();
       
   673         entry->SetTextValueL( KMPXMediaGeneralDrive, driveName );
       
   674         
       
   675         medias->AppendL(entry);
       
   676         CleanupStack::Pop(entry);
       
   677         }
       
   678     return medias;
       
   679     }
       
   680 
       
   681 void CMediaManager::CreateNewPlaylistL( TInt& aNewUid, CPlaylistItem& aList )
       
   682     {
       
   683     TRACE_FUNC_ENTRY;
       
   684     
       
   685     CMPXMedia* playlist = CreatePlaylistMediaL( aList );
       
   686     CleanupStack::PushL( playlist );
       
   687     iMediator->AddItemL( playlist );
       
   688     
       
   689     if ( !playlist->IsSupported(KMPXMediaGeneralId) )
       
   690         {
       
   691         LOGGER_WRITE("KMPXMediaGeneralId not supported");
       
   692         User::Leave( KErrNotSupported );
       
   693         }
       
   694     else
       
   695         {
       
   696         // Extract the playlist id from the found object
       
   697         TUint32 playlistId = *playlist->Value<TMPXItemId>(KMPXMediaGeneralId);
       
   698         LOGGER_WRITE_1("KMPXMediaGeneralId: %d", playlistId);
       
   699         aNewUid = playlistId;
       
   700         
       
   701         if ( !playlist->IsSupported( KMPXMediaGeneralUri ) )
       
   702             {
       
   703             // get url
       
   704             const TDesC& uri = playlist->ValueText( KMPXMediaGeneralUri );
       
   705             aList.SetUrlL( uri );
       
   706             }
       
   707         }
       
   708     
       
   709     CleanupStack::PopAndDestroy(playlist);
       
   710     
       
   711     TRACE_FUNC_EXIT;
       
   712     }
       
   713 
       
   714 void CMediaManager::ReplacePlaylistL( TInt aPlaylistId, CPlaylistItem& aPlaylist )
       
   715     {
       
   716     TRACE_FUNC_ENTRY;
       
   717     CMPXMedia* playlist = CMPXMedia::NewL();
       
   718     CleanupStack::PushL( playlist );
       
   719     
       
   720     // add playlist info into the playlist media
       
   721     playlist->SetTObjectValueL<TUid>(
       
   722         KMPXMediaGeneralCollectionId, KMpxLocalCollectionUid );
       
   723     
       
   724     // set type
       
   725     playlist->SetTObjectValueL( KMPXMediaGeneralType, EMPXItem );
       
   726      
       
   727     // set category
       
   728     playlist->SetTObjectValueL( KMPXMediaGeneralCategory, EMPXPlaylist ); 
       
   729     
       
   730     
       
   731     
       
   732     // find playlist uri
       
   733     CMPXMedia* oldPlaylist = FindPlaylistL( aPlaylistId );
       
   734     CleanupStack::PushL( oldPlaylist );
       
   735     if ( !oldPlaylist->IsSupported(KMPXMediaGeneralUri) )
       
   736         {
       
   737         User::Leave( KErrNotSupported );
       
   738         }
       
   739     const TDesC& uri = oldPlaylist->ValueText( KMPXMediaGeneralUri );
       
   740     LOGGER_WRITE_1("uri: %S", &uri);
       
   741     
       
   742     playlist->SetTextValueL(
       
   743             KMPXMediaGeneralUri, uri );
       
   744     
       
   745 	aPlaylist.SetUrlL( uri );
       
   746     
       
   747     const TDesC& title = oldPlaylist->ValueText( KMPXMediaGeneralTitle );
       
   748     if ( title.Compare( aPlaylist.Title() ) != 0 )
       
   749         {
       
   750         LOGGER_WRITE_1("title changed old: %S", &title);
       
   751         LOGGER_WRITE_1("new title: %S", &aPlaylist.Title());
       
   752         // set playlist title
       
   753         playlist->SetTextValueL( KMPXMediaGeneralTitle, aPlaylist.Title() );
       
   754         
       
   755         // update title first
       
   756         iMediator->SetItemL( playlist );
       
   757         }
       
   758     
       
   759     
       
   760     CleanupStack::PopAndDestroy( oldPlaylist );
       
   761     
       
   762     // update songs
       
   763     CMPXMediaArray* songArray = CreateMediaArrayLC( aPlaylist );
       
   764     // set medias
       
   765     playlist->SetCObjectValueL( KMPXMediaArrayContents, songArray );
       
   766     /// set media count        
       
   767     playlist->SetTObjectValueL( KMPXMediaArrayCount, songArray->Count() );
       
   768     
       
   769     iMediator->SetItemL( playlist );
       
   770     
       
   771     CleanupStack::PopAndDestroy( songArray );
       
   772     CleanupStack::PopAndDestroy( playlist );
       
   773     
       
   774     TRACE_FUNC_EXIT;
       
   775     }
       
   776 
       
   777 
       
   778 void CMediaManager::DeletePlaylistL( TInt aPlaylistId )
       
   779     {
       
   780     TRACE_FUNC_ENTRY;
       
   781     
       
   782     CMPXMedia* playlist = FindPlaylistL( aPlaylistId );
       
   783     CleanupStack::PushL( playlist );
       
   784     iCollection->Collection().RemoveL( *playlist );
       
   785     CleanupStack::PopAndDestroy( playlist );
       
   786     
       
   787     TRACE_FUNC_EXIT;
       
   788     }
       
   789 
       
   790 void CMediaManager::HandleAddFileCompleteL( TInt aErr )
       
   791     {
       
   792     TRACE_FUNC_ENTRY;
       
   793     LOGGER_WRITE_1("aErr: %d", aErr);
       
   794     if ( iWaitOngoing )
       
   795         {
       
   796         LOGGER_WRITE("AsyncStop");
       
   797         iWaitOngoing = EFalse;
       
   798         iWait.AsyncStop();
       
   799         }
       
   800     TRACE_FUNC_EXIT;
       
   801     }
       
   802 
       
   803 void CMediaManager::ReadSongAttributesL( const CMPXMedia& aMedia, CSongItem& aSongItem,
       
   804         TBool aReadFileDetails )
       
   805     {
       
   806     if ( !aMedia.IsSupported(KMPXMediaGeneralUri) )
       
   807         {
       
   808         LOGGER_WRITE("KMPXMediaGeneralUri not supported");
       
   809         User::Leave(KErrNotSupported);
       
   810         }    
       
   811     
       
   812     if ( !aMedia.IsSupported(KMPXMediaGeneralId) )
       
   813         {
       
   814         LOGGER_WRITE("KMPXMediaGeneralId not supported");
       
   815         User::Leave(KErrNotSupported);
       
   816         }
       
   817     
       
   818     aSongItem.SetId( *aMedia.Value<TMPXItemId>(KMPXMediaGeneralId) );
       
   819     
       
   820     const TDesC& uri = aMedia.ValueText(KMPXMediaGeneralUri);
       
   821     aSongItem.SetUriL( uri );
       
   822     
       
   823     if ( aMedia.IsSupported(KMPXMediaGeneralTitle) )
       
   824         {
       
   825         aSongItem.SetTitleL( aMedia.ValueText(KMPXMediaGeneralTitle) );
       
   826         }
       
   827     
       
   828     if ( aMedia.IsSupported(KMPXMediaGeneralMimeType) )
       
   829         {
       
   830         aSongItem.SetMimeTypeL( aMedia.ValueText(KMPXMediaGeneralMimeType) );
       
   831         }
       
   832     
       
   833     if ( aMedia.IsSupported(KMPXMediaGeneralDuration) )
       
   834         {
       
   835         aSongItem.SetDuration( aMedia.ValueTObjectL<TInt>(KMPXMediaGeneralDuration) );
       
   836         }
       
   837     
       
   838     if ( aMedia.IsSupported(KMPXMediaMusicArtist) )
       
   839         {
       
   840         aSongItem.SetArtistL( aMedia.ValueText(KMPXMediaMusicArtist) );
       
   841         }
       
   842     
       
   843     if ( aMedia.IsSupported(KMPXMediaMusicAlbum) )
       
   844         {
       
   845         aSongItem.SetAlbumL( aMedia.ValueText(KMPXMediaMusicAlbum) );
       
   846         }
       
   847     
       
   848     if ( aMedia.IsSupported(KMPXMediaMusicYear) )
       
   849         {
       
   850         TInt64 year = aMedia.ValueTObjectL<TInt64>( KMPXMediaMusicYear );
       
   851         TTime yearTime( year);
       
   852         aSongItem.SetYear( yearTime.DateTime().Year() );
       
   853         }
       
   854     
       
   855     if ( aMedia.IsSupported(KMPXMediaMusicAlbumTrack) )
       
   856         {
       
   857         aSongItem.SetAlbumTrack( *aMedia.Value<TInt>(KMPXMediaMusicAlbumTrack) );
       
   858         }
       
   859     
       
   860     if ( aMedia.IsSupported(KMPXMediaMusicGenre) )
       
   861         {
       
   862         const TDesC& genre = aMedia.ValueText(KMPXMediaMusicGenre);
       
   863         aSongItem.SetGenreL( genre );
       
   864         }
       
   865     
       
   866     if ( aMedia.IsSupported(KMPXMediaMusicComposer) )
       
   867         {
       
   868         aSongItem.SetComposerL( aMedia.ValueText(KMPXMediaMusicComposer) );
       
   869         }
       
   870     
       
   871     if ( aMedia.IsSupported(KMPXMediaGeneralComment) )
       
   872         {
       
   873         aSongItem.SetCommentL( aMedia.ValueText(KMPXMediaGeneralComment) );
       
   874         }
       
   875     
       
   876     // Reading file timestamp and size is slow, read only if needed
       
   877     if ( aReadFileDetails )
       
   878         {
       
   879         TEntry fileEntry;
       
   880         TInt err = iFs.Entry(uri,fileEntry);
       
   881         if ( !err )
       
   882             {
       
   883             aSongItem.SetModifiedTime( fileEntry.iModified );
       
   884             aSongItem.SetFileSize( fileEntry.iSize );
       
   885             }
       
   886         }
       
   887     
       
   888     }
       
   889 
       
   890 
       
   891 void CMediaManager::DoCancel()
       
   892     {
       
   893     TRACE_FUNC_ENTRY;
       
   894     
       
   895     delete iFoundedMedia;
       
   896     iFoundedMedia = NULL;
       
   897     iReadedItems = 0;
       
   898     iItemsFound = 0;      
       
   899     TState state = iCurrentState;
       
   900     iCurrentState = EIdle;
       
   901     
       
   902     switch (state)
       
   903         {
       
   904         case EReadingPlaylistCount:
       
   905         case EEnumeratingPlaylists:
       
   906             iObserver->RegisterAllPlayListsCompleted( KErrCancel );
       
   907             break;
       
   908             
       
   909         case EReadingSongCount:
       
   910         case EEnumeratingSongs:
       
   911             iObserver->RegisterAllSongsCompleted( KErrCancel );
       
   912             break;
       
   913         default:
       
   914             break;
       
   915         }
       
   916     
       
   917     LOGGER_WRITE("signal cancel");
       
   918     TRequestStatus* status = &iStatus;
       
   919     User::RequestComplete( status, KErrCancel );
       
   920     
       
   921     TRACE_FUNC_EXIT;
       
   922     }
       
   923 
       
   924 void CMediaManager::RunL()
       
   925     {
       
   926     TRACE_FUNC_ENTRY;
       
   927     LOGGER_WRITE_1("iStatus.Int(): %d", iStatus.Int())
       
   928     TRequestStatus* status = &iStatus;
       
   929     switch (iCurrentState)
       
   930         {
       
   931         case EReadingPlaylistCount:
       
   932             //if ( !iCancel )
       
   933             //    {
       
   934                 ReadPlaylistCountL();
       
   935                 iReadedItems=0;
       
   936                 // next state
       
   937                 iCurrentState = EEnumeratingPlaylists;
       
   938                 SetActive();
       
   939                 User::RequestComplete( status, KErrNone );
       
   940             /*    }
       
   941             else
       
   942                 {
       
   943                 LOGGER_WRITE("cancel");
       
   944                 iCancel = EFalse;
       
   945                 delete iFoundedMedia; iFoundedMedia = NULL;
       
   946                 iReadedItems = 0;
       
   947                 iItemsFound = 0;
       
   948                 iCurrentState = EIdle;
       
   949                 iObserver->RegisterAllPlayListsCompleted( KErrCancel );
       
   950                 }*/
       
   951             break;
       
   952             
       
   953         case EEnumeratingPlaylists:
       
   954             //if ( !iCancel )
       
   955             //    {
       
   956                 ReadPlaylistItemsL();
       
   957                 if ( iReadedItems == iItemsFound )
       
   958                     {
       
   959                     // all items readed
       
   960                     delete iFoundedMedia;
       
   961                     iFoundedMedia = NULL;
       
   962                     // next state
       
   963                     iCurrentState = EIdle;
       
   964                     iObserver->RegisterAllPlayListsCompleted( KErrNone );
       
   965                     }
       
   966                 else
       
   967                     {
       
   968                     // not ready yet, continue same operation
       
   969                     SetActive();
       
   970                     User::RequestComplete( status, KErrNone );
       
   971                     }
       
   972              /*   }
       
   973             else
       
   974                 {
       
   975                 // cancel
       
   976                 LOGGER_WRITE("cancel");
       
   977                 iCancel = EFalse;
       
   978                 delete iFoundedMedia; iFoundedMedia = NULL;
       
   979                 iReadedItems = 0;
       
   980                 iItemsFound = 0;
       
   981                 iCurrentState = EIdle;
       
   982                 iObserver->RegisterAllPlayListsCompleted( KErrCancel );
       
   983                 }*/
       
   984             break;
       
   985             
       
   986         case EReadingSongCount:
       
   987             //if ( !iCancel )
       
   988             //    {
       
   989                 ReadSongCountL();
       
   990                 iReadedItems=0;
       
   991                 // next state
       
   992                 iCurrentState = EEnumeratingSongs;
       
   993                 SetActive();
       
   994                 User::RequestComplete( status, KErrNone );
       
   995             /*    }
       
   996             else
       
   997                 {
       
   998                 LOGGER_WRITE("cancel");
       
   999                 iCancel = EFalse;
       
  1000                 delete iFoundedMedia; iFoundedMedia = NULL;
       
  1001                 iReadedItems = 0;
       
  1002                 iItemsFound = 0;
       
  1003                 iCurrentState = EIdle;
       
  1004                 iObserver->RegisterAllSongsCompleted( KErrCancel );
       
  1005                 }*/
       
  1006             break;
       
  1007             
       
  1008         case EEnumeratingSongs:
       
  1009 //            if ( !iCancel )
       
  1010               //  {
       
  1011                 ReadSongItemsL();
       
  1012                 if ( iReadedItems == iItemsFound )
       
  1013                     {
       
  1014                     // all items readed
       
  1015                     delete iFoundedMedia;
       
  1016                     iFoundedMedia = NULL;
       
  1017                     // next state
       
  1018                     iCurrentState = EIdle;
       
  1019                     iObserver->RegisterAllSongsCompleted( KErrNone );
       
  1020                     }
       
  1021                 else
       
  1022                     {
       
  1023                     // not ready yet, continue same operation
       
  1024                     SetActive();
       
  1025                     User::RequestComplete( status, KErrNone );
       
  1026                     }
       
  1027               /*  }
       
  1028             else
       
  1029                 {
       
  1030                 LOGGER_WRITE("cancel");
       
  1031                 iCancel = EFalse;
       
  1032                 delete iFoundedMedia; iFoundedMedia = NULL;
       
  1033                 iReadedItems = 0;
       
  1034                 iItemsFound = 0;
       
  1035                 iCurrentState = EIdle;
       
  1036                 iObserver->RegisterAllSongsCompleted( KErrCancel );
       
  1037                 }*/
       
  1038             break;
       
  1039         default:
       
  1040             break;
       
  1041         }
       
  1042     TRACE_FUNC_EXIT;
       
  1043     }
       
  1044 
       
  1045 
       
  1046 TInt CMediaManager::RunError( TInt aError )
       
  1047     {
       
  1048     TRACE_FUNC_ENTRY;
       
  1049     LOGGER_WRITE_1("aError: %d", aError)
       
  1050     switch (iCurrentState)
       
  1051         {
       
  1052         case EReadingPlaylistCount:
       
  1053         case EEnumeratingPlaylists:
       
  1054             iObserver->RegisterAllPlayListsCompleted( aError );
       
  1055             break;
       
  1056         case EReadingSongCount:
       
  1057         case EEnumeratingSongs:
       
  1058             iObserver->RegisterAllSongsCompleted( aError );
       
  1059             break;
       
  1060         default:
       
  1061             LOGGER_WRITE("Unknown state!");
       
  1062             break;
       
  1063         }
       
  1064     
       
  1065     TRACE_FUNC_EXIT;
       
  1066     return KErrNone;
       
  1067     }
       
  1068 
       
  1069 void CMediaManager::ReadPlaylistCountL()
       
  1070     {
       
  1071     TRACE_FUNC_ENTRY;
       
  1072     // Find all playlist files in the playlist DB
       
  1073     RArray<TInt> contentIDs;
       
  1074     CleanupClosePushL( contentIDs );
       
  1075     contentIDs.AppendL(KMPXMediaIdGeneral);
       
  1076     
       
  1077     CMPXMedia* searchMedia = CMPXMedia::NewL(contentIDs.Array());
       
  1078     CleanupStack::PopAndDestroy( &contentIDs );
       
  1079     CleanupStack::PushL(searchMedia);
       
  1080     
       
  1081     searchMedia->SetTObjectValueL(KMPXMediaGeneralType, EMPXItem);    
       
  1082     searchMedia->SetTObjectValueL(KMPXMediaGeneralCategory, EMPXPlaylist);
       
  1083     
       
  1084     RArray<TMPXAttribute> playlistAttributes;
       
  1085     CleanupClosePushL(playlistAttributes);
       
  1086     playlistAttributes.AppendL( KMPXMediaGeneralId );
       
  1087     playlistAttributes.AppendL( KMPXMediaGeneralTitle );
       
  1088     playlistAttributes.AppendL( KMPXMediaGeneralUri );
       
  1089     
       
  1090     LOGGER_WRITE("find all");
       
  1091     delete iFoundedMedia; iFoundedMedia=NULL;
       
  1092     iFoundedMedia = iCollectionHelper->FindAllL(*searchMedia, playlistAttributes.Array());
       
  1093     CleanupStack::PopAndDestroy(&playlistAttributes);
       
  1094     CleanupStack::PopAndDestroy(searchMedia);
       
  1095 
       
  1096     if ( !iFoundedMedia->IsSupported(KMPXMediaArrayCount) )
       
  1097         {
       
  1098         delete iFoundedMedia; iFoundedMedia=NULL;
       
  1099         User::Leave( KErrNotSupported );
       
  1100         }
       
  1101     
       
  1102     iItemsFound = *iFoundedMedia->Value<TInt>(KMPXMediaArrayCount);
       
  1103     
       
  1104     LOGGER_WRITE_1("playlists: %d", iItemsFound);
       
  1105 
       
  1106     if ( iItemsFound )
       
  1107         {
       
  1108         if ( !iFoundedMedia->IsSupported(KMPXMediaArrayContents) )
       
  1109             {
       
  1110             delete iFoundedMedia; iFoundedMedia=NULL;
       
  1111             iItemsFound = 0;
       
  1112             User::Leave(KErrNotSupported);
       
  1113             }
       
  1114         }
       
  1115     
       
  1116     TRACE_FUNC_EXIT;
       
  1117     }
       
  1118 
       
  1119 void CMediaManager::ReadPlaylistItemsL()
       
  1120     {
       
  1121     TRACE_FUNC_ENTRY;
       
  1122     TInt start = iReadedItems;
       
  1123     TInt end = start+iMaxPlaylistItemsToEnumerate;
       
  1124     if ( end >iItemsFound )
       
  1125         {
       
  1126         end = iItemsFound;
       
  1127         }
       
  1128     
       
  1129     const CMPXMediaArray* foundArray = iFoundedMedia->Value<CMPXMediaArray>(KMPXMediaArrayContents);
       
  1130             
       
  1131     for (TInt i = start; i < end; i++)
       
  1132         {
       
  1133         LOGGER_WRITE_1("read playlistItem: %d", i);
       
  1134         CMPXMedia* playlistMedia = CMPXMedia::NewL(*(*foundArray)[i]);
       
  1135         CleanupStack::PushL( playlistMedia );
       
  1136         
       
  1137         // Append playlist to item array
       
  1138         RegisterPlaylistL( *playlistMedia );
       
  1139                
       
  1140         CleanupStack::PopAndDestroy( playlistMedia );
       
  1141         iReadedItems++;
       
  1142         }
       
  1143     
       
  1144     TRACE_FUNC_EXIT;
       
  1145     }
       
  1146 
       
  1147     
       
  1148 
       
  1149 void CMediaManager::ReadSongCountL()
       
  1150     {
       
  1151     TRACE_FUNC_ENTRY;
       
  1152 
       
  1153     RArray<TInt> contentIDs;
       
  1154     CleanupClosePushL( contentIDs );
       
  1155     contentIDs.AppendL( KMPXMediaIdGeneral );
       
  1156     contentIDs.AppendL( KMPXMediaIdMusic );
       
  1157     
       
  1158     CMPXMedia* criteria = CMPXMedia::NewL(contentIDs.Array());
       
  1159     CleanupStack::PopAndDestroy( &contentIDs );
       
  1160     CleanupStack::PushL(criteria);
       
  1161     
       
  1162     criteria->SetTObjectValueL( KMPXMediaGeneralType, EMPXItem );
       
  1163     criteria->SetTObjectValueL( KMPXMediaGeneralCategory, EMPXSong );
       
  1164     
       
  1165     // define attributes fetched
       
  1166     RArray<TMPXAttribute> songAttributes;
       
  1167     CleanupClosePushL(songAttributes);
       
  1168     
       
  1169     songAttributes.AppendL( KMPXMediaGeneralType );
       
  1170     songAttributes.AppendL( KMPXMediaGeneralCategory );
       
  1171     songAttributes.AppendL( KMPXMediaGeneralUri );
       
  1172     songAttributes.AppendL( KMPXMediaGeneralId );
       
  1173     songAttributes.AppendL( KMPXMediaGeneralTitle );
       
  1174     songAttributes.AppendL( KMPXMediaGeneralMimeType );
       
  1175     songAttributes.AppendL( KMPXMediaGeneralDuration );
       
  1176     songAttributes.AppendL( KMPXMediaGeneralComment );
       
  1177     
       
  1178     songAttributes.AppendL( KMPXMediaMusicArtist );
       
  1179     songAttributes.AppendL( KMPXMediaMusicAlbum );
       
  1180     songAttributes.AppendL( KMPXMediaMusicYear );
       
  1181     songAttributes.AppendL( KMPXMediaMusicAlbumTrack );
       
  1182     songAttributes.AppendL( KMPXMediaMusicGenre );
       
  1183     songAttributes.AppendL( KMPXMediaMusicComposer );
       
  1184     
       
  1185     delete iFoundedMedia;
       
  1186     iFoundedMedia = NULL;
       
  1187     // now find
       
  1188     LOGGER_WRITE("FindAllL");
       
  1189     iFoundedMedia = iCollectionHelper->FindAllL( *criteria , songAttributes.Array() );
       
  1190         
       
  1191     LOGGER_WRITE("FindAllL ok");
       
  1192     CleanupStack::PopAndDestroy( &songAttributes );
       
  1193     CleanupStack::PopAndDestroy( criteria );
       
  1194     
       
  1195 
       
  1196     if ( !iFoundedMedia->IsSupported(KMPXMediaArrayCount) )
       
  1197          {
       
  1198          LOGGER_WRITE("KMPXMediaArrayCount not supported");
       
  1199          delete iFoundedMedia;
       
  1200          iFoundedMedia = NULL;
       
  1201          iItemsFound = 0;
       
  1202          User::Leave(KErrNotSupported);
       
  1203          }
       
  1204      
       
  1205     // Number of references
       
  1206     iItemsFound = *iFoundedMedia->Value<TInt>(KMPXMediaArrayCount);
       
  1207     LOGGER_WRITE_1("songs: %d", iItemsFound);
       
  1208     if ( iItemsFound )
       
  1209         {
       
  1210         if (!iFoundedMedia->IsSupported(KMPXMediaArrayContents))
       
  1211             {
       
  1212             delete iFoundedMedia;
       
  1213             iFoundedMedia = NULL;
       
  1214             iItemsFound = 0;
       
  1215             User::Leave(KErrNotSupported);
       
  1216             }
       
  1217         }
       
  1218     
       
  1219     TRACE_FUNC_EXIT;
       
  1220     }
       
  1221 
       
  1222 void CMediaManager::ReadSongItemsL()
       
  1223     {
       
  1224     TRACE_FUNC_ENTRY;
       
  1225     
       
  1226     TInt start = iReadedItems;
       
  1227     TInt end = start+iMaxSongItemsToEnumerate;
       
  1228     if ( end >iItemsFound )
       
  1229         {
       
  1230         end = iItemsFound;
       
  1231         }
       
  1232     
       
  1233     const CMPXMediaArray* foundArray = iFoundedMedia->Value<CMPXMediaArray>(KMPXMediaArrayContents);
       
  1234     
       
  1235     TKeyArrayFix key( iKey );
       
  1236     // Import the references
       
  1237     for (TInt i = start; i < end; i++)
       
  1238         {
       
  1239         //LOGGER_WRITE_1("read songitem: %d",i);
       
  1240         const CMPXMedia* refMedia = (*foundArray)[i];
       
  1241         
       
  1242         CSongItem* song = CSongItem::NewLC();
       
  1243         
       
  1244         ReadSongAttributesL( *refMedia, *song, EFalse );
       
  1245         TSnapshotItem snapshotItem( song->Id(), KAllSongsFolderId );
       
  1246         snapshotItem.SetItemHash( *song, iHasher );
       
  1247         
       
  1248         iSnapshotArray->InsertIsqL( snapshotItem, key );
       
  1249         
       
  1250         CleanupStack::PopAndDestroy( song );
       
  1251         iReadedItems++;
       
  1252         }
       
  1253     TRACE_FUNC_EXIT;
       
  1254     }