upnpframework/upnpcommand/src/upnpcommandimplementation.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 CUpnpCommandImplementation class.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 // system
       
    21 #include <centralrepository.h>
       
    22 
       
    23 // upnpframework / avcontroller helper api
       
    24 #include "upnpfileutility.h"                    // IsFileProtectedL()
       
    25 
       
    26 // upnpframework / command api
       
    27 #include "upnpcommand.h"                        // CUpnpCommand
       
    28 #include "upnpcommandcallback.h"                // MUpnpCommandCallback
       
    29 
       
    30 // command internal
       
    31 #include "upnpcommandimplementation.h"
       
    32 #include "upnpfilepipe.h"
       
    33 #include "upnpcommandparameters.h"
       
    34 #include "upnpnotehandler.h"
       
    35 #include "upnpshowtask.h"
       
    36 #include "upnpcopytask.h"
       
    37 #include "upnpmovetask.h"
       
    38 #include "upnpbrowsetask.h"
       
    39 #include "upnprunsetuptask.h"
       
    40 
       
    41 _LIT( KComponentLogfile, "upnpcommand.log");
       
    42 #include "upnplog.h"
       
    43 
       
    44 // CONSTANTS
       
    45 const TUid KCRUidUPnPApplication = {0x20009cae};
       
    46 const TUint32 KUPnPAppAccessPointSetting = 0x00000001;
       
    47 
       
    48 
       
    49 // --------------------------------------------------------------------------
       
    50 // CUpnpCommandImplementation::NewL
       
    51 // Creates an instance of the implementation.
       
    52 // --------------------------------------------------------------------------
       
    53 //
       
    54 CUpnpCommandImplementation* CUpnpCommandImplementation::NewL()
       
    55     {
       
    56     __LOG( "[UpnpCommand]\t CUpnpCommandImplementation::NewL" );
       
    57     
       
    58     CUpnpCommandImplementation* self = NULL;
       
    59     self = new (ELeave) CUpnpCommandImplementation();
       
    60     CleanupStack::PushL( self );
       
    61     self->ConstructL();
       
    62     CleanupStack::Pop( self );
       
    63     return self;
       
    64     }
       
    65 
       
    66 // --------------------------------------------------------------------------
       
    67 // CUpnpCommandImplementation::CUpnpCommandImplementation
       
    68 // First phase construction.
       
    69 // --------------------------------------------------------------------------
       
    70 //
       
    71 CUpnpCommandImplementation::CUpnpCommandImplementation()
       
    72     {
       
    73     __LOG( "[UpnpCommand]\t CUpnpCommandImplementation::Constructor" );
       
    74 
       
    75     // Set command ID to unknown by default. Client is forced to set this
       
    76     // (inline code in the NewL method of CUpnpCommand interface class).
       
    77     iCommandId = UpnpCommand::ECommandUndefined;
       
    78 
       
    79     // By default there is no task
       
    80     iTask = NULL;
       
    81 
       
    82     // By default the state is IDLE (resources not allocated nor executing)
       
    83     iState = UpnpCommand::EStateIdle;
       
    84 
       
    85     // DRM note will be shown only once
       
    86     iDrmNoteShown = EFalse;
       
    87     }
       
    88 
       
    89 // --------------------------------------------------------------------------
       
    90 // CUpnpCommandImplementation::ConstructL
       
    91 // Second phase construction.
       
    92 // --------------------------------------------------------------------------
       
    93 //
       
    94 void CUpnpCommandImplementation::ConstructL()
       
    95     {
       
    96     __LOG( "[UpnpCommand]\t CUpnpCommandImplementation::ConstructL" );
       
    97 
       
    98     iFilePipe = CUpnpFilePipe::NewL();
       
    99     iParameters = CUpnpCommandParameters::NewL();
       
   100     iNoteHandler = CUpnpNoteHandler::NewL();
       
   101     }
       
   102 
       
   103 // --------------------------------------------------------------------------
       
   104 // Destructor.
       
   105 // --------------------------------------------------------------------------
       
   106 //
       
   107 CUpnpCommandImplementation::~CUpnpCommandImplementation()
       
   108     {
       
   109     __LOG( "[UpnpCommand]\t CUpnpCommandImplementation::Destructor" );
       
   110 
       
   111     delete iTask;
       
   112     iTask = NULL;
       
   113     delete iFilePipe;
       
   114     iFilePipe = NULL;
       
   115     delete iParameters;
       
   116     iParameters = NULL;
       
   117     delete iNoteHandler;
       
   118     iNoteHandler = NULL;
       
   119     }
       
   120 
       
   121 // --------------------------------------------------------------------------
       
   122 // CUpnpCommandImplementation::SetCommandIdL
       
   123 // Sets the command ID.
       
   124 // --------------------------------------------------------------------------
       
   125 //
       
   126 void CUpnpCommandImplementation::SetCommandIdL(
       
   127     UpnpCommand::TUpnpCommandId aCommandId )
       
   128     {
       
   129     __LOG1( "[UpnpCommand]\t CUpnpCommandImplementation::SetCommandIdL %d",
       
   130         TInt( aCommandId ) );
       
   131 
       
   132     // command ID can only be set ONCE !
       
   133     __ASSERTD( iCommandId == UpnpCommand::ECommandUndefined,
       
   134         __FILE__, __LINE__ );
       
   135     if( iCommandId != UpnpCommand::ECommandUndefined )
       
   136         {
       
   137         User::Leave( KErrServerBusy );
       
   138         }
       
   139 
       
   140     iCommandId = aCommandId;
       
   141 
       
   142     // If the command is to Show images & video, re-create the file pipe.
       
   143     // In this case we are using a file pipe of a limited size (1)
       
   144     if( iCommandId == UpnpCommand::ECommandShow )
       
   145         {
       
   146         delete iFilePipe;
       
   147         iFilePipe = NULL; // In case the following method leaves
       
   148         iFilePipe = CUpnpFilePipe::NewL( 1 );
       
   149         }
       
   150     }
       
   151 
       
   152 // --------------------------------------------------------------------------
       
   153 // CUpnpCommandImplementation::SetObserver
       
   154 // Sets the callback interface
       
   155 // --------------------------------------------------------------------------
       
   156 //
       
   157 void CUpnpCommandImplementation::SetObserver(
       
   158     MUpnpCommandCallback* aCallback )
       
   159     {
       
   160     __LOG( "[UpnpCommand]\t CUpnpCommandImplementation::SetObserver" );
       
   161 
       
   162     // Set the observer
       
   163     iParameters->SetObserver( aCallback );
       
   164     }
       
   165 
       
   166 // --------------------------------------------------------------------------
       
   167 // CUpnpCommandImplementation::IsAvailableL
       
   168 // Checks if a command is available for execution
       
   169 // --------------------------------------------------------------------------
       
   170 //
       
   171 TBool CUpnpCommandImplementation::IsAvailableL()
       
   172     {
       
   173     return IsAvailableL( iCommandId );
       
   174     }
       
   175 
       
   176 // --------------------------------------------------------------------------
       
   177 // CUpnpCommandImplementation::IsAvailableL
       
   178 // Checks if a command is available for execution
       
   179 // --------------------------------------------------------------------------
       
   180 //
       
   181 TBool CUpnpCommandImplementation::IsAvailableL(
       
   182     UpnpCommand::TUpnpCommandId aCommandId )
       
   183     {
       
   184     switch( aCommandId )
       
   185         {
       
   186         case UpnpCommand::ECommandShow: // flow through
       
   187         case UpnpCommand::ECommandCopy: // flow through
       
   188         case UpnpCommand::ECommandMove: // flow through
       
   189         case UpnpCommand::ECommandBrowse:
       
   190             {
       
   191             // available if upnp is configured
       
   192             return IsUpnpConfiguredL();
       
   193             }
       
   194         case UpnpCommand::ECommandSetup:
       
   195             {
       
   196             // setup is always available
       
   197             return ETrue;
       
   198             }
       
   199         default:
       
   200             __PANICD( __FILE__, __LINE__ );
       
   201         }
       
   202 
       
   203     return EFalse;
       
   204     }
       
   205 
       
   206 // --------------------------------------------------------------------------
       
   207 // CUpnpCommandImplementation::AllocateResourcesL
       
   208 // Allocates the Upnp Framework resources.
       
   209 // --------------------------------------------------------------------------
       
   210 //
       
   211 void CUpnpCommandImplementation::AllocateResourcesL()
       
   212     {
       
   213     __LOG( "[UpnpCommand]\t CUpnpCommandImplementation::AllocateResourcesL" );
       
   214 
       
   215     // Leave if a task is already going (resources allocated)
       
   216     if( iTask )
       
   217         {
       
   218         User::Leave( KErrAlreadyExists );
       
   219         }
       
   220 
       
   221     // Instantiate a task according to the command ID
       
   222     if( iCommandId == UpnpCommand::ECommandShow )
       
   223         {
       
   224         iTask = CUpnpShowTask::NewL();
       
   225         }
       
   226     else if( iCommandId == UpnpCommand::ECommandCopy )
       
   227         {
       
   228         iTask = CUpnpCopyTask::NewL();
       
   229         }
       
   230     else if( iCommandId == UpnpCommand::ECommandMove )
       
   231         {
       
   232         iTask = CUpnpMoveTask::NewL();
       
   233         }
       
   234     else if( iCommandId == UpnpCommand::ECommandBrowse )
       
   235         {
       
   236         iTask = CUpnpBrowseTask::NewL();
       
   237         }
       
   238     else if( iCommandId == UpnpCommand::ECommandSetup )
       
   239         {
       
   240         iTask = CUpnpRunSetupTask::NewL();
       
   241         }
       
   242     else
       
   243         {
       
   244         __PANICD( __FILE__, __LINE__ );
       
   245         User::Leave( KErrNotSupported );
       
   246         }
       
   247 
       
   248     // Set the pointers for the task (using base class CUpnpTask methods)
       
   249     iTask->SetTaskHandlerL( this );
       
   250     iTask->SetCommandParametersL( iParameters );
       
   251     iTask->SetFilePipeL( iFilePipe );
       
   252     iTask->SetNoteHandlerL( iNoteHandler );
       
   253 
       
   254     // Allocate the resources
       
   255     TRAPD( allocateError, iTask->AllocateResourcesL() );
       
   256     
       
   257     // If allocating resources failed, delete the task and forward the
       
   258     // leave code.
       
   259     if( allocateError != KErrNone )
       
   260         {
       
   261         __LOG1( "[UpnpCommand]\t CUpnpCommandImplementation::AllocateResourcesL \
       
   262 failed %d", allocateError );
       
   263         // show note only if operation was not cancelled by user
       
   264         if( allocateError != KErrCancel )
       
   265             {
       
   266             TRAP_IGNORE( iNoteHandler->ShowConnectionLostNoteL() );
       
   267             }
       
   268             
       
   269         delete iTask;
       
   270         iTask = NULL;
       
   271         iState = UpnpCommand::EStateIdle;
       
   272         User::Leave( allocateError );
       
   273         }
       
   274 
       
   275     // Update the state
       
   276     iState = UpnpCommand::EStateAllocated;
       
   277     }
       
   278 
       
   279 // --------------------------------------------------------------------------
       
   280 // CUpnpCommandImplementation::ReleaseResources
       
   281 // Releases the Upnp Framework resources.
       
   282 // --------------------------------------------------------------------------
       
   283 //
       
   284 void CUpnpCommandImplementation::ReleaseResources()
       
   285     {
       
   286     __LOG( "[UpnpCommand]\t CUpnpCommandImplementation::ReleaseResources" );
       
   287 
       
   288     // Destroy the task
       
   289     if( iTask )
       
   290         {
       
   291         delete iTask;
       
   292         iTask = NULL;
       
   293         }
       
   294 
       
   295     // Update the state
       
   296     iState = UpnpCommand::EStateIdle;
       
   297     }
       
   298 
       
   299 // --------------------------------------------------------------------------
       
   300 // CUpnpCommandImplementation::ExecuteL
       
   301 // Executes the command.
       
   302 // --------------------------------------------------------------------------
       
   303 //
       
   304 void CUpnpCommandImplementation::ExecuteL()
       
   305     {
       
   306     __LOG( "[UpnpCommand]\t CUpnpCommandImplementation::ExecuteL" );
       
   307 
       
   308     // Allocates Upnp Fw resources if not yet allocated
       
   309     if( !iTask )
       
   310         {
       
   311 	    __LOG( "[UpnpCommand]\t CUpnpCommandImplementation::ExecuteL task deleted, leave" );
       
   312         User::Leave( KErrNotReady );
       
   313         }
       
   314 
       
   315     // Update the state
       
   316     iState = UpnpCommand::EStateExecuting;
       
   317 
       
   318     // Execute the task
       
   319     TInt status = KErrNone;
       
   320     TRAP( status, iTask->ExecuteL() );
       
   321 
       
   322     // Update the state
       
   323     iState = UpnpCommand::EStateAllocated;
       
   324 
       
   325     // If operation failed, leave
       
   326     if( status != KErrNone )
       
   327         {
       
   328         User::Leave( status );
       
   329         }
       
   330     }
       
   331 
       
   332 // --------------------------------------------------------------------------
       
   333 // CUpnpCommandImplementation::SetParameterL
       
   334 // Set a parameter.
       
   335 // --------------------------------------------------------------------------
       
   336 //
       
   337 void CUpnpCommandImplementation::SetParameterL(
       
   338     UpnpCommand::TUpnpParameterType aParamType,
       
   339     const TDesC& aParamValue )
       
   340     {
       
   341     __LOG( "[UpnpCommand]\t CUpnpCommandImplementation::SetParameterL" );
       
   342     __ASSERTD( iParameters!=0, __FILE__, __LINE__ );
       
   343 
       
   344     iParameters->SetL( aParamType, aParamValue );
       
   345     }
       
   346 
       
   347 // --------------------------------------------------------------------------
       
   348 // CUpnpCommandImplementation::Parameter
       
   349 // --------------------------------------------------------------------------
       
   350 //
       
   351 const TDesC& CUpnpCommandImplementation::Parameter(
       
   352     UpnpCommand::TUpnpParameterType aParamType )
       
   353     {
       
   354     __ASSERTD( iParameters!=0, __FILE__, __LINE__ );
       
   355     return iParameters->Get( aParamType );
       
   356     }
       
   357 
       
   358 // --------------------------------------------------------------------------
       
   359 // CUpnpCommandImplementation::ResetParameters
       
   360 // Resets parameteres.
       
   361 // --------------------------------------------------------------------------
       
   362 //
       
   363 void CUpnpCommandImplementation::ResetParameters()
       
   364     {
       
   365     __LOG( "[UpnpCommand]\t CUpnpCommandImplementation::ResetParameters" );
       
   366 
       
   367     // Reset parameters
       
   368     iParameters->Reset();
       
   369     }
       
   370 
       
   371 // --------------------------------------------------------------------------
       
   372 // CUpnpCommandImplementation::PushFileL
       
   373 // Pushes one file into the file pipe.
       
   374 // --------------------------------------------------------------------------
       
   375 //
       
   376 void CUpnpCommandImplementation::PushFileL( const TDesC& aFilename )
       
   377     {
       
   378     __LOG( "[UpnpCommand]\t CUpnpCommandImplementation::PushFileL" );
       
   379         
       
   380     // Push it in the file pipe
       
   381     iFilePipe->PushL( aFilename );
       
   382     }
       
   383 
       
   384 // --------------------------------------------------------------------------
       
   385 // CUpnpCommandImplementation::CountFiles
       
   386 // --------------------------------------------------------------------------
       
   387 //
       
   388 TInt CUpnpCommandImplementation::FileCount()
       
   389     {
       
   390     return iFilePipe->Count();
       
   391     }
       
   392 
       
   393 // --------------------------------------------------------------------------
       
   394 // CUpnpCommandImplementation::File
       
   395 // Pops oldest file from the file pipe.
       
   396 // --------------------------------------------------------------------------
       
   397 //
       
   398 const TDesC& CUpnpCommandImplementation::File( TInt aIndex )
       
   399     {
       
   400     return iFilePipe->FileAt( aIndex );
       
   401     }
       
   402 
       
   403 
       
   404 // --------------------------------------------------------------------------
       
   405 // CUpnpCommandImplementation::ResetFiles
       
   406 // Resets files.
       
   407 // --------------------------------------------------------------------------
       
   408 //
       
   409 void CUpnpCommandImplementation::ResetFiles()
       
   410     {
       
   411     __LOG( "[UpnpCommand]\t CUpnpCommandImplementation::ResetFiles" );
       
   412 
       
   413     // Reset files
       
   414     iFilePipe->Reset();
       
   415     }
       
   416 
       
   417 // --------------------------------------------------------------------------
       
   418 // CUpnpCommandImplementation::State
       
   419 // Returns the state of the command.
       
   420 // --------------------------------------------------------------------------
       
   421 //
       
   422 UpnpCommand::TUpnpCommandState CUpnpCommandImplementation::State()
       
   423     {
       
   424     __LOG( "[UpnpCommand]\t CUpnpCommandImplementation::State" );
       
   425 
       
   426     return iState;
       
   427     }
       
   428 
       
   429 // --------------------------------------------------------------------------
       
   430 // CUpnpCommandImplementation::DestroyTask
       
   431 // Destroys the ongoing task.
       
   432 // --------------------------------------------------------------------------
       
   433 //
       
   434 void CUpnpCommandImplementation::DestroyTask()
       
   435     {
       
   436     __LOG( "[UpnpCommand]\t CUpnpCommandImplementation::DestroyTask" );
       
   437 
       
   438     // Release resources (delete the ongoing UpnpTask).
       
   439     ReleaseResources();
       
   440     }
       
   441 
       
   442 // --------------------------------------------------------------------------
       
   443 // CUpnpCommandImplementation::IsUpnpConfigured
       
   444 // --------------------------------------------------------------------------
       
   445 //
       
   446 TBool CUpnpCommandImplementation::IsUpnpConfiguredL()
       
   447     {
       
   448     TBool returnValue = EFalse;
       
   449     // Access the Upnp Fw central repository key
       
   450     CRepository* repository = NULL;
       
   451     TRAPD( error, repository = CRepository::NewL( KCRUidUPnPApplication ) );
       
   452 
       
   453     if ( error == KErrNone )
       
   454         {
       
   455         // Read the IAP setting
       
   456         TInt iapDefined = KErrNotFound;
       
   457         TInt getError = repository->Get( KUPnPAppAccessPointSetting,
       
   458                                          iapDefined );
       
   459     
       
   460         delete repository;
       
   461     
       
   462         // Define the return value (min. valid IAP Id value is 1,
       
   463         // 0=None selected)
       
   464         if( getError == KErrNone &&
       
   465             iapDefined > 0 )
       
   466             {
       
   467             returnValue = ETrue;
       
   468             }
       
   469         }
       
   470     
       
   471     return returnValue;
       
   472     }
       
   473 
       
   474 // End of File