upnpframework/upnpfiletransferengine/src/upnpfiletransferhandler.cpp
changeset 0 7f85d04be362
equal deleted inserted replaced
-1:000000000000 0:7f85d04be362
       
     1 /*
       
     2 * Copyright (c) 2006-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:  Implementation of the CUpnpFileTransferHandler class
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 // System
       
    21 #include <eikenv.h>                             // CEikonEnv
       
    22 #include <eikprogi.h>                           // CEikProgressInfo
       
    23 #include <bautils.h>                            // BaflUtils
       
    24 #include <data_caging_path_literals.hrh>        // KDC_RESOURCE_FILES_DIR
       
    25 
       
    26 #include <upnpitem.h>                           // CUpnpItem
       
    27 #include <upnpcontainer.h>                      // CUpnpContainer
       
    28 #include <upnpstring.h>                         // UpnpString
       
    29 
       
    30 // upnpavcontroller api
       
    31 #include "upnpavbrowsingsession.h"              // MUPnPAVSessionBase
       
    32 #include "upnpavcontrollerfactory.h"            // UPnPAVControllerFactory
       
    33 #include "upnpavcontroller.h"                   // MUPnPAVController
       
    34 #include "upnpfiledownloadsession.h"            // MUPnPFileDownloadSession
       
    35 
       
    36 // upnpavcontroller / xml parser api
       
    37 #include "upnpxmlparser.h"
       
    38 
       
    39 // upnpframework / internal api's
       
    40 #include "upnpgallerynotifier.h"                // UpnpGalleryNotifer
       
    41 
       
    42 // filetransferengine internal
       
    43 #include <upnpfiletransferengineresources.rsg>  // Dialog resources
       
    44 #include "upnpfiletransferhandler.h"            // CUpnpFileTransferHandler
       
    45 
       
    46 // CONSTANTS
       
    47 _LIT( KResFileName,                 "upnpfiletransferengineresources.rsc");
       
    48 _LIT8( KBrowseSortCriteria,         "" );
       
    49 _LIT8( KBrowseFilter,               "*" );
       
    50 const TInt KBrowseRequestCount =        1;
       
    51 const TInt KProgressInfoInitialValue =  1;
       
    52 const TInt KProgressInfoFinalValue =    50000;
       
    53 const TInt KProgressNoteStepper =       2;
       
    54 const TInt KCopyPositionInitialValue =  0;
       
    55 
       
    56 _LIT( KComponentLogfile, "filetransferengine.txt");
       
    57 #include "upnplog.h"
       
    58 
       
    59 
       
    60 // --------------------------------------------------------------------------
       
    61 // CUpnpFileTransferHandler::NewL
       
    62 // NewL.
       
    63 // --------------------------------------------------------------------------
       
    64 //
       
    65 CUpnpFileTransferHandler* CUpnpFileTransferHandler::NewL(
       
    66         MUPnPAVBrowsingSession* aBrowsingSession )
       
    67     {
       
    68     __LOG( "CUpnpFileTransferHandler::NewL"  );
       
    69 
       
    70     // Check that the browsing session is valid and has target device set.
       
    71     if( !aBrowsingSession )
       
    72         {
       
    73         User::Leave( KErrArgument );
       
    74         }
       
    75 
       
    76     CUpnpFileTransferHandler* self = NULL;
       
    77     self = new (ELeave) CUpnpFileTransferHandler;
       
    78     CleanupStack::PushL( self );
       
    79     self->ConstructL( aBrowsingSession );
       
    80     CleanupStack::Pop( self );
       
    81     __LOG( "CUpnpFileTransferHandler::NewL -end"  );
       
    82     return self;
       
    83     }
       
    84 
       
    85 // --------------------------------------------------------------------------
       
    86 // Constuctor
       
    87 // --------------------------------------------------------------------------
       
    88 //
       
    89 CUpnpFileTransferHandler::CUpnpFileTransferHandler()
       
    90     {
       
    91     __LOG( "CUpnpFileTransferHandler::CUpnpFileTransferHandler" );
       
    92     iNumCopiedItemFromContainer = 0;
       
    93     __LOG( "CUpnpFileTransferHandler::CUpnpFileTransferHandler -end" );
       
    94     }
       
    95 
       
    96 // --------------------------------------------------------------------------
       
    97 // Destructor
       
    98 // --------------------------------------------------------------------------
       
    99 //
       
   100 CUpnpFileTransferHandler::~CUpnpFileTransferHandler()
       
   101     {
       
   102     __LOG( "CUpnpFileTransferHandler::~CUpnpFileTransferHandler" );
       
   103 
       
   104     // Restore the browse session observer
       
   105     if( iBrowsingSession &&
       
   106         iBrowsingSessionObserverBackup )
       
   107         {
       
   108         iBrowsingSession->RemoveObserver();
       
   109         iBrowsingSession->SetObserver( *iBrowsingSessionObserverBackup );
       
   110         }
       
   111 
       
   112     // Delete the playlist handler.
       
   113     delete iPlaylistHandler;
       
   114 
       
   115     // Delete copy item (used in container and playlist copy) is it exists
       
   116     delete iCopyItem;
       
   117     
       
   118     // Deletes download session and avcontroller instances.
       
   119     if( iDownloadSession && iAvController )
       
   120         {
       
   121         iAvController->StopDownloadSession( *iDownloadSession );
       
   122         }
       
   123     delete iAvController;
       
   124 
       
   125     // Un-load resource file
       
   126     if ( iResFileOffset )
       
   127         {
       
   128         iCoeEnv->DeleteResourceFile( iResFileOffset );
       
   129         iResFileOffset = 0;
       
   130         }
       
   131     __LOG( "CUpnpFileTransferHandler::~CUpnpFileTransferHandler -end" );
       
   132     }
       
   133 
       
   134 // --------------------------------------------------------------------------
       
   135 // CUpnpFileTransferHandler::ConstructL
       
   136 // Second phase constructor
       
   137 // --------------------------------------------------------------------------
       
   138 //
       
   139 void CUpnpFileTransferHandler::ConstructL(
       
   140         MUPnPAVBrowsingSession* aBrowsingSession )
       
   141     {
       
   142     __LOG( "CUpnpFileTransferHandler::ConstructL" );
       
   143 
       
   144     if( aBrowsingSession )
       
   145         {
       
   146         iBrowsingSession = aBrowsingSession;
       
   147         }
       
   148     else
       
   149         {
       
   150         User::Leave( KErrArgument );
       
   151         }
       
   152 
       
   153     // Leave if UI context is not available
       
   154     iCoeEnv = CEikonEnv::Static();
       
   155     if( !iCoeEnv )
       
   156         {
       
   157         User::Leave( KErrNotReady );
       
   158         }
       
   159 
       
   160     // Get the file server session handle
       
   161     RFs& fs = iCoeEnv->FsSession();
       
   162 
       
   163     // Parse the resource file path
       
   164     TParse parse;
       
   165     parse.Set( KResFileName, &KDC_RESOURCE_FILES_DIR, NULL );
       
   166     TFileName rscFileName;
       
   167     rscFileName.Append( parse.FullName() );
       
   168     TFileName dllName;
       
   169     Dll::FileName( dllName );
       
   170     TBuf<2> drive = dllName.Left( 2 ); // Drive letter followed by ':' 
       
   171     rscFileName.Insert( 0, drive );
       
   172     
       
   173     // Get the exact filename of the resource file
       
   174     BaflUtils::NearestLanguageFile( fs, rscFileName );
       
   175 
       
   176     // Check if the resource file exists or not
       
   177     if ( !BaflUtils::FileExists( fs, rscFileName ) )
       
   178         {
       
   179         __LOG( "Resource file does not exist!" );
       
   180         User::Leave( KErrNotFound );
       
   181         }
       
   182 
       
   183     // Read the resource file offset
       
   184     iResFileOffset = iCoeEnv->AddResourceFileL( rscFileName );
       
   185 
       
   186     // Backup the browse session observer and set this object as an observer.
       
   187     iBrowsingSessionObserverBackup = iBrowsingSession->Observer();
       
   188     
       
   189 
       
   190     iAvController = UPnPAVControllerFactory::NewUPnPAVControllerL();
       
   191 
       
   192     // Create download session
       
   193     iDownloadSession = &iAvController->StartDownloadSessionL(
       
   194                                             iBrowsingSession->Device() );
       
   195 
       
   196     // Set this object to be the download session observer
       
   197     iDownloadSession->SetObserver( *this );
       
   198     
       
   199     iFileTransferMode = EUpnpIdleTransferMode;
       
   200     
       
   201     // Instantiate the playlist handler
       
   202     iPlaylistHandler = CUpnpPlaylistHandler::NewL();
       
   203     
       
   204     __LOG( "CUpnpFileTransferHandler::ConstructL -end" );
       
   205     }
       
   206    
       
   207 // --------------------------------------------------------------------------
       
   208 // CUpnpFileTransferHandler::TransferRemoteItemsToHandsetL
       
   209 // Copies items from a remote Upnp Media Server to the handset.
       
   210 // --------------------------------------------------------------------------
       
   211 //
       
   212 void CUpnpFileTransferHandler::TransferRemoteItemsToHandsetL(
       
   213         TUpnpFileTransferMode aTransferMode,
       
   214         RPointerArray<CUpnpItem>& aItems )
       
   215     {
       
   216     __LOG( "CUpnpFileTransferHandler::TransferRemoteItemsToHandsetL" );
       
   217     if( aItems.Count() <= 0 ||
       
   218         aTransferMode != EUpnpCopyRemoteItemsToDefaultLocation )
       
   219         {
       
   220         User::Leave( KErrArgument ); 
       
   221         }
       
   222 
       
   223     // Initialise member variables
       
   224     iFileTransferMode = aTransferMode;
       
   225     iCopyItems = &aItems;
       
   226     iCopyPosition = KCopyPositionInitialValue;
       
   227     iStatusCode = KErrNone;
       
   228 
       
   229     // Copy the first file
       
   230     TRAP( iStatusCode, iDownloadSession->StartDownloadL( 
       
   231             (*(*iCopyItems)[iCopyPosition]),
       
   232             iCopyPosition ) );
       
   233     
       
   234     // If first copy action was sent succesfully, draw the progress note,
       
   235     // otherwise leave with the error code
       
   236     if( iStatusCode == KErrNone )
       
   237         {
       
   238         RunCopyNoteL( iCopyItems->Count() );
       
   239         }
       
   240 
       
   241     // Notify Media Gallery of the filesystem changes (files added)
       
   242     TRAP_IGNORE( UpnpGalleryNotifier::NotifyMediaGalleryL() );
       
   243 
       
   244     // Clean up
       
   245     iFileTransferMode = EUpnpIdleTransferMode;
       
   246 
       
   247     // Leave if there was an error
       
   248     if( iStatusCode != KErrNone )
       
   249         {
       
   250         User::Leave( iStatusCode );
       
   251         }
       
   252     
       
   253     __LOG( "CUpnpFileTransferHandler::TransferRemoteItemsToHandsetL -end" );
       
   254     }
       
   255     
       
   256 // --------------------------------------------------------------------------
       
   257 // CUpnpFileTransferHandler::TransferRemoteContainerToHandsetL
       
   258 // Copies a container from a remote Upnp Media Server to the handset.
       
   259 // --------------------------------------------------------------------------
       
   260 //
       
   261 void CUpnpFileTransferHandler::TransferRemoteContainerToHandsetL(
       
   262         TUpnpFileTransferMode aTransferMode,
       
   263         CUpnpContainer* aSourceContainer )
       
   264     {
       
   265     __LOG( "CUpnpFileTransferHandler::TransferRemoteContainerToHandsetL" );
       
   266 
       
   267     iStatusCode = KErrNone;
       
   268 
       
   269     if( !aSourceContainer ||
       
   270         aSourceContainer->Title() == KNullDesC8 ||
       
   271         ( aTransferMode != EUpnpCopyRemoteContainerToDefaultLocation &&
       
   272           aTransferMode != EUpnpCopyRemotePlaylistToDefaultLocation ) )
       
   273         {
       
   274         User::Leave( KErrArgument );
       
   275         }
       
   276 
       
   277     // Reset the playlist handler
       
   278     iPlaylistHandler->Reset();
       
   279 
       
   280     // Convert the container title to playlist name
       
   281     HBufC* titleUnicode = NULL;
       
   282     titleUnicode = UpnpString::ToUnicodeL( aSourceContainer->Title() );
       
   283     CleanupStack::PushL( titleUnicode );
       
   284     iPlaylistHandler->SetPlaylistNameL( *titleUnicode );
       
   285     CleanupStack::PopAndDestroy( titleUnicode );
       
   286     titleUnicode = NULL;
       
   287 
       
   288     // Initialise copy related member variables
       
   289     iCopyFromContainer = aSourceContainer;
       
   290     iFileTransferMode = aTransferMode;
       
   291     iContainerCopyFirstBrowse = ETrue;
       
   292     iContainerCopyBrowseIndex = 0;
       
   293 
       
   294     iBrowsingSession->SetObserver( *this );
       
   295     // Start the copy process
       
   296     TRAP( iStatusCode,
       
   297           iBrowsingSession->BrowseL(
       
   298                               iCopyFromContainer->Id(),
       
   299                               KBrowseFilter(),
       
   300                               MUPnPAVBrowsingSession::EDirectChildren,
       
   301                               iContainerCopyBrowseIndex,
       
   302                               KBrowseRequestCount,
       
   303                               KBrowseSortCriteria() ) );
       
   304 
       
   305     if( iStatusCode == KErrNone )
       
   306         {
       
   307         // Start the progress Dialog, but do not set current and 
       
   308         // final values for the progress bar. This info will be set
       
   309         // when the first browse result will be reseived.
       
   310         RunCopyNoteL();
       
   311 
       
   312         // If no files were copied, change the copy status code
       
   313         if( iPlaylistHandler->AudioItemCount() <= 0 &&
       
   314             iPlaylistHandler->ImageItemCount() <= 0 &&
       
   315             iPlaylistHandler->VideoItemCount() <= 0 &&
       
   316             iStatusCode == KErrNone )
       
   317             {
       
   318             iStatusCode = KErrNotFound;
       
   319             }
       
   320 
       
   321         // If files were copied, create playlists, albums and notify
       
   322         // Media Gallery
       
   323         else
       
   324             {
       
   325             // If audio files were copied, and the operation was to
       
   326             // copy a playlist, then create a MPX playlist
       
   327             if( iPlaylistHandler->AudioItemCount() > 0 &&
       
   328                 ( aTransferMode ==
       
   329                         EUpnpCopyRemotePlaylistToDefaultLocation ) )
       
   330                 {
       
   331                 TRAP_IGNORE( iPlaylistHandler->CreateMusicPlaylistL() );
       
   332                 }
       
   333 
       
   334             // If image files were copied, create an image album
       
   335             if( iPlaylistHandler->ImageItemCount() > 0 &&
       
   336                 ( aTransferMode ==
       
   337                         EUpnpCopyRemoteContainerToDefaultLocation ) )
       
   338                 {
       
   339                 TRAP_IGNORE( iPlaylistHandler->CreateImageAlbumL() );
       
   340                 }
       
   341 
       
   342             // If video files were copied, create a video album
       
   343             if( iPlaylistHandler->VideoItemCount() > 0 &&
       
   344                 ( aTransferMode ==
       
   345                         EUpnpCopyRemoteContainerToDefaultLocation ) )
       
   346                 {
       
   347                 TRAP_IGNORE( iPlaylistHandler->CreateVideoAlbumL() );
       
   348                 }
       
   349             }
       
   350 
       
   351         // Notify Media Gallery of filesystem changes, if files were
       
   352         // added.
       
   353         if( iPlaylistHandler->ImageItemCount() > 0 ||
       
   354             iPlaylistHandler->VideoItemCount() > 0 || 
       
   355             iPlaylistHandler->AudioItemCount() > 0 )
       
   356             {
       
   357             __LOG( "Notifying the Media Gallery of new \
       
   358 image/video/music files." );
       
   359             TRAP_IGNORE( UpnpGalleryNotifier::NotifyMediaGalleryL() );
       
   360             }
       
   361         
       
   362         __LOG( "TransferRemoteContainerToHandsetL Reset");
       
   363         // Reset the playlist handler
       
   364         iPlaylistHandler->Reset();
       
   365         }
       
   366 
       
   367     // Set file transfer mode to idle
       
   368     iFileTransferMode = EUpnpIdleTransferMode;
       
   369 
       
   370     // If the copy failed, leave with the error code
       
   371     if( iStatusCode != KErrNone )
       
   372         {
       
   373         User::Leave( iStatusCode );
       
   374         }
       
   375     
       
   376     __LOG( "CUpnpFileTransferHandler::TransferRemoteContainerToHandsetL -end" );
       
   377     }
       
   378 
       
   379 // --------------------------------------------------------------------------
       
   380 // CUpnpFileTransferHandler::CopyCompleteContainersL
       
   381 // Handles the copy complete callback when copying containers.
       
   382 // --------------------------------------------------------------------------
       
   383 //
       
   384 void CUpnpFileTransferHandler::CopyCompleteContainersL(
       
   385         TInt aError,
       
   386         const TDesC& aFilepath )
       
   387     {
       
   388     __LOG( "CUpnpFileTransferHandler::CopyCompleteContainersL" );
       
   389 
       
   390     // Update the progress note
       
   391     iCurrentProgressValue++;
       
   392     if( iProgressInfo )
       
   393         {
       
   394         iProgressInfo->SetAndDraw( iCurrentProgressValue );
       
   395         }
       
   396 
       
   397     if( aError == KErrNone )
       
   398         {
       
   399         iNumCopiedItemFromContainer++;
       
   400         // Resolve the type of the media file
       
   401         TUPnPItemType itemType = ResolveFileType( aFilepath );
       
   402 
       
   403         // Add the file to playlist handler's lists
       
   404         if( itemType == ETypeAudio &&
       
   405             iCopyItem )
       
   406             {
       
   407             TRAP_IGNORE(
       
   408                 iPlaylistHandler->NotifyNewAudioFileL(
       
   409                     aFilepath, *iCopyItem );
       
   410                 iPlaylistHandler->AddAudioItemL( aFilepath ) );
       
   411             }
       
   412         else if( itemType == ETypeVideo )
       
   413             {
       
   414             TRAP_IGNORE( iPlaylistHandler->AddVideoItemL( aFilepath) );
       
   415             }
       
   416         else if( itemType == ETypeImage )
       
   417             {
       
   418             TRAP_IGNORE( iPlaylistHandler->AddImageItemL( aFilepath) );
       
   419             }
       
   420         else
       
   421             {
       
   422             // Nothing
       
   423             }
       
   424         }
       
   425 
       
   426     // Clean up the copy item
       
   427     delete iCopyItem;
       
   428     iCopyItem = NULL;
       
   429 
       
   430     // If the copy of the previous file failed with any error code, quit the
       
   431     // whole copy operation
       
   432     if( aError != KErrNone )
       
   433         {
       
   434         iStatusCode = aError;
       
   435         
       
   436         FinishNotes();
       
   437         return;
       
   438         }
       
   439 
       
   440     // If there are more objects (possibly items), browse for the next one,
       
   441     // otherwise finish the progress note.
       
   442     if( iContainerCopyBrowseIndex < iContainerCopyBrowseTotalCount )
       
   443         {
       
   444         iBrowsingSession->SetObserver( *this );
       
   445         TRAPD( browseError,
       
   446                iBrowsingSession->BrowseL(
       
   447                                   iCopyFromContainer->Id(),
       
   448                                   KBrowseFilter(),
       
   449                                   MUPnPAVBrowsingSession::EDirectChildren,
       
   450                                   iContainerCopyBrowseIndex,
       
   451                                   KBrowseRequestCount,
       
   452                                   KBrowseSortCriteria() ) );
       
   453 
       
   454         // If the browse could not be sent, finish the operation
       
   455         if( browseError != KErrNone && KErrServerBusy != browseError )
       
   456             {
       
   457             // Update the status code
       
   458             if( iStatusCode == KErrNone )
       
   459                 {
       
   460                 iStatusCode = browseError;
       
   461                 }
       
   462             FinishNotes();
       
   463             }
       
   464             
       
   465         }
       
   466     else
       
   467         {
       
   468         FinishNotes();
       
   469         }
       
   470     
       
   471     __LOG( "CUpnpFileTransferHandler::CopyCompleteContainersL -end" );
       
   472     }
       
   473 
       
   474 // --------------------------------------------------------------------------
       
   475 // CUpnpFileTransferHandler::CopyCompleteFilesL
       
   476 // Handles the copy complete callback when copying files
       
   477 // --------------------------------------------------------------------------
       
   478 //
       
   479 void CUpnpFileTransferHandler::CopyCompleteFilesL( 
       
   480         TInt aError,
       
   481         const TDesC& aFilepath )
       
   482     {
       
   483     __LOG1( "CUpnpFileTransferHandler::CopyCompleteFilesL %d", aError );
       
   484 
       
   485     if( aError == KErrNone )
       
   486         {
       
   487         iCurrentProgressValue = iCurrentProgressValue + iProgressIncrement;
       
   488         if( iProgressInfo )
       
   489             {
       
   490             iProgressInfo->SetAndDraw( iCurrentProgressValue );
       
   491             }
       
   492         
       
   493         // If a new file was copied to the handset, inform MPX
       
   494         TInt returnValue = KErrNone;
       
   495         if( iFileTransferMode == EUpnpCopyRemoteItemsToDefaultLocation )
       
   496             {
       
   497             if( (*iCopyItems)[iCopyPosition] )
       
   498                 {
       
   499                 // Resolve the type of the media file
       
   500                 TUPnPItemType itemType = ResolveFileType( aFilepath );
       
   501 
       
   502                 // Nofity MPX of a new file
       
   503                 if( itemType == ETypeAudio )
       
   504                     {
       
   505                     TRAP_IGNORE( iPlaylistHandler->NotifyNewAudioFileL(
       
   506                         aFilepath, *(*iCopyItems)[iCopyPosition] ) );
       
   507                     }
       
   508                 }
       
   509 
       
   510             // Delete the filename and remove from the array
       
   511             delete (*iCopyItems)[iCopyPosition];
       
   512             (*iCopyItems)[iCopyPosition] = NULL;
       
   513             
       
   514             iCopyItems->Remove( iCopyPosition );
       
   515             iCopyItems->Compress();
       
   516             
       
   517             if( iCopyPosition > iCopyItems->Count() ||
       
   518                 iCopyPosition == iCopyItems->Count() )
       
   519                 {
       
   520                 __LOG( "All files copied. Exiting..." );
       
   521 
       
   522                 // Finish the progress Dialog
       
   523                 iStatusCode = aError;
       
   524                 
       
   525                 FinishNotes();
       
   526                 }
       
   527             // If there are still more files to copy, increment the position
       
   528             // counter, and do the next copy    
       
   529             else
       
   530                 {
       
   531                 TRAP( returnValue, 
       
   532                       iDownloadSession->StartDownloadL( 
       
   533                                           (*(*iCopyItems)[iCopyPosition]),
       
   534                                           iCopyPosition ) );
       
   535                 }
       
   536             }
       
   537             
       
   538         // In case of an error, quit copying
       
   539         if( returnValue != KErrNone )
       
   540             {
       
   541             // Update the status
       
   542             if( iStatusCode == KErrNone )
       
   543                 {
       
   544                 iStatusCode = returnValue;
       
   545                 }
       
   546             // Finish the operation
       
   547             FinishNotes();
       
   548             }     
       
   549         } //if( aError == KErrNone )
       
   550     else
       
   551         {
       
   552         __LOG1("Unable to cont. copy: error %d!", aError );
       
   553         
       
   554         iStatusCode = aError;
       
   555 
       
   556         // Finish the progress Dialog
       
   557         FinishNotes();
       
   558         }
       
   559     
       
   560     __LOG( "CUpnpFileTransferHandler::CopyCompleteFilesL -end" );    
       
   561     }
       
   562     
       
   563 // --------------------------------------------------------------------------
       
   564 // CUpnpFileTransferHandler::DialogDismissedL
       
   565 // ProgressDialog call back method. Get's called when a Dialog is
       
   566 // dismissed.
       
   567 // --------------------------------------------------------------------------
       
   568 //
       
   569 void CUpnpFileTransferHandler::DialogDismissedL( TInt aButtonId )
       
   570     {
       
   571     
       
   572     // if dialog is being dismissed, cancel every ongoing requrest
       
   573     __LOG( "CUpnpFileTransferHandler::DialogDismissedL" );
       
   574     __LOG( " User cancelled the copy operation." );
       
   575 
       
   576     iBrowsingSession->CancelBrowse();
       
   577     iDownloadSession->CancelAllTransfers();
       
   578     iDownloadSession->RemoveObserver();
       
   579         
       
   580     if( aButtonId == EAknSoftkeyCancel )
       
   581         {
       
   582         // Update the status code
       
   583         if( iStatusCode == KErrNone )
       
   584             {
       
   585             iStatusCode = KErrCancel;
       
   586             }
       
   587         }
       
   588     
       
   589     __LOG( "CUpnpFileTransferHandler::DialogDismissedL -end" );
       
   590     }
       
   591 
       
   592 // --------------------------------------------------------------------------
       
   593 // CUpnpFileTransferHandler::MediaServerDisappeared
       
   594 // Notifies that the Media Server we have a session with has disappeared. 
       
   595 // Session is now unusable and must be closed.
       
   596 // --------------------------------------------------------------------------
       
   597 //
       
   598 void CUpnpFileTransferHandler::MediaServerDisappeared(
       
   599         TUPnPDeviceDisconnectedReason aReason )
       
   600     {
       
   601     __LOG( "CUpnpFileTransferHandler::MediaServerDisappeared" );
       
   602 
       
   603     // Update the status code
       
   604     if( iStatusCode == KErrNone )
       
   605         {
       
   606         if( aReason == EDisconnected )
       
   607             {
       
   608             iStatusCode = KErrSessionClosed;
       
   609             }
       
   610         else if( aReason == EWLANLost )
       
   611             {
       
   612             iStatusCode = KErrDisconnected;
       
   613             }
       
   614         else
       
   615             {
       
   616             iStatusCode = KErrUnknown;
       
   617             }
       
   618         }
       
   619 
       
   620     // Finish wait/progress note
       
   621     FinishNotes();
       
   622     
       
   623     __LOG( "CUpnpFileTransferHandler::MediaServerDisappeared -end" );
       
   624     }
       
   625 
       
   626 // --------------------------------------------------------------------------
       
   627 // CUpnpFileTransferHandler::BrowseResponse
       
   628 // Returns processed browse results received from a Media Server.
       
   629 // --------------------------------------------------------------------------
       
   630 //
       
   631 void CUpnpFileTransferHandler::BrowseResponse( 
       
   632         const TDesC8& aBrowseResponse,
       
   633         TInt aError, 
       
   634         TInt /*aMatches*/, 
       
   635         TInt aTotalCount, 
       
   636         const TDesC8& /*aUpdateId*/ )
       
   637     {
       
   638     __LOG( "CUpnpFileTransferHandler::BrowseResponse" );
       
   639 
       
   640     if( iBrowsingSession &&
       
   641         iBrowsingSessionObserverBackup )
       
   642         {
       
   643         iBrowsingSession->SetObserver( *iBrowsingSessionObserverBackup );
       
   644         }
       
   645     CUPnPXMLParser* parser = NULL;
       
   646     TRAP( aError, parser = CUPnPXMLParser::NewL() );
       
   647     if( aError == KErrNone )
       
   648         {
       
   649         RPointerArray<CUpnpObject> array;
       
   650         
       
   651 
       
   652         TRAP( aError, parser->ParseResultDataL( array,
       
   653         aBrowseResponse ) );
       
   654         
       
   655         if( aError == KErrNone &&
       
   656             iStatusCode == KErrNone )
       
   657             {
       
   658             if( aTotalCount > 0 )
       
   659                 {
       
   660                 iContainerCopyBrowseTotalCount = aTotalCount;
       
   661                 iContainerCopyBrowseIndex++;
       
   662                 iCurrentProgressValue++;
       
   663 
       
   664                 if( iProgressInfo )
       
   665                     {
       
   666                     // If this was the first browse in container copy
       
   667                     // operation, initialize the progress note, otherwise
       
   668                     // just update it
       
   669                     if( iContainerCopyFirstBrowse )
       
   670                         {
       
   671                         iContainerCopyFirstBrowse = EFalse;
       
   672                         iProgressInfo->SetFinalValue( ( aTotalCount * 
       
   673                             KProgressNoteStepper ) );
       
   674                         iProgressInfo->SetAndDraw( iCurrentProgressValue );
       
   675                         }
       
   676                     else
       
   677                         {
       
   678                         iProgressInfo->SetAndDraw( iCurrentProgressValue );
       
   679                         }
       
   680                     }
       
   681 
       
   682                 // If the object was an item, create a copy of the item
       
   683                 // object
       
   684                 iStatusCode = KErrNone;
       
   685                 if( array.Count() > 0 &&
       
   686                     array[0] &&
       
   687                     array[0]->ObjectType() == EUPnPItem )
       
   688                     {
       
   689                     // Store the item (the item's metadata is needed when the
       
   690                     // copy is complete)
       
   691                     delete iCopyItem;
       
   692                     iCopyItem = NULL;
       
   693 
       
   694                     TRAP( iStatusCode,
       
   695                           iCopyItem = CUpnpItem::NewL() );
       
   696                     if( iStatusCode == KErrNone )
       
   697                         {
       
   698                         TRAP( iStatusCode, iCopyItem->CopyL(
       
   699                             *(CUpnpItem*)array[0] ) );
       
   700                         }
       
   701                     }
       
   702                 
       
   703                 // Copy the item
       
   704                 if( iStatusCode == KErrNone &&
       
   705                     iCopyItem &&
       
   706                     array.Count() > 0 &&
       
   707                     array[0] &&
       
   708                     array[0]->ObjectType() == EUPnPItem )
       
   709                     {
       
   710                     // Copy the next file
       
   711                     if( iFileTransferMode ==
       
   712                                 EUpnpCopyRemoteContainerToDefaultLocation ||
       
   713                         iFileTransferMode ==
       
   714                                 EUpnpCopyRemotePlaylistToDefaultLocation )
       
   715                         {
       
   716                         TRAP( iStatusCode, iDownloadSession->
       
   717                             StartDownloadL( (*(CUpnpItem*)array[0]), 0 ) );
       
   718                         }
       
   719                     else
       
   720                         {
       
   721                         // If the transfer mode is unknown, finish progress
       
   722                         // note
       
   723                         iStatusCode = KErrUnknown;
       
   724                         FinishNotes();
       
   725                         }
       
   726 
       
   727                     // If the copy could not be started, finish progress note
       
   728                     if( iStatusCode != KErrNone )
       
   729                         {
       
   730                         FinishNotes();
       
   731                         }
       
   732                     }
       
   733                 // If the object was not an item, but there are still objects
       
   734                 // left in the container, do a browse for the next object,
       
   735                 // otherwise finish the progress note.
       
   736                 else if( iContainerCopyBrowseIndex < 
       
   737                          iContainerCopyBrowseTotalCount )
       
   738                     {
       
   739                     // Update the progress bar ("CopyFile action part" of the 
       
   740                     // progress has to drawn here since the copy is not
       
   741                     // performed for container objects.
       
   742                     iCurrentProgressValue++;
       
   743                     if( iProgressInfo )
       
   744                         {
       
   745                         iProgressInfo->SetAndDraw( iCurrentProgressValue );
       
   746                         }
       
   747 
       
   748                     iBrowsingSession->SetObserver( *this );
       
   749                     
       
   750                     TRAP( iStatusCode,
       
   751                           iBrowsingSession->BrowseL(
       
   752                                     iCopyFromContainer->Id(),
       
   753                                     KBrowseFilter(),
       
   754                                     MUPnPAVBrowsingSession::EDirectChildren,
       
   755                                     iContainerCopyBrowseIndex,
       
   756                                     KBrowseRequestCount,
       
   757                                     KBrowseSortCriteria() ) );
       
   758 
       
   759                     // If the browse could not be sent, finish progress note
       
   760                     if( iStatusCode != KErrNone )
       
   761                         {
       
   762                         FinishNotes();
       
   763                         }
       
   764 
       
   765                     }
       
   766                 // If the last object of the container was a container,
       
   767                 // finish the progress note
       
   768                 else
       
   769                     {
       
   770                     FinishNotes();
       
   771                     }
       
   772                 }
       
   773             else
       
   774                 {
       
   775                 __LOG( "BrowseResponse, \
       
   776 empty container, exit (-1)." );
       
   777                 iStatusCode = KErrNotFound;
       
   778                 FinishNotes();
       
   779                 }
       
   780             }
       
   781         else //if( aError == KErrNone && iStatusCode == KErrNone )
       
   782             {
       
   783             if( iStatusCode == KErrNone )
       
   784                 {
       
   785                 iStatusCode = aError;
       
   786                 }
       
   787             FinishNotes();
       
   788             __LOG1( "BrowseResponse, parsing failed, exit (%d).", aError );
       
   789             
       
   790             }
       
   791         
       
   792         array.ResetAndDestroy();
       
   793                 
       
   794         delete parser;
       
   795         }
       
   796     else
       
   797         {
       
   798         __LOG1( "BrowseResponse,browse failed, exit (%d)." , aError );
       
   799 
       
   800         // Update the status code
       
   801         if( iStatusCode == KErrNone )
       
   802             {
       
   803             iStatusCode = aError;
       
   804             }
       
   805 
       
   806         // Finish the operation
       
   807         FinishNotes();
       
   808         }
       
   809     
       
   810     __LOG( "CUpnpFileTransferHandler::BrowseResponse -end" );
       
   811     }
       
   812 
       
   813 // --------------------------------------------------------------------------
       
   814 // CUpnpFileTransferHandler::RunCopyNoteL
       
   815 // Initialises and runs the progress note.
       
   816 // --------------------------------------------------------------------------
       
   817 //
       
   818 void CUpnpFileTransferHandler::RunCopyNoteL( TInt aCount )
       
   819     {
       
   820     __LOG( "CUpnpFileTransferHandler::RunCopyNoteL" );
       
   821     
       
   822     if( iProgressNoteDialog )
       
   823         {
       
   824         User::Leave( KErrInUse );
       
   825         }
       
   826     
       
   827     // If aCount is zero, creates "unitialized" dialog.
       
   828     if( aCount < 0 )
       
   829         {
       
   830         User::Leave( KErrArgument );
       
   831         }    
       
   832         
       
   833     // Creates dialog
       
   834     iProgressNoteDialog = new (ELeave) CAknProgressDialog(
       
   835             ( REINTERPRET_CAST( CEikDialog**, &iProgressNoteDialog ) ) );
       
   836     iProgressNoteDialog->PrepareLC( R_FT_COPY_PROGRESS_NOTE_DIALOG );
       
   837     iProgressNoteDialog->SetCallback( this );
       
   838     iProgressInfo = iProgressNoteDialog->GetProgressInfoL();
       
   839     
       
   840     if ( aCount )
       
   841         {
       
   842         // Incrementing dialog values.
       
   843         iProgressInfo->SetFinalValue( KProgressInfoFinalValue );
       
   844         iProgressIncrement = KProgressInfoFinalValue /
       
   845                          ( aCount * KProgressNoteStepper + 
       
   846                                  KProgressNoteStepper );
       
   847         iCurrentProgressValue = KProgressNoteStepper * iProgressIncrement;
       
   848         }
       
   849     else
       
   850         {
       
   851         // Uninitialized dialog values.
       
   852         iProgressInfo->SetFinalValue( KProgressInfoInitialValue );
       
   853         iProgressIncrement = 0;
       
   854         iCurrentProgressValue = 0;
       
   855         }
       
   856     
       
   857     iProgressInfo->SetAndDraw( iCurrentProgressValue );
       
   858     iProgressNoteDialog->RunLD();
       
   859     
       
   860     __LOG( "CUpnpFileTransferHandler::RunCopyNoteL -end" );
       
   861     }
       
   862 
       
   863 // --------------------------------------------------------------------------
       
   864 // CUpnpFileTransferHandler::FinishNotes
       
   865 // Finishes the progress and wait note if they are running.
       
   866 // --------------------------------------------------------------------------
       
   867 //
       
   868 void CUpnpFileTransferHandler::FinishNotes()
       
   869     {
       
   870     __LOG( "CUpnpFileTransferHandler::FinishNotes" );
       
   871 
       
   872     // If the progress note is running, finish it
       
   873     if( iProgressNoteDialog )
       
   874         {
       
   875         TRAP_IGNORE( iProgressNoteDialog->ProcessFinishedL() );
       
   876         // since ProcessFinishedL is called iProgressInfo is not 
       
   877         // valid anymore, set it to NULL
       
   878         iProgressInfo = NULL;
       
   879         }
       
   880 
       
   881     __LOG( "CUpnpFileTransferHandler::FinishNotes -end" );    
       
   882     }
       
   883 
       
   884 // --------------------------------------------------------------------------
       
   885 // CUpnpFileTransferHandler::StartLocalMediaServerCompleted
       
   886 // Notifies that the Media Server startup has completed.
       
   887 // --------------------------------------------------------------------------
       
   888 //
       
   889 void CUpnpFileTransferHandler::ReserveLocalMSServicesCompleted( TInt aError )
       
   890     {
       
   891     __LOG( "CUpnpFileTransferHandler::ReserveLocalMSServicesCompleted" );
       
   892 
       
   893     // If the connection is still good (we did not receive media server
       
   894     // disappear/WLAN disconnect while we were waiting), update the status
       
   895     // code.
       
   896     if( iStatusCode == KErrNone )
       
   897         {
       
   898         iStatusCode = aError;
       
   899         }
       
   900 
       
   901     __LOG( "CUpnpFileTransferHandler::ReserveLocalMSServicesCompleted -end" );
       
   902     }
       
   903 
       
   904 // --------------------------------------------------------------------------
       
   905 // CUpnpFileTransferHandler::ResolveFileType
       
   906 // Resolves the media type of the given file.
       
   907 // --------------------------------------------------------------------------
       
   908 //
       
   909 TUPnPItemType CUpnpFileTransferHandler::ResolveFileType(
       
   910         const TDesC& aFilepath )
       
   911     {
       
   912     __LOG( "CUpnpFileTransferHandler::ResolveFileType" );
       
   913 
       
   914     TUPnPItemType itemType = ETypeOther;
       
   915     TRAP_IGNORE( itemType = UPnPCommonUtils::ResolveFileTypeL( aFilepath ) );
       
   916     
       
   917     __LOG( "CUpnpFileTransferHandler::ResolveFileType -end" );
       
   918     
       
   919     return itemType;
       
   920     }
       
   921 
       
   922 // --------------------------------------------------------------------------
       
   923 // CUpnpFileTransferHandler::ItemCopiedFromContainer
       
   924 // Resolves the media type of the given file.
       
   925 // --------------------------------------------------------------------------
       
   926 //
       
   927 TInt CUpnpFileTransferHandler::ItemCopiedFromContainer()
       
   928     {
       
   929     return iNumCopiedItemFromContainer;
       
   930     }
       
   931 
       
   932 // --------------------------------------------------------------------------
       
   933 // CUpnpFileTransferHandler::TransferStarted
       
   934 // --------------------------------------------------------------------------
       
   935 // 
       
   936 void CUpnpFileTransferHandler::TransferStarted( 
       
   937         TInt /*aKey*/,
       
   938         TInt aError )
       
   939     {
       
   940     __LOG1( "CUpnpFileTransferHandler::TransferStarted, %d", aError );
       
   941 
       
   942     // Just update the progress Dialog. No need for further error checks,
       
   943     // because in error situations UpnpAvController calls also CopyComplete,
       
   944     // and detailed error check is done there.
       
   945     if( aError == KErrNone &&
       
   946         iProgressInfo )
       
   947         {
       
   948         iCurrentProgressValue = iCurrentProgressValue + iProgressIncrement;
       
   949         iProgressInfo->SetAndDraw( iCurrentProgressValue );
       
   950         }
       
   951     __LOG( "CUpnpFileTransferHandler::TransferStarted -end" );
       
   952     }
       
   953     
       
   954 // --------------------------------------------------------------------------
       
   955 // CUpnpFileTransferHandler::TransferCompleted
       
   956 // --------------------------------------------------------------------------
       
   957 //    
       
   958 void CUpnpFileTransferHandler::TransferCompleted( 
       
   959         TInt /*aKey*/,
       
   960         TInt aError,
       
   961         const TDesC& aFilepath )
       
   962     {
       
   963     __LOG1( "CUpnpFileTransferHandler::TransferCompleted, %d", aError );
       
   964 
       
   965     TInt copyError = KErrNone;
       
   966     
       
   967     if( iFileTransferMode == EUpnpCopyRemoteContainerToDefaultLocation ||
       
   968         iFileTransferMode == EUpnpCopyRemotePlaylistToDefaultLocation )
       
   969         {
       
   970         TRAP( copyError, CopyCompleteContainersL( aError, aFilepath ) );
       
   971         }
       
   972     else 
       
   973         {
       
   974         TRAP( copyError, CopyCompleteFilesL( aError, aFilepath ) );
       
   975         }
       
   976     if( copyError != KErrNone )
       
   977         {
       
   978         FinishNotes();
       
   979         }
       
   980     __LOG( "CUpnpFileTransferHandler::TransferCompleted -end");    
       
   981     }
       
   982 
       
   983 // --------------------------------------------------------------------------
       
   984 // CUpnpFileTransferHandler::TransferProgress
       
   985 // --------------------------------------------------------------------------
       
   986 //    
       
   987 
       
   988 void CUpnpFileTransferHandler::TransferProgress( 
       
   989         TInt /*aKey*/,
       
   990         TInt /*aBytes*/,
       
   991         TInt /*aTotalBytes*/ )
       
   992     {
       
   993     
       
   994     }
       
   995 
       
   996 // End of file