upnpavcontroller/upnpavcontrollerhelper/src/upnplocalitemresolver.cpp
branchnew development branch with rendering state machine and other goodies
changeset 38 5360b7ddc251
parent 0 7f85d04be362
equal deleted inserted replaced
32:3785f754ee62 38:5360b7ddc251
     1 /*
     1 /*
     2 * Copyright (c) 2007 Nokia Corporation and/or its subsidiary(-ies).
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     3 * All rights reserved.
     4 * This component and the accompanying materials are made available
     4 * This component and the accompanying materials are made available
     5 * under the terms of "Eclipse Public License v1.0"
     5 * under the terms of "Eclipse Public License v1.0"
     6 * which accompanies this distribution, and is available
     6 * which accompanies this distribution, and is available
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
    19 
    19 
    20 
    20 
    21 
    21 
    22 
    22 
    23 // INCLUDE FILES
    23 // INCLUDE FILES
    24 // upnp stack api's
    24 // dlnasrv / mediaserver api
    25 #include <upnpitem.h>
    25 #include <upnpitem.h>
    26 
    26 // mpx
    27 // upnpframework / avcontroller api
    27 #include <mpxmedia.h>
       
    28 #include <mpxmediamusicdefs.h>
       
    29 #include <mpxcollectionhelper.h>
       
    30 #include <mpxcollectionhelperfactory.h>
       
    31 // dlnasrv / avcontroller api
    28 #include "upnpavcontroller.h" // avcontroller service
    32 #include "upnpavcontroller.h" // avcontroller service
    29 #include "upnpavbrowsingsession.h" // browsing session
    33 #include "upnpavbrowsingsession.h" // browsing session
    30 #include "upnpavdevice.h" // device (for creating a session)
    34 #include "upnpavdevice.h" // device (for creating a session)
    31 #include <upnpfilesharing.h>
    35 
    32 
    36 // dlnasrv / avcontroller helper api
    33 // upnpframework / avcontroller helper api
       
    34 #include "upnpitemresolverfactory.h" // optimisation flags
    37 #include "upnpitemresolverfactory.h" // optimisation flags
    35 #include "upnpitemresolverobserver.h" // MUPnPItemResolverObserver
    38 #include "upnpitemresolverobserver.h" // MUPnPItemResolverObserver
    36 #include "upnpfileutility.h" // IsFileProtected
    39 #include "upnpfileutility.h" // IsFileProtected
    37 #include "upnpresourceselector.h" // MUPnPResourceSelector
    40 #include "upnpresourceselector.h" // MUPnPResourceSelector
    38 
    41 #include "upnpitemutility.h" // GetResElements
    39 // upnpframework / internal api's
    42 #include "upnpconstantdefs.h" // for upnp-specific stuff
       
    43 
       
    44 // dlnasrv / internal api
       
    45 #include "upnppushserver.h" // CUpnpPushServer
    40 #include "upnpmetadatafetcher.h" // CreateItemFromFileLC
    46 #include "upnpmetadatafetcher.h" // CreateItemFromFileLC
    41 #include "upnpcommonutils.h" // ReplacePlaceHolderInURIL
    47 #include "upnpstring.h"
    42 
    48 
    43 // avcontrollerhelper internal
    49 // dlnasrv / avcontrollerhelper internal
    44 #include "upnplocalitemresolver.h"
    50 #include "upnplocalitemresolver.h"
    45 
    51 
    46 #include "upnpconstantdefs.h" // for upnp-specific stuff
    52 #include "upnptranscodehelper.h"
    47 #include "upnpitemutility.h" // for GetResElements
    53 
    48 #include "upnpsecaccesscontroller.h" // CUpnpSecAccessController
    54 #include "upnpcdsreselementutility.h"
    49 #include "upnpperiodic.h"
    55 #include "upnpdlnaprofiler.h"
    50 
    56 
    51 _LIT( KComponentLogfile, "upnpavcontrollerhelper.txt");
    57 _LIT( KComponentLogfile, "upnpavcontrollerhelper.txt");
    52 #include "upnplog.h"
    58 #include "upnplog.h"
    53 
    59 
    54 // CONSTANTS
    60 // CONSTANTS
    55 const TInt KCancelWaitMaximum =   4000000;
    61 static TUint KFirstSharedHash = 8647544;
    56 const TInt KCancelWaitResolution = 500000;
       
    57 const TInt KUnshareWait =  1000000;
       
    58 
    62 
    59 // METHODS
    63 // METHODS
    60 
    64 
    61 // --------------------------------------------------------------------------
    65 // --------------------------------------------------------------------------
    62 // CUPnPLocalItemResolver:: NewL
    66 // CUPnPLocalItemResolver:: NewL
    66     const TDesC& aFilePath,
    70     const TDesC& aFilePath,
    67     MUPnPAVController& aAvController,
    71     MUPnPAVController& aAvController,
    68     MUPnPResourceSelector& aSelector,
    72     MUPnPResourceSelector& aSelector,
    69     TInt aOptimisationFlags    )
    73     TInt aOptimisationFlags    )
    70     {
    74     {
    71     CUPnPLocalItemResolver* self =
    75     CUPnPLocalItemResolver* self = new (ELeave )CUPnPLocalItemResolver(
    72         new (ELeave )CUPnPLocalItemResolver(
    76 								aAvController, aSelector, aOptimisationFlags );
    73         aAvController, aSelector, aOptimisationFlags );
       
    74     CleanupStack::PushL( self );
    77     CleanupStack::PushL( self );
    75     self->ConstructL( aFilePath );
    78     self->ConstructL( aFilePath );
    76     CleanupStack::Pop( self );
    79     CleanupStack::Pop( self );
    77     return self;
    80     return self;
    78     }
    81     }
    80 // --------------------------------------------------------------------------
    83 // --------------------------------------------------------------------------
    81 // CUPnPLocalItemResolver::CUPnPLocalItemResolver
    84 // CUPnPLocalItemResolver::CUPnPLocalItemResolver
    82 // See upnplocalitemresolver.h
    85 // See upnplocalitemresolver.h
    83 //---------------------------------------------------------------------------
    86 //---------------------------------------------------------------------------
    84 CUPnPLocalItemResolver::CUPnPLocalItemResolver(
    87 CUPnPLocalItemResolver::CUPnPLocalItemResolver(
    85     MUPnPAVController& aAvController,
    88     MUPnPAVController& /*aAvController*/,
    86     MUPnPResourceSelector& aSelector,
    89     MUPnPResourceSelector& aSelector,
    87     TInt aOptimisationFlags )
    90     TInt aOptimisationFlags )
    88     : CActive( EPriorityStandard )
    91     : iSelector( aSelector )
    89     , iAvController( aAvController )
    92     {
    90     , iSelector( aSelector )
       
    91     {
       
    92     CActiveScheduler::Add( this );
       
    93     iOptimisationFlags = aOptimisationFlags;
    93     iOptimisationFlags = aOptimisationFlags;
    94     iState = EStateIdle;
       
    95     }
    94     }
    96 
    95 
    97 
    96 
    98 // --------------------------------------------------------------------------
    97 // --------------------------------------------------------------------------
    99 // CUPnPLocalItemResolver::ConstructL
    98 // CUPnPLocalItemResolver::ConstructL
   102 void CUPnPLocalItemResolver::ConstructL(
   101 void CUPnPLocalItemResolver::ConstructL(
   103     const TDesC& aFilePath )
   102     const TDesC& aFilePath )
   104     {
   103     {
   105     __LOG1( "LocalItemResolver:ConstructL() 0x%d", TInt(this) );
   104     __LOG1( "LocalItemResolver:ConstructL() 0x%d", TInt(this) );
   106     iFilePath = aFilePath.AllocL();
   105     iFilePath = aFilePath.AllocL();
   107     iFileSharing = CUpnpFileSharing::NewL();    
   106     
   108     iAccessController = CUpnpSecAccessController::NewL();
   107 #ifdef UPNP_USE_GSTREAMER
   109     iWait = new (ELeave) CActiveSchedulerWait();
   108     iTranscodeHelper = CUpnpTranscodeHelper::NewL();
   110     iTimer = CUPnPPeriodic::NewL( CActive::EPriorityHigh );
   109 #endif
       
   110     
       
   111     // create mpx collection helper
       
   112     iCollectionHelper = CMPXCollectionHelperFactory::NewCollectionHelperL();
   111     }
   113     }
   112 
   114 
   113 
   115 
   114 // --------------------------------------------------------------------------
   116 // --------------------------------------------------------------------------
   115 // CUPnPLocalItemResolver::~CUPnPLocalItemResolver
   117 // CUPnPLocalItemResolver::~CUPnPLocalItemResolver
   116 // See upnplocalitemresolver.h
   118 // See upnplocalitemresolver.h
   117 //---------------------------------------------------------------------------
   119 //---------------------------------------------------------------------------
   118 CUPnPLocalItemResolver::~CUPnPLocalItemResolver()
   120 CUPnPLocalItemResolver::~CUPnPLocalItemResolver()
   119     {
   121     {
   120     __LOG1( "LocalItemResolver destructor 0x%d", TInt(this) );
   122     __LOG1( "LocalItemResolver destructor 0x%d", TInt(this) );
   121     
       
   122     Cleanup();
       
   123     
       
   124     if ( iTempSession )
       
   125         {
       
   126         iTempSession->RemoveObserver();
       
   127         iAvController.StopBrowsingSession( *iTempSession );
       
   128         }
       
   129 
       
   130     delete iFileSharing;
       
   131     iFileSharing = NULL;
       
   132     
       
   133     // cancel any async calls
       
   134     if ( IsActive() )
       
   135         {
       
   136          __LOG( "LocalItemResolver destructor RequestComplete" );
       
   137         TRequestStatus* stat = &iStatus;
       
   138         User::RequestComplete( stat, KErrNone );
       
   139         }
       
   140     
       
   141     Cancel();
       
   142       
       
   143     delete iFilePath;
   123     delete iFilePath;
   144     iFilePath = NULL;
   124     iFilePath = NULL;
   145     
       
   146     delete iAccessController;
       
   147     iAccessController = NULL;
       
   148     
       
   149     delete iSharedItem;
   125     delete iSharedItem;
   150     iSharedItem = NULL;
   126     iSharedItem = NULL;
   151         
   127     delete iThumbnailCreator;
   152     if( iTimer )
   128     iThumbnailCreator = NULL;
   153         {
   129     Cleanup();
   154         iTimer->Cancel();
   130 #ifdef UPNP_USE_GSTREAMER
   155         delete iTimer;
   131     delete iTranscodeHelper;
   156         iTimer = NULL;
   132 #endif
   157         }
   133     iCollectionHelper->Close();
   158     
       
   159     if( iWait->IsStarted() )
       
   160         {
       
   161         iWait->AsyncStop();
       
   162         }
       
   163     delete iWait;
       
   164     iWait = NULL;
       
   165         
       
   166     __LOG( "LocalItemResolver destructor end" );
   134     __LOG( "LocalItemResolver destructor end" );
   167     }
   135     }
   168 
   136 
   169 // --------------------------------------------------------------------------
   137 // --------------------------------------------------------------------------
   170 // CUPnPLocalItemResolver::ResolveL
   138 // CUPnPLocalItemResolver::ResolveL
   171 // See upnplocalitemresolver.h
   139 // See upnplocalitemresolver.h
   172 //---------------------------------------------------------------------------
   140 //---------------------------------------------------------------------------
   173 void CUPnPLocalItemResolver::ResolveL(
   141 void CUPnPLocalItemResolver::ResolveL(
   174     MUPnPItemResolverObserver& aObserver )
   142     MUPnPItemResolverObserver& aObserver, CUpnpAVDevice* aDevice )
   175     {
   143     {
   176     __LOG1( "LocalItemResolver:Resolve() 0x%d", TInt(this) );
   144     __LOG1( "LocalItemResolver:Resolve() 0x%d", TInt(this) );
   177     __ASSERTD( iState == EStateIdle, __FILE__, __LINE__ );
   145     
       
   146     _LIT(KExtJpeg, ".jpeg");
       
   147     _LIT(KExtJpg,  ".jpg");
       
   148     
   178     iObserver = &aObserver;
   149     iObserver = &aObserver;
   179 
   150     if ( !(iOptimisationFlags & UPnPItemResolverFactory::EOmitDrmCheck ))
   180     if ( iOptimisationFlags & UPnPItemResolverFactory::EOmitDrmCheck )
       
   181         {
       
   182         // no need to check DRM ! this branch is empty.
       
   183         }
       
   184     else
       
   185         {
   151         {
   186         // check DRM
   152         // check DRM
   187         if ( UPnPFileUtility::IsFileProtectedL( iFilePath->Des() ) )
   153         if ( UPnPFileUtility::IsFileProtectedL( iFilePath->Des() ) )
   188             {
   154             {
   189             User::Leave( KErrNotSupported );
   155             User::Leave( KErrNotSupported );
   190             }
   156             }
   191         }
   157         }
   192 
       
   193     // create item metadata
   158     // create item metadata
   194     iState = EStateCreatingItem;
   159 	delete iLocalItem;
   195     CUpnpItem* item = UPnPMetadataFetcher::CreateItemFromFileLC(
   160 	iLocalItem = NULL;
   196         iFilePath->Des() );
   161     iLocalItem =
   197     CleanupStack::Pop( item );
   162 		UPnPMetadataFetcher::CreateItemFromFileLC(
   198     __LOG1( "LocalItemResolver:Resolve CreateItemFromFileLC done 0x%d", TInt(this) );
   163 			iFilePath->Des());
   199     if( iSharedItem )
   164     CleanupStack::Pop( iLocalItem );
   200         {
   165 
   201         delete iSharedItem;
   166 #ifdef UPNP_USE_GSTREAMER
   202         iSharedItem = 0;
   167     if( aDevice )
   203         }
   168         {
   204     iSharedItem = item;
   169         HBufC8* pipeline = NULL;
   205 
   170         HBufC8* protocolInfo = NULL;
   206     // state check
   171         
   207     if ( iState == EStateCreatingItem )
   172         TRAPD( err, iTranscodeHelper->PreDefinedCfgL( *aDevice, 
   208         {
   173                 *iLocalItem, pipeline, protocolInfo ) );
   209         // start local mediaserver
   174         
   210         if ( iOptimisationFlags & UPnPItemResolverFactory::EOmitLocalMSStart )
   175         __ASSERT_ALWAYS( err == KErrNone || err == KErrNotFound, 
   211             {
   176                 User::Invariant() );
   212             // omit mediaserver start - go directly to sharing.
   177         
   213             DoShareL();
   178         if( err == KErrNone )
       
   179             {
       
   180             CleanupStack::PushL(pipeline);
       
   181             CleanupStack::PushL(protocolInfo);
       
   182             
       
   183             //config found -> start transc and replace resource
       
   184             iTranscodeHelper->TranscodeL( *pipeline );
       
   185             iTranscodeHelper->ReplaceResourceL( *iLocalItem, *protocolInfo );
       
   186             
       
   187             CleanupStack::PopAndDestroy(protocolInfo);
       
   188             CleanupStack::PopAndDestroy(pipeline);
       
   189             }               
       
   190         }
       
   191 #endif
       
   192     
       
   193     TParse p;
       
   194     p.Set(iFilePath->Des(),NULL,NULL);
       
   195     if( p.Ext().CompareF(KExtJpeg) == KErrNone 
       
   196         || p.Ext().CompareF(KExtJpg) == KErrNone )
       
   197         {
       
   198         //TThumbnailDlnaSize size (EThumbnail);
       
   199         //iThumbnailCreator = CUpnpThumbnailCreator::NewL(*this,
       
   200         //                                                *iFilePath, size);
       
   201         // TODO: the above code starts thumbnail creation,
       
   202         // but for some reason thumbnail manager returns with KErrBadName.
       
   203         // For now, just share the original, without creating a thumbnail res element
       
   204         ShareL();
       
   205         }
       
   206     else if( UPnPItemUtility::BelongsToClass(*iLocalItem,KClassAudio()) )
       
   207         {
       
   208         // 
       
   209         TRAPD( err, SetAlbumArtResourceToItemL( *iFilePath ) );
       
   210         __LOG1( "LocalItemResolver:Resolve() music album art err=%d",err );
       
   211         if( err )
       
   212             {
       
   213             // if song did not have album art share normally
       
   214             ShareL();
       
   215             }
       
   216         }
       
   217     else
       
   218         {
       
   219         ShareL();
       
   220         }
       
   221     __LOG( "LocalItemResolver:Resolve() END" );
       
   222     }
       
   223 
       
   224 // --------------------------------------------------------------------------
       
   225 // CUPnPLocalItemResolver::ShareL
       
   226 //---------------------------------------------------------------------------
       
   227 void CUPnPLocalItemResolver::ShareL()
       
   228     {
       
   229     // share
       
   230     CUpnpPushServer::ShareL( KFirstSharedHash++, *iLocalItem );
       
   231 
       
   232     // Store item & resource
       
   233     delete iSharedItem;
       
   234     iSharedItem = NULL;    
       
   235     iSharedItem = iLocalItem;
       
   236 	iLocalItem = NULL;
       
   237         
       
   238     iResource = &iSelector.SelectResourceL( *iSharedItem );
       
   239 	
       
   240     //inform the observer
       
   241     iObserver->ResolveComplete( *this, KErrNone );
       
   242     }
       
   243 
       
   244 // --------------------------------------------------------------------------
       
   245 // CUPnPLocalItemResolver::Item
       
   246 // See upnplocalitemresolver.h
       
   247 //---------------------------------------------------------------------------
       
   248 const CUpnpItem& CUPnPLocalItemResolver::Item() const
       
   249     {
       
   250     __ASSERT( iSharedItem, __FILE__, __LINE__ );
       
   251     return *iSharedItem;
       
   252     }
       
   253 
       
   254 // --------------------------------------------------------------------------
       
   255 // CUPnPLocalItemResolver::Resource
       
   256 // See upnplocalitemresolver.h
       
   257 //---------------------------------------------------------------------------
       
   258 const CUpnpElement& CUPnPLocalItemResolver::Resource() const
       
   259     {
       
   260     __ASSERT( iResource, __FILE__, __LINE__ );
       
   261     return *iResource;
       
   262     }
       
   263 
       
   264 // --------------------------------------------------------------------------
       
   265 // CUPnPLocalItemResolver::Cleanup
       
   266 // See upnplocalitemresolver.h
       
   267 //---------------------------------------------------------------------------
       
   268 void CUPnPLocalItemResolver::Cleanup()
       
   269     {
       
   270     __LOG( "CUPnPLocalItemResolver:Cleanup() ");
       
   271     // unshare
       
   272     TRAP_IGNORE(CUpnpPushServer::UnshareL( (TUint)this ));
       
   273     __LOG( "CUPnPLocalItemResolver:Cleanup() end" );
       
   274     }
       
   275 
       
   276 // -----------------------------------------------------------------------------
       
   277 // CUPnPLocalItemResolver::SetAlbumArtResourceToItemL
       
   278 // -----------------------------------------------------------------------------
       
   279 //
       
   280 void CUPnPLocalItemResolver::SetAlbumArtResourceToItemL( const TDesC& aFileName )
       
   281     {
       
   282     __LOG1( "CUPnPLocalItemResolver:SetAlbumArtResourceToItemL() fileName = %S",&aFileName);
       
   283     RArray<TMPXAttribute> attrs;
       
   284     CleanupClosePushL( attrs );
       
   285     attrs.AppendL( TMPXAttribute( KMPXMediaMusicAlbumArtFileName ) );
       
   286     
       
   287     CMPXMedia* media( NULL );
       
   288     media = iCollectionHelper->GetL( aFileName,
       
   289         attrs.Array(), EMPXSong );
       
   290     CleanupStack::PopAndDestroy( &attrs );
       
   291     CleanupStack::PushL( media );
       
   292     
       
   293     const TDesC& filePath = media->ValueText( KMPXMediaMusicAlbumArtFileName );
       
   294     
       
   295     TInt leaveErr(KErrNone);
       
   296     if( filePath != KNullDesC && filePath.CompareF(aFileName) != KErrNone )
       
   297         {
       
   298         __LOG( "CUPnPLocalItemResolver:SetAlbumArtResourceToItemL() \
       
   299                 start waiting album art");
       
   300         TThumbnailDlnaSize size (EThumbnail);
       
   301         iThumbnailCreator = CUpnpThumbnailCreator::NewL(*this,
       
   302                 filePath, size);
       
   303         }
       
   304     else
       
   305         {
       
   306         leaveErr = KErrNotFound;
       
   307         }
       
   308     CleanupStack::PopAndDestroy( media );
       
   309     if( leaveErr ) 
       
   310         {
       
   311         User::Leave(leaveErr);
       
   312         }
       
   313     }
       
   314 
       
   315 // --------------------------------------------------------------------------
       
   316 // CUPnPLocalItemResolver::ThumbnailCreatorReady
       
   317 // See upnplocalitemresolver.h
       
   318 //---------------------------------------------------------------------------
       
   319 void CUPnPLocalItemResolver::ThumbnailCreatorReady( TInt aError)
       
   320     {
       
   321     __LOG1( "CUPnPLocalItemResolver:ThumbnailCreatorReady() %d",aError);
       
   322     if(aError == KErrNone)
       
   323     	{
       
   324         if( UPnPItemUtility::BelongsToClass(*iLocalItem,KClassAudio()) )
       
   325             {
       
   326             TRAPD(err, AddAlbumArtAndShareL())
       
   327             aError = err;
   214             }
   328             }
   215         else
   329         else
   216             {
   330             {
   217             // start a session for local MS keepalive
   331             TRAPD(err,AddThumbnailandShareL());    
   218             CUpnpAVDevice* dummyDevice = CUpnpAVDevice::NewLC();
   332             aError = err;
   219             dummyDevice->SetUuidL( KNullDesC8 );
       
   220             dummyDevice->SetDeviceType(CUpnpAVDevice::EMediaServer);    
       
   221             iTempSession = &iAvController.StartBrowsingSessionL( *dummyDevice );
       
   222             CleanupStack::PopAndDestroy( dummyDevice );
       
   223 
       
   224             iTempSession->SetObserver( *this );
       
   225             // request for start local MS
       
   226             iState = EStateStartingMS;
       
   227             iTempSession->ReserveLocalMSServicesL();
       
   228             }
   333             }
   229         
   334     	}
   230         }
   335     //inform the observer
   231     else
   336     iObserver->ResolveComplete( *this, aError );
   232         {
   337     }
   233         __LOG( "LocalItemResolver: create item interrupted" );
   338 
   234 		iState = EStateIdle;
   339 // --------------------------------------------------------------------------
   235         }
   340 // CUPnPLocalItemResolver::AddAlbumArtAndShareL
   236         
   341 //---------------------------------------------------------------------------
   237     __LOG( "LocalItemResolver:Resolve() END" );
   342 void CUPnPLocalItemResolver::AddAlbumArtAndShareL()
   238     }
   343     {
   239 
   344     __ASSERT( iLocalItem, __FILE__, __LINE__ );    
   240 
   345     __LOG( "CUPnPLocalItemResolver:AddAlbumArtAndShareL()");
   241 // --------------------------------------------------------------------------
   346     CUpnpElement* albumArtUri = CUpnpElement::NewLC( KElementAlbumArtUri );
   242 // CUPnPLocalItemResolver::DoCancel
   347     CUpnpAttribute* attr = CUpnpAttribute::NewLC( KAttributeProfileId() );
   243 // See upnplocalitemresolver.h
   348     CUpnpDlnaProfiler* profiler = CUpnpDlnaProfiler::NewLC();
   244 // --------------------------------------------------------------------------
   349     
   245 void CUPnPLocalItemResolver::DoCancel()
   350     HBufC8* profile = NULL;
   246     {
   351     profile = UpnpString::FromUnicodeL(*(profiler->ProfileForFileL( 
   247     }
   352             iThumbnailCreator->ThumbnailFilePath())) ); 
   248 
   353     
   249 // --------------------------------------------------------------------------
   354     CleanupStack::PopAndDestroy( profiler );                  
   250 // CUPnPLocalItemResolver::RunError
   355     __LOG1( "CUPnPLocalItemResolver:AddAlbumArtAndShareL() \
   251 // See upnplocalitemresolver.h
   356             profile=%S",profile);
   252 // --------------------------------------------------------------------------
   357     
   253 TInt CUPnPLocalItemResolver::RunError( TInt aError )
   358     attr->SetValueL(*profile);
   254     {
   359     albumArtUri->AddAttributeL(attr);    
   255     __LOG1( "CUPnPLocalItemResolver::RunError %d", aError );
   360     CleanupStack::Pop( attr );                  
   256     // should never be here.
   361 
   257     __PANICD( __FILE__, __LINE__ );
   362     albumArtUri->SetFilePathL( iThumbnailCreator->ThumbnailFilePath() );
   258     return KErrNone;
   363 
   259     }
   364     iLocalItem->AddElementL( albumArtUri );
   260 
   365     CleanupStack::Pop( albumArtUri );    
   261 // --------------------------------------------------------------------------
   366     
   262 // CUPnPLocalItemResolver::RunL
   367     // share
   263 // See upnplocalitemresolver.h
   368     CUpnpPushServer::ShareL( KFirstSharedHash++, *iLocalItem );	
   264 // --------------------------------------------------------------------------
   369     
   265 void CUPnPLocalItemResolver::RunL()
   370     // Store item & resource
   266     {
   371     delete iSharedItem;
   267     __LOG2( "CUPnPLocalItemResolver::RunL iStatus=%d, iState=%d", 
   372     iSharedItem = NULL;
   268         iStatus.Int(), iState );
   373     
   269 
   374     iSharedItem = iLocalItem;
   270 
   375     iResource = &iSelector.SelectResourceL( *iSharedItem );
   271 	if ( iState == EStateSharing )
   376     }
   272 		{
   377 
   273 		// If the sharing failed
   378 // --------------------------------------------------------------------------
   274         if( iStatus.Int() != KErrNone )
   379 // CUPnPLocalItemResolver::AddThumbnailandShareL
   275             {
   380 // See upnplocalitemresolver.h
   276             // Deny access to the files listed in res-elements
   381 //---------------------------------------------------------------------------
   277             SetAccesstoItemResources( *iSharedItem, EFalse );
   382 void CUPnPLocalItemResolver::AddThumbnailandShareL()
   278             }
   383     {
   279         
   384     __ASSERT( iLocalItem, __FILE__, __LINE__ );
   280         if( iStatus.Int() == KErrNone )
   385     UpnpCdsResElementUtility::AddResElementL(*iLocalItem,
   281     		{
   386                                 iThumbnailCreator->ThumbnailFilePath());
   282     	    // replace the IP address in the URI
   387     // share
   283     	    __LOG( "LocalItemResolver:replacing IP in URI" );
   388     CUpnpPushServer::ShareL( KFirstSharedHash++, *iLocalItem );
   284     	    TInetAddr address;
   389 
   285     	    User::LeaveIfError( iMediaServer.Connect() );
   390     // Store item & resource
   286     	    iMediaServer.GetAddress( address );
   391     delete iSharedItem;
   287     	    UPnPCommonUtils::ReplacePlaceHolderInURIL(
   392     iSharedItem = NULL;
   288     	        *iSharedItem, address );
   393     
   289     	    iMediaServer.Close();
   394     iSharedItem = iLocalItem;
   290 
   395     iResource = &iSelector.SelectResourceL( *iSharedItem );
   291     	    // select the resource
   396     }
   292     	    iResource = &iSelector.SelectResourceL( *iSharedItem );
   397 
   293             }
       
   294                
       
   295         Complete( iStatus.Int() );
       
   296 		}
       
   297 		
       
   298     else if ( iState == EStateUnsharing )
       
   299 		{	
       
   300 		iTimer->Cancel();
       
   301 		
       
   302         // Deny access to the files listed in res-elements
       
   303         SetAccesstoItemResources( *iSharedItem, EFalse );
       
   304         
       
   305         iResource = 0; // NOTE: no deletion !
       
   306         iState = EStateIdle;
       
   307         
       
   308         iWait->AsyncStop();
       
   309         }
       
   310         
       
   311     __LOG( "CUPnPLocalItemResolver::RunL end" );
       
   312     }
       
   313     
       
   314     
       
   315 // --------------------------------------------------------------------------
       
   316 // CUPnPLocalItemResolver::ReserveLocalMSServicesCompleted
       
   317 // See upnplocalitemresolver.h
       
   318 //---------------------------------------------------------------------------
       
   319 void CUPnPLocalItemResolver::ReserveLocalMSServicesCompleted(
       
   320     TInt aError )
       
   321     {
       
   322     __LOG( "LocalItemResolver:MSServicesComplete" );
       
   323     __ASSERTD( iState == EStateStartingMS, __FILE__, __LINE__ );
       
   324 
       
   325     if ( aError == KErrNone )
       
   326         {
       
   327         TRAPD( err, DoShareL() );
       
   328         if( err != KErrNone )
       
   329             {
       
   330             __LOG1( "LocalItemResolver:MSServicesComplete\
       
   331  DoShareL failed %d", err );
       
   332             }
       
   333         }
       
   334     else
       
   335         {
       
   336         Complete( aError );
       
   337         }
       
   338     }
       
   339 
       
   340 // --------------------------------------------------------------------------
       
   341 // CUPnPLocalItemResolver::DoShareL
       
   342 // See upnplocalitemresolver.h
       
   343 //---------------------------------------------------------------------------
       
   344 void CUPnPLocalItemResolver::DoShareL()
       
   345     {
       
   346     __LOG( "CUPnPLocalItemResolver::DoShareL start" );
       
   347     // share the item
       
   348     iState = EStateSharing;
       
   349     
       
   350     // Allow access to the files listed in res-elements
       
   351     SetAccesstoItemResources( *iSharedItem, ETrue );
       
   352     
       
   353     // Share the item
       
   354     iFileSharing->ShareItemL( KContainerIdRoot, *iSharedItem, iStatus );
       
   355         
       
   356     SetActive();
       
   357     __LOG( "CUPnPLocalItemResolver::DoShareL end" );
       
   358     }
       
   359 
       
   360 // --------------------------------------------------------------------------
       
   361 // CUPnPLocalItemResolver::DoUnshareL
       
   362 // See upnplocalitemresolver.h
       
   363 // --------------------------------------------------------------------------
       
   364 void CUPnPLocalItemResolver::DoUnshareL()
       
   365     {
       
   366     __LOG( "CUPnPLocalItemResolver::DoUnshareL" );
       
   367 
       
   368     iState = EStateUnsharing;
       
   369     
       
   370     TInt id;
       
   371     TLex8 idParser( iSharedItem->Id() );
       
   372     TInt status = idParser.Val( id );
       
   373     if ( status == KErrNone )
       
   374         {
       
   375         iFileSharing->UnshareItemL( id, iStatus );
       
   376 
       
   377         SetActive();
       
   378         }
       
   379     else
       
   380         {
       
   381         __LOG1( "CUPnPLocalItemResolver::DoUnshareL leave %d", status );
       
   382         User::Leave( status );
       
   383         }
       
   384         
       
   385     __LOG( "CUPnPLocalItemResolver::DoUnshareL - end" );
       
   386     }
       
   387 
       
   388 
       
   389 // --------------------------------------------------------------------------
       
   390 // CUPnPLocalItemResolver::Item
       
   391 // See upnplocalitemresolver.h
       
   392 //---------------------------------------------------------------------------
       
   393 const CUpnpItem& CUPnPLocalItemResolver::Item() const
       
   394     {
       
   395     __ASSERTD( iState == EStateReady, __FILE__, __LINE__ );
       
   396     __ASSERTD( iSharedItem, __FILE__, __LINE__ );
       
   397 
       
   398     return *iSharedItem;
       
   399     }
       
   400 
       
   401 
       
   402 // --------------------------------------------------------------------------
       
   403 // CUPnPLocalItemResolver::Resource
       
   404 // See upnplocalitemresolver.h
       
   405 //---------------------------------------------------------------------------
       
   406 const CUpnpElement& CUPnPLocalItemResolver::Resource() const
       
   407     {
       
   408     __ASSERTD( iState == EStateReady, __FILE__, __LINE__ );
       
   409     __ASSERTD( iResource, __FILE__, __LINE__ );
       
   410 
       
   411     return *iResource;
       
   412     }
       
   413 
       
   414 // --------------------------------------------------------------------------
       
   415 // CUPnPLocalItemResolver::Complete
       
   416 // See upnplocalitemresolver.h
       
   417 //---------------------------------------------------------------------------
       
   418 void CUPnPLocalItemResolver::Complete( TInt aError )
       
   419     {
       
   420     __LOG1( "LocalItemResolver:Complete() %d", aError );
       
   421     __ASSERTD( iState == EStateStartingMS || 
       
   422         iState == EStateSharing, __FILE__, __LINE__ );
       
   423         
       
   424     MUPnPItemResolverObserver& observer = *iObserver;
       
   425     iObserver = 0;
       
   426     if ( aError == KErrNone )
       
   427         {
       
   428         iState = EStateReady;
       
   429         }
       
   430     else
       
   431         {
       
   432         iState = EStateIdle;
       
   433         Cleanup();
       
   434         }
       
   435 
       
   436     observer.ResolveComplete( *this, aError );
       
   437     __LOG( "LocalItemResolver:Complete() END" );
       
   438     }
       
   439 
       
   440 // --------------------------------------------------------------------------
       
   441 // CUPnPLocalItemResolver::Cleanup
       
   442 // See upnplocalitemresolver.h
       
   443 //---------------------------------------------------------------------------
       
   444 void CUPnPLocalItemResolver::Cleanup()
       
   445     {
       
   446     __LOG1( "CUPnPLocalItemResolver:Cleanup() iState %d", iState );
       
   447     
       
   448     iObserver = 0;
       
   449 
       
   450     if ( iState == EStateCreatingItem )
       
   451         {
       
   452         // signal cancel, wait until create item exits
       
   453         iState = EStateCancel;
       
   454         for ( TInt t = KCancelWaitMaximum;
       
   455             t > 0 && iState != EStateIdle;
       
   456             t -= KCancelWaitResolution )
       
   457             {
       
   458             User::After( TTimeIntervalMicroSeconds32(
       
   459                 KCancelWaitResolution ) );
       
   460             }
       
   461         }
       
   462     else if ( iState == EStateStartingMS )
       
   463         {
       
   464         if( iTempSession )
       
   465             {
       
   466             TRAP_IGNORE( iTempSession->CancelReserveLocalMSServicesL() );
       
   467             }
       
   468         }
       
   469     else if ( iState == EStateSharing )
       
   470 		{
       
   471         // do nothing, cannot unshare item if sharing hasn't finished yet.
       
   472         __LOG( "CUPnPLocalItemResolver:Cleanup() sharing in progress, do nothing" );
       
   473 		} 
       
   474     else if ( iState == EStateUnsharing )
       
   475 		{
       
   476         // do nothing, unsharing is still in progress. This happens,
       
   477         // if we start shutting down while previous cleanup hasn't finished yet.
       
   478         __LOG( "CUPnPLocalItemResolver:Cleanup() unsharing in progress, do nothing" );
       
   479 		}
       
   480     else if ( iState == EStateCancel )
       
   481 		{
       
   482         // do nothing, we shouldn't be here
       
   483         __LOG( "CUPnPLocalItemResolver:Cleanup() cancelling, shouldn't be here.." );
       
   484 		}
       
   485     else if ( iSharedItem )
       
   486         {
       
   487         TRAPD( error, DoUnshareL() );
       
   488         
       
   489         if( error == KErrNone )
       
   490             {
       
   491             // wait some time.. 
       
   492             // if unshare doesn't finish in time, just cancel andcontinue
       
   493             __LOG( "CUPnPLocalItemResolver:Cleanup() start timer" );
       
   494             iTimer->Start( KUnshareWait, KUnshareWait, TCallBack( TimerExpired, this ) );
       
   495             iWait->Start();
       
   496             
       
   497             iTimer->Cancel();
       
   498             }
       
   499         else
       
   500             {
       
   501             __LOG1( "CUPnPLocalItemResolver:Cleanup() unshare failed %d", error );
       
   502             }
       
   503         }
       
   504 
       
   505     __LOG( "CUPnPLocalItemResolver:Cleanup() end" );
       
   506     }
       
   507 
       
   508 // --------------------------------------------------------------------------
       
   509 // CUPnPLocalItemResolver::SetAccesstoItemResources
       
   510 // See upnplocalitemresolver.h
       
   511 // --------------------------------------------------------------------------
       
   512 void CUPnPLocalItemResolver::SetAccesstoItemResources(
       
   513                                                 CUpnpItem& aItem,
       
   514                                                 TBool aAccessAllowed )
       
   515     {
       
   516     __LOG( "CUPnPLocalItemResolver::SetAccesstoItemResources" );
       
   517 
       
   518     // Get all filenames and set the access for those.
       
   519     RUPnPElementsArray resElements;
       
   520     UPnPItemUtility::GetResElements( aItem, resElements );
       
   521     for( TInt i=0; i<resElements.Count(); i++ )
       
   522         {
       
   523         if( aAccessAllowed )
       
   524             {
       
   525             iAccessController->AddAllowedFile( resElements[i]->FilePath() );
       
   526             }
       
   527         else
       
   528             {
       
   529             iAccessController->RemoveAllowedFile(
       
   530                                         resElements[i]->FilePath() );
       
   531             }
       
   532         }
       
   533     resElements.Close();
       
   534     }
       
   535 
       
   536 // --------------------------------------------------------------------------
       
   537 // CUPnPLocalItemResolver::TimerExpired
       
   538 // See upnplocalitemresolver.h
       
   539 // -------------------------------------------------------------------------- 
       
   540 TInt CUPnPLocalItemResolver::TimerExpired( TAny* aArg )
       
   541     {
       
   542     __LOG( "CUPnPLocalItemResolver::TimerExpired" );
       
   543     
       
   544     CUPnPLocalItemResolver* resolver =
       
   545         (static_cast<CUPnPLocalItemResolver*>( aArg ));
       
   546     
       
   547     resolver->iTimer->Cancel();
       
   548     resolver->iWait->AsyncStop();
       
   549     
       
   550     __LOG( "CUPnPLocalItemResolver::TimerExpired end" );
       
   551     return KErrNone;
       
   552     }
       
   553