filesystemuis/memstatepopup/src/cmemstatepopupimpl.cpp
branchRCL_3
changeset 38 491b3ed49290
parent 36 95243422089a
child 39 65326cf895ed
equal deleted inserted replaced
36:95243422089a 38:491b3ed49290
     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:  Memory state popup implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // SYSTEM INCLUDES
       
    20 #include <e32std.h>
       
    21 #include <AknProgressDialog.h>
       
    22 #include <aknlists.h>
       
    23 #include <ConeResLoader.h>
       
    24 #include <bautils.h>
       
    25 #include <StringLoader.h>
       
    26 #include <aknPopup.h>
       
    27 #include <memstatepopup.rsg>
       
    28 #include <f32file.h>
       
    29 #include <data_caging_path_literals.hrh> 
       
    30 
       
    31 // USER INCLUDES
       
    32 #include "msputil.h"
       
    33 #include "cmemstatepopup.h"
       
    34 #include "cmemstatepopupimpl.h"
       
    35 #include "cmemscaneventreceiver.h"
       
    36 
       
    37 //  CONSTANTS
       
    38 _LIT( KDirAndFile,"Z:MemStatePopup.RSC" );
       
    39 _LIT( KMemListSeparator, "\t" );
       
    40 
       
    41 // Max length of a list row
       
    42 // Has to be big enough to hold "%N %U"
       
    43 const TInt KListItemMaxLength = 32;
       
    44 
       
    45 // ---------------------------------------------------------------------------
       
    46 
       
    47 CMemStatePopupImpl::~CMemStatePopupImpl( )
       
    48     {
       
    49     Cancel();
       
    50     // Close the resource
       
    51     iResLoader.Close( );
       
    52 
       
    53     if( iWaitDialog )
       
    54         {
       
    55         iWaitDialog->SetObserver( NULL );
       
    56         delete iWaitDialog;
       
    57         }
       
    58 
       
    59     delete iEventReceiver; // deletes also scan engine and server 
       
    60      
       
    61     delete iGroupNames;
       
    62     delete iListModel;
       
    63     delete iListBox;
       
    64     delete iScanResults;
       
    65 
       
    66 
       
    67     if( iOwnsUtil )
       
    68         {
       
    69         // Delete only if the actual dialog was launched
       
    70         delete iUtil;
       
    71         }
       
    72 
       
    73     // Try to delete also iPopup in case of leave
       
    74     // Must call CBase's destructor
       
    75     // because CAknPopupList destructor is protected
       
    76     delete ( CBase* ) iPopup;
       
    77     }
       
    78 
       
    79 // ---------------------------------------------------------------------------
       
    80 
       
    81 CMemStatePopupImpl::CMemStatePopupImpl( TDriveNumber aDrive, const TDesC& aTitle ) :
       
    82             CActive(EPriorityStandard),
       
    83             iFinished( EFalse ),
       
    84             iDrive( aDrive ),
       
    85             iTitle( const_cast<TDesC*> (&aTitle) ),
       
    86             iCoeEnv( *CCoeEnv::Static( ) ),
       
    87             iResLoader( iCoeEnv ),
       
    88             iOwnsUtil( ETrue )
       
    89     {
       
    90     CActiveScheduler::Add(this);
       
    91     }
       
    92 
       
    93 // ---------------------------------------------------------------------------
       
    94 
       
    95 // Used by GetUtilL
       
    96 CMemStatePopupImpl::CMemStatePopupImpl( ) :
       
    97             CActive(EPriorityStandard),
       
    98             iFinished( EFalse ),
       
    99             iDrive( EDriveC ),
       
   100             iCoeEnv( *CCoeEnv::Static( ) ),
       
   101             iResLoader( iCoeEnv ),
       
   102             iOwnsUtil( EFalse )
       
   103     {
       
   104     }
       
   105 
       
   106 // ---------------------------------------------------------------------------
       
   107 
       
   108 void CMemStatePopupImpl::ConstructL( )
       
   109     {
       
   110     OpenResourceAndReadArrayL( );
       
   111 
       
   112     // Instantiate event receiver, memory scan engine and server
       
   113     iEventReceiver = CMemScanEventReceiver::NewL( *this );
       
   114 
       
   115     // Get data group names from engine
       
   116     iGroupNames = iEventReceiver->DataGroupsL( );
       
   117 
       
   118     // Create popup
       
   119     ConstructPopupL( );
       
   120 
       
   121     // Create wait dialog
       
   122     iWaitDialog = new( ELeave ) CAknWaitDialog(
       
   123                     reinterpret_cast< CEikDialog** >( &iWaitDialog ) );
       
   124     iWaitDialog->SetCallback( this );
       
   125     iWaitDialog->PrepareLC( R_MEMSTATE_WAIT_NOTE );
       
   126     
       
   127     StartObserver();
       
   128     }
       
   129 
       
   130 // ---------------------------------------------------------------------------
       
   131 
       
   132 void CMemStatePopupImpl::RunLD( TDriveNumber aDrive, const TDesC& aTitle )
       
   133     {
       
   134     CMemStatePopupImpl* self = new( ELeave ) CMemStatePopupImpl( aDrive, aTitle );
       
   135     CleanupStack::PushL( self );
       
   136     self->ConstructL( );
       
   137 
       
   138     // Run the actual process
       
   139     self->ExecuteL( );
       
   140 
       
   141     CleanupStack::PopAndDestroy( self );
       
   142     }
       
   143 
       
   144 
       
   145 // ---------------------------------------------------------------------------
       
   146 
       
   147 void CMemStatePopupImpl::GetUtilL( CMSPUtil*& aUtil )
       
   148     {
       
   149     CMemStatePopupImpl* self = new( ELeave ) CMemStatePopupImpl;
       
   150     CleanupStack::PushL( self );
       
   151 
       
   152     // Read the unit array from resource
       
   153     self->OpenResourceAndReadArrayL( );
       
   154     aUtil = self->iUtil; // iUtil is not deleted
       
   155 
       
   156     CleanupStack::PopAndDestroy( self );
       
   157     }
       
   158 
       
   159 // ---------------------------------------------------------------------------
       
   160 
       
   161 void CMemStatePopupImpl::StartL( )
       
   162     {
       
   163 #ifdef _DEBUG
       
   164     RDebug::Print( _L( "Scanning started!" ) );
       
   165 #endif
       
   166     }
       
   167 
       
   168 // ---------------------------------------------------------------------------
       
   169 
       
   170 void CMemStatePopupImpl::QuitL( TInt /*aReason*/ )
       
   171     {
       
   172 #ifdef _DEBUG
       
   173     RDebug::Print( _L( "Scanning ended!" ) );
       
   174 #endif
       
   175     if( !iFinished )
       
   176         {
       
   177         // Tell waitdialog that it can quit now
       
   178         iWaitDialog->ProcessFinishedL( );
       
   179         iFinished = ETrue;
       
   180         }
       
   181     }
       
   182 
       
   183 // ---------------------------------------------------------------------------
       
   184 
       
   185 void CMemStatePopupImpl::ErrorL( TInt aError )
       
   186     {
       
   187 #ifdef _DEBUG
       
   188     RDebug::Print( _L( "Error scanning memory: %d" ), aError );
       
   189 #endif
       
   190 
       
   191     // getting rid of UREL compiler warning
       
   192     if( !aError ) 
       
   193         {
       
   194         aError = KErrNone;
       
   195         }
       
   196     }
       
   197 
       
   198 // ---------------------------------------------------------------------------
       
   199 
       
   200 void CMemStatePopupImpl::DialogDismissedL( TInt aButtonId )
       
   201     {
       
   202 
       
   203     // Cancel is pressed while scanning is in progress
       
   204     if( iEventReceiver->ScanInProgress( ) )
       
   205         {
       
   206         iFinished = ETrue;
       
   207         iEventReceiver->Cancel( ); // Stop scanning
       
   208         }
       
   209 
       
   210     // Cancel is pressed while wait note is on screen (scanning may be completed)
       
   211     if( aButtonId == EAknSoftkeyCancel ) // instead of EAknSoftkeyDone
       
   212         {
       
   213         iPopup->CancelPopup( ); // Remove memory state popup from screen
       
   214         return;
       
   215         }
       
   216 
       
   217     // Get the new result array
       
   218     delete iScanResults;
       
   219     iScanResults = NULL;
       
   220     TRAPD( err, iScanResults = iEventReceiver->ScanResultL( ) );
       
   221     if( err != KErrNone )
       
   222         {
       
   223         iPopup->CancelPopup( ); // Remove memory state popup from screen
       
   224         return;
       
   225         }
       
   226     RefreshL( );
       
   227     }
       
   228 
       
   229 // ---------------------------------------------------------------------------
       
   230 
       
   231 void CMemStatePopupImpl::ConstructPopupL( )
       
   232     {
       
   233     iListBox = new( ELeave ) CMemStateListBox( );
       
   234 
       
   235     iPopup = CAknPopupList::NewL( iListBox, R_AVKON_SOFTKEYS_OK_EMPTY,
       
   236                             AknPopupLayouts::EMenuGraphicHeadingWindow );
       
   237     iPopup->SetTitleL( *iTitle );
       
   238 
       
   239     // Set up listbox
       
   240     iListBox->ConstructL( iPopup, EAknListBoxViewerFlags );
       
   241 
       
   242     // Create listbox model and give the model to the listbox
       
   243     iListModel = new( ELeave ) CDesCArraySeg( iGroupNames->Count( ) );
       
   244 
       
   245     // Create scroll indicator
       
   246     iListBox->CreateScrollBarFrameL( ETrue );
       
   247     iListBox->ScrollBarFrame( )->SetScrollBarVisibilityL(
       
   248                     CEikScrollBarFrame::EOff, CEikScrollBarFrame::EAuto );
       
   249     iListBox->Model( )->SetItemTextArray( iListModel );
       
   250     iListBox->Model( )->SetOwnershipType( ELbmDoesNotOwnItemArray );
       
   251     iListBox->ItemDrawer()->ColumnData()->EnableMarqueeL( ETrue );
       
   252     }
       
   253 
       
   254 // ---------------------------------------------------------------------------
       
   255 
       
   256 void CMemStatePopupImpl::ExecuteL( )
       
   257     {
       
   258     // Show wait dialog
       
   259     iWaitDialog->RunLD( );
       
   260 
       
   261     // Start the scanning
       
   262     User::LeaveIfError(iEventReceiver->ScanL( iDrive ));
       
   263     
       
   264     // Get the result array
       
   265     iScanResults = iEventReceiver->ScanResultL( );
       
   266    
       
   267     // Set all results zero, so that no actual results are shown
       
   268     // before the scan is done.
       
   269     NullifyResults( );
       
   270 
       
   271     // Update the list with initial values (0's)
       
   272     RefreshL( );
       
   273 
       
   274     // Show popup
       
   275     iPopup->ExecuteLD( );
       
   276     iPopup = NULL; // Has to be set NULL because deleted in destructor
       
   277     }
       
   278 
       
   279 // ---------------------------------------------------------------------------
       
   280 
       
   281 void CMemStatePopupImpl::RefreshL( )
       
   282     {
       
   283     //TBuf< KListItemMaxLength > result;
       
   284     TBuf< KListItemMaxLength > unitText;
       
   285     TBuf< KListItemMaxLength > tempText;
       
   286     TPtrC unit;
       
   287     TInt64 res64( 0 );
       
   288     TInt count( iScanResults ? iScanResults->Count( ) : 0 );
       
   289     TInt maxGroupName(0);
       
   290     
       
   291     for( TInt i = 0; i < iGroupNames->Count(); i++ )
       
   292         {
       
   293         if( maxGroupName < ( *iGroupNames )[ i ].Length() )
       
   294             {
       
   295             maxGroupName = ( *iGroupNames )[ i ].Length();
       
   296             }
       
   297         }
       
   298         
       
   299     // Allocate memory for size, unit text and actual group name
       
   300     HBufC* result = HBufC::NewLC( maxGroupName + 2*KListItemMaxLength );
       
   301     TPtr resultPtr( result->Des() );
       
   302     HBufC* unitFormatter = StringLoader::LoadLC( R_UNIT_FORMATTER, &iCoeEnv );
       
   303 
       
   304     iListModel->Reset( );
       
   305 
       
   306     // Go through the result array and append to the model as text
       
   307     for( TInt i = 0; i < count; i++ )
       
   308         {
       
   309         res64 = ( *iScanResults )[ i ];
       
   310 
       
   311         unit.Set( iUtil->SolveUnitAndSize( res64 ) );
       
   312         StringLoader::Format( tempText, *unitFormatter, 0, I64INT( res64 ) );
       
   313         StringLoader::Format( unitText, tempText, 1, unit );
       
   314         resultPtr.Zero( );
       
   315 
       
   316         // The actual listbox row is constructed here
       
   317         // iListModel is just an array of descriptors
       
   318         if ( User::Language() == ELangArabic )
       
   319             {
       
   320             resultPtr.Append( KRightToLeftMark );
       
   321             }
       
   322         resultPtr.Append( unitText );
       
   323         resultPtr.Append( KMemListSeparator );
       
   324         if( i < iGroupNames->Count() )
       
   325             {
       
   326             resultPtr.Append( ( *iGroupNames )[ i ] );
       
   327             }
       
   328         else
       
   329             {
       
   330             resultPtr.Append( ( *iGroupNames )[ 0 ] );
       
   331             }
       
   332         iListModel->AppendL( resultPtr );
       
   333         }
       
   334 
       
   335     CleanupStack::PopAndDestroy( unitFormatter );
       
   336     CleanupStack::PopAndDestroy( result );
       
   337 
       
   338     // Update the listbox
       
   339     iListBox->HandleItemAdditionL( );
       
   340     }
       
   341 
       
   342 // ---------------------------------------------------------------------------
       
   343 
       
   344 void CMemStatePopupImpl::NullifyResults( )
       
   345     {
       
   346     // Set all results to zero
       
   347     if( iScanResults )
       
   348         {
       
   349         TInt count( iScanResults->Count( ) );
       
   350         for( TInt t = 0; t < count; t++ )
       
   351             {
       
   352             ( *iScanResults )[ t ] = 0;
       
   353             }
       
   354         }
       
   355     }
       
   356 
       
   357 // ---------------------------------------------------------------------------
       
   358 
       
   359 void CMemStatePopupImpl::OpenResourceAndReadArrayL( )
       
   360     {
       
   361     // Open dll resource
       
   362     
       
   363     TParse* fp = new(ELeave) TParse(); 
       
   364     fp->Set(KDirAndFile, &KDC_RESOURCE_FILES_DIR, NULL); 
       
   365     TFileName fileName( fp->FullName() );
       
   366     delete fp;
       
   367 
       
   368     BaflUtils::NearestLanguageFile( iCoeEnv.FsSession( ),
       
   369                                     fileName );
       
   370     iResLoader.OpenL( fileName );
       
   371 
       
   372     // Read localised unit texts from resource
       
   373     TResourceReader reader;
       
   374     iCoeEnv.CreateResourceReaderLC( reader, R_ARRAY_UNITS );
       
   375     CDesCArrayFlat* units = reader.ReadDesCArrayL( );
       
   376     CleanupStack::PushL( units );
       
   377     iUtil = CMSPUtil::NewL( units ); // Give the array to the util class
       
   378     CleanupStack::Pop( units );
       
   379     CleanupStack::PopAndDestroy(); // Private HBufC8* variable
       
   380                                    // of CreateResourceReaderLC()
       
   381     }
       
   382 
       
   383 // --------------------------------------------------------------------------- 
       
   384 void CMemStatePopupImpl::RunL()
       
   385     {
       
   386     TVolumeInfo volInfo;
       
   387     TInt err = iCoeEnv.FsSession().Volume( volInfo, iDrive );
       
   388     if(err != KErrNone)
       
   389         {
       
   390         iPopup->CancelPopup( );
       
   391         }
       
   392     else
       
   393         {
       
   394         StartObserver();
       
   395         }
       
   396     }
       
   397 
       
   398 // --------------------------------------------------------------------------- 
       
   399 void CMemStatePopupImpl::DoCancel()
       
   400     {
       
   401     iCoeEnv.FsSession().NotifyChangeCancel();
       
   402     }
       
   403 
       
   404 // --------------------------------------------------------------------------- 
       
   405 void CMemStatePopupImpl::StartObserver()
       
   406     {
       
   407     if ( IsActive() ) 
       
   408         {
       
   409         Cancel();
       
   410         }
       
   411     iCoeEnv.FsSession().NotifyChange( ENotifyDisk, iStatus );
       
   412     SetActive();
       
   413     }
       
   414 
       
   415 // End of File