// ----------------------------------------------------------------------------------------------------------
// Remove an item from the collection database using the given path
// ----------------------------------------------------------------------------------------------------------
//
void CTestCollectionPlugin::RemoveL (const CMPXCollectionPath& aPath)
{
    LOG1(_L("CTestCollectionPlugin::RemoveL"));
    LogPath( aPath );
    CDesCArrayFlat* fp = new (ELeave) CDesCArrayFlat(8);
    CleanupStack::PushL( fp );
    switch( aPath.Levels() )
    {
    case 2:
        if(aPath.Id() == 14)
        {   // Test0091
            iObs->HandleRemove( *fp, KErrNotFound );
        }
        break;
    case 3:
        if(aPath.Id() == 212)
        {   // Test0090
            fp->AppendL( _L("\\song3121.ts") );
            fp->AppendL( _L("\\song3122.ts") );
            fp->AppendL( _L("\\song3123.tp") );
            iObs->HandleRemove( *fp, KErrNone );
        }
        break;
    case 4:
        if(aPath.Id() == 3111)
        {   // Test0088
            fp->AppendL( _L("\\song3111.ts") );
            iObs->HandleRemove( *fp, KErrNone );
        }
        break;
    default:
        User::Panic(_L("CTestCollectionPlugin::RemoveL panic"), 1); // magic number
    }
    CleanupStack::PopAndDestroy( fp );
}
/*!
 Stub function.
*/
CMPXCollectionPath* MMPXCollectionUiHelper::MusicPlaylistPathL()
{
    CMPXCollectionPath* path = CMPXCollectionPath::NewL();
    CleanupStack::PushL( path );
    path->AppendL( TUid::Uid(EMPXCollectionPluginMusic).iUid );
    path->AppendL( KMPXPlaylistViewIndex );
    //Fake some elements for the path to make it unique
    for(TInt i=0; i<3; i++){
        path->InsertL(TUid::Uid(EMPXCollectionPluginMusic).iUid,0);
    }
    CleanupStack::Pop( path );
        
    return path;
}
/*!
 Stub function.
*/
void MMPXCollectionUtility::OpenL(const CMPXCollectionPath& aPath, TMPXOpenMode aMode)
{
    Q_UNUSED(aMode);
    iCountPath = aPath.Count();
    iOpen = ETrue;
    iOpenCount++;
}
// ----------------------------------------------------------------------------
// Navigates to the given path
// ----------------------------------------------------------------------------
//
void CGlxMediaListsTestCollectionPlugin::OpenL(const CMPXCollectionPath& aPath,
                   const TArray<TMPXAttribute>& /*aAttrs*/,
                   CMPXFilter* /*aFilter*/)
    {
    RArray<TInt> supportedIds;
    CleanupClosePushL(supportedIds);
    supportedIds.AppendL(KMPXMediaIdContainer);
    supportedIds.AppendL(KMPXMediaIdGeneral);
    iOpenEntries = CMPXMedia::NewL(supportedIds.Array());

    CMPXMediaArray* array = CMPXMediaArray::NewL();
    CleanupStack::PushL(array);

 	if ( aPath.Levels() == 1 ) 
 		{
 		TInt count = _iItemDBHackAlbums.Count();
 		for ( TInt i = 0; i < count; i++ )
 			{
 			AppendL(_iItemDBHackAlbums[i]->iId, *_iItemDBHackAlbums[i]->iTitle, *array, EMPXGroup, EMPXAlbum);
 			}
 		}
 	else if ( aPath.Levels() == 2 ) 
 		{
 		TInt count = _iItemDBHackContent.Count();
 		for ( TInt i = 0; i < count; i++ )
 			{
 			AppendL(_iItemDBHackContent[i]->iId, *_iItemDBHackContent[i]->iTitle, *array, EMPXItem, EMPXImage);
 			}

		StartEvents();
 		}
 	else 
 		{
 		__DEBUG_ONLY(Panic(EGlxPanicDebugUnexpectedError));
 		}
    
    iOpenEntries->SetCObjectValueL(KMPXMediaArrayContents, array);
    iOpenEntries->SetTObjectValueL(KMPXMediaArrayCount, array->Count());

    CleanupStack::PopAndDestroy(array);    

	//iObs->HandleOpen(*iOpenEntries, KErrNone);
	
    AsyncNotifyL(iOpenEntries, KErrNone, CGlxAsyncNotifier::EOpen);
        
    CleanupStack::PopAndDestroy(&supportedIds);        
    }
// ----------------------------------------------------------------------------
// Extended properties of the current file (async)
// ----------------------------------------------------------------------------
//
void CGlxMediaListsTestCollectionPlugin::MediaL(const CMPXCollectionPath& aPath,
		const TArray<TMPXAttribute>& aAttrs,
                        const TArray<TCapability>& /*aCaps*/,
                        CMPXAttributeSpecs* /*aSpecs*/)
    {
    __ASSERT_ALWAYS(!iMedia, Panic(EGlxPanicIllegalState));

    TInt error = KErrNone;
    CItem* item = NULL;
    TInt bitmapHandle = 0;

    RArray<TInt> supportedIds;
    CleanupClosePushL(supportedIds);
    iMedia = CMPXMedia::NewL(supportedIds.Array());
    CleanupStack::PopAndDestroy(&supportedIds);

    TArray<TInt> selection = aPath.Selection();
    TInt selectionCount = selection.Count();
    if (selectionCount > 1)
        {
        CMPXMediaArray* mediaArray = CMPXMediaArray::NewL();
        CleanupStack::PushL(mediaArray);

        for (TInt count = 0; count < selectionCount; ++count)
            {
            item = ItemL(aPath, selection[count]);

            if (item)
                {
                RArray<TInt> supportedIds;
                CleanupClosePushL(supportedIds);
                CMPXMedia* media = CMPXMedia::NewL(supportedIds.Array());
                CleanupStack::PopAndDestroy(&supportedIds);

                CleanupStack::PushL(media);

                (void)PopulateMediaWithItemL(media, item, aAttrs);

                mediaArray->AppendL(*media);

                CleanupStack::PopAndDestroy(media);
                }
            else
                {
                error = KErrNotFound;
                break;
                }
            }

        PopulateMediaWithArrayL(iMedia, mediaArray);

        CleanupStack::PopAndDestroy(mediaArray);
        }
    else
        {
	item = ItemL(aPath, aPath.Index());

        if (item)
            {
            bitmapHandle = PopulateMediaWithItemL(iMedia, item, aAttrs);
            }
        else
            {
            error = KErrNotFound;
            }
        }

    if ( (KErrNone == error) && bitmapHandle )
        {
        // Get thumbnail for the item
        TGlxThumbnailRequest request;
        request.iId = item->iId;
    	request.iSizeClass = TSize(60, 45);
    	request.iPriorityMode = TGlxThumbnailRequest::EPrioritizeSpeed;
    	request.iBitmapHandle = bitmapHandle;

       	iThumbnailCreator->FetchThumbnailL(request, *this);
        }
    else
        {
        /*****************************************************
        * Old synchronous code
        ******************************************************/
        // No thumbnail requested - send response now
        //iObs->HandleMedia(*iMedia, error);
        //delete iMedia;
        //iMedia = NULL;
        
        /*****************************************************
        * New Async Code. Instantiate callback mechanism
        ******************************************************/
        
    	AsyncNotifyL(iMedia, error, CGlxAsyncNotifier::EMedia);  
        }
    }
// ----------------------------------------------------------------------------------------------------------
// Get the extended properties of the current file (async)
// ----------------------------------------------------------------------------------------------------------
//
void CTestCollectionPlugin::MediaL (const CMPXCollectionPath& aPath,
                                    const TArray<TMPXAttribute>& aAttrs,
                                    const TArray<TCapability>& aCaps,
                                    CMPXAttributeSpecs* aSpecs)
{
    LOG1(_L("CTestCollectionPlugin::MediaL"));
    LogPath( aPath );
    TMPXOpenMode openMode = aPath.OpenNextMode();
    //TInt idIndex = aPath.Levels() - 1;
    LOG2(_L("TMPXOpendMode=%d"), openMode);
    LOG2(_L("aAttrs.Count()=%d"), aAttrs.Count());
    LOG2(_L("aCaps.Count()=%d"), aCaps.Count());
    LOG2(_L("aSpecs=0x%x"), aSpecs);

    switch( (TInt)aPath.Id(1) )
    {
    case 14:    // Test0146
    {
        iObs->HandleMedia(NULL, KErrNone);
        break;
    }
    case 15:    // Test0147
    {
        CMPXMedia* media = CMPXMedia::NewL();
        CleanupStack::PushL( media );
        iObs->HandleMedia(media, KErrNone);
        CleanupStack::PopAndDestroy( media );
        break;
    }
    case 16:    // Test0148
    {
        CMPXMedia* media = CMPXMedia::NewL();
        CleanupStack::PushL( media );
        iObs->HandleMedia(media, KErrNotFound);
        CleanupStack::PopAndDestroy( media );
        break;
    }
    case 17:    // Test0149
    {
        CMPXMedia* media = CMPXMedia::NewL();
        CleanupStack::PushL( media );
        (*media) = *aSpecs;
        iObs->HandleMedia(media, KErrNone);
        CleanupStack::PopAndDestroy( media );
        break;
    }
    case 990:   // Test data for playback framework teests
    {
        CMPXMedia* media = CMPXMedia::NewL();
        CleanupStack::PushL( media );

        switch( (TInt)aPath.Id(2) )
        {
        case 9901:
            AddAttrbuteL(*media, 9901, _L("song9901"), _L("\\song9901.ts"), aAttrs);
            AddAttributePlaybackFwTestL(*media, 9901, aAttrs);
            break;
        case 9902:
            AddAttrbuteL(*media, 9902, _L("song9902"), _L("\\song9902.ts"), aAttrs);
            AddAttributePlaybackFwTestL(*media, 9902, aAttrs);
            break;
        case 9903:
            AddAttrbuteL(*media, 9903, _L("song9903"), _L("\\song9903.ts"), aAttrs);
            AddAttributePlaybackFwTestL(*media, 9903, aAttrs);
            break;
        case 9904:
            AddAttrbuteL(*media, 9904, _L("song9904"), _L("\\song9904.ts"), aAttrs);
            AddAttributePlaybackFwTestL(*media, 9904, aAttrs);
            break;
        case 9905:
            AddAttrbuteL(*media, 9905, _L("song9905"), _L("\\song9905.ts"), aAttrs);
            AddAttributePlaybackFwTestL(*media, 9905, aAttrs);
            break;
        case 9906:
            AddAttrbuteL(*media, 9906, _L("song9906"), _L("\\song9906.ts"), aAttrs);
            AddAttributePlaybackFwTestL(*media, 9906, aAttrs);
            break;
        case 9907:
            AddAttrbuteL(*media, 9907, _L("song9907"), _L("\\song9907.ts"), aAttrs);
            AddAttributePlaybackFwTestL(*media, 9907, aAttrs);
            break;
        };
        iObs->HandleMedia(media, KErrNone);
        CleanupStack::PopAndDestroy( media );
        break;
    }
    default:
        User::Leave( KErrNotFound );
    };
}
// ----------------------------------------------------------------------------------------------------------
// Navigates to the given path
// ----------------------------------------------------------------------------------------------------------
//
void CTestCollectionPlugin::OpenL(const CMPXCollectionPath& aPath,
                                  const TArray<TMPXAttribute>& aAttrs,
                                  CMPXFilter* aFilter)
{
    LOG1(_L("CTestCollectionPlugin::OpenL"));
    LogPath( aPath );
    TMPXOpenMode openMode = aPath.OpenNextMode();
    TInt idIndex = aPath.Levels() - 1;
    LOG2(_L("TMPXOpendMode=%d"), openMode);
    LOG2(_L("aFilter=%x"), aFilter);

    RArray<TInt> supportedIds;
    CleanupClosePushL(supportedIds);
    supportedIds.AppendL(KMPXMediaIdContainer);
    supportedIds.AppendL(KMPXMediaIdGeneral);

    CMPXMedia* entries = CMPXMedia::NewL(supportedIds.Array());
    CleanupStack::PopAndDestroy(&supportedIds);
    CleanupStack::PushL(entries);
    entries->SetTObjectValueL<TMPXItemId>(TMPXAttribute(KMPXMediaIdGeneral,EMPXMediaGeneralId), aPath.Id(idIndex));

    if(aPath.Levels() == 1)
    {
        // Plugin's root level
        RArray<TInt> ids;
        CleanupClosePushL( ids );
        ids.Append( 11 );
        ids.Append( 12 );
        ids.Append( 13 );
        ids.Append( 990 );  // for Playback Framework Test
        RArray<TMPXAttribute> attrs;
        CleanupClosePushL( attrs );
        attrs.Append( KMPXMediaGeneralId );
        attrs.Append( KMPXMediaGeneralUri );
        attrs.Append( KMPXMediaGeneralTitle );

        CMPXMediaArray* array=CMPXMediaArray::NewL();
        CleanupStack::PushL( array );

        const TArray<TMPXAttribute>& tempRef = aAttrs.Count() == 0? attrs.Array() : aAttrs;
        AddMediaL(*array, ids, tempRef);
        FilterMediaArray(*array, aFilter);

        entries->SetCObjectValueL (TMPXAttribute(KMPXMediaIdContainer,EMPXMediaArrayContents),array);
        entries->SetTObjectValueL (TMPXAttribute(KMPXMediaIdContainer,EMPXMediaArrayCount),array->Count());

        iObs->HandleOpen (entries, KErrNone);
        CleanupStack::PopAndDestroy(3, &ids);   // attrs, array, ids
    }
    else if(aPath.Levels() == 2)
    {
        // Opening plugin's root menu
        TInt topLevId = aPath.Id(1);
        RArray<TInt> ids;
        CleanupClosePushL( ids );
        RArray<TMPXAttribute> attrs;
        CleanupClosePushL( attrs );
        CMPXMediaArray* array=CMPXMediaArray::NewL();
        CleanupStack::PushL( array );

        attrs.Append( KMPXMediaGeneralId );
        attrs.Append( KMPXMediaGeneralUri );
        attrs.Append( KMPXMediaGeneralTitle );
        if(topLevId == 11)
        {
            ids.Append( 211 );
            ids.Append( 212 );
            ids.Append( 213 );
        }
        else if(topLevId == 12)
        {
            ids.Append( 221 );
        }
        else if(topLevId == 13)
        {
            ids.Append( 231 );
            ids.Append( 232 );
        }
        else if(topLevId == 990)
        {
            ids.Append( 9901 );
            ids.Append( 9902 );
            ids.Append( 9903 );
            ids.Append( 9904 );
            ids.Append( 9905 );
            ids.Append( 9906 );
            ids.Append( 9907 );
        }
        const TArray<TMPXAttribute>& tempRef = aAttrs.Count() == 0? attrs.Array() : aAttrs;
        AddMediaL(*array, ids, tempRef);
        FilterMediaArray(*array, aFilter);

        entries->SetCObjectValueL (TMPXAttribute(KMPXMediaIdContainer,EMPXMediaArrayContents),array);
        entries->SetTObjectValueL (TMPXAttribute(KMPXMediaIdContainer,EMPXMediaArrayCount),array->Count());

        iObs->HandleOpen (entries, KErrNone);
        CleanupStack::PopAndDestroy(3, &ids);   // attrs, array, ids
    }
    else if(aPath.Levels() == 3)
    {
        // Opening plugin's 2nd
        TInt secLevId = aPath.Id(2);

        RArray<TInt> ids;
        CleanupClosePushL( ids );
        RArray<TMPXAttribute> attrs;
        CleanupClosePushL( attrs );
        CMPXMediaArray* array=CMPXMediaArray::NewL();
        CleanupStack::PushL( array );

        attrs.Append( KMPXMediaGeneralId );
        attrs.Append( KMPXMediaGeneralUri );
        attrs.Append( KMPXMediaGeneralTitle );
        switch( secLevId )
        {
        case 211:
            ids.Append( 3111 );
            break;
        case 212:
            ids.Append( 3121 );
            break;
        case 213:
            ids.Append( 3131 );
            break;
        case 221:
            // Empty
            break;
        case 231:
            ids.Append( 3311 );
            break;
        case 232:
            ids.Append( 3321 );
            break;
        };
        const TArray<TMPXAttribute>& tempRef = aAttrs.Count() == 0? attrs.Array() : aAttrs;
        AddMediaL(*array, ids, tempRef);
        FilterMediaArray(*array, aFilter);

        entries->SetCObjectValueL (TMPXAttribute(KMPXMediaIdContainer,EMPXMediaArrayContents),array);
        entries->SetTObjectValueL (TMPXAttribute(KMPXMediaIdContainer,EMPXMediaArrayCount),array->Count());

        iObs->HandleOpen (entries, KErrNone);
        CleanupStack::PopAndDestroy(3, &ids);   // attrs, array, ids
    }
    else
    {
        User::Leave( KErrNotFound );
    }
    CleanupStack::PopAndDestroy( entries );
}