phonebookui/Phonebook2/GroupExtension/src/CPguGroupNameQueryDlg.cpp
changeset 0 e686773b3f54
child 6 e8e3147d53eb
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 name query dialog.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "CPguGroupNameQueryDlg.h"
       
    20 
       
    21 // Phonebook 2
       
    22 #include "Pbk2GroupConsts.h"
       
    23 #include <MPbk2ContactNameFormatter.h>
       
    24 #include <Pbk2GroupUIRes.rsg>
       
    25 #include <Pbk2PresentationUtils.h>
       
    26 
       
    27 // Virtual Phonebook
       
    28 #include <MVPbkContactGroup.h>
       
    29 #include <MVPbkContactStore.h>
       
    30 #include <MVPbkContactViewBase.h>
       
    31 
       
    32 // System includes
       
    33 #include <aknnotewrappers.h>
       
    34 #include <StringLoader.h>
       
    35 
       
    36 /// Unnamed namespace for local definitions
       
    37 namespace {
       
    38 
       
    39 /**
       
    40  * Strips directionality markers from given text.
       
    41  *
       
    42  * @param aText     The text to strip.
       
    43  */
       
    44 void StripCharacters( TDes& aText)
       
    45     {
       
    46     // Strip any directionality markers to get pure text
       
    47     const TUint32 KPbk2LeftToRightMarker = 0x200F;
       
    48     const TUint32 KPbk2RightToLeftMarker = 0x200E;
       
    49     const TInt markersLength( 2 );
       
    50     TBuf<markersLength> bufMarkers;
       
    51     bufMarkers.Append( KPbk2LeftToRightMarker );
       
    52     bufMarkers.Append( KPbk2RightToLeftMarker );
       
    53     AknTextUtils::StripCharacters( aText, bufMarkers );
       
    54     }
       
    55 
       
    56 } /// namespace
       
    57 
       
    58 // --------------------------------------------------------------------------
       
    59 // CPguGroupNameQueryDlg::CPguGroupNameQueryDlg
       
    60 // --------------------------------------------------------------------------
       
    61 //
       
    62  CPguGroupNameQueryDlg::CPguGroupNameQueryDlg
       
    63         ( TDes& aDataText, MVPbkContactViewBase& aGroupsView,
       
    64           MPbk2ContactNameFormatter& aNameFormatter ) :
       
    65             CAknTextQueryDialog( aDataText ),
       
    66             iGroupsListView( aGroupsView ),
       
    67             iNameFormatter( aNameFormatter )
       
    68     {
       
    69     }
       
    70 
       
    71 // --------------------------------------------------------------------------
       
    72 // CPguGroupNameQueryDlg::~CPguGroupNameQueryDlg
       
    73 // --------------------------------------------------------------------------
       
    74 //
       
    75 CPguGroupNameQueryDlg::~CPguGroupNameQueryDlg()
       
    76     {
       
    77     delete iOriginalName;
       
    78     }
       
    79 
       
    80 // --------------------------------------------------------------------------
       
    81 // CPguGroupNameQueryDlg::ConstructL
       
    82 // --------------------------------------------------------------------------
       
    83 //
       
    84 inline void CPguGroupNameQueryDlg::ConstructL
       
    85         ( TDes& aDataText, TBool aNameGeneration )
       
    86     {
       
    87     // Take a copy of the original name
       
    88     iOriginalName = HBufC::NewL( KGroupLabelLength );
       
    89     TPtr originalTextPtr = iOriginalName->Des();
       
    90     originalTextPtr.Append( aDataText );
       
    91 
       
    92     if ( aNameGeneration )
       
    93         {
       
    94         UpdateGroupTitleL();
       
    95         }
       
    96     }
       
    97 
       
    98 // --------------------------------------------------------------------------
       
    99 // CPguGroupNameQueryDlg::NewL
       
   100 // --------------------------------------------------------------------------
       
   101 //
       
   102 CPguGroupNameQueryDlg* CPguGroupNameQueryDlg::NewL
       
   103         ( TDes& aDataText, MVPbkContactViewBase& aGroupsView,
       
   104           MPbk2ContactNameFormatter& aNameFormatter,
       
   105           TBool aNameGeneration )
       
   106     {
       
   107     CPguGroupNameQueryDlg* self = new ( ELeave ) CPguGroupNameQueryDlg
       
   108         ( aDataText, aGroupsView, aNameFormatter );
       
   109     CleanupStack::PushL( self );
       
   110     self->ConstructL( aDataText, aNameGeneration );
       
   111     CleanupStack::Pop( self );
       
   112     return self;
       
   113     }
       
   114 
       
   115 // --------------------------------------------------------------------------
       
   116 // CPguGroupNameQueryDlg::OkToExitL
       
   117 // --------------------------------------------------------------------------
       
   118 //
       
   119 TBool CPguGroupNameQueryDlg::OkToExitL( TInt aButtonId )
       
   120     {
       
   121     TBool result = CAknTextQueryDialog::OkToExitL( aButtonId );
       
   122 
       
   123     HBufC* text = Text().AllocLC();
       
   124     TPtr textPtr( text->Des() );
       
   125     StripCharacters( textPtr );
       
   126 
       
   127     // Format text
       
   128     HBufC* formattedText = HBufC::NewLC( text->Length() );
       
   129     TPtr formattedTextPtr( formattedText->Des() );
       
   130     Pbk2PresentationUtils::TrimRightAppend( *text, formattedTextPtr );
       
   131 
       
   132     TBool nameAlreadyExists = ContainsL( *formattedText );
       
   133 
       
   134     if ( nameAlreadyExists )
       
   135         {
       
   136         if (iOriginalName->CompareC( *formattedText ) != 0 )
       
   137         	{
       
   138             // Display information note
       
   139             HBufC* prompt = StringLoader::LoadLC(
       
   140                     R_QTN_FLDR_NAME_ALREADY_USED, *formattedText );
       
   141     
       
   142             CAknInformationNote* dlg = new(ELeave) CAknInformationNote( ETrue );
       
   143             dlg->ExecuteLD( *prompt );
       
   144             CleanupStack::PopAndDestroy(); // prompt
       
   145     
       
   146             CAknQueryControl* queryControl = QueryControl();
       
   147             if ( queryControl )
       
   148                 {
       
   149                 CEikEdwin* edwin = static_cast<CEikEdwin*>(
       
   150                     queryControl->ControlByLayoutOrNull( EDataLayout ) );
       
   151                 if ( edwin )
       
   152                     {
       
   153                     edwin->SetSelectionL( edwin->TextLength(), 0 );
       
   154                     }
       
   155                 }
       
   156             result = EFalse;
       
   157             }
       
   158         }
       
   159 
       
   160     CleanupStack::PopAndDestroy( 2 ); // formattedText, text
       
   161     return result;
       
   162     }
       
   163 
       
   164 // --------------------------------------------------------------------------
       
   165 // CPguGroupNameQueryDlg::UpdateGroupTitleL
       
   166 // --------------------------------------------------------------------------
       
   167 //
       
   168 void CPguGroupNameQueryDlg::UpdateGroupTitleL()
       
   169     {
       
   170     TBool found = EFalse;
       
   171     HBufC* groupTitle;
       
   172     for ( TInt number = 1; !found; ++number )
       
   173         {
       
   174         groupTitle = StringLoader::LoadLC
       
   175             ( R_PHONEBOOK2_QTN_FLDR_DEFAULT_GROUP_NAME, number );
       
   176 
       
   177         // Convert the digits if necessary
       
   178         TPtr ptr = groupTitle->Des();
       
   179         AknTextUtils::DisplayTextLanguageSpecificNumberConversion( ptr );
       
   180 
       
   181         if ( !ContainsL( *groupTitle ) )
       
   182             {
       
   183             Text().Copy( *groupTitle );
       
   184             found = ETrue;
       
   185             }
       
   186         CleanupStack::PopAndDestroy( groupTitle );
       
   187         }
       
   188     }
       
   189 
       
   190 // --------------------------------------------------------------------------
       
   191 // CPguGroupNameQueryDlg::ContainsL
       
   192 // Checks if the group view contains a group named similarly as the given
       
   193 // name.
       
   194 // --------------------------------------------------------------------------
       
   195 //
       
   196 TBool CPguGroupNameQueryDlg::ContainsL( const TDesC& aText )
       
   197     {
       
   198     TBool ret = EFalse;
       
   199 
       
   200     const TInt count( iGroupsListView.ContactCountL() );
       
   201     for ( TInt i(0); i < count; ++i )
       
   202         {
       
   203         const MVPbkViewContact& contact = iGroupsListView.ContactAtL( i );
       
   204         HBufC* groupName = iNameFormatter.GetContactTitleOrNullL
       
   205             ( contact.Fields(),
       
   206               MPbk2ContactNameFormatter::EPreserveLeadingSpaces );
       
   207 
       
   208         if ( groupName )
       
   209             {
       
   210             TPtr groupNamePtr( groupName->Des() );
       
   211             StripCharacters( groupNamePtr );
       
   212 
       
   213             if ( groupNamePtr.Compare( aText ) == 0 )
       
   214                 {
       
   215                 ret = ETrue;
       
   216                 }
       
   217             }
       
   218 
       
   219         delete groupName;
       
   220         }
       
   221 
       
   222     return ret;
       
   223     }
       
   224 
       
   225 // End of File