S60 3rd Edition SDK for Symbian OS
Example Applications Guide

FilelistContainer.cpp

00001 /*
00002 * ==============================================================================
00003 *  Name        : FilelistContainer.cpp
00004 *  Part of     : Filelist
00005 *  Interface   : 
00006 *  Description : 
00007 *  Version     : 
00008 *
00009 *  Copyright (c) 2005-2006 Nokia Corporation.
00010 *  This material, including documentation and any related 
00011 *  computer programs, is protected by copyright controlled by 
00012 *  Nokia Corporation.
00013 * ==============================================================================
00014 */
00015 
00016 // INCLUDE FILES
00017 #include <AknLists.h> // for AknListBoxLayouts
00018 #include <Avkon.hrh>
00019 #include <AknIconArray.h> // for CArrayPtr
00020 #include <AknIconUtils.h>
00021 #include <AknNoteWrappers.h> // for CAknErrorNote
00022 #include <AknUtils.h> 
00023 #include <barsread.h> // for TResourceReader
00024 #include <gulicon.h> // for CGulIcon
00025 #include <f32file.h>
00026 #include <aknnavide.h> //NaviPane Decorator
00027 #include <GDI.H>
00028 
00029 #include <Filelist.rsg>
00030 #include <Filelist.mbg>
00031 #include "FilelistContainer.h"
00032 #include "FilelistAppUi.h"
00033 #include "Filelist.hrh"
00034 
00035 
00036 // ================= MEMBER FUNCTIONS ==========================================
00037 
00038 // -----------------------------------------------------------------------------
00039 // CFilelistContainer::NewL()
00040 // Two-phased constructor.
00041 // -----------------------------------------------------------------------------
00042 //
00043 CFilelistContainer* CFilelistContainer::NewL( const TRect& aRect )
00044     {
00045     CFilelistContainer* self = CFilelistContainer::NewLC( aRect );
00046     CleanupStack::Pop( self );
00047     return self;
00048     }
00049 
00050 // -----------------------------------------------------------------------------
00051 // CFilelistContainer::NewLC()
00052 // Two-phased constructor.
00053 // -----------------------------------------------------------------------------
00054 //
00055 CFilelistContainer* CFilelistContainer::NewLC( const TRect& aRect )
00056     {
00057     CFilelistContainer* self = new ( ELeave ) CFilelistContainer();
00058     CleanupStack::PushL( self );
00059     self->ConstructL( aRect );
00060     return self;
00061     }
00062     
00063 // -----------------------------------------------------------------------------
00064 // CFilelistContainer::~CFilelistContainer()
00065 // Destructor
00066 // -----------------------------------------------------------------------------
00067 //
00068 CFilelistContainer::~CFilelistContainer()
00069     {
00070     iSession.Close();
00071     delete iGrid;    
00072     delete iFilelist;
00073     delete iNaviDecorator;
00074     }
00075 
00076 // -----------------------------------------------------------------------------
00077 // CFilelistContainer::SetGridInitialParameters()
00078 // Sets initial parameters for grid
00079 // -----------------------------------------------------------------------------
00080 //
00081 void CFilelistContainer::SetGridInitialParametersL()
00082     {
00083     TSize mpSize;     // main pane size
00084     AknLayoutUtils::LayoutMetricsSize( AknLayoutUtils::EMainPane, mpSize );
00085 
00086     iNumOfItems = iFilelist->Count(); 
00087     
00088     iNumOfColumns = KGridColumns;
00089     iNumOfRows = KGridRows;
00090     
00091     // Determinate cell size
00092     iSizeOfCell.iWidth = mpSize.iWidth / iNumOfColumns;
00093     iSizeOfCell.iHeight = mpSize.iHeight / iNumOfRows;
00094     
00095     // Determinate scrolling type
00096     iVerticalScrollingType = CAknGridView::EScrollIncrementLineAndLoops;    
00097     iHorizontalScrollingType = CAknGridView::EScrollIncrementLineAndLoops;
00098 
00099       
00100     iVerticalOrientation = EFalse;
00101     iLeftToRight = ETrue;
00102     iTopToBottom = ETrue;
00103     
00104     // Set grid layout
00105     iGrid->SetLayoutL( iVerticalOrientation, 
00106         iLeftToRight, iTopToBottom, 
00107         iNumOfColumns, iNumOfRows,  
00108         iSizeOfCell );
00109 
00110     // Set scrolling type
00111     iGrid->SetPrimaryScrollingType( iVerticalScrollingType );
00112     iGrid->SetSecondaryScrollingType( iHorizontalScrollingType );
00113     
00114     // Set current index in grid
00115      iGrid->SetCurrentDataIndex( 0 );
00116     }
00117 
00118 // -----------------------------------------------------------------------------
00119 // CFilelistContainer::CreateGridResourceL()
00120 // Prepares resources for the grid
00121 // -----------------------------------------------------------------------------
00122 //
00123 void CFilelistContainer::CreateGridResourceL()
00124     {
00125     // Load graphics
00126     LoadGraphicsL();
00127 
00128     // Create data
00129     AddDataL();
00130     }
00131 
00132 // -----------------------------------------------------------------------------
00133 // CFilelistContainer::LoadGraphicsL()
00134 // Loads icon resource
00135 // -----------------------------------------------------------------------------
00136 //
00137 void CFilelistContainer::LoadGraphicsL()
00138     {    
00139     // Create icon array with granularity of 1 icon
00140     CArrayPtr< CGulIcon >* icons = new(ELeave) CAknIconArray( 1 );
00141     CleanupStack::PushL( icons );
00142 
00143         // Creates bitmap and mask for an icon.
00144     AknIconUtils::CreateIconL( iBitmap, 
00145             iMask, 
00146             KIconName, 
00147             EMbmFilelistQgn_menu_filelist, 
00148             EMbmFilelistQgn_menu_filelist_mask );
00149 
00150     // Determinate icon size
00151     TSize iconSize;
00152     iconSize.iWidth = iSizeOfCell.iWidth / 2;
00153     iconSize.iHeight = iSizeOfCell.iHeight / 2;
00154 
00155         //Initializes the icon to the given size.
00156         //Note that this call sets the sizes of both bitmap and mask 
00157     AknIconUtils::SetSize( iBitmap, iconSize );
00158     
00159     // Append the icon to icon array
00160     icons->AppendL( CGulIcon::NewL( iBitmap, iMask ) );
00161     
00162     // Attach icon array to grid's item drawer
00163     iGrid->ItemDrawer()->FormattedCellData()->SetIconArrayL( icons );
00164 
00165     CleanupStack::Pop( icons );    
00166     }
00167 
00168 // -----------------------------------------------------------------------------
00169 // CFilelistContainer::AddDataL()
00170 // Adds filenames into the grid.
00171 // -----------------------------------------------------------------------------
00172 //
00173 void CFilelistContainer::AddDataL()
00174     {
00175     
00176     MDesCArray* array = iGrid->Model()->ItemTextArray();
00177     CDesCArray* cArray = ( CDesCArray* )array;
00178         
00179         // The number of items in the grid.
00180     TInt numberOfData = iFilelist->Count();
00181     
00182     // Delete data from array
00183     if ( iGrid->GridModel()->NumberOfData() > 0 )
00184     {
00185         cArray->Delete( 0, iGrid->GridModel()->NumberOfData() );
00186     }
00187     iGrid->HandleItemRemovalL();
00188 
00189     // Create text which is added to the grid.
00190     TBuf< KLengthOfItemResourceString > cellText;
00191     TBuf< KLengthOfItemResourceString > cellTextFormat;
00192 
00193     iCoeEnv->ReadResourceL( cellTextFormat, R_FILELIST_FORMATSTRING );
00194 
00195     // Add the item to the last index
00196     for ( TInt loop = 0; loop < numberOfData; loop++ )
00197         {
00198         cellText.Format( cellTextFormat, &(*iFilelist)[loop].iName );
00199         cArray->AppendL( cellText );
00200         }
00201 
00202     // Inform list box that data was added.
00203     iGrid->HandleItemAdditionL();
00204     }
00205 
00206 // -----------------------------------------------------------------------------
00207 // CFilelistContainer::ApplySelGridGraphicStyle()
00208 // Applies the layout to cells in the grid
00209 // -----------------------------------------------------------------------------
00210 //
00211 void CFilelistContainer::ApplySelGridGraphicStyle()
00212     {
00213     AknListBoxLayouts::SetupStandardGrid( *iGrid );
00214 
00215     // Layout of the graphic
00216     AknListBoxLayouts::SetupFormGfxCell(
00217        *iGrid,              // Reference to grid control
00218        iGrid->ItemDrawer(),// Pointer to the item drawer
00219        0,                   // Column index
00220        0,                   // Left position
00221        0,                   // Top position
00222        0,                   // Right - unused
00223        0,                   // Bottom - unused
00224        iSizeOfCell.iWidth, // Width
00225        iSizeOfCell.iHeight/2,// Height
00226        TPoint(0,0),         // Start position
00227        TPoint(iSizeOfCell.iWidth, iSizeOfCell.iHeight/2));     // End position
00228      
00229     const CFont* fontText = AknLayoutUtils::FontFromId(
00230                     EAknLogicalFontPrimaryFont, NULL );
00231                     
00232     // Deretminate the baseline for the text
00233     TInt baseline = iSizeOfCell.iHeight - fontText->DescentInPixels() - 1;
00234 
00235     // Layout of text
00236     AknListBoxLayouts::SetupFormTextCell(
00237        *iGrid,                          // Reference to grid
00238        iGrid->ItemDrawer(),            // Pointer to the item drawer
00239        1,                           // Column index
00240        fontText,                       // Font
00241        215,                         // Color (215 = black)
00242        0,                           // Left margin
00243        0,                           // Right margin - unused
00244        baseline,                    // Baseline
00245        iSizeOfCell.iWidth,            // Text width
00246        CGraphicsContext::ECenter,    // Text alignment
00247        TPoint(0, iSizeOfCell.iHeight/2), // Start position
00248        TPoint(iSizeOfCell.iWidth, 
00249                iSizeOfCell.iHeight));    // End position
00250                
00251     //Update icon size
00252         SetIconSizes( iGrid->ItemDrawer()->FormattedCellData()->IconArray() );
00253     }
00254 
00255 
00256 // -----------------------------------------------------------------------------
00257 // CFilelistContainer::SetIconSizes()
00258 // Sets size of the icon in iconarray.
00259 // -----------------------------------------------------------------------------
00260 //
00261 void CFilelistContainer::SetIconSizes( CArrayPtr<CGulIcon>* aIconArray )
00262     {
00263     TSize iconSize;
00264     iconSize.iWidth = iSizeOfCell.iWidth / 2;
00265     iconSize.iHeight = iSizeOfCell.iHeight / 2;
00266 
00267     // note  AknIconUtils::SetSize sets both mask and bitmap size.
00268     // regardless which is given.
00269     AknIconUtils::SetSize( (*aIconArray)[0]->Bitmap(), iconSize );
00270     }
00271     
00272 // -----------------------------------------------------------------------------
00273 // CFilelistContainer::CreateFilelist()
00274 // Gets filename from private folder.
00275 // -----------------------------------------------------------------------------
00276 //
00277 void CFilelistContainer::CreateFilelist()
00278     {
00279     // List must be empty
00280     if ( iFilelist )
00281         {
00282         delete iFilelist;
00283         iFilelist=NULL;
00284         }        
00285 
00286     TBuf< KPathNameLength > dPath;
00287     TBuf< KPathNameLength > dPathF;
00288 
00289     // Get application's private path
00290     iSession.PrivatePath( dPath );
00291 
00292     // Set actual folder path
00293     if ( FolderID() == EPrivateFolder2 )
00294         {
00295         dPathF.Format( KPrivateFolderPath2, &dPath );
00296         }
00297     else
00298         {
00299         dPathF.Format( KPrivateFolderPath1, &dPath );
00300         }  
00301 
00302     // Get the file list, sorted by name
00303     User::LeaveIfError( iSession.GetDir( 
00304             dPathF,
00305             KEntryAttMaskSupported,
00306             ESortByName,
00307             iFilelist ) );
00308     }
00309 
00310 // -----------------------------------------------------------------------------
00311 // CFilelistContainer::ChangePrivateFolderL()
00312 // Changes private folder to another one
00313 // -----------------------------------------------------------------------------
00314 //
00315 void CFilelistContainer::ChangePrivateFolderL( TInt aDestFolder )
00316     {
00317     // Change folder ID
00318     SetFolderID( aDestFolder );
00319     
00320     // Read filenames from folder
00321     CreateFilelist();
00322     
00323     // Add text to grid
00324     AddDataL();
00325     
00326     // Set text to navi pane
00327     SetNaviPaneTextL();
00328     
00329     iGrid->DrawNow();
00330     }
00331     
00332 // -----------------------------------------------------------------------------
00333 // CFilelistContainer::ChangeProtectedFolderL()
00334 // Protected folder
00335 // -----------------------------------------------------------------------------
00336 //
00337 void CFilelistContainer::ChangeProtectedFolderL()
00338     {    
00339     CDir* protFolder;
00340     
00341     //Expected value is KErrPermissionDenied=(-46)
00342     TInt err = iSession.GetDir( 
00343                 KProtectedFolderPath,
00344                 KEntryAttMaskSupported,
00345                 ESortByName,
00346                 protFolder ) ;
00347                 
00348     if ( err != KErrNone )
00349         {    
00350         ShowErrorNoteL( err );
00351         }
00352     delete protFolder;    
00353     }
00354 
00355 // -----------------------------------------------------------------------------
00356 // CFilelistContainer::ShowErrorNoteL()
00357 // Shows error note
00358 // -----------------------------------------------------------------------------
00359 //
00360 void CFilelistContainer::ShowErrorNoteL( TInt aErr )
00361     {
00362     TBuf< KErrorNoteLength > errorBuf;
00363     // Create CAknErrorNote instance
00364     CAknErrorNote* dialog = new ( ELeave ) CAknErrorNote();
00365     
00366     //Allocate TBuf with constant length.
00367     TBuf< KErrorNoteLength > errNote;
00368 
00369      // Reads a resource into a descriptor.
00370     iCoeEnv->ReadResourceL( errNote, R_FILELIST_ERRORNOTE_TEXT );
00371     errorBuf.Format( errNote, aErr );
00372 
00373     // Show the Dialog 
00374     dialog->ExecuteLD( errorBuf );    
00375     }  
00376 
00377 // -----------------------------------------------------------------------------
00378 // CFilelistContainer::HandleListBoxEventL
00379 // Called by the framework whenever a grid event occurs for which this container
00380 // is an observer.
00381 // @param aListBoxEvent The type of event which occured
00382 // -----------------------------------------------------------------------------    
00383 void CFilelistContainer::HandleListBoxEventL( CEikListBox* /*aListBox*/,
00384                 TListBoxEvent /*aListBoxEvent*/ )
00385     {
00386     // no implementation required 
00387     }
00388 
00389 // ---------------------------------------------------------
00390 // CFilelistContainer::HandleResourceChange()
00391 // Called by framework when layout is changed.
00392 // ---------------------------------------------------------
00393 //
00394 /*void CFilelistContainer::HandleResourceChange( TInt aType )
00395     {
00396     CCoeControl::HandleResourceChange( aType );
00397 
00398     // ADDED FOR SCALABLE UI SUPPORT
00399     // *****************************
00400     if ( aType==KEikDynamicLayoutVariantSwitch )
00401         {
00402         TRect rect;
00403         AknLayoutUtils::LayoutMetricsRect( AknLayoutUtils::EMainPane, rect );
00404         SetRect( rect );
00405         }
00406     }
00407 */      
00408 // -----------------------------------------------------------------------------
00409 // CFilelistContainer::ConstructL(const TRect& aRect)
00410 // Symbian two phased constructor
00411 // -----------------------------------------------------------------------------
00412 //
00413 void CFilelistContainer::ConstructL( const TRect& aRect )
00414     {
00415     CreateWindowL();
00416 
00417         iSession.Connect();
00418         
00419     SetFolderID( EPrivateFolder1 );
00420 
00421     // Create CAknGrid instance:  
00422     iGrid = new(ELeave) CAknGrid;
00423 
00424     //Set the container window for grid control
00425     iGrid->SetContainerWindowL( *this );
00426 
00427     //Create a model and attach it to grid control: 
00428     iGridM = new(ELeave) CAknGridM;
00429     iGrid->SetModel( iGridM );    
00430     
00431     //Call ConstructL for the grid object: 
00432     TInt gridFlags = EAknListBoxSelectionGrid;
00433     iGrid->ConstructL( this, gridFlags);
00434    
00435     // Gets filename from folder
00436     CreateFilelist();
00437     
00438     // Set initial parameters for the grid
00439     SetGridInitialParametersL();    
00440         
00441     //Sets text to empty grid
00442     SetEmptyTextL();
00443      
00444     // Create resouce for grid
00445     CreateGridResourceL();
00446     
00447     // Set observer
00448     iGrid->SetListBoxObserver( this );
00449     
00450     //Set navi pane text from resource
00451     SetNaviPaneTextL();
00452 
00453     // Set the windows size
00454     SetRect( aRect );    
00455     
00456     // Activate the window, which makes it ready to be drawn
00457     ActivateL();   
00458     }   
00459      
00460 // -----------------------------------------------------------------------------
00461 // CFilelistContainer::SizeChanged()
00462 // Called by framework when the view size is changed.
00463 // -----------------------------------------------------------------------------
00464 //
00465 void CFilelistContainer::SizeChanged()
00466     {         
00467     if ( iGrid )
00468         {
00469         // Set initial parameters for the grid
00470         TRAP_IGNORE( SetGridInitialParametersL() );
00471     
00472             //Apply the layout to cells in the grid
00473                 ApplySelGridGraphicStyle();   
00474 
00475         iGrid->SetRect( Rect() );
00476         }
00477     }
00478 
00479 // -----------------------------------------------------------------------------
00480 // CFilelistContainer::Draw()
00481 // Draws the display.
00482 // -----------------------------------------------------------------------------
00483 //
00484 void CFilelistContainer::Draw( const TRect& /*aRect*/ ) const
00485     {
00486     // no implementation required 
00487     }
00488 
00489 // -----------------------------------------------------------------------------
00490 // CFilelistContainer::CountComponentControls()
00491 // Called by the framework in compound controls    
00492 // @return The number of controls in this CSimpleGridContainer
00493 // -----------------------------------------------------------------------------
00494 //
00495 TInt CFilelistContainer::CountComponentControls() const
00496     {
00497     return 1; // return number of controls inside this container
00498     }
00499 
00500 // -----------------------------------------------------------------------------
00501 // CFilelistContainer::ComponentControl
00502 // Called by the framework in compound controls    
00503 // @param The index of the control to return
00504 // @return The control for aIndex
00505 // -----------------------------------------------------------------------------
00506 //
00507 CCoeControl* CFilelistContainer::ComponentControl(TInt aIndex) const
00508     {
00509     switch ( aIndex )
00510         {
00511         case 0:
00512             return iGrid;
00513         default:
00514             return NULL;
00515         }
00516     }
00517 
00518 // -----------------------------------------------------------------------------
00519 // CFilelistContainer::OfferKeyEventL
00520 // Called by the framework whenever a key event occurs.    
00521 // @param aType the type of Key event which occurred, e.g. key up, key down
00522 // @return TKeyResponse EKeyWasNotConsumed if the key was not processed,
00523 // EKeyWasConsumed if it was 
00524 // -----------------------------------------------------------------------------
00525 TKeyResponse CFilelistContainer::OfferKeyEventL( const TKeyEvent& aKeyEvent,
00526         TEventCode aType )
00527     {
00528     if ( iGrid )
00529         {
00530         return iGrid->OfferKeyEventL ( aKeyEvent, aType );
00531         }
00532     else
00533         {
00534         return EKeyWasNotConsumed;    
00535         }
00536     }
00537 
00538 // -----------------------------------------------------------------------------
00539 // CFilelistContainer::SetFolderID()
00540 // Sets folderID to point to desired folder
00541 // -----------------------------------------------------------------------------
00542 //
00543 void CFilelistContainer::SetFolderID( TInt aFolderID )
00544     {
00545     iFolderID = aFolderID;
00546     }
00547     
00548 // -----------------------------------------------------------------------------
00549 // FilelistContainer::FolderID()
00550 // Gets folder ID
00551 // -----------------------------------------------------------------------------
00552 //
00553 TInt CFilelistContainer::FolderID() const
00554     {
00555     return iFolderID;
00556     }
00557 
00558 // -----------------------------------------------------------------------------
00559 // CFilelistContainer::SetEmptyText()
00560 // Sets text to empty grid
00561 // -----------------------------------------------------------------------------
00562 //
00563 void CFilelistContainer::SetEmptyTextL()
00564     {
00565     TBuf< KEmptyGridText > emptyTxt;
00566     iCoeEnv->ReadResourceL( emptyTxt, R_FILELIST_EMPTYTEXT );
00567     iGrid->SetEmptyGridTextL( emptyTxt );
00568     }
00569 
00570 // -----------------------------------------------------------------------------
00571 // CFilelistContainer::SetNaviPaneTextL()
00572 // Changes text to navi pane
00573 // -----------------------------------------------------------------------------    
00574 void CFilelistContainer::SetNaviPaneTextL()
00575     {
00576     TResourceReader spText; 
00577         
00578         // Create navipane pointer
00579         if ( !iNaviPane )
00580                 {
00581                 CEikStatusPane *sp = 
00582                             ( ( CAknAppUi* )iEikonEnv->EikAppUi() )->StatusPane();
00583                 // Fetch pointer to the default navi pane control
00584                 iNaviPane = ( CAknNavigationControlContainer * )
00585                             sp->ControlL( TUid::Uid( EEikStatusPaneUidNavi ) );
00586                 }
00587     
00588     if ( iNaviDecorator )
00589         {
00590         delete iNaviDecorator;
00591         iNaviDecorator = NULL;        
00592         }
00593         
00594     // Read text from resource
00595     if ( FolderID() == EPrivateFolder1 )
00596         {
00597         iCoeEnv->CreateResourceReaderLC( spText, R_FILELIST_NAVI_PANE_TEXT1 );
00598         }
00599     else
00600         {
00601         iCoeEnv->CreateResourceReaderLC( spText, R_FILELIST_NAVI_PANE_TEXT2 );
00602         }
00603         
00604     // Set text to navi pane
00605     iNaviDecorator = iNaviPane->CreateNavigationLabelL( spText );
00606     CleanupStack::PopAndDestroy(); // Pushed by CreateResourceReaderLC
00607     iNaviPane->PushL( *iNaviDecorator );
00608     }
00609 
00610 // End of File  
00611 

© Nokia 2006

Back to top