menucontentsrv/engsrc/menuengflags.cpp
branchRCL_3
changeset 114 a5a39a295112
parent 0 79c6a41cd166
equal deleted inserted replaced
113:0efa10d348c0 114:a5a39a295112
       
     1 /*
       
     2 * Copyright (c) 2009 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:
       
    15 *
       
    16 */
       
    17 
       
    18 // INCLUDE FILES
       
    19 
       
    20 #include "menuengflags.h"
       
    21 #include "mcsmenuitem.h"
       
    22 
       
    23 // CLASS DECLARATION
       
    24 
       
    25 NONSHARABLE_STRUCT( TMenuFlagEntry )
       
    26     {
       
    27     TUint32 iFlag; // Flag bit value (one bit only).
       
    28     const TText* iString; // Flag string name.
       
    29     };
       
    30 
       
    31 // CONSTANTS
       
    32 
       
    33 /// Flag name table. Concatenated names (with spaces between) should fit into
       
    34 // KMenuMaxAttrValueLen!
       
    35 LOCAL_D const TMenuFlagEntry KTable[] =
       
    36     {
       
    37         { TMenuItem::ELockDelete,   _S("lock_delete") },
       
    38         { TMenuItem::ELockName,     _S("lock_name") },
       
    39         { TMenuItem::ELockIcon,     _S("lock_icon") },
       
    40         { TMenuItem::ELockMoveInto, _S("lock_moveinto") },
       
    41         { TMenuItem::ELockItem,     _S("lock_item") },
       
    42         { TMenuItem::EHidden,       _S("hidden") },
       
    43         { TMenuItem::EMissing,      _S("missing") },
       
    44         { 0 }
       
    45     };
       
    46 
       
    47 // ================= MEMBER FUNCTIONS =======================
       
    48 
       
    49 // ---------------------------------------------------------
       
    50 // MenuEngFlags::AsInt
       
    51 // ---------------------------------------------------------
       
    52 //
       
    53 TUint32 MenuEngFlags::AsInt( const TDesC& aStringValue )
       
    54     {
       
    55     TUint32 flags = 0;
       
    56     TLex lex( aStringValue );
       
    57     TPtrC token( lex.NextToken() );
       
    58     while ( token.Length() )
       
    59         {
       
    60         flags |= FlagValue( token );
       
    61         token.Set( lex.NextToken() );
       
    62         }
       
    63     return flags;
       
    64     }
       
    65 
       
    66 // ---------------------------------------------------------
       
    67 // MenuEngFlags::AsString
       
    68 // ---------------------------------------------------------
       
    69 //
       
    70 void MenuEngFlags::AsString( TUint32 aIntValue, TDes& aStringValue )
       
    71     {
       
    72     // Note, we assume that the result fits in aStringValue.
       
    73     // It is expected to be of length KMenuMaxAttrValueLen.
       
    74     // If new flags are added, we should check that their concatenated
       
    75     // names (with spaces between) still fit in one attribute. If not,
       
    76     // then we should use more than one "flags" attribute; but this is
       
    77     // easy to avoid if flag names are kept short. (32 flags should normally
       
    78     // fit in 512 characters!)
       
    79     __ASSERT_DEBUG( aStringValue.MaxLength() >= KMenuMaxAttrValueLen, \
       
    80         User::Invariant() );
       
    81     aStringValue.SetLength( 0 );
       
    82     TUint32 mask;
       
    83     TPtrC ptr;
       
    84     TBool leadingSpace = EFalse;
       
    85     for ( mask = 0x1; mask; mask = mask << 1 )
       
    86         {
       
    87         ptr.Set( StringValue( mask & aIntValue ) );
       
    88         if ( ptr.Length() )
       
    89             {
       
    90             if ( leadingSpace )
       
    91                 {
       
    92                 aStringValue.Append( TChar(' ') );
       
    93                 }
       
    94             aStringValue.Append( ptr );
       
    95             leadingSpace = ETrue; // Need a leading space from the 2nd onwards.
       
    96             }
       
    97         }
       
    98     }
       
    99 
       
   100 // ---------------------------------------------------------
       
   101 // MenuEngFlags::FlagValue
       
   102 // ---------------------------------------------------------
       
   103 //
       
   104 TUint32 MenuEngFlags::FlagValue( const TDesC& aString )
       
   105     {
       
   106     // Note: This could be optimized
       
   107     const TMenuFlagEntry* entry;
       
   108     for ( entry = &KTable[0]; entry->iFlag; entry++ )
       
   109         {
       
   110         if ( aString == TPtrC( entry->iString ) )
       
   111             {
       
   112             return entry->iFlag;
       
   113             }
       
   114         }
       
   115     return 0;
       
   116     }
       
   117 
       
   118 // ---------------------------------------------------------
       
   119 // MenuEngFlags::StringValue
       
   120 // ---------------------------------------------------------
       
   121 //
       
   122 TPtrC MenuEngFlags::StringValue( TUint32 aFlag )
       
   123     {
       
   124     // It is expected that aFlag has exactly one bit set -> first
       
   125     // match is accepted.
       
   126     const TMenuFlagEntry* entry;
       
   127     for ( entry = &KTable[0]; entry->iFlag; entry++ )
       
   128         {
       
   129         if ( aFlag & entry->iFlag )
       
   130             {
       
   131             return TPtrC( entry->iString );
       
   132             }
       
   133         }
       
   134     return KNullDesC();
       
   135     }
       
   136 
       
   137 // ================= MEMBER FUNCTIONS =======================
       
   138 
       
   139 // ---------------------------------------------------------
       
   140 // MenuEngId::AsInt
       
   141 // ---------------------------------------------------------
       
   142 //
       
   143 TInt MenuEngId::AsInt( const TDesC& aStringValue )
       
   144     {
       
   145     TInt position( aStringValue.Find(KHexPrefix) );
       
   146 	TPtrC string( aStringValue );
       
   147 	TRadix radix( EDecimal );
       
   148 	if ( position == 0 )
       
   149 		{
       
   150 		// is hex
       
   151 		radix = EHex;
       
   152 		string.Set( aStringValue.Mid( KHexPrefix().Length() ) );
       
   153 		}
       
   154     
       
   155     TUint id = 0;
       
   156     if ( KErrNone != TLex( string ).Val( id, radix ) )
       
   157     	{
       
   158     	return 0;
       
   159     	}
       
   160     return id;
       
   161     }
       
   162 
       
   163 // ---------------------------------------------------------
       
   164 // MenuEngId::AsString
       
   165 // ---------------------------------------------------------
       
   166 //
       
   167 void MenuEngId::AsString( TInt aIntValue, TDes& aStringValue )
       
   168     {
       
   169     // Note, we assume that the result fits in aStringValue.
       
   170     // It is expected to be of length KMenuMaxAttrValueLen.
       
   171     __ASSERT_DEBUG( aStringValue.MaxLength() >= KMenuMaxAttrValueLen, \
       
   172         User::Invariant() );
       
   173     aStringValue.SetLength( 0 );
       
   174     aStringValue.AppendNum( aIntValue );
       
   175     }
       
   176 
       
   177 //  End of File