mmfenh/enhancedmediaclient/Client/src/Components/ClientProgDLSource/ClientProgDLSource.cpp
branchRCL_3
changeset 20 0ac9a5310753
equal deleted inserted replaced
19:095bea5f582e 20:0ac9a5310753
       
     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:  Implementation of the ClientProgDLSource class.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <e32std.h>
       
    20 #include <MCustomCommand.h>
       
    21 #include <SourceControlObserver.h>
       
    22 #include "ClientProgDLSource.h"
       
    23 #include "EventNotifier.h"
       
    24 #include "EventAO.h"
       
    25 #include "ErrorCode.h"
       
    26 #include "ProgDLSourceUid.h"
       
    27 
       
    28 #define RETURN_IF_ERROR(x) if(x != KErrNone) return x
       
    29 
       
    30 // CONSTANTS
       
    31 const TInt KLocalFile = -1;
       
    32 const TInt KTranferRateMonitorFrequency = 2;
       
    33 const TInt KFileSizeChangeDelta = 10000;
       
    34 const TInt KNofityAfterPercentage = 5;
       
    35 
       
    36 using namespace multimedia;
       
    37 
       
    38 CClientProgDLSource::CClientProgDLSource()
       
    39     {
       
    40     iSourceState = CMultimediaDataSource::ECLOSED;
       
    41     isDownloadComplete = false;
       
    42     iProgressiveMode = false;
       
    43     }
       
    44 
       
    45 CClientProgDLSource::~CClientProgDLSource()
       
    46     {
       
    47     delete iEventNotifier;
       
    48     delete iMonitor;
       
    49     delete iFileName;
       
    50     
       
    51     if(iStateEvent->IsActive())
       
    52         {
       
    53         iStateEvent->Cancel();
       
    54         }
       
    55     
       
    56     delete iStateEvent;
       
    57     }
       
    58 
       
    59 TInt CClientProgDLSource::PostConstructor()
       
    60     {
       
    61     TInt status(KErrNone);
       
    62     // Make sure this doesn't get called second time around.
       
    63     if ( !iEventNotifier )
       
    64         {
       
    65         TRAP(status, iEventNotifier = CEventNotifier::NewL());
       
    66         }
       
    67     
       
    68     RETURN_IF_ERROR(status);
       
    69     
       
    70     TRAP(status, iStateEvent  = CEventAO::NewL(*this)); 
       
    71     
       
    72     return status;
       
    73     }
       
    74 
       
    75 TInt CClientProgDLSource::AddObserver( MControlObserver& aObserver )
       
    76     {
       
    77     return iEventNotifier->AddObserver( aObserver );
       
    78     }
       
    79 
       
    80 TInt CClientProgDLSource::RemoveObserver( MControlObserver& aObserver )
       
    81     {
       
    82     return iEventNotifier->RemoveObserver( aObserver );
       
    83     }
       
    84 
       
    85 TUid CClientProgDLSource::Type()
       
    86     {
       
    87     return KProgDLSourceControl;
       
    88     }
       
    89 
       
    90 TControlType CClientProgDLSource::ControlType()
       
    91     {
       
    92 	return ESourceControl;
       
    93     }
       
    94     
       
    95 TInt CClientProgDLSource::GetSize( TUint& aSize )
       
    96     {
       
    97     TInt status(KErrNone);
       
    98     aSize = iSourceSize;
       
    99     return status;
       
   100     }
       
   101 
       
   102 TInt CClientProgDLSource::GetMimeType( TDes8& aMimeType )
       
   103     {
       
   104     TInt status = iDownloadGateway->GetMimeType(iDownloadId,aMimeType);
       
   105     return status;
       
   106     }
       
   107 
       
   108 TInt CClientProgDLSource::Close()
       
   109     {
       
   110     TInt status(KErrNone);
       
   111     if(iMonitor)
       
   112         {
       
   113         iMonitor->Stop();
       
   114         }
       
   115     // Reset the Progressive Mode flag always
       
   116     iDownloadGateway->SetProgressiveMode(iDownloadId,EFalse);
       
   117     iProgressiveMode = EFalse;
       
   118     iDownloadId = 0;
       
   119     iCurrentBytes = 0;
       
   120     isDownloadPaused = EFalse;
       
   121     isDownloadComplete = EFalse;
       
   122     iCurrentSize = 0;
       
   123     iSourceSize = 0;
       
   124     iServerSourceExists = EFalse;
       
   125     return status;
       
   126     }
       
   127 
       
   128 TInt CClientProgDLSource::SetDownloadGateway(CDownloadGateway* aDownloadGateway)
       
   129     {
       
   130     iDownloadGateway = aDownloadGateway;
       
   131     return KErrNone;
       
   132     }
       
   133 
       
   134 
       
   135 TInt CClientProgDLSource::Open(const TDesC& aFileName,TInt32 aDLTxId )
       
   136     {
       
   137     TInt status(KErrNone);
       
   138     if(aDLTxId != KLocalFile)
       
   139         {
       
   140         status = iDownloadGateway->ConnectToDownload(aDLTxId,*this,KNullUid);
       
   141         if(status == KErrNone)
       
   142         	{
       
   143 		    iDownloadId = aDLTxId;
       
   144 		    isDownloadComplete = EFalse;
       
   145 	        //Gets current downloaded size
       
   146 	        status = iDownloadGateway->GetDownloadSize(iDownloadId,iSourceSize);
       
   147 	        RETURN_IF_ERROR(status);
       
   148 	    	}
       
   149 	    else
       
   150 	    	{
       
   151 	    	iDownloadId = 0;	
       
   152 	    	return status;	
       
   153 	    	}	
       
   154         }
       
   155     else
       
   156         {
       
   157         isDownloadComplete = ETrue;
       
   158         }        
       
   159     
       
   160     if(iFileName)
       
   161         {
       
   162         delete iFileName;
       
   163         iFileName = NULL;
       
   164         }
       
   165 
       
   166     TRAP(status,iFileName = HBufC::NewL(aFileName.Size()));
       
   167 	
       
   168 	if(!status)
       
   169 	    {
       
   170 	    TPtr des = iFileName->Des();
       
   171 	    des.Copy(aFileName);
       
   172 	    }    
       
   173     
       
   174     return status;
       
   175     }
       
   176 
       
   177 TInt CClientProgDLSource::MoveFile(const TDesC& aDestFileName )
       
   178     {
       
   179     TInt status(KErrNone);
       
   180     
       
   181     if (iServerSourceExists)
       
   182         iCustomCommand->CustomCommandSync( iSourceHandle, ESetFileMoving, KNullDesC8, KNullDesC8 );
       
   183 
       
   184     iDestFileName.Copy(aDestFileName);
       
   185     status = iDownloadGateway->MoveFile(iDownloadId,aDestFileName);
       
   186     return status;
       
   187     }
       
   188 
       
   189 TInt CClientProgDLSource::IsDownloadComplete(TBool& aBool)
       
   190     {
       
   191     TInt status(KErrNone);
       
   192     
       
   193     TDownloadStatus dlStatus;
       
   194     status = iDownloadGateway->GetDownloadStatus(iDownloadId,dlStatus);
       
   195     if(dlStatus == ECompleted)
       
   196         {
       
   197         aBool = true;
       
   198         }
       
   199     else
       
   200         {
       
   201         aBool = false;
       
   202         }
       
   203             
       
   204     return status;
       
   205     }
       
   206 
       
   207 TInt CClientProgDLSource::GetPercentageDownloaded(TUint& aPercent)
       
   208     {
       
   209     TInt status(KErrNone);
       
   210     TUint curSize;
       
   211     TUint fullSize;
       
   212     if(!isDownloadComplete)
       
   213         {
       
   214         status = iDownloadGateway->GetCurrentSize(iDownloadId,curSize);
       
   215         RETURN_IF_ERROR(status);
       
   216         status = iDownloadGateway->GetDownloadSize(iDownloadId,fullSize);    
       
   217         RETURN_IF_ERROR(status);
       
   218         }
       
   219     else
       
   220         {
       
   221         curSize = iCurrentSize;
       
   222         fullSize = iSourceSize;
       
   223         }
       
   224     
       
   225     if(fullSize)
       
   226         {
       
   227         aPercent = (curSize*100/fullSize);
       
   228         }
       
   229     else
       
   230         {
       
   231         aPercent = 0;
       
   232         }        
       
   233     return status;
       
   234     }
       
   235 
       
   236 TInt CClientProgDLSource::GetDownloadingRate(TUint& aRate)
       
   237     {
       
   238     TInt status(KErrNone);
       
   239     if(iMonitor)
       
   240         {
       
   241         aRate = iMonitor->TransferRate();
       
   242         }
       
   243     else
       
   244         {
       
   245         status = KErrNotFound;
       
   246         }
       
   247     return status;
       
   248     }    
       
   249 
       
   250 TInt CClientProgDLSource::GetCurrentFileSize( TUint& aSize )
       
   251     {
       
   252     TInt status(KErrNotFound);
       
   253     if(!isDownloadComplete)
       
   254         {
       
   255         status = iDownloadGateway->GetCurrentSize(iDownloadId,aSize);
       
   256         }
       
   257     else
       
   258         {
       
   259         iCurrentSize = iSourceSize;
       
   260         status = KErrNone;
       
   261         }
       
   262     return status;
       
   263     }
       
   264      
       
   265 TInt CClientProgDLSource::GetExpectedFileSize( TUint& aSize )
       
   266     {
       
   267     TInt status(KErrNotFound);
       
   268     if(!isDownloadComplete)
       
   269         {
       
   270         status = iDownloadGateway->GetDownloadSize(iDownloadId,aSize);
       
   271         }
       
   272     else
       
   273         {
       
   274         aSize = iSourceSize;
       
   275         status = KErrNone;
       
   276         }
       
   277     return status;
       
   278     }
       
   279     
       
   280 MProgDLSource::TDownloadStatus CClientProgDLSource::GetDownloadStatus()
       
   281     {
       
   282     MProgDLSource::TDownloadStatus dlStatus;
       
   283     iDownloadGateway->GetDownloadStatus(iDownloadId,dlStatus);
       
   284     return dlStatus;
       
   285     }
       
   286     
       
   287 
       
   288 TInt CClientProgDLSource::GetPercentageBuffered(TUint& aPercent)
       
   289     {
       
   290     //Source Custom Command
       
   291     TPckgBuf<TUint> pckg;
       
   292     if(iServerSourceExists)
       
   293         {
       
   294         iCustomCommand->CustomCommandSync( iSourceHandle, EGetPercentageBuffered, KNullDesC8, KNullDesC8, pckg );
       
   295         aPercent = pckg();
       
   296         return KErrNone;
       
   297         }
       
   298     else
       
   299         {
       
   300         aPercent = 0;
       
   301         return KErrNotFound;    
       
   302         }
       
   303     }            
       
   304 
       
   305 TInt CClientProgDLSource::GetBitRate(TUint& aRate)
       
   306     {
       
   307     //Source Custom Command
       
   308     TPckgBuf<TUint> pckg;
       
   309     if(iServerSourceExists)
       
   310         {
       
   311         iCustomCommand->CustomCommandSync( iSourceHandle, EGetBitRate, KNullDesC8, KNullDesC8, pckg );
       
   312         aRate = pckg();
       
   313         return KErrNone;
       
   314         }
       
   315     else
       
   316         {
       
   317         aRate = 0;
       
   318         return KErrNotFound;
       
   319         }
       
   320     }
       
   321 
       
   322 TInt CClientProgDLSource::FileName(TPtr& aFileName)
       
   323     {
       
   324     TInt status(KErrNone);
       
   325     aFileName.Set(iFileName->Des());
       
   326     return status;
       
   327     }
       
   328 
       
   329 
       
   330 TInt CClientProgDLSource::CancelDownload()
       
   331     {
       
   332     TInt status(KErrNone);
       
   333     status = iDownloadGateway->DeleteDownload(iDownloadId);
       
   334     return status;
       
   335     }
       
   336 
       
   337 TInt CClientProgDLSource::ResumeDownload()
       
   338     {
       
   339     TInt status(KErrNone);
       
   340     if(isDownloadPaused)
       
   341         {
       
   342         iDownloadGateway->ResumeDownload(iDownloadId);
       
   343         }
       
   344     return status;
       
   345     }    
       
   346 
       
   347 void CClientProgDLSource::ServerSourceCreated( MCustomCommand& aCustomCommand,
       
   348                                                    TMMFMessageDestination& aSourceHandle )
       
   349     {
       
   350     iServerSourceExists = ETrue;
       
   351     iCustomCommand = &aCustomCommand;
       
   352     iSourceHandle = aSourceHandle;
       
   353     
       
   354     if(!isDownloadComplete)
       
   355         {
       
   356         TRAPD(err1,iMonitor = CTransferRateMonitor::NewL(*this, KTranferRateMonitorFrequency, KFileSizeChangeDelta));
       
   357         if(!err1)
       
   358     	    {
       
   359     		iMonitor->Start();
       
   360     		}
       
   361     	
       
   362         // Send data to server source
       
   363         TPckgBuf<TInt> pckg(iSourceSize);
       
   364         iCustomCommand->CustomCommandSync( iSourceHandle, ESetActualSize, pckg, KNullDesC8 );
       
   365         
       
   366         iStateEvent->SetActive();
       
   367         iStatePckg() = iSourceState;	  
       
   368         iCustomCommand->CustomCommandAsync(
       
   369             iSourceHandle,
       
   370             (TInt)EGetSourceState,
       
   371             iStatePckg,
       
   372             KNullDesC8,
       
   373             iStatePckg,
       
   374             iStateEvent->iStatus);
       
   375             
       
   376          }
       
   377      else
       
   378         {
       
   379         iCustomCommand->CustomCommandSync( iSourceHandle, ESetDownloadState, KNullDesC8, KNullDesC8 );        
       
   380         }
       
   381     }
       
   382 
       
   383 void CClientProgDLSource::ServerSourceDeleted()
       
   384     {
       
   385 #ifdef _DEBUG
       
   386     RDebug::Print(_L("CClientProgDLSource::ServerSourceDeleted()"));    
       
   387 #endif    
       
   388     iServerSourceExists = EFalse;
       
   389     iCustomCommand = NULL;
       
   390     
       
   391     if(iMonitor)
       
   392         {
       
   393         iMonitor->Stop();
       
   394         }
       
   395     }
       
   396 
       
   397 TBool CClientProgDLSource::IsEncrypted()
       
   398     {
       
   399     return EFalse;
       
   400     }
       
   401     
       
   402 TUid CClientProgDLSource::GetSourceUid()
       
   403     {
       
   404     return KMmfProgDLSource;
       
   405     }
       
   406     
       
   407 TInt CClientProgDLSource::GetHeaderData(TPtr& /*aPtr*/)
       
   408     {
       
   409     return KErrNotSupported;
       
   410     }
       
   411     
       
   412 void CClientProgDLSource::MoveFileComplete(TInt aError)
       
   413     {
       
   414     if(iServerSourceExists)
       
   415         {
       
   416         TPtr des = iFileName->Des();
       
   417         if(aError == KErrNone)
       
   418             {
       
   419     	    des.Copy(iDestFileName);
       
   420             }
       
   421 	    else if (aError == KErrAlreadyExists)
       
   422             {
       
   423             iDownloadGateway->GetLocalFileName(iDownloadId,des);
       
   424             }
       
   425 	    iDestFileName.Copy(des);
       
   426         TPckgBuf<TFileName> pckg(iDestFileName);
       
   427         iCustomCommand->CustomCommandSync( iSourceHandle, ESetUpdateFileName, pckg, KNullDesC8 );
       
   428         }
       
   429     
       
   430     CErrorCode* event1 = new CErrorCode( aError );       
       
   431     iEventNotifier->Event(this,MSourceControlObserver::KFileMoveCompleteEvent,event1);
       
   432     }
       
   433 
       
   434 void CClientProgDLSource::Event(TDownloadStatus aStatus)
       
   435     {   
       
   436 #ifdef _DEBUG
       
   437     RDebug::Print(_L("CClientProgDLSource::Event DownloadStatus[%d]"),aStatus);
       
   438 #endif            
       
   439 
       
   440     switch(aStatus)
       
   441         {
       
   442             case ECompleted:
       
   443                 {
       
   444                 if(iMonitor)
       
   445                     {
       
   446                     iMonitor->Stop();	
       
   447                     }
       
   448                 TUint size(0);
       
   449                 GetCurrentFileSize(size);
       
   450                 TInt value = size;
       
   451                 TPckgBuf<TInt> pckg(value);
       
   452 				if(iServerSourceExists)
       
   453 				{
       
   454                 	if (iSourceState != CMultimediaDataSource::ESTOPPED)
       
   455                     	{
       
   456 	                    iCustomCommand->CustomCommandSync( iSourceHandle, ESetDownloadSize, pckg, KNullDesC8 );
       
   457     	                }
       
   458         	        iCustomCommand->CustomCommandSync( iSourceHandle, ESetDownloadState, KNullDesC8, KNullDesC8 );
       
   459 				}
       
   460                 isDownloadComplete = ETrue;               
       
   461                 break;                    
       
   462                 }
       
   463             case EStarted:
       
   464                 {
       
   465                 isDownloadPaused = EFalse;    
       
   466                 break;
       
   467                 }
       
   468             case EPaused:
       
   469                 {
       
   470                 isDownloadPaused = ETrue;
       
   471                 break;    
       
   472                 }
       
   473             case EDeleted:
       
   474                 {
       
   475                 if(iMonitor)
       
   476                     {
       
   477                     iMonitor->Stop();
       
   478                     }
       
   479                 break;
       
   480                 }
       
   481 
       
   482             case EUnknown:
       
   483                 break;
       
   484                 
       
   485         }
       
   486         //Send only Known Events
       
   487         if(aStatus != EUnknown)
       
   488             {
       
   489             CErrorCode* event1 = new CErrorCode( KErrNone );       
       
   490             iEventNotifier->Event(this,MSourceControlObserver::KDownloadStatusChangedEvent,event1); 
       
   491             }
       
   492     }
       
   493 
       
   494 TInt CClientProgDLSource::GetCurrentSize( TUint& aCurrentSize )
       
   495     {
       
   496     //RDebug::Print(_L("CClientProgDLSource::GetCurrentSize"));			
       
   497     TUint value(0);
       
   498     GetExpectedFileSize(value);
       
   499     
       
   500     GetCurrentFileSize(aCurrentSize);
       
   501     
       
   502     if(aCurrentSize - iCurrentBytes > ((KNofityAfterPercentage/100) * value)) //Currently checking for 5%
       
   503         {
       
   504             iCurrentBytes = aCurrentSize;
       
   505             CErrorCode* event1 = new CErrorCode( KErrNone );       
       
   506             iEventNotifier->Event(this,MSourceControlObserver::KPercentageDownloadedChangedEvent,event1);
       
   507         }
       
   508     
       
   509     iCurrentSize = aCurrentSize;
       
   510     
       
   511     if ( iServerSourceExists && iSourceState != CMultimediaDataSource::ESTOPPED)
       
   512         {
       
   513         // Send data to server source
       
   514         
       
   515         TPckgBuf<TInt> pckg(iCurrentSize);
       
   516         iCustomCommand->CustomCommandSync( iSourceHandle, ESetDownloadSize, pckg, KNullDesC8 );
       
   517         
       
   518         }			
       
   519 #ifdef _DEBUG
       
   520     RDebug::Print(_L("CClientProgDLSource::GetCurrentSize:[%d]"), aCurrentSize);	
       
   521 #endif    
       
   522     
       
   523     return KErrNone;			
       
   524     }
       
   525 
       
   526 void CClientProgDLSource::TransferRateChanged()
       
   527     {
       
   528 #ifdef _DEBUG    
       
   529     RDebug::Print(_L("CClientProgDLSource::TransferRateChanged:[%d]"), iMonitor->TransferRate());
       
   530 #endif    
       
   531     if ( iServerSourceExists  && !isDownloadComplete && iSourceState != CMultimediaDataSource::ESTOPPED)
       
   532         {
       
   533         // Send data to server source
       
   534         
       
   535         TPckgBuf<TUint> pckg1(iMonitor->TransferRate());
       
   536         iCustomCommand->CustomCommandSync( iSourceHandle, EDownloadRate, pckg1, KNullDesC8 );
       
   537         
       
   538         }			
       
   539     }
       
   540 
       
   541 void CClientProgDLSource::SourceStateChanged()		
       
   542     {
       
   543 #ifdef _DEBUG
       
   544     RDebug::Print(_L("CClientProgDLSource::SourceStateChanged() :PrevState[%d] NewState[%d]"), iSourceState,iStatePckg());
       
   545 #endif    
       
   546     iSourceState = iStatePckg();
       
   547     
       
   548     if(iSourceState == CMultimediaDataSource::ESTOPPED || iSourceState == CMultimediaDataSource::ECLOSED)        
       
   549         {
       
   550         //iDownloadGateway->SetProgressiveMode(iDownloadId,EFalse);
       
   551         iProgressiveMode = EFalse;
       
   552         }
       
   553     else if(!iProgressiveMode && !isDownloadComplete)
       
   554         {
       
   555         iDownloadGateway->SetProgressiveMode(iDownloadId,ETrue);
       
   556         TPtr ptrFileName = iFileName->Des();
       
   557         iDownloadGateway->SetLocalFileName( iDownloadId, ptrFileName );
       
   558         iProgressiveMode = ETrue;
       
   559         }
       
   560     
       
   561         
       
   562     if(iStateEvent->Error() != KErrDied && iServerSourceExists)
       
   563         {
       
   564         iStateEvent->SetActive();	    
       
   565         iCustomCommand->CustomCommandAsync(
       
   566             iSourceHandle,
       
   567             (TInt)EGetSourceState,
       
   568             iStatePckg,
       
   569             KNullDesC8,
       
   570             iStatePckg,
       
   571             iStateEvent->iStatus);    
       
   572         }                               
       
   573     }
       
   574 // End of file