mmfenh/enhancedmediaclient/Client/src/Components/ClientProgDLSource/ClientProgDLSource.cpp
changeset 16 43d09473c595
parent 14 80975da52420
child 22 128eb6a32b84
equal deleted inserted replaced
14:80975da52420 16:43d09473c595
     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     iDestFileName.Copy(aDestFileName);
       
   181     status = iDownloadGateway->MoveFile(iDownloadId,aDestFileName);
       
   182     return status;
       
   183     }
       
   184 
       
   185 TInt CClientProgDLSource::IsDownloadComplete(TBool& aBool)
       
   186     {
       
   187     TInt status(KErrNone);
       
   188     
       
   189     TDownloadStatus dlStatus;
       
   190     status = iDownloadGateway->GetDownloadStatus(iDownloadId,dlStatus);
       
   191     if(dlStatus == ECompleted)
       
   192         {
       
   193         aBool = true;
       
   194         }
       
   195     else
       
   196         {
       
   197         aBool = false;
       
   198         }
       
   199             
       
   200     return status;
       
   201     }
       
   202 
       
   203 TInt CClientProgDLSource::GetPercentageDownloaded(TUint& aPercent)
       
   204     {
       
   205     TInt status(KErrNone);
       
   206     TUint curSize;
       
   207     TUint fullSize;
       
   208     if(!isDownloadComplete)
       
   209         {
       
   210         status = iDownloadGateway->GetCurrentSize(iDownloadId,curSize);
       
   211         RETURN_IF_ERROR(status);
       
   212         status = iDownloadGateway->GetDownloadSize(iDownloadId,fullSize);    
       
   213         RETURN_IF_ERROR(status);
       
   214         }
       
   215     else
       
   216         {
       
   217         curSize = iCurrentSize;
       
   218         fullSize = iSourceSize;
       
   219         }
       
   220     
       
   221     if(fullSize)
       
   222         {
       
   223         aPercent = (curSize*100/fullSize);
       
   224         }
       
   225     else
       
   226         {
       
   227         aPercent = 0;
       
   228         }        
       
   229     return status;
       
   230     }
       
   231 
       
   232 TInt CClientProgDLSource::GetDownloadingRate(TUint& aRate)
       
   233     {
       
   234     TInt status(KErrNone);
       
   235     if(iMonitor)
       
   236         {
       
   237         aRate = iMonitor->TransferRate();
       
   238         }
       
   239     else
       
   240         {
       
   241         status = KErrNotFound;
       
   242         }
       
   243     return status;
       
   244     }    
       
   245 
       
   246 TInt CClientProgDLSource::GetCurrentFileSize( TUint& aSize )
       
   247     {
       
   248     TInt status(KErrNotFound);
       
   249     if(!isDownloadComplete)
       
   250         {
       
   251         status = iDownloadGateway->GetCurrentSize(iDownloadId,aSize);
       
   252         }
       
   253     else
       
   254         {
       
   255         iCurrentSize = iSourceSize;
       
   256         status = KErrNone;
       
   257         }
       
   258     return status;
       
   259     }
       
   260      
       
   261 TInt CClientProgDLSource::GetExpectedFileSize( TUint& aSize )
       
   262     {
       
   263     TInt status(KErrNotFound);
       
   264     if(!isDownloadComplete)
       
   265         {
       
   266         status = iDownloadGateway->GetDownloadSize(iDownloadId,aSize);
       
   267         }
       
   268     else
       
   269         {
       
   270         aSize = iSourceSize;
       
   271         status = KErrNone;
       
   272         }
       
   273     return status;
       
   274     }
       
   275     
       
   276 MProgDLSource::TDownloadStatus CClientProgDLSource::GetDownloadStatus()
       
   277     {
       
   278     MProgDLSource::TDownloadStatus dlStatus;
       
   279     iDownloadGateway->GetDownloadStatus(iDownloadId,dlStatus);
       
   280     return dlStatus;
       
   281     }
       
   282     
       
   283 
       
   284 TInt CClientProgDLSource::GetPercentageBuffered(TUint& aPercent)
       
   285     {
       
   286     //Source Custom Command
       
   287     TPckgBuf<TUint> pckg;
       
   288     if(iServerSourceExists)
       
   289         {
       
   290         iCustomCommand->CustomCommandSync( iSourceHandle, EGetPercentageBuffered, KNullDesC8, KNullDesC8, pckg );
       
   291         aPercent = pckg();
       
   292         return KErrNone;
       
   293         }
       
   294     else
       
   295         {
       
   296         aPercent = 0;
       
   297         return KErrNotFound;    
       
   298         }
       
   299     }            
       
   300 
       
   301 TInt CClientProgDLSource::GetBitRate(TUint& aRate)
       
   302     {
       
   303     //Source Custom Command
       
   304     TPckgBuf<TUint> pckg;
       
   305     if(iServerSourceExists)
       
   306         {
       
   307         iCustomCommand->CustomCommandSync( iSourceHandle, EGetBitRate, KNullDesC8, KNullDesC8, pckg );
       
   308         aRate = pckg();
       
   309         return KErrNone;
       
   310         }
       
   311     else
       
   312         {
       
   313         aRate = 0;
       
   314         return KErrNotFound;
       
   315         }
       
   316     }
       
   317 
       
   318 TInt CClientProgDLSource::FileName(TPtr& aFileName)
       
   319     {
       
   320     TInt status(KErrNone);
       
   321     aFileName.Set(iFileName->Des());
       
   322     return status;
       
   323     }
       
   324 
       
   325 
       
   326 TInt CClientProgDLSource::CancelDownload()
       
   327     {
       
   328     TInt status(KErrNone);
       
   329     status = iDownloadGateway->DeleteDownload(iDownloadId);
       
   330     return status;
       
   331     }
       
   332 
       
   333 TInt CClientProgDLSource::ResumeDownload()
       
   334     {
       
   335     TInt status(KErrNone);
       
   336     if(isDownloadPaused)
       
   337         {
       
   338         iDownloadGateway->ResumeDownload(iDownloadId);
       
   339         }
       
   340     return status;
       
   341     }    
       
   342 
       
   343 void CClientProgDLSource::ServerSourceCreated( MCustomCommand& aCustomCommand,
       
   344                                                    TMMFMessageDestination& aSourceHandle )
       
   345     {
       
   346     iServerSourceExists = ETrue;
       
   347     iCustomCommand = &aCustomCommand;
       
   348     iSourceHandle = aSourceHandle;
       
   349     
       
   350     if(!isDownloadComplete)
       
   351         {
       
   352         TRAPD(err1,iMonitor = CTransferRateMonitor::NewL(*this, KTranferRateMonitorFrequency, KFileSizeChangeDelta));
       
   353         if(!err1)
       
   354     	    {
       
   355     		iMonitor->Start();
       
   356     		}
       
   357     	
       
   358         // Send data to server source
       
   359         TPckgBuf<TInt> pckg(iSourceSize);
       
   360         iCustomCommand->CustomCommandSync( iSourceHandle, ESetActualSize, pckg, KNullDesC8 );
       
   361         
       
   362         iStateEvent->SetActive();
       
   363         iStatePckg() = iSourceState;	  
       
   364         iCustomCommand->CustomCommandAsync(
       
   365             iSourceHandle,
       
   366             (TInt)EGetSourceState,
       
   367             iStatePckg,
       
   368             KNullDesC8,
       
   369             iStatePckg,
       
   370             iStateEvent->iStatus);
       
   371             
       
   372          }
       
   373      else
       
   374         {
       
   375         iCustomCommand->CustomCommandSync( iSourceHandle, ESetDownloadState, KNullDesC8, KNullDesC8 );        
       
   376         }
       
   377     }
       
   378 
       
   379 void CClientProgDLSource::ServerSourceDeleted()
       
   380     {
       
   381 #ifdef _DEBUG
       
   382     RDebug::Print(_L("CClientProgDLSource::ServerSourceDeleted()"));    
       
   383 #endif    
       
   384     iServerSourceExists = EFalse;
       
   385     iCustomCommand = NULL;
       
   386     
       
   387     if(iMonitor)
       
   388         {
       
   389         iMonitor->Stop();
       
   390         }
       
   391     }
       
   392 
       
   393 TBool CClientProgDLSource::IsEncrypted()
       
   394     {
       
   395     return EFalse;
       
   396     }
       
   397     
       
   398 TUid CClientProgDLSource::GetSourceUid()
       
   399     {
       
   400     return KMmfProgDLSource;
       
   401     }
       
   402     
       
   403 TInt CClientProgDLSource::GetHeaderData(TPtr& /*aPtr*/)
       
   404     {
       
   405     return KErrNotSupported;
       
   406     }
       
   407     
       
   408 void CClientProgDLSource::MoveFileComplete(TInt aError)
       
   409     {
       
   410     if(iServerSourceExists)
       
   411         {
       
   412         TPtr des = iFileName->Des();
       
   413         if(aError == KErrNone)
       
   414             {
       
   415     	    des.Copy(iDestFileName);
       
   416             }
       
   417 	    else
       
   418             {
       
   419             iDownloadGateway->GetLocalFileName(iDownloadId,des);
       
   420             }
       
   421         TPckgBuf<TFileName> pckg(iDestFileName);
       
   422         iCustomCommand->CustomCommandSync( iSourceHandle, ESetUpdateFileName, pckg, KNullDesC8 );
       
   423         }
       
   424     
       
   425     CErrorCode* event1 = new CErrorCode( aError );       
       
   426     iEventNotifier->Event(this,MSourceControlObserver::KFileMoveCompleteEvent,event1);
       
   427     }
       
   428 
       
   429 void CClientProgDLSource::Event(TDownloadStatus aStatus)
       
   430     {   
       
   431 #ifdef _DEBUG
       
   432     RDebug::Print(_L("CClientProgDLSource::Event DownloadStatus[%d]"),aStatus);
       
   433 #endif            
       
   434 
       
   435     switch(aStatus)
       
   436         {
       
   437             case ECompleted:
       
   438                 {
       
   439                 if(iMonitor)
       
   440                     {
       
   441                     iMonitor->Stop();	
       
   442                     }
       
   443                 TUint size(0);
       
   444                 GetCurrentFileSize(size);
       
   445                 TInt value = size;
       
   446                 TPckgBuf<TInt> pckg(value);
       
   447 				if(iServerSourceExists)
       
   448 				{
       
   449                 	if (iSourceState != CMultimediaDataSource::ESTOPPED)
       
   450                     	{
       
   451 	                    iCustomCommand->CustomCommandSync( iSourceHandle, ESetDownloadSize, pckg, KNullDesC8 );
       
   452     	                }
       
   453         	        iCustomCommand->CustomCommandSync( iSourceHandle, ESetDownloadState, KNullDesC8, KNullDesC8 );
       
   454 				}
       
   455                 isDownloadComplete = ETrue;               
       
   456                 break;                    
       
   457                 }
       
   458             case EStarted:
       
   459                 {
       
   460                 isDownloadPaused = EFalse;    
       
   461                 break;
       
   462                 }
       
   463             case EPaused:
       
   464                 {
       
   465                 isDownloadPaused = ETrue;
       
   466                 break;    
       
   467                 }
       
   468             case EDeleted:
       
   469                 {
       
   470                 if(iMonitor)
       
   471                     {
       
   472                     iMonitor->Stop();
       
   473                     }
       
   474                 break;
       
   475                 }
       
   476 
       
   477             case EUnknown:
       
   478                 break;
       
   479                 
       
   480         }
       
   481         //Send only Known Events
       
   482         if(aStatus != EUnknown)
       
   483             {
       
   484             CErrorCode* event1 = new CErrorCode( KErrNone );       
       
   485             iEventNotifier->Event(this,MSourceControlObserver::KDownloadStatusChangedEvent,event1); 
       
   486             }
       
   487     }
       
   488 
       
   489 TInt CClientProgDLSource::GetCurrentSize( TUint& aCurrentSize )
       
   490     {
       
   491     //RDebug::Print(_L("CClientProgDLSource::GetCurrentSize"));			
       
   492     TUint value(0);
       
   493     GetExpectedFileSize(value);
       
   494     
       
   495     GetCurrentFileSize(aCurrentSize);
       
   496     
       
   497     if(aCurrentSize - iCurrentBytes > ((KNofityAfterPercentage/100) * value)) //Currently checking for 5%
       
   498         {
       
   499             iCurrentBytes = aCurrentSize;
       
   500             CErrorCode* event1 = new CErrorCode( KErrNone );       
       
   501             iEventNotifier->Event(this,MSourceControlObserver::KPercentageDownloadedChangedEvent,event1);
       
   502         }
       
   503     
       
   504     iCurrentSize = aCurrentSize;
       
   505     
       
   506     if ( iServerSourceExists && iSourceState != CMultimediaDataSource::ESTOPPED)
       
   507         {
       
   508         // Send data to server source
       
   509         
       
   510         TPckgBuf<TInt> pckg(iCurrentSize);
       
   511         iCustomCommand->CustomCommandSync( iSourceHandle, ESetDownloadSize, pckg, KNullDesC8 );
       
   512         
       
   513         }			
       
   514 #ifdef _DEBUG
       
   515     RDebug::Print(_L("CClientProgDLSource::GetCurrentSize:[%d]"), aCurrentSize);	
       
   516 #endif    
       
   517     
       
   518     return KErrNone;			
       
   519     }
       
   520 
       
   521 void CClientProgDLSource::TransferRateChanged()
       
   522     {
       
   523 #ifdef _DEBUG    
       
   524     RDebug::Print(_L("CClientProgDLSource::TransferRateChanged:[%d]"), iMonitor->TransferRate());
       
   525 #endif    
       
   526     if ( iServerSourceExists  && !isDownloadComplete && iSourceState != CMultimediaDataSource::ESTOPPED)
       
   527         {
       
   528         // Send data to server source
       
   529         
       
   530         TPckgBuf<TUint> pckg1(iMonitor->TransferRate());
       
   531         iCustomCommand->CustomCommandSync( iSourceHandle, EDownloadRate, pckg1, KNullDesC8 );
       
   532         
       
   533         }			
       
   534     }
       
   535 
       
   536 void CClientProgDLSource::SourceStateChanged()		
       
   537     {
       
   538 #ifdef _DEBUG
       
   539     RDebug::Print(_L("CClientProgDLSource::SourceStateChanged() :PrevState[%d] NewState[%d]"), iSourceState,iStatePckg());
       
   540 #endif    
       
   541     iSourceState = iStatePckg();
       
   542     
       
   543     if(iSourceState == CMultimediaDataSource::ESTOPPED || iSourceState == CMultimediaDataSource::ECLOSED)        
       
   544         {
       
   545         //iDownloadGateway->SetProgressiveMode(iDownloadId,EFalse);
       
   546         iProgressiveMode = EFalse;
       
   547         }
       
   548     else if(!iProgressiveMode && !isDownloadComplete)
       
   549         {
       
   550         iDownloadGateway->SetProgressiveMode(iDownloadId,ETrue);
       
   551         TPtr ptrFileName = iFileName->Des();
       
   552         iDownloadGateway->SetLocalFileName( iDownloadId, ptrFileName );
       
   553         iProgressiveMode = ETrue;
       
   554         }
       
   555     
       
   556         
       
   557     if(iStateEvent->Error() != KErrDied && iServerSourceExists)
       
   558         {
       
   559         iStateEvent->SetActive();	    
       
   560         iCustomCommand->CustomCommandAsync(
       
   561             iSourceHandle,
       
   562             (TInt)EGetSourceState,
       
   563             iStatePckg,
       
   564             KNullDesC8,
       
   565             iStatePckg,
       
   566             iStateEvent->iStatus);    
       
   567         }                               
       
   568     }
       
   569 // End of file