browserui/browser/BrowserAppSrc/BrowserProgressIndicator.cpp
branchRCL_3
changeset 65 8e6fa1719340
equal deleted inserted replaced
64:6385c4c93049 65:8e6fa1719340
       
     1 /*
       
     2 * Copyright (c) 2002 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 the License "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 
       
    20 // INCLUDE FILES
       
    21 
       
    22 #include <AknAppUi.h>
       
    23 #include <e32math.h>
       
    24 #include <BrowserNG.rsg>
       
    25 #include <FeatMgr.h>
       
    26 #include <stringloader.h>
       
    27 
       
    28 // USER INCLUDES
       
    29 
       
    30 #include "Display.h"
       
    31 #include "ApiProvider.h"
       
    32 #include "CommonConstants.h"  // for View Ids
       
    33 #include "BrowserProgressIndicator.h"
       
    34 #include "Logger.h"
       
    35 #include "AknUtils.h"
       
    36 
       
    37 // CONSTANTS
       
    38 
       
    39 const TInt KGranularity = 4;
       
    40 const TUint KkBLimit = 999;
       
    41 const TUint KOnekB = 1024;
       
    42 const TUint KOneMB = 1024 * 1024;
       
    43 const TInt KMaxPercentage = 100;
       
    44 const TInt KMBLimit = 10;
       
    45 const TInt KMaxMBLimit = 100;
       
    46 _LIT(text,"(%S) %S ");
       
    47 _LIT(twoDecimals,"%2.2lf");
       
    48 _LIT(oneDecimal, "%2.1lf");
       
    49 _LIT(noDecimals, "%d");
       
    50 
       
    51 // ================= MEMBER FUNCTIONS =======================
       
    52 
       
    53 // ---------------------------------------------------------
       
    54 // CBrowserProgressIndicator::NewL
       
    55 // ---------------------------------------------------------
       
    56 //
       
    57 CBrowserProgressIndicator* CBrowserProgressIndicator::NewL(
       
    58         MApiProvider& aApiProvider )
       
    59 	{
       
    60 	CBrowserProgressIndicator* self = new (ELeave)
       
    61 	    CBrowserProgressIndicator( aApiProvider );
       
    62     CleanupStack::PushL( self );
       
    63     self->ConstructL();
       
    64     CleanupStack::Pop();
       
    65     return self;
       
    66 	}
       
    67 
       
    68 // ---------------------------------------------------------
       
    69 // CBrowserProgressIndicator::ConstructL
       
    70 // ---------------------------------------------------------
       
    71 //
       
    72 void CBrowserProgressIndicator::ConstructL()
       
    73     {
       
    74     iProgressData = new(ELeave) CArrayFixFlat<TProgressData>( KGranularity );
       
    75     iInitialDownloadIndicatorState = EFalse;
       
    76 
       
    77     HBufC* myKb = StringLoader::LoadL( R_WML_UNIT_KB );
       
    78     iMyKb = myKb->Alloc();
       
    79     delete myKb;
       
    80 
       
    81     HBufC* myMb = StringLoader::LoadL( R_WML_UNIT_MB );
       
    82     iMyMb = myMb->Alloc();
       
    83     delete myMb;
       
    84     }
       
    85 
       
    86 // ---------------------------------------------------------
       
    87 // CBrowserProgressIndicator::CBrowserProgressIndicator
       
    88 // ---------------------------------------------------------
       
    89 //
       
    90 CBrowserProgressIndicator::CBrowserProgressIndicator(
       
    91         MApiProvider& aApiProvider ) : 
       
    92     iIsRunning( EFalse ),
       
    93     iApiProvider( aApiProvider ),
       
    94     iLastMaxEstimate( 0 ),
       
    95     iLastReceivedData( 0 )
       
    96 	{
       
    97 	}
       
    98 
       
    99 // ---------------------------------------------------------
       
   100 // CBrowserProgressIndicator::~CBrowserProgressIndicator
       
   101 // ---------------------------------------------------------
       
   102 //
       
   103 CBrowserProgressIndicator::~CBrowserProgressIndicator()
       
   104 	{
       
   105     delete iProgressData;
       
   106     delete iMyKb;
       
   107     delete iMyMb;
       
   108 	}
       
   109 
       
   110 // ---------------------------------------------------------
       
   111 // CBrowserProgressIndicator::AddTransActIdL
       
   112 // ---------------------------------------------------------
       
   113 //
       
   114 void CBrowserProgressIndicator::AddTransActIdL( TUint16 aId )
       
   115 	{
       
   116     //Format each id at start point.
       
   117     AddProgressDataL(aId, 0, 0);
       
   118 	}
       
   119 
       
   120 // ---------------------------------------------------------
       
   121 // CBrowserProgressIndicator::DeleteProgressDataItem
       
   122 // ---------------------------------------------------------
       
   123 //
       
   124 void CBrowserProgressIndicator::DeleteProgressDataItem( TUint16 aId )
       
   125 	{
       
   126     TKeyArrayFix key(0, ECmpTUint16);	
       
   127 	TInt pos = 0;
       
   128 	TProgressData data;
       
   129 	data.iId = aId;
       
   130 	TInt retVal = iProgressData->Find( data,key,pos );
       
   131     if ( retVal == KErrNone )
       
   132         {
       
   133         iProgressData->Delete( pos );
       
   134         }		
       
   135 
       
   136 	}
       
   137 
       
   138 // ---------------------------------------------------------
       
   139 // CBrowserProgressIndicator::TransActIdAmount()
       
   140 // ---------------------------------------------------------
       
   141 //
       
   142 TInt CBrowserProgressIndicator::TransActIdAmount() const
       
   143 	{
       
   144     return iProgressData->Count();
       
   145 	}
       
   146 
       
   147 // ---------------------------------------------------------
       
   148 // CBrowserProgressIndicator::TransActIdAmountCompleted()
       
   149 // ---------------------------------------------------------
       
   150 //
       
   151 TInt CBrowserProgressIndicator::TransActIdAmountCompleted() const
       
   152     {
       
   153     TInt numOfCompleted( 0 );
       
   154     TInt i;
       
   155     for( i=0; i<TransActIdAmount(); ++i )
       
   156     {
       
   157         if( iProgressData->At( i ).iComplete )
       
   158             ++numOfCompleted;
       
   159     }
       
   160     return numOfCompleted;
       
   161     }
       
   162 
       
   163 // ---------------------------------------------------------
       
   164 // CBrowserProgressIndicator::ResetValues()
       
   165 // ---------------------------------------------------------
       
   166 //
       
   167 void CBrowserProgressIndicator::ResetValues()
       
   168 	{
       
   169 	iProgressData->Reset();
       
   170     iProgressEstimate = 0;
       
   171 	}
       
   172 
       
   173 // ---------------------------------------------------------
       
   174 // CBrowserProgressIndicator::ReceivedData()
       
   175 // ---------------------------------------------------------
       
   176 //
       
   177 TUint32 CBrowserProgressIndicator::ReceivedData()
       
   178 	{
       
   179 	TUint32 recvdData = 0;
       
   180 	for (TInt i = 0; i < iProgressData->Count();i++)
       
   181 		{
       
   182 		TProgressData data = iProgressData->At( i );
       
   183 		recvdData += data.iRecvdData;
       
   184 		}
       
   185 	return recvdData;
       
   186 	}
       
   187 
       
   188 // ---------------------------------------------------------
       
   189 // CBrowserProgressIndicator::EstimateMaxData()
       
   190 // ---------------------------------------------------------
       
   191 //
       
   192 TUint32 CBrowserProgressIndicator::EstimateMaxData()
       
   193 	{
       
   194 	TUint32 maxData = 0;
       
   195     TInt count = 0;
       
   196     TUint32 estimation = 0;
       
   197 	for ( TInt i = 0; i < iProgressData->Count();i++ )
       
   198 		{
       
   199 		TProgressData data = iProgressData->At( i );
       
   200         if( data.iMaxData )
       
   201             {
       
   202             maxData += data.iMaxData;
       
   203             count++;
       
   204             }
       
   205 		}
       
   206      // estimate maximum data what will be received
       
   207      if ( count )
       
   208          {
       
   209          return estimation = ((maxData/count)* iProgressData->Count());
       
   210          }
       
   211 
       
   212 	return estimation;
       
   213 
       
   214 	}
       
   215 
       
   216 // ---------------------------------------------------------
       
   217 // CBrowserProgressIndicator::TransActId()
       
   218 // ---------------------------------------------------------
       
   219 //
       
   220 TUint16 CBrowserProgressIndicator::TransActId( TInt aIndex )
       
   221 	{
       
   222     return iProgressData->At( aIndex ).iId;
       
   223 	}
       
   224 
       
   225 // ---------------------------------------------------------
       
   226 // CBrowserProgressIndicator::AddProgressDataL()
       
   227 // ---------------------------------------------------------
       
   228 //
       
   229 void CBrowserProgressIndicator::AddProgressDataL( 
       
   230                      TUint16 aId, TUint32 aRecvdData, TUint32 aMaxData )
       
   231 	{
       
   232 	//Try to seek if current id exist in array
       
   233 	TKeyArrayFix key(0, ECmpTUint16);
       
   234 	TInt pos = 0;
       
   235 	TProgressData data;
       
   236 	data.iId = aId;
       
   237 	TInt retVal = iProgressData->Find( data,key,pos );
       
   238 	if ( retVal!=KErrNone ) //id was NOT found
       
   239 		{
       
   240 		//Add id, recvdData and maxData to array
       
   241 		TProgressData data;
       
   242 		data.iId = aId;
       
   243 		data.iRecvdData = aRecvdData;		
       
   244         data.iMaxData = aMaxData;
       
   245         data.iComplete = EFalse;
       
   246         iProgressData->AppendL( data );
       
   247 		}
       
   248 	else
       
   249 		{
       
   250 
       
   251         data = iProgressData->At( pos );
       
   252 		if( ((data.iRecvdData != aRecvdData) && aRecvdData!=0 ) )
       
   253 			{
       
   254 			data.iRecvdData = aRecvdData;
       
   255             }
       
   256         if ( aMaxData!=0 ) 
       
   257             {
       
   258             data.iMaxData = aMaxData;
       
   259             }
       
   260 
       
   261         iProgressData->Delete( pos );
       
   262         iProgressData->InsertL( pos,data );	
       
   263 		}
       
   264 	}
       
   265 
       
   266 
       
   267 // ---------------------------------------------------------
       
   268 // CBrowserProgressIndicator::TransActIdCompleteL()
       
   269 // ---------------------------------------------------------
       
   270 //
       
   271 void CBrowserProgressIndicator::TransActIdCompleteL( TUint16 aId )
       
   272     {
       
   273     TKeyArrayFix key( 0, ECmpTUint16 );	
       
   274 	TInt pos = 0;
       
   275 	TProgressData data;
       
   276 	data.iId = aId;
       
   277 	TInt retVal = iProgressData->Find( data,key,pos );
       
   278     if ( retVal == KErrNone && ! iProgressData->At(pos).iComplete )
       
   279         {
       
   280         data = iProgressData->At( pos );
       
   281         // we don't need this id anymore if both data values are empty
       
   282         if ( !data.iMaxData && !data.iRecvdData )
       
   283             {
       
   284             iProgressData->Delete( pos );
       
   285             }
       
   286         else 
       
   287             {
       
   288             data.iComplete = ETrue;
       
   289             if ( data.iMaxData > data.iRecvdData )
       
   290                 {
       
   291                 data.iRecvdData = data.iMaxData;
       
   292                 }
       
   293             iProgressData->Delete( pos );
       
   294             iProgressData->InsertL( pos,data );
       
   295             }
       
   296        }
       
   297     }
       
   298 
       
   299 // ---------------------------------------------------------
       
   300 // CBrowserProgressIndicator::EnquireStatusL()
       
   301 // ---------------------------------------------------------
       
   302 //
       
   303 void CBrowserProgressIndicator::EnquireStatusL()
       
   304 	{
       
   305 LOG_ENTERFN("EnquireStatusL");
       
   306     // Reset string storage.
       
   307 	iPercentText.SetLength( 0 );
       
   308 	iDataText.SetLength( 0 );
       
   309     iMBvalue.SetLength( 0 );
       
   310 
       
   311     TUint32 receivedData = ReceivedData();
       
   312     TUint32 maxEstimate = EstimateMaxData();
       
   313     
       
   314     if (receivedData == 0 || maxEstimate == 0)
       
   315         {
       
   316         // fake initial progress to reflect 5% downloaded and 0 kb received. 
       
   317         receivedData    =  5;
       
   318         maxEstimate     =  100;
       
   319         }
       
   320 
       
   321     //Calculate either kB-text or MB-text
       
   322     if ( ((receivedData)/KOnekB ) <= KkBLimit )
       
   323         {
       
   324         HBufC* kbUnit = HBufC::NewL( iMyKb->Length()+5 );
       
   325         TPtr ptr1( kbUnit->Des() );
       
   326         StringLoader::Format( ptr1, *iMyKb, -1, (receivedData)/KOnekB );
       
   327         AknTextUtils::DisplayTextLanguageSpecificNumberConversion(ptr1);
       
   328         iDataText.Copy( ptr1 );
       
   329         delete kbUnit;
       
   330         }
       
   331     else
       
   332         {
       
   333         
       
   334         TReal received( receivedData );
       
   335         TReal oneMB( KOneMB );
       
   336         TReal result( received / oneMB );
       
   337         // TUint32 result( receivedData / KOneMB );
       
   338         if ( result < KMBLimit )
       
   339             {
       
   340             // TUint32 res2( ( ( receivedData * 100 ) / KOneMB ) % 100 );
       
   341             // need to modify the format string too
       
   342             iMBvalue.Format( twoDecimals, result );
       
   343             }
       
   344         else if ( result < KMaxMBLimit )
       
   345             {
       
   346             // TUint32 res2( ( ( receivedData * 10 ) / KOneMB ) % 10 );
       
   347             // need to modify the format string too
       
   348             iMBvalue.Format( oneDecimal, result );
       
   349             }
       
   350         else if ( result > KMaxMBLimit)
       
   351             {
       
   352             TInt16 resultInt( 0 );
       
   353             Math::Int( resultInt, result );
       
   354             iMBvalue.Format( noDecimals, resultInt );
       
   355             }
       
   356         HBufC* mbUnit = HBufC::NewL( iMyMb->Length() + iMBvalue.Length() + 1 );
       
   357         TPtr ptr1( mbUnit->Des() );
       
   358         StringLoader::Format( ptr1, *iMyMb, -1, iMBvalue );
       
   359         AknTextUtils::DisplayTextLanguageSpecificNumberConversion(ptr1);
       
   360         iDataText.Copy( ptr1 );
       
   361         delete mbUnit;
       
   362         }
       
   363        
       
   364     // progress animation should be shown only in content view (not in bookmarks)
       
   365     if( iApiProvider.LastActiveViewId() == KUidBrowserContentViewId )
       
   366         {
       
   367         // Check all possible states.
       
   368         if ( !iInitialDownloadIndicatorState && maxEstimate <= 0 )
       
   369             {
       
   370             iInitialDownloadIndicatorState = ETrue;
       
   371             // The initial indicator state changed from EFalse to ETrue. Update it.
       
   372             iApiProvider.Display().UpdateFSDownloadInitialIndicator( ETrue );
       
   373             }
       
   374         else if ( !iInitialDownloadIndicatorState && 0 < maxEstimate )
       
   375             {
       
   376             //iInitialDownloadIndicatorState = EFalse; // Unnecessary statement.
       
   377             }
       
   378         else if ( iInitialDownloadIndicatorState && maxEstimate <= 0 )
       
   379             {
       
   380             //iInitialDownloadIndicatorState = ETrue; // Unnecessary statement.
       
   381             }
       
   382         else if ( iInitialDownloadIndicatorState && 0 < maxEstimate )
       
   383             {
       
   384             iInitialDownloadIndicatorState = EFalse;
       
   385             // The initial indicator state changed from ETrue to EFalse. Update it.
       
   386             iApiProvider.Display().UpdateFSDownloadInitialIndicator( EFalse );
       
   387             }
       
   388 
       
   389         iApiProvider.Display().UpdateFSProgressIndicator( maxEstimate, receivedData );
       
   390 
       
   391         // Render the downloaded data size.
       
   392         iApiProvider.Display().UpdateFSProgressDataL( iDataText );
       
   393         }
       
   394     else
       
   395         {
       
   396         // is this section really run?
       
   397 
       
   398         // Show how many percentage has been dowloaded
       
   399         if ( maxEstimate )
       
   400             {
       
   401             TReal received( receivedData );
       
   402             TReal maxEst( maxEstimate );
       
   403             TReal factor( 100 );
       
   404             TReal percentage( (received / maxEst) * factor );
       
   405             TInt16 percentInt( 0 );
       
   406             Math::Int( percentInt, percentage );
       
   407             if ( percentInt > KMaxPercentage )
       
   408                 {
       
   409                 percentInt = KMaxPercentage;
       
   410                 }
       
   411             HBufC* percentUnit = 
       
   412                 StringLoader::LoadLC( R_WML_UNIT_PERCENT, percentInt );
       
   413             iPercentText.Format( text, percentUnit, &iDataText );
       
   414             CleanupStack::PopAndDestroy( percentUnit ); //percentUnit
       
   415 
       
   416             //Update navi pane
       
   417             iApiProvider.UpdateNaviPaneL( iPercentText );
       
   418             }
       
   419         //Show amount of downloaded data (recvd data)
       
   420         //(if maximum data is not available)
       
   421         else
       
   422             {
       
   423             iApiProvider.UpdateNaviPaneL( iDataText );
       
   424             }
       
   425         }
       
   426 	}
       
   427 
       
   428 // ---------------------------------------------------------
       
   429 // CBrowserProgressIndicator::StartL()
       
   430 // ---------------------------------------------------------
       
   431 //
       
   432 void CBrowserProgressIndicator::StartL()
       
   433     {
       
   434     if ( !iIsRunning )
       
   435         {
       
   436         // clear progress bar data
       
   437         iLastMaxEstimate = 0;
       
   438         iLastReceivedData = 0;
       
   439                     
       
   440         // progress animation should be shown 
       
   441         // only in content view (not in bookmarks)
       
   442         if( iApiProvider.LastActiveViewId() == KUidBrowserContentViewId )
       
   443             {
       
   444             //Start progress bar
       
   445             NotifyProgress();
       
   446             
       
   447             iInitialDownloadIndicatorState = ETrue;
       
   448             iApiProvider.Display().UpdateFSDownloadInitialIndicator( ETrue );
       
   449             // Show initially 0 kB as downloaded size.
       
   450             HBufC* kbUnit = HBufC::NewL( iMyKb->Length()+5 );
       
   451             TPtr ptr1( kbUnit->Des() );
       
   452             StringLoader::Format( ptr1, *iMyKb, -1, 0 );
       
   453             iDataText.Copy( ptr1 );
       
   454             delete kbUnit;
       
   455             iApiProvider.Display().UpdateFSProgressDataL( iDataText );
       
   456             }
       
   457 
       
   458         iIsRunning = ETrue;
       
   459         }
       
   460     }
       
   461 
       
   462 // ---------------------------------------------------------
       
   463 // CBrowserProgressIndicator::StopL()
       
   464 // ---------------------------------------------------------
       
   465 //
       
   466 void CBrowserProgressIndicator::StopL()
       
   467     {
       
   468     if( iApiProvider.LastActiveViewId() == KUidBrowserContentViewId )
       
   469         {
       
   470         ResetValues();
       
   471         if( iInitialDownloadIndicatorState )
       
   472             {
       
   473             iInitialDownloadIndicatorState = EFalse;
       
   474             iApiProvider.Display().
       
   475                 UpdateFSDownloadInitialIndicator( EFalse );
       
   476             }
       
   477         iApiProvider.Display().RestoreTitleL();
       
   478         }
       
   479     iIsRunning = EFalse;
       
   480     }
       
   481 
       
   482 // ---------------------------------------------------------
       
   483 // CBrowserProgressIndicator::NotifyProgress()
       
   484 // ---------------------------------------------------------
       
   485 //
       
   486 void CBrowserProgressIndicator::NotifyProgress()
       
   487     {
       
   488     // Unfortunately we could not eliminate TRAP. The Engine cannot 
       
   489     // support a leavable NotifyProgressL() function.
       
   490     TRAP_IGNORE( EnquireStatusL() );
       
   491     }
       
   492 
       
   493 //  End of File