upnpmpxplugins/upnpplaybackplugins/src/upnptrack.cpp
branchIOP_Improvements
changeset 40 08b5eae9f9ff
parent 39 6369bfd1b60d
child 41 b4d83ea1d6e2
equal deleted inserted replaced
39:6369bfd1b60d 40:08b5eae9f9ff
     1 /*
       
     2 * Copyright (c) 2008 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:      Class for containing and obtaining music track-specific data
       
    15 *                from media server
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 
       
    21 
       
    22 
       
    23 
       
    24 // INCLUDES
       
    25 #include <mpxmediaaudiodefs.h>
       
    26 #include <mpxmediamusicdefs.h>
       
    27 #include <mpxmediageneraldefs.h>
       
    28 #include <mpxmedia.h>
       
    29 #include <mpxattribute.h>
       
    30 #include <mpxplaybackpluginobserver.h>
       
    31 #include <mpxmessagegeneraldefs.h> // for messaging
       
    32 #include <mpxplaybackmessagedefs.h> // for messaging
       
    33 
       
    34 #include <escapeutils.h> // for unicode conversion
       
    35 
       
    36 #include <upnpitem.h>
       
    37 #include <upnpobject.h>
       
    38 #include "upnpitemutility.h" // for FindElementByName & ResourceFromItemL
       
    39 #include "upnpfileutility.h" // for IsFileProtectedL
       
    40 #include "upnpconstantdefs.h" // browse filtering and item element names
       
    41 #include <upnpdlnaprotocolinfo.h> // for resolving object mimetype
       
    42 #include "upnpavcontroller.h"
       
    43 #include "upnpavbrowsingsession.h"
       
    44 #include "upnpavdevice.h"
       
    45 #include "upnpavdevicelist.h"
       
    46 
       
    47 #include "upnpitemresolverobserver.h" // MUPnPItemResolverObserver
       
    48 #include "upnpitemresolverfactory.h" // factory class
       
    49 #include "upnpitemresolver.h" // MUPnPItemResolver
       
    50 
       
    51 #include "upnptrackobserver.h"
       
    52 #include "upnptrack.h"
       
    53 
       
    54 _LIT( KComponentLogfile, "musicplugins.txt");
       
    55 #include "upnplog.h"
       
    56 
       
    57 // CONSTANTS
       
    58 _LIT16( KUPnPPrefix, "upnp:" );   // Prefix for separate local/remote song
       
    59 const TInt KUPnPPrefixLength = 5;
       
    60 const TInt KCharCodeColon = 58;
       
    61 const TInt KCharCodeSeparate = 42;
       
    62 const TInt InitialTrackDuration = 1;
       
    63 _LIT( KTimeFormatYearOnly, "%F%Y" );
       
    64 
       
    65 // ======== MEMBER FUNCTIONS ========
       
    66 
       
    67 // --------------------------------------------------------------------------
       
    68 // CUPnPTrack::NewL
       
    69 // 1st phase constructor.
       
    70 // --------------------------------------------------------------------------
       
    71 //
       
    72 CUPnPTrack* CUPnPTrack::NewL( MUPnPAVController& aAvController )
       
    73     {
       
    74     __LOG( "CUPnPTrack::NewL" );
       
    75     CUPnPTrack* self = new( ELeave ) CUPnPTrack( aAvController );
       
    76     return self;
       
    77     }
       
    78 
       
    79 // --------------------------------------------------------------------------
       
    80 // CUPnPTrack::Delete
       
    81 // deletor
       
    82 // --------------------------------------------------------------------------
       
    83 //
       
    84 void CUPnPTrack::Delete()
       
    85     {
       
    86     __LOG1( "CUPnPTrack::Delete, state=%d", iState );
       
    87     if ( iState == EStateResolving )
       
    88         {
       
    89         // asynchronous delete - will be deleted after
       
    90         // asynchronous operation is complete. This is because the
       
    91         // S60 command can not be cancelled
       
    92         iState = EStateSelfDestruct;
       
    93         }
       
    94     else
       
    95         {
       
    96         delete this;
       
    97         }
       
    98     }
       
    99 
       
   100 // --------------------------------------------------------------------------
       
   101 // CUPnPTrack::CUPnPTrack
       
   102 // Default constructor.
       
   103 // --------------------------------------------------------------------------
       
   104 // 
       
   105 CUPnPTrack::CUPnPTrack( MUPnPAVController& aAvController )
       
   106     : iAvController( aAvController )
       
   107     , iQueriedAttributes()
       
   108     {
       
   109     iBrowsingSession = NULL;
       
   110     iOriginalURI = NULL;
       
   111     iMediaServer = NULL;
       
   112     iObjectId = NULL;
       
   113     iTrackObserver = NULL;
       
   114     // Use initial duration value (1). It will guarantee that in
       
   115     // case were remote device does not support duration query and playback
       
   116     // is unpaused, SetPosition is called with real value even
       
   117     // if remote player does not return real value for getposition
       
   118     // call. Note that SetPosition(0) is used when user really wants
       
   119     // to move to track beginning.
       
   120     iTrackDuration = InitialTrackDuration;
       
   121     iIsItemSolved = EFalse;
       
   122     iState = EStateIdle;
       
   123     }
       
   124 
       
   125 // --------------------------------------------------------------------------
       
   126 // CUPnPTrack::~CUPnPTrack
       
   127 // destructor.
       
   128 // --------------------------------------------------------------------------
       
   129 //
       
   130 CUPnPTrack::~CUPnPTrack()
       
   131     {
       
   132     __LOG( "CUPnPTrack::~CUPnPTrack()" );
       
   133     StopBrowsingSession();
       
   134 
       
   135     // Free memory of owned members
       
   136     delete iOriginalURI;
       
   137     delete iMediaServer;        
       
   138     delete iObjectId;
       
   139     delete iItemResolver;
       
   140     iItemResolver = 0;
       
   141     __LOG( "CUPnPTrack::~CUPnPTrack End" );
       
   142     }
       
   143 
       
   144 // --------------------------------------------------------------------------
       
   145 // CUPnPTrack::ResolveURIL
       
   146 // Resolve local / remote URI.
       
   147 // --------------------------------------------------------------------------
       
   148 //
       
   149 void CUPnPTrack::ResolveURIL( const TDesC& aCodedTrackUri,
       
   150     MUPnPTrackObserver& aTrackObserver,
       
   151     TPlaybackDirection aPlaybackDirection )
       
   152     {  
       
   153     __LOG( "CUPnPTrack::ResolveURI" );
       
   154 
       
   155     delete iOriginalURI;
       
   156     iOriginalURI = 0;
       
   157     delete iMediaServer;
       
   158     iMediaServer = 0;
       
   159     delete iObjectId;
       
   160     iObjectId = 0;
       
   161     iTrackObserver = &aTrackObserver;
       
   162 
       
   163     // Either local to remote or remote to remote
       
   164     if( aPlaybackDirection == EDirectionRemote )
       
   165         {
       
   166         if( aCodedTrackUri.Find( KUPnPPrefix ) == 0 )
       
   167             {
       
   168             // track is located in remote
       
   169             iTrackLocation = ETrackLocationRemote;
       
   170             // parse and save media server id and object id
       
   171             ParsePiecesL( aCodedTrackUri );
       
   172             // start a browsing session
       
   173             StartBrowsingSessionL();
       
   174             // create resolver 
       
   175             iItemResolver =
       
   176                 UPnPItemResolverFactory::NewRemoteItemResolverL(
       
   177                 *iObjectId, *iBrowsingSession, 
       
   178                 iDefaultSelector, KFilterCommon );
       
   179             }
       
   180         else // Local stored. Save URI to local member
       
   181             {
       
   182             // track is located in local file system
       
   183             iTrackLocation = ETrackLocationLocal;
       
   184             // store original URI
       
   185             iOriginalURI = HBufC::NewL( aCodedTrackUri.Length() );
       
   186             iOriginalURI->Des().Copy( aCodedTrackUri );
       
   187 			// check for DRM protection
       
   188 			if ( UPnPFileUtility::IsFileProtectedL( iOriginalURI->Des() ) )
       
   189 				{
       
   190 				// call back directly with an error code
       
   191 				iTrackObserver->ResolveURIComplete( KErrNotSupported );
       
   192 				return;
       
   193 				}
       
   194 			else
       
   195 				{
       
   196 	            // create resolver.
       
   197 	            iItemResolver =
       
   198 	                UPnPItemResolverFactory::NewLocalItemResolverL(
       
   199 						*iOriginalURI, iAvController, iFirstSelector,
       
   200 						UPnPItemResolverFactory::EOmitDrmCheck );
       
   201 				}
       
   202             }
       
   203         }
       
   204     else // Remote to local direction
       
   205         {
       
   206         // track is located in remote
       
   207         iTrackLocation = ETrackLocationRemote;
       
   208         // parse and save media server id and object id
       
   209         ParsePiecesL( aCodedTrackUri );
       
   210         // start a browsing session
       
   211         StartBrowsingSessionL();
       
   212         // create resolver
       
   213         iItemResolver =
       
   214             UPnPItemResolverFactory::NewDownloadItemResolverL(
       
   215             *iObjectId, iAvController, *iBrowsingSession, 
       
   216             iFirstSelector, KFilterCommon );
       
   217         }
       
   218 
       
   219     // Resolve remote item. Calls back to ResolveComplete
       
   220     iState = EStateResolving;
       
   221     TRAPD( e, iItemResolver->ResolveL( *this ); )
       
   222     if ( e != KErrNone )
       
   223         {
       
   224         if ( iState == EStateSelfDestruct )
       
   225             {
       
   226             __LOG( "CUPnPTrack: self-destructing" );
       
   227             delete this;
       
   228             }
       
   229         else
       
   230             {
       
   231             iState = EStateIdle;
       
   232             User::Leave( e );
       
   233             }
       
   234         }
       
   235     }
       
   236 
       
   237 // --------------------------------------------------------------------------
       
   238 // CUPnPTrack::StartBrowsingSessionL
       
   239 // Finds the apropriate mediaserver and starts a browsing session
       
   240 // --------------------------------------------------------------------------
       
   241 //
       
   242 void CUPnPTrack::StartBrowsingSessionL()
       
   243     {
       
   244     // Get list of media servers                 
       
   245     CUpnpAVDeviceList* devices = iAvController.GetMediaServersL();
       
   246     CleanupStack::PushL( devices );
       
   247 
       
   248     // Find out index of wanted media server from media server list 
       
   249     TInt index = KErrNotFound; 
       
   250     TInt count = devices->Count();
       
   251 
       
   252     for( TInt i = 0; i < count; i++ )
       
   253         {                                              
       
   254         // If media server id match.
       
   255         if( !iMediaServer->Compare( (*devices)[ i ]->Uuid() ) )
       
   256             {
       
   257             index = i;
       
   258             break;      
       
   259             }
       
   260         }
       
   261 
       
   262     // Leave if media server does not find by id 
       
   263     if( index == KErrNotFound )
       
   264         {
       
   265         __LOG1( "CUPnPTrack: Media server not found: %S",
       
   266             iMediaServer );
       
   267         User::Leave( KErrNotFound );    
       
   268         }
       
   269 
       
   270     // start a browsing session
       
   271     iBrowsingSession = &iAvController.StartBrowsingSessionL(
       
   272         *(*devices)[ index ] );
       
   273     CleanupStack::PopAndDestroy( devices );
       
   274     }
       
   275 
       
   276 // --------------------------------------------------------------------------
       
   277 // CUPnPTrack::StopBrowsingSession
       
   278 // Finds the apropriate mediaserver and starts a browsing session
       
   279 // --------------------------------------------------------------------------
       
   280 //
       
   281 void CUPnPTrack::StopBrowsingSession()
       
   282     {
       
   283     // Stop browsing session if exist.
       
   284     if( iBrowsingSession )
       
   285         {
       
   286         __LOG( "Stop browsing session" );
       
   287         iAvController.StopBrowsingSession( *iBrowsingSession );
       
   288         iBrowsingSession = 0;
       
   289         }
       
   290     }
       
   291 
       
   292 // --------------------------------------------------------------------------
       
   293 // CUPnPTrack::URI
       
   294 // Return original URI of track.
       
   295 // --------------------------------------------------------------------------
       
   296 //  
       
   297 const TDesC& CUPnPTrack::URI() const
       
   298     {
       
   299     __ASSERTD( iOriginalURI != 0,__FILE__, __LINE__ );
       
   300     return *iOriginalURI;
       
   301     }
       
   302 
       
   303 // --------------------------------------------------------------------------
       
   304 // CUPnPTrack::UpnpURI
       
   305 // Return True UPNP URI of track.
       
   306 // --------------------------------------------------------------------------
       
   307 //  
       
   308 const TDesC8& CUPnPTrack::UpnpURI() const
       
   309     {
       
   310     __ASSERTD( iIsItemSolved,__FILE__, __LINE__ );
       
   311     return iItemResolver->Resource().Value();
       
   312     }
       
   313 
       
   314 // --------------------------------------------------------------------------
       
   315 // CUPnPTrack::UpnpItem
       
   316 // Return True UPNP item representing the track
       
   317 // --------------------------------------------------------------------------
       
   318 //  
       
   319 const CUpnpItem& CUPnPTrack::UpnpItem() const
       
   320     {
       
   321     __ASSERTD( iIsItemSolved,__FILE__, __LINE__ );
       
   322     return iItemResolver->Item();
       
   323     }
       
   324     
       
   325 // --------------------------------------------------------------------------
       
   326 // CUPnPTrack::FilePath
       
   327 // Returns the file path of local item 
       
   328 // --------------------------------------------------------------------------
       
   329 //  
       
   330 const TDesC& CUPnPTrack::FilePath() const
       
   331     {
       
   332     __ASSERTD( iTrackLocation == ETrackLocationRemote, __FILE__, __LINE__ );
       
   333     __ASSERTD( iIsItemSolved,__FILE__, __LINE__ );
       
   334     return iItemResolver->Resource().FilePath();
       
   335     }
       
   336 
       
   337 // --------------------------------------------------------------------------
       
   338 // CUPnPTrack::IsRemote
       
   339 // Return location of track. ETrue = remote. EFalse = local
       
   340 // --------------------------------------------------------------------------
       
   341 //  
       
   342 TBool CUPnPTrack::IsRemote() const
       
   343     {
       
   344     return iTrackLocation == ETrackLocationRemote;
       
   345     }
       
   346 
       
   347 // --------------------------------------------------------------------------
       
   348 // CUPnPTrack::TrackDuration
       
   349 // returns duration of the track
       
   350 // --------------------------------------------------------------------------
       
   351 // 
       
   352 TInt CUPnPTrack::TrackDuration()
       
   353     {
       
   354     __LOG1( "CUPnPTrack::TrackDuration duration: %d", iTrackDuration );
       
   355     TInt ms = InitialTrackDuration;
       
   356     if ( iTrackDuration > InitialTrackDuration )
       
   357         {
       
   358         ms = iTrackDuration;
       
   359         }
       
   360     else if ( iIsItemSolved )
       
   361         {
       
   362         const CUpnpAttribute* attr = UPnPItemUtility
       
   363             ::FindAttributeByName( iItemResolver->Resource(),
       
   364              KAttributeDuration );
       
   365         if ( attr != 0 )
       
   366             {
       
   367             if ( UPnPItemUtility::UPnPDurationAsMilliseconds(
       
   368                 attr->Value(), ms ) == KErrNone )
       
   369                 {
       
   370                 // store duration for quicker future queries
       
   371                 iTrackDuration = ms;
       
   372                 __LOG1( "store duration: %d", iTrackDuration );
       
   373                 }
       
   374             }
       
   375         }
       
   376     return ms;
       
   377     }
       
   378 
       
   379 // --------------------------------------------------------------------------
       
   380 // CUPnPTrack::SetTrackDuration
       
   381 // overrides duration of the track
       
   382 // --------------------------------------------------------------------------
       
   383 // 
       
   384 void CUPnPTrack::SetTrackDuration( TInt aMilliseconds )
       
   385     {
       
   386     iTrackDuration = aMilliseconds;
       
   387     }
       
   388 
       
   389 // --------------------------------------------------------------------------
       
   390 // CUPnPTrack::GetMetaDataL
       
   391 // Get metadata information for track from media server
       
   392 // --------------------------------------------------------------------------
       
   393 //  
       
   394 void CUPnPTrack::GetMetaDataL( const TArray<TMPXAttribute>& aAttrs, 
       
   395     MMPXPlaybackPluginObserver& aObs )
       
   396     {
       
   397     // Check if remote track
       
   398     if( iTrackLocation == ETrackLocationLocal )
       
   399         {
       
   400         __LOG( "CUPnPTrack::GetMetaData - No metadata for local track!" );
       
   401         User::Leave( KErrNotSupported );
       
   402         }
       
   403 
       
   404     if ( iIsItemSolved )
       
   405         {
       
   406         __LOG( "CUPnPTrack::GetMetaDataL" );
       
   407         DeliverMedataL( aAttrs, aObs );
       
   408         }
       
   409     else
       
   410         {
       
   411         __LOG( "CUPnPTrack::GetMetaDataL - pending" );
       
   412         iMetadataObserver = &aObs;
       
   413         for( TInt i = 0; i < aAttrs.Count(); ++i )
       
   414             {
       
   415             iQueriedAttributes.AppendL( aAttrs[i] );
       
   416             }
       
   417         }
       
   418     }
       
   419 
       
   420 // --------------------------------------------------------------------------
       
   421 // CUPnPTrack::SendMediaChangedEventL
       
   422 // --------------------------------------------------------------------------
       
   423 // 
       
   424 void CUPnPTrack::SendMediaChangedEventL(
       
   425     MMPXPlaybackPluginObserver& aObs )
       
   426     {
       
   427     RArray<TMPXAttribute> attrs;
       
   428     CleanupClosePushL( attrs );
       
   429     attrs.AppendL( KMPXMediaGeneralTitle );
       
   430     attrs.AppendL( KMPXMediaMusicArtist );
       
   431     attrs.AppendL( KMPXMediaMusicAlbum );
       
   432     attrs.AppendL( KMPXMediaMusicGenre );
       
   433     attrs.AppendL( KMPXMediaGeneralDate );
       
   434     attrs.AppendL( KMPXMediaMusicYear );
       
   435     attrs.AppendL( KMPXMediaGeneralComment );
       
   436     attrs.AppendL( KMPXMediaGeneralMimeType );
       
   437 
       
   438     // Create and fill CMPXMedia class 
       
   439     RArray<TInt> suppIds;
       
   440     CleanupClosePushL( suppIds );
       
   441     suppIds.AppendL( KMPXMediaIdMusic );
       
   442     suppIds.AppendL( KMPXMediaIdGeneral );
       
   443     suppIds.AppendL( KMPXMediaIdAudio );
       
   444     CMPXMedia* media = CMPXMedia::NewL( suppIds.Array() );
       
   445     CleanupStack::PopAndDestroy( &suppIds );
       
   446     CleanupStack::PushL( media );
       
   447     FillMediaFromItemL( *media, attrs.Array() );
       
   448     
       
   449     // create an MPX message
       
   450     CMPXMessage* msg = CMPXMessage::NewL();
       
   451     CleanupStack::PushL( msg );
       
   452     msg->SetTObjectValueL<TMPXMessageId>(
       
   453         KMPXMessageGeneralId, KMPXMessagePbMediaChanged );
       
   454     msg->SetCObjectValueL<CMPXMedia>(
       
   455         KMPXMessagePbMedia, media );
       
   456 
       
   457     // send message
       
   458     aObs.HandlePlaybackMessage( *msg );
       
   459 
       
   460     CleanupStack::PopAndDestroy( msg );
       
   461     CleanupStack::PopAndDestroy( media );
       
   462     CleanupStack::PopAndDestroy( &attrs );
       
   463     }
       
   464 
       
   465 // --------------------------------------------------------------------------
       
   466 // CUPnPTrack::DeliverMedatataL
       
   467 // --------------------------------------------------------------------------
       
   468 // 
       
   469 void CUPnPTrack::DeliverMedataL( const TArray<TMPXAttribute>& aAttrs, 
       
   470     MMPXPlaybackPluginObserver& aObs, TInt aError )
       
   471     {
       
   472     __LOG( "CUPnPTrack::DeliverMedatata");
       
   473 
       
   474     // Create and fill CMPXMedia class 
       
   475     RArray<TInt> suppIds;
       
   476     CleanupClosePushL( suppIds );
       
   477     suppIds.AppendL( KMPXMediaIdMusic );
       
   478     suppIds.AppendL( KMPXMediaIdGeneral );
       
   479     suppIds.AppendL( KMPXMediaIdAudio );
       
   480     CMPXMedia* media = CMPXMedia::NewL( suppIds.Array() );
       
   481     CleanupStack::PopAndDestroy( &suppIds );        
       
   482     CleanupStack::PushL( media );
       
   483 
       
   484     if ( aError == KErrNone )
       
   485         {
       
   486         FillMediaFromItemL( *media, aAttrs );
       
   487         }
       
   488 
       
   489     // Return metadata
       
   490     aObs.HandleMedia( *media, aError );
       
   491     CleanupStack::PopAndDestroy( media );
       
   492     }
       
   493 
       
   494 // --------------------------------------------------------------------------
       
   495 // CUPnPTrack::FillMediaFromItemL
       
   496 // --------------------------------------------------------------------------
       
   497 // 
       
   498 void CUPnPTrack::FillMediaFromItemL( CMPXMedia& aMedia,
       
   499     const TArray<TMPXAttribute>& aAttrs )
       
   500     {
       
   501     __ASSERTD( iIsItemSolved,__FILE__, __LINE__ );
       
   502 
       
   503     // Fill metadata 
       
   504     const CUpnpElement* elem;
       
   505     // Song title
       
   506     if( Exists( aAttrs, KMPXMediaGeneralTitle ) )
       
   507         {
       
   508         __LOG( "CUPnPTrack: Metadata: Title");
       
   509         aMedia.SetTextValueL(
       
   510             KMPXMediaGeneralTitle, *To16LC( 
       
   511             iItemResolver->Item().Title() ) );
       
   512         CleanupStack::PopAndDestroy();
       
   513         }
       
   514     // Artist
       
   515     if( Exists( aAttrs, KMPXMediaMusicArtist ) )
       
   516         {
       
   517         if ( ( elem = UPnPItemUtility::FindElementByName(
       
   518             iItemResolver->Item(), KElementArtist ) ) != 0 )
       
   519             {
       
   520             __LOG( "CUPnPTrack: Metadata: Artist" );
       
   521             aMedia.SetTextValueL(
       
   522                 KMPXMediaMusicArtist, *To16LC( elem->Value() ) );
       
   523             CleanupStack::PopAndDestroy();
       
   524             }
       
   525         else if ( ( elem = UPnPItemUtility::FindElementByName(
       
   526             iItemResolver->Item(), KElementCreator ) ) != 0 )
       
   527             {
       
   528             __LOG( "CUPnPTrack: Metadata Creator" );
       
   529             aMedia.SetTextValueL(
       
   530                 KMPXMediaMusicArtist, *To16LC( elem->Value() ) );
       
   531             CleanupStack::PopAndDestroy();
       
   532             }
       
   533         }
       
   534     // Album
       
   535     if( Exists( aAttrs, KMPXMediaMusicAlbum ) )
       
   536         {
       
   537         if ( ( elem = UPnPItemUtility::FindElementByName(
       
   538             iItemResolver->Item(), KElementAlbum ) ) != 0 )
       
   539             {
       
   540             __LOG( "CUPnPTrack: Metadata: Album");
       
   541             aMedia.SetTextValueL(
       
   542                 KMPXMediaMusicAlbum, *To16LC( elem->Value() ) );
       
   543             CleanupStack::PopAndDestroy();
       
   544             }
       
   545         }
       
   546     // Genre
       
   547     if( Exists( aAttrs, KMPXMediaMusicGenre ) )
       
   548         {
       
   549         if ( ( elem = UPnPItemUtility::FindElementByName(
       
   550             iItemResolver->Item(), KElementGenre ) ) != 0 )
       
   551             {
       
   552             __LOG( "CUPnPTrack: Metadata: Genre" );
       
   553             aMedia.SetTextValueL(
       
   554                 KMPXMediaMusicGenre, *To16LC( elem->Value() ) );
       
   555             CleanupStack::PopAndDestroy();
       
   556             }
       
   557         }
       
   558     // Date / Year
       
   559     if( ( elem = UPnPItemUtility::FindElementByName(
       
   560           iItemResolver->Item(), KElementDate ) ) != 0 )
       
   561         {
       
   562         TTime timestamp;
       
   563         TInt conversionError =
       
   564             UPnPItemUtility::UPnPDateAsTTime( elem->Value(), timestamp );
       
   565         if ( conversionError == KErrNone )
       
   566             {
       
   567             if ( Exists( aAttrs, KMPXMediaGeneralDate ) )
       
   568                 {
       
   569                 __LOG( "CUPnPTrack: Metadata: Date" );
       
   570                 aMedia.SetTextValueL(
       
   571                     KMPXMediaGeneralDate, iTempBuf );
       
   572                 }
       
   573             if ( Exists( aAttrs, KMPXMediaMusicYear ) )
       
   574                 {
       
   575                 __LOG( "CUPnPTrack: Metadata: Year" );
       
   576                 timestamp.FormatL( iTempBuf, KTimeFormatYearOnly );
       
   577                 aMedia.SetTextValueL(
       
   578                     KMPXMediaMusicYear, iTempBuf );
       
   579                 }
       
   580             }
       
   581         }
       
   582     // Duration
       
   583     if( Exists( aAttrs, KMPXMediaGeneralDuration ) )
       
   584         {
       
   585         TInt duration = TrackDuration();
       
   586         if ( duration >= 0 )
       
   587             {
       
   588             aMedia.SetTObjectValueL<TInt>(
       
   589                 KMPXMediaGeneralDuration, duration );
       
   590             }
       
   591         }
       
   592     // Size
       
   593     if( Exists( aAttrs, KMPXMediaGeneralSize ) )
       
   594         {
       
   595         const CUpnpAttribute* attr = UPnPItemUtility
       
   596             ::FindAttributeByName( iItemResolver->Resource(),
       
   597              KAttributeSize );
       
   598         if ( attr != 0 )
       
   599             {
       
   600             __LOG( "CUPnPTrack: Metadata: Size" );
       
   601             TInt size;
       
   602             TLex8 sizeconvert( attr->Value() );
       
   603             if ( sizeconvert.Val( size ) == KErrNone )
       
   604                 {
       
   605                 aMedia.SetTObjectValueL<TInt>(
       
   606                     KMPXMediaGeneralSize, size );
       
   607                 }
       
   608             }
       
   609         }
       
   610     // Mimetype
       
   611     if( Exists( aAttrs, KMPXMediaGeneralMimeType ) )
       
   612         {        
       
   613         const CUpnpAttribute* attr = UPnPItemUtility
       
   614             ::FindAttributeByName( iItemResolver->Resource(),
       
   615              KAttributeProtocolInfo );
       
   616         if ( attr != 0 )
       
   617             {
       
   618             __LOG( "CUPnPTrack: Metadata: MimeType" );
       
   619             CUpnpDlnaProtocolInfo* pInfo =
       
   620                 CUpnpDlnaProtocolInfo::NewL( attr->Value() );
       
   621             CleanupStack::PushL( pInfo );
       
   622             aMedia.SetTextValueL(
       
   623                 KMPXMediaGeneralMimeType, *To16LC( pInfo->ThirdField() ) );
       
   624             CleanupStack::PopAndDestroy();
       
   625             CleanupStack::PopAndDestroy( pInfo );
       
   626             pInfo = NULL;
       
   627             }
       
   628         }
       
   629     }
       
   630 
       
   631 // --------------------------------------------------------------------------
       
   632 // CUPnPTrack::Exists
       
   633 // --------------------------------------------------------------------------
       
   634 // 
       
   635 TBool CUPnPTrack::Exists( const TArray<TMPXAttribute>& aAttrs,
       
   636     const TMPXAttributeData& aAttrData ) const
       
   637     {
       
   638     TBool found = EFalse;
       
   639     for( TInt i = 0; i < aAttrs.Count() && !found; ++i )
       
   640         {
       
   641         if ( aAttrs[i].ContentId() == aAttrData.iContentId &&
       
   642             aAttrs[i].AttributeId() & aAttrData.iAttributeId )
       
   643             {
       
   644             found = ETrue;
       
   645             }
       
   646         }
       
   647     return found;
       
   648     }
       
   649 
       
   650 // --------------------------------------------------------------------------
       
   651 // CUPnPTrack::To16LC
       
   652 // --------------------------------------------------------------------------
       
   653 // 
       
   654 const HBufC16* CUPnPTrack::To16LC( const TDesC8& aText )
       
   655     {
       
   656     HBufC16* result = 0;
       
   657     result = EscapeUtils::ConvertToUnicodeFromUtf8L( aText );
       
   658     CleanupStack::PushL( result );
       
   659     return result;
       
   660     }
       
   661 
       
   662 // --------------------------------------------------------------------------
       
   663 // callbacks from MUPnPItemResolverObserver
       
   664 // --------------------------------------------------------------------------
       
   665 //  
       
   666 void CUPnPTrack::ResolveComplete( const MUPnPItemResolver& /*aResolver*/,
       
   667     TInt aError )
       
   668     {
       
   669     __LOG1( "CUPnPTrack::ResolveComplete err: %d", aError );
       
   670     iIsItemSolved = ETrue;
       
   671     if ( iState == EStateResolving )
       
   672         {
       
   673         iState = EStateReady;
       
   674 
       
   675         // handle pending metadata query
       
   676         if ( iTrackLocation == ETrackLocationRemote && iMetadataObserver )
       
   677             {
       
   678             __LOG( "Handle pending metadata query");
       
   679              TRAP_IGNORE( 
       
   680                 DeliverMedataL( iQueriedAttributes.Array(), *iMetadataObserver,
       
   681                 aError ) );
       
   682              iMetadataObserver = 0;
       
   683              iQueriedAttributes.Reset();
       
   684             }
       
   685 
       
   686         // call back
       
   687         MUPnPTrackObserver& observer = *iTrackObserver;
       
   688         iTrackObserver = 0;
       
   689         observer.ResolveURIComplete( aError );
       
   690         }
       
   691     else if ( iState == EStateSelfDestruct )
       
   692         {
       
   693         __LOG( "CUPnPTrack: self-destructing" );
       
   694         delete this;
       
   695         }
       
   696     }
       
   697     
       
   698 // --------------------------------------------------------------------------
       
   699 // Private methods of CUPnPTrack
       
   700 // --------------------------------------------------------------------------
       
   701 
       
   702 // --------------------------------------------------------------------------
       
   703 // CUPnPTrack::ParsePiecesL
       
   704 // Parse and save media server id and object id from given descriptor
       
   705 // --------------------------------------------------------------------------
       
   706 //    
       
   707 void CUPnPTrack::ParsePiecesL( const TDesC& aSong )
       
   708     {    
       
   709     // Delete if already exist
       
   710     if( iMediaServer )
       
   711         {
       
   712         delete iMediaServer;
       
   713         iMediaServer = 0;
       
   714         }
       
   715     if( iObjectId )
       
   716         {
       
   717         delete iObjectId;
       
   718         iObjectId = 0;
       
   719         }
       
   720     
       
   721     // Leave if argument is not valid
       
   722     if( aSong.Length() < KUPnPPrefixLength )
       
   723         {
       
   724         User::Leave( KErrArgument );
       
   725         }
       
   726         
       
   727     TInt lenght = aSong.Length();
       
   728     TInt position = 0;
       
   729     TChar colon( KCharCodeColon );
       
   730     TChar separate( KCharCodeSeparate );
       
   731         
       
   732     // At first separe "upnp:" prefix from descriptor 
       
   733     position = aSong.Locate( colon );   
       
   734     TPtrC tmp = aSong.Mid( position + 1, (lenght - KUPnPPrefixLength ) );
       
   735     
       
   736     // Get media server id
       
   737     position = tmp.Locate( separate );
       
   738     // Leave if separator character not found
       
   739     if( position == KErrNotFound )
       
   740         {
       
   741         User::Leave( KErrNotFound );
       
   742         }
       
   743     TPtrC mediaserverId = tmp.Left( position );
       
   744     iMediaServer = HBufC8::NewL( mediaserverId.Length() );
       
   745     iMediaServer->Des().Copy( mediaserverId );
       
   746         
       
   747     // Get object id
       
   748     TPtrC objId = tmp.Mid( position + 1, ( (tmp.Length() 
       
   749         - mediaserverId.Length() ) - 1 ) ); 
       
   750     iObjectId = HBufC8::NewL( objId.Length() );
       
   751     iObjectId->Des().Copy( objId );
       
   752     }
       
   753 
       
   754