upnpframework/upnpmusicadapter/src/upnpmpxhelper.cpp
changeset 0 7f85d04be362
equal deleted inserted replaced
-1:000000000000 0:7f85d04be362
       
     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 class for some common MPX routines
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDES
       
    20 class MMPXCollection;
       
    21 #include <mpxcollectionmediator.h>
       
    22 #include <mpxharvesterutility.h>
       
    23 #include <mpxcollectionutility.h>
       
    24 #include <mpxcollectionpath.h>
       
    25 #include <mpxmedia.h>
       
    26 #include <mpxmediageneraldefs.h> // for KMPXMediaGeneralCollectionId
       
    27 #include <mpxmediacontainerdefs.h> // for KMPXMediaArrayContents
       
    28 #include <mpxcollectiontype.h> // for CMPXCollectionType
       
    29 #include <mpxplaylistenginedefs.h> // for EMPXPlaylistTypeM3U
       
    30 #include <mpxcollectionplugin.hrh> // for EMPXCollectionPluginHidden and
       
    31 #include "upnpmusicadapter.h"      // EMPXCollectionPluginTemporary
       
    32 
       
    33 // ourselves
       
    34 #include "upnpmpxhelper.h" // myself
       
    35 
       
    36 // ======== MEMBER FUNCTIONS ========
       
    37 
       
    38 // --------------------------------------------------------------------------
       
    39 // CUPnPMpxHelper::NewL
       
    40 // 1st phase constructor.
       
    41 // --------------------------------------------------------------------------
       
    42 //
       
    43 CUPnPMpxHelper* CUPnPMpxHelper::NewL( const TUid& aModeId )
       
    44     {
       
    45     CUPnPMpxHelper* self = new(ELeave)
       
    46         CUPnPMpxHelper();
       
    47     CleanupStack::PushL( self );
       
    48     self->ConstructL( aModeId );
       
    49     CleanupStack::Pop( self );
       
    50     return self;
       
    51     }
       
    52 
       
    53 // --------------------------------------------------------------------------
       
    54 // CUPnPMpxHelper::CUPnPMpxHelper
       
    55 // Default constructor.
       
    56 // --------------------------------------------------------------------------
       
    57 //    
       
    58 CUPnPMpxHelper::CUPnPMpxHelper()
       
    59     {
       
    60     }
       
    61 
       
    62 // --------------------------------------------------------------------------
       
    63 // CUPnPMpxHelper::ConstructL
       
    64 // 2nd phase constructor
       
    65 // --------------------------------------------------------------------------
       
    66 //    
       
    67 void CUPnPMpxHelper::ConstructL( const TUid& aModeId )
       
    68     {
       
    69     iHarvester = CMPXHarvesterFactory::NewL();
       
    70     iCollectionUtil = MMPXCollectionUtility::NewL( 0, aModeId );
       
    71         
       
    72     iMediator = CMPXCollectionMediator::NewL( 
       
    73         iCollectionUtil->Collection(), this );
       
    74     }
       
    75 
       
    76 // --------------------------------------------------------------------------
       
    77 // CUPnPMpxHelper::~CUPnPMpxHelper
       
    78 // Destructor.
       
    79 // --------------------------------------------------------------------------
       
    80 //
       
    81 CUPnPMpxHelper::~CUPnPMpxHelper()
       
    82     {
       
    83     if( iHarvester )
       
    84         {
       
    85         iHarvester->Close();
       
    86         }
       
    87     
       
    88     if( iCollectionUtil )
       
    89         {
       
    90         iCollectionUtil->Close(); 
       
    91         }
       
    92     delete iMediator;
       
    93     }
       
    94 
       
    95 
       
    96 // --------------------------------------------------------------------------
       
    97 // CUPnPMpxHelper::AddTrackL
       
    98 // --------------------------------------------------------------------------
       
    99 //
       
   100 void CUPnPMpxHelper::AddTrackL( CMPXMedia* aMedia )
       
   101     {
       
   102     // Add to harvester 
       
   103     TInt colUid = iHarvester->AddFileL( aMedia );
       
   104 
       
   105     // Add to collection, make sure we set the collection ID
       
   106     aMedia->SetTObjectValueL<TUid>(
       
   107         KMPXMediaGeneralCollectionId, TUid::Uid( colUid ) );
       
   108 
       
   109     iMediator->AddItemL( aMedia );
       
   110 
       
   111 
       
   112     }
       
   113 
       
   114 // --------------------------------------------------------------------------
       
   115 // CUPnPMpxHelper::AddPlaylistL
       
   116 // --------------------------------------------------------------------------
       
   117 //
       
   118 void CUPnPMpxHelper::AddPlaylistL( CMPXMedia* aMedia )
       
   119     {
       
   120     // leave if the given media doesn't contain the following attributes
       
   121     if ( !aMedia->IsSupported( KMPXMediaGeneralType ) ||
       
   122          !aMedia->IsSupported( KMPXMediaGeneralCategory ) )
       
   123         {
       
   124         User::Leave( KErrArgument );
       
   125         }
       
   126 
       
   127     // leave if the given media isn't a playlist
       
   128     TMPXGeneralType mediaType =
       
   129         *aMedia->Value<TMPXGeneralType>( KMPXMediaGeneralType );
       
   130     TMPXGeneralCategory mediaCategory =
       
   131         *aMedia->Value<TMPXGeneralCategory>( KMPXMediaGeneralCategory );
       
   132     if ( mediaType != EMPXItem || mediaCategory != EMPXPlaylist )
       
   133         {
       
   134         User::Leave( KErrNotSupported );
       
   135         }
       
   136 
       
   137     // leave if the media doesn't contain mandatory attributes
       
   138     if ( !aMedia->IsSupported( KMPXMediaArrayContents ) ||
       
   139          !aMedia->IsSupported( KMPXMediaArrayCount ) )
       
   140         {
       
   141         User::Leave( KErrArgument );
       
   142         }
       
   143 
       
   144     // check if we are adding new or appending to existing
       
   145     if (!aMedia->IsSupported(KMPXMediaGeneralId))
       
   146         {
       
   147         if (!aMedia->IsSupported(KMPXMediaGeneralTitle) ||
       
   148             !aMedia->IsSupported(KMPXMediaGeneralUri))
       
   149             {
       
   150             User::Leave( KErrArgument );
       
   151             }
       
   152         else
       
   153             {
       
   154             // adding new
       
   155             // find the collection Id
       
   156             HBufC* playlistExtension =
       
   157                 iHarvester->PlaylistFileExtensionLC( EMPXPlaylistTypeM3U );
       
   158             TInt collectionId = FindCollectionIdL( *playlistExtension );                
       
   159             CleanupStack::PopAndDestroy( playlistExtension );
       
   160 
       
   161             // Add to collection, make sure we set the collection ID.
       
   162             aMedia->SetTObjectValueL<TUid>(
       
   163                 KMPXMediaGeneralCollectionId, TUid::Uid( collectionId ));
       
   164             }
       
   165         }
       
   166     else
       
   167         {
       
   168         // appending to existing
       
   169         FillInPlaylistDetailsL( *aMedia );
       
   170         }
       
   171 
       
   172     iMediator->AddItemL( aMedia );
       
   173     }
       
   174 
       
   175 // --------------------------------------------------------------------------
       
   176 // CUPnPMpxHelper::OpenL
       
   177 // --------------------------------------------------------------------------
       
   178 //
       
   179 void CUPnPMpxHelper::AddAndOpenL( const TUid& aHostId, CMPXMedia& aMedia )
       
   180     {
       
   181     // Grab the in memory collection plugin UID
       
   182     // aPluginInfo provides additional resolution to find the plugin
       
   183     //
       
   184     RArray<TUid> ary;
       
   185     CleanupClosePushL( ary );
       
   186     ary.AppendL( TUid::Uid( EMPXCollectionPluginHidden ) );
       
   187     ary.AppendL( TUid::Uid( EMPXCollectionPluginTemporary ) );
       
   188     TUid inMemCollection = iCollectionUtil->CollectionIDL( ary.Array() );
       
   189     CleanupStack::PopAndDestroy( &ary );
       
   190 
       
   191     // First step is to add this media to the in memory plugin
       
   192     // Set the item id to be the host ID
       
   193     // 
       
   194     aMedia.SetTObjectValueL( KMPXMediaGeneralCollectionId, inMemCollection );
       
   195     aMedia.SetTObjectValueL( KMPXMediaGeneralId, aHostId.iUid );
       
   196     iCollectionUtil->Collection().AddL( aMedia );
       
   197 
       
   198     // Second Step is to construct collection path
       
   199     // | collection id | host id |
       
   200     //
       
   201     CMPXCollectionPath* path = CMPXCollectionPath::NewL();
       
   202     CleanupStack::PushL( path );
       
   203     path->AppendL( inMemCollection.iUid );
       
   204     path->AppendL( aHostId.iUid );
       
   205 
       
   206     // Last step is to open this path
       
   207     //
       
   208     iCollectionUtil->Collection().OpenL( *path );
       
   209     CleanupStack::PopAndDestroy( path );
       
   210     }
       
   211 
       
   212 // ---------------------------------------------------------------------------
       
   213 // retrieve collection from URI
       
   214 // ResolvePlugin should be able to resolve the plugin without
       
   215 // the client looking for the collection Id
       
   216 // ---------------------------------------------------------------------------
       
   217 //    
       
   218 TInt CUPnPMpxHelper::FindCollectionIdL( const TDesC& aUri )   
       
   219     {
       
   220     TInt collectionId(KErrNotFound);
       
   221 
       
   222     TParsePtrC parser( aUri );   
       
   223             
       
   224     RPointerArray<CMPXCollectionType> collectionType;
       
   225     iCollectionUtil->Collection().GetSupportedTypesL(collectionType);
       
   226     
       
   227     TInt index(KErrNotFound);
       
   228     TInt count( collectionType.Count() );
       
   229     
       
   230     for (TInt i = 0; i < count; i++)
       
   231         {
       
   232         const CDesCArray& extensions = collectionType[i]->Extensions();
       
   233         
       
   234         if (extensions.FindIsq(parser.Ext(), index) == 0)
       
   235             {
       
   236             collectionId = collectionType[i]->Uid().iUid;
       
   237             break;
       
   238             }
       
   239         }
       
   240         
       
   241     collectionType.ResetAndDestroy();
       
   242     
       
   243     return collectionId;
       
   244     }
       
   245 
       
   246 // ---------------------------------------------------------------------------
       
   247 // retrieve information for the required attributes
       
   248 // ---------------------------------------------------------------------------
       
   249 //
       
   250 void CUPnPMpxHelper::FillInPlaylistDetailsL( CMPXMedia& aMedia )
       
   251     {
       
   252     // retrieve info about the playlist itself
       
   253     RArray<TMPXAttribute> playlistAttributes;
       
   254     CleanupClosePushL(playlistAttributes);
       
   255     playlistAttributes.AppendL(KMPXMediaGeneralId);
       
   256     playlistAttributes.AppendL(KMPXMediaGeneralTitle);
       
   257     playlistAttributes.AppendL(KMPXMediaGeneralUri);
       
   258     playlistAttributes.AppendL(KMPXMediaGeneralCollectionId);
       
   259 
       
   260     CMPXMedia* playlistSearchResult = iCollectionUtil->Collection()
       
   261         .FindAllL( aMedia, playlistAttributes.Array() );
       
   262     CleanupStack::PopAndDestroy(&playlistAttributes);
       
   263     CleanupStack::PushL(playlistSearchResult);
       
   264 
       
   265     const CMPXMediaArray* results =
       
   266         playlistSearchResult->Value<CMPXMediaArray>(KMPXMediaArrayContents);
       
   267 
       
   268     if ( results->Count() != 1 )
       
   269         {
       
   270         User::Leave(KErrArgument);
       
   271         }
       
   272 
       
   273     aMedia.SetTObjectValueL(
       
   274         KMPXMediaGeneralId, *(*results)[0]->Value<TMPXItemId>(KMPXMediaGeneralId));
       
   275     aMedia.SetTextValueL(
       
   276         KMPXMediaGeneralTitle, (*results)[0]->ValueText(KMPXMediaGeneralTitle));
       
   277     aMedia.SetTextValueL(
       
   278         KMPXMediaGeneralUri, (*results)[0]->ValueText(KMPXMediaGeneralUri));
       
   279     aMedia.SetTObjectValueL(
       
   280         KMPXMediaGeneralCollectionId, *(*results)[0]->Value<TUid>(KMPXMediaGeneralCollectionId));
       
   281 
       
   282     CleanupStack::PopAndDestroy(playlistSearchResult);        
       
   283     }
       
   284 
       
   285