phoneuis/easydialing/src/easydialingutils.cpp
branchRCL_3
changeset 62 5266b1f337bd
child 69 8baf28733c3d
equal deleted inserted replaced
61:41a7f70b3818 62:5266b1f337bd
       
     1 /*
       
     2 * Copyright (c) 2010 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:  Helper class for easydialing.
       
    15 *
       
    16 */
       
    17 
       
    18 #include <e32std.h>
       
    19 #include <AknUtils.h>
       
    20 
       
    21 #include "easydialingutils.h"
       
    22 
       
    23 const TText KNameSeparatorChar = ' ';
       
    24 const TText KListFieldSeparatorChar = '\t';
       
    25 
       
    26 // -----------------------------------------------------------------------------
       
    27 // CreateContactStringLC
       
    28 //
       
    29 // -----------------------------------------------------------------------------
       
    30 //
       
    31 HBufC* EasyDialingUtils::CreateContactStringLC(
       
    32         const TDesC& aFirstName,
       
    33         const TDesC& aLastName,
       
    34         const TDesC& aCompanyName,
       
    35         CEasyDialingContactDataManager::TNameOrder aNameOrder )
       
    36     {
       
    37     // first strip control chars from the names
       
    38     HBufC* firstName = aFirstName.AllocLC();
       
    39     TPtr fPtr = firstName->Des();
       
    40     AknTextUtils::StripCharacters( fPtr, KAknStripListControlChars );
       
    41 
       
    42     HBufC* lastName = aLastName.AllocLC();
       
    43     TPtr lPtr = lastName->Des();
       
    44     AknTextUtils::StripCharacters( lPtr, KAknStripListControlChars );
       
    45 
       
    46     HBufC* companyName = aCompanyName.AllocLC();
       
    47     TPtr cPtr = companyName->Des();
       
    48     AknTextUtils::StripCharacters( cPtr, KAknStripListControlChars );
       
    49 
       
    50     // Calculate string lenght. If first name and last name are missing, company name is duplicated.
       
    51     // Add company name lenght twice to make sure there is enough space.
       
    52     TInt stringLength = firstName->Length() + lastName->Length() + 2 * companyName->Length() + 2;
       
    53     HBufC* string = HBufC::NewLC( stringLength );
       
    54     TPtr ptr = string->Des();
       
    55     
       
    56     // If there is no first name, the last name will be used.
       
    57     if ( firstName->Length() == 0 )
       
    58         {
       
    59         ptr.Copy( *lastName );
       
    60         }
       
    61     
       
    62     // If there is no last name, the first name will be used.
       
    63     else if ( lastName->Length() == 0 )
       
    64         {
       
    65         ptr.Copy( *firstName );
       
    66         }
       
    67     
       
    68     // If there are both first and last name, the name string is composed of them both.
       
    69     else 
       
    70         {
       
    71         if( aNameOrder == CEasyDialingContactDataManager::EFirstnameLastname )
       
    72             {
       
    73             ptr.Copy( *firstName );
       
    74             ptr.Append( KNameSeparatorChar );
       
    75             ptr.Append( *lastName );
       
    76             }
       
    77         else
       
    78             {
       
    79             ptr.Copy( *lastName );
       
    80             ptr.Append( KNameSeparatorChar );
       
    81             ptr.Append( *firstName );
       
    82             }        
       
    83         }
       
    84 
       
    85     // If firstname and last name are missing, put company name into the first field.
       
    86     if ( ptr.Length() == 0 )
       
    87         {
       
    88         ptr.Append( *companyName );
       
    89         }
       
    90     else
       
    91         {
       
    92         // Otherwise add company name into the second field using tab as a separator.
       
    93         ptr.Append( KListFieldSeparatorChar );
       
    94         ptr.Append( *companyName );
       
    95         }
       
    96 
       
    97     CleanupStack::Pop( string );
       
    98     CleanupStack::PopAndDestroy( 3, firstName );
       
    99     CleanupStack::PushL( string );
       
   100     return string;
       
   101     }