ncdengine/provider/server/src/ncddownloadoperationimpl.cpp
changeset 4 32704c33136d
equal deleted inserted replaced
-1:000000000000 4:32704c33136d
       
     1 /*
       
     2 * Copyright (c) 2006 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:  
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "ncddownloadoperationimpl.h"
       
    20 
       
    21 #include <s32mem.h>
       
    22 #include <f32file.h>
       
    23 
       
    24 #include "catalogsbasemessage.h"
       
    25 #include "ncdnodemanager.h"
       
    26 #include "ncdfilehandler.h"
       
    27 #include "ncdskindownloadhandler.h"
       
    28 #include "ncdicondownloadhandler.h"
       
    29 #include "ncdpreviewdownloadhandler.h"
       
    30 #include "ncdfiledownloadhandler.h"
       
    31 #include "ncdscreenshotdownloadhandler.h"
       
    32 #include "ncdfilestoragefilehandler.h"
       
    33 #include "ncdnodeimpl.h"
       
    34 #include "ncddatabasefilehandler.h"
       
    35 #include "ncdnodeidentifier.h"
       
    36 #include "ncdsessionhandler.h"
       
    37 #include "ncdhttpheaders.h"
       
    38 #include "catalogsutils.h"
       
    39 #include "ncdconfigurationmanager.h"
       
    40 #include "catalogscontext.h"
       
    41 #include "ncdproviderdefines.h"
       
    42 #include "ncdnodeclassids.h"
       
    43 #include "ncdnodelink.h"
       
    44 #include "catalogsaccesspointmanager.h"
       
    45 #include "ncdoperationremovehandler.h"
       
    46 #include "ncdproviderutils.h"
       
    47 #include "ncdnodemetadataimpl.h"
       
    48 #include "catalogshttpincludes.h"
       
    49 #include "ncdgeneralmanager.h"
       
    50 
       
    51 #include "catalogsdebug.h"
       
    52 
       
    53 // ======== MEMBER FUNCTIONS ========
       
    54 
       
    55 // ---------------------------------------------------------------------------
       
    56 // NewL
       
    57 // ---------------------------------------------------------------------------
       
    58 //
       
    59 CNcdDownloadOperation* CNcdDownloadOperation::NewL( 
       
    60     MNcdOperationRemoveHandler& aRemoveHandler, 
       
    61     TNcdDownloadDataType aType, 
       
    62     const CNcdNodeIdentifier& aNodeId,
       
    63     CNcdGeneralManager& aGeneralManager,
       
    64     MCatalogsHttpSession& aHttpSession,
       
    65     MNcdSessionHandler* aSessionHandler,
       
    66     MNcdStorageClient* aStorageClient,
       
    67     const TUid& aClientUid,
       
    68     TInt aDownloadIndex,
       
    69     MCatalogsSession& aSession )
       
    70     {
       
    71     CNcdDownloadOperation* self = new( ELeave ) CNcdDownloadOperation( 
       
    72         aRemoveHandler,
       
    73         aGeneralManager, 
       
    74         aHttpSession, 
       
    75         aSessionHandler, 
       
    76         aType, 
       
    77         aClientUid, 
       
    78         aSession );
       
    79     CleanupClosePushL( *self );
       
    80     self->ConstructL( aNodeId, aDownloadIndex, aStorageClient );
       
    81 
       
    82     CleanupStack::Pop();
       
    83     return self;
       
    84     }
       
    85 
       
    86 
       
    87 // ---------------------------------------------------------------------------
       
    88 // Destructor
       
    89 // ---------------------------------------------------------------------------
       
    90 //
       
    91 CNcdDownloadOperation::~CNcdDownloadOperation()
       
    92     {
       
    93     DLTRACEIN( ( "" ) );
       
    94     if ( iDownload ) 
       
    95         {
       
    96         iDownload->Release();
       
    97         iDownload = NULL;
       
    98         }
       
    99         
       
   100     delete iFileHandler;
       
   101     delete iDownloadHandler;
       
   102     
       
   103     delete iSessionId;
       
   104     DLTRACEOUT((""));
       
   105     }
       
   106 
       
   107 
       
   108 // ---------------------------------------------------------------------------
       
   109 // Download config getter
       
   110 // ---------------------------------------------------------------------------
       
   111 //
       
   112 MCatalogsHttpConfig& CNcdDownloadOperation::Config()
       
   113     {
       
   114     DASSERT( iDownload );
       
   115     return iDownload->Config();
       
   116     }
       
   117 
       
   118 // ---------------------------------------------------------------------------
       
   119 // Node Id getter
       
   120 // ---------------------------------------------------------------------------
       
   121 //
       
   122 const CNcdNodeIdentifier& CNcdDownloadOperation::NodeIdentifier() const
       
   123     {
       
   124     return iDownloadHandler->NodeId();
       
   125     }
       
   126 
       
   127 // ---------------------------------------------------------------------------
       
   128 // Cancel
       
   129 // ---------------------------------------------------------------------------
       
   130 //
       
   131 void CNcdDownloadOperation::Cancel() 
       
   132     {    
       
   133     DLTRACEIN(( "" ));
       
   134     if ( iDownload ) 
       
   135         {
       
   136         // Cancel also releases the dl        
       
   137         iDownload->Cancel();
       
   138         iDownload = NULL;
       
   139         }
       
   140     DLTRACEOUT(( "" ));
       
   141     }
       
   142 
       
   143 
       
   144 // ---------------------------------------------------------------------------
       
   145 // ReceiveMessage
       
   146 // ---------------------------------------------------------------------------
       
   147 //
       
   148 void CNcdDownloadOperation::ReceiveMessage( 
       
   149     MCatalogsBaseMessage* aMessage,
       
   150     TInt aFunctionNumber )
       
   151     {
       
   152     DLTRACEIN(( "Function: %d", aFunctionNumber ));
       
   153     TNcdOperationMessageCompletionId completionId;
       
   154     TInt err = KErrNone;
       
   155         
       
   156     // Response to pause and resume messages, other messages are handled
       
   157     // by the base class
       
   158     switch ( aFunctionNumber )
       
   159         {
       
   160         // GetData which in fact is pausable check
       
   161         case ENCDOperationFunctionGetData: 
       
   162             {
       
   163             DLTRACE(( "ENCDOperationFunctionGetData "));
       
   164             // Only error will come from CompleteMessageL
       
   165             TRAP_IGNORE( GetPausableStateL( *aMessage ) );
       
   166             return;
       
   167             }
       
   168             
       
   169         // Pause download if it's pausable
       
   170         case ENCDOperationFunctionPause:
       
   171             {
       
   172             DLTRACE( ( "ENCDOperationFunctionPause" ) );
       
   173             err = iDownload->Pause();              
       
   174             if ( err == KErrNone ) 
       
   175                 {
       
   176                 iDownloadState = ENcdDownloadPaused;
       
   177                 }
       
   178             completionId = ENCDOperationMessageCompletionPause;
       
   179             break;
       
   180             }
       
   181             
       
   182         
       
   183         // Resume download
       
   184         case ENCDOperationFunctionResume:
       
   185             {
       
   186             DLTRACE( ( "ENCDOperationFunctionResume" ) );
       
   187             
       
   188             if ( iDownload->Progress().iState == ECatalogsHttpOpPaused ) 
       
   189                 {
       
   190                 err = iDownload->Start();                
       
   191                 if ( err == KErrNone ) 
       
   192                     {
       
   193                     iDownloadState = ENcdDownloadInProgress;
       
   194                     }
       
   195                 }
       
   196             completionId = ENCDOperationMessageCompletionResume;
       
   197             break;
       
   198             }
       
   199             
       
   200         default:
       
   201             {
       
   202             DLTRACE(("Calling baseclass"));
       
   203             // Call implementation in the base class
       
   204             CNcdBaseOperation::ReceiveMessage( aMessage, aFunctionNumber );
       
   205             DLTRACEOUT(( "Called baseclass" ));
       
   206             return;
       
   207             }
       
   208         }
       
   209         
       
   210         
       
   211     TCatalogsTransportProgress progress( iDownload->Progress() );
       
   212     
       
   213     iProgress = TNcdSendableProgress( 
       
   214         iCurrentFile,
       
   215         iTotalFileCount, 
       
   216         progress.iProgress,
       
   217         progress.iMaxProgress );
       
   218     
       
   219     // this ensures that the proxy handles pausing correctly
       
   220     iProgress.iState = iDownloadState;
       
   221 
       
   222     if ( !iPendingMessage && 
       
   223           iDownloadState != ENcdDownloadPaused )
       
   224         {
       
   225         DLTRACE(("No pending message, ask for one"));        
       
   226         iProgress.iState = KNcdDownloadContinueMessageRequired;    
       
   227         }
       
   228     
       
   229     // Complete pause/resume message
       
   230     if ( CompleteMessage( aMessage, completionId, iProgress, err ) 
       
   231         != KErrNone ) 
       
   232         {
       
   233         DLTRACE( ( "Complete message failed" ) );
       
   234         }
       
   235         
       
   236     iProgress.iState = iCurrentFile;
       
   237     iProgress.iOperationId = iTotalFileCount;
       
   238     DLTRACEOUT(( "" ));
       
   239     }
       
   240 
       
   241 
       
   242 // ---------------------------------------------------------------------------
       
   243 // Checks if the download matches the given criteria
       
   244 // ---------------------------------------------------------------------------
       
   245 //
       
   246 TBool CNcdDownloadOperation::MatchDownload( 
       
   247     const CNcdNodeIdentifier& aId, 
       
   248     TNcdDownloadDataType& aType, TInt aIndex ) const
       
   249     {
       
   250     DLTRACEIN((""));
       
   251     if ( aType != iType || 
       
   252          aIndex != ( iDownloadHandler->CurrentDownload() - 1 ) ) 
       
   253         {
       
   254         DLTRACEOUT(("Type or index didn't match"));
       
   255         return EFalse;
       
   256         }
       
   257     
       
   258     // Generic file download don't have node nor metadata
       
   259     if ( aType != ENcdGenericFileDownload ) 
       
   260         {
       
   261         DLTRACE(("Not a generic file download"));
       
   262         // Compare metadata id's because temp nodes have different node 
       
   263         // id's but same metadata id's as normal nodes
       
   264         const CNcdNode* node = NULL;    
       
   265         TRAP_IGNORE( node = &iDownloadHandler->NodeL() );
       
   266         
       
   267         if ( node ) 
       
   268             {        
       
   269             const CNcdNodeMetaData* metadata = node->NodeMetaData();
       
   270             if ( metadata && metadata->Identifier().Equals( aId ) )
       
   271                 {
       
   272                 return ETrue;
       
   273                 }
       
   274             }
       
   275         }
       
   276     else // generic file downloads
       
   277         {
       
   278         DLTRACEOUT(("Generic file download"));
       
   279         return aId.Equals( iDownloadHandler->NodeId() );
       
   280         }
       
   281     return EFalse;
       
   282     }
       
   283 
       
   284 
       
   285 // ---------------------------------------------------------------------------
       
   286 // HandleHttpEvent
       
   287 // ---------------------------------------------------------------------------
       
   288 //
       
   289 void CNcdDownloadOperation::HandleHttpEventL( 
       
   290     MCatalogsHttpOperation& aOperation, 
       
   291     TCatalogsHttpEvent aEvent )
       
   292     {
       
   293     DLTRACEIN( ( "" ) );
       
   294     (void) aOperation; // suppresses compiler warning
       
   295     
       
   296     DASSERT( &aOperation == iDownload );
       
   297     DASSERT( aOperation.OperationType() == ECatalogsHttpDownload );
       
   298 
       
   299         // minimizing stack usage
       
   300         {        
       
   301         TCatalogsTransportProgress progress( iDownload->Progress() );
       
   302         
       
   303         iProgress = TNcdSendableProgress( 
       
   304             iCurrentFile,
       
   305             iTotalFileCount, 
       
   306             progress.iProgress,
       
   307             progress.iMaxProgress );
       
   308         }        
       
   309     
       
   310     switch( aEvent.iOperationState ) 
       
   311         {
       
   312         // Handle completed operation
       
   313         case ECatalogsHttpOpCompleted:
       
   314             {
       
   315             iCurrentFile++;
       
   316             DLTRACE(("ECatalogsHttpOpCompleted"));
       
   317             if ( iPendingMessage ) 
       
   318                 {
       
   319                 DLTRACE( ( "Completing message" ) );
       
   320                 // Finish the download = move it to destination
       
   321                 TRAPD( err, iDownloadHandler->FinishDownloadL( 
       
   322                     *iDownload, iFileHandler ) );
       
   323                 
       
   324                 // Release old download                
       
   325                 iDownload->Release();
       
   326                 iDownload = NULL;
       
   327                 
       
   328                 if ( iDownloadHandler->CurrentDownload() == 
       
   329                      iDownloadHandler->DownloadCount() )
       
   330                     {   
       
   331                     DLTRACE(("Download(s) finished"));                 
       
   332                     CompleteMessage( iPendingMessage,
       
   333                         ENCDOperationMessageCompletionComplete,
       
   334                         iProgress, err );
       
   335                     iDownloadState = ENcdDownloadComplete;
       
   336                     iOperationState = EStateComplete;
       
   337                     }
       
   338                 else 
       
   339                     {
       
   340                     DLTRACE(("More downloads"));
       
   341 
       
   342                     
       
   343                     // Get the next download
       
   344                     TRAP( err, iDownload = iDownloadHandler->DownloadL() );
       
   345                     iError = err;
       
   346                     if ( err != KErrNone ) 
       
   347                         {
       
   348                         /**
       
   349                          * @ Is this correct?
       
   350                          */
       
   351                         return;
       
   352                         }
       
   353                     
       
   354                     // Set observer
       
   355                     iDownload->Config().SetObserver( this );
       
   356                     UpdateHeadersL( iPendingMessage->Session().Context() );
       
   357                     err = iDownload->Start( MCatalogsHttpOperation::EAutomaticResume );
       
   358                     if ( err >= 0 ) 
       
   359                         {
       
   360                         iDownloadState = ENcdDownloadInProgress;
       
   361                         err = KErrNone;
       
   362                         iOperationState = EStateRunning;
       
   363                         TCatalogsTransportProgress progress( 
       
   364                             iDownload->Progress() );
       
   365                         
       
   366                         iProgress = TNcdSendableProgress( 
       
   367                             iCurrentFile,
       
   368                             iTotalFileCount, progress.iProgress,                            
       
   369                             progress.iMaxProgress );
       
   370                             
       
   371                         CompleteMessage( iPendingMessage,
       
   372                             ENCDOperationMessageCompletionProgress,
       
   373                             iProgress, KErrNone );
       
   374                         }
       
   375                      else 
       
   376                         {
       
   377                         /** @ Handle error
       
   378                         */
       
   379                         iOperationState = EStateComplete;
       
   380                         iDownloadState = ENcdDownloadComplete;
       
   381 
       
   382                         }
       
   383                     
       
   384                     }                
       
   385                 iPendingMessage = NULL;
       
   386                 }
       
   387             else 
       
   388                 {
       
   389                 iDownloadState = ENcdDownloadComplete;
       
   390                 }
       
   391             break;
       
   392             }
       
   393         
       
   394         // Handle operation in progress
       
   395         case ECatalogsHttpOpInProgress:
       
   396             {
       
   397             iDownloadState = ENcdDownloadInProgress;
       
   398             switch( aEvent.iProgressState ) 
       
   399                 {
       
   400                 case ECatalogsHttpResponseBodyReceived:
       
   401                     {                    
       
   402                     //Notify Proxy about progress                    
       
   403                     /*
       
   404                     if ( iPendingMessage ) 
       
   405                         {            
       
   406                                    
       
   407                         CompleteMessage( iPendingMessage,
       
   408                             ENCDOperationMessageCompletionProgress,
       
   409                             iProgress, KErrNone );
       
   410                         }
       
   411                     */
       
   412                     iOperationState = EStateRunning;
       
   413                     iUnhandledEvent = ETrue;
       
   414                     RunOperation();
       
   415                     break;
       
   416                     }
       
   417                     
       
   418                 default:
       
   419                     {
       
   420                     }
       
   421                 }
       
   422             break;
       
   423             }
       
   424         
       
   425         default:
       
   426             {
       
   427             break;
       
   428             }
       
   429         }
       
   430     DLTRACEOUT((""));
       
   431     }
       
   432     
       
   433     
       
   434 // ---------------------------------------------------------------------------
       
   435 // HandleHttpError
       
   436 // ---------------------------------------------------------------------------
       
   437 //
       
   438 TBool CNcdDownloadOperation::HandleHttpError(
       
   439     MCatalogsHttpOperation& aOperation,
       
   440     TCatalogsHttpError aError )
       
   441     {    
       
   442     DLTRACEIN( ( "Error type: %i, id: %i", aError.iType, aError.iError ) );
       
   443     DASSERT( &aOperation == iDownload );
       
   444     
       
   445     if ( aError.iError == KErrCancel ) 
       
   446         {
       
   447         aOperation.Cancel();
       
   448         }
       
   449     else 
       
   450         {        
       
   451         aOperation.Release();
       
   452         }
       
   453     iDownload = NULL;
       
   454     iError = aError.iError;
       
   455     iDownloadState = ENcdDownloadFailed;
       
   456     if ( iPendingMessage ) 
       
   457         {        
       
   458         
       
   459         CompleteMessage( iPendingMessage,
       
   460             ENCDOperationMessageCompletionError, iProgress, aError.iError );
       
   461         
       
   462         iOperationState = EStateComplete;   
       
   463         }
       
   464     DLTRACEOUT((""));
       
   465     return ETrue;
       
   466     }
       
   467 
       
   468 
       
   469 // ---------------------------------------------------------------------------
       
   470 // RunOperation
       
   471 // ---------------------------------------------------------------------------
       
   472 //
       
   473 TInt CNcdDownloadOperation::RunOperation()
       
   474     {
       
   475     DLTRACEIN(( "Pending message: %X", iPendingMessage ));
       
   476     DASSERT( iDownload );
       
   477     
       
   478     if ( !iPendingMessage ) 
       
   479         {      
       
   480         DLTRACE(("No pending message"));  
       
   481         return KErrNotReady;
       
   482         }
       
   483      
       
   484     TInt err = KErrNone;
       
   485     DLTRACE(("DL state: %i", iDownloadState ));
       
   486 
       
   487     switch( iDownloadState ) 
       
   488         {
       
   489         // Start the download
       
   490         case ENcdDownloadStopped: 
       
   491             {
       
   492             DLTRACE(("ENcdDownloadStopped"));
       
   493             
       
   494             TRAP( err, UpdateHeadersL( iPendingMessage->Session().Context() ));
       
   495             if ( err != KErrNone ) 
       
   496                 {
       
   497                 break;
       
   498                 }
       
   499             
       
   500             DLTRACE(("Starting the download"));
       
   501             err = iDownload->Start();
       
   502             if ( err >= 0 ) 
       
   503                 {
       
   504                 iDownloadState = ENcdDownloadInProgress;
       
   505                 err = KErrNone;
       
   506                 iOperationState = EStateRunning;
       
   507                 TCatalogsTransportProgress progress( iDownload->Progress() );
       
   508                 
       
   509                 // Send progress information
       
   510                 iProgress = TNcdSendableProgress( iCurrentFile,
       
   511                     iTotalFileCount, 
       
   512                     progress.iProgress,
       
   513                     progress.iMaxProgress );
       
   514                 }
       
   515             DLTRACE(("ENcdDownloadStopped done"));
       
   516             break;
       
   517             }
       
   518         
       
   519         case ENcdDownloadInProgress: 
       
   520             {
       
   521             DLTRACE(("ENcdDownloadInProgress"));
       
   522             if ( iUnhandledEvent ) 
       
   523                 {
       
   524                 DLTRACE(("Completing progress message"));
       
   525                 CompleteMessage( iPendingMessage,
       
   526                     ENCDOperationMessageCompletionProgress,
       
   527                     iProgress, KErrNone );
       
   528                 iUnhandledEvent = EFalse;
       
   529                 }
       
   530                 
       
   531             DLTRACE(("ENcdDownloadInProgress done"));
       
   532             break;
       
   533             }
       
   534             
       
   535         case ENcdDownloadComplete:
       
   536             {
       
   537             DLTRACE(("ENcdDownloadComplete"));
       
   538             iUnhandledEvent = EFalse;
       
   539             // Finish the download = move it to destination
       
   540             TRAP( err, iDownloadHandler->FinishDownloadL( *iDownload,
       
   541                 iFileHandler ) );
       
   542             
       
   543             // Check if the download was the last
       
   544             if ( iDownloadHandler->CurrentDownload() == 
       
   545                  iDownloadHandler->DownloadCount() ) 
       
   546                 {
       
   547                 // Send completion message in case didn't have any pending
       
   548                 // messages when the download actually completed
       
   549                 
       
   550                 if ( CompleteMessage( iPendingMessage,
       
   551                     ENCDOperationMessageCompletionComplete, 
       
   552                     iProgress, err ) != KErrNone ) 
       
   553                     {
       
   554                     DLERROR(( "Message completion failed" ));
       
   555                     }
       
   556                     
       
   557                 iOperationState = EStateComplete;
       
   558                 }
       
   559              else 
       
   560                 {
       
   561                 // Release old download
       
   562                 iDownload->Release();
       
   563                 iDownload = NULL;
       
   564                 
       
   565                 // Get the next download
       
   566                 TRAP( err, iDownload = iDownloadHandler->DownloadL() );
       
   567                 if ( err != KErrNone ) 
       
   568                     {
       
   569                     /**
       
   570                      * @ Is this correct?
       
   571                      */
       
   572                     break;
       
   573                     }
       
   574                 
       
   575                 // Set observer
       
   576                 iDownload->Config().SetObserver( this );
       
   577                 TRAP( err, UpdateHeadersL( iPendingMessage->Session().Context() ));
       
   578                 if ( err != KErrNone ) 
       
   579                     {
       
   580                     break;
       
   581                     }
       
   582                 err = iDownload->Start( MCatalogsHttpOperation::EAutomaticResume );
       
   583                 if ( err >= 0 ) 
       
   584                     {
       
   585                     iDownloadState = ENcdDownloadInProgress;
       
   586                     err = KErrNone;
       
   587                     iOperationState = EStateRunning;
       
   588                     TCatalogsTransportProgress progress( iDownload->Progress() );
       
   589                     
       
   590                     // Send progress information
       
   591                     iProgress = TNcdSendableProgress( iCurrentFile,
       
   592                         iTotalFileCount, 
       
   593                         progress.iProgress,
       
   594                         progress.iMaxProgress );
       
   595                         
       
   596                     CompleteMessage( iPendingMessage,
       
   597                         ENCDOperationMessageCompletionProgress, iProgress, iError );
       
   598                     }
       
   599                 
       
   600                 }
       
   601             iPendingMessage = NULL;
       
   602             DLTRACE(("ENcdDownloadComplete done"));
       
   603             break;
       
   604             }
       
   605             
       
   606         case ENcdDownloadFailed:
       
   607             {
       
   608             DLTRACE(("ENcdDownloadFailed"));
       
   609             // Send error message in case didn't have any pending
       
   610             // messages when the download actually failed
       
   611             
       
   612             CompleteMessage( iPendingMessage,
       
   613                 ENCDOperationMessageCompletionError, iProgress,
       
   614                 iError );
       
   615             DLTRACE(("ENcdDownloadFailed done"));
       
   616             break;            
       
   617             }
       
   618             
       
   619         default: 
       
   620             {
       
   621             DLTRACE(("Default"));
       
   622             DASSERT( 0 );
       
   623             }
       
   624         }
       
   625         
       
   626     if ( err != KErrNone )
       
   627         {
       
   628         DLTRACE(("error: %d", err));
       
   629         Cancel();
       
   630         iDownloadState = ENcdDownloadFailed;
       
   631         iError = err;
       
   632         if ( iPendingMessage )
       
   633             {
       
   634             CompleteMessage( iPendingMessage,
       
   635                 ENCDOperationMessageCompletionError, iError );
       
   636             }
       
   637         // call observers
       
   638         CompleteCallback();
       
   639         }
       
   640     DLTRACEOUT(("err: %d", err));
       
   641     return err;
       
   642     }
       
   643 
       
   644 
       
   645 // ---------------------------------------------------------------------------
       
   646 // Initializer
       
   647 // ---------------------------------------------------------------------------
       
   648 //
       
   649 TInt CNcdDownloadOperation::Initialize()
       
   650     {
       
   651     DLTRACEIN( ( "" ) );
       
   652     DASSERT( iDownloadHandler );
       
   653     TRAPD(err,
       
   654         {
       
   655     
       
   656         switch( iType ) 
       
   657             {
       
   658             // read the target filename and path from the message
       
   659             case ENcdSkinDownload: // Flowthrough
       
   660                 {
       
   661                 RCatalogsMessageReader reader;
       
   662                 reader.OpenLC( *iPendingMessage );
       
   663                               
       
   664                 HBufC* filename = NULL;
       
   665                 DLTRACE(("Internalizing filename"));
       
   666                 InternalizeDesL( filename, reader() );
       
   667                                 
       
   668                 CleanupStack::PopAndDestroy( &reader );
       
   669                 
       
   670                 CleanupStack::PushL( filename );
       
   671                 
       
   672                 DLTRACE(( _L("Parsing filename: %S"), filename ));
       
   673                 // Parse the path and set directory and filename
       
   674                 TParsePtrC parse( *filename );
       
   675                 iDownload->Config().SetDirectoryL( parse.DriveAndPath() );
       
   676                 iDownload->Config().SetFilenameL( parse.NameAndExt() );
       
   677                 
       
   678                 CleanupStack::PopAndDestroy( filename );
       
   679                 
       
   680                 // Just check that we're not already below critical level
       
   681                 WouldDiskSpaceGoBelowCriticalLevelL( 
       
   682                     iDownload->Config().Directory(),
       
   683                     CNcdProviderUtils::FileSession(),
       
   684                     0 );
       
   685                 
       
   686                 break;
       
   687                 }
       
   688                 
       
   689             default:
       
   690                 // nothing to initialize for other types
       
   691                 break;
       
   692                 
       
   693             } // switch
       
   694     
       
   695 
       
   696     
       
   697         DLTRACE(("Writing initialize response"));
       
   698         iCurrentFile = 1;
       
   699         iTotalFileCount = iDownloadHandler->DownloadCount();
       
   700         // Write the response
       
   701         RCatalogsBufferWriter writer;
       
   702         writer.OpenLC();
       
   703         writer().WriteInt32L( ENCDOperationMessageCompletionInit );
       
   704         writer().WriteInt32L( iDownloadHandler->DownloadCount() );
       
   705         
       
   706         // write pause-status
       
   707         writer().WriteInt32L( iDownloadState == ENcdDownloadPaused );
       
   708         
       
   709         iPendingMessage->CompleteAndReleaseL( writer.PtrL(), KErrNone );
       
   710         iPendingMessage = NULL;
       
   711         CleanupStack::PopAndDestroy( &writer );
       
   712         });
       
   713         
       
   714     if ( err != KErrNone ) 
       
   715         {
       
   716         DLINFO(("err: %d", err));
       
   717         iPendingMessage->CompleteAndRelease( err );
       
   718         iPendingMessage = NULL;
       
   719         }
       
   720     
       
   721     DLTRACEOUT(( "err: %d", err ));
       
   722     return err;    
       
   723     }
       
   724 
       
   725 
       
   726 // ---------------------------------------------------------------------------
       
   727 // Constructor
       
   728 // ---------------------------------------------------------------------------
       
   729 //
       
   730 CNcdDownloadOperation::CNcdDownloadOperation( 
       
   731     MNcdOperationRemoveHandler& aRemoveHandler,
       
   732     CNcdGeneralManager& aGeneralManager,
       
   733     MCatalogsHttpSession& aHttpSession, 
       
   734     MNcdSessionHandler* aSessionHandler, 
       
   735     TNcdDownloadDataType aType,
       
   736     const TUid& aClientUid,
       
   737     MCatalogsSession& aSession ) : 
       
   738         CNcdBaseOperation( aGeneralManager, &aRemoveHandler, EDownloadOperation,
       
   739             aSession ),
       
   740         iHttpSession( aHttpSession ),
       
   741         iSessionHandler( aSessionHandler ),
       
   742         iConfigurationManager( aGeneralManager.ConfigurationManager() ),
       
   743         iAccessPointManager( aGeneralManager.AccessPointManager() ),
       
   744         iType( aType ),
       
   745         iDownloadState( ENcdDownloadStopped ),
       
   746         iClientUid( aClientUid )
       
   747     {
       
   748     }
       
   749 
       
   750 
       
   751 // ---------------------------------------------------------------------------
       
   752 // ConstructL
       
   753 // ---------------------------------------------------------------------------
       
   754 //
       
   755 void CNcdDownloadOperation::ConstructL( const CNcdNodeIdentifier& aNodeId, 
       
   756     TInt aDownloadIndex,
       
   757     MNcdStorageClient* aStorageClient )
       
   758     {
       
   759     DLTRACEIN(( "DL index: %i", aDownloadIndex ));
       
   760     // Call ConstructL for the base class
       
   761     CNcdBaseOperation::ConstructL();    
       
   762 
       
   763     if ( iSessionHandler && iType != ENcdGenericFileDownload ) 
       
   764         {        
       
   765         // Ignore error, it's possible that there's no session for URI
       
   766         // anymore
       
   767         TRAP_IGNORE( 
       
   768             {
       
   769             // Leaves if the node is not found        
       
   770             CNcdNode& node = iNodeManager->NodeL( aNodeId );                    
       
   771 
       
   772             // Get server URI from the node
       
   773             const TDesC& serverUri = node.NodeLinkL().ServerUri();
       
   774             if ( iSessionHandler->DoesSessionExist( serverUri, 
       
   775                 aNodeId.NodeNameSpace() ) ) 
       
   776                 {        
       
   777                 // Get session ID for the URI
       
   778                 iSessionId = iSessionHandler->Session( serverUri,
       
   779                     aNodeId.NodeNameSpace() ).AllocL();
       
   780                 }
       
   781             });
       
   782         }
       
   783     
       
   784         
       
   785     // Choose the correct handlers for the download
       
   786     switch ( iType ) 
       
   787         {
       
   788         case ENcdSkinDownload : 
       
   789             {
       
   790             DLTRACE(("Skin download"));
       
   791             iFileHandler = NULL;
       
   792             iDownloadHandler = CNcdSkinDownloadHandler::NewL( aNodeId, 
       
   793                 *iNodeManager,
       
   794                 iHttpSession );
       
   795             break;
       
   796             
       
   797             }
       
   798         case ENcdGenericFileDownload :
       
   799             {
       
   800             DLTRACE(("Generic file download"));
       
   801             // No file handler, Transport takes care of moving the file
       
   802             iFileHandler = NULL;
       
   803             iDownloadHandler = CNcdFileDownloadHandler::NewL( aNodeId,
       
   804                 *iNodeManager,
       
   805                 iHttpSession );
       
   806             break;
       
   807             }
       
   808 
       
   809         case ENcdPreviewDownload :
       
   810             {
       
   811             DLTRACE(("Preview download"));
       
   812             DASSERT( aStorageClient );
       
   813             iFileHandler = CNcdFileStorageFileHandler::NewL( *aStorageClient );
       
   814             iDownloadHandler = CNcdPreviewDownloadHandler::NewL( aNodeId,
       
   815                 *iNodeManager,
       
   816                 iHttpSession );
       
   817             break;
       
   818             }
       
   819 
       
   820         case ENcdIconDownload :
       
   821             {
       
   822             DLTRACE(("Icon download"));
       
   823             DASSERT( aStorageClient );
       
   824             iFileHandler = CNcdDatabaseFileHandler::NewL( *aStorageClient,
       
   825                 NcdNodeClassIds::ENcdIconData );            
       
   826             iDownloadHandler = CNcdIconDownloadHandler::NewL( aNodeId,
       
   827                 *iNodeManager,
       
   828                 iHttpSession );
       
   829             break;
       
   830             }
       
   831 
       
   832         case ENcdScreenshotDownload :
       
   833             {
       
   834             DLTRACE(("Screenshot download"));
       
   835             DASSERT( aStorageClient );
       
   836             iFileHandler = CNcdDatabaseFileHandler::NewL( *aStorageClient,
       
   837                 NcdNodeClassIds::ENcdScreenshotData );
       
   838             iDownloadHandler = CNcdScreenshotDownloadHandler::NewL( aNodeId,
       
   839                 *iNodeManager,
       
   840                 iHttpSession );
       
   841             break;
       
   842             }
       
   843         
       
   844         default:
       
   845             {
       
   846             DASSERT( 0 );
       
   847             }
       
   848         }
       
   849     
       
   850     // Start downloading from the correct index
       
   851     iDownloadHandler->SetCurrentDownload( aDownloadIndex );
       
   852     
       
   853     // Create the download
       
   854     iDownload = iDownloadHandler->DownloadL();
       
   855     iDownload->Config().SetObserver( this );
       
   856     
       
   857     DLTRACE(("Check that we're not below critical level on disk space"));
       
   858     
       
   859     // Target path for skin download is set in Initialize so
       
   860     // we can't check it yet
       
   861     if ( iType != ENcdSkinDownload )
       
   862         {        
       
   863         // Just check that we're not already below critical level
       
   864         WouldDiskSpaceGoBelowCriticalLevelL( 
       
   865             iDownload->Config().Directory(),
       
   866             CNcdProviderUtils::FileSession(),
       
   867             0 );
       
   868         }
       
   869     
       
   870     if ( iType == ENcdSkinDownload || iType == ENcdPreviewDownload || 
       
   871          iType == ENcdIconDownload || iType == ENcdScreenshotDownload ) 
       
   872         {
       
   873         // verify the node exists, otherwise call to AccessPointIdL won't work correctly
       
   874         DASSERT( iNodeManager->NodePtrL( aNodeId ) != NULL );        
       
   875         TUint32 apId;
       
   876         TInt error = iAccessPointManager.AccessPointIdL(
       
   877             aNodeId, MCatalogsAccessPointManager::EBrowse, iClientUid, apId);
       
   878         if ( error == KErrNone ) 
       
   879             {
       
   880             DLTRACE(( "Setting access point %d for browse", apId ))   
       
   881             Config().SetConnectionMethod( 
       
   882                 TCatalogsConnectionMethod( 
       
   883                     apId, ECatalogsConnectionMethodTypeAccessPoint ) );
       
   884             }
       
   885         }
       
   886     
       
   887     DLTRACEOUT(( "" ));
       
   888     }
       
   889     
       
   890 
       
   891 // ---------------------------------------------------------------------------
       
   892 // Updates the download headers
       
   893 // ---------------------------------------------------------------------------
       
   894 //    
       
   895 void CNcdDownloadOperation::UpdateHeadersL( const MCatalogsContext& aContext )
       
   896     {    
       
   897     DLTRACEIN((""));
       
   898     
       
   899     DLTRACE(( _L("Client id: %S"), &iConfigurationManager.ClientIdL( 
       
   900         aContext) ));
       
   901     DASSERT( iDownload );
       
   902     
       
   903     // Add ClientId, SSIS and sessionId to file downloads
       
   904     MCatalogsHttpHeaders& headers = iDownload->Config().RequestHeaders();
       
   905     headers.AddHeaderL( NcdHttpHeaders::KClientIdHeader, 
       
   906         iConfigurationManager.ClientIdL( aContext) );
       
   907     headers.AddHeaderL( NcdHttpHeaders::KSsidHeader, 
       
   908         iConfigurationManager.SsidL( aContext) );
       
   909         
       
   910     if ( iSessionId )
       
   911         {  
       
   912         DLTRACE(( _L("Adding session id: %S"), iSessionId ));      
       
   913         headers.AddHeaderL( NcdHttpHeaders::KSessionIdHeader, *iSessionId );
       
   914         }
       
   915     DLTRACEOUT(( "" ));
       
   916     }
       
   917 
       
   918 
       
   919 // ---------------------------------------------------------------------------
       
   920 // 
       
   921 // ---------------------------------------------------------------------------
       
   922 //    
       
   923 void CNcdDownloadOperation::GetPausableStateL( MCatalogsBaseMessage& aMessage )
       
   924     {
       
   925     DLTRACEIN((""));
       
   926     TInt pausableState = KNcdDownloadIsNotPausable;
       
   927     if ( iDownload && iDownload->IsPausable() ) 
       
   928         {
       
   929         DLTRACE(("Download is pausable"));
       
   930         pausableState = KNcdDownloadIsPausable;
       
   931         }
       
   932                 
       
   933     aMessage.CompleteAndReleaseL(
       
   934         pausableState, 
       
   935         KErrNone );
       
   936     
       
   937     }