ncdengine/engine/transport/src/catalogshttpdownloadmanagerimpl.cpp
changeset 53 ae54820ef82c
parent 29 26b6f0522fd8
child 50 c6e8afe0ba85
equal deleted inserted replaced
52:92f864ef0288 53:ae54820ef82c
    30 #include "catalogsnetworkmanager.h"
    30 #include "catalogsnetworkmanager.h"
    31 #include "catalogshttpsessionimpl.h"
    31 #include "catalogshttpsessionimpl.h"
    32 #include "catalogsconnection.h"
    32 #include "catalogsconnection.h"
    33 
    33 
    34 #include "catalogsdebug.h"
    34 #include "catalogsdebug.h"
    35 
    35 #include <QString.h>
       
    36 #include <QVector.h>
       
    37 #include <QList.h>
       
    38 #include <downloadevent.h>
    36 // ======== MEMBER FUNCTIONS ========
    39 // ======== MEMBER FUNCTIONS ========
    37 
    40 
    38 // ---------------------------------------------------------------------------
    41 // ---------------------------------------------------------------------------
    39 // Creator
    42 // Creator
    40 // ---------------------------------------------------------------------------
    43 // ---------------------------------------------------------------------------
    57                 aTransactionManager, 
    60                 aTransactionManager, 
    58                 aConnectionManager );        
    61                 aConnectionManager );        
    59             
    62             
    60     DLTRACE(("constructor ok"));
    63     DLTRACE(("constructor ok"));
    61     CleanupStack::PushL( self );
    64     CleanupStack::PushL( self );
       
    65    
    62     self->ConstructL( aCleanup );
    66     self->ConstructL( aCleanup );
    63     CleanupStack::Pop( self );
    67     CleanupStack::Pop( self );
    64     return self;
    68     return self;
    65     }
    69     }
    66 
    70 
   100     if ( iNetworkManager ) 
   104     if ( iNetworkManager ) 
   101         {
   105         {
   102         iNetworkManager->RemoveObserver( *this );
   106         iNetworkManager->RemoveObserver( *this );
   103         }
   107         }
   104 
   108 
   105     if ( iDmgr.Handle() ) 
   109 
       
   110     if ( iDmgr ) 
   106         {
   111         {
   107         DLTRACE(("We were connected to DL manager"));
   112         DLTRACE(("We were connected to DL manager"));
   108         DeleteHangingDownloads();
   113         DeleteHangingDownloads();
   109         }
   114         }
   110         
   115       
   111     // Close download manager session    
   116     // Close download manager session    
   112     iDmgr.Close();
   117     iDmgr->removeAll();
   113     
   118     
       
   119    
   114     delete iDefaultConfig;
   120     delete iDefaultConfig;
   115     iManager.Release();
   121     iManager.Release();
   116     
   122     
   117     iFs.Close();
   123     iFs.Close();
   118 
   124 
   122 
   128 
   123 void CCatalogsHttpDownloadManager::DeleteHangingDownloads() 
   129 void CCatalogsHttpDownloadManager::DeleteHangingDownloads() 
   124     {
   130     {
   125     DLTRACEIN((""));
   131     DLTRACEIN((""));
   126     // This pauses all downloads in addition to disconnecting
   132     // This pauses all downloads in addition to disconnecting
   127     iDmgr.Disconnect();
   133  	iDmgr->pauseAll(); 
   128     const CDownloadArray& downloads( iDmgr.CurrentDownloads() );
   134    TInt count = iDownloads.Count();
   129 
   135     for ( TInt i = 0; i < count; ++i )
   130     TInt count = downloads.Count();
   136         {
   131     while( count-- )
   137          TInt32 deleteStatus = 0;
   132         {                         
   138          deleteStatus = dynamic_cast<CCatalogsHttpDownload*>(iDownloads[i])->GetStatusState();
   133         RHttpDownload& dl = *( downloads[ count ] );
   139        
   134         TInt32 deleteStatus = 0;
   140          if ( deleteStatus == CCatalogsHttpDownload::EDownloadCanBeDeleted ) 
   135         dl.GetIntAttribute( EDlAttrUserData, deleteStatus );
       
   136         DLTRACE(("DeleteStatus: %d", deleteStatus ));
       
   137         
       
   138         if ( deleteStatus == CCatalogsHttpDownload::EDownloadCanBeDeleted ) 
       
   139             {
   141             {
   140             DLTRACE(("Deleting download"));
   142             DLTRACE(("Deleting download"));
   141             dl.Delete();
   143             iDmgr->removeOne(dynamic_cast<CCatalogsHttpDownload*>(iDownloads[i])->GetDownload());
   142             }
   144             }
   143         
   145         }        
   144         }
   146         
   145     }
   147     }
   146 
   148 
   147 // ---------------------------------------------------------------------------
   149 // ---------------------------------------------------------------------------
   148 // Download creator
   150 // Download creator
   149 // ---------------------------------------------------------------------------
   151 // ---------------------------------------------------------------------------
   170 MCatalogsHttpOperation* CCatalogsHttpDownloadManager::CreateDownloadL( 
   172 MCatalogsHttpOperation* CCatalogsHttpDownloadManager::CreateDownloadL( 
   171     const TDesC8& aUrl, TBool /* aStart */, MCatalogsHttpObserver* aObserver )
   173     const TDesC8& aUrl, TBool /* aStart */, MCatalogsHttpObserver* aObserver )
   172     {
   174     {
   173     DLTRACEIN((""));
   175     DLTRACEIN((""));
   174     // Create a new download
   176     // Create a new download
       
   177    	QString Url =	QString::fromRawData( reinterpret_cast<const QChar*>(aUrl.Ptr()),aUrl.Length());
       
   178     iDownload = iDmgr->createDownload( Url );
       
   179     
   175     CCatalogsHttpDownload* dl = CCatalogsHttpDownload::NewLC( 
   180     CCatalogsHttpDownload* dl = CCatalogsHttpDownload::NewLC( 
   176         *this,
   181         *this,
   177         //download,
   182         //download,
   178         NULL,
   183         iDownload,
   179         *iDefaultConfig );
   184         *iDefaultConfig );
   180     
   185     
   181     dl->SetUriL( aUrl );
   186     dl->SetUriL( aUrl );
   182     dl->SetFileServerSession( iFs );
   187     dl->SetFileServerSession( iFs );
   183     
   188         // If the given observer != NULL, set it as the observer for the download
   184     // If the given observer != NULL, set it as the observer for the download
       
   185     if ( aObserver ) 
   189     if ( aObserver ) 
   186         {        
   190         {        
   187         dl->Config().SetObserver( aObserver );        
   191         dl->Config().SetObserver( aObserver );        
   188         }
   192         }
   189     
   193     
   205     const TCatalogsTransportOperationId& aId ) const
   209     const TCatalogsTransportOperationId& aId ) const
   206     {
   210     {
   207     TInt count = iDownloads.Count();
   211     TInt count = iDownloads.Count();
   208     for ( TInt i = 0; i < count; ++i )
   212     for ( TInt i = 0; i < count; ++i )
   209         {
   213         {
       
   214         	
   210         if ( iDownloads[i]->OperationId() == aId ) 
   215         if ( iDownloads[i]->OperationId() == aId ) 
   211             {
   216             {
   212             return iDownloads[i];
   217             return iDownloads[i];
   213             }
   218             }
   214         }
   219         }
   394     const TCatalogsConnectionMethod& aMethod )
   399     const TCatalogsConnectionMethod& aMethod )
   395     {
   400     {
   396     DLTRACEIN((""));
   401     DLTRACEIN((""));
   397     if ( aMethod != iCurrentAp )
   402     if ( aMethod != iCurrentAp )
   398         {
   403         {
       
   404 		try
       
   405 		{
   399         DLTRACE( ( "Set AP, type: %d, id: %u, apn: %d", 
   406         DLTRACE( ( "Set AP, type: %d, id: %u, apn: %d", 
   400             aMethod.iType, aMethod.iId, aMethod.iApnId ));
   407             aMethod.iType, aMethod.iId, aMethod.iApnId ));
   401         User::LeaveIfError( iDmgr.Disconnect() );    
   408 		 iDmgr->pauseAll(); 
   402         User::LeaveIfError( iDmgr.SetIntAttribute( EDlMgrIap, aMethod.iApnId ) );
   409 		 
   403         iCurrentAp = aMethod;
   410         iCurrentAp = aMethod;
   404         DLTRACE(("AP set"));
   411         DLTRACE(("AP set"));
       
   412 		}
       
   413 		 catch(const std::exception& exception)
       
   414 		 {
       
   415 		 qt_symbian_exception2LeaveL(exception);
       
   416 		 }
   405         }    
   417         }    
   406     }
   418     }
   407 
   419 
   408 
   420 
   409 // ---------------------------------------------------------------------------
   421 // ---------------------------------------------------------------------------
   458 //
   470 //
   459 TInt32 CCatalogsHttpDownloadManager::SessionId() const
   471 TInt32 CCatalogsHttpDownloadManager::SessionId() const
   460     {
   472     {
   461     return iSessionId;
   473     return iSessionId;
   462     }
   474     }
   463     
   475 
   464     
       
   465 // ---------------------------------------------------------------------------
   476 // ---------------------------------------------------------------------------
   466 // Handles download manager events
   477 // Handles download manager events
   467 // ---------------------------------------------------------------------------
   478 // ---------------------------------------------------------------------------
   468 //
   479 //    
   469 void CCatalogsHttpDownloadManager::HandleDMgrEventL( RHttpDownload& aDownload,
   480  void CCatalogsHttpDownloadManager::downloadMgrEventRecieved(WRT::DownloadEvent* dlmEvent)
   470 	THttpDownloadEvent aEvent )
   481   {
   471     {
   482   	
   472     DLTRACEIN(( "Dl state: %i, pr: %i, DL: %x", aEvent.iDownloadState, 
   483   	switch(dlmEvent ->type())
   473         aEvent.iProgressState, &aDownload ));
   484 		{
   474         
   485  
   475     TDownloadEvent event( aDownload, aEvent );
   486     default:
   476 
   487     	break;
   477     if ( IsOneOf( aEvent.iDownloadState, EHttpDlDeleted, EHttpDlDeleting ) )
   488 		};
   478         {
   489 
   479         DLTRACEOUT(("Download was deleted"))
   490   	
   480         RemoveUnhandledEvents( event );
   491   }  
   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 
   492 
   498 
   493 
   499 // ---------------------------------------------------------------------------
   494 // ---------------------------------------------------------------------------
   500 // Accesspoint observer
   495 // Accesspoint observer
   501 // ---------------------------------------------------------------------------
   496 // ---------------------------------------------------------------------------
   505     const TCatalogsAccessPointEvent& aEvent )
   500     const TCatalogsAccessPointEvent& aEvent )
   506     {
   501     {
   507     DLTRACEIN((""));
   502     DLTRACEIN((""));
   508     if ( aEvent == ECatalogsAccessPointClosed ) 
   503     if ( aEvent == ECatalogsAccessPointClosed ) 
   509         {
   504         {
       
   505 		try
       
   506 		{
   510         DLTRACE(("Disconnecting download manager"));
   507         DLTRACE(("Disconnecting download manager"));
   511         User::LeaveIfError( iDmgr.Disconnect() );
   508 
       
   509 		iDmgr->pauseAll();
       
   510 	}
       
   511 
       
   512  		catch(const std::exception& exception) 
       
   513                { 
       
   514                qt_symbian_exception2LeaveL(exception); 
       
   515                } 
       
   516             
       
   517 
   512         }
   518         }
   513     }
   519     }
   514 
   520 
   515 
   521 
   516 // ---------------------------------------------------------------------------
   522 // ---------------------------------------------------------------------------
   554     DLTRACEIN((""));
   560     DLTRACEIN((""));
   555     User::LeaveIfError( iFs.Connect() );
   561     User::LeaveIfError( iFs.Connect() );
   556     
   562     
   557     // shared so that RFiles can be given to Download manager
   563     // shared so that RFiles can be given to Download manager
   558     User::LeaveIfError( iFs.ShareProtected() ); 
   564     User::LeaveIfError( iFs.ShareProtected() ); 
       
   565    	
       
   566    	QString DmgrUid(QString::number(KNCDEngineAppID));
       
   567     iDmgr =  new DownloadManager(DmgrUid);
       
   568     iDmgr->initialize();
       
   569     iQTmgr = new CCatalogsHttpQTDownloadManager(this,iDmgr);
   559     
   570     
   560     TUid sessionId( TUid::Uid( iSessionId ) );
   571     TUid sessionId( TUid::Uid( iSessionId ) );
   561     if ( aCleanup ) 
   572     if ( aCleanup ) 
   562         {
   573         {
   563         DLTRACE(("Cleaning download manager before connecting"));        
   574         DLTRACE(("Cleaning download manager before connecting"));        
   579     TInt err = KErrNone;
   590     TInt err = KErrNone;
   580     TInt retries = KCatalogsDlMgrConnectRetryAttempts;
   591     TInt retries = KCatalogsDlMgrConnectRetryAttempts;
   581     
   592     
   582     do    
   593     do    
   583         {
   594         {
   584         // TRAP( err, iDmgr.ConnectL( sessionId, *this, ETrue ) );
       
   585         if ( err != KErrNone ) 
   595         if ( err != KErrNone ) 
   586             {
   596             {
   587             DLERROR(("DL manager connection failed with err: %d, retry attempts left", 
   597             DLERROR(("DL manager connection failed with err: %d, retry attempts left", 
   588                 err, retries ));
   598                 err, retries ));
   589             // This halts the whole thread which is not nice but shouldn't
   599             // This halts the whole thread which is not nice but shouldn't
   605     iDefaultConfig = CCatalogsHttpConfig::NewL();
   615     iDefaultConfig = CCatalogsHttpConfig::NewL();
   606     
   616     
   607     iNetworkManager = &CCatalogsHttpSessionManager::NetworkManagerL();
   617     iNetworkManager = &CCatalogsHttpSessionManager::NetworkManagerL();
   608     iNetworkManager->AddObserverL( *this );    
   618     iNetworkManager->AddObserverL( *this );    
   609     
   619     
   610 //    iDmgr.SetIntAttribute( EDlMgrExitAction, EExitPause );
       
   611     // Restore downloads from previous sessions
   620     // Restore downloads from previous sessions
   612 //    RestoreDownloadsL();
   621 //    RestoreDownloadsL();
       
   622  
   613     DLTRACEOUT((""));
   623     DLTRACEOUT((""));
   614     }
   624 
   615     
   625     }
   616 
   626     
       
   627 DownloadManager* CCatalogsHttpDownloadManager::GetDownloadManager()
       
   628 {
       
   629 	return iDmgr;
       
   630 }
   617 // ---------------------------------------------------------------------------
   631 // ---------------------------------------------------------------------------
   618 // Restore downloads from previous session
   632 // Restore downloads from previous session
   619 // ---------------------------------------------------------------------------
   633 // ---------------------------------------------------------------------------
   620 //        
   634 //        
   621 void CCatalogsHttpDownloadManager::RestoreDownloadsL()
   635 void CCatalogsHttpDownloadManager::RestoreDownloadsL()
   622     {
   636     {
   623     DLTRACEIN((""));
   637     DLTRACEIN((""));
   624     const CDownloadArray& downloads = iDmgr.CurrentDownloads();    
       
   625     
   638     
   626     TBuf8<KMaxUrlLength> url;
   639     TBuf8<KMaxUrlLength> url;
   627     TBuf<KMaxPath> filename;
   640     
   628     
   641     
       
   642     QList<WRT::Download*> downloads = iDmgr->currentDownloads();
       
   643 
       
   644 
   629     // Go through downloads, create wrappers for DL manager downloads and
   645     // Go through downloads, create wrappers for DL manager downloads and
   630     // add them to HttpDownloadManager
   646     // add them to HttpDownloadManager
   631     for ( TInt i = 0; i < downloads.Count(); ++i ) 
   647     for ( TInt i = 0; i < downloads.size(); ++i ) 
   632         {                 
   648         {                 
   633         DLTRACE(( "Restoring dl %i", i ));
   649         DLTRACE(( "Restoring dl %i", i ));
   634         RHttpDownload* dlPtr = downloads[i];
   650         WRT::Download *dlPtr = downloads[i];
   635 
   651 
   636         // Create the download and push it to cleanup stack
   652         // Create the download and push it to cleanup stack
   637         CCatalogsHttpDownload* dl = CCatalogsHttpDownload::NewLC( *this,
   653         CCatalogsHttpDownload* dl = CCatalogsHttpDownload::NewLC( *this,
   638             dlPtr, *iDefaultConfig );
   654             dlPtr, *iDefaultConfig );
   639 
   655 
   640         // set file server session
   656         // set file server session
   641         dl->SetFileServerSession( iFs );
   657         dl->SetFileServerSession( iFs );
   642         
   658        
   643         dlPtr->GetStringAttribute( EDlAttrDestFilename, filename );
   659          QString name   =  dlPtr->attribute(FileName).toString();
       
   660          TBuf<KMaxPath> filename(name.utf16());
   644 
   661 
   645         // Separate the filename from the path
   662         // Separate the filename from the path
   646         TParsePtrC parser( filename );
   663         TParsePtrC parser( filename );
   647         
   664         
   648         if ( parser.PathPresent() ) 
   665         if ( parser.PathPresent() ) 
   681     RHttpDownload* aDownload ) const
   698     RHttpDownload* aDownload ) const
   682     {    
   699     {    
   683     DASSERT( aDownload );
   700     DASSERT( aDownload );
   684     
   701     
   685     TInt32 id = 0;
   702     TInt32 id = 0;
   686     aDownload->GetIntAttribute( EDlAttrId, id );    
   703     //aDownload->GetIntAttribute( EDlAttrId, id );    
   687             
   704             
   688     TInt count = aArray.Count();
   705     TInt count = aArray.Count();
   689     for ( TInt i = 0; i < count; ++i )
   706     for ( TInt i = 0; i < count; ++i )
   690         {
   707         {
   691         if ( aArray[ i ]->OperationId().SecondaryId() == id ) 
   708         if ( aArray[ i ]->OperationId().SecondaryId() == id ) 
   952     {
   969     {
   953     return ++iCurrentDlId;
   970     return ++iCurrentDlId;
   954     }
   971     }
   955 
   972 
   956 
   973 
   957 RHttpDownload& CCatalogsHttpDownloadManager::CreatePlatformDownloadL( 
   974 Download& CCatalogsHttpDownloadManager::CreatePlatformDownloadL( 
   958     const TDesC8& aUrl )
   975     const TDesC8& aUrl )
   959     {
   976     {
   960     return iDmgr.CreateDownloadL( aUrl );
   977     
   961     }
   978    	QString myString=	QString::fromRawData( reinterpret_cast<const QChar*>(aUrl.Ptr()),aUrl.Length());
       
   979 
       
   980     return *(iDmgr->createDownload( myString ));
       
   981     }
       
   982     
       
   983 CCatalogsHttpQTDownloadManager::CCatalogsHttpQTDownloadManager(CCatalogsHttpDownloadManager* aDownloadManager,DownloadManager* aDmgr)
       
   984 	{
       
   985 		iDownloadManager = aDownloadManager;
       
   986 		iDmgr = aDmgr;
       
   987 		connect(iDmgr, SIGNAL(downloadManagerEvent(WRT::DownloadManagerEvent*)), this,SLOT(downloadMgrEventRecieved(WRT::DownloadEvent*)));
       
   988 	}
       
   989 	
       
   990 void CCatalogsHttpQTDownloadManager::downloadMgrEventRecieved(WRT::DownloadEvent* aEvent)
       
   991 	{
       
   992 		iDownloadManager->downloadMgrEventRecieved(aEvent);
       
   993 	}