commonuis/CommonDialogs/src/CAknFileSelectionEventHandler.cpp
changeset 0 2f259fa3e83a
child 10 9f56a4e1b8ab
equal deleted inserted replaced
-1:000000000000 0:2f259fa3e83a
       
     1 /*
       
     2 * Copyright (c) 2002-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:  ?Description
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDES
       
    20 #include "CAknFileSelectionEventHandler.h"
       
    21 
       
    22 #include <eikbtgpc.h> // CEikButtonGroupContainer
       
    23 #include <avkon.hrh>
       
    24 
       
    25 #include "CAknFileSelectionModel.h"
       
    26 #include "MAknFileSelectionObserver.h"
       
    27 #include "AknCFDUtility.h"
       
    28 
       
    29 
       
    30 
       
    31 // ============================ MEMBER FUNCTIONS ===============================
       
    32 
       
    33 // -----------------------------------------------------------------------------
       
    34 // CAknFileSelectionEventHandler::CAknFileSelectionEventHandler
       
    35 //
       
    36 //
       
    37 // -----------------------------------------------------------------------------
       
    38 //
       
    39 CAknFileSelectionEventHandler::CAknFileSelectionEventHandler(
       
    40     CCoeEnv* aCoeEnv,
       
    41     CAknFileSelectionModel* aModel,
       
    42     const TCommonDialogType& aDialogType,
       
    43     MAknFileSelectionObserver*& aObserver,
       
    44     const HBufC*& aLeftSoftkeyFile,
       
    45     const HBufC*& aLeftSoftkeyFolder,
       
    46     const HBufC*& aRightSoftkeyRootFolder,
       
    47     const HBufC*& aRightSoftkeySubfolder )
       
    48     :   iCoeEnv( aCoeEnv ),
       
    49         iModel( aModel ),
       
    50         iDialogType( aDialogType ),
       
    51         iObserver( aObserver ),
       
    52         iLeftSoftkeyFile( aLeftSoftkeyFile ),
       
    53         iLeftSoftkeyFolder( aLeftSoftkeyFolder ),
       
    54         iRightSoftkeyRootFolder( aRightSoftkeyRootFolder ),
       
    55         iRightSoftkeySubfolder( aRightSoftkeySubfolder )
       
    56     {
       
    57     ResetSoftkeyStatus();
       
    58     }
       
    59 
       
    60 // -----------------------------------------------------------------------------
       
    61 // CAknFileSelectionEventHandler::ConstructL
       
    62 //
       
    63 // -----------------------------------------------------------------------------
       
    64 //
       
    65 void CAknFileSelectionEventHandler::ConstructL()
       
    66     {
       
    67     }
       
    68 
       
    69 // -----------------------------------------------------------------------------
       
    70 // CAknFileSelectionEventHandler::NewL
       
    71 //
       
    72 // -----------------------------------------------------------------------------
       
    73 //
       
    74 CAknFileSelectionEventHandler* CAknFileSelectionEventHandler::NewL(
       
    75     CCoeEnv* aCoeEnv,
       
    76     CAknFileSelectionModel* aModel,
       
    77     const TCommonDialogType& aDialogType,
       
    78     MAknFileSelectionObserver*& aObserver,
       
    79     const HBufC*& aLeftSoftkeyFile,
       
    80     const HBufC*& aLeftSoftkeyFolder,
       
    81     const HBufC*& aRightSoftkeyRootFolder,
       
    82     const HBufC*& aRightSoftkeySubfolder )
       
    83     {
       
    84     CAknFileSelectionEventHandler* self = new( ELeave ) CAknFileSelectionEventHandler(
       
    85         aCoeEnv, aModel, aDialogType, aObserver,
       
    86         aLeftSoftkeyFile, aLeftSoftkeyFolder, aRightSoftkeyRootFolder, aRightSoftkeySubfolder );
       
    87     CleanupStack::PushL( self );
       
    88     self->ConstructL();
       
    89     CleanupStack::Pop();
       
    90     return self;
       
    91     }
       
    92 
       
    93 // Destructor
       
    94 CAknFileSelectionEventHandler::~CAknFileSelectionEventHandler()
       
    95     {
       
    96     }
       
    97 
       
    98 
       
    99 // -----------------------------------------------------------------------------
       
   100 // CAknFileSelectionEventHandler::PushIndicesL
       
   101 //
       
   102 //
       
   103 // -----------------------------------------------------------------------------
       
   104 //
       
   105 void CAknFileSelectionEventHandler::PushIndicesL( TInt aTopIndex, TInt aFocus )
       
   106     {
       
   107     TFocusItem* item = new( ELeave ) TFocusItem( aTopIndex, aFocus );
       
   108     CleanupStack::PushL( item );
       
   109     iStack.PushL( item );
       
   110     CleanupStack::Pop();
       
   111     }
       
   112 
       
   113 // -----------------------------------------------------------------------------
       
   114 // CAknFileSelectionEventHandler::PopIndices
       
   115 //
       
   116 //
       
   117 // -----------------------------------------------------------------------------
       
   118 //
       
   119 void CAknFileSelectionEventHandler::PopIndices( TInt& aTopIndex, TInt& aFocus )
       
   120     {
       
   121     if( iStack.IsEmpty() )
       
   122         {
       
   123         aTopIndex = 0;
       
   124         aFocus = 0;
       
   125         return;
       
   126         }
       
   127     TFocusItem* item = iStack.Pop();
       
   128     aTopIndex = item->iTopIndex;
       
   129     aFocus = item->iFocus;
       
   130     delete item;
       
   131     }
       
   132 
       
   133 // -----------------------------------------------------------------------------
       
   134 // CAknFileSelectionEventHandler::ResetSoftkeyStatus
       
   135 //
       
   136 //
       
   137 // -----------------------------------------------------------------------------
       
   138 //
       
   139 void CAknFileSelectionEventHandler::ResetSoftkeyStatus()
       
   140     {
       
   141     iCurrentLeftSoftkey = EFirstTime;
       
   142     iCurrentRightSoftkey = EFirstTime;
       
   143     }
       
   144 
       
   145 // -----------------------------------------------------------------------------
       
   146 // CAknFileSelectionEventHandler::UpdateSoftkeysL
       
   147 //
       
   148 //
       
   149 // -----------------------------------------------------------------------------
       
   150 //
       
   151 void CAknFileSelectionEventHandler::UpdateSoftkeysL( TInt aCurrentItemIndex,
       
   152     CEikButtonGroupContainer* aContainer )
       
   153     {
       
   154     if( !aContainer )
       
   155         {
       
   156         return;
       
   157         }
       
   158     TBool needDrawing( EFalse );
       
   159     TSoftkeyStatus targetLeftSoftkey( ESKNotVisible );
       
   160     TSoftkeyStatus targetRightSoftkey( ESKNotVisible );
       
   161 
       
   162     // Solve correct left softkey
       
   163     if( iModel->MdcaCount() > 0 )
       
   164         {
       
   165         if( ( aCurrentItemIndex == 0 ) &&
       
   166             ( AknCFDUtility::DirectoriesOnly( iDialogType ) ) )
       
   167             {
       
   168             targetLeftSoftkey = ESKFileFocused;
       
   169             }
       
   170         else
       
   171             {
       
   172             if( iModel->Entry( aCurrentItemIndex ).IsDir() )
       
   173                 {
       
   174                 targetLeftSoftkey = ESKFolderFocused;
       
   175                 }
       
   176             else
       
   177                 {
       
   178                 targetLeftSoftkey = ESKFileFocused;
       
   179                 }
       
   180             }
       
   181         }
       
   182 
       
   183     // Solve correct right softkey
       
   184     if( iModel->DirectoryLevel() > 0 )
       
   185         {
       
   186         targetRightSoftkey = ESKInSubfolder;
       
   187         }
       
   188     else
       
   189         {
       
   190         targetRightSoftkey = ESKInRootFolder;
       
   191         }
       
   192 
       
   193     MEikButtonGroup* bg = aContainer->ButtonGroup();
       
   194 
       
   195     // Change left softkey text if needed
       
   196     if( targetLeftSoftkey != iCurrentLeftSoftkey )
       
   197         {
       
   198         TInt commandId( bg->CommandId( 0 ) ); // Left softkey command id
       
   199         switch( targetLeftSoftkey )
       
   200             {
       
   201             case ESKFileFocused:
       
   202                 {
       
   203                 aContainer->SetCommandL( commandId, *iLeftSoftkeyFile );
       
   204                 break;
       
   205                 }
       
   206             case ESKFolderFocused:
       
   207                 {
       
   208                 aContainer->SetCommandL( commandId, *iLeftSoftkeyFolder );
       
   209                 break;
       
   210                 }
       
   211             case ESKNotVisible:
       
   212                 {
       
   213                 aContainer->SetCommandL( commandId, KNullDesC );
       
   214                 break;
       
   215                 }
       
   216             default:
       
   217                 {
       
   218                 break;
       
   219                 }
       
   220             }
       
   221         needDrawing = ETrue;
       
   222         iCurrentLeftSoftkey = targetLeftSoftkey;
       
   223         }
       
   224 
       
   225     // Change right softkey text if needed
       
   226     if( targetRightSoftkey != iCurrentRightSoftkey )
       
   227         {
       
   228         TInt commandId( bg->CommandId( 2 ) ); // Right softkey command id
       
   229         switch( targetRightSoftkey )
       
   230             {
       
   231             case ESKInRootFolder:
       
   232                 {
       
   233                 aContainer->SetCommandL( commandId, *iRightSoftkeyRootFolder );
       
   234                 break;
       
   235                 }
       
   236             case ESKInSubfolder:
       
   237                 {
       
   238                 aContainer->SetCommandL( commandId, *iRightSoftkeySubfolder );
       
   239                 break;
       
   240                 }
       
   241             case ESKNotVisible:
       
   242                 {
       
   243                 aContainer->SetCommandL( commandId, KNullDesC );
       
   244                 break;
       
   245                 }
       
   246             default:
       
   247                 {
       
   248                 break;
       
   249                 }
       
   250             }
       
   251         needDrawing = ETrue;
       
   252         iCurrentRightSoftkey = targetRightSoftkey;
       
   253         }
       
   254 
       
   255     // Redraw if necessary
       
   256     if( needDrawing )
       
   257         {
       
   258         aContainer->DrawNow();
       
   259         }
       
   260     }
       
   261 
       
   262 // -----------------------------------------------------------------------------
       
   263 // CAknFileSelectionEventHandler::HandleEventL
       
   264 //
       
   265 //
       
   266 // -----------------------------------------------------------------------------
       
   267 //
       
   268 MAknCommonDialogsEventObserver::TAction CAknFileSelectionEventHandler::HandleEventL(
       
   269     TEvent aEvent,
       
   270     TInt& aFocusedItemIndex,
       
   271     TInt& aTopItemIndex,
       
   272     CEikButtonGroupContainer* aContainer )
       
   273     {
       
   274 
       
   275     // Default action is did nothing
       
   276     TAction returnType( EDidNothing );
       
   277     // Cache entry count
       
   278     TInt entries( iModel->MdcaCount() );
       
   279 
       
   280     //TEntry entry;
       
   281 
       
   282     if( entries > 0 )
       
   283         {
       
   284         iEntry = iModel->Entry( aFocusedItemIndex );
       
   285         }
       
   286 
       
   287     switch( aEvent )
       
   288         {
       
   289         case ERightSoftkeyPress: // USER HAS HIT RIGHT SOFTKEY
       
   290             {
       
   291             if( iModel->DirectoryLevel() > 0 ) // We are not in the root folder
       
   292                 {
       
   293                 while( ETrue ) // Find until existing contents
       
   294                     {
       
   295                     entries = iModel->GotoParentFolderL();
       
   296                     PopIndices( aTopItemIndex, aFocusedItemIndex );
       
   297                     if( entries > 0 || entries < KErrNotFound )
       
   298                         {
       
   299                         break;
       
   300                         }
       
   301                     }
       
   302                 if( entries > 0 )
       
   303                     {
       
   304                     if (aFocusedItemIndex >= entries)
       
   305                         {
       
   306                         aFocusedItemIndex = entries - 1;
       
   307                         }
       
   308                     UpdateSoftkeysL( aFocusedItemIndex, aContainer );
       
   309                     returnType = EItemsUpdated;
       
   310                     }
       
   311                 }
       
   312             else // We are in the root folder
       
   313                 {
       
   314                 returnType = ETryingToExit;
       
   315                 }
       
   316             break;
       
   317             }
       
   318 
       
   319         case ELeftSoftkeyPress: // USER HAS HIT LEFT SOFTKEY
       
   320         case ESelectionKeyPress: // USER HAS HIT SELECTION KEY
       
   321         case EEventClicked:
       
   322             {
       
   323             if( entries > 0 ) // There are entries in the directory
       
   324                 {
       
   325                 if( ( aFocusedItemIndex == 0 ) && ( AknCFDUtility::DirectoriesOnly( iDialogType ) ) )
       
   326                     {
       
   327                     returnType = ETryingToExit;
       
   328                     break;
       
   329                     }
       
   330                 if( iEntry.IsDir() )
       
   331                     {
       
   332                     entries = iModel->GotoSubFolderL( iEntry.iName );
       
   333                     if ( ( entries == 0 ) && ( iDialogType == ECFDDialogTypeSelect ) )
       
   334                         {
       
   335                         entries = iModel->GotoParentFolderL();
       
   336                         returnType = ELaunchEmptyPopup;
       
   337                         }
       
   338                     else if ( entries >= KErrNone )
       
   339                         {
       
   340                         PushIndicesL( aTopItemIndex, aFocusedItemIndex );
       
   341                         // In new folder the focus is always on first item
       
   342                         aTopItemIndex = 0;
       
   343                         aFocusedItemIndex = 0;
       
   344                         UpdateSoftkeysL( aFocusedItemIndex, aContainer );
       
   345                         returnType = EItemsUpdated;
       
   346                         }
       
   347                     }
       
   348                 else // The selected entry is a file
       
   349                     {
       
   350                     returnType = ETryingToExit;
       
   351                     }
       
   352                 }
       
   353             break;
       
   354             }
       
   355 
       
   356         case EFocusChange: // USER HAS HIT UP OR DOWN
       
   357             {
       
   358             UpdateSoftkeysL( aFocusedItemIndex, aContainer );
       
   359             break;
       
   360             }
       
   361 
       
   362         default:
       
   363             {
       
   364             break;
       
   365             }
       
   366         }
       
   367     if( ( returnType == ETryingToExit ) && ( aEvent != ERightSoftkeyPress ) )
       
   368         {
       
   369         if( iObserver )
       
   370             {
       
   371             //TPath path;
       
   372             HBufC * bufPath = HBufC::NewLC(KMaxPath);
       
   373             TPtr path = bufPath->Des();
       
   374 
       
   375             iModel->GetCurrentPath( path );
       
   376             if( !iObserver->OkToExitL( path, iEntry ) )
       
   377                 {
       
   378                 returnType = EDidNothing;
       
   379                 }
       
   380 
       
   381             CleanupStack::PopAndDestroy();
       
   382             }
       
   383         }
       
   384     return returnType;
       
   385     }
       
   386 
       
   387 // End of File