ncdengine/engine/transport/src/catalogshttpdownloadmanagerimpl.cpp
changeset 0 ba25891c3a9e
child 25 7333d7932ef7
equal deleted inserted replaced
-1:000000000000 0:ba25891c3a9e
       
     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 "catalogshttpdownloadmanagerimpl.h"
       
    20 
       
    21 #include "catalogsconstants.h"
       
    22 #include "catalogshttpoperation.h"
       
    23 #include "catalogsutils.h"
       
    24 #include "catalogshttpdownload.h"
       
    25 #include "catalogshttpconfigimpl.h"
       
    26 #include "catalogshttpsessionmanager.h"
       
    27 #include "catalogshttptransactionmanager.h"
       
    28 #include "catalogshttpconnectionmanager.h"
       
    29 #include "catalogshttpsessionmanagerimpl.h"
       
    30 #include "catalogsnetworkmanager.h"
       
    31 #include "catalogshttpsessionimpl.h"
       
    32 #include "catalogsconnection.h"
       
    33 
       
    34 #include "catalogsdebug.h"
       
    35 
       
    36 // ======== MEMBER FUNCTIONS ========
       
    37 
       
    38 // ---------------------------------------------------------------------------
       
    39 // Creator
       
    40 // ---------------------------------------------------------------------------
       
    41 //
       
    42 CCatalogsHttpDownloadManager* CCatalogsHttpDownloadManager::NewL( 
       
    43     MCatalogsHttpSessionManager& aManager, 
       
    44     CCatalogsHttpSession& aSession,
       
    45     TInt32 aSessionId,
       
    46     MCatalogsHttpTransactionManager& aTransactionManager,
       
    47     CCatalogsHttpConnectionManager& aConnectionManager,
       
    48     TBool aCleanup )
       
    49     {
       
    50     DLTRACEIN(("aMan=%X aTrans=%X",&aManager,&aTransactionManager));
       
    51     
       
    52     CCatalogsHttpDownloadManager* self = new ( ELeave ) 
       
    53             CCatalogsHttpDownloadManager( 
       
    54                 aManager, 
       
    55                 aSession,
       
    56                 aSessionId, 
       
    57                 aTransactionManager, 
       
    58                 aConnectionManager );        
       
    59             
       
    60     DLTRACE(("constructor ok"));
       
    61     CleanupStack::PushL( self );
       
    62     self->ConstructL( aCleanup );
       
    63     CleanupStack::Pop( self );
       
    64     return self;
       
    65     }
       
    66 
       
    67 // ---------------------------------------------------------------------------
       
    68 // Destructor
       
    69 // ---------------------------------------------------------------------------
       
    70 //
       
    71 CCatalogsHttpDownloadManager::~CCatalogsHttpDownloadManager()
       
    72     {
       
    73     DLTRACEIN( ( "" ) );
       
    74 
       
    75     Cancel();
       
    76     iEventQueue.Close();
       
    77 
       
    78     // Release downloads
       
    79     TInt count = iDownloads.Count() - 1;
       
    80     while( count > -1 )
       
    81         {
       
    82         while( iDownloads[count]->Release() )
       
    83             {
       
    84             }
       
    85         --count;
       
    86         }
       
    87 
       
    88     count = iRestoredDownloads.Count() - 1;
       
    89     while( count > -1 )
       
    90         {
       
    91         while( iRestoredDownloads[count]->Release() )
       
    92             {
       
    93             }
       
    94         --count;
       
    95         }
       
    96 
       
    97     iDownloads.Close();    
       
    98     iRestoredDownloads.Close();
       
    99 
       
   100     if ( iNetworkManager ) 
       
   101         {
       
   102         iNetworkManager->RemoveObserver( *this );
       
   103         }
       
   104 
       
   105     if ( iDmgr.Handle() ) 
       
   106         {
       
   107         DLTRACE(("We were connected to DL manager"));
       
   108         DeleteHangingDownloads();
       
   109         }
       
   110         
       
   111     // Close download manager session    
       
   112     iDmgr.Close();
       
   113     
       
   114     delete iDefaultConfig;
       
   115     iManager.Release();
       
   116     
       
   117     iFs.Close();
       
   118 
       
   119     DLTRACEOUT( ( "" ) );
       
   120     }
       
   121 
       
   122 
       
   123 void CCatalogsHttpDownloadManager::DeleteHangingDownloads() 
       
   124     {
       
   125     DLTRACEIN((""));
       
   126     // This pauses all downloads in addition to disconnecting
       
   127     iDmgr.Disconnect();
       
   128     const CDownloadArray& downloads( iDmgr.CurrentDownloads() );
       
   129 
       
   130     TInt count = downloads.Count();
       
   131     while( count-- )
       
   132         {                         
       
   133         RHttpDownload& dl = *( downloads[ count ] );
       
   134         TInt32 deleteStatus = 0;
       
   135         dl.GetIntAttribute( EDlAttrUserData, deleteStatus );
       
   136         DLTRACE(("DeleteStatus: %d", deleteStatus ));
       
   137         
       
   138         if ( deleteStatus == CCatalogsHttpDownload::EDownloadCanBeDeleted ) 
       
   139             {
       
   140             DLTRACE(("Deleting download"));
       
   141             dl.Delete();
       
   142             }
       
   143         
       
   144         }
       
   145     }
       
   146 
       
   147 // ---------------------------------------------------------------------------
       
   148 // Download creator
       
   149 // ---------------------------------------------------------------------------
       
   150 //
       
   151 MCatalogsHttpOperation* CCatalogsHttpDownloadManager::CreateDownloadL( 
       
   152     const TDesC16& aUrl, TBool aStart, MCatalogsHttpObserver* aObserver )
       
   153     {
       
   154     DLTRACEIN((""));
       
   155     HBufC8* buf = ConvertUnicodeToUtf8L( aUrl );
       
   156     CleanupStack::PushL( buf );
       
   157     DLTRACE(("Creating download"));
       
   158     MCatalogsHttpOperation* operation = CreateDownloadL( *buf, 
       
   159         aStart, aObserver );
       
   160     CleanupStack::PopAndDestroy( buf );    
       
   161     DLTRACEOUT((""));
       
   162     return operation;
       
   163     }
       
   164     
       
   165 
       
   166 // ---------------------------------------------------------------------------
       
   167 // Download creator
       
   168 // ---------------------------------------------------------------------------
       
   169 //
       
   170 MCatalogsHttpOperation* CCatalogsHttpDownloadManager::CreateDownloadL( 
       
   171     const TDesC8& aUrl, TBool /* aStart */, MCatalogsHttpObserver* aObserver )
       
   172     {
       
   173     DLTRACEIN((""));
       
   174     // Create a new download
       
   175     CCatalogsHttpDownload* dl = CCatalogsHttpDownload::NewLC( 
       
   176         *this,
       
   177         //download,
       
   178         NULL,
       
   179         *iDefaultConfig );
       
   180     
       
   181     dl->SetUriL( aUrl );
       
   182     dl->SetFileServerSession( iFs );
       
   183     
       
   184     // If the given observer != NULL, set it as the observer for the download
       
   185     if ( aObserver ) 
       
   186         {        
       
   187         dl->Config().SetObserver( aObserver );        
       
   188         }
       
   189     
       
   190     // Add the download to downloads
       
   191     iDownloads.AppendL( dl );
       
   192         
       
   193     CleanupStack::Pop( dl );
       
   194     
       
   195     DLTRACEOUT((""));
       
   196     return dl;        
       
   197     }
       
   198     
       
   199 
       
   200 // ---------------------------------------------------------------------------
       
   201 // Download getter
       
   202 // ---------------------------------------------------------------------------
       
   203 //    
       
   204 MCatalogsHttpOperation* CCatalogsHttpDownloadManager::Download( 
       
   205     const TCatalogsTransportOperationId& aId ) const
       
   206     {
       
   207     TInt count = iDownloads.Count();
       
   208     for ( TInt i = 0; i < count; ++i )
       
   209         {
       
   210         if ( iDownloads[i]->OperationId() == aId ) 
       
   211             {
       
   212             return iDownloads[i];
       
   213             }
       
   214         }
       
   215     return NULL;
       
   216     }
       
   217     
       
   218     
       
   219 // ---------------------------------------------------------------------------
       
   220 // Current downloads getter
       
   221 // ---------------------------------------------------------------------------
       
   222 //    
       
   223 const RCatalogsHttpOperationArray& 
       
   224     CCatalogsHttpDownloadManager::CurrentDownloads() const
       
   225     {
       
   226     return iDownloads;
       
   227     }
       
   228 
       
   229 
       
   230 // ---------------------------------------------------------------------------
       
   231 // Restored downloads getter
       
   232 // ---------------------------------------------------------------------------
       
   233 //
       
   234 const RCatalogsHttpOperationArray& 
       
   235     CCatalogsHttpDownloadManager::RestoredDownloads() const
       
   236     {
       
   237     return iRestoredDownloads;
       
   238     }
       
   239 
       
   240 
       
   241 // ---------------------------------------------------------------------------
       
   242 // Cancels all downloads
       
   243 // ---------------------------------------------------------------------------
       
   244 //
       
   245 void CCatalogsHttpDownloadManager::CancelAll()
       
   246     {
       
   247     TInt count = iDownloads.Count();
       
   248     while( count-- )
       
   249         {
       
   250         iDownloads[ count ]->Cancel();        
       
   251         }
       
   252 
       
   253     iDownloads.Reset();
       
   254 
       
   255     DeleteRestoredDownloads();
       
   256     iCancellingAll = EFalse;      
       
   257       
       
   258     }
       
   259 
       
   260 
       
   261 // ---------------------------------------------------------------------------
       
   262 // 
       
   263 // ---------------------------------------------------------------------------
       
   264 //
       
   265 void CCatalogsHttpDownloadManager::NotifyCancelAll()
       
   266     {
       
   267     DLTRACEIN((""));    
       
   268     
       
   269     TInt count = iDownloads.Count();
       
   270     
       
   271     while( count-- && iDownloads.Count() )
       
   272         {
       
   273         // NotifyCancel can lead to canceling other downloads too so we must
       
   274         // make sure that we don't go out of bounds
       
   275         if ( count >= iDownloads.Count() ) 
       
   276             {
       
   277             count = iDownloads.Count() - 1;
       
   278             }
       
   279             
       
   280         iDownloads[count]->NotifyCancel();        
       
   281         }
       
   282     }
       
   283 
       
   284 
       
   285 // ---------------------------------------------------------------------------
       
   286 // Deletes restored downloads
       
   287 // ---------------------------------------------------------------------------
       
   288 //
       
   289 void CCatalogsHttpDownloadManager::DeleteRestoredDownloads()
       
   290     {
       
   291     DLTRACEIN((""));
       
   292     TInt count = iRestoredDownloads.Count();
       
   293     while ( count-- )
       
   294         {
       
   295         iRestoredDownloads[count]->Cancel();        
       
   296         }
       
   297     iRestoredDownloads.Reset();             
       
   298     }
       
   299     
       
   300 
       
   301 // ---------------------------------------------------------------------------
       
   302 // Returns the default configuration for downloads
       
   303 // ---------------------------------------------------------------------------
       
   304 //
       
   305 MCatalogsHttpConfig& CCatalogsHttpDownloadManager::DefaultConfig() const
       
   306     {
       
   307     return *iDefaultConfig;
       
   308     }
       
   309 
       
   310 
       
   311 // ---------------------------------------------------------------------------
       
   312 // Moves a restored download to current downloads
       
   313 // ---------------------------------------------------------------------------
       
   314 //
       
   315 TInt CCatalogsHttpDownloadManager::MoveRestoredDlToCurrentDls( 
       
   316     MCatalogsHttpOperation& aDownload )
       
   317     {
       
   318     DLTRACEIN((""));
       
   319     DASSERT( aDownload.OperationType() == ECatalogsHttpDownload );
       
   320     TInt index = FindInDownloads( iRestoredDownloads, &aDownload );
       
   321     if ( index != KErrNotFound ) 
       
   322         {
       
   323         DLTRACE(( "Removing from restored downloads" ));
       
   324         iRestoredDownloads.Remove( index );
       
   325         index = iDownloads.Append( &aDownload );
       
   326         
       
   327         // Update latest download id counter
       
   328         TInt opId = aDownload.OperationId().Id();
       
   329         if ( opId > iCurrentDlId ) 
       
   330             {
       
   331             iCurrentDlId = opId;
       
   332             }
       
   333 
       
   334         DLTRACE(( "Append err: %i", index ));                                
       
   335         }
       
   336     return index;
       
   337     }
       
   338 
       
   339 // ---------------------------------------------------------------------------
       
   340 // Removes the download from the manager
       
   341 // ---------------------------------------------------------------------------
       
   342 //
       
   343 void CCatalogsHttpDownloadManager::RemoveDownload( 
       
   344     MCatalogsHttpOperation* aDownload )
       
   345     {    
       
   346     DLTRACEIN( ( "Total DLs before: %i", iDownloads.Count() ) );
       
   347     TInt index = KErrNotFound;
       
   348     // If cancelling all, we don't want to remove them separately 
       
   349     // from the list
       
   350     if ( !iCancellingAll ) 
       
   351         {        
       
   352         // Search from downloads
       
   353         index = FindInDownloads( iDownloads, aDownload );
       
   354         
       
   355         if ( index != KErrNotFound ) 
       
   356             {
       
   357             DLTRACE( ( "Removing from downloads" ) );
       
   358             iDownloads.Remove( index );
       
   359             }
       
   360         else 
       
   361             {
       
   362             // Search from restored
       
   363             index = FindInDownloads( iRestoredDownloads, aDownload );
       
   364                 
       
   365             if ( index != KErrNotFound ) 
       
   366                 {
       
   367                 DLTRACE( ("Removing from restored downloads") );
       
   368                 iRestoredDownloads.Remove( index );
       
   369                 }
       
   370             }
       
   371         
       
   372         }
       
   373     DLTRACEOUT( ( "Total DLs after: %i", iDownloads.Count() ) );
       
   374     }
       
   375 
       
   376 
       
   377 // ---------------------------------------------------------------------------
       
   378 // Create download transaction
       
   379 // ---------------------------------------------------------------------------
       
   380 //
       
   381 MCatalogsHttpOperation* CCatalogsHttpDownloadManager::CreateDlTransactionL( 
       
   382     const TDesC8& aUrl, MCatalogsHttpObserver& aObserver,
       
   383     const CCatalogsHttpConfig& aConfig )
       
   384     {
       
   385     return iTrManager.CreateDlTransactionL( aUrl, aObserver, aConfig );
       
   386     }
       
   387 
       
   388 
       
   389 // ---------------------------------------------------------------------------
       
   390 // Sets the access point for the next download
       
   391 // ---------------------------------------------------------------------------
       
   392 //
       
   393 void CCatalogsHttpDownloadManager::SetConnectionMethodL( 
       
   394     const TCatalogsConnectionMethod& aMethod )
       
   395     {
       
   396     DLTRACEIN((""));
       
   397     if ( aMethod != iCurrentAp )
       
   398         {
       
   399         DLTRACE( ( "Set AP, type: %d, id: %u, apn: %d", 
       
   400             aMethod.iType, aMethod.iId, aMethod.iApnId ));
       
   401         User::LeaveIfError( iDmgr.Disconnect() );    
       
   402         User::LeaveIfError( iDmgr.SetIntAttribute( EDlMgrIap, aMethod.iApnId ) );
       
   403         iCurrentAp = aMethod;
       
   404         DLTRACE(("AP set"));
       
   405         }    
       
   406     }
       
   407 
       
   408 
       
   409 // ---------------------------------------------------------------------------
       
   410 // Connection setter
       
   411 // ---------------------------------------------------------------------------
       
   412 //
       
   413 void CCatalogsHttpDownloadManager::SetConnectionL(
       
   414     CCatalogsConnection& aConnection )
       
   415     {
       
   416     DLTRACEIN((""));
       
   417     // No need to do any comparisons, SetConnectionMethodL ensures that
       
   418     // we don't update the APN unnecessarily to download manager
       
   419     //
       
   420     // There used to be a pointer comparison here but it didn't always work
       
   421     // correctly because the new CCatalogsConnection object was often
       
   422     // created to the same address as the old one so the APN was not
       
   423     // updated to the download manager    
       
   424     
       
   425     SetConnectionMethodL( aConnection.ConnectionMethod() );            
       
   426     }
       
   427 
       
   428 // ---------------------------------------------------------------------------
       
   429 // Add a reference
       
   430 // ---------------------------------------------------------------------------
       
   431 //
       
   432 void CCatalogsHttpDownloadManager::AddRef()
       
   433     {
       
   434     iRefCount++;    
       
   435     }
       
   436 
       
   437 
       
   438 // ---------------------------------------------------------------------------
       
   439 // Release a reference
       
   440 // ---------------------------------------------------------------------------
       
   441 //
       
   442 TInt CCatalogsHttpDownloadManager::Release()
       
   443     {
       
   444     DLTRACEIN( ( "" ) );
       
   445     iRefCount--;
       
   446     if ( !iRefCount ) 
       
   447         {
       
   448         delete this;
       
   449         return 0;
       
   450         }
       
   451     return iRefCount;
       
   452     }
       
   453 
       
   454 
       
   455 // ---------------------------------------------------------------------------
       
   456 // Returns the session id
       
   457 // ---------------------------------------------------------------------------
       
   458 //
       
   459 TInt32 CCatalogsHttpDownloadManager::SessionId() const
       
   460     {
       
   461     return iSessionId;
       
   462     }
       
   463     
       
   464     
       
   465 // ---------------------------------------------------------------------------
       
   466 // Handles download manager events
       
   467 // ---------------------------------------------------------------------------
       
   468 //
       
   469 void CCatalogsHttpDownloadManager::HandleDMgrEventL( RHttpDownload& aDownload,
       
   470 	THttpDownloadEvent aEvent )
       
   471     {
       
   472     DLTRACEIN(( "Dl state: %i, pr: %i, DL: %x", aEvent.iDownloadState, 
       
   473         aEvent.iProgressState, &aDownload ));
       
   474         
       
   475     TDownloadEvent event( aDownload, aEvent );
       
   476 
       
   477     if ( IsOneOf( aEvent.iDownloadState, EHttpDlDeleted, EHttpDlDeleting ) )
       
   478         {
       
   479         DLTRACEOUT(("Download was deleted"))
       
   480         RemoveUnhandledEvents( event );
       
   481         }
       
   482     // Handle everything but Download name change events
       
   483     else if ( aEvent.iProgressState != EHttpProgDlNameChanged )     
       
   484         {        
       
   485         DLTRACE(("Adding event to queue"));        
       
   486         
       
   487         if ( aEvent.iProgressState == EHttpProgResponseBodyReceived ) 
       
   488             {
       
   489             // Filter unhandled progress events from the queue
       
   490             RemoveUnhandledProgressEvents( event );
       
   491             }
       
   492         
       
   493         iEventQueue.AppendL( event );    
       
   494         ContinueEventHandling();    
       
   495         }
       
   496     }
       
   497 
       
   498 
       
   499 // ---------------------------------------------------------------------------
       
   500 // Accesspoint observer
       
   501 // ---------------------------------------------------------------------------
       
   502 //
       
   503 void CCatalogsHttpDownloadManager::HandleAccessPointEventL( 
       
   504     const TCatalogsConnectionMethod& /* aAp */,
       
   505     const TCatalogsAccessPointEvent& aEvent )
       
   506     {
       
   507     DLTRACEIN((""));
       
   508     if ( aEvent == ECatalogsAccessPointClosed ) 
       
   509         {
       
   510         DLTRACE(("Disconnecting download manager"));
       
   511         User::LeaveIfError( iDmgr.Disconnect() );
       
   512         }
       
   513     }
       
   514 
       
   515 
       
   516 // ---------------------------------------------------------------------------
       
   517 // Constructor
       
   518 // ---------------------------------------------------------------------------
       
   519 //
       
   520 CCatalogsHttpDownloadManager::CCatalogsHttpDownloadManager( 
       
   521     MCatalogsHttpSessionManager& aManager, 
       
   522     CCatalogsHttpSession& aSession,
       
   523     TInt32 aSessionId,
       
   524     MCatalogsHttpTransactionManager& aTransactionManager,
       
   525     CCatalogsHttpConnectionManager& aConnectionManager ) 
       
   526     :    
       
   527 #ifdef __SERIES60_31__    
       
   528     // LOL, it works
       
   529     CActive( CActive::EPriorityHigh ),
       
   530 #else    
       
   531     CActive( CActive::EPriorityLow - 21 ),
       
   532     
       
   533 #endif
       
   534     iManager( aManager ), 
       
   535     iSession( aSession ),
       
   536     iTrManager( aTransactionManager ),
       
   537     iConnectionManager( aConnectionManager ), 
       
   538     iSessionId( aSessionId ),
       
   539     iCancellingAll( EFalse ), 
       
   540     iRefCount( 1 )    
       
   541     {
       
   542     DLTRACEIN((""));
       
   543     iManager.AddRef();
       
   544     CActiveScheduler::Add( this );
       
   545     }
       
   546 
       
   547 
       
   548 // ---------------------------------------------------------------------------
       
   549 // 2nd phase constructor
       
   550 // ---------------------------------------------------------------------------
       
   551 //    
       
   552 void CCatalogsHttpDownloadManager::ConstructL( TBool aCleanup )
       
   553     {        
       
   554     DLTRACEIN((""));
       
   555     User::LeaveIfError( iFs.Connect() );
       
   556     
       
   557     // shared so that RFiles can be given to Download manager
       
   558     User::LeaveIfError( iFs.ShareProtected() ); 
       
   559     
       
   560     TUid sessionId( TUid::Uid( iSessionId ) );
       
   561     if ( aCleanup ) 
       
   562         {
       
   563         DLTRACE(("Cleaning download manager before connecting"));        
       
   564         TUidName client( CleanUidName( sessionId ) );
       
   565         TPath path;        
       
   566         path.Format( KCatalogsDownloadMgrPath, &client );
       
   567 
       
   568         CFileMan* fileman = CFileMan::NewL( iFs );
       
   569         CleanupStack::PushL( fileman );
       
   570         DLINFO(( _L("Clearing directory: %S"), &path ));
       
   571         // Ignoring errors
       
   572         fileman->Delete( path );
       
   573         CleanupStack::PopAndDestroy( fileman );
       
   574         }
       
   575         
       
   576     // Connect with the download manager
       
   577 	// If the 3rd parameter == ETrue, this session inherits downloads 
       
   578 	// from other sessions that have the same UID.    
       
   579     TInt err = KErrNone;
       
   580     TInt retries = KCatalogsDlMgrConnectRetryAttempts;
       
   581     
       
   582     do    
       
   583         {
       
   584         TRAP( err, iDmgr.ConnectL( sessionId, *this, ETrue ) );
       
   585         if ( err != KErrNone ) 
       
   586             {
       
   587             DLERROR(("DL manager connection failed with err: %d, retry attempts left", 
       
   588                 err, retries ));
       
   589             // This halts the whole thread which is not nice but shouldn't
       
   590             // be a problem since this should occur only when the download
       
   591             // manager is being shutdown when we try to connect to it
       
   592             User::After( KCatalogsDlMgrConnectRetryInterval ); 
       
   593             }
       
   594         }
       
   595     while ( err != KErrNone && retries-- );
       
   596     
       
   597     if( err != KErrNone )
       
   598         {
       
   599         DLINFO(("Leaving.. DL manager connection failed with: %d", err ));
       
   600         User::Leave( err );
       
   601         }
       
   602 
       
   603     DLTRACE(("ConnectL ok"));
       
   604         
       
   605     iDefaultConfig = CCatalogsHttpConfig::NewL();
       
   606     
       
   607     iNetworkManager = &CCatalogsHttpSessionManager::NetworkManagerL();
       
   608     iNetworkManager->AddObserverL( *this );    
       
   609     
       
   610     iDmgr.SetIntAttribute( EDlMgrExitAction, EExitPause );
       
   611     // Restore downloads from previous sessions
       
   612     RestoreDownloadsL();
       
   613     DLTRACEOUT((""));
       
   614     }
       
   615     
       
   616 
       
   617 // ---------------------------------------------------------------------------
       
   618 // Restore downloads from previous session
       
   619 // ---------------------------------------------------------------------------
       
   620 //        
       
   621 void CCatalogsHttpDownloadManager::RestoreDownloadsL()
       
   622     {
       
   623     DLTRACEIN((""));
       
   624     const CDownloadArray& downloads = iDmgr.CurrentDownloads();    
       
   625     
       
   626     TBuf8<KMaxUrlLength> url;
       
   627     TBuf<KMaxPath> filename;
       
   628     
       
   629     // Go through downloads, create wrappers for DL manager downloads and
       
   630     // add them to HttpDownloadManager
       
   631     for ( TInt i = 0; i < downloads.Count(); ++i ) 
       
   632         {                 
       
   633         DLTRACE(( "Restoring dl %i", i ));
       
   634         RHttpDownload* dlPtr = downloads[i];
       
   635 
       
   636         // Create the download and push it to cleanup stack
       
   637         CCatalogsHttpDownload* dl = CCatalogsHttpDownload::NewLC( *this,
       
   638             dlPtr, *iDefaultConfig );
       
   639 
       
   640         // set file server session
       
   641         dl->SetFileServerSession( iFs );
       
   642         
       
   643         dlPtr->GetStringAttribute( EDlAttrDestFilename, filename );
       
   644 
       
   645         // Separate the filename from the path
       
   646         TParsePtrC parser( filename );
       
   647         
       
   648         if ( parser.PathPresent() ) 
       
   649             {
       
   650             
       
   651             dl->Config().SetDirectoryL( parser.DriveAndPath() );
       
   652             DLTRACE( ( dl->Config().Directory() ) );    
       
   653             }
       
   654             
       
   655         if ( parser.NameOrExtPresent() ) 
       
   656             {
       
   657             dl->Config().SetFilenameL( parser.NameAndExt() );
       
   658             DLTRACE( ( dl->Config().Filename() ) );    
       
   659             }
       
   660             
       
   661         // Update the content type from download manager       
       
   662         dl->UpdateContentType();
       
   663         
       
   664         // Update URI from download manager
       
   665         dl->UpdateUriL();               
       
   666         
       
   667         // Append the download to the list of downloads
       
   668         iRestoredDownloads.AppendL( dl );
       
   669                 
       
   670         CleanupStack::Pop( dl );
       
   671         }
       
   672     }
       
   673     
       
   674 
       
   675 // ---------------------------------------------------------------------------
       
   676 // Finds the index for the given download in iDownloads
       
   677 // ---------------------------------------------------------------------------
       
   678 //            
       
   679 TInt CCatalogsHttpDownloadManager::FindInDownloads( 
       
   680     const RCatalogsHttpOperationArray& aArray,
       
   681     RHttpDownload* aDownload ) const
       
   682     {    
       
   683     DASSERT( aDownload );
       
   684     
       
   685     TInt32 id = 0;
       
   686     aDownload->GetIntAttribute( EDlAttrId, id );    
       
   687             
       
   688     TInt count = aArray.Count();
       
   689     for ( TInt i = 0; i < count; ++i )
       
   690         {
       
   691         if ( aArray[ i ]->OperationId().SecondaryId() == id ) 
       
   692             {
       
   693             return i;
       
   694             }
       
   695         }
       
   696     return KErrNotFound;
       
   697     }
       
   698 
       
   699 
       
   700 // ---------------------------------------------------------------------------
       
   701 // Finds the index for the given download in the given array
       
   702 // ---------------------------------------------------------------------------
       
   703 //            
       
   704 TInt CCatalogsHttpDownloadManager::FindInDownloads( 
       
   705     const RCatalogsHttpOperationArray& aArray,
       
   706     MCatalogsHttpOperation* aDownload ) const
       
   707     {
       
   708     DASSERT( aDownload );
       
   709         
       
   710     return aArray.Find( aDownload );    
       
   711     }
       
   712 
       
   713 
       
   714 // ---------------------------------------------------------------------------
       
   715 // Decides whether the download can start or not
       
   716 // ---------------------------------------------------------------------------
       
   717 //            
       
   718 TInt CCatalogsHttpDownloadManager::StartOperation( 
       
   719     MCatalogsHttpOperation* aOperation )
       
   720     {
       
   721     DLTRACEIN(( "" ));
       
   722     DASSERT( aOperation );
       
   723     TInt err = iManager.StartOperation( aOperation );
       
   724     if ( err == KErrNone || err == KCatalogsHttpOperationQueued ) 
       
   725         {
       
   726         TInt err2 = MoveRestoredDlToCurrentDls( *aOperation );
       
   727         // Check for unwanted errors
       
   728         if ( err2 != KErrNone && err2 != KErrNotFound ) 
       
   729             {
       
   730             return err2;
       
   731             }
       
   732         }
       
   733     return err;
       
   734     }
       
   735 
       
   736 
       
   737 // ---------------------------------------------------------------------------
       
   738 // Moves the download to the queue if it isn't there already
       
   739 // ---------------------------------------------------------------------------
       
   740 //            
       
   741 TInt CCatalogsHttpDownloadManager::PauseOperation( 
       
   742     MCatalogsHttpOperation* aOperation )
       
   743     {    
       
   744     DLTRACEIN(( "" ));    
       
   745     return iManager.PauseOperation( aOperation );
       
   746     }
       
   747 
       
   748 
       
   749 // ---------------------------------------------------------------------------
       
   750 // Complete the download and resume a download from the queue if any
       
   751 // ---------------------------------------------------------------------------
       
   752 //            
       
   753 TInt CCatalogsHttpDownloadManager::CompleteOperation( 
       
   754     MCatalogsHttpOperation* aOperation )
       
   755     {    
       
   756     DLTRACE(( "Total DLs: %i", iDownloads.Count() ));
       
   757     return iManager.CompleteOperation( aOperation );
       
   758     }
       
   759 
       
   760 
       
   761 // ---------------------------------------------------------------------------
       
   762 // 
       
   763 // ---------------------------------------------------------------------------
       
   764 //            
       
   765 void CCatalogsHttpDownloadManager::ReportConnectionStatus( TBool aActive )
       
   766     {
       
   767     iSession.ReportConnectionStatus( aActive );
       
   768     }
       
   769     
       
   770 
       
   771 // ---------------------------------------------------------------------------
       
   772 // Handles priority change
       
   773 // ---------------------------------------------------------------------------
       
   774 //            
       
   775 TInt CCatalogsHttpDownloadManager::OperationPriorityChanged( 
       
   776     MCatalogsHttpOperation* aOperation )
       
   777     {
       
   778     return iManager.OperationPriorityChanged( aOperation );
       
   779     }
       
   780 
       
   781 
       
   782 // ---------------------------------------------------------------------------
       
   783 // 
       
   784 // ---------------------------------------------------------------------------
       
   785 //            
       
   786 CCatalogsHttpConnectionCreator& CCatalogsHttpDownloadManager::ConnectionCreatorL()
       
   787     {
       
   788     return iManager.ConnectionCreatorL();
       
   789     }
       
   790 
       
   791 // ---------------------------------------------------------------------------
       
   792 // 
       
   793 // ---------------------------------------------------------------------------
       
   794 //            
       
   795 CCatalogsHttpConnectionManager& CCatalogsHttpDownloadManager::ConnectionManager()
       
   796     {
       
   797     return iConnectionManager;
       
   798     }
       
   799 
       
   800 
       
   801 // ---------------------------------------------------------------------------
       
   802 // 
       
   803 // ---------------------------------------------------------------------------
       
   804 //            
       
   805 CDocumentHandler& CCatalogsHttpDownloadManager::DocumentHandler()
       
   806     {
       
   807     return iManager.DocumentHandler();
       
   808     }
       
   809 
       
   810 
       
   811 // ---------------------------------------------------------------------------
       
   812 // Event handling
       
   813 // ---------------------------------------------------------------------------
       
   814 //            
       
   815 void CCatalogsHttpDownloadManager::RunL()
       
   816     {
       
   817     DLTRACEIN(( "Event queue count: %d", iEventQueue.Count() ));
       
   818    
       
   819     // Handle the event queue 
       
   820     while ( iEventQueue.Count() ) 
       
   821         {
       
   822         
       
   823         TDownloadEvent dlEvent( iEventQueue[0] );
       
   824         iEventQueue.Remove( 0 );
       
   825         THttpDownloadEvent& event( dlEvent.iEvent );
       
   826 
       
   827         if ( event.iDownloadState == EHttpDlDeleted )
       
   828             {
       
   829             DLTRACEOUT(("Download was deleted"));
       
   830             RemoveUnhandledEvents( dlEvent );
       
   831             continue;
       
   832             }        
       
   833         
       
   834         // Find the correct download
       
   835         TInt index = FindInDownloads( iDownloads, dlEvent.iDownload );
       
   836         if ( index == KErrNotFound ) 
       
   837             {
       
   838             DLTRACE(("Download was not found, dl-ptr: %x", dlEvent.iDownload ));            
       
   839             continue;
       
   840             }
       
   841             
       
   842         CCatalogsHttpDownload* dl = 
       
   843             static_cast<CCatalogsHttpDownload*>( iDownloads[index] );
       
   844 
       
   845         DLINFO(("Found the download, index: %d", index));
       
   846         DLTRACE(( "DL id: %i", iDownloads[index]->OperationId().Id() ));
       
   847         // Use the download object to handle the event
       
   848 
       
   849 #ifdef __SERIES60_31__
       
   850         if ( dl->IsCancelled() && 
       
   851             IsOneOf( 
       
   852                 event.iDownloadState,             
       
   853                 EHttpDlPaused, 
       
   854                 EHttpDlCompleted, 
       
   855                 EHttpDlFailed ) )
       
   856             {
       
   857             RemoveUnhandledEvents( dlEvent );
       
   858             }
       
   859 #endif        
       
   860         
       
   861         TRAPD( eventErr, dl->HandleEventL( event ) );
       
   862         
       
   863         if ( eventErr != KErrNone )
       
   864             {
       
   865             DLERROR(( "" ));
       
   866             MCatalogsHttpConfig& config = dl->Config();
       
   867             MCatalogsHttpObserver* observer = config.Observer();
       
   868             
       
   869             if ( observer )
       
   870                 {
       
   871                 DLINFO(( "Calling HandleHttpError" ));
       
   872                 observer->HandleHttpError( *dl, TCatalogsHttpError( ECatalogsHttpErrorGeneral, eventErr ) );
       
   873                 }
       
   874             else
       
   875                 {
       
   876                 DLERROR(( "Observer was NULL" ));
       
   877                 User::Leave( eventErr );
       
   878                 }
       
   879             }
       
   880         }
       
   881     }
       
   882     
       
   883     
       
   884 void CCatalogsHttpDownloadManager::DoCancel()
       
   885     {
       
   886     DLTRACEIN((""));
       
   887     // Nothing to be done
       
   888     }
       
   889     
       
   890     
       
   891 TInt CCatalogsHttpDownloadManager::RunError( TInt aError )
       
   892     {
       
   893     DLTRACEIN(("aError: %d", aError));
       
   894     (void) aError;
       
   895     
       
   896     DLERROR(( "Trying to cancel all downloads" ));
       
   897     NotifyCancelAll();
       
   898     
       
   899     return KErrNone;
       
   900     }    
       
   901     
       
   902     
       
   903 void CCatalogsHttpDownloadManager::ContinueEventHandling()
       
   904     {
       
   905     DLTRACEIN((""));
       
   906     if( iEventQueue.Count() && !IsActive() ) 
       
   907         {
       
   908         DLTRACE(("More events to handle"));
       
   909         iStatus = KRequestPending;
       
   910         SetActive();
       
   911         TRequestStatus* status = &iStatus;
       
   912         User::RequestComplete( status, KErrNone );
       
   913         }
       
   914     }
       
   915     
       
   916     
       
   917 void CCatalogsHttpDownloadManager::RemoveUnhandledEvents( 
       
   918     const TDownloadEvent& aEvent )
       
   919     {
       
   920     DLTRACEIN((""));
       
   921     TInt index = iEventQueue.Count();
       
   922     while( index-- ) 
       
   923         {
       
   924         if ( iEventQueue[ index ].iDownload == aEvent.iDownload ) 
       
   925             {
       
   926             DLTRACE(("Removing unhandled event"));
       
   927             iEventQueue.Remove( index );
       
   928             }        
       
   929         }    
       
   930     }
       
   931 
       
   932 
       
   933 void CCatalogsHttpDownloadManager::RemoveUnhandledProgressEvents( 
       
   934     const TDownloadEvent& aEvent )
       
   935     {
       
   936     DLTRACEIN((""));
       
   937     TInt index = iEventQueue.Count();
       
   938     while( index-- ) 
       
   939         {
       
   940         if ( iEventQueue[ index ].iDownload == aEvent.iDownload &&
       
   941              iEventQueue[ index ].iEvent.iProgressState == EHttpProgResponseBodyReceived ) 
       
   942             {
       
   943             DLTRACE(("Removing unhandled progress event"));
       
   944             iEventQueue.Remove( index );
       
   945             break;
       
   946             }        
       
   947         }    
       
   948     }
       
   949 
       
   950 
       
   951 TInt CCatalogsHttpDownloadManager::NewDownloadId()
       
   952     {
       
   953     return ++iCurrentDlId;
       
   954     }
       
   955 
       
   956 
       
   957 RHttpDownload& CCatalogsHttpDownloadManager::CreatePlatformDownloadL( 
       
   958     const TDesC8& aUrl )
       
   959     {
       
   960     return iDmgr.CreateDownloadL( aUrl );
       
   961     }