mpengine/src/mpmpxcollectionframeworkwrapper_p.cpp
changeset 22 ecf06a08d4d9
child 25 3ec52facab4d
equal deleted inserted replaced
20:82baf59ce8dd 22:ecf06a08d4d9
       
     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: Wrapper for mpx collection framework utilities - private implementation.
       
    15 *
       
    16 */
       
    17 
       
    18 #include <QStringList>
       
    19 
       
    20 #include <s32strm.h>
       
    21 #include <s32mem.h>
       
    22 #include <apgcli.h>
       
    23 #include <apacmdln.h>
       
    24 
       
    25 #include <mpxplaybackutility.h>
       
    26 #include <mpxcollectionutility.h>
       
    27 #include <mpxharvesterutility.h>
       
    28 #include <mpxcollectionpath.h>
       
    29 #include <mpxcollectionmessage.h>
       
    30 #include <mpxcollectionplaylist.h>
       
    31 #include <mpxmessagecontainerdefs.h>
       
    32 #include <mpxmediacollectiondetaildefs.h>
       
    33 #include <mpxmediamusicdefs.h>
       
    34 #include <mpxmediageneraldefs.h>
       
    35 #include <mpxcollectionplugin.hrh>
       
    36 #include <mpxmessagegeneraldefs.h>
       
    37 #include <mpxmediacontainerdefs.h>
       
    38 #include <mpxcollectionmessagedefs.h>
       
    39 #include <mpxparameter.h>
       
    40 #include <mpxcollectionhelperfactory.h>
       
    41 #include <mpxcollectionuihelper.h>
       
    42 #include <mpxcollectionopenutility.h>
       
    43 #include <mpxmediaarray.h>
       
    44 #include <mpxmedia.h>
       
    45 #include <mpxlog.h>
       
    46 
       
    47 #include "mpmpxcollectionframeworkwrapper_p.h"
       
    48 #include "mpmpxcollectionframeworkwrapper.h"
       
    49 #include "mpmpxcollectiondata.h"
       
    50 #include "mpcommondefs.h"
       
    51 #include "mptrace.h"
       
    52 #include "mpsettingsmanager.h"
       
    53 
       
    54 const TInt KIncrementalDelayNone = 0;
       
    55 const TInt KIncrementalDelayHalfSecond = 1000000;
       
    56 const TInt KIncrementalFetchBlockSize = 40;
       
    57 
       
    58 const TInt KMPXChunkSize = 100;  // number of songs added in each chunk, IncAddL
       
    59 _LIT( KPlaylistPath, "C:\\Data\\Playlists\\" ); // Todo
       
    60 
       
    61 /*!
       
    62     \class MpMpxCollectionFrameworkWrapperPrivate
       
    63     \brief Wrapper for mpx framework utilities - private implementation.
       
    64 
       
    65     This is a private implementation of the mpx framework wrapper utilties interface.
       
    66 */
       
    67 
       
    68 /*!
       
    69  \internal
       
    70  */
       
    71 MpMpxCollectionFrameworkWrapperPrivate::MpMpxCollectionFrameworkWrapperPrivate( MpMpxCollectionFrameworkWrapper *wrapper )
       
    72     : q_ptr( wrapper ),
       
    73       iCollectionUtility( 0 ),
       
    74       iCollectionUiHelper( 0 ),
       
    75       iIncrementalOpenUtil( 0 ),
       
    76       iIsolatedCollectionHelper( 0 ),
       
    77       iPlaybackUtility( 0 ),
       
    78       iCollectionData( 0 ),
       
    79       iIsolatedCollectionData( 0 ),
       
    80       iFirstIncrementalOpen( EFalse ),
       
    81       iUserPlaylists( 0 )
       
    82 {
       
    83     TX_LOG
       
    84 }
       
    85 
       
    86 /*!
       
    87  \internal
       
    88  */
       
    89 MpMpxCollectionFrameworkWrapperPrivate::~MpMpxCollectionFrameworkWrapperPrivate()
       
    90 {
       
    91     TX_ENTRY
       
    92     delete iCollectionData;
       
    93     delete iIsolatedCollectionData;
       
    94 
       
    95     if ( iCollectionUtility ) {
       
    96         iCollectionUtility->Collection().CancelRequest();
       
    97         iCollectionUtility->Close();
       
    98     }
       
    99 
       
   100     if ( iCollectionUiHelper ) {
       
   101         iCollectionUiHelper->Close();
       
   102     }
       
   103 
       
   104     if ( iPlaybackUtility ) {
       
   105         iPlaybackUtility->Close();
       
   106     }
       
   107 
       
   108     delete iIncrementalOpenUtil;
       
   109     delete iIsolatedCollectionHelper;
       
   110     delete iUserPlaylists;
       
   111 
       
   112     TX_EXIT
       
   113 }
       
   114 
       
   115 /*!
       
   116  \internal
       
   117  */
       
   118 void MpMpxCollectionFrameworkWrapperPrivate::init( MpCommon::MpViewMode viewMode, TUid hostUid )
       
   119 {
       
   120 
       
   121     iViewMode = viewMode;
       
   122     mHostUid = hostUid;
       
   123     TRAPD( err, DoInitL() );
       
   124     if ( err != KErrNone ) {
       
   125         TX_LOG_ARGS( "Error: " << err << "; should never get here." );
       
   126     }
       
   127 }
       
   128 
       
   129 /*!
       
   130  \internal
       
   131  */
       
   132 void MpMpxCollectionFrameworkWrapperPrivate::openCollection( TCollectionContext context )
       
   133 {
       
   134     TRAPD( err, DoOpenCollectionL( context ) );
       
   135     if ( err != KErrNone ) {
       
   136         TX_LOG_ARGS( "Error: " << err << "; should never get here." );
       
   137     }
       
   138 }
       
   139 
       
   140 /*!
       
   141  \internal
       
   142  */
       
   143 void MpMpxCollectionFrameworkWrapperPrivate::openCollectionItem( int index )
       
   144 {
       
   145     TRAPD( err, DoOpenCollectionItemL( index ) );
       
   146     if ( err != KErrNone ) {
       
   147         TX_LOG_ARGS( "Error: " << err << "; should never get here." );
       
   148     }
       
   149 }
       
   150 
       
   151 /*!
       
   152  \internal
       
   153  */
       
   154 void MpMpxCollectionFrameworkWrapperPrivate::reopenCollection()
       
   155 {
       
   156     TRAPD( err, DoReopenCollectionL() );
       
   157     if ( err != KErrNone ) {
       
   158         TX_LOG_ARGS( "Error: " << err << "; should never get here." );
       
   159     }
       
   160 }
       
   161 
       
   162 /*!
       
   163  \internal
       
   164  */
       
   165 void MpMpxCollectionFrameworkWrapperPrivate::back()
       
   166 {
       
   167     TRAPD( err, DoBackL() );
       
   168     if ( err != KErrNone ) {
       
   169         TX_LOG_ARGS( "Error: " << err << "; should never get here." );
       
   170     }
       
   171 }
       
   172 /*!
       
   173  \internal
       
   174  */
       
   175 void MpMpxCollectionFrameworkWrapperPrivate::findPlaylists( QStringList &playlists )
       
   176 {
       
   177     TRAPD( err, DoFindPlaylistsL( playlists ) );
       
   178     if ( err != KErrNone ) {
       
   179         TX_LOG_ARGS( "Error: " << err << "; should never get here." );
       
   180     }
       
   181 }
       
   182 
       
   183 /*!
       
   184  \internal
       
   185  */
       
   186 void MpMpxCollectionFrameworkWrapperPrivate::createPlaylist( QString &playlistName, QList<int> &selection, MpMpxCollectionData* collectionData )
       
   187 {
       
   188     TRAPD( err, DoCreatePlaylistL( playlistName, selection, collectionData ) );
       
   189     if ( err != KErrNone ) {
       
   190         TX_LOG_ARGS( "Error: " << err << "; should never get here." );
       
   191     }
       
   192 }
       
   193 
       
   194 /*!
       
   195  \internal
       
   196  */
       
   197 void MpMpxCollectionFrameworkWrapperPrivate::saveToPlaylist( int playlistIndex, QList<int> &selection )
       
   198 {
       
   199     TRAPD( err, DoSaveToPlaylistL( playlistIndex, selection ) );
       
   200     if ( err != KErrNone ) {
       
   201         TX_LOG_ARGS( "Error: " << err << "; should never get here." );
       
   202     }
       
   203 }
       
   204 
       
   205 /*!
       
   206  \internal
       
   207  */
       
   208 void MpMpxCollectionFrameworkWrapperPrivate::saveToCurrentPlaylist( QList<int> &selection, MpMpxCollectionData *collectionData )
       
   209 {
       
   210     TRAPD( err, DoSaveToCurrentPlaylistL( selection, collectionData ) );
       
   211     if ( err != KErrNone ) {
       
   212         TX_LOG_ARGS( "Error: " << err << "; should never get here." );
       
   213     }
       
   214 } 
       
   215 
       
   216 /*!
       
   217  \internal
       
   218  */
       
   219 void MpMpxCollectionFrameworkWrapperPrivate::deleteSongs( QList<int> &selection )
       
   220 {
       
   221     TRAPD( err, DoDeleteSongsL( selection ) );
       
   222     if ( err != KErrNone ) {
       
   223         TX_LOG_ARGS( "Error: " << err << "; should never get here." );
       
   224     }
       
   225 }
       
   226 
       
   227 /*!
       
   228  \internal
       
   229  */
       
   230 void MpMpxCollectionFrameworkWrapperPrivate::renamePlaylist( QString &newName, int index )
       
   231 {      
       
   232     TRAPD( err, DoRenamePlaylistL( newName, index ) );
       
   233     if ( err != KErrNone ) {
       
   234     TX_LOG_ARGS( "Error: " << err << "; should never get here." );
       
   235     }
       
   236 }
       
   237 
       
   238 /*!
       
   239  \internal
       
   240  */
       
   241 void MpMpxCollectionFrameworkWrapperPrivate::renamePlaylist( QString &newName )
       
   242 {      
       
   243     TRAPD( err, DoRenamePlaylistL( newName ) );
       
   244     if ( err != KErrNone ) {
       
   245     TX_LOG_ARGS( "Error: " << err << "; should never get here." );
       
   246     }
       
   247 }
       
   248 /*!
       
   249  \internal
       
   250  */
       
   251 void MpMpxCollectionFrameworkWrapperPrivate::setShuffle( bool active )
       
   252 {
       
   253     TRAPD( err, DoSetShuffleL( active ) );
       
   254     if ( err != KErrNone ) {
       
   255         TX_LOG_ARGS( "Error: " << err << "; should never get here." );
       
   256     }
       
   257 }
       
   258 
       
   259 /*!
       
   260  \internal
       
   261  */
       
   262 void MpMpxCollectionFrameworkWrapperPrivate::previewItem( int index )
       
   263 {
       
   264     TRAPD( err, DoPreviewItemL( index ) );
       
   265     if ( err != KErrNone ) {
       
   266         TX_LOG_ARGS( "Error: " << err << "; should never get here." );
       
   267     }
       
   268 }
       
   269 
       
   270 /*!
       
   271  \internal
       
   272  */
       
   273  void MpMpxCollectionFrameworkWrapperPrivate::openIsolatedCollection( TCollectionContext context )
       
   274 {
       
   275     TRAPD( err, DoOpenIsolatedCollectionL( context ) );
       
   276     if ( err != KErrNone ) {
       
   277         TX_LOG_ARGS( "Error: " << err << "; should never get here." );
       
   278     }
       
   279 }
       
   280 
       
   281 /*!
       
   282  \internal
       
   283  */
       
   284 void MpMpxCollectionFrameworkWrapperPrivate::releaseIsolatedCollection()
       
   285 {
       
   286     delete iIsolatedCollectionHelper;
       
   287     iIsolatedCollectionHelper = 0;
       
   288     delete iIsolatedCollectionData;
       
   289     iIsolatedCollectionData = 0;
       
   290 }
       
   291 
       
   292 /*!
       
   293  \internal
       
   294  */
       
   295 void MpMpxCollectionFrameworkWrapperPrivate::reorderPlaylist( int playlistId, int songId, int originalOrdinal, int newOrdinal )
       
   296 {
       
   297     TRAPD( err, DoReorderPlaylistL( playlistId, songId, originalOrdinal, newOrdinal ) );
       
   298     if ( err != KErrNone ) {
       
   299         TX_LOG_ARGS( "Error: " << err << "; should never get here." );
       
   300     }
       
   301 }
       
   302 
       
   303 /*!
       
   304  \internal
       
   305  */
       
   306 MpMpxCollectionData *MpMpxCollectionFrameworkWrapperPrivate::collectionData()
       
   307 {
       
   308     return iCollectionData;
       
   309 }
       
   310 
       
   311 /*!
       
   312  \internal
       
   313  */
       
   314 void MpMpxCollectionFrameworkWrapperPrivate::HandleOpenL( 
       
   315     const CMPXMedia& aEntries,
       
   316     TInt aIndex,
       
   317     TBool aComplete,
       
   318     TInt aError )
       
   319 {
       
   320     Q_UNUSED( aIndex );
       
   321     TX_UNUSED( aComplete );
       
   322     TX_ENTRY_ARGS( "aError=" << aError << "aComplete=" << aComplete );
       
   323     if ( aError == KErrNone ) {
       
   324         if ( iFirstIncrementalOpen ) {
       
   325             iCollectionData->setMpxMedia( aEntries );
       
   326             iFirstIncrementalOpen = EFalse;
       
   327         }
       
   328     }
       
   329     else {
       
   330         TX_LOG_ARGS( "Error: " << aError << "; should never get here." );
       
   331     }
       
   332     TX_EXIT
       
   333 }
       
   334 
       
   335 /*!
       
   336  \internal
       
   337  */
       
   338 void MpMpxCollectionFrameworkWrapperPrivate::HandleOpenL( 
       
   339     const CMPXCollectionPlaylist& aPlaylist,
       
   340     TInt aError )
       
   341 {
       
   342     TX_ENTRY_ARGS( "aError=" << aError );
       
   343 
       
   344     if ( aError == KErrNone ) {
       
   345         if ( !iPlaybackUtility ) {
       
   346 
       
   347             iPlaybackUtility = MMPXPlaybackUtility::UtilityL( mHostUid );
       
   348 
       
   349             if ( iViewMode == MpCommon::DefaultView ) {
       
   350                 iPlaybackUtility->SetL( EPbPropertyRandomMode, MpSettingsManager::shuffle() ? ETrue : EFalse );
       
   351                 iPlaybackUtility->SetL( EPbPropertyRepeatMode, MpSettingsManager::repeat() ? EPbRepeatAll : EPbRepeatOff );
       
   352             }
       
   353         }
       
   354         iPlaybackUtility->InitL( aPlaylist, ETrue );
       
   355         emit q_ptr->collectionPlaylistOpened();
       
   356     }
       
   357     else {
       
   358         TX_LOG_ARGS( "Error: " << aError << "; should never get here." );
       
   359     }
       
   360     TX_EXIT
       
   361 }
       
   362 
       
   363 /*!
       
   364  \internal
       
   365  */
       
   366 void MpMpxCollectionFrameworkWrapperPrivate::HandleCollectionMessage( 
       
   367     CMPXMessage* aMsg,
       
   368     TInt aErr )
       
   369 {
       
   370     TX_ENTRY_ARGS( "aErr=" << aErr );
       
   371     if ( aErr == KErrNone && aMsg )
       
   372         {
       
   373         TRAP_IGNORE( DoHandleCollectionMessageL( *aMsg ) );
       
   374     }
       
   375     TX_EXIT
       
   376 }
       
   377 
       
   378 /*!
       
   379  \internal
       
   380  */
       
   381 void MpMpxCollectionFrameworkWrapperPrivate::HandleCollectionMediaL( 
       
   382     const CMPXMedia& aMedia,
       
   383     TInt aError )
       
   384 {
       
   385     Q_UNUSED( aMedia );
       
   386     Q_UNUSED( aError );
       
   387 }
       
   388 
       
   389 /*!
       
   390  \internal
       
   391  Handles the completion of any collection helper event.
       
   392  */
       
   393 void MpMpxCollectionFrameworkWrapperPrivate::HandleOperationCompleteL( 
       
   394     TCHelperOperation aOperation,
       
   395     TInt aErr,
       
   396     void* aArgument )
       
   397 {
       
   398     TX_ENTRY_ARGS( "aErr=" << aErr );
       
   399     switch( aOperation ) {
       
   400     case EDeleteOp:
       
   401         emit q_ptr->songsDeleted( KErrNone == aErr );
       
   402         break;
       
   403     case EAddOp:
       
   404         emit q_ptr->playlistSaved( KErrNone == aErr );
       
   405         break;
       
   406     case ERenameOp:
       
   407         emit q_ptr->playlistsRenamed( KErrNone == aErr );
       
   408         break;
       
   409     default:
       
   410         break;
       
   411     }
       
   412 
       
   413     if ( aArgument ) {
       
   414         delete ( CBase* )aArgument;
       
   415     }
       
   416 }
       
   417 
       
   418 /*!
       
   419  \internal
       
   420  */
       
   421 void MpMpxCollectionFrameworkWrapperPrivate::HandleIsolatedOpenL( const CMPXMedia& aEntries, TInt aError )
       
   422 {
       
   423     TX_ENTRY_ARGS( "aError=" << aError );
       
   424     if ( aError == KErrNone ) {
       
   425         if ( iIsolatedCollectionData ) {
       
   426             delete iIsolatedCollectionData;
       
   427             iIsolatedCollectionData = 0;
       
   428         }  
       
   429         iIsolatedCollectionData = new MpMpxCollectionData();
       
   430         iIsolatedCollectionData->setMpxMedia( aEntries );
       
   431         emit q_ptr->isolatedCollectionOpened( iIsolatedCollectionData );
       
   432     }
       
   433     else {
       
   434         TX_LOG_ARGS( "Error: " << aError << "; should never get here." );
       
   435     }
       
   436     TX_EXIT
       
   437 }
       
   438 
       
   439 /*!
       
   440  \internal
       
   441  */
       
   442 void MpMpxCollectionFrameworkWrapperPrivate::DoInitL()
       
   443 {
       
   444     TX_ENTRY
       
   445     if ( iViewMode == MpCommon::FetchView ) {
       
   446         iCollectionUtility = MMPXCollectionUtility::NewL( this, mHostUid );
       
   447         iCollectionUiHelper = CMPXCollectionHelperFactory:: NewCollectionUiHelperL( mHostUid );
       
   448         iIncrementalOpenUtil = CMPXCollectionOpenUtility::NewL( this, mHostUid );
       
   449     }
       
   450     else {
       
   451         iCollectionUtility = MMPXCollectionUtility::NewL( this, KMcModeDefault );
       
   452         iCollectionUiHelper = CMPXCollectionHelperFactory:: NewCollectionUiHelperL();
       
   453         iIncrementalOpenUtil = CMPXCollectionOpenUtility::NewL( this );
       
   454     }
       
   455     
       
   456     iCollectionData = new MpMpxCollectionData();
       
   457     TX_EXIT
       
   458 }
       
   459 
       
   460 /*!
       
   461  \internal
       
   462  */
       
   463 void MpMpxCollectionFrameworkWrapperPrivate::DoOpenCollectionL( 
       
   464     TCollectionContext aContext )
       
   465 {
       
   466     TX_ENTRY_ARGS( "aContext=" << aContext );
       
   467 
       
   468     switch ( aContext ) {
       
   469     case ECollectionContextAllSongs:
       
   470         {
       
   471         CMPXCollectionPath* cpath = iCollectionUiHelper->MusicAllSongsPathL();
       
   472         CleanupStack::PushL( cpath );
       
   473         iCollectionUtility->Collection().OpenL( *cpath );
       
   474         CleanupStack::PopAndDestroy( cpath );
       
   475         break;
       
   476         }
       
   477     case ECollectionContextArtists:
       
   478         {
       
   479         CMPXCollectionPath* cpath = iCollectionUiHelper->MusicMenuPathL();
       
   480         CleanupStack::PushL( cpath );
       
   481         cpath->AppendL( 2 );
       
   482         iCollectionUtility->Collection().OpenL( *cpath );
       
   483         CleanupStack::PopAndDestroy( cpath );
       
   484         break;
       
   485         }
       
   486     case ECollectionContextAlbums:
       
   487         {
       
   488         CMPXCollectionPath* cpath = iCollectionUiHelper->MusicMenuPathL();
       
   489         CleanupStack::PushL( cpath );
       
   490         cpath->AppendL( 3 );
       
   491         iCollectionUtility->Collection().OpenL( *cpath );
       
   492         CleanupStack::PopAndDestroy( cpath );
       
   493         break;
       
   494         }
       
   495     case ECollectionContextPlaylists:
       
   496         {
       
   497         CMPXCollectionPath* cpath = iCollectionUiHelper->MusicPlaylistPathL();
       
   498         CleanupStack::PushL( cpath );
       
   499         iCollectionUtility->Collection().OpenL( *cpath );
       
   500         CleanupStack::PopAndDestroy( cpath );
       
   501         break;
       
   502         }
       
   503     case ECollectionContextGenres:
       
   504         {
       
   505         CMPXCollectionPath* cpath = iCollectionUiHelper->MusicMenuPathL();
       
   506         CleanupStack::PushL( cpath );
       
   507         // Temp
       
   508         cpath->AppendL( 5 );
       
   509         iCollectionUtility->Collection().OpenL( *cpath );
       
   510         CleanupStack::PopAndDestroy( cpath );
       
   511         break;
       
   512         }
       
   513     default:
       
   514         TX_LOG_ARGS( "Error: Unexpected context; should never get here." );
       
   515         break;
       
   516     }
       
   517     TX_EXIT
       
   518 }
       
   519 
       
   520 /*!
       
   521  \internal
       
   522  */
       
   523 void MpMpxCollectionFrameworkWrapperPrivate::DoOpenCollectionItemL( TInt aIndex )
       
   524     {
       
   525     TX_ENTRY_ARGS( "aIndex=" << aIndex );
       
   526     iCollectionUtility->Collection().OpenL( aIndex );
       
   527     TX_EXIT
       
   528     }
       
   529 
       
   530 /*!
       
   531  \internal
       
   532  */
       
   533 void MpMpxCollectionFrameworkWrapperPrivate::DoIncrementalOpenL()
       
   534 {
       
   535     TX_ENTRY
       
   536     // Cancel any reads
       
   537     iIncrementalOpenUtil->Stop();
       
   538 
       
   539     // Start the read
       
   540     iFirstIncrementalOpen = ETrue;
       
   541     RArray<TMPXAttribute> attrs;
       
   542     CleanupClosePushL( attrs );
       
   543     TArray<TMPXAttribute> ary = attrs.Array();
       
   544     iIncrementalOpenUtil->SetDelay( KIncrementalDelayNone );
       
   545     iIncrementalOpenUtil->StartL( ary, KIncrementalFetchBlockSize,
       
   546                                   KErrNotFound, CMPXCollectionOpenUtility::EFetchNormal );
       
   547     iIncrementalOpenUtil->SetDelay( KIncrementalDelayHalfSecond );
       
   548     CleanupStack::PopAndDestroy( &attrs );
       
   549     TX_EXIT
       
   550 }
       
   551 
       
   552 /*!
       
   553  \internal
       
   554  */
       
   555 void MpMpxCollectionFrameworkWrapperPrivate::DoReopenCollectionL()
       
   556 {
       
   557     TX_ENTRY
       
   558     CMPXCollectionPath* cpath = iCollectionUtility->Collection().PathL();
       
   559     CleanupStack::PushL( cpath );
       
   560     cpath->Back();
       
   561     iCollectionUtility->Collection().OpenL( *cpath );
       
   562     CleanupStack::PopAndDestroy( cpath );
       
   563     TX_EXIT
       
   564 }
       
   565 
       
   566 /*!
       
   567  \internal
       
   568  */
       
   569 void MpMpxCollectionFrameworkWrapperPrivate::DoBackL()
       
   570 {
       
   571     TX_ENTRY
       
   572     iCollectionUtility->Collection().BackL();
       
   573     TX_EXIT
       
   574 }
       
   575 
       
   576 /*!
       
   577  \internal
       
   578  */
       
   579 void MpMpxCollectionFrameworkWrapperPrivate::DoFindPlaylistsL( QStringList &playlists )
       
   580 {
       
   581     TX_ENTRY
       
   582     delete iUserPlaylists;
       
   583     iUserPlaylists = NULL;
       
   584 
       
   585     RArray<TMPXAttribute> attrs;
       
   586     CleanupClosePushL( attrs );
       
   587     attrs.Append( KMPXMediaGeneralId );
       
   588     attrs.Append( KMPXMediaGeneralTitle );
       
   589 
       
   590     CMPXMedia* criteria = CMPXMedia::NewL();
       
   591     CleanupStack::PushL( criteria );
       
   592     criteria->SetTObjectValueL<TMPXGeneralType>( 
       
   593         KMPXMediaGeneralType, EMPXGroup );
       
   594     criteria->SetTObjectValueL<TMPXGeneralCategory>( 
       
   595         KMPXMediaGeneralCategory, EMPXPlaylist );
       
   596 
       
   597     // Look up collection UID and set to criteria
       
   598     RArray<TUid> ary;
       
   599     CleanupClosePushL( ary );
       
   600     ary.AppendL( TUid::Uid( EMPXCollectionPluginMusic ) );
       
   601     TUid musicCollection = iCollectionUtility->CollectionIDL( ary.Array() );
       
   602     CleanupStack::PopAndDestroy( &ary );
       
   603 
       
   604     criteria->SetTObjectValueL<TUid>( KMPXMediaGeneralCollectionId, musicCollection );
       
   605 
       
   606     iUserPlaylists = iCollectionUtility->Collection().FindAllL( *criteria, attrs.Array() );
       
   607     CleanupStack::PopAndDestroy( criteria );
       
   608     CleanupStack::PopAndDestroy( &attrs );
       
   609 
       
   610     if ( iUserPlaylists ) {
       
   611         const CMPXMediaArray* mediaArray =
       
   612             iUserPlaylists->Value<CMPXMediaArray>( KMPXMediaArrayContents );
       
   613         User::LeaveIfNull( const_cast<CMPXMediaArray*>( mediaArray ) );
       
   614         TInt count = mediaArray->Count();
       
   615         for ( TInt i = 0; i < count; i++ ) {
       
   616             CMPXMedia* media( mediaArray->AtL( i ) );
       
   617             const TDesC& titleText = media->ValueText( KMPXMediaGeneralTitle );
       
   618             if ( titleText.Compare( KNullDesC ) != 0 ) {
       
   619             playlists += QString::fromUtf16( titleText.Ptr(), titleText.Length() );
       
   620             }
       
   621         }
       
   622     }
       
   623     TX_EXIT
       
   624 }
       
   625 
       
   626 /*!
       
   627  \internal
       
   628  */
       
   629 void MpMpxCollectionFrameworkWrapperPrivate::DoCreatePlaylistL( QString &playlistName, QList<int> &selection, MpMpxCollectionData* collectionData )
       
   630 {
       
   631     TX_ENTRY_ARGS( "playlistName=" << playlistName );
       
   632     CMPXMedia* tracks = CMPXMedia::NewL();
       
   633     CleanupStack::PushL( tracks );
       
   634 
       
   635     TPtrC ptr( reinterpret_cast<const TText*>( playlistName.constData() ) );
       
   636     tracks->SetTextValueL( KMPXMediaGeneralTitle, ptr );
       
   637     tracks->SetTextValueL( KMPXMediaGeneralUri, KPlaylistPath );
       
   638     //if collection data is not provided we use the browsing collection.
       
   639     PreparePlaylistMediaL( *tracks, selection, collectionData ? collectionData : iCollectionData );
       
   640 
       
   641     iCollectionUiHelper->IncAddL( *tracks, this, KMPXChunkSize );
       
   642     CleanupStack::PopAndDestroy( tracks );
       
   643     TX_EXIT
       
   644 }
       
   645 
       
   646 /*!
       
   647  \internal
       
   648  */
       
   649 void MpMpxCollectionFrameworkWrapperPrivate::DoSaveToPlaylistL( TMPXItemId playlistId, QList<int> &selection, MpMpxCollectionData *collectionData )
       
   650 {
       
   651     TX_ENTRY_ARGS( "playlistId=" << int( playlistId ) );
       
   652     
       
   653     CMPXMedia* tracks = CMPXMedia::NewL();
       
   654     CleanupStack::PushL( tracks );
       
   655 
       
   656     RArray<TUid> ary;
       
   657     CleanupClosePushL( ary );
       
   658     ary.AppendL( TUid::Uid( EMPXCollectionPluginMusic ) );
       
   659     TUid musicCollection = iCollectionUtility->CollectionIDL( ary.Array() );
       
   660     CleanupStack::PopAndDestroy( &ary );
       
   661 
       
   662     tracks->SetTObjectValueL<TMPXItemId>( KMPXMediaGeneralId, playlistId );
       
   663     tracks->SetTObjectValueL<TUid>( KMPXMediaGeneralCollectionId, musicCollection );
       
   664     PreparePlaylistMediaL( *tracks, selection, collectionData );
       
   665 
       
   666     iCollectionUiHelper->IncAddL( *tracks, this, KMPXChunkSize );
       
   667     CleanupStack::PopAndDestroy( tracks );
       
   668     TX_EXIT
       
   669 }
       
   670 
       
   671 /*!
       
   672  \internal
       
   673  */
       
   674 void MpMpxCollectionFrameworkWrapperPrivate::DoSaveToPlaylistL( int playlistIndex, QList<int> &selection )
       
   675 {
       
   676     TX_ENTRY_ARGS( "playlistIndex=" << playlistIndex );
       
   677 
       
   678     const CMPXMediaArray* mediaArray =  iUserPlaylists->Value<CMPXMediaArray>( KMPXMediaArrayContents );
       
   679     User::LeaveIfNull( const_cast<CMPXMediaArray*>( mediaArray ) );
       
   680     CMPXMedia* media( mediaArray->AtL( playlistIndex ) );
       
   681     TMPXItemId playlistId = media->ValueTObjectL<TMPXItemId>( KMPXMediaGeneralId );
       
   682     DoSaveToPlaylistL( playlistId, selection, iCollectionData );
       
   683     TX_EXIT
       
   684 }
       
   685 
       
   686 /*!
       
   687  \internal
       
   688  */
       
   689 void MpMpxCollectionFrameworkWrapperPrivate::DoSaveToCurrentPlaylistL( QList<int> &selection, MpMpxCollectionData *collectionData )
       
   690 {
       
   691     TX_ENTRY
       
   692 
       
   693     const CMPXMedia& container = iCollectionData->containerMedia();
       
   694     if ( container.ValueTObjectL<TMPXGeneralType>(KMPXMediaGeneralType) != EMPXItem && 
       
   695         container.ValueTObjectL<TMPXGeneralCategory>(KMPXMediaGeneralCategory) != EMPXPlaylist) {
       
   696         User::Leave( KErrArgument );
       
   697     }
       
   698     TMPXItemId playlistId( container.ValueTObjectL<TMPXItemId>( KMPXMediaGeneralId ) );
       
   699     DoSaveToPlaylistL( playlistId, selection, collectionData );
       
   700     TX_EXIT
       
   701 }
       
   702 
       
   703 /*!
       
   704  \internal
       
   705  */
       
   706 void MpMpxCollectionFrameworkWrapperPrivate::DoDeleteSongsL( QList<int> &selection )
       
   707 {
       
   708     int count = selection.count();
       
   709     TX_ENTRY_ARGS( "selection count=" << count );
       
   710 
       
   711     CMPXCollectionPath* path( iCollectionUtility->Collection().PathL() );
       
   712     CleanupStack::PushL( path );
       
   713 
       
   714     for ( TInt i = 0; i < count; i++ ) {
       
   715         path->SelectL( selection.at( i ) );
       
   716     }
       
   717     iCollectionUiHelper->DeleteL( *path, this );
       
   718     CleanupStack::PopAndDestroy( path );
       
   719     TX_EXIT
       
   720 }
       
   721 
       
   722 /*!
       
   723  \internal
       
   724  */
       
   725 void MpMpxCollectionFrameworkWrapperPrivate::DoRenamePlaylistL( QString &newName, int index )
       
   726 {
       
   727     TX_ENTRY
       
   728     CMPXMediaArray *mediaArray;  
       
   729     const CMPXMedia& container = iCollectionData->containerMedia();
       
   730     mediaArray = const_cast<CMPXMediaArray*>( container.Value<CMPXMediaArray>( KMPXMediaArrayContents ) );
       
   731     CMPXMedia* currentPlaylistMedia( mediaArray->AtL( index ) );
       
   732     TMPXItemId id( currentPlaylistMedia->ValueTObjectL<TMPXItemId>( KMPXMediaGeneralId ) );
       
   733     DoRenamePlaylistL( id, newName );
       
   734     TX_EXIT
       
   735 }
       
   736 
       
   737 /*!
       
   738  \internal
       
   739  */
       
   740 void MpMpxCollectionFrameworkWrapperPrivate::DoRenamePlaylistL( QString &newName )
       
   741 {
       
   742     TX_ENTRY
       
   743     const CMPXMedia& container = iCollectionData->containerMedia();
       
   744     TMPXItemId id( container.ValueTObjectL<TMPXItemId>( KMPXMediaGeneralId ) );
       
   745     DoRenamePlaylistL( id, newName );
       
   746     TX_EXIT
       
   747 }
       
   748 
       
   749 /*!
       
   750  \internal
       
   751  */
       
   752 void MpMpxCollectionFrameworkWrapperPrivate::DoRenamePlaylistL( TMPXItemId id, QString &newName )
       
   753  {
       
   754      CMPXMedia* media = CMPXMedia::NewL();
       
   755      CleanupStack::PushL( media );
       
   756      media->SetTObjectValueL<TMPXGeneralType>( 
       
   757      KMPXMediaGeneralType, EMPXItem );
       
   758      media->SetTObjectValueL<TMPXGeneralCategory>( 
       
   759      KMPXMediaGeneralCategory, EMPXPlaylist );
       
   760      media->SetTObjectValueL<TMPXItemId>( KMPXMediaGeneralId, id );
       
   761      TPtrC ptr( reinterpret_cast<const TText*>( newName.constData() ) );
       
   762      media->SetTextValueL( KMPXMediaGeneralTitle, ptr );
       
   763      iCollectionUiHelper->RenameL( *media, this );
       
   764      CleanupStack::PopAndDestroy( media );
       
   765  }     
       
   766 
       
   767 /*!
       
   768  \internal
       
   769  */
       
   770 void MpMpxCollectionFrameworkWrapperPrivate::DoSetShuffleL( bool active )
       
   771 {
       
   772     TX_ENTRY
       
   773     if ( iPlaybackUtility ) {
       
   774         iPlaybackUtility->SetL( EPbPropertyRandomMode, active );
       
   775     }
       
   776     TX_EXIT
       
   777 }
       
   778 
       
   779 /*!
       
   780  \internal
       
   781  */
       
   782 void MpMpxCollectionFrameworkWrapperPrivate::DoPreviewItemL( int index )
       
   783 {
       
   784     TX_ENTRY
       
   785     if ( !iPlaybackUtility ) {
       
   786         iPlaybackUtility = MMPXPlaybackUtility::UtilityL( mHostUid );
       
   787     }
       
   788 
       
   789     // Get the current path
       
   790     CMPXCollectionPath* cpath = iCollectionUtility->Collection().PathL();
       
   791     CleanupStack::PushL( cpath );
       
   792     MPX_DEBUG_PATH( *cpath );
       
   793     cpath->Back();
       
   794 
       
   795     CMPXMediaArray *mediaArray;
       
   796     const CMPXMedia& container = iCollectionData->containerMedia();
       
   797     mediaArray = const_cast<CMPXMediaArray*>( container.Value<CMPXMediaArray>( KMPXMediaArrayContents ) );
       
   798     CMPXMedia* currentMedia( mediaArray->AtL( index ) );
       
   799     TMPXItemId id( currentMedia->ValueTObjectL<TMPXItemId>( KMPXMediaGeneralId ) );
       
   800 
       
   801     cpath->AppendL( id ); // Top level items of songs
       
   802     cpath->Set( 0 ); // Select 1st song
       
   803 
       
   804     CMPXCollectionPlaylist* playList = CMPXCollectionPlaylist::NewL( *cpath );
       
   805     CleanupStack::PushL( playList );
       
   806     playList->SetToFirst();
       
   807 
       
   808     iPlaybackUtility->InitL( *playList, ETrue );
       
   809     emit q_ptr->collectionPlaylistOpened();
       
   810 
       
   811     CleanupStack::PopAndDestroy( playList );
       
   812     CleanupStack::PopAndDestroy( cpath );
       
   813     TX_EXIT
       
   814 }
       
   815 
       
   816 /*!
       
   817  \internal
       
   818  */
       
   819 void MpMpxCollectionFrameworkWrapperPrivate::DoOpenIsolatedCollectionL( TCollectionContext context )
       
   820 {
       
   821 
       
   822     if ( ECollectionContextAllSongs == context ) {
       
   823         CMPXCollectionPath* cpath = iCollectionUiHelper->MusicAllSongsPathL();
       
   824         CleanupStack::PushL( cpath );
       
   825         if ( !iIsolatedCollectionHelper ) {
       
   826             iIsolatedCollectionHelper = CMpMpxIsolatedCollectionHelper::NewL( this );
       
   827         }   
       
   828         iIsolatedCollectionHelper->OpenCollectionL( *cpath );
       
   829         CleanupStack::PopAndDestroy( cpath );
       
   830     }
       
   831 }
       
   832 
       
   833 /*!
       
   834  \internal
       
   835  */
       
   836 void MpMpxCollectionFrameworkWrapperPrivate::DoReorderPlaylistL( int playlistId, int songId, int originalOrdinal, int newOrdinal )
       
   837 {
       
   838     iCollectionUiHelper->ReorderPlaylistL( playlistId, songId, originalOrdinal, newOrdinal, this );           
       
   839 }
       
   840 
       
   841 
       
   842 /*!
       
   843  \internal
       
   844  */
       
   845 void MpMpxCollectionFrameworkWrapperPrivate::DoHandleCollectionMessageL( 
       
   846     const CMPXMessage& aMsg )
       
   847 {
       
   848     TX_ENTRY
       
   849     TMPXMessageId id( aMsg.ValueTObjectL<TMPXMessageId>( KMPXMessageGeneralId ) );
       
   850     if ( KMPXMessageGeneral == id ) {
       
   851         TInt event( aMsg.ValueTObjectL<TInt>( KMPXMessageGeneralEvent ) );
       
   852         TInt type( aMsg.ValueTObjectL<TInt>( KMPXMessageGeneralType ) );
       
   853         TInt data( aMsg.ValueTObjectL<TInt>( KMPXMessageGeneralData ) );
       
   854         TX_LOG_ARGS( "event=" << event << ", type=" << type << ", data=" << data );
       
   855 
       
   856         if ( event == TMPXCollectionMessage::EPathChanged &&
       
   857              type == EMcPathChangedByOpen &&
       
   858              data == EMcContainerOpened ) {
       
   859             // Incremental Open when browsing to the next level
       
   860             DoIncrementalOpenL();
       
   861         }
       
   862         else if ( event == TMPXCollectionMessage::EPathChanged &&
       
   863                   type == EMcPathChangedByOpen &&
       
   864                   data == EMcItemOpened ) {
       
   865             // Opened a song
       
   866             // This will result in HandleOpenL with CMPXCollectionPlaylist
       
   867             iCollectionUtility->Collection().OpenL();
       
   868         }
       
   869     }
       
   870     TX_EXIT
       
   871 }
       
   872 
       
   873 
       
   874 /*!
       
   875  \internal
       
   876  */
       
   877 void MpMpxCollectionFrameworkWrapperPrivate::PreparePlaylistMediaL( 
       
   878         CMPXMedia& aMedia,
       
   879         QList<int> &selection,
       
   880         MpMpxCollectionData *collectionData )
       
   881 {
       
   882     int count = selection.count();
       
   883     TX_ENTRY_ARGS( "selection count=" << count );
       
   884 
       
   885     const CMPXMedia& container = collectionData->containerMedia();
       
   886     const CMPXMediaArray* containerArray = container.Value<CMPXMediaArray>( KMPXMediaArrayContents );
       
   887     User::LeaveIfNull( const_cast<CMPXMediaArray*>( containerArray ) );
       
   888     CMPXMediaArray* tracksArray( CMPXMediaArray::NewL() );
       
   889     CleanupStack::PushL( tracksArray );
       
   890 
       
   891     CMPXCollectionPath* path( iCollectionUtility->Collection().PathL() );
       
   892     CleanupStack::PushL( path );
       
   893     TMPXItemId collectionId( path->Id( 0 ) );
       
   894     CleanupStack::PopAndDestroy( path );
       
   895 
       
   896     if ( collectionData->context() == ECollectionContextAlbums ) {
       
   897         for ( TInt i = 0; i < count; i++ ) {
       
   898             CMPXMedia* results;
       
   899             CMPXMedia* album( containerArray->AtL( selection[i] ) );
       
   900             // Fetch the songs for the selected album
       
   901             TMPXItemId albumId = album->ValueTObjectL<TMPXItemId>( KMPXMediaGeneralId );
       
   902             CMPXMedia* findCriteria = CMPXMedia::NewL();
       
   903             CleanupStack::PushL( findCriteria );
       
   904             findCriteria->SetTObjectValueL<TMPXGeneralType>( KMPXMediaGeneralType, EMPXGroup );
       
   905             findCriteria->SetTObjectValueL<TMPXGeneralCategory>( KMPXMediaGeneralCategory, EMPXSong );
       
   906             findCriteria->SetTObjectValueL<TMPXItemId>( KMPXMediaGeneralId, albumId );
       
   907             RArray<TMPXAttribute> attrs;
       
   908             CleanupClosePushL( attrs );
       
   909             attrs.Append( TMPXAttribute( KMPXMediaIdGeneral,
       
   910                                          EMPXMediaGeneralTitle |
       
   911                                          EMPXMediaGeneralId ) );
       
   912             attrs.Append( KMPXMediaMusicAlbumTrack );
       
   913             results = iCollectionUtility->Collection().FindAllL( *findCriteria, attrs.Array() );
       
   914             CleanupStack::PopAndDestroy( &attrs );
       
   915             CleanupStack::PopAndDestroy( findCriteria );
       
   916             CleanupStack::PushL( results );
       
   917             
       
   918             const CMPXMediaArray* resultsArray = results->Value<CMPXMediaArray>( KMPXMediaArrayContents );
       
   919             User::LeaveIfNull( resultsArray );
       
   920             for ( int j = 0 ; j < resultsArray->Count();j++ ) {
       
   921                 CMPXMedia* media( resultsArray->AtL( j ) );
       
   922                 CMPXMedia* entry = CMPXMedia::NewL();
       
   923                 CleanupStack::PushL( entry );
       
   924                 entry->SetTextValueL( KMPXMediaGeneralTitle,
       
   925                     media->ValueText( KMPXMediaGeneralTitle ) );
       
   926                 entry->SetTObjectValueL( KMPXMediaGeneralType, EMPXItem );
       
   927                 entry->SetTObjectValueL( KMPXMediaGeneralCategory, EMPXSong );
       
   928                 entry->SetTObjectValueL( KMPXMediaGeneralId,
       
   929                     media->ValueTObjectL<TMPXItemId>( KMPXMediaGeneralId ) );
       
   930                 entry->SetTObjectValueL( KMPXMediaGeneralCollectionId, collectionId );
       
   931                 CleanupStack::Pop( entry );
       
   932                 tracksArray->AppendL( entry );
       
   933             }
       
   934             CleanupStack::PopAndDestroy( results );
       
   935         }
       
   936         
       
   937     }
       
   938     else {   
       
   939         for ( TInt i = 0; i < count; i++ ) {
       
   940             CMPXMedia* media( containerArray->AtL( selection.at( i ) ) );
       
   941             CMPXMedia* entry = CMPXMedia::NewL();
       
   942             CleanupStack::PushL( entry );
       
   943             entry->SetTextValueL( KMPXMediaGeneralTitle,
       
   944                 media->ValueText( KMPXMediaGeneralTitle ) );
       
   945             entry->SetTObjectValueL( KMPXMediaGeneralType, EMPXItem );
       
   946             entry->SetTObjectValueL( KMPXMediaGeneralCategory, EMPXSong );
       
   947             entry->SetTObjectValueL( KMPXMediaGeneralId,
       
   948                 media->ValueTObjectL<TMPXItemId>( KMPXMediaGeneralId ) );
       
   949             entry->SetTObjectValueL( KMPXMediaGeneralCollectionId, collectionId );
       
   950             CleanupStack::Pop( entry );
       
   951             tracksArray->AppendL( entry );
       
   952         }
       
   953     }
       
   954     aMedia.SetTObjectValueL<TMPXGeneralType>( KMPXMediaGeneralType, EMPXItem );
       
   955     aMedia.SetTObjectValueL<TMPXGeneralCategory>( KMPXMediaGeneralCategory, EMPXPlaylist );
       
   956     aMedia.SetCObjectValueL( KMPXMediaArrayContents, tracksArray );
       
   957     aMedia.SetTObjectValueL( KMPXMediaArrayCount, tracksArray->Count() );
       
   958 
       
   959     CleanupStack::PopAndDestroy( tracksArray );
       
   960     TX_EXIT
       
   961 }