mpxplugins/viewplugins/views/collectionviewhg/src/mpxcollectionviewhgplaylisthelper.cpp
changeset 0 ff3acec5bc43
child 11 13afc0e517bd
equal deleted inserted replaced
-1:000000000000 0:ff3acec5bc43
       
     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         playlist->SetShuffleEnabledL( aEnableShuffle );
       
   188         iPlaybackUtility->SetL( EPbPropertyRandomMode, aEnableShuffle );
       
   189         iPlaybackUtility->InitL( *playlist, ETrue );
       
   190         CleanupStack::PopAndDestroy( playlist );
       
   191 
       
   192 		//iPlaybackUtility->SetL( EPbPropertyRandomMode, aEnableShuffle );
       
   193 		//iCollectionUtility->Collection().OpenL(aPath);
       
   194 
       
   195         iPathInitialized = ETrue;
       
   196         iInitCanceled = EFalse;
       
   197         iDefaultPath = EFalse;
       
   198         iSelectionId = KMPXInvalidItemId;
       
   199         }
       
   200     }
       
   201 
       
   202 
       
   203 // ---------------------------------------------------------------------------
       
   204 // Check the initialized flag
       
   205 // ---------------------------------------------------------------------------
       
   206 //
       
   207 TBool CMPXCollectionViewHgPlaylistHelper::Initialized()
       
   208     {
       
   209     return iPathInitialized;
       
   210     }
       
   211 
       
   212 // ---------------------------------------------------------------------------
       
   213 // Check the iIsInitializing flag
       
   214 // ---------------------------------------------------------------------------
       
   215 //
       
   216 TBool CMPXCollectionViewHgPlaylistHelper::IsInitializing()
       
   217     {
       
   218     return iIsInitializing;
       
   219     }
       
   220 
       
   221 // ---------------------------------------------------------------------------
       
   222 // Set the initialized flag
       
   223 // ---------------------------------------------------------------------------
       
   224 //
       
   225 void CMPXCollectionViewHgPlaylistHelper::SetInitialized( TBool aInit )
       
   226     {
       
   227     iPathInitialized = aInit;
       
   228     iIsInitializing = EFalse;
       
   229     }
       
   230 
       
   231 // ---------------------------------------------------------------------------
       
   232 // Cancels the initialize operation
       
   233 // ---------------------------------------------------------------------------
       
   234 //
       
   235 void CMPXCollectionViewHgPlaylistHelper::CancelInit()
       
   236     {
       
   237     iInitCanceled = ETrue;
       
   238     SetInitialized( ETrue );
       
   239     }
       
   240 
       
   241 // ---------------------------------------------------------------------------
       
   242 // From MMPXCollectionObserver.
       
   243 // Handle media properties.
       
   244 // ---------------------------------------------------------------------------
       
   245 //
       
   246 void CMPXCollectionViewHgPlaylistHelper::HandleCollectionMediaL(
       
   247     const CMPXMedia& /*aMedia*/,
       
   248     TInt /*aError*/ )
       
   249     {
       
   250     // Do nothing
       
   251     }
       
   252 
       
   253 // ---------------------------------------------------------------------------
       
   254 // From MMPXCollectionObserver.
       
   255 // Handle collection message.
       
   256 // ---------------------------------------------------------------------------
       
   257 //
       
   258 void CMPXCollectionViewHgPlaylistHelper::HandleCollectionMessage(
       
   259     CMPXMessage* aMessage, TInt aError )
       
   260     {
       
   261     if ( aError == KErrNone && aMessage )
       
   262         {
       
   263         TRAP_IGNORE( DoHandleCollectionMessageL( *aMessage ) );
       
   264         }
       
   265     }
       
   266 
       
   267 // ---------------------------------------------------------------------------
       
   268 // From MMPXCollectionObserver.
       
   269 // Handles the collection entries being opened.
       
   270 // ---------------------------------------------------------------------------
       
   271 //
       
   272 void CMPXCollectionViewHgPlaylistHelper::HandleOpenL(
       
   273     const CMPXMedia& /* aEntries */,
       
   274     TInt /* aIndex */,
       
   275     TBool /* aComplete */,
       
   276     TInt aError )
       
   277     {
       
   278     // OpenL() callback from trying to open the default Music Menu -> All Songs
       
   279     // or from opening a container
       
   280     //
       
   281     MPX_DEBUG1("CMPXCollectionViewHgPlaylistHelper::HandleOpenL -- Passing path to playback engine");
       
   282 
       
   283     // Stop the incremental open algorithm. We are only interested in fetcing
       
   284     // a single block containing the item we want to play. This will speed up the
       
   285     // initalization procedure. Once the playlist is created, the playlist
       
   286     // takes over the rest of the incremental open operation
       
   287     //
       
   288     iIncOpenUtil->Stop();
       
   289     if ( aError == KErrNone )
       
   290         {
       
   291 
       
   292         CMPXCollectionPath* cPath = iIncOpenUtil->PathL();
       
   293         CleanupStack::PushL( cPath );
       
   294 		InitPlaybackEngineL( *cPath );
       
   295         CleanupStack::PopAndDestroy( cPath );
       
   296         iIsInitializing = EFalse;
       
   297         iPathInitialized = ETrue;
       
   298         iInitCanceled = EFalse;
       
   299         }
       
   300     else
       
   301         {
       
   302         if( iDefaultPath )
       
   303             {
       
   304             MPX_DEBUG1("CMPXCollectionViewHgPlaylistHelper::HandleOpenL - Failed to open default path");
       
   305             // Failed to open default path, set to initialized
       
   306             // "No songs view"
       
   307             //
       
   308             iDefaultPath = EFalse;
       
   309             iPathInitialized = ETrue;
       
   310             iIsInitializing = EFalse;
       
   311             }
       
   312         else
       
   313             {
       
   314             MPX_DEBUG1("CMPXCollectionViewHgPlaylistHelper::HandleOpenL - Opening default Path");
       
   315             iIsInitializing = EFalse;
       
   316             // Try opening default
       
   317             LoadDefaultPathL();
       
   318             }
       
   319         }
       
   320     }
       
   321 
       
   322 // ---------------------------------------------------------------------------
       
   323 // Handle playback message.
       
   324 // ---------------------------------------------------------------------------
       
   325 //
       
   326 void CMPXCollectionViewHgPlaylistHelper::HandlePlaybackMessage(
       
   327     CMPXMessage* aMessage, TInt aError )
       
   328     {
       
   329     if ( aError == KErrNone && aMessage )
       
   330         {
       
   331         TRAP_IGNORE( DoHandlePlaybackMessageL( *aMessage ) );
       
   332         }
       
   333     }
       
   334 
       
   335 
       
   336 // ---------------------------------------------------------------------------
       
   337 // From MMPXCollectionObserver.
       
   338 // Handles the item being opened.
       
   339 // ---------------------------------------------------------------------------
       
   340 //
       
   341 void CMPXCollectionViewHgPlaylistHelper::HandleOpenL(
       
   342     const CMPXCollectionPlaylist& aPlaylist,
       
   343     TInt aError )
       
   344     {
       
   345     MPX_FUNC("CMPXCollectionViewHgPlaylistHelper::HandleOpenL");
       
   346     if( aError == KErrNone )
       
   347         {
       
   348         if ( !iInitCanceled )
       
   349             {
       
   350             MPX_DEBUG1( "CMPXCollectionViewHgPlaylistHelper::HandleOpenL - Opening playlist" );
       
   351 
       
   352             // toggle repeat and shuffle to off if podcast playlist
       
   353             if(KMPXUidPodcastDBPlugin == TUid::Uid((TInt)aPlaylist.Path().Id(0)))
       
   354                 {
       
   355                 CMPXCollectionPlaylist* tmp =
       
   356                                 CMPXCollectionPlaylist::NewL( aPlaylist );
       
   357                 CleanupStack::PushL( tmp );
       
   358                 tmp->SetRepeatEnabled( EFalse );
       
   359                 tmp->SetShuffleEnabledL( EFalse );
       
   360                 iPlaybackUtility->InitL( *tmp, EFalse );
       
   361                 CleanupStack::PopAndDestroy( tmp );
       
   362                 }
       
   363             else
       
   364                 {
       
   365                 iPlaybackUtility->InitL( aPlaylist, EFalse );
       
   366                 }
       
   367             }
       
   368         else
       
   369             {
       
   370             MPX_DEBUG1( "CMPXCollectionViewHgPlaylistHelper::HandleOpenL - Init canceled, do nothing" );
       
   371             }
       
   372         }
       
   373     else
       
   374         {
       
   375         if( iDefaultPath )
       
   376             {
       
   377             // Failed to open default path, set to initialized
       
   378             // "No songs view"
       
   379             //
       
   380             iDefaultPath = EFalse;
       
   381             iPathInitialized = ETrue;
       
   382             iIsInitializing = EFalse;
       
   383             }
       
   384         else
       
   385             {
       
   386             // Try opening default
       
   387             LoadDefaultPathL();
       
   388             }
       
   389         }
       
   390     }
       
   391 
       
   392 // ---------------------------------------------------------------------------
       
   393 // Initialize the playback engine with a collection path
       
   394 // ---------------------------------------------------------------------------
       
   395 //
       
   396 void CMPXCollectionViewHgPlaylistHelper::InitPlaybackEngineL( CMPXCollectionPath& aPath )
       
   397     {
       
   398     MPX_FUNC( "CMPXCollectionViewHgPlaylistHelper::InitPlaybackEngineL" );
       
   399 
       
   400     if( !iInitCanceled )
       
   401         {
       
   402         aPath.Remove(0); // we want to remove index 0 which is shuffle item
       
   403         TTime time;
       
   404         time.UniversalTime();
       
   405         TInt64 seed = time.Int64();
       
   406         TInt randIndex = Math::Rand(seed) % aPath.Count();
       
   407         aPath.Set(randIndex);
       
   408         CMPXCollectionPlaylist* playlist = CMPXCollectionPlaylist::NewL( aPath );
       
   409         CleanupStack::PushL( playlist );
       
   410         playlist->SetShuffleEnabledL( ETrue );
       
   411         iPlaybackUtility->SetL( EPbPropertyRandomMode, iEnableShuffle );
       
   412         iPlaybackUtility->InitL( *playlist, ETrue );
       
   413         CleanupStack::PopAndDestroy( playlist );
       
   414         }
       
   415     else
       
   416         {
       
   417         iIsInitializing = EFalse;
       
   418         iPathInitialized = ETrue;
       
   419         iInitCanceled = EFalse;
       
   420         }
       
   421     }
       
   422 
       
   423 // -----------------------------------------------------------------------------
       
   424 // Handle playback message
       
   425 // -----------------------------------------------------------------------------
       
   426 //
       
   427 void CMPXCollectionViewHgPlaylistHelper::DoHandlePlaybackMessageL( const CMPXMessage& aMessage )
       
   428     {
       
   429     MPX_FUNC( "CMPXCollectionViewHgPlaylistHelper::DoHandlePlaybackMessageL" );
       
   430     TMPXMessageId id( aMessage.ValueTObjectL<TMPXMessageId>( KMPXMessageGeneralId ) );
       
   431     if ( KMPXMessageGeneral == id )
       
   432         {
       
   433         TInt event( aMessage.ValueTObjectL<TInt>( KMPXMessageGeneralEvent ) );
       
   434         MPX_DEBUG2( "CMPXCollectionViewHgPlaylistHelper::DoHandlePlaybackMessageL(%d)", event );
       
   435 
       
   436         switch ( event )
       
   437             {
       
   438             default:
       
   439                 {
       
   440                 // ignore other messages
       
   441                 break;
       
   442                 }
       
   443             }
       
   444         }
       
   445     }
       
   446 
       
   447 // -----------------------------------------------------------------------------
       
   448 // Handle collection message
       
   449 // -----------------------------------------------------------------------------
       
   450 //
       
   451 void CMPXCollectionViewHgPlaylistHelper::DoHandleCollectionMessageL(
       
   452 	const CMPXMessage& /*aMessage*/ )
       
   453     {
       
   454     MPX_FUNC( "CMPXCollectionViewHgPlaylistHelper::DoHandleCollectionMessageL" );
       
   455 
       
   456     }
       
   457 
       
   458 // -----------------------------------------------------------------------------
       
   459 // Start the incremental reading operation at an offset
       
   460 // -----------------------------------------------------------------------------
       
   461 //
       
   462 void CMPXCollectionViewHgPlaylistHelper::DoIncrementalOpenL( CMPXCollectionPath& aPath,
       
   463                                           TInt aIndex,
       
   464                                           TInt aDelay )
       
   465     {
       
   466     RArray<TMPXAttribute> attrs;
       
   467     CleanupClosePushL( attrs );
       
   468     TArray<TMPXAttribute> ary = attrs.Array();
       
   469     iIncOpenUtil->Stop();
       
   470     iIncOpenUtil->SetDelay( aDelay );
       
   471     iIncOpenUtil->StartL( aPath, ary, KIncrementalFetchCount,
       
   472                           aIndex, CMPXCollectionOpenUtility::EFetchNormal );
       
   473     CleanupStack::PopAndDestroy( &attrs );
       
   474     }
       
   475 
       
   476 
       
   477 // End of File