bluetoothengine/btui/Ecom/inc/bluetoothuiutil.inl
branchRCL_3
changeset 56 9386f31cc85b
parent 6 6a29d5ad0713
equal deleted inserted replaced
55:613943a21004 56:9386f31cc85b
       
     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:   This provides utility for loading a BT device name without
       
    15 * localised to a string to be shown in GUI.
       
    16 *
       
    17 */
       
    18 
       
    19 inline TInt BluetoothUiUtil::GetSubstringKeyPos( 
       
    20         const TDes& aDes, const TDesC& aKey, 
       
    21         TInt aKeyPos, TInt& aKeyLength )
       
    22     {
       
    23     ASSERT( aKeyPos > KErrNotFound );
       
    24     _LIT(KPercentSymbol, "%" );
       
    25     // 32 would be to fit any key substring in practice:
       
    26     TBuf<32> key;
       
    27     TInt pos( KErrNotFound );
       
    28     // if the specified position is 0, check if "%<aKey>" (%U or %N) exists.
       
    29     if ( aKeyPos == 0 )
       
    30         {
       
    31         key.Copy( KPercentSymbol );
       
    32         key.Append(aKey);
       
    33         pos = aDes.Find( key );
       
    34         }
       
    35     // Either the key is not found, or aKeyPos is not 0.
       
    36     if ( pos == KErrNotFound )
       
    37         {
       
    38         key.Copy( KPercentSymbol );
       
    39         key.AppendNum( aKeyPos );
       
    40         key.Append( aKey );
       
    41         pos = aDes.Find( key );
       
    42         }
       
    43     if ( pos > KErrNotFound )
       
    44         {
       
    45         aKeyLength = key.Length();
       
    46         }
       
    47     return pos;
       
    48     }
       
    49 
       
    50 inline TInt BluetoothUiUtil::GetStringSubstringKeyPos( 
       
    51         const TDes& aDes, TInt aKeyPos, TInt& aKeyLength )
       
    52     {
       
    53     _LIT(KStringKeyword,"U");
       
    54     TPtrC ptr( KStringKeyword );
       
    55     return GetSubstringKeyPos(aDes, ptr, aKeyPos, aKeyLength );
       
    56     }
       
    57 
       
    58 inline void BluetoothUiUtil::AddSubstringL( 
       
    59         RBuf& aBuf, const TDesC& aSub, TInt aSubPos)
       
    60     {
       
    61     // Find the position of the specified "%U" or %[0-9]U in aBuf.
       
    62     TInt keyLength;
       
    63     TInt pos = GetStringSubstringKeyPos( aBuf, aSubPos, keyLength );
       
    64     if ( pos == KErrNotFound )
       
    65         {
       
    66         User::Leave( pos );
       
    67         }
       
    68     // Enlarge the max length of aBuf if needed:
       
    69     TInt reqedLen = aBuf.Length() - keyLength + aSub.Length();
       
    70     if ( aBuf.MaxLength() <  reqedLen )
       
    71         {
       
    72         aBuf.ReAllocL( reqedLen );
       
    73         }
       
    74     aBuf.Replace( pos, keyLength, aSub );
       
    75     }
       
    76 
       
    77 inline void BluetoothUiUtil::LoadResourceAndSubstringL(RBuf& aBuf,
       
    78         TInt aResourceId, const TDesC& aSub, TInt aSubPos )
       
    79     {
       
    80     HBufC* string = StringLoader::LoadL( aResourceId );
       
    81     aBuf.Assign( string );
       
    82     // Logical loc strings may contain redundant "[<chars>]" for some reason.
       
    83     // This sub-string should be removed in our ad hoc parameter loading method.
       
    84     _LIT( KLeftSquareBracket,"[" );
       
    85     _LIT( KRightSquareBracket,"]" );
       
    86     while ( 1 )
       
    87         {
       
    88         TInt lpos = aBuf.Find( KLeftSquareBracket );
       
    89         TInt rpos = aBuf.Find( KRightSquareBracket );
       
    90         if ( lpos > KErrNotFound && rpos > KErrNotFound && ( rpos > lpos ) )
       
    91             {
       
    92             aBuf.Delete(lpos, rpos - lpos + 1 );
       
    93             continue;
       
    94             }
       
    95         // No sub-strings that match "[" and ends with "]". Done
       
    96         break;
       
    97         }
       
    98     // Add the substring to this string loaded from resource:
       
    99     AddSubstringL( aBuf,  aSub, aSubPos );
       
   100     }
       
   101 
       
   102 // End of File
       
   103