upnpavcontroller/upnpavcontrollerclient/src/upnpfiledownloadsessionimpl.cpp
changeset 0 7f85d04be362
child 38 5360b7ddc251
equal deleted inserted replaced
-1:000000000000 0:7f85d04be362
       
     1 /*
       
     2 * Copyright (c) 2007 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:      Implements download session
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 
       
    22 
       
    23 // INTERNAL INCLUDES
       
    24 // upnp stack api
       
    25 #include <upnpitem.h>
       
    26 #include <upnpelement.h>
       
    27 #include <upnpstring.h>
       
    28 
       
    29 // upnpframework / avcontroller api
       
    30 #include "upnpavdevice.h"
       
    31 #include "upnpfiletransfersessionobserver.h"
       
    32 
       
    33 // upnpframework / avcontroller helper api
       
    34 #include "upnpconstantdefs.h" // for upnp-specific stuff
       
    35 #include "upnpitemutility.h" // FindAttributeByName
       
    36 #include "upnpfileutility.h" // FitsInMemory
       
    37 
       
    38 // avcontroller internal
       
    39 #include "upnpfiledownloadsessionimpl.h"
       
    40 #include "upnpavcontrollerclient.h"
       
    41 #include "upnpfiletransferitem.h"
       
    42 
       
    43 // EXTERNAL INCLUDES
       
    44 
       
    45 _LIT( KComponentLogfile, "upnpavcontrollerclient.txt");
       
    46 #include "upnplog.h"
       
    47 
       
    48 // ======== MEMBER FUNCTIONS ========
       
    49 
       
    50 // --------------------------------------------------------------------------
       
    51 // CUPnPFileDownloadSessionImpl::NewL
       
    52 // Two-phase construction
       
    53 // --------------------------------------------------------------------------
       
    54 CUPnPFileDownloadSessionImpl* CUPnPFileDownloadSessionImpl::NewL(
       
    55     RUPnPAVControllerClient& aServer, const CUpnpAVDevice& aDevice )
       
    56     {
       
    57     CUPnPFileDownloadSessionImpl* self = new (ELeave)
       
    58         CUPnPFileDownloadSessionImpl( aServer );
       
    59     CleanupStack::PushL( self );
       
    60     self->iDevice = CUpnpAVDevice::NewL( aDevice );
       
    61     self->ConstructL();
       
    62     CleanupStack::Pop( self );
       
    63     return self;
       
    64     }
       
    65 
       
    66 // --------------------------------------------------------------------------
       
    67 // CUPnPFileDownloadSessionImpl::CUPnPFileDownloadSessionImpl
       
    68 // Constructor
       
    69 // --------------------------------------------------------------------------
       
    70 CUPnPFileDownloadSessionImpl::CUPnPFileDownloadSessionImpl(
       
    71     RUPnPAVControllerClient& aServer ) :
       
    72     CActive( EPriorityStandard ),
       
    73     iServer( aServer ),
       
    74     iBufferPtr( 0, 0 ),
       
    75     iBufferPtr2( 0, 0 ),
       
    76     iAlive( ETrue ),
       
    77     iEventPkg( iEvent )
       
    78     {
       
    79     CActiveScheduler::Add( this );
       
    80     }
       
    81 
       
    82 // --------------------------------------------------------------------------
       
    83 // CUPnPFileDownloadSessionImpl::~CUPnPFileDownloadSessionImpl
       
    84 // Destructor
       
    85 // --------------------------------------------------------------------------
       
    86 CUPnPFileDownloadSessionImpl::~CUPnPFileDownloadSessionImpl()
       
    87     {
       
    88     __LOG( "CUPnPFileDownloadSessionImpl::~CUPnPFileDownloadSessionImpl" );
       
    89     
       
    90     CancelAllTransfers();
       
    91     
       
    92     Cancel();
       
    93     iServer.DestroyDownloadSession( (TInt)this );
       
    94     
       
    95     delete iDevice;
       
    96     delete iBuffer;
       
    97     delete iBuffer2;
       
    98     }
       
    99 
       
   100 // --------------------------------------------------------------------------
       
   101 // CUPnPFileDownloadSessionImpl::ConstructL
       
   102 // Two-phase construction
       
   103 // --------------------------------------------------------------------------
       
   104 void CUPnPFileDownloadSessionImpl::ConstructL()
       
   105     {
       
   106     __LOG( "CUPnPFileDownloadSessionImpl::ConstructL" );
       
   107     
       
   108     iBuffer = iDevice->Uuid().AllocL();
       
   109     iBufferPtr.Set( iBuffer->Des() );
       
   110     User::LeaveIfError( iServer.CreateDownloadSession(
       
   111         (TInt)this , iBufferPtr ) );
       
   112     
       
   113     iBuffer2 = HBufC::NewL( KMaxPath );
       
   114     iBufferPtr2.Set( iBuffer2->Des() );    
       
   115     iServer.GetDownloadEvent( (TInt)this, iEventPkg, iStatus, iBufferPtr2 );
       
   116     SetActive();
       
   117     }
       
   118 
       
   119 // --------------------------------------------------------------------------
       
   120 // CUPnPFileDownloadSessionImpl::ConstructL
       
   121 // From CActive
       
   122 // --------------------------------------------------------------------------
       
   123 void CUPnPFileDownloadSessionImpl::RunL()
       
   124     {
       
   125     __LOG( "CUPnPFileDownloadSessionImpl::RunL" );
       
   126 
       
   127     TUpnpFileTransferEvent event = iEvent;
       
   128 
       
   129     if( iStatus.Int() == KErrNone )
       
   130         {
       
   131         __LOG( "RunL - Activate again" );
       
   132 
       
   133         iServer.GetDownloadEvent( (TInt)this, iEventPkg, iStatus,
       
   134             iBufferPtr2 );
       
   135         SetActive();        
       
   136         }
       
   137     else
       
   138         {
       
   139         __LOG1( "RunL - %d", iStatus.Int() );      
       
   140         }              
       
   141     
       
   142     // Handle event and activate again
       
   143     switch( event.iEvent )
       
   144         {
       
   145         case TUpnpFileTransferEvent::ETransferStarted:
       
   146             {
       
   147             if( iObserver )
       
   148                 {
       
   149                 __LOG( "RunL - ETransferStarted" );
       
   150                 
       
   151                 // iKey - 1 since it's incremented when we request the
       
   152                 // download. The reason behind of this is the fact that
       
   153                 // httptransfer does not accept value 0 which is legal from
       
   154                 // the API point of view
       
   155                 iObserver->TransferStarted( event.iKey - 1,
       
   156                     event.iStatus );
       
   157                 }
       
   158             break;
       
   159             }
       
   160 
       
   161         case TUpnpFileTransferEvent::ETransferCompleted:
       
   162             {
       
   163             if( iObserver )
       
   164                 {
       
   165                 __LOG( "RunL - ETransferCompleted" );
       
   166                 
       
   167                 iObserver->TransferCompleted( event.iKey - 1,
       
   168                     event.iStatus, *iBuffer2 );   
       
   169                 }                  
       
   170             break;
       
   171             }
       
   172             
       
   173         case TUpnpFileTransferEvent::ETransferProgress:
       
   174             {
       
   175             if( iObserver )
       
   176                 {
       
   177                 __LOG( "RunL - ETransferProgress" );
       
   178                 
       
   179                 iObserver->TransferProgress( event.iKey - 1,
       
   180                     event.iParam1, event.iParam2 );
       
   181                 }                  
       
   182             break;
       
   183             }
       
   184             
       
   185         case TUpnpFileTransferEvent::EDeviceDisconnected:
       
   186             {
       
   187             CUpnpAVDevice* tmpDev = NULL;
       
   188             UPnPDeviceDisappeared( *tmpDev );
       
   189             break;
       
   190             }
       
   191             
       
   192         default:
       
   193             {
       
   194             __LOG( "RunL - default!" );      
       
   195             break;
       
   196             }
       
   197         
       
   198         }        
       
   199     }
       
   200     
       
   201 // --------------------------------------------------------------------------
       
   202 // CUPnPFileDownloadSessionImpl::DoCancel
       
   203 // From CActive
       
   204 // --------------------------------------------------------------------------
       
   205 void CUPnPFileDownloadSessionImpl::DoCancel()
       
   206     {
       
   207     __LOG( "CUPnPFileDownloadSessionImpl::DoCancel" );
       
   208     
       
   209     iServer.CancelDownloadEvent( (TInt)this );
       
   210     
       
   211     __LOG( "CUPnPFileDownloadSessionImpl::DoCancel - end" );
       
   212     }
       
   213     
       
   214 // --------------------------------------------------------------------------
       
   215 // CUPnPFileDownloadSessionImpl::RunError
       
   216 // From CActive
       
   217 // --------------------------------------------------------------------------
       
   218 TInt CUPnPFileDownloadSessionImpl::RunError( TInt /*aError*/ )
       
   219     {
       
   220     __LOG( "CUPnPFileDownloadSessionImpl::RunError" );
       
   221     
       
   222     return KErrNone;
       
   223     }
       
   224 
       
   225 // --------------------------------------------------------------------------
       
   226 // CUPnPFileDownloadSessionImpl::StartDownloadL
       
   227 // From CActive
       
   228 // --------------------------------------------------------------------------
       
   229 void CUPnPFileDownloadSessionImpl::StartDownloadL(
       
   230     const CUpnpItem& aItem, TInt aKey )
       
   231     {
       
   232     __LOG( "CUPnPFileDownloadSessionImpl::StartDownloadL" );
       
   233     
       
   234     ResetL();
       
   235 
       
   236     CUpnpFileTransferItem* item = CUpnpFileTransferItem::NewLC();
       
   237     item->SetTitleL( aItem.Title() );
       
   238     // aKey + 1 
       
   239     // the reason behind of this is the fact that httptransfer
       
   240     // does not accept value 0 which is legal from the API point
       
   241     // of view.    
       
   242     item->SetKey( aKey + 1 );
       
   243 
       
   244     // Get the most suitable (original) res-element from the item
       
   245     const CUpnpElement& tmpEl = UPnPItemUtility::ResourceFromItemL( 
       
   246         aItem );
       
   247     
       
   248     if( !UPnPFileUtility::FitsInMemory( tmpEl ) )
       
   249         {
       
   250         User::Leave( KErrDiskFull );
       
   251         }
       
   252 
       
   253     item->SetUriL( tmpEl.Value() );    
       
   254 
       
   255     // Get the protocolinfo-attribute
       
   256     const CUpnpAttribute* tmpPInfo = UPnPItemUtility::FindAttributeByName(
       
   257         tmpEl, KAttributeProtocolInfo );
       
   258     if( tmpPInfo )
       
   259         {
       
   260         item->SetProtocolInfoL( tmpPInfo->Value() );
       
   261         }
       
   262     else
       
   263         {
       
   264         User::Leave( KErrArgument );
       
   265         }    
       
   266 
       
   267     iBuffer = item->ToDes8L();
       
   268     iBufferPtr.Set( iBuffer->Des() );
       
   269     
       
   270     CleanupStack::PopAndDestroy( item );
       
   271     
       
   272     User::LeaveIfError( iServer.StartDownload( (TInt)this, iBufferPtr ) );
       
   273     }
       
   274 
       
   275 // --------------------------------------------------------------------------
       
   276 // CUPnPFileDownloadSessionImpl::StartDownloadL
       
   277 // See upnpfiledownloadsessionimpl.h
       
   278 // --------------------------------------------------------------------------
       
   279 void CUPnPFileDownloadSessionImpl::StartDownloadL( 
       
   280     const CUpnpElement& aResElement, const CUpnpItem& aItem,
       
   281     RFile& aFile, TInt aKey )
       
   282     {
       
   283     __LOG( "CUPnPFileDownloadSessionImpl::StartDownloadL" );
       
   284     
       
   285     ResetL();
       
   286 
       
   287     CUpnpFileTransferItem* item = CUpnpFileTransferItem::NewLC();
       
   288     item->SetTitleL( aItem.Title() );
       
   289     item->SetUriL( aResElement.Value() );
       
   290     item->SetKey( aKey + 1 );
       
   291 
       
   292     const CUpnpAttribute* pInfo = UPnPItemUtility::FindAttributeByName(
       
   293         aResElement, KAttributeProtocolInfo );
       
   294     if( pInfo )
       
   295         {
       
   296         item->SetProtocolInfoL( pInfo->Value() );
       
   297         }
       
   298     else
       
   299         {
       
   300         User::Leave( KErrArgument );
       
   301         }   
       
   302 
       
   303     iBuffer = item->ToDes8L();
       
   304     iBufferPtr.Set( iBuffer->Des() );
       
   305     
       
   306     CleanupStack::PopAndDestroy( item );
       
   307     
       
   308     User::LeaveIfError( iServer.StartDownload( (TInt)this, iBufferPtr,
       
   309         aFile ) );
       
   310     }
       
   311 
       
   312 // --------------------------------------------------------------------------
       
   313 // CUPnPFileDownloadSessionImpl::SetObserver
       
   314 // See upnpfiledownloadsessionimpl.h
       
   315 // --------------------------------------------------------------------------
       
   316 void CUPnPFileDownloadSessionImpl::SetObserver(
       
   317     MUPnPFileTransferSessionObserver& aObserver )
       
   318     {
       
   319     __LOG( "CUPnPFileDownloadSessionImpl::SetObserver" );
       
   320     
       
   321     iObserver = &aObserver;
       
   322     }
       
   323      
       
   324 // --------------------------------------------------------------------------
       
   325 // CUPnPFileDownloadSessionImpl::RemoveObserver
       
   326 // See upnpfiledownloadsessionimpl.h
       
   327 // --------------------------------------------------------------------------
       
   328 void CUPnPFileDownloadSessionImpl::RemoveObserver()
       
   329     {
       
   330     iObserver = NULL;
       
   331     }
       
   332 
       
   333 // --------------------------------------------------------------------------
       
   334 // CUPnPFileDownloadSessionImpl::Observer
       
   335 // See upnpfiledownloadsessionimpl.h
       
   336 // --------------------------------------------------------------------------
       
   337 MUPnPFileTransferSessionObserver* CUPnPFileDownloadSessionImpl::Observer()
       
   338     const
       
   339     {
       
   340     return iObserver;
       
   341     }
       
   342 
       
   343 // --------------------------------------------------------------------------
       
   344 // CUPnPFileDownloadSessionImpl::StartTrackingProgressL
       
   345 // See upnpfiledownloadsessionimpl.h
       
   346 // --------------------------------------------------------------------------
       
   347 void CUPnPFileDownloadSessionImpl::StartTrackingProgressL( TInt aKey )
       
   348     {
       
   349     ResetL();
       
   350     User::LeaveIfError( iServer.StartTrackingDownloadProgress(
       
   351         (TInt)this, aKey + 1 ) );
       
   352                    
       
   353     }
       
   354     
       
   355 // --------------------------------------------------------------------------
       
   356 // CUPnPFileDownloadSessionImpl::CancelTransfer
       
   357 // See upnpfiledownloadsessionimpl.h
       
   358 // --------------------------------------------------------------------------
       
   359 void CUPnPFileDownloadSessionImpl::CancelTransfer( TInt aKey )
       
   360     {
       
   361     iServer.CancelDownload( (TInt)this, aKey + 1 ); // Ignore error
       
   362     }
       
   363     
       
   364 // --------------------------------------------------------------------------
       
   365 // CUPnPFileDownloadSessionImpl::CancelAllTransfers
       
   366 // See upnpfiledownloadsessionimpl.h
       
   367 // --------------------------------------------------------------------------
       
   368 void CUPnPFileDownloadSessionImpl::CancelAllTransfers()
       
   369     {
       
   370     iServer.CancelAllDownloads( (TInt)this ); // Ignore error
       
   371     }
       
   372 
       
   373 // --------------------------------------------------------------------------
       
   374 // CUPnPFileDownloadSessionImpl::UPnPDeviceDiscovered
       
   375 // Device discovered
       
   376 // --------------------------------------------------------------------------
       
   377 void CUPnPFileDownloadSessionImpl::UPnPDeviceDiscovered(
       
   378     const CUpnpAVDevice& /*aDevice*/ )
       
   379     {
       
   380     // No implementation needed
       
   381     }
       
   382 
       
   383   
       
   384 // --------------------------------------------------------------------------
       
   385 // CUPnPFileUploadSessionImpl::UPnPDeviceDisappeared
       
   386 // Device disappeared
       
   387 // --------------------------------------------------------------------------
       
   388 void CUPnPFileDownloadSessionImpl::UPnPDeviceDisappeared(
       
   389     const CUpnpAVDevice& /*aDevice*/ )
       
   390     {
       
   391     __LOG( "CUPnPAVBrowsingSessionImpl::UPnPDeviceDisappeared" );
       
   392     
       
   393     iAlive = EFalse;
       
   394     if( iObserver )
       
   395         {
       
   396         iObserver->MediaServerDisappeared( 
       
   397             MUPnPAVSessionObserverBase::EDisconnected );
       
   398         }
       
   399     }
       
   400 
       
   401 // --------------------------------------------------------------------------
       
   402 // CUPnPFileUploadSessionImpl::WLANConnectionLost
       
   403 // Connection lost
       
   404 // --------------------------------------------------------------------------
       
   405 void CUPnPFileDownloadSessionImpl::WLANConnectionLost()
       
   406     {
       
   407     __LOG( "CUPnPAVBrowsingSessionImpl::WLANConnectionLost" );
       
   408     
       
   409     iAlive = EFalse;
       
   410     if( iObserver )    
       
   411         {
       
   412         iObserver->MediaServerDisappeared( 
       
   413             MUPnPAVSessionObserverBase::EWLANLost );
       
   414         }
       
   415     }
       
   416 
       
   417 // --------------------------------------------------------------------------
       
   418 // CUPnPFileDownloadSessionImpl::Device
       
   419 // Returns used device
       
   420 // --------------------------------------------------------------------------
       
   421 const CUpnpAVDevice& CUPnPFileDownloadSessionImpl::Device() const
       
   422     {
       
   423     return *iDevice;
       
   424     }
       
   425     
       
   426 // --------------------------------------------------------------------------
       
   427 // CUPnPFileDownloadSessionImpl::ResetL
       
   428 // Reset
       
   429 // --------------------------------------------------------------------------
       
   430 void CUPnPFileDownloadSessionImpl::ResetL()
       
   431     {
       
   432     __LOG( "CUPnPFileDownloadSessionImpl::ResetL" );
       
   433     
       
   434     if( !iAlive )
       
   435         {
       
   436         User::Leave( KErrDisconnected );
       
   437         }
       
   438 
       
   439     delete iBuffer; iBuffer = NULL;
       
   440     }    
       
   441 
       
   442 // end of file