mobilemessaging/postcard/postcardsrc/PostcardUtils.cpp
changeset 0 72b543305e3a
equal deleted inserted replaced
-1:000000000000 0:72b543305e3a
       
     1 /*
       
     2 * Copyright (c) 2005 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:   Collection of utility functions used by Postcard classes
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include <e32base.h>
       
    22 
       
    23 #include "PostcardUtils.h"
       
    24 #include "Postcard.hrh"
       
    25 #include <cntitem.h>
       
    26 #include <cntfldst.h>
       
    27 #include <centralrepository.h>
       
    28 #include "PostcardPrivateCRKeys.h"  // cenrep keys
       
    29 #include "PostcardPanic.h"        // Panic enums
       
    30 
       
    31 // CONSTANTS
       
    32 
       
    33 static const struct
       
    34     {
       
    35     const TText* iText;
       
    36     TInt iId;
       
    37     } ContactItemNames[] =
       
    38         {
       
    39         {_S("NAME"), EPostcardAddressName},
       
    40         {_S("INFO"), EPostcardAddressInfo},
       
    41         {_S("STREET"), EPostcardAddressStreet},
       
    42         {_S("ZIP"), EPostcardAddressZip},
       
    43         {_S("CITY"), EPostcardAddressCity},
       
    44         {_S("STATE"), EPostcardAddressState},
       
    45         {_S("COUNTRY"), EPostcardAddressCountry},
       
    46         };
       
    47 const TInt KContactItemNames =
       
    48     sizeof(ContactItemNames) / sizeof(ContactItemNames[0]);
       
    49 
       
    50 // ============================ MEMBER FUNCTIONS ===============================
       
    51 
       
    52 // -----------------------------------------------------------------------------
       
    53 // PostcardUtils::ContactItemNameFromId
       
    54 // -----------------------------------------------------------------------------
       
    55 const TPtrC TPostcardUtils::ContactItemNameFromId( TInt aId )
       
    56     {
       
    57     for(TInt i = 0; i < KContactItemNames; i++)
       
    58         {
       
    59         if (ContactItemNames[i].iId == aId)
       
    60             {
       
    61             return ContactItemNames[i].iText;
       
    62             }
       
    63         }
       
    64 	return KNullDesC();
       
    65 	}
       
    66 
       
    67 // -----------------------------------------------------------------------------
       
    68 // PostcardUtils::IdFromContactItemName
       
    69 // -----------------------------------------------------------------------------
       
    70 TInt TPostcardUtils::IdFromContactItemName( const TDesC& aLabel )
       
    71 	{
       
    72     for(TInt i = 0; i < KContactItemNames; i++)
       
    73         {
       
    74         TPtrC ptr(ContactItemNames[i].iText);
       
    75         if (aLabel.Compare(ptr) == 0)
       
    76             {
       
    77             return ContactItemNames[i].iId;
       
    78             }
       
    79         }
       
    80     return KErrNotFound;
       
    81 	}
       
    82 
       
    83 // -----------------------------------------------------------------------------
       
    84 // PostcardUtils::FieldOrNull
       
    85 // -----------------------------------------------------------------------------
       
    86 const CContactItemField* TPostcardUtils::FieldOrNull(
       
    87     const CContactItemFieldSet& aSet, const TDesC& aLabel )
       
    88     {
       
    89     for( TInt i = 0; i < aSet.Count( ); i++ )
       
    90         {
       
    91         const CContactItemField& field = aSet[i];
       
    92         if ( field.Label().Compare( aLabel ) == 0 )
       
    93             {
       
    94             return &field;
       
    95             }
       
    96         }
       
    97     return NULL;
       
    98     }
       
    99 
       
   100 // -----------------------------------------------------------------------------
       
   101 // PostcardUtils::AddContactField
       
   102 // -----------------------------------------------------------------------------
       
   103 void TPostcardUtils::AddContactFieldL(CContactCard& aCard, const TDesC& aLabel,
       
   104     const TDesC& aFieldText)
       
   105     {
       
   106     // Create a new CContactCard field
       
   107     CContactItemField* newField = CContactItemField::NewLC(KStorageTypeText);
       
   108     CContactTextField* itemText = newField->TextStorage();
       
   109     itemText->SetTextL(aFieldText);
       
   110     // Set the label in the field
       
   111     newField->SetLabelL(aLabel);
       
   112     // Add the field in the card
       
   113     aCard.AddFieldL(*newField);
       
   114     CleanupStack::Pop( newField );
       
   115     }
       
   116 
       
   117 // -----------------------------------------------------------------------------
       
   118 // PostcardUtils::ContactItemNameFromTag
       
   119 // -----------------------------------------------------------------------------
       
   120 const TPtrC TPostcardUtils::ContactItemNameFromTag( TInt aTag )
       
   121     {
       
   122     // Special format tags are following:
       
   123     //   0 = greeting text
       
   124     //   1 = name
       
   125     //   2 = address info
       
   126     //   3 = street
       
   127     //   4 = zip code
       
   128     //   5 = city
       
   129     //   6 = state
       
   130     //   7 = country
       
   131 
       
   132     static const TInt tagToControlId[ENumSpecFormatTags] = 
       
   133         {
       
   134 	    EPostcardTextEditor, EPostcardAddressName,
       
   135 	    EPostcardAddressInfo, EPostcardAddressStreet,
       
   136 	    EPostcardAddressZip, EPostcardAddressCity,
       
   137 	    EPostcardAddressState, EPostcardAddressCountry
       
   138         };
       
   139 
       
   140     __ASSERT_DEBUG( aTag < ENumSpecFormatTags, Panic( EPostcardPanicCoding ) );
       
   141 
       
   142     return ContactItemNameFromId( tagToControlId[aTag] );
       
   143     }
       
   144 
       
   145 // -----------------------------------------------------------------------------
       
   146 // PostcardUtils::NextTag
       
   147 // -----------------------------------------------------------------------------
       
   148 TInt TPostcardUtils::NextTag(const TDesC& aFormat, TInt& aTag)
       
   149     {
       
   150     // Find next tag from a special format string
       
   151 
       
   152     // We look for a tags like "%0U"
       
   153     _LIT(KTagFormat, "%0U");
       
   154     const TInt KTagLen = 3;
       
   155     TBuf<KTagLen> tag( KTagFormat );
       
   156 
       
   157     // Find first tag (minimum position) in the string
       
   158     TInt minPos = aFormat.Length();
       
   159     TInt i;
       
   160     for( i = 0; i < ENumSpecFormatTags; i++ )
       
   161         {
       
   162         TInt pos = aFormat.Find( tag );
       
   163         if ( pos != KErrNotFound && pos < minPos )
       
   164             {
       
   165             minPos = pos;
       
   166             aTag = i;
       
   167             }
       
   168         tag[1]++; // next tag to look for
       
   169         }
       
   170     return minPos < aFormat.Length() ? minPos : KErrNotFound;
       
   171     }
       
   172     
       
   173 // ---------------------------------------------------------
       
   174 // Panic
       
   175 // Postcard Panic function
       
   176 // ---------------------------------------------------------
       
   177 void Panic( TPostcardPanic aPanic )
       
   178     {
       
   179     _LIT( KCategory, "Postcard" );
       
   180     User::Panic( KCategory, aPanic );
       
   181     }