mpxplugins/viewplugins/views/collectionviewhg/src/mpxcollectionviewhgplaylisthelper.cpp
branchRCL_3
changeset 53 3de6c4cf6b67
child 66 1f1dad4af8f8
equal deleted inserted replaced
52:14979e23cb5e 53:3de6c4cf6b67
       
     1 /*
       
     2 * Copyright (c) 2006 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:  Helper to construct a collection playlist.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <bldvariant.hrh>
       
    21 #include <s32file.h>
       
    22 #include <e32math.h>
       
    23 #include <mpxcollectionframeworkdefs.h>
       
    24 #include <mpxcollectionmessage.h>
       
    25 #include <mpxcollectionutility.h>
       
    26 #include <mpxcollectionplaylist.h>
       
    27 #include <mpxcollectionpath.h>
       
    28 #include <mpxcollectionuihelper.h>
       
    29 #include <mpxplaybackutility.h>
       
    30 #include <mpxplaybackmessage.h>
       
    31 #include <mpxmessagegeneraldefs.h>
       
    32 #include <mpxplaybackmessagedefs.h>
       
    33 #include <mpxcollectionopenutility.h>
       
    34 #include <mpxconstants.h>
       
    35 
       
    36 #include "mpxcollectionviewhgplaylisthelper.h"
       
    37 #include "mpxlog.h"
       
    38 
       
    39 // CONSTANTS
       
    40 const TInt KIncrementalFetchCount = 400;
       
    41 const TInt KIncrementalNullOffset = 0;
       
    42 
       
    43 // ============================ MEMBER FUNCTIONS ==============================
       
    44 
       
    45 // ----------------------------------------------------------------------------
       
    46 // Two-phased constructor.
       
    47 // ----------------------------------------------------------------------------
       
    48 //
       
    49 CMPXCollectionViewHgPlaylistHelper* CMPXCollectionViewHgPlaylistHelper::NewL(
       
    50 	MMPXPlaybackUtility* aPlaybackUtility,
       
    51 	MMPXCollectionUiHelper* aCollectionUiHelper)
       
    52     {
       
    53     CMPXCollectionViewHgPlaylistHelper* self = NewLC(aPlaybackUtility, aCollectionUiHelper);
       
    54     CleanupStack::Pop(self);
       
    55     return self;
       
    56     }
       
    57 
       
    58 // ----------------------------------------------------------------------------
       
    59 // Two-phased constructor.
       
    60 // ----------------------------------------------------------------------------
       
    61 //
       
    62 CMPXCollectionViewHgPlaylistHelper* CMPXCollectionViewHgPlaylistHelper::NewLC(
       
    63 	MMPXPlaybackUtility* aPlaybackUtility,
       
    64 	MMPXCollectionUiHelper* aCollectionUiHelper )
       
    65     {
       
    66     CMPXCollectionViewHgPlaylistHelper* self =
       
    67     	new (ELeave) CMPXCollectionViewHgPlaylistHelper(aPlaybackUtility,
       
    68                                                          aCollectionUiHelper);
       
    69     CleanupStack::PushL(self);
       
    70     self->ConstructL();
       
    71     return self;
       
    72     }
       
    73 
       
    74 // ----------------------------------------------------------------------------
       
    75 // C++ constructor can NOT contain any code that might leave.
       
    76 // ----------------------------------------------------------------------------
       
    77 //
       
    78 CMPXCollectionViewHgPlaylistHelper::CMPXCollectionViewHgPlaylistHelper(
       
    79 	MMPXPlaybackUtility* aPlaybackUtility,
       
    80 	MMPXCollectionUiHelper* aCollectionUiHelper )
       
    81 	: iPlaybackUtility(aPlaybackUtility),
       
    82 	  iCollectionUiHelper( aCollectionUiHelper ),
       
    83 	  iPathInitialized(0),
       
    84 	  iIsInitializing(EFalse),
       
    85 	  iInitCanceled(EFalse),
       
    86 	  iLoadSavedPath(EFalse),
       
    87 	  iEnableShuffle(EFalse)
       
    88     {
       
    89     }
       
    90 
       
    91 // ----------------------------------------------------------------------------
       
    92 // Destructor.
       
    93 // ----------------------------------------------------------------------------
       
    94 //
       
    95 CMPXCollectionViewHgPlaylistHelper::~CMPXCollectionViewHgPlaylistHelper()
       
    96     {
       
    97     if ( iCollectionUtility )
       
    98         {
       
    99         iCollectionUtility->Close();
       
   100         }
       
   101 
       
   102     delete iIncOpenUtil;
       
   103     }
       
   104 
       
   105 // ----------------------------------------------------------------------------
       
   106 // Symbian 2nd phase constructor can leave.
       
   107 // ----------------------------------------------------------------------------
       
   108 //
       
   109 void CMPXCollectionViewHgPlaylistHelper::ConstructL()
       
   110     {
       
   111     iCollectionUtility = MMPXCollectionUtility::NewL( this, KMcModeIsolated );
       
   112     iIncOpenUtil = CMPXCollectionOpenUtility::NewL( this, KMcModeIsolated );
       
   113     }
       
   114 
       
   115 
       
   116 // ---------------------------------------------------------------------------
       
   117 // Create the default path and load it in collection
       
   118 // ---------------------------------------------------------------------------
       
   119 //
       
   120 void CMPXCollectionViewHgPlaylistHelper::LoadDefaultPathL( TInt aDelay )
       
   121     {
       
   122     MPX_DEBUG1("CMPXCollectionViewHgPlaylistHelper::LoadDefaultPathL");
       
   123     CMPXCollectionPath* cpath = iCollectionUiHelper->CreateDefaultPlaylistPathLC();
       
   124 
       
   125     if (!iIsInitializing)
       
   126         {
       
   127         // Get incremental utility to open the path
       
   128         //
       
   129         DoIncrementalOpenL( *cpath, KIncrementalNullOffset, aDelay );
       
   130 
       
   131         iPathInitialized = EFalse;
       
   132         iIsInitializing = ETrue;
       
   133         iInitCanceled = EFalse;
       
   134         iDefaultPath = ETrue;
       
   135         iSelectionId = KMPXInvalidItemId;
       
   136         }
       
   137     CleanupStack::PopAndDestroy( cpath );
       
   138     }
       
   139 
       
   140 // ---------------------------------------------------------------------------
       
   141 // Create playlist from default path.
       
   142 // ---------------------------------------------------------------------------
       
   143 //
       
   144 void CMPXCollectionViewHgPlaylistHelper::InitPlaylistL(TBool aEnableShuffle)
       
   145     {
       
   146     MPX_DEBUG1("CMPXCollectionViewHgPlaylistHelper::InitPlaylistL");
       
   147     CMPXCollectionPath* cpath = iCollectionUiHelper->CreateDefaultPlaylistPathLC();
       
   148 
       
   149     if (!iIsInitializing)
       
   150         {
       
   151         // Get incremental utility to open the path
       
   152         //
       
   153         DoIncrementalOpenL( *cpath, KIncrementalNullOffset, 0 );
       
   154 
       
   155         iPathInitialized = EFalse;
       
   156         iIsInitializing = ETrue;
       
   157         iInitCanceled = EFalse;
       
   158         iDefaultPath = ETrue;
       
   159         iSelectionId = KMPXInvalidItemId;
       
   160         iEnableShuffle = aEnableShuffle;
       
   161         }
       
   162     CleanupStack::PopAndDestroy( cpath );
       
   163     }
       
   164 
       
   165 // ---------------------------------------------------------------------------
       
   166 // Create playlist from provided path
       
   167 // ---------------------------------------------------------------------------
       
   168 //
       
   169 void CMPXCollectionViewHgPlaylistHelper::InitPlaylistL(CMPXCollectionPath& aPath, TBool aEnableShuffle)
       
   170     {
       
   171     MPX_DEBUG1("CMPXCollectionViewHgPlaylistHelper::InitPlaylistL");
       
   172 
       
   173     if (!iIsInitializing)
       
   174         {
       
   175 		if (aEnableShuffle)
       
   176 			{
       
   177 			TTime time;
       
   178 			time.UniversalTime();
       
   179 			TInt64 seed = time.Int64();
       
   180 			TInt randIndex = Math::Rand(seed) % aPath.Count();
       
   181 			aPath.Set(randIndex);
       
   182 			}
       
   183 
       
   184         CMPXCollectionPlaylist* playlist = CMPXCollectionPlaylist::NewL( aPath );
       
   185         CleanupStack::PushL( playlist );
       
   186 
       
   187         iPlaybackUtility->SetL( EPbPropertyRandomMode, aEnableShuffle );
       
   188         iPlaybackUtility->InitL( *playlist, ETrue );
       
   189         CleanupStack::PopAndDestroy( playlist );
       
   190 
       
   191 		//iPlaybackUtility->SetL( EPbPropertyRandomMode, aEnableShuffle );
       
   192 		//iCollectionUtility->Collection().OpenL(aPath);
       
   193 
       
   194         iPathInitialized = ETrue;
       
   195         iInitCanceled = EFalse;
       
   196         iDefaultPath = EFalse;
       
   197         iSelectionId = KMPXInvalidItemId;
       
   198         }
       
   199     }
       
   200 
       
   201 
       
   202 // ---------------------------------------------------------------------------
       
   203 // Check the initialized flag
       
   204 // ---------------------------------------------------------------------------
       
   205 //
       
   206 TBool CMPXCollectionViewHgPlaylistHelper::Initialized()
       
   207     {
       
   208     return iPathInitialized;
       
   209     }
       
   210 
       
   211 // ---------------------------------------------------------------------------
       
   212 // Check the iIsInitializing flag
       
   213 // ---------------------------------------------------------------------------
       
   214 //
       
   215 TBool CMPXCollectionViewHgPlaylistHelper::IsInitializing()
       
   216     {
       
   217     return iIsInitializing;
       
   218     }
       
   219 
       
   220 // ---------------------------------------------------------------------------
       
   221 // Set the initialized flag
       
   222 // ---------------------------------------------------------------------------
       
   223 //
       
   224 void CMPXCollectionViewHgPlaylistHelper::SetInitialized( TBool aInit )
       
   225     {
       
   226     iPathInitialized = aInit;
       
   227     iIsInitializing = EFalse;
       
   228     }
       
   229 
       
   230 // ---------------------------------------------------------------------------
       
   231 // Cancels the initialize operation
       
   232 // ---------------------------------------------------------------------------
       
   233 //
       
   234 void CMPXCollectionViewHgPlaylistHelper::CancelInit()
       
   235     {
       
   236     iInitCanceled = ETrue;
       
   237     SetInitialized( ETrue );
       
   238     }
       
   239 
       
   240 // ---------------------------------------------------------------------------
       
   241 // From MMPXCollectionObserver.
       
   242 // Handle media properties.
       
   243 // ---------------------------------------------------------------------------
       
   244 //
       
   245 void CMPXCollectionViewHgPlaylistHelper::HandleCollectionMediaL(
       
   246     const CMPXMedia& /*aMedia*/,
       
   247     TInt /*aError*/ )
       
   248     {
       
   249     // Do nothing
       
   250     }
       
   251 
       
   252 // ---------------------------------------------------------------------------
       
   253 // From MMPXCollectionObserver.
       
   254 // Handle collection message.
       
   255 // ---------------------------------------------------------------------------
       
   256 //
       
   257 void CMPXCollectionViewHgPlaylistHelper::HandleCollectionMessage(
       
   258     CMPXMessage* aMessage, TInt aError )
       
   259     {
       
   260     if ( aError == KErrNone && aMessage )
       
   261         {
       
   262         TRAP_IGNORE( DoHandleCollectionMessageL( *aMessage ) );
       
   263         }
       
   264     }
       
   265 
       
   266 // ---------------------------------------------------------------------------
       
   267 // From MMPXCollectionObserver.
       
   268 // Handles the collection entries being opened.
       
   269 // ---------------------------------------------------------------------------
       
   270 //
       
   271 void CMPXCollectionViewHgPlaylistHelper::HandleOpenL(
       
   272     const CMPXMedia& /* aEntries */,
       
   273     TInt /* aIndex */,
       
   274     TBool /* aComplete */,
       
   275     TInt aError )
       
   276     {
       
   277     // OpenL() callback from trying to open the default Music Menu -> All Songs
       
   278     // or from opening a container
       
   279     //
       
   280     MPX_DEBUG1("CMPXCollectionViewHgPlaylistHelper::HandleOpenL -- Passing path to playback engine");
       
   281 
       
   282     // Stop the incremental open algorithm. We are only interested in fetcing
       
   283     // a single block containing the item we want to play. This will speed up the
       
   284     // initalization procedure. Once the playlist is created, the playlist
       
   285     // takes over the rest of the incremental open operation
       
   286     //
       
   287     iIncOpenUtil->Stop();
       
   288     if ( aError == KErrNone )
       
   289         {
       
   290 
       
   291         CMPXCollectionPath* cPath = iIncOpenUtil->PathL();
       
   292         CleanupStack::PushL( cPath );
       
   293 		InitPlaybackEngineL( *cPath );
       
   294         CleanupStack::PopAndDestroy( cPath );
       
   295         iIsInitializing = EFalse;
       
   296         iPathInitialized = ETrue;
       
   297         iInitCanceled = EFalse;
       
   298         }
       
   299     else
       
   300         {
       
   301         if( iDefaultPath )
       
   302             {
       
   303             MPX_DEBUG1("CMPXCollectionViewHgPlaylistHelper::HandleOpenL - Failed to open default path");
       
   304             // Failed to open default path, set to initialized
       
   305             // "No songs view"
       
   306             //
       
   307             iDefaultPath = EFalse;
       
   308             iPathInitialized = ETrue;
       
   309             iIsInitializing = EFalse;
       
   310             }
       
   311         else
       
   312             {
       
   313             MPX_DEBUG1("CMPXCollectionViewHgPlaylistHelper::HandleOpenL - Opening default Path");
       
   314             iIsInitializing = EFalse;
       
   315             // Try opening default
       
   316             LoadDefaultPathL();
       
   317             }
       
   318         }
       
   319     }
       
   320 
       
   321 // ---------------------------------------------------------------------------
       
   322 // Handle playback message.
       
   323 // ---------------------------------------------------------------------------
       
   324 //
       
   325 void CMPXCollectionViewHgPlaylistHelper::HandlePlaybackMessage(
       
   326     CMPXMessage* aMessage, TInt aError )
       
   327     {
       
   328     if ( aError == KErrNone && aMessage )
       
   329         {
       
   330         TRAP_IGNORE( DoHandlePlaybackMessageL( *aMessage ) );
       
   331         }
       
   332     }
       
   333 
       
   334 
       
   335 // ---------------------------------------------------------------------------
       
   336 // From MMPXCollectionObserver.
       
   337 // Handles the item being opened.
       
   338 // ---------------------------------------------------------------------------
       
   339 //
       
   340 void CMPXCollectionViewHgPlaylistHelper::HandleOpenL(
       
   341     const CMPXCollectionPlaylist& aPlaylist,
       
   342     TInt aError )
       
   343     {
       
   344     MPX_FUNC("CMPXCollectionViewHgPlaylistHelper::HandleOpenL");
       
   345     if( aError == KErrNone )
       
   346         {
       
   347         if ( !iInitCanceled )
       
   348             {
       
   349             MPX_DEBUG1( "CMPXCollectionViewHgPlaylistHelper::HandleOpenL - Opening playlist" );
       
   350 
       
   351             // toggle repeat and shuffle to off if podcast playlist
       
   352             if(KMPXUidPodcastDBPlugin == TUid::Uid((TInt)aPlaylist.Path().Id(0)))
       
   353                 {
       
   354                 CMPXCollectionPlaylist* tmp =
       
   355                                 CMPXCollectionPlaylist::NewL( aPlaylist );
       
   356                 CleanupStack::PushL( tmp );
       
   357                 tmp->SetRepeatEnabled( EFalse );
       
   358                 tmp->SetShuffleEnabledL( EFalse );
       
   359                 iPlaybackUtility->InitL( *tmp, EFalse );
       
   360                 CleanupStack::PopAndDestroy( tmp );
       
   361                 }
       
   362             else
       
   363                 {
       
   364                 iPlaybackUtility->InitL( aPlaylist, EFalse );
       
   365                 }
       
   366             }
       
   367         else
       
   368             {
       
   369             MPX_DEBUG1( "CMPXCollectionViewHgPlaylistHelper::HandleOpenL - Init canceled, do nothing" );
       
   370             }
       
   371         }
       
   372     else
       
   373         {
       
   374         if( iDefaultPath )
       
   375             {
       
   376             // Failed to open default path, set to initialized
       
   377             // "No songs view"
       
   378             //
       
   379             iDefaultPath = EFalse;
       
   380             iPathInitialized = ETrue;
       
   381             iIsInitializing = EFalse;
       
   382             }
       
   383         else
       
   384             {
       
   385             // Try opening default
       
   386             LoadDefaultPathL();
       
   387             }
       
   388         }
       
   389     }
       
   390 
       
   391 // ---------------------------------------------------------------------------
       
   392 // Initialize the playback engine with a collection path
       
   393 // ---------------------------------------------------------------------------
       
   394 //
       
   395 void CMPXCollectionViewHgPlaylistHelper::InitPlaybackEngineL( CMPXCollectionPath& aPath )
       
   396     {
       
   397     MPX_FUNC( "CMPXCollectionViewHgPlaylistHelper::InitPlaybackEngineL" );
       
   398 
       
   399     if( !iInitCanceled )
       
   400         {
       
   401         TTime time;
       
   402         time.UniversalTime();
       
   403         TInt64 seed = time.Int64();
       
   404         TInt randIndex = Math::Rand(seed) % aPath.Count();
       
   405         aPath.Set(randIndex);
       
   406         CMPXCollectionPlaylist* playlist = CMPXCollectionPlaylist::NewL( aPath );
       
   407         CleanupStack::PushL( playlist );
       
   408         playlist->SetShuffleEnabledL( ETrue );
       
   409         iPlaybackUtility->SetL( EPbPropertyRandomMode, iEnableShuffle );
       
   410         iPlaybackUtility->InitL( *playlist, ETrue );
       
   411         CleanupStack::PopAndDestroy( playlist );
       
   412         }
       
   413     else
       
   414         {
       
   415         iIsInitializing = EFalse;
       
   416         iPathInitialized = ETrue;
       
   417         iInitCanceled = EFalse;
       
   418         }
       
   419     }
       
   420 
       
   421 // -----------------------------------------------------------------------------
       
   422 // Handle playback message
       
   423 // -----------------------------------------------------------------------------
       
   424 //
       
   425 void CMPXCollectionViewHgPlaylistHelper::DoHandlePlaybackMessageL( const CMPXMessage& aMessage )
       
   426     {
       
   427     MPX_FUNC( "CMPXCollectionViewHgPlaylistHelper::DoHandlePlaybackMessageL" );
       
   428     TMPXMessageId id( aMessage.ValueTObjectL<TMPXMessageId>( KMPXMessageGeneralId ) );
       
   429     if ( KMPXMessageGeneral == id )
       
   430         {
       
   431         TInt event( aMessage.ValueTObjectL<TInt>( KMPXMessageGeneralEvent ) );
       
   432         MPX_DEBUG2( "CMPXCollectionViewHgPlaylistHelper::DoHandlePlaybackMessageL(%d)", event );
       
   433 
       
   434         switch ( event )
       
   435             {
       
   436             default:
       
   437                 {
       
   438                 // ignore other messages
       
   439                 break;
       
   440                 }
       
   441             }
       
   442         }
       
   443     }
       
   444 
       
   445 // -----------------------------------------------------------------------------
       
   446 // Handle collection message
       
   447 // -----------------------------------------------------------------------------
       
   448 //
       
   449 void CMPXCollectionViewHgPlaylistHelper::DoHandleCollectionMessageL(
       
   450 	const CMPXMessage& /*aMessage*/ )
       
   451     {
       
   452     MPX_FUNC( "CMPXCollectionViewHgPlaylistHelper::DoHandleCollectionMessageL" );
       
   453 
       
   454     }
       
   455 
       
   456 // -----------------------------------------------------------------------------
       
   457 // Start the incremental reading operation at an offset
       
   458 // -----------------------------------------------------------------------------
       
   459 //
       
   460 void CMPXCollectionViewHgPlaylistHelper::DoIncrementalOpenL( CMPXCollectionPath& aPath,
       
   461                                           TInt aIndex,
       
   462                                           TInt aDelay )
       
   463     {
       
   464     RArray<TMPXAttribute> attrs;
       
   465     CleanupClosePushL( attrs );
       
   466     TArray<TMPXAttribute> ary = attrs.Array();
       
   467     iIncOpenUtil->Stop();
       
   468     iIncOpenUtil->SetDelay( aDelay );
       
   469     iIncOpenUtil->StartL( aPath, ary, KIncrementalFetchCount,
       
   470                           aIndex, CMPXCollectionOpenUtility::EFetchNormal );
       
   471     CleanupStack::PopAndDestroy( &attrs );
       
   472     }
       
   473 
       
   474 
       
   475 // End of File