phonebookui/Phonebook2/remotecontactlookup/contactactionservice/src/CFscSelectFieldDlg.cpp
branchRCL_3
changeset 20 f4a778e096c2
parent 0 e686773b3f54
equal deleted inserted replaced
19:5b6f26637ad3 20:f4a778e096c2
       
     1 /*
       
     2 * Copyright (c) 2008 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 field selection dialog.
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 #include "emailtrace.h"
       
    20 #include "CFscSelectFieldDlg.h"
       
    21 
       
    22 // Phonebook 2
       
    23 #include "CFscFieldListBoxModel.h"
       
    24 #include "MFscControlKeyObserver.h"
       
    25 #include "MFscClipListBoxText.h"
       
    26 #include <CPbk2IconArray.h>
       
    27 #include "CFscPresentationContactFieldCollection.h"
       
    28 #include <CPbk2StorePropertyArray.h>
       
    29 #include <pbk2rclactionutils.rsg>
       
    30 
       
    31 // Virtual Phonebook
       
    32 #include <CVPbkContactManager.h>
       
    33 
       
    34 // System includes
       
    35 #include <aknPopup.h>
       
    36 #include <aknlists.h>
       
    37 #include <eikclbd.h>
       
    38 #include <calslbs.h>
       
    39 #include <barsread.h>
       
    40 
       
    41 /// Unnamed namespace for local definitions
       
    42 namespace
       
    43     {
       
    44 
       
    45 #ifdef _DEBUG
       
    46     enum TPanicCode
       
    47         {
       
    48         EPanicPostCond_Constructor = 1,
       
    49         EPanicPreCond_ExecuteLD,
       
    50         EPanicPreCond_AttemptExitL,
       
    51         EPanicPreCond_ResetWhenDestroyed
       
    52         };
       
    53 
       
    54     void Panic(TPanicCode aReason)
       
    55         {
       
    56         _LIT(KPanicText, "CPbkSelectFieldDlg");
       
    57         User::Panic(KPanicText, aReason);
       
    58         }
       
    59 #endif // _DEBUG
       
    60     /**
       
    61      * Creates and returns an icon array with Phonebook 2
       
    62      * default and extension icons.
       
    63      *
       
    64      * @return  An array filled with icons.
       
    65      */
       
    66     CPbk2IconArray* CreateListBoxIconArrayL()
       
    67         {
       
    68         FUNC_LOG;
       
    69         // Create icon array
       
    70         TResourceReader reader;
       
    71         CCoeEnv::Static()->CreateResourceReaderLC(reader, R_FSC_FIELDTYPE_ICONS);
       
    72         CPbk2IconArray* iconArray = CPbk2IconArray::NewL(reader);
       
    73         CleanupStack::PopAndDestroy(); // reader
       
    74 
       
    75         return iconArray;
       
    76         }
       
    77 
       
    78     } /// namespace
       
    79 
       
    80 
       
    81 /**
       
    82  * Specialized popup list class.
       
    83  */
       
    84 NONSHARABLE_CLASS( CFscSelectFieldDlg::CPopupList ) :
       
    85 public CAknPopupList
       
    86     {
       
    87 public: // Interface
       
    88 
       
    89     /**
       
    90      * Like CAknPopupList::NewL.
       
    91      * @see CAknPopupList::NewL
       
    92      */
       
    93     static CPopupList* NewL(
       
    94             CEikListBox* aListBox,
       
    95             TInt aCbaResource,
       
    96             AknPopupLayouts::TAknPopupLayouts aType =
       
    97             AknPopupLayouts::EMenuWindow );
       
    98 
       
    99     /**
       
   100      * Sets the listbox item to focus initially.
       
   101      * By default focus is on the first item.
       
   102      *
       
   103      * @param aFocusIndex  Index of the listbox item to focus initially.
       
   104      */
       
   105     inline void SetInitialFocus(
       
   106             TInt aFocusIndex );
       
   107 
       
   108     /**
       
   109      * Like CAknPopupList::AttemptExitL.
       
   110      * @see CAknPopupList::AttemptExitL
       
   111      */
       
   112     inline void AttemptExitL(
       
   113             TBool aAccept );
       
   114 
       
   115 private: // From CAknPopupList
       
   116     void SetupWindowLayout(
       
   117             AknPopupLayouts::TAknPopupLayouts aType );
       
   118 
       
   119 private: // Implementation
       
   120     inline CPopupList();
       
   121 
       
   122 private: // Data
       
   123     /// Own: Focus index
       
   124     TInt iFocusIndex;
       
   125     };
       
   126 
       
   127 /**
       
   128  * Specialized listbox class.
       
   129  */
       
   130 NONSHARABLE_CLASS( CFscSelectFieldDlg::CListBox ) :
       
   131 public CAknDoubleGraphicPopupMenuStyleListBox,
       
   132 public MFscClipListBoxText
       
   133     {
       
   134 public: // Interface
       
   135 
       
   136     /**
       
   137      * Constructor.
       
   138      */
       
   139     inline CListBox();
       
   140 
       
   141     /**
       
   142      * Sets the observer.
       
   143      *
       
   144      * @param aKeyObserver      The observer to set.
       
   145      */
       
   146     inline void SetObserver(
       
   147             MFscControlKeyObserver* aKeyObserver );
       
   148 
       
   149     /**
       
   150      * Returns CONE environment.
       
   151      *
       
   152      * @return  CONE environment.
       
   153      */
       
   154     inline CCoeEnv* CoeEnv();
       
   155 
       
   156 private: // From CAknDoubleGraphicPopupMenuStyleListBox
       
   157     TKeyResponse OfferKeyEventL(
       
   158             const TKeyEvent& aKeyEvent,
       
   159             TEventCode aType );
       
   160 
       
   161 private: // From MFscClipListBoxText
       
   162     TBool ClipFromBeginning(
       
   163             TDes& aBuffer,
       
   164             TInt aItemIndex,
       
   165             TInt aSubCellNumber );
       
   166 
       
   167 private: // Data
       
   168     /// Ref: Observer
       
   169     MFscControlKeyObserver* iKeyObserver;
       
   170     };
       
   171 
       
   172 // --------------------------------------------------------------------------
       
   173 // CFscSelectFieldDlg::CListBox::CPopupList
       
   174 // --------------------------------------------------------------------------
       
   175 //
       
   176 inline CFscSelectFieldDlg::CPopupList::CPopupList() :
       
   177     iFocusIndex(KErrNotFound)
       
   178     {
       
   179     FUNC_LOG;
       
   180     }
       
   181 
       
   182 // --------------------------------------------------------------------------
       
   183 // CFscSelectFieldDlg::CListBox::NewL
       
   184 // --------------------------------------------------------------------------
       
   185 //
       
   186 CFscSelectFieldDlg::CPopupList* CFscSelectFieldDlg::CPopupList::NewL(
       
   187         CEikListBox* aListBox, TInt aCbaResource,
       
   188         AknPopupLayouts::TAknPopupLayouts aType)
       
   189     {
       
   190     FUNC_LOG;
       
   191     CPopupList* self = new ( ELeave ) CPopupList();
       
   192     CleanupStack::PushL(self);
       
   193     self->ConstructL(aListBox, aCbaResource, aType);
       
   194     CleanupStack::Pop(self);
       
   195     return self;
       
   196     }
       
   197 
       
   198 // --------------------------------------------------------------------------
       
   199 // CFscSelectFieldDlg::CListBox::SetInitialFocus
       
   200 // --------------------------------------------------------------------------
       
   201 //
       
   202 inline void CFscSelectFieldDlg::CPopupList::SetInitialFocus(TInt aFocusIndex)
       
   203     {
       
   204     FUNC_LOG;
       
   205     iFocusIndex = aFocusIndex;
       
   206     }
       
   207 
       
   208 // --------------------------------------------------------------------------
       
   209 // CFscSelectFieldDlg::CPopupList::AttemptExitL
       
   210 // --------------------------------------------------------------------------
       
   211 //
       
   212 inline void CFscSelectFieldDlg::CPopupList::AttemptExitL(TBool aAccept)
       
   213     {
       
   214     FUNC_LOG;
       
   215     CAknPopupList::AttemptExitL(aAccept);
       
   216     }
       
   217 
       
   218 // --------------------------------------------------------------------------
       
   219 // CFscSelectFieldDlg::CListBox::SetupWindowLayout
       
   220 // --------------------------------------------------------------------------
       
   221 //
       
   222 void CFscSelectFieldDlg::CPopupList::SetupWindowLayout(
       
   223         AknPopupLayouts::TAknPopupLayouts aType)
       
   224     {
       
   225     FUNC_LOG;
       
   226     // Call base class
       
   227     CAknPopupList::SetupWindowLayout(aType);
       
   228     // Set initial focus
       
   229     if (iFocusIndex >= 0)
       
   230         {
       
   231         ListBox()->SetCurrentItemIndex(iFocusIndex);
       
   232         }
       
   233     }
       
   234 
       
   235 // --------------------------------------------------------------------------
       
   236 // CFscSelectFieldDlg::CListBox::CListBox
       
   237 // --------------------------------------------------------------------------
       
   238 //
       
   239 inline CFscSelectFieldDlg::CListBox::CListBox()
       
   240     {
       
   241     FUNC_LOG;
       
   242     }
       
   243 
       
   244 // --------------------------------------------------------------------------
       
   245 // CFscSelectFieldDlg::CListBox::SetObserver
       
   246 // --------------------------------------------------------------------------
       
   247 //
       
   248 inline void CFscSelectFieldDlg::CListBox::SetObserver(
       
   249         MFscControlKeyObserver* aKeyObserver)
       
   250     {
       
   251     FUNC_LOG;
       
   252     iKeyObserver = aKeyObserver;
       
   253     }
       
   254 
       
   255 // --------------------------------------------------------------------------
       
   256 // CFscSelectFieldDlg::CListBox::CoeEnv
       
   257 // --------------------------------------------------------------------------
       
   258 //
       
   259 inline CCoeEnv* CFscSelectFieldDlg::CListBox::CoeEnv()
       
   260     {
       
   261     FUNC_LOG;
       
   262     return iCoeEnv;
       
   263     }
       
   264 
       
   265 // --------------------------------------------------------------------------
       
   266 // CFscSelectFieldDlg::CListBox::OfferKeyEventL
       
   267 // --------------------------------------------------------------------------
       
   268 //
       
   269 TKeyResponse CFscSelectFieldDlg::CListBox::OfferKeyEventL(
       
   270         const TKeyEvent& aKeyEvent, TEventCode aType)
       
   271     {
       
   272     FUNC_LOG;
       
   273     TKeyResponse ret = EKeyWasNotConsumed;
       
   274 
       
   275     if (iKeyObserver && iKeyObserver->FscControlKeyEventL(aKeyEvent, aType)
       
   276             == EKeyWasConsumed)
       
   277         {
       
   278         ret = EKeyWasConsumed;
       
   279         }
       
   280     else
       
   281         {
       
   282         ret = CAknDoubleGraphicPopupMenuStyleListBox::OfferKeyEventL(
       
   283                 aKeyEvent, aType);
       
   284         }
       
   285 
       
   286     return ret;
       
   287     }
       
   288 
       
   289 // --------------------------------------------------------------------------
       
   290 // CFscSelectFieldDlg::CListBox::ClipFromBeginning
       
   291 // --------------------------------------------------------------------------
       
   292 //
       
   293 TBool CFscSelectFieldDlg::CListBox::ClipFromBeginning(TDes& aBuffer,
       
   294         TInt aItemIndex, TInt aSubCellNumber)
       
   295     {
       
   296     FUNC_LOG;
       
   297     return AknTextUtils::ClipToFit(aBuffer, AknTextUtils::EClipFromBeginning,
       
   298             this, aItemIndex, aSubCellNumber);
       
   299     }
       
   300 
       
   301 // --------------------------------------------------------------------------
       
   302 // CFscSelectFieldDlg::CFscSelectFieldDlg
       
   303 // --------------------------------------------------------------------------
       
   304 //
       
   305 CFscSelectFieldDlg::CFscSelectFieldDlg()
       
   306     {
       
   307     FUNC_LOG;
       
   308     __ASSERT_DEBUG(
       
   309             !iListBox && !iPopupList && !iKeyObserver && !iDestroyedPtr,
       
   310             Panic(EPanicPostCond_Constructor));
       
   311     }
       
   312 
       
   313 // --------------------------------------------------------------------------
       
   314 // CFscSelectFieldDlg::~CFscSelectFieldDlg
       
   315 // --------------------------------------------------------------------------
       
   316 //
       
   317 CFscSelectFieldDlg::~CFscSelectFieldDlg()
       
   318     {
       
   319     FUNC_LOG;
       
   320     if (iSelfPtr)
       
   321         {
       
   322         *iSelfPtr = NULL;
       
   323         }
       
   324     if (iDestroyedPtr)
       
   325         {
       
   326         *iDestroyedPtr = ETrue;
       
   327         }
       
   328     if (iPopupList)
       
   329         {
       
   330         iPopupList->CancelPopup();
       
   331         }
       
   332 
       
   333     delete iListBox;
       
   334     }
       
   335 
       
   336 // --------------------------------------------------------------------------
       
   337 // CFscSelectFieldDlg::ExecuteLD
       
   338 // --------------------------------------------------------------------------
       
   339 //
       
   340 MVPbkStoreContactField* CFscSelectFieldDlg::ExecuteLD( 
       
   341         MVPbkStoreContactFieldCollection& aFieldCollection,
       
   342         const CVPbkContactManager& aContactManager,
       
   343         const MPbk2FieldPropertyArray& aFieldProperties, TInt aCbaResourceId,
       
   344         const TDesC& aHeading /*=KNullDesC*/, TInt aFocusIndex /*=KErrNotFound*/)
       
   345     {
       
   346     FUNC_LOG;
       
   347     __ASSERT_DEBUG(!iListBox && !iPopupList, Panic(EPanicPreCond_ExecuteLD));
       
   348 
       
   349     // "D" function semantics
       
   350     CleanupStack::PushL( this);
       
   351     TBool thisDestroyed = EFalse;
       
   352     // Destructor will set thisDestroyed to ETrue if this object is destroyed
       
   353     iDestroyedPtr = &thisDestroyed;
       
   354 
       
   355     CFscPresentationContactFieldCollection* fields =
       
   356             CFscPresentationContactFieldCollection::NewL(aFieldProperties,
       
   357                     aFieldCollection, aFieldCollection.ParentStoreContact() );
       
   358     CleanupStack::PushL(fields);
       
   359 
       
   360     if (aFocusIndex != KErrNotFound)
       
   361         {
       
   362         // The focus index is from store contact field order and
       
   363         // we have to map it to presentation contact field order
       
   364         MVPbkStoreContactField* initiallyFocusedField =
       
   365                 aFieldCollection.FieldAtLC(aFocusIndex);
       
   366         for (TInt i( 0); i < fields->FieldCount(); ++i)
       
   367             {
       
   368             if (fields->FieldAt( i ).IsSame( *initiallyFocusedField) )
       
   369                 {
       
   370                 // Fix the focus index to match with
       
   371                 // presentation contact fields
       
   372                 aFocusIndex = i;
       
   373                 break;
       
   374                 }
       
   375             }
       
   376         CleanupStack::PopAndDestroy(); // initiallyFocusedField
       
   377         }
       
   378 
       
   379     // Create list box
       
   380     CreateListBoxL( *fields, aContactManager, aFieldProperties,
       
   381             aCbaResourceId, aHeading, aFocusIndex);
       
   382 
       
   383     // Show the list query
       
   384     TInt dlgResult = 0;
       
   385     TRAPD( err, dlgResult = iPopupList->ExecuteLD() )
       
   386     ;
       
   387     if ( !thisDestroyed)
       
   388         {
       
   389         iPopupList = NULL;
       
   390         }
       
   391     if (err != KErrNone)
       
   392         {
       
   393         User::Leave(err);
       
   394         }
       
   395 
       
   396     // Get the return value
       
   397     MVPbkStoreContactField* result= NULL;
       
   398     if (thisDestroyed)
       
   399         {
       
   400         // This object has been destroyed
       
   401         CleanupStack::PopAndDestroy(fields);
       
   402         CleanupStack::Pop(); // this
       
   403         }
       
   404     else
       
   405         {
       
   406         if (dlgResult)
       
   407             {
       
   408             TInt index = iListBox->CurrentItemIndex();
       
   409             result = fields->FieldAt( index ).CloneLC();
       
   410             CleanupStack::Pop();
       
   411             }
       
   412         CleanupStack::PopAndDestroy(fields);
       
   413         CleanupStack::PopAndDestroy(); // this
       
   414         }
       
   415 
       
   416     return result;
       
   417     }
       
   418 
       
   419 // --------------------------------------------------------------------------
       
   420 // CFscSelectFieldDlg::SetObserver
       
   421 // --------------------------------------------------------------------------
       
   422 //
       
   423 void CFscSelectFieldDlg::SetObserver(MFscControlKeyObserver* aKeyObserver)
       
   424     {
       
   425     FUNC_LOG;
       
   426     iKeyObserver = aKeyObserver;
       
   427     }
       
   428 
       
   429 // --------------------------------------------------------------------------
       
   430 // CFscSelectFieldDlg::AttemptExitL
       
   431 // --------------------------------------------------------------------------
       
   432 //
       
   433 void CFscSelectFieldDlg::AttemptExitL(TBool aAccept)
       
   434     {
       
   435     FUNC_LOG;
       
   436     __ASSERT_DEBUG( iPopupList, Panic( EPanicPreCond_AttemptExitL ) );
       
   437     iPopupList->AttemptExitL(aAccept);
       
   438     }
       
   439 
       
   440 // --------------------------------------------------------------------------
       
   441 // CFscSelectFieldDlg::ResetWhenDestroyed
       
   442 // --------------------------------------------------------------------------
       
   443 //
       
   444 void CFscSelectFieldDlg::ResetWhenDestroyed(CFscSelectFieldDlg** aSelfPtr)
       
   445     {
       
   446     FUNC_LOG;
       
   447     __ASSERT_DEBUG(!aSelfPtr || *aSelfPtr==this,
       
   448             Panic(EPanicPreCond_ResetWhenDestroyed));
       
   449 
       
   450     iSelfPtr = aSelfPtr;
       
   451     }
       
   452 
       
   453 // --------------------------------------------------------------------------
       
   454 // CFscSelectFieldDlg::CreateListBoxL
       
   455 // --------------------------------------------------------------------------
       
   456 //
       
   457 void CFscSelectFieldDlg::CreateListBoxL(
       
   458         const CFscPresentationContactFieldCollection& aFieldCollection,
       
   459         const CVPbkContactManager& aContactManager,
       
   460         const MPbk2FieldPropertyArray& aFieldProperties, TInt aCbaResourceId,
       
   461         const TDesC& aHeading, TInt aFocusIndex)
       
   462     {
       
   463     FUNC_LOG;
       
   464     // Create a list box
       
   465     iListBox = new(ELeave) CListBox;
       
   466 
       
   467     // Create a popup list
       
   468     CPopupList* popupList = CPopupList::NewL(iListBox, aCbaResourceId,
       
   469             AknPopupLayouts::EMenuDoubleLargeGraphicWindow);
       
   470     CleanupStack::PushL(popupList);
       
   471 
       
   472     // Init list box
       
   473     iListBox->ConstructL(popupList, CEikListBox::ELeftDownInViewRect);
       
   474     iListBox->SetObserver(iKeyObserver);
       
   475 
       
   476     // Set title of popuplist
       
   477     if (aHeading.Length() > 0)
       
   478         {
       
   479         popupList->SetTitleL(aHeading);
       
   480         }
       
   481 
       
   482     // Create icon array
       
   483     CPbk2IconArray* iconArray = CreateListBoxIconArrayL();
       
   484     iListBox->ItemDrawer()->ColumnData()->SetIconArray(iconArray);
       
   485 
       
   486     // Create list box model
       
   487     HBufC* timeFormat = iListBox->CoeEnv()->AllocReadResourceLC( R_QTN_DATE_USUAL);
       
   488 
       
   489     CFscFieldListBoxModel::TParams params(aFieldCollection, aContactManager,
       
   490             aFieldProperties, *timeFormat, *iconArray, 
       
   491             NULL, 
       
   492             NULL);
       
   493     CFscFieldListBoxModel* listBoxModel = CFscFieldListBoxModel::NewL(params);
       
   494     CleanupStack::PushL(listBoxModel);
       
   495     listBoxModel->FormatFieldsL();
       
   496     CleanupStack::Pop(listBoxModel);
       
   497     CleanupStack::PopAndDestroy(); // timeFormat
       
   498     listBoxModel->SetClipper( *iListBox);
       
   499     iListBox->Model()->SetItemTextArray(listBoxModel);
       
   500     iListBox->Model()->SetOwnershipType(ELbmOwnsItemArray);
       
   501     iListBox->Reset();
       
   502 
       
   503     if (aFocusIndex >= 0 && aFocusIndex < iListBox->Model()->NumberOfItems() )
       
   504         {
       
   505         popupList->SetInitialFocus(aFocusIndex);
       
   506         }
       
   507 
       
   508     iListBox->CreateScrollBarFrameL(ETrue);
       
   509     iListBox->ScrollBarFrame()->SetScrollBarVisibilityL(CEikScrollBarFrame::EOff,
       
   510             CEikScrollBarFrame::EAuto);
       
   511 
       
   512     CleanupStack::Pop(); // listBox
       
   513     iPopupList = popupList;
       
   514     }
       
   515 
       
   516 // End of File
       
   517