upnpframework/upnpmusicadapter/src/upnpplaylistservices.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:  provides playlist handling services for UPnP framework
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDES
       
    20 // System
       
    21 #include <escapeutils.h> // for unicode conversion
       
    22 
       
    23 // upnp stack api
       
    24 #include <upnpitem.h> // s60 upnp stack
       
    25 #include <upnpelement.h> // s60 upnp stack
       
    26 
       
    27 // MPX framework api
       
    28 #include <mpxcollectionutility.h>
       
    29 #include <mpxcollectionframeworkdefs.h> // main attribute keys
       
    30 #include <mpxmediageneraldefs.h> // commonly used attribute keys
       
    31 #include <mpxmediacontainerdefs.h> // container-specific attribute keys
       
    32 #include <mpxmediamusicdefs.h> // music-specific attribute keys
       
    33 #include <mpxcollectionplugin.hrh> // collection plugin types
       
    34 #include <mpxmedia.h>
       
    35 #include <mpxmediaarray.h>
       
    36 
       
    37 // upnpframework / avcontroller helper api
       
    38 #include "upnpitemutility.h" // for FindElementByName & ResourceFromItemL
       
    39 #include "upnpconstantdefs.h" // for element names
       
    40 
       
    41 // upnpframework / internal api's
       
    42 #include "upnpcdsreselementutility.h" // for UriFromItemL 
       
    43 
       
    44 // music adapter internal
       
    45 #include "upnpmpxhelper.h" // for some mpx operations
       
    46 #include "upnpplaylistservices.h" // ourselves
       
    47 
       
    48 // upnpframework / settings engine api
       
    49 #include "upnpsettingsengine.h"     // CUPnPSettingsEngine
       
    50 
       
    51 // debug
       
    52 _LIT16( KComponentLogfile, "musicadapter.txt" );
       
    53 #include "upnplog.h"
       
    54 
       
    55 
       
    56 // constant definitions
       
    57 const TUid KHomeConnectUid = { 0x10208A0A };
       
    58 const TUid KMpxLocalCollectionUid = { 0x101FFC3A };
       
    59 
       
    60 
       
    61 // ======== MEMBER FUNCTIONS ========
       
    62 
       
    63 // --------------------------------------------------------------------------
       
    64 // CUPnPPlaylistServices::NewL
       
    65 // 1st phase constructor.
       
    66 // --------------------------------------------------------------------------
       
    67 //
       
    68 EXPORT_C CUPnPPlaylistServices* CUPnPPlaylistServices::NewL()
       
    69     {
       
    70     __LOG( "CUPnPPlaylistServices::NewL" );
       
    71 
       
    72     CUPnPPlaylistServices* self = new(ELeave) CUPnPPlaylistServices();
       
    73     CleanupStack::PushL( self );
       
    74     self->ConstructL();
       
    75     CleanupStack::Pop( self );
       
    76     return self;
       
    77     }
       
    78 
       
    79 // --------------------------------------------------------------------------
       
    80 // CUPnPPlaylistServices::CUPnPPlaylistServices
       
    81 // Default constructor.
       
    82 // --------------------------------------------------------------------------
       
    83 //    
       
    84 CUPnPPlaylistServices::CUPnPPlaylistServices()
       
    85     {
       
    86     }
       
    87 
       
    88 
       
    89 // --------------------------------------------------------------------------
       
    90 // CUPnPPlaylistServices::CUPnPPlaylistServices
       
    91 // 2nd phase constructor
       
    92 // --------------------------------------------------------------------------
       
    93 //    
       
    94 void CUPnPPlaylistServices::ConstructL()
       
    95     {
       
    96     __LOG( "CUPnPPlaylistServices::ConstructL" );
       
    97     iStatus = KErrNone;
       
    98     
       
    99     iCollectionUtility = MMPXCollectionUtility::NewL(
       
   100         (MMPXCollectionObserver*)0, KMcModePlaylist );
       
   101             
       
   102     // Initialize/Merge music collection databases
       
   103     RArray<TUid> uid;
       
   104     CleanupClosePushL( uid );
       
   105     uid.AppendL( TUid::Uid( EMPXCollectionPluginMusic ) );
       
   106     TUid collection = iCollectionUtility->CollectionIDL( uid.Array() );
       
   107     TRAP( iStatus, iCollectionUtility->Collection().CommandL(
       
   108         EMcCmdCollectionInit,  collection.iUid ) );
       
   109     CleanupStack::PopAndDestroy( &uid );
       
   110 
       
   111     iMpxHelper = CUPnPMpxHelper::NewL( KHomeConnectUid );
       
   112     
       
   113     
       
   114     __LOG1( "CUPnPPlaylistServices::ConstructL end iStatus %d",
       
   115          iStatus );
       
   116     }
       
   117 
       
   118 
       
   119 // --------------------------------------------------------------------------
       
   120 // CUPnPPlaylistServices::~CUPnPPlaylistServices
       
   121 // Destructor.
       
   122 // --------------------------------------------------------------------------
       
   123 //
       
   124 EXPORT_C CUPnPPlaylistServices::~CUPnPPlaylistServices()
       
   125     {
       
   126     __LOG( "CUPnPPlaylistServices ~()" );
       
   127     if ( iCollectionUtility )
       
   128         {
       
   129         iCollectionUtility->Close();
       
   130         iCollectionUtility = 0;
       
   131         }
       
   132 
       
   133     delete iMpxHelper;
       
   134     }
       
   135 
       
   136 // --------------------------------------------------------------------------
       
   137 // CUPnPPlaylistServices::ListPlaylistsL
       
   138 // --------------------------------------------------------------------------
       
   139 //
       
   140 EXPORT_C void CUPnPPlaylistServices::ListPlaylistsL(
       
   141     CDesCArray& aPlaylistIds,
       
   142     CDesCArray& aPlaylistNames )
       
   143     {
       
   144     __LOG( "ListPlaylistsL()" );
       
   145     
       
   146     if( iStatus != KErrNone)
       
   147         {
       
   148         __LOG1( "CUPnPPlaylistServices::ListPlaylistsL iStatus %d return",
       
   149          iStatus );
       
   150         return;
       
   151         }
       
   152         
       
   153     CMPXMedia* result = FindItemL( EPlaylists,
       
   154         KNullDesC, &KMPXMediaGeneralId, &KMPXMediaGeneralTitle );
       
   155     if ( result != 0 )
       
   156         {
       
   157         CleanupStack::PushL( result );
       
   158         __LOG("getting media array");
       
   159         const CMPXMediaArray* medias = result->Value<CMPXMediaArray>(
       
   160             KMPXMediaArrayContents );
       
   161         TInt count = medias->Count();
       
   162         for( TInt i = 0; i < count; ++i )
       
   163             {
       
   164             const CMPXMedia* entry = (*medias)[i];
       
   165             TMPXItemId id = *entry->Value<TMPXItemId>(
       
   166                 KMPXMediaGeneralId );
       
   167             const TDesC& title = entry->ValueText(
       
   168                 KMPXMediaGeneralTitle );
       
   169             __LOG3( "media array %d/%d [%S]",
       
   170                 i, count, &title );
       
   171             aPlaylistIds.AppendL( Id2Desc( id ) );
       
   172             aPlaylistNames.AppendL( title );
       
   173             }
       
   174         CleanupStack::PopAndDestroy( result );
       
   175         }
       
   176     }
       
   177 
       
   178 
       
   179 // --------------------------------------------------------------------------
       
   180 // CUPnPPlaylistServices::OpenPlaylistL
       
   181 // --------------------------------------------------------------------------
       
   182 //
       
   183 EXPORT_C void CUPnPPlaylistServices::OpenPlaylistL(
       
   184     const TDesC& aPlaylistId,
       
   185     CDesCArray& aContentMedia )
       
   186     {
       
   187     __LOG1( "OpenPlaylistL(%S)", &aPlaylistId );
       
   188 
       
   189     if( iStatus != KErrNone)
       
   190         {
       
   191         __LOG1( "CUPnPPlaylistServices::OpenPlaylistL iStatus %d return",
       
   192          iStatus );
       
   193         return;
       
   194         }
       
   195         
       
   196     CMPXMedia* result = FindItemL( EPlaylistById, aPlaylistId,
       
   197         &KMPXMediaArrayContents, &KMPXMediaGeneralUri );
       
   198     if ( result != 0 )
       
   199         {
       
   200         CleanupStack::PushL( result );
       
   201         const CMPXMediaArray* resultMedias =
       
   202             result->Value<CMPXMediaArray>( KMPXMediaArrayContents );
       
   203 
       
   204         if ( resultMedias->Count() == 1 )
       
   205             {
       
   206             // fetch content 
       
   207             CMPXMedia* playlist = FetchPlaylistContentL( aPlaylistId );              
       
   208             if( playlist != 0 )
       
   209                 {
       
   210                 CleanupStack::PushL( playlist );
       
   211                 const CMPXMediaArray* playlistMedias =
       
   212                 playlist->Value<CMPXMediaArray>( KMPXMediaArrayContents );
       
   213                 if ( playlistMedias )
       
   214                     {
       
   215                     TInt count = playlistMedias->Count();
       
   216                     for( TInt i = 0; i < count; ++i )
       
   217                         {
       
   218                         __LOG2( "accessing mediaList: item %d of %d",
       
   219                             i, count );
       
   220                         const CMPXMedia* entry = (*playlistMedias)[i];
       
   221                         aContentMedia.AppendL( entry->ValueText(
       
   222                             KMPXMediaGeneralUri ) );
       
   223                         }
       
   224                     }
       
   225                 CleanupStack::PopAndDestroy( playlist );
       
   226                 }
       
   227             }
       
   228         else
       
   229             {
       
   230             __LOG1("Unknown result medias count: %d",
       
   231                 resultMedias->Count() );
       
   232             }
       
   233         CleanupStack::PopAndDestroy( result );
       
   234         }
       
   235     else
       
   236         {
       
   237         User::Leave( KErrNotFound );
       
   238         }
       
   239     // now ready to return
       
   240     }
       
   241 
       
   242 
       
   243 // --------------------------------------------------------------------------
       
   244 // CUPnPPlaylistServices::CreateTrackL
       
   245 // --------------------------------------------------------------------------
       
   246 //
       
   247 EXPORT_C void CUPnPPlaylistServices::CreateTrackL(
       
   248     const TDesC& aTrackPath,
       
   249     const CUpnpItem& aTrackMetadata )
       
   250     {
       
   251     __LOG1( "CreateTrackL(%S)", &aTrackPath );
       
   252 
       
   253     if( iStatus != KErrNone)
       
   254         {
       
   255         __LOG1( "CUPnPPlaylistServices::CreateTrackL iStatus %d return",
       
   256          iStatus );
       
   257         return;
       
   258         }
       
   259         
       
   260     CMPXMedia* item = CMPXMedia::NewL();
       
   261     CleanupStack::PushL( item );
       
   262     item->SetTObjectValueL<TUid>(
       
   263         KMPXMediaGeneralCollectionId, KMpxLocalCollectionUid );
       
   264     item->SetTObjectValueL<TMPXGeneralType>(
       
   265         KMPXMediaGeneralType, EMPXItem );
       
   266     item->SetTObjectValueL<TMPXGeneralCategory>(
       
   267         KMPXMediaGeneralCategory, EMPXSong );
       
   268     item->SetTextValueL(
       
   269         KMPXMediaGeneralUri, aTrackPath );
       
   270     // Insert metadata
       
   271     HBufC16* buf = NULL;
       
   272     // title
       
   273     buf = EscapeUtils::ConvertToUnicodeFromUtf8L( aTrackMetadata.Title() );
       
   274     CleanupStack::PushL( buf );
       
   275     item->SetTextValueL(
       
   276         KMPXMediaGeneralTitle, *buf );
       
   277     CleanupStack::PopAndDestroy( buf );
       
   278     // artist
       
   279     if ( ( buf = GetElementLC( aTrackMetadata, KElementArtist ) ) != 0 )
       
   280         {
       
   281         item->SetTextValueL(
       
   282             KMPXMediaMusicArtist, *buf );
       
   283         CleanupStack::PopAndDestroy( buf );
       
   284         }
       
   285     else if ( ( buf = GetElementLC( aTrackMetadata, KElementCreator ) ) != 0 )
       
   286         {
       
   287         item->SetTextValueL(
       
   288             KMPXMediaMusicArtist, *buf );
       
   289         CleanupStack::PopAndDestroy( buf );
       
   290         }
       
   291     // album
       
   292     if ( ( buf = GetElementLC( aTrackMetadata, KElementAlbum ) ) != 0 )
       
   293         {
       
   294         item->SetTextValueL(
       
   295             KMPXMediaMusicAlbum, *buf );
       
   296         CleanupStack::PopAndDestroy( buf );
       
   297         }
       
   298     // genre
       
   299     if ( ( buf = GetElementLC( aTrackMetadata, KElementGenre ) ) != 0 )
       
   300         {
       
   301         item->SetTextValueL(
       
   302             KMPXMediaMusicGenre, *buf );
       
   303         CleanupStack::PopAndDestroy( buf );
       
   304         }
       
   305     // date and year
       
   306     const CUpnpElement* elem = UPnPItemUtility::FindElementByName(
       
   307         aTrackMetadata, KElementDate );
       
   308     if ( elem != 0 )
       
   309         {
       
   310         TTime timestamp;
       
   311         TInt err = 
       
   312             UPnPItemUtility::UPnPDateAsTTime( elem->Value(), timestamp );
       
   313         if( err == KErrNone )
       
   314             {
       
   315             item->SetTObjectValueL<TInt64>( 
       
   316                 KMPXMediaGeneralDate, timestamp.Int64() );
       
   317             
       
   318             item->SetTObjectValueL<TInt64>( 
       
   319                 KMPXMediaMusicYear, timestamp.Int64() );
       
   320             }
       
   321         }
       
   322         
       
   323     // duration
       
   324     const CUpnpElement* trackResource = 
       
   325         &UPnPItemUtility::ResourceFromItemL( aTrackMetadata );
       
   326      
       
   327     if( trackResource != 0 )
       
   328         {
       
   329         const CUpnpAttribute* attr = UPnPItemUtility
       
   330             ::FindAttributeByName( *trackResource, KAttributeDuration );
       
   331 
       
   332         if ( attr != 0 )
       
   333             {
       
   334             TInt ms = 0;
       
   335             UPnPItemUtility
       
   336                 ::UPnPDurationAsMilliseconds( attr->Value(), ms );
       
   337             
       
   338             item->SetTObjectValueL<TInt>(
       
   339                 KMPXMediaGeneralDuration, ms );
       
   340             }
       
   341         }
       
   342 
       
   343     // track number
       
   344     if ( ( buf = GetElementLC( aTrackMetadata, KElementTrackNumber ) ) != 0 )
       
   345         {
       
   346         item->SetTextValueL(
       
   347             KMPXMediaMusicAlbumTrack, *buf );
       
   348         CleanupStack::PopAndDestroy( buf );
       
   349         }
       
   350 
       
   351     __LOG("...about to add track...");
       
   352     iMpxHelper->AddTrackL( item );
       
   353 
       
   354     CleanupStack::PopAndDestroy( item );
       
   355     }
       
   356 
       
   357 // --------------------------------------------------------------------------
       
   358 // CUPnPPlaylistServices::GetElement
       
   359 // --------------------------------------------------------------------------
       
   360 //
       
   361 HBufC16* CUPnPPlaylistServices::GetElementLC(
       
   362     const CUpnpItem& aSource, const TDesC8& aSourceField ) const
       
   363     {
       
   364     HBufC16* result = 0;
       
   365     const CUpnpElement* elem = UPnPItemUtility::FindElementByName(
       
   366         aSource, aSourceField );
       
   367     if ( elem != 0 )
       
   368         {
       
   369         result = EscapeUtils::ConvertToUnicodeFromUtf8L( elem->Value() );
       
   370         CleanupStack::PushL( result );
       
   371         }
       
   372     return result;
       
   373     }
       
   374 
       
   375 // --------------------------------------------------------------------------
       
   376 // CUPnPPlaylistServices::CreatePlaylistL
       
   377 // --------------------------------------------------------------------------
       
   378 //
       
   379 EXPORT_C void CUPnPPlaylistServices::CreatePlaylistL(
       
   380     const TDesC& aPlaylistName,
       
   381     const MDesCArray& aTrackPaths,
       
   382     TDes* aPlaylistId )
       
   383     {
       
   384     __LOG1( "CreatePlaylistL(%S)", &aPlaylistName );
       
   385 
       
   386     if( iStatus != KErrNone)
       
   387         {
       
   388         __LOG1( "CUPnPPlaylistServices::CreatePlaylistL iStatus %d return",
       
   389          iStatus );
       
   390         return;
       
   391         }
       
   392         
       
   393     // create a media list
       
   394     CMPXMediaArray* medias = CMPXMediaArray::NewL();
       
   395     CleanupStack::PushL( medias );
       
   396 
       
   397     // add track info into the media list
       
   398     for( TInt i = 0; i < aTrackPaths.MdcaCount(); ++i )
       
   399         {
       
   400         TPtrC16 trackUri  = aTrackPaths.MdcaPoint(i);
       
   401         CMPXMedia* entry = CMPXMedia::NewL();
       
   402         CleanupStack::PushL( entry );
       
   403         entry->SetTObjectValueL<TUid>(
       
   404             KMPXMediaGeneralCollectionId, KMpxLocalCollectionUid );
       
   405         entry->SetTObjectValueL<TMPXGeneralType>(
       
   406             KMPXMediaGeneralType, EMPXItem );
       
   407         entry->SetTObjectValueL<TMPXGeneralCategory>(
       
   408             KMPXMediaGeneralCategory, EMPXSong );
       
   409         entry->SetTextValueL(
       
   410             KMPXMediaGeneralUri, trackUri );
       
   411         medias->AppendL( entry );
       
   412         CleanupStack::Pop( entry );
       
   413         }
       
   414 
       
   415     // create a media item for the playlist
       
   416     CMPXMedia* playlist = CMPXMedia::NewL();
       
   417     CleanupStack::PushL( playlist );
       
   418 
       
   419     CUPnPSettingsEngine* engine = CUPnPSettingsEngine::NewL();;
       
   420     CleanupStack::PushL( engine );
       
   421     
       
   422     HBufC* location = HBufC::NewLC( KMaxFileName );
       
   423     TPtr locationPtr( location->Des() );
       
   424     TBool isPhoneMemory;
       
   425     engine->GetCopyLocationL( locationPtr, isPhoneMemory );
       
   426 
       
   427     // add playlist info into the playlist media
       
   428     playlist->SetTObjectValueL<TUid>(
       
   429         KMPXMediaGeneralCollectionId, KMpxLocalCollectionUid );
       
   430     playlist->SetTObjectValueL<TMPXGeneralType>(
       
   431         KMPXMediaGeneralType, EMPXItem );
       
   432     playlist->SetTObjectValueL<TMPXGeneralCategory>(
       
   433         KMPXMediaGeneralCategory, EMPXPlaylist );
       
   434     playlist->SetTextValueL(
       
   435         KMPXMediaGeneralTitle, aPlaylistName );
       
   436     playlist->SetTextValueL(
       
   437         KMPXMediaGeneralUri, locationPtr );
       
   438     playlist->SetCObjectValueL(
       
   439         KMPXMediaArrayContents, medias );
       
   440     playlist->SetTObjectValueL<TInt>(
       
   441         KMPXMediaArrayCount, medias->Count() );
       
   442 
       
   443     // add in collection
       
   444     __LOG("...about to add playlist...");
       
   445     iMpxHelper->AddPlaylistL( playlist );
       
   446     
       
   447     iCurrentId = *playlist->Value<TMPXItemId>(
       
   448         KMPXMediaGeneralId );
       
   449 
       
   450     CleanupStack::PopAndDestroy( location );
       
   451     CleanupStack::PopAndDestroy( engine );
       
   452     CleanupStack::PopAndDestroy( playlist );
       
   453     CleanupStack::PopAndDestroy( medias );
       
   454 
       
   455     // Find out playlist ID
       
   456     if ( aPlaylistId )
       
   457         {
       
   458         __LOG("...about to retrieve playlist id...");
       
   459         aPlaylistId->Copy( Id2Desc( iCurrentId ) );
       
   460         }
       
   461     }
       
   462 
       
   463 // --------------------------------------------------------------------------
       
   464 // CUPnPPlaylistServices::AddMediaToPlaylistL
       
   465 // --------------------------------------------------------------------------
       
   466 //
       
   467 EXPORT_C void CUPnPPlaylistServices::AddMediaToPlaylistL(
       
   468     const TDesC& aPlaylistId,
       
   469     const TDesC& aTrackPath )
       
   470     {
       
   471     __LOG2( "AddMediaToPlaylistL(%S,%S)",
       
   472         &aPlaylistId, &aTrackPath );
       
   473     
       
   474     if( iStatus != KErrNone)
       
   475         {
       
   476         __LOG1( "CUPnPPlaylistServices::AddMediaToPlaylistL iStatus %d return",
       
   477          iStatus );
       
   478         return;
       
   479         }
       
   480         
       
   481     // create a media list
       
   482     CMPXMediaArray* medias = CMPXMediaArray::NewL();
       
   483     CleanupStack::PushL( medias );
       
   484 
       
   485     // add new track to media list
       
   486     CMPXMedia* entry = CMPXMedia::NewL();
       
   487     CleanupStack::PushL( entry );
       
   488     entry->SetTObjectValueL<TUid>(
       
   489         KMPXMediaGeneralCollectionId, KMpxLocalCollectionUid );
       
   490     entry->SetTObjectValueL<TMPXGeneralType>(
       
   491         KMPXMediaGeneralType, EMPXItem );
       
   492     entry->SetTObjectValueL<TMPXGeneralCategory>(
       
   493         KMPXMediaGeneralCategory, EMPXSong );
       
   494     entry->SetTextValueL(
       
   495         KMPXMediaGeneralUri, aTrackPath );
       
   496     medias->AppendL( entry );
       
   497     CleanupStack::Pop( entry );
       
   498 
       
   499     // create the playlist delta
       
   500     CMPXMedia* playlistDelta = CMPXMedia::NewL();
       
   501     CleanupStack::PushL( playlistDelta );
       
   502 
       
   503     // add playlist info into the playlist delta media
       
   504     playlistDelta->SetTObjectValueL<TUid>(
       
   505         KMPXMediaGeneralCollectionId, KMpxLocalCollectionUid );
       
   506     playlistDelta->SetTObjectValueL<TMPXGeneralType>(
       
   507         KMPXMediaGeneralType, EMPXItem );
       
   508     playlistDelta->SetTObjectValueL<TMPXGeneralCategory>(
       
   509         KMPXMediaGeneralCategory, EMPXPlaylist );
       
   510     playlistDelta->SetTObjectValueL<TMPXItemId>(
       
   511         KMPXMediaGeneralId, Desc2Id( aPlaylistId ) );
       
   512     playlistDelta->SetCObjectValueL(
       
   513         KMPXMediaArrayContents, medias ); // new content
       
   514     playlistDelta->SetTObjectValueL<TInt>(
       
   515         KMPXMediaArrayCount, medias->Count() );
       
   516 
       
   517     // add in collection
       
   518     __LOG("Add playlist delta to collection");
       
   519     iMpxHelper->AddPlaylistL( playlistDelta );
       
   520 
       
   521     CleanupStack::PopAndDestroy( playlistDelta );
       
   522     CleanupStack::PopAndDestroy( medias );
       
   523     }
       
   524 
       
   525 
       
   526 // --------------------------------------------------------------------------
       
   527 // CUPnPPlaylistServices::DeleteTrackL
       
   528 // --------------------------------------------------------------------------
       
   529 //
       
   530 EXPORT_C void CUPnPPlaylistServices::DeleteTrackL(
       
   531     const TDesC& aContentFile )
       
   532     {
       
   533     __LOG1( "DeleteTrackL(%S)", &aContentFile );
       
   534     
       
   535     if( iStatus != KErrNone)
       
   536         {
       
   537         __LOG1( "CUPnPPlaylistServices::DeleteTrackL iStatus %d return",
       
   538          iStatus );
       
   539         return;
       
   540         }
       
   541         
       
   542     // item to delete
       
   543     CMPXMedia* item = CMPXMedia::NewL();
       
   544     CleanupStack::PushL( item );
       
   545     item->SetTObjectValueL<TUid>(
       
   546         KMPXMediaGeneralCollectionId, KMpxLocalCollectionUid );
       
   547     item->SetTObjectValueL<TMPXGeneralType>(
       
   548         KMPXMediaGeneralType, EMPXItem );
       
   549     item->SetTObjectValueL<TMPXGeneralCategory>(
       
   550         KMPXMediaGeneralCategory, EMPXSong );
       
   551     item->SetTextValueL(
       
   552         KMPXMediaGeneralUri, aContentFile );
       
   553     iCollectionUtility->Collection().RemoveL( *item );
       
   554     CleanupStack::PopAndDestroy( item );
       
   555     }
       
   556 
       
   557 // --------------------------------------------------------------------------
       
   558 // CUPnPPlaylistServices::DeletePlaylistL
       
   559 // --------------------------------------------------------------------------
       
   560 //
       
   561 EXPORT_C void CUPnPPlaylistServices::DeletePlaylistL(
       
   562     const TDesC& aPlaylistId )
       
   563     {
       
   564     __LOG1( "DeletePlaylistL(%S)", &aPlaylistId );
       
   565     
       
   566     if( iStatus != KErrNone)
       
   567         {
       
   568         __LOG1( "CUPnPPlaylistServices::DeletePlaylistL iStatus %d return",
       
   569          iStatus );
       
   570         return;
       
   571         }
       
   572         
       
   573     // item to delete
       
   574     CMPXMedia* playlist = CMPXMedia::NewL();
       
   575     CleanupStack::PushL( playlist );
       
   576     playlist->SetTObjectValueL<TUid>(
       
   577         KMPXMediaGeneralCollectionId, KMpxLocalCollectionUid );
       
   578     playlist->SetTObjectValueL<TMPXGeneralType>(
       
   579         KMPXMediaGeneralType, EMPXItem );
       
   580     playlist->SetTObjectValueL<TMPXGeneralCategory>(
       
   581         KMPXMediaGeneralCategory, EMPXPlaylist );
       
   582     playlist->SetTObjectValueL<TMPXItemId>(
       
   583         KMPXMediaGeneralId, Desc2Id( aPlaylistId ) );
       
   584     iCollectionUtility->Collection().RemoveL( *playlist );
       
   585     CleanupStack::PopAndDestroy( playlist );
       
   586     }
       
   587 
       
   588 // --------------------------------------------------------------------------
       
   589 // CUPnPPlaylistServices::IsValidTrackL
       
   590 // --------------------------------------------------------------------------
       
   591 //
       
   592 EXPORT_C TBool CUPnPPlaylistServices::IsValidTrackL(
       
   593     const TDesC& aContentFile )
       
   594     {
       
   595     __LOG1( "IsValidTrackL(%S)", &aContentFile );
       
   596     
       
   597     if( iStatus != KErrNone)
       
   598         {
       
   599         __LOG1( "CUPnPPlaylistServices::IsValidTrackL iStatus %d return",
       
   600          iStatus );
       
   601         return EFalse;
       
   602         }
       
   603         
       
   604     TBool found = EFalse;
       
   605     CMPXMedia* track = FindItemL( ESongByUri, aContentFile );
       
   606     if ( track != 0 )
       
   607         {
       
   608         CleanupStack::PushL( track );
       
   609         TInt count = *track->Value<TInt>( KMPXMediaArrayCount );
       
   610         if ( count >= 0 )
       
   611             {
       
   612             found = ETrue;
       
   613             }
       
   614         CleanupStack::PopAndDestroy( track );
       
   615         }
       
   616 
       
   617     return found;
       
   618     }
       
   619 
       
   620 // --------------------------------------------------------------------------
       
   621 // CUPnPPlaylistServices::IsValidPlaylistL
       
   622 // --------------------------------------------------------------------------
       
   623 //
       
   624 EXPORT_C TBool CUPnPPlaylistServices::IsValidPlaylistL(
       
   625     const TDesC& aPlaylistTitle )
       
   626     {
       
   627     __LOG1( "IsValidPlaylistL(%S)", &aPlaylistTitle );
       
   628     
       
   629     if( iStatus != KErrNone)
       
   630         {
       
   631         __LOG1( "CUPnPPlaylistServices::IsValidPlaylistL iStatus %d return",
       
   632          iStatus );
       
   633         return EFalse;
       
   634         }
       
   635         
       
   636     TBool found = EFalse;
       
   637     // find the playlist
       
   638     CMPXMedia* playlist = FindItemL( EPlaylistByTitle, aPlaylistTitle );
       
   639     if ( playlist != 0 )
       
   640         {
       
   641         CleanupStack::PushL( playlist );
       
   642         TInt count = *playlist->Value<TInt>( KMPXMediaArrayCount );
       
   643         if ( count >= 0 )
       
   644             {
       
   645             found = ETrue;
       
   646             }
       
   647         CleanupStack::PopAndDestroy( playlist );
       
   648         }
       
   649         
       
   650     return found;
       
   651     }
       
   652 
       
   653 
       
   654 // --------------------------------------------------------------------------
       
   655 // Find a playlist
       
   656 // --------------------------------------------------------------------------
       
   657 //
       
   658 CMPXMedia* CUPnPPlaylistServices::FindItemL(
       
   659     TFindCategory aCategory,
       
   660     const TDesC& aKey,
       
   661     const TMPXAttributeData* aAttribute1,
       
   662     const TMPXAttributeData* aAttribute2,
       
   663     const TMPXAttributeData* aAttribute3 )
       
   664     {
       
   665     __LOG( "FindItemL" );
       
   666     CMPXMedia* criteria = CMPXMedia::NewL();
       
   667     CleanupStack::PushL( criteria );
       
   668     criteria->SetTObjectValueL<TUid>(
       
   669         KMPXMediaGeneralCollectionId, KMpxLocalCollectionUid );
       
   670     criteria->SetTObjectValueL<TMPXGeneralType>(
       
   671         KMPXMediaGeneralType, EMPXItem );
       
   672 
       
   673     // set search keys
       
   674     if ( aCategory == ESongs ||
       
   675         aCategory == ESongByUri )
       
   676         {
       
   677         criteria->SetTObjectValueL<TMPXGeneralCategory>(
       
   678             KMPXMediaGeneralCategory, EMPXSong );
       
   679         if ( aCategory == ESongByUri )
       
   680             {
       
   681             criteria->SetTextValueL( 
       
   682                 KMPXMediaGeneralUri, aKey );
       
   683             }
       
   684         }
       
   685     else if ( aCategory == EPlaylists ||
       
   686         aCategory == EPlaylistById ||
       
   687         aCategory == EPlaylistByTitle )
       
   688         {
       
   689         criteria->SetTObjectValueL<TMPXGeneralCategory>(
       
   690             KMPXMediaGeneralCategory, EMPXPlaylist );
       
   691         if ( aCategory == EPlaylistById )
       
   692             {
       
   693             criteria->SetTObjectValueL<TMPXItemId>(
       
   694                 KMPXMediaGeneralId, Desc2Id( aKey ) );
       
   695             }
       
   696         else if ( aCategory == EPlaylistByTitle )
       
   697             {
       
   698             criteria->SetTextValueL( 
       
   699                 KMPXMediaGeneralTitle, aKey );
       
   700             }
       
   701         }
       
   702     else
       
   703         {
       
   704         __PANICD( __FILE__, __LINE__ );
       
   705         }
       
   706 
       
   707 
       
   708     // define attributes fetched
       
   709     RArray<TMPXAttribute> attributes;
       
   710     CleanupClosePushL( attributes );
       
   711     if ( aAttribute1 )
       
   712         {
       
   713         // add attribute 1
       
   714         attributes.AppendL( *aAttribute1 );
       
   715         if ( aAttribute2 )
       
   716             {
       
   717             // add attribute 2
       
   718             attributes.AppendL( *aAttribute2 );
       
   719             if ( aAttribute3 )
       
   720                 {
       
   721                 // add attribute 3
       
   722                 attributes.AppendL( *aAttribute3 );
       
   723                 }
       
   724             }
       
   725         }
       
   726     else
       
   727         {
       
   728         // add a dummy attribute because the query will fail if
       
   729         // there are no attributes
       
   730         attributes.AppendL( KMPXMediaGeneralId );
       
   731         }
       
   732 
       
   733     // now find
       
   734     CMPXMedia* result = 0;
       
   735     TRAPD( err, result = iCollectionUtility->Collection()
       
   736         .FindAllL( *criteria , attributes.Array() ) );
       
   737     CleanupStack::PopAndDestroy(); // attributes
       
   738     CleanupStack::PopAndDestroy( criteria );
       
   739 
       
   740     if ( err == KErrNotFound ||
       
   741          err == KErrPathNotFound )
       
   742         {
       
   743         __LOG1( "FindAll: err=%d - not found.", err );
       
   744         result = 0;
       
   745         }
       
   746     else if ( err != KErrNone )
       
   747         {
       
   748         __LOG1("FindAll: error=%d", err);
       
   749         User::Leave( err );
       
   750         }
       
   751     else
       
   752         {
       
   753         __LOG( "FindAll: ok" );
       
   754         }
       
   755 
       
   756     return result;
       
   757     }
       
   758 
       
   759 
       
   760 // --------------------------------------------------------------------------
       
   761 // Fetch a playlist content
       
   762 // --------------------------------------------------------------------------
       
   763 //   
       
   764 CMPXMedia* CUPnPPlaylistServices::FetchPlaylistContentL( const TDesC& aId )
       
   765     {
       
   766     __LOG( "FindPlaylistContentL" );
       
   767     CMPXMedia* criteria = CMPXMedia::NewL();
       
   768     CleanupStack::PushL( criteria );
       
   769     criteria->SetTObjectValueL<TUid>(
       
   770         KMPXMediaGeneralCollectionId, KMpxLocalCollectionUid );
       
   771     criteria->SetTObjectValueL<TMPXGeneralType>(
       
   772         KMPXMediaGeneralType, EMPXGroup );
       
   773 
       
   774     // set search keys
       
   775     criteria->SetTObjectValueL<TMPXGeneralCategory>(
       
   776         KMPXMediaGeneralCategory, EMPXSong );    
       
   777     criteria->SetTObjectValueL<TMPXItemId>(
       
   778         KMPXMediaGeneralId, Desc2Id( aId ) );
       
   779         
       
   780     // define attributes fetched
       
   781     RArray<TMPXAttribute> attributes;
       
   782     CleanupClosePushL( attributes );
       
   783     attributes.AppendL( KMPXMediaGeneralUri );
       
   784     
       
   785     // now find
       
   786     CMPXMedia* result = 0;
       
   787     TRAPD( err, result = iCollectionUtility->Collection()
       
   788         .FindAllL( *criteria , attributes.Array() ) );
       
   789     CleanupStack::PopAndDestroy(); // attributes
       
   790     CleanupStack::PopAndDestroy( criteria );
       
   791     
       
   792     if ( err == KErrNotFound ||
       
   793          err == KErrPathNotFound )
       
   794         {
       
   795         __LOG1( "FindAll: err=%d - not found.", err );
       
   796         result = 0;
       
   797         }
       
   798     else if ( err != KErrNone )
       
   799         {
       
   800         __LOG1("FindAll: error=%d", err);
       
   801         User::Leave( err );
       
   802         }
       
   803     else
       
   804         {
       
   805         __LOG( "FindAll: ok" );
       
   806         }
       
   807 
       
   808     return result;
       
   809     }
       
   810 
       
   811 // --------------------------------------------------------------------------
       
   812 // Converts an ID from TMPXItemId form to descriptor form.
       
   813 // --------------------------------------------------------------------------
       
   814 //
       
   815 const TDesC& CUPnPPlaylistServices::Id2Desc( const TMPXItemId& aId )
       
   816     {  
       
   817     iTempBuffer.Num( aId );
       
   818     return iTempBuffer;
       
   819     }
       
   820 
       
   821 // --------------------------------------------------------------------------
       
   822 // Converts an ID from descriptor form to TMPXItemId form.
       
   823 // --------------------------------------------------------------------------
       
   824 //
       
   825 TMPXItemId CUPnPPlaylistServices::Desc2Id( const TDesC& aDesc )
       
   826     {
       
   827     TLex convert( aDesc );
       
   828     TUint temp;
       
   829     convert.Val( temp, EDecimal );
       
   830     TMPXItemId id(temp);
       
   831     return id;
       
   832     }