upnpframework/upnpcommand/src/upnpfiletransferbasetask.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:  Source file for CUpnpFileTransferBaseTask class.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 // system
       
    21 #include <utf.h>                        // ConvertToUnicodeFromUtf8L
       
    22 
       
    23 // upnp stack api
       
    24 #include <upnpitem.h>                   // CUpnpItem
       
    25 #include <upnpobject.h>                 // CUpnpObject (cast)
       
    26 
       
    27 // upnp framework / avcontroller api
       
    28 #include "upnpavcontrollerfactory.h"    // UPnPAVControllerFactory
       
    29 #include "upnpavcontroller.h"           // MUPnPAVController
       
    30 #include "upnpavbrowsingsession.h"      // MUPnPAVBrowsingSession
       
    31 #include "upnpavsessionobserverbase.h"  // MUPnPAVSessionObserverBase
       
    32 #include "upnpavdevice.h"               // CUpnpAVDevice
       
    33 
       
    34 // upnp framework / common utils api
       
    35 #include "upnpcommonutils.h"            // TUPnPItemType
       
    36 #include "upnpmetadatafetcher.h"        // UPnPMetadataFetcher
       
    37 
       
    38 // upnp framework / fte
       
    39 #include "upnpfiletransferengine.h"     // CUpnpFileTransferEngine
       
    40 // upnp framework / common ui
       
    41 #include "upnpcommonui.h"               // CUpnpCommonUI
       
    42 
       
    43 // upnpcommand internal
       
    44 #include "upnpfilepipe.h"               // CUpnpFilePipe
       
    45 #include "upnpcommandparameters.h"      // CUpnpCommandParameters
       
    46 #include "upnptaskhandler.h"            // MUpnpTaskHandler
       
    47 #include "upnpnotehandler.h"            // CUpnpNoteHandler
       
    48 #include "upnpcommand.h"                // CUpnpCommand
       
    49 #include "upnptaskresourceallocator.h"  // CUpnpTaskResourceAllocator
       
    50 #include "upnpfiletransferbasetask.h"   // CUpnpFileTransferBaseTask
       
    51 
       
    52 
       
    53 _LIT( KComponentLogfile, "upnpcommand.log");
       
    54 #include "upnplog.h"
       
    55 
       
    56 
       
    57 // --------------------------------------------------------------------------
       
    58 // CUpnpFileTransferBaseTask::CUpnpFileTransferBaseTask
       
    59 // First phase construction.
       
    60 // --------------------------------------------------------------------------
       
    61 //
       
    62 CUpnpFileTransferBaseTask::CUpnpFileTransferBaseTask()
       
    63     {
       
    64     __LOG( "[UpnpCommand]\t CUpnpFileTransferBaseTask::Constructor" );
       
    65     }
       
    66 
       
    67 // --------------------------------------------------------------------------
       
    68 // Destructor.
       
    69 // --------------------------------------------------------------------------
       
    70 //
       
    71 CUpnpFileTransferBaseTask::~CUpnpFileTransferBaseTask()
       
    72     {
       
    73     __LOG( "[UpnpCommand]\t CUpnpFileTransferBaseTask::Destructor" );
       
    74 
       
    75     // delete the resource allocator
       
    76     // Local mediaserver and AVController resources will be freed.
       
    77     delete iResourceAllocator;
       
    78     delete iCommonUI;
       
    79     }
       
    80 
       
    81 
       
    82 // --------------------------------------------------------------------------
       
    83 // CUpnpFileTransferBaseTask::AllocateFileTransferResourcesL
       
    84 // Allocates the Upnp Fw resources.
       
    85 // --------------------------------------------------------------------------
       
    86 //
       
    87 void CUpnpFileTransferBaseTask::AllocateFileTransferResourcesL( TInt aMode)
       
    88     {
       
    89     __LOG( "[UpnpCommand]\t CUpnpFileTransferBaseTask::AllocateResourcesL" );
       
    90     
       
    91     // create common UI
       
    92     iCommonUI = CUPnPCommonUI::NewL();
       
    93     
       
    94     // create the resource allocator
       
    95     iResourceAllocator = CUpnpTaskResourceAllocator::NewL(
       
    96         *iCommonUI, aMode);
       
    97         
       
    98     iState = EStateConnected; 
       
    99     
       
   100     iResourceAllocator->SetNoteHandlerL( NoteHandler() );
       
   101     
       
   102     // now allocate!
       
   103     iResourceAllocator->AllocateL();
       
   104 
       
   105      // start a browsing session
       
   106     iBrowsingSession =
       
   107         &iResourceAllocator->AVController().StartBrowsingSessionL(
       
   108             iResourceAllocator->SelectedDevice() );
       
   109             
       
   110     iBrowsingSession->SetObserver( *this );
       
   111     }
       
   112 
       
   113 // --------------------------------------------------------------------------
       
   114 // CUpnpFileTransferBaseTask::ResourceAllocator
       
   115 // Executes the task.
       
   116 // --------------------------------------------------------------------------
       
   117 //
       
   118 CUpnpTaskResourceAllocator* CUpnpFileTransferBaseTask::ResourceAllocator()
       
   119     {
       
   120     __LOG( "[UpnpCommand]\t CUpnpFileTransferBaseTask::ResourceAllocator" );
       
   121  
       
   122     return iResourceAllocator;
       
   123     }
       
   124 
       
   125     
       
   126 // --------------------------------------------------------------------------
       
   127 // CUpnpFileTransferBaseTask::ExecuteFileTransferL
       
   128 // Executes the task.
       
   129 // --------------------------------------------------------------------------
       
   130 //
       
   131 void CUpnpFileTransferBaseTask::ExecuteFileTransferL( TBool aRemoveFiles, 
       
   132     TInt& aTransferredFiles)
       
   133     {
       
   134     __LOG( "[UpnpCommand]\t CUpnpFileTransferBaseTask::ExecuteL" );
       
   135 
       
   136 
       
   137     // Leave if command has not been allocated yet
       
   138     if( !FilePipe() ||
       
   139         !CommandParameters() ||
       
   140         !NoteHandler() ||
       
   141         !iBrowsingSession ||
       
   142         !iResourceAllocator )
       
   143         {
       
   144         __LOG( "[UpnpCommand]\t CUpnpFileTransferBaseTask::\
       
   145 ExecuteFileTransferL resources not allocated, leave" );
       
   146         User::Leave( KErrNotReady );
       
   147         }
       
   148 
       
   149     // reset drm-note calculator ( note is shown once per operation )
       
   150     NoteHandler()->ResetDrmNoteCount();
       
   151     
       
   152     TInt status = KErrNone;
       
   153     TInt origFileCount = FilePipe()->Count();;    
       
   154     TInt failedFileCount = 0;   
       
   155 
       
   156     // Initialise and execute the UpnpFileTransferEngine
       
   157     CUpnpFileTransferEngine* ftEngine = NULL;
       
   158     ftEngine = CUpnpFileTransferEngine::NewL( iBrowsingSession );
       
   159     if( status == KErrNone &&
       
   160         ftEngine )
       
   161         {
       
   162         CleanupStack::PushL( ftEngine );
       
   163         
       
   164         // move operation
       
   165         if( aRemoveFiles )
       
   166             {
       
   167             __LOG( "[UpnpCommand]\t CUpnpFileTransferBaseTask, move files" );
       
   168             ftEngine->MoveLocalFilesToRemoteServerL(
       
   169                  &FilePipe()->AsArray() );
       
   170             }
       
   171         // copy operation
       
   172         else
       
   173             {  
       
   174             // Read the collection name (if any)
       
   175             const TDesC& collectionName =
       
   176                 CommandParameters()->Get( UpnpCommand::EParamCollectionName );
       
   177 
       
   178             // If collection name is not set, the operation is file copy
       
   179             if( collectionName != KNullDesC )
       
   180                 {
       
   181                 __LOG( "[UpnpCommand]\t CUpnpFileTransferBaseTask,\
       
   182  copy playlist" );
       
   183                 ftEngine->CopyLocalPlaylistToRemoteServerL(
       
   184                               collectionName, &FilePipe()->AsArray() );
       
   185                 }
       
   186             // If collection name is set, the operation is playlist copy
       
   187             else
       
   188                 {
       
   189                 __LOG( "[UpnpCommand]\t CUpnpFileTransferBaseTask,\
       
   190  copy files" );
       
   191                 ftEngine->CopyLocalFilesToRemoteServerL(
       
   192                               &FilePipe()->AsArray() );
       
   193                 }
       
   194             }
       
   195 
       
   196         // Clean up
       
   197         CleanupStack::PopAndDestroy( ftEngine );
       
   198         ftEngine = NULL;
       
   199         }
       
   200 
       
   201     // calculate number of transferred files based on original file count and
       
   202     // items remaining in filepipe (those were NOT transferred)
       
   203     failedFileCount = FilePipe()->Count();
       
   204     aTransferredFiles = origFileCount - failedFileCount;
       
   205 
       
   206     // check if connection was lost during transfer
       
   207     if( iState != EStateConnected )
       
   208         {
       
   209         status = KErrDisconnected;
       
   210         }
       
   211 
       
   212     __LOG1( "[UpnpCommand]\t CUpnpFileTransferBaseTask::\
       
   213 ExecuteL done status %d", status );
       
   214     
       
   215     // If there was an error, leave
       
   216     if( status != KErrNone )
       
   217         {
       
   218         User::Leave( status );
       
   219         }
       
   220     }
       
   221 
       
   222 // --------------------------------------------------------------------------
       
   223 // CUpnpFileTransferBaseTask::MediaServerDisappeared
       
   224 // Notifies that the Media Renderer we have a session with has disappeared.
       
   225 // --------------------------------------------------------------------------
       
   226 //
       
   227 void CUpnpFileTransferBaseTask::MediaServerDisappeared( 
       
   228     TUPnPDeviceDisconnectedReason aReason )
       
   229     {
       
   230     __LOG1( "[UpnpCommand]\t CUpnpFileTransferBaseTask::\
       
   231 MediaServerDisappeared aReason %d", aReason );
       
   232 
       
   233     // need to know if wlan or server was lost
       
   234     if( aReason == MUPnPAVSessionObserverBase::EWLANLost )
       
   235         {
       
   236         iState = EStateWLANLost;
       
   237         }
       
   238     else
       
   239         {
       
   240         iState = EStateMSLost;
       
   241         }
       
   242     
       
   243     }
       
   244 
       
   245 // --------------------------------------------------------------------------
       
   246 // CUpnpFileTransferBaseTask::IsWlanActive
       
   247 // Returns connection state
       
   248 // --------------------------------------------------------------------------
       
   249 //
       
   250 TBool CUpnpFileTransferBaseTask::IsWlanActive()
       
   251     {
       
   252     __LOG( "[UpnpCommand]\t CUpnpFileTransferBaseTask::IsWlanActive" );
       
   253 
       
   254     return (iState != EStateWLANLost);
       
   255     }
       
   256 
       
   257 // End of File