phonebookui/Phonebook2/GroupExtension/src/CPguGroupPopup.cpp
changeset 0 e686773b3f54
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     1 /*
       
     2 * Copyright (c) 2005-2007 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:  Phonebook 2 group popup.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "CPguGroupPopup.h"
       
    20 
       
    21 // Phonebook 2
       
    22 #include <CPbk2InputAbsorber.h>
       
    23 #include <MPbk2ContactNameFormatter.h>
       
    24 #include <MPbk2ApplicationServices.h>
       
    25 #include <MPbk2AppUi.h>
       
    26 
       
    27 // Virtual Phonebook
       
    28 #include <MVPbkContactViewBase.h>
       
    29 #include <MVPbkViewContact.h>
       
    30 #include <MVPbkContactLink.h>
       
    31 #include <MVPbkContactLinkArray.h>
       
    32 #include <CVPbkFilteredContactView.h>
       
    33 #include <CVPbkContactManager.h>
       
    34 
       
    35 // System includes
       
    36 #include <aknlists.h>
       
    37 #include <aknPopup.h>
       
    38 #include <avkon.rsg>
       
    39 #include <StringLoader.h>
       
    40 
       
    41 /// Unnamed namespace for local definitions
       
    42 namespace {
       
    43 
       
    44 const TInt KMaxListBoxText( 256 );
       
    45 
       
    46 } /// namespace
       
    47 
       
    48 // CLASS DECLARATION
       
    49 
       
    50 class CPopupList : public CAknPopupList
       
    51     {
       
    52     public: // Construction and destruction
       
    53 
       
    54         /**
       
    55          * Creates a new instance of this class.
       
    56          *
       
    57          * @param aListbox      The listbox to use in the list.
       
    58          * @param aCbaResource  Softkey resource id.
       
    59          * @return  A new instance of this class.
       
    60          */
       
    61         static CPopupList* NewLC(
       
    62                 CEikListBox* aListBox,
       
    63                 TInt aCbaResource );
       
    64 
       
    65         /**
       
    66          * Destructor.
       
    67          */
       
    68         ~CPopupList();
       
    69 
       
    70     public: // Interface
       
    71 
       
    72         /**
       
    73          * Sets the object to resets the self pointer after destruction.
       
    74          *
       
    75          * @param aSelf     Self pointer.
       
    76          */
       
    77         void ResetWhenDestroyed(
       
    78                 CPopupList** aSelf );
       
    79 
       
    80     protected: // Implementation
       
    81         CPopupList();
       
    82 
       
    83     private: // Data
       
    84         /// Ref: Self pointer
       
    85         CPopupList** iSelfPtr;
       
    86     };
       
    87 
       
    88 class CListBoxModel : public CBase,
       
    89                       public MDesCArray
       
    90     {
       
    91     public: // Construction
       
    92 
       
    93         /**
       
    94          * Constructor.
       
    95          *
       
    96          * @param aView             View.
       
    97          * @param aNameFormatter    Name formatter
       
    98          */
       
    99         CListBoxModel(
       
   100                 MVPbkContactViewBase& aView,
       
   101                 MPbk2ContactNameFormatter& aNameFormatter ) :
       
   102                     iView( aView ),
       
   103                     iNameFormatter( aNameFormatter )
       
   104                         {
       
   105                         }
       
   106 
       
   107     public: // From MDesCArray
       
   108         TInt MdcaCount() const;
       
   109         TPtrC MdcaPoint(
       
   110                 TInt aIndex ) const;
       
   111 
       
   112     private: // Implementation
       
   113         void FormatBufferL(
       
   114                 TInt aIndex ) const;
       
   115 
       
   116     private: // Data
       
   117         // Ref: View
       
   118         MVPbkContactViewBase& iView;
       
   119         // Ref: Name formatter
       
   120         MPbk2ContactNameFormatter& iNameFormatter;
       
   121         // Own: Formatting buffer
       
   122         mutable TBuf<KMaxListBoxText> iFormattingBuffer;
       
   123         // Own: Counter
       
   124         mutable TInt iPreviousCount;
       
   125     };
       
   126 
       
   127 // --------------------------------------------------------------------------
       
   128 // CListBoxModel::MdcaCount
       
   129 // --------------------------------------------------------------------------
       
   130 //
       
   131 TInt CListBoxModel::MdcaCount() const
       
   132     {
       
   133     TInt result = iPreviousCount;
       
   134     TRAPD( err, result = iView.ContactCountL() );
       
   135     if ( err != KErrNone )
       
   136         {
       
   137         CCoeEnv::Static()->HandleError( err );
       
   138         }
       
   139     iPreviousCount = result;
       
   140     return result;
       
   141     }
       
   142 
       
   143 // --------------------------------------------------------------------------
       
   144 // CListBoxModel::MdcaPoint
       
   145 // --------------------------------------------------------------------------
       
   146 //
       
   147 TPtrC CListBoxModel::MdcaPoint( TInt aIndex ) const
       
   148     {
       
   149     iFormattingBuffer.Zero();
       
   150 
       
   151     TRAPD( err, FormatBufferL( aIndex ) );
       
   152     switch ( err )
       
   153         {
       
   154         case KErrNone:
       
   155             {
       
   156             // OK
       
   157             break;
       
   158             }
       
   159         case KErrNotFound:      // FALLTHROUGH
       
   160         case KErrAccessDenied:
       
   161             {
       
   162             // Do not report these errors as they occur sometimes when
       
   163             // the view is under a massive update
       
   164             break;
       
   165             }
       
   166         default:
       
   167             {
       
   168             // Report error
       
   169             CCoeEnv::Static()->HandleError( err );
       
   170             break;
       
   171             }
       
   172         }
       
   173     return iFormattingBuffer;
       
   174     }
       
   175 
       
   176 // --------------------------------------------------------------------------
       
   177 // CListBoxModel::FormatBufferL
       
   178 // --------------------------------------------------------------------------
       
   179 //
       
   180 void CListBoxModel::FormatBufferL( TInt aIndex ) const
       
   181     {
       
   182     const TInt KDefaultListFormatting =
       
   183         MPbk2ContactNameFormatter::EUseSeparator |
       
   184         MPbk2ContactNameFormatter::EPreserveLeadingSpaces;
       
   185 
       
   186     const MVPbkViewContact& contact = iView.ContactAtL( aIndex );
       
   187     HBufC* name = iNameFormatter.GetContactTitleL(
       
   188         contact.Fields(),
       
   189         KDefaultListFormatting );
       
   190     iFormattingBuffer.Append( *name );
       
   191     delete name;
       
   192     }
       
   193 
       
   194 // --------------------------------------------------------------------------
       
   195 // CPopupList::CPopupList
       
   196 // --------------------------------------------------------------------------
       
   197 //
       
   198 inline CPopupList::CPopupList()
       
   199     {
       
   200     }
       
   201 
       
   202 // --------------------------------------------------------------------------
       
   203 // CPopupList::NewLC
       
   204 // --------------------------------------------------------------------------
       
   205 //
       
   206 CPopupList* CPopupList::NewLC( CEikListBox* aListBox, TInt aCbaResource )
       
   207     {
       
   208     CPopupList* self = new(ELeave) CPopupList;
       
   209     CleanupStack::PushL(self);
       
   210     self->CAknPopupList::ConstructL(
       
   211             aListBox,
       
   212             aCbaResource,
       
   213             AknPopupLayouts::EMenuWindow );
       
   214     return self;
       
   215     }
       
   216 
       
   217 // --------------------------------------------------------------------------
       
   218 // CPopupList::~CPopupList
       
   219 // --------------------------------------------------------------------------
       
   220 //
       
   221 CPopupList::~CPopupList()
       
   222     {
       
   223     }
       
   224 
       
   225 // --------------------------------------------------------------------------
       
   226 // CPguGroupPopup::CPguGroupPopup
       
   227 // --------------------------------------------------------------------------
       
   228 //
       
   229 CPguGroupPopup::CPguGroupPopup
       
   230         ( MVPbkContactLinkArray* aGroupsJoined, TInt32 aTitleResId,
       
   231           TInt32 aEmptyTextResId, TInt32 aSoftKeyResId,
       
   232           TInt32 aListBoxFlags ) :
       
   233             iTitleResId( aTitleResId ),
       
   234             iEmptyTextResId( aEmptyTextResId ),
       
   235             iSoftKeyResId( aSoftKeyResId ),
       
   236             iListBoxFlags( aListBoxFlags ),
       
   237             iGroupsJoined( aGroupsJoined )
       
   238     {
       
   239     }
       
   240 
       
   241 // --------------------------------------------------------------------------
       
   242 // CPguGroupPopup::ConstructL
       
   243 // --------------------------------------------------------------------------
       
   244 //
       
   245 inline void CPguGroupPopup::ConstructL
       
   246         ( MVPbkContactViewBase& aAllGroupsView )
       
   247     {
       
   248     iGroupView = CVPbkFilteredContactView::NewL
       
   249         ( aAllGroupsView, *this, *this );
       
   250     }
       
   251 
       
   252 // --------------------------------------------------------------------------
       
   253 // CPguGroupPopup::NewL
       
   254 // --------------------------------------------------------------------------
       
   255 //
       
   256 CPguGroupPopup* CPguGroupPopup::NewL
       
   257         ( MVPbkContactLinkArray* aGroupsJoined,
       
   258           TInt32 aTitleResId, TInt32 aEmptyTextResId,
       
   259           TInt32 aSoftKeyResId, TInt32 aListBoxFlags,
       
   260           MVPbkContactViewBase& aAllGroupsView )
       
   261     {
       
   262     CPguGroupPopup* self = new ( ELeave ) CPguGroupPopup
       
   263         ( aGroupsJoined, aTitleResId, aEmptyTextResId,
       
   264           aSoftKeyResId, aListBoxFlags );
       
   265     CleanupStack::PushL( self );
       
   266     self->ConstructL( aAllGroupsView );
       
   267     CleanupStack::Pop( self );
       
   268     return self;
       
   269     }
       
   270 
       
   271 // --------------------------------------------------------------------------
       
   272 // CPguGroupPopup::~CPguGroupPopup
       
   273 // --------------------------------------------------------------------------
       
   274 //
       
   275 CPguGroupPopup::~CPguGroupPopup()
       
   276     {
       
   277     if ( iGroupView )
       
   278         {
       
   279         iGroupView->RemoveObserver( *this );
       
   280         }
       
   281 
       
   282     delete iGroupView;
       
   283     delete iInputAbsorber;
       
   284     delete iSelectedGroup;
       
   285     }
       
   286 
       
   287 // --------------------------------------------------------------------------
       
   288 // CPguGroupPopup::ExecuteLD
       
   289 // --------------------------------------------------------------------------
       
   290 //
       
   291 MVPbkContactLink* CPguGroupPopup::ExecuteLD()
       
   292     {
       
   293     CleanupStack::PushL( this );
       
   294 
       
   295     iInputAbsorber = CPbk2InputAbsorber::NewL( R_AVKON_SOFTKEYS_CANCEL );
       
   296     iInputAbsorber->SetCommandObserver( this );
       
   297 
       
   298     iInputAbsorber->Wait();
       
   299 
       
   300     MVPbkContactLink* result = iSelectedGroup;
       
   301     iSelectedGroup = NULL;
       
   302     CleanupStack::PopAndDestroy( this );
       
   303     return result;
       
   304     }
       
   305 
       
   306 // --------------------------------------------------------------------------
       
   307 // CPguGroupPopup::ContactViewReady
       
   308 // --------------------------------------------------------------------------
       
   309 //
       
   310 void CPguGroupPopup::ContactViewReady( MVPbkContactViewBase& /*aView*/ )
       
   311     {
       
   312     TRAPD( err, RunPopupL() );
       
   313     if ( err != KErrNone )
       
   314         {
       
   315         CCoeEnv::Static()->HandleError( err );
       
   316         Close();
       
   317         }
       
   318     }
       
   319 
       
   320 // --------------------------------------------------------------------------
       
   321 // CPguGroupPopup::ContactViewUnavailable
       
   322 // --------------------------------------------------------------------------
       
   323 //
       
   324 void CPguGroupPopup::ContactViewUnavailable
       
   325         ( MVPbkContactViewBase& /*aView*/ )
       
   326     {
       
   327     //Do nothing
       
   328     }
       
   329 
       
   330 // --------------------------------------------------------------------------
       
   331 // CPguGroupPopup::ContactAddedToView
       
   332 // --------------------------------------------------------------------------
       
   333 //
       
   334 void CPguGroupPopup::ContactAddedToView(
       
   335         MVPbkContactViewBase& /*aView*/,
       
   336         TInt /*aIndex*/,
       
   337         const MVPbkContactLink& /*aContactLink*/ )
       
   338     {
       
   339     //Do nothing
       
   340     }
       
   341 
       
   342 // --------------------------------------------------------------------------
       
   343 // CPguGroupPopup::ContactRemovedFromView
       
   344 // --------------------------------------------------------------------------
       
   345 //
       
   346 void CPguGroupPopup::ContactRemovedFromView(
       
   347         MVPbkContactViewBase& /*aView*/,
       
   348         TInt /*aIndex*/,
       
   349         const MVPbkContactLink& /*aContactLink*/ )
       
   350     {
       
   351     //Do nothing
       
   352     }
       
   353 
       
   354 // --------------------------------------------------------------------------
       
   355 // CPguGroupPopup::ContactViewError
       
   356 // --------------------------------------------------------------------------
       
   357 //
       
   358 void CPguGroupPopup::ContactViewError(
       
   359         MVPbkContactViewBase& /*aView*/,
       
   360         TInt /*aError*/,
       
   361         TBool /*aErrorNotified*/ )
       
   362     {
       
   363     //Do nothing
       
   364     }
       
   365 
       
   366 // --------------------------------------------------------------------------
       
   367 // CPguGroupPopup::ProcessCommandL
       
   368 // --------------------------------------------------------------------------
       
   369 //
       
   370 void CPguGroupPopup::ProcessCommandL( TInt aCommandId )
       
   371     {
       
   372     switch ( aCommandId )
       
   373         {
       
   374         case EAknSoftkeyCancel: // FALLTHROUGH
       
   375         case EAknSoftkeyBack:   // FALLTHROUGH
       
   376         case EAknSoftkeyNo:     // FALLTHROUGH
       
   377         case EAknSoftkeyClose:  // FALLTHROUGH
       
   378         case EAknSoftkeyExit:
       
   379             {
       
   380             Close();
       
   381             break;
       
   382             }
       
   383 
       
   384         default:
       
   385             break;
       
   386         }
       
   387     }
       
   388 
       
   389 // --------------------------------------------------------------------------
       
   390 // CPguGroupPopup::Close
       
   391 // --------------------------------------------------------------------------
       
   392 //
       
   393 void CPguGroupPopup::Close()
       
   394     {
       
   395     delete this;
       
   396     }
       
   397 
       
   398 // --------------------------------------------------------------------------
       
   399 // CPguGroupPopup::RunPopupL
       
   400 // --------------------------------------------------------------------------
       
   401 //
       
   402 void CPguGroupPopup::RunPopupL()
       
   403     {
       
   404     CEikColumnListBox* listBox = static_cast<CEikColumnListBox*>(
       
   405         EikControlFactory::CreateByTypeL(
       
   406             EAknCtSinglePopupMenuListBox ).iControl );
       
   407     CleanupStack::PushL( listBox );
       
   408 
       
   409     CPopupList* popupList = CPopupList::NewLC( listBox, iSoftKeyResId );
       
   410     listBox->ConstructL( popupList,
       
   411         iListBoxFlags | CEikListBox::ELeftDownInViewRect );
       
   412 
       
   413     // Create own listbox model
       
   414     CListBoxModel* listBoxModel =
       
   415             new (ELeave) CListBoxModel( *iGroupView,
       
   416             Phonebook2::Pbk2AppUi()->ApplicationServices().NameFormatter() );
       
   417 
       
   418     listBox->Model()->SetOwnershipType( ELbmOwnsItemArray );
       
   419     listBox->Model()->SetItemTextArray( listBoxModel );
       
   420     listBox->CreateScrollBarFrameL( ETrue );
       
   421     listBox->ScrollBarFrame()->SetScrollBarVisibilityL(
       
   422             CEikScrollBarFrame::EOff,
       
   423             CEikScrollBarFrame::EAuto );
       
   424 
       
   425     HBufC* prompt = StringLoader::LoadLC( iTitleResId );
       
   426     popupList->SetTitleL( *prompt );
       
   427     CleanupStack::PopAndDestroy(); // prompt
       
   428 
       
   429     HBufC* empty = StringLoader::LoadLC( iEmptyTextResId );
       
   430     listBox->View()->SetListEmptyTextL( *empty );
       
   431     CleanupStack::PopAndDestroy(); // empty
       
   432 
       
   433     iResult = popupList->ExecuteLD();
       
   434     CleanupStack::Pop( popupList ); // destroyed in ExecuteLD
       
   435     if ( iResult )
       
   436         {
       
   437         TInt index = listBox->CurrentItemIndex();
       
   438         iSelectedGroup = iGroupView->ContactAtL( index ).CreateLinkLC();
       
   439         CleanupStack::Pop();
       
   440         }
       
   441     CleanupStack::PopAndDestroy( listBox );
       
   442 
       
   443     // Wait is started in ExecuteLD()
       
   444     iInputAbsorber->StopWait();
       
   445     }
       
   446 
       
   447 // --------------------------------------------------------------------------
       
   448 // CPguGroupPopup::IsContactIncluded
       
   449 // --------------------------------------------------------------------------
       
   450 //
       
   451 TBool CPguGroupPopup::IsContactIncluded(
       
   452         const MVPbkBaseContact& aContact )
       
   453     {
       
   454     TBool result = EFalse;
       
   455     const TInt count = iGroupsJoined->Count();
       
   456     for ( TInt i(0); i < count; ++i )
       
   457         {
       
   458         if ( iGroupsJoined->At( i ).RefersTo( aContact ) )
       
   459             {
       
   460             result = ETrue;
       
   461             break;
       
   462             }
       
   463         }
       
   464     return result;
       
   465     }
       
   466 //  End of File