phonebookui/Phonebook2/USIMExtension/src/CPsu2SimContactProcessor.cpp
changeset 0 e686773b3f54
child 58 d4f567ce2e7c
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     1 /*
       
     2 * Copyright (c) 2002-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:  A class that processes sim contacts into appropritate form for
       
    15 *                the copying process.
       
    16 *                Handles errors related to SIM contact fields
       
    17 *
       
    18 */
       
    19 
       
    20 
       
    21 // INCLUDE FILES
       
    22 #include "CPsu2SimContactProcessor.h"
       
    23 
       
    24 // Phonebook 2
       
    25 #include "Pbk2USimUI.hrh"
       
    26 #include "CPsu2CopyToSimFieldInfoArray.h"
       
    27 #include "CPsu2CharConv.h"
       
    28 #include <MPbk2FieldProperty.h>
       
    29 #include <MPbk2ContactNameFormatter.h>
       
    30 #include <Pbk2ContactFieldCopy.h>
       
    31 
       
    32 // Virtual Phonebook
       
    33 #include <TVPbkFieldTypeMapping.h>
       
    34 #include <CVPbkContactManager.h>
       
    35 #include <MVPbkContactStore.h>
       
    36 #include <MVPbkFieldType.h>
       
    37 #include <MVPbkBaseContact.h>
       
    38 #include <MVPbkStoreContact.h>
       
    39 #include <MVPbkStoreContactField.h>
       
    40 #include <MVPbkContactStoreProperties.h>
       
    41 #include <MVPbkContactFieldTextData.h>
       
    42 #include <CVPbkFieldTypeRefsList.h>
       
    43 #include <CVPbkContactFieldIterator.h>
       
    44 #include <CVPbkFieldTypeRefsList.h>
       
    45 #include <VPbkEng.rsg>
       
    46 
       
    47 // System includes
       
    48 #include <gsmerror.h>
       
    49 #include <exterror.h>
       
    50 #include <barsread.h>
       
    51 #include <coemain.h>
       
    52 #include <featmgr.h>
       
    53 
       
    54 // Debugging headers
       
    55 #include <Pbk2Debug.h>
       
    56 
       
    57 
       
    58 /// Unnamed namespace for local definitions
       
    59 namespace {
       
    60 
       
    61 #ifdef _DEBUG
       
    62 enum TPanicCode
       
    63     {
       
    64     EPreCond_SplitToSimContactsL
       
    65     };
       
    66 
       
    67 void Panic(TInt aReason)
       
    68     {
       
    69     _LIT( KPanicText, "CPsu2SimContactProcessor");
       
    70     User::Panic( KPanicText, aReason );
       
    71     }
       
    72 
       
    73 #endif // _DEBUG
       
    74 
       
    75 /**
       
    76  * A helper class to keep track of the number fields of certain type
       
    77  */
       
    78 class TFieldTypeCounter
       
    79     {
       
    80     public: // Construction
       
    81 
       
    82         /**
       
    83          * Constructor.
       
    84          *
       
    85          * @param aType     Field type.
       
    86          */
       
    87         TFieldTypeCounter(
       
    88                 const MVPbkFieldType& aType ) :
       
    89                     iType( aType ), iCounter( 0 )
       
    90             {}
       
    91 
       
    92     public: // Data
       
    93         // Ref: Field type
       
    94         const MVPbkFieldType& iType;
       
    95         // Own: Number of fields
       
    96         TInt iCounter;
       
    97     };
       
    98 
       
    99 
       
   100 /**
       
   101  * Returns matching field type counter object based on given field type.
       
   102  *
       
   103  * @param aArray    Array of field type counters.
       
   104  * @param aType     Field type of interest.
       
   105  * @return  Matching field type counter.
       
   106  */
       
   107 TFieldTypeCounter& FieldTypeCounterL( 
       
   108         RArray<TFieldTypeCounter>& aArray, const MVPbkFieldType& aType )
       
   109     {
       
   110     const TInt count = aArray.Count();
       
   111     for (TInt i = 0; i < count; ++i)
       
   112         {
       
   113         if (aArray[i].iType.IsSame(aType))
       
   114             {
       
   115             return aArray[i];
       
   116             }
       
   117         }
       
   118     aArray.AppendL(TFieldTypeCounter(aType));
       
   119 
       
   120     return aArray[count];
       
   121     }
       
   122 
       
   123 /**
       
   124  * Checks if the contact already has maximum amount fields
       
   125  * of given field type.
       
   126  *
       
   127  * @param aMaxAmountFieldInContact  Max number of allowed fields of type.
       
   128  * @param aType                     Field type.
       
   129  * @param aArray                    Array of field type counters.
       
   130  * @return  ETrue if contact already has max amount of fields of given type.
       
   131  */
       
   132 TBool CheckNumberOfFieldsL( 
       
   133         TInt aMaxAmountFieldInContact, const MVPbkFieldType& aType,
       
   134         RArray<TFieldTypeCounter>& aArray )
       
   135     {
       
   136     TBool ret( EFalse );
       
   137 
       
   138     TFieldTypeCounter& counter = FieldTypeCounterL( aArray, aType );
       
   139 
       
   140     // Compare the max amount fields to the current amount of the
       
   141     // fields in the contact
       
   142     if (counter.iCounter >= aMaxAmountFieldInContact)
       
   143         {
       
   144         ret = ETrue;
       
   145         }
       
   146 
       
   147     // Add one to counter of the given type
       
   148     ++counter.iCounter;
       
   149 
       
   150     return ret;
       
   151     }
       
   152 
       
   153 /**
       
   154  * Checks is given field type a number type.
       
   155  *
       
   156  * @param aSimType  Field type.
       
   157  * @return  ETrue if the field is of number type.
       
   158  */
       
   159 TBool IsNumberType( const MVPbkFieldType& aSimType )
       
   160     {
       
   161     TBool ret = EFalse;
       
   162 
       
   163     // SIM number type is always Mobile phone (general) -> EVPbkVersitNameTEL,
       
   164     // therefore a selector is not needed
       
   165     TArray<TVPbkFieldVersitProperty> props = aSimType.VersitProperties();
       
   166     if ( props.Count() > 0 && props[0].Name() == EVPbkVersitNameTEL )
       
   167         {
       
   168         ret = ETrue;
       
   169         }
       
   170 
       
   171     return ret;
       
   172     }
       
   173 
       
   174 /**
       
   175  * Returns a valid number, removes e.g spaces, braces...
       
   176  *
       
   177  * @param aSource           Source descriptor.
       
   178  * @param aNumberKeyMap     Number key mapping.
       
   179  * @return  Formatted valid number.
       
   180  */
       
   181 HBufC* CreateValidNumberLC( 
       
   182         const TDesC& aSource, const TDesC& aNumberKeyMap )
       
   183     {
       
   184     const TInt length = aSource.Length();
       
   185     HBufC* number = HBufC::NewLC( length );
       
   186     TPtr ptr( number->Des() );
       
   187     for ( TInt i = 0; i < length; ++i )
       
   188         {
       
   189         if ( aNumberKeyMap.Locate( aSource[i] ) != KErrNotFound )
       
   190             {
       
   191             ptr.Append( aSource[i] );
       
   192             }
       
   193         }
       
   194     return number;
       
   195     }
       
   196 
       
   197 } /// namespace
       
   198 
       
   199 // --------------------------------------------------------------------------
       
   200 // CPsu2SimContactProcessor::CPsu2SimContactProcessor
       
   201 // --------------------------------------------------------------------------
       
   202 //
       
   203 CPsu2SimContactProcessor::CPsu2SimContactProcessor( 
       
   204         MVPbkContactStore& aTargetStore,
       
   205         CPsu2CopyToSimFieldInfoArray& aCopyToSimFieldInfoArray,
       
   206         MPbk2ContactNameFormatter& aNameFormatter,
       
   207         const MVPbkFieldTypeList& aMasterFieldTypeList )
       
   208         :   iTargetStore( aTargetStore ),
       
   209             iCopyToSimFieldInfoArray( aCopyToSimFieldInfoArray ),
       
   210             iNameFormatter( aNameFormatter ),
       
   211             iMasterFieldTypeList( aMasterFieldTypeList ),
       
   212             iSimMaxMatchPriority( 
       
   213                 aTargetStore.StoreProperties().SupportedFields().
       
   214                   MaxMatchPriority() )
       
   215     {
       
   216     }
       
   217 
       
   218 // --------------------------------------------------------------------------
       
   219 // CPsu2SimContactProcessor::~CPsu2SimContactProcessor
       
   220 // --------------------------------------------------------------------------
       
   221 //
       
   222 CPsu2SimContactProcessor::~CPsu2SimContactProcessor()
       
   223     {
       
   224     iNewSimContacts.ResetAndDestroy();
       
   225     iIncludedTypes.Close();
       
   226     delete iCharConvUcs2;
       
   227     delete iCharConvSms7Bit;
       
   228     }
       
   229 
       
   230 // --------------------------------------------------------------------------
       
   231 // CPsu2SimContactProcessor::NewL
       
   232 // --------------------------------------------------------------------------
       
   233 //
       
   234 CPsu2SimContactProcessor* CPsu2SimContactProcessor::NewL( 
       
   235         MVPbkContactStore& aTargetStore,
       
   236         CPsu2CopyToSimFieldInfoArray& aCopyToSimFieldInfoArray,
       
   237         MPbk2ContactNameFormatter& aNameFormatter,
       
   238         const MVPbkFieldTypeList& aMasterFieldTypeList,
       
   239         RFs& aFs )
       
   240     {
       
   241     CPsu2SimContactProcessor* self = new( ELeave ) CPsu2SimContactProcessor
       
   242         ( aTargetStore, aCopyToSimFieldInfoArray, aNameFormatter,
       
   243           aMasterFieldTypeList );
       
   244     CleanupStack::PushL( self );
       
   245     self->ConstructL( aFs );
       
   246     CleanupStack::Pop( self );
       
   247     return self;
       
   248     }
       
   249 
       
   250 // --------------------------------------------------------------------------
       
   251 // CPsu2SimContactProcessor::ConstructL
       
   252 // --------------------------------------------------------------------------
       
   253 //
       
   254 void CPsu2SimContactProcessor::ConstructL( RFs& aFs )
       
   255     {
       
   256     const TInt count = iCopyToSimFieldInfoArray.Count();
       
   257     for ( TInt i = 0; i < count; ++i )
       
   258         {
       
   259         iIncludedTypes.AppendL( 
       
   260             &iCopyToSimFieldInfoArray[i].SourceType() );
       
   261         }
       
   262 
       
   263 
       
   264     iCharConvUcs2 = CPsu2CharConv::NewL( aFs, KCharacterSetIdentifierUcs2 );
       
   265     // Symbian character converter uses CR/LF by default. (U)SIM uses CR.
       
   266     // (JustLineFeed is ok here because the converter is used just to check
       
   267     // SIM conversion lengths)
       
   268     iCharConvUcs2->SetDownGradeLf( CCnvCharacterSetConverter::
       
   269             EDowngradeExoticLineTerminatingCharactersToJustLineFeed );
       
   270 
       
   271     iCharConvSms7Bit = CPsu2CharConv::NewL( aFs, KCharacterSetIdentifierSms7Bit );
       
   272     iCharConvSms7Bit->SetDownGradeLf( CCnvCharacterSetConverter::
       
   273             EDowngradeExoticLineTerminatingCharactersToJustLineFeed );
       
   274     }
       
   275 
       
   276 // --------------------------------------------------------------------------
       
   277 // CPsu2SimContactProcessor::HandleSimError
       
   278 // --------------------------------------------------------------------------
       
   279 //
       
   280 TBool CPsu2SimContactProcessor::HandleSimError( TInt aError )
       
   281     {
       
   282     TBool result = EFalse;
       
   283     switch (aError)
       
   284         {
       
   285         case KErrGsmSimServAnrFull:
       
   286             {
       
   287             PBK2_DEBUG_PRINT(PBK2_DEBUG_STRING
       
   288             ("CPsu2SimContactProcessor::HandleSimError KErrGsmSimServAnrFull"));
       
   289 
       
   290             // There might be several EF ANR files in USIM. Save the number
       
   291             // of error messages and use it later to check how many numbers
       
   292             // can be put to one contact
       
   293             ++iNumOfAdditionalNumberErrors;
       
   294             result = ETrue;
       
   295             break;
       
   296             }
       
   297         case KErrGsmSimServEmailFull:
       
   298             {
       
   299             PBK2_DEBUG_PRINT(PBK2_DEBUG_STRING
       
   300                 ("CPsu2SimContactProcessor::HandleSimError KErrGsmSimServEmailFull"));
       
   301 
       
   302             // SIM contact can not have emails anymore because EF(email) is full
       
   303             iSimErrors |= KPsu2EMailFullError;
       
   304             RemoveFieldTypesFromIncludedTypes( KPsu2EMailFullError );
       
   305             result = ETrue;
       
   306             break;
       
   307             }
       
   308         case KErrGsmSimServSneFull:
       
   309             {
       
   310             PBK2_DEBUG_PRINT(PBK2_DEBUG_STRING
       
   311                 ("CPsu2SimContactProcessor::HandleSimError KErrGsmSimServSneFull"));
       
   312             iSimErrors |= KPsu2SecondNameFullError;
       
   313             RemoveFieldTypesFromIncludedTypes( KPsu2SecondNameFullError );
       
   314             result = ETrue;
       
   315             break;
       
   316             }
       
   317         default:
       
   318             {
       
   319             PBK2_DEBUG_PRINT(PBK2_DEBUG_STRING
       
   320                 ("CPsu2SimContactProcessor::HandleSimError unhandled error code %d"),
       
   321                     aError );
       
   322             break;
       
   323             }
       
   324         }
       
   325     return result;
       
   326     }
       
   327 
       
   328 // --------------------------------------------------------------------------
       
   329 // CPsu2SimContactProcessor::CreateSimContactsL
       
   330 //
       
   331 // This called to create SIM contacts from a source contact
       
   332 // The copying logic:
       
   333 //  1) Create a SIM contact and add all the fields from the source
       
   334 //     that possibly can be copied.
       
   335 //  2) If the created SIM contact has too many fields for one SIM contact,
       
   336 //     split the SIM contact. If the splitted contact has still too many
       
   337 //     fields for one SIM contact split the splitted contact. This is
       
   338 //     continued until the splitted contact doesn't need to be splitted
       
   339 //     again.
       
   340 // --------------------------------------------------------------------------
       
   341 //
       
   342 void CPsu2SimContactProcessor::CreateSimContactsL( 
       
   343         MVPbkStoreContact& aSourceContact,
       
   344         RPointerArray<MVPbkStoreContact>& aSimContacts )
       
   345     {
       
   346     iNewSimContacts.ResetAndDestroy();
       
   347     // Create a target contact
       
   348     MVPbkStoreContact* contact = iTargetStore.CreateNewContactLC();
       
   349     PBK2_DEBUG_PRINT(PBK2_DEBUG_STRING
       
   350         ("CPsu2SimContactProcessor::CreateSimContactsL target contact created"));
       
   351 
       
   352     // Add name to the new contact
       
   353     AddNameFieldsL(aSourceContact, *contact);
       
   354     PBK2_DEBUG_PRINT(PBK2_DEBUG_STRING
       
   355         ("CPsu2SimContactProcessor::CreateSimContactsL name fields added"));
       
   356 
       
   357     // Append fields that can possible be copied to the SIM
       
   358     AppendSupportedFieldsL(aSourceContact, *contact);
       
   359     PBK2_DEBUG_PRINT(PBK2_DEBUG_STRING
       
   360         ("CPsu2SimContactProcessor::CreateSimContactsL supported fields added"));
       
   361 
       
   362     // Split the contact
       
   363     CleanupStack::Pop(); // contact
       
   364     SplitToSimContactsL(contact);
       
   365     PBK2_DEBUG_PRINT(PBK2_DEBUG_STRING
       
   366         ("CPsu2SimContactProcessor::CreateSimContactsL contact splitted"));
       
   367 
       
   368     // Contacts that have only name are not copied to SIM
       
   369     RemoveContactsThatHaveOnlyNameL();
       
   370     PBK2_DEBUG_PRINT(PBK2_DEBUG_STRING
       
   371         ("CPsu2SimContactProcessor::CreateSimContactsL name only contacts removed"));
       
   372 
       
   373     const TInt firstPos = 0;
       
   374     const TInt count = iNewSimContacts.Count();
       
   375     for ( TInt i = count - 1; i >= 0; --i )
       
   376         {
       
   377         aSimContacts.InsertL( iNewSimContacts[i], firstPos );
       
   378         PBK2_DEBUG_PRINT(PBK2_DEBUG_STRING
       
   379             ("CPsu2SimContactProcessor::CreateSimContactsL contact added to new SIM contacts"));
       
   380 
       
   381         iNewSimContacts.Remove( i );
       
   382         }
       
   383     }
       
   384 
       
   385 // --------------------------------------------------------------------------
       
   386 // CPsu2SimContactProcessor::CreateFixedSimContactsL
       
   387 //
       
   388 // This is called when saving a SIM contact has failed and HandleSimError
       
   389 // has handled the error. It means that the state of this processor has
       
   390 // changed and the failed SIM contact can be splitted or there are fields
       
   391 // that must be removed from the failed contact. After this the resulted
       
   392 // SIM contacts will be copied again.
       
   393 // --------------------------------------------------------------------------
       
   394 //
       
   395 void CPsu2SimContactProcessor::CreateFixedSimContactsL( 
       
   396         MVPbkStoreContact& aSimContact,
       
   397         RPointerArray<MVPbkStoreContact>& aSimContacts )
       
   398     {
       
   399     iNewSimContacts.ResetAndDestroy();
       
   400     // Create a target contact
       
   401     MVPbkStoreContact* contact = iTargetStore.CreateNewContactLC();
       
   402     PBK2_DEBUG_PRINT(PBK2_DEBUG_STRING
       
   403         ("CPsu2SimContactProcessor::CreateFixedSimContactsL target contact created"));
       
   404 
       
   405     // Add name to the new contact
       
   406     AddNameFieldsL(aSimContact, *contact);
       
   407     PBK2_DEBUG_PRINT(PBK2_DEBUG_STRING
       
   408         ("CPsu2SimContactProcessor::CreateFixedSimContactsL name fields added"));
       
   409 
       
   410     // Append fields that can possible be copied to the SIM
       
   411     AppendSupportedFieldsL(aSimContact, *contact);
       
   412     PBK2_DEBUG_PRINT(PBK2_DEBUG_STRING
       
   413         ("CPsu2SimContactProcessor::CreateFixedSimContactsL supported fields added"));
       
   414 
       
   415     const TInt firstPos = 0;
       
   416     const TInt newCount = contact->Fields().FieldCount();
       
   417     if (newCount > 0 && newCount != aSimContact.Fields().FieldCount())
       
   418         {
       
   419         // After appending supported fields the amount of field is different
       
   420         // than in the original contact. This means that there has been
       
   421         // an error that has changed the included types list.
       
   422         if ( IsValidContactToSaveL( *contact ) )
       
   423             {
       
   424             PBK2_DEBUG_PRINT(PBK2_DEBUG_STRING
       
   425                 ("CPsu2SimContactProcessor::CreateFixedSimContactsL is valid contact to save"));
       
   426 
       
   427             aSimContacts.InsertL(contact, firstPos);
       
   428             CleanupStack::Pop(); // contact
       
   429 
       
   430             PBK2_DEBUG_PRINT(PBK2_DEBUG_STRING
       
   431                 ("CPsu2SimContactProcessor::CreateFixedSimContactsL contact added to new SIM contacts"));
       
   432             }
       
   433         else
       
   434             {
       
   435             PBK2_DEBUG_PRINT(PBK2_DEBUG_STRING
       
   436                 ("CPsu2SimContactProcessor::CreateFixedSimContactsL is not valid contact to save"));
       
   437 
       
   438             CleanupStack::PopAndDestroy(); // contact
       
   439             }
       
   440         }
       
   441     else
       
   442         {
       
   443         // Split the contact
       
   444         CleanupStack::Pop(); // contact
       
   445         // Takes ownership of the contact
       
   446         if ( SplitToSimContactsL( contact ) )
       
   447             {
       
   448             PBK2_DEBUG_PRINT(PBK2_DEBUG_STRING
       
   449                 ("CPsu2SimContactProcessor::CreateFixedSimContactsL contact splitted"));
       
   450 
       
   451             // Contacts that have only name are not copied to SIM
       
   452             RemoveContactsThatHaveOnlyNameL();
       
   453             PBK2_DEBUG_PRINT(PBK2_DEBUG_STRING
       
   454                 ("CPsu2SimContactProcessor::CreateFixedSimContactsL name only contacts removed"));
       
   455 
       
   456             const TInt count = iNewSimContacts.Count();
       
   457             for (TInt i = count - 1; i >= 0; --i)
       
   458                 {
       
   459                 aSimContacts.InsertL(iNewSimContacts[i], firstPos);
       
   460                 iNewSimContacts.Remove(i);
       
   461 
       
   462                 PBK2_DEBUG_PRINT(PBK2_DEBUG_STRING
       
   463                     ("CPsu2SimContactProcessor::CreateFixedSimContactsL contact added to new SIM contacts"));
       
   464                 }
       
   465             }
       
   466         else
       
   467             {
       
   468             PBK2_DEBUG_PRINT(PBK2_DEBUG_STRING
       
   469                 ("CPsu2SimContactProcessor::CreateFixedSimContactsL contact does not split"));
       
   470 
       
   471             iNewSimContacts.ResetAndDestroy();
       
   472             }
       
   473         }
       
   474     }
       
   475 
       
   476 // --------------------------------------------------------------------------
       
   477 // CPsu2SimContactProcessor::CreateFixedSimContactsL
       
   478 // --------------------------------------------------------------------------
       
   479 //
       
   480 TBool CPsu2SimContactProcessor::DetailsDropped()
       
   481     {
       
   482     TBool ret( EFalse );
       
   483     ret = iSimErrors & KPsu2EMailFullError;
       
   484     if ( !ret  )
       
   485         {
       
   486         ret = iSimErrors & KPsu2SecondNameFullError;
       
   487         }
       
   488     return ret;
       
   489     }
       
   490 
       
   491 // --------------------------------------------------------------------------
       
   492 // CPsu2SimContactProcessor::RemoveFieldTypesFromIncludedTypes
       
   493 //
       
   494 // Removes error related field types from the included types so those
       
   495 // types won't be copied to SIM contact anymore.
       
   496 // --------------------------------------------------------------------------
       
   497 //
       
   498 void CPsu2SimContactProcessor::RemoveFieldTypesFromIncludedTypes( 
       
   499         TPsu2ErrorCode aBlockingError )
       
   500     {
       
   501     const TInt includedCount = iIncludedTypes.Count();
       
   502     for ( TInt i = includedCount - 1; i >= 0; --i )
       
   503         {
       
   504         const TPsu2CopyToSimFieldInfo* info = 
       
   505             iCopyToSimFieldInfoArray.FindInfoForSourceType( 
       
   506                 *iIncludedTypes[i] );
       
   507         if ( info && info->BlockedByError( aBlockingError ) )
       
   508             {
       
   509             iIncludedTypes.Remove( i );
       
   510             }
       
   511         }
       
   512     }
       
   513 
       
   514 // --------------------------------------------------------------------------
       
   515 // CPsu2SimContactProcessor::AddNameFieldsL
       
   516 //
       
   517 // Adds name fields from source contact to the target.
       
   518 // --------------------------------------------------------------------------
       
   519 //
       
   520 void CPsu2SimContactProcessor::AddNameFieldsL( 
       
   521         MVPbkStoreContact& aSource, MVPbkStoreContact& aTarget )
       
   522     {
       
   523     // Copy formatted name always to SIM's name field
       
   524     CopyTitleFieldDataL( aSource, aTarget,
       
   525         iCopyToSimFieldInfoArray.SimNameType() );
       
   526     }
       
   527 
       
   528 // --------------------------------------------------------------------------
       
   529 // CPsu2SimContactProcessor::CopyReadingFieldsL
       
   530 //
       
   531 // Copies reading fields in Japanese variants.
       
   532 // --------------------------------------------------------------------------
       
   533 //
       
   534 void CPsu2SimContactProcessor::CopyReadingFieldsL( 
       
   535         MVPbkStoreContact& aSource, MVPbkStoreContact& aTarget )
       
   536     {
       
   537     // In case the name can be built without first name reading
       
   538     // and last name reading, the last name reading and first name reading
       
   539     // field data is combined according to name formatting rules and
       
   540     // the formatted name Reading is copied to Second name field
       
   541     // in Japanese variants.
       
   542 
       
   543     const MVPbkFieldTypeList& supportedTypes =
       
   544         iTargetStore.StoreProperties().SupportedFields();
       
   545     if ( supportedTypes.ContainsSame(
       
   546          iCopyToSimFieldInfoArray.LastNameReadingType() ) )
       
   547         {
       
   548         CVPbkFieldTypeRefsList* list = CVPbkFieldTypeRefsList::NewL();
       
   549         CleanupStack::PushL( list );
       
   550         // Get fields that actually a part of the formatted name
       
   551         CVPbkBaseContactFieldTypeListIterator* itr =
       
   552             iNameFormatter.ActualTitleFieldsLC( *list, aSource.Fields() );
       
   553         // Check if the title has reading fields
       
   554         TBool containsReading = EFalse;
       
   555         while ( itr->HasNext() )
       
   556             {
       
   557             TInt typeId =
       
   558                 itr->Next()->BestMatchingFieldType()->FieldTypeResId();
       
   559             if ( typeId == R_VPBK_FIELD_TYPE_LASTNAMEREADING ||
       
   560                  typeId == R_VPBK_FIELD_TYPE_FIRSTNAMEREADING )
       
   561                 {
       
   562                 containsReading = ETrue;
       
   563                 }
       
   564             }
       
   565         CleanupStack::PopAndDestroy(2, list);
       
   566 
       
   567         // If title doesn't contain reading fields then copy formatted
       
   568         // reading to SIM's reading (=second name) field
       
   569         if ( !containsReading )
       
   570             {
       
   571             // Create a temp contact from source store for getting
       
   572             // field collection of reading fields
       
   573             MVPbkStoreContact* tmpCnt =
       
   574                 aSource.ParentStore().CreateNewContactLC();
       
   575             MVPbkStoreContactFieldCollection& sourceFields =
       
   576                 aSource.Fields();
       
   577             const TInt fieldCount = sourceFields.FieldCount();
       
   578             for ( TInt i = 0; i < fieldCount; ++i )
       
   579                 {
       
   580                 const MVPbkFieldType* sourceType =
       
   581                     sourceFields.FieldAt( i ).BestMatchingFieldType();
       
   582                 if ( sourceType )
       
   583                     {
       
   584                     TInt typeId = sourceType->FieldTypeResId();
       
   585                     if ( typeId == R_VPBK_FIELD_TYPE_LASTNAMEREADING ||
       
   586                          typeId == R_VPBK_FIELD_TYPE_FIRSTNAMEREADING )
       
   587                         {
       
   588                         Pbk2ContactFieldCopy::CopyFieldL
       
   589                             ( sourceFields.FieldAt( i ),
       
   590                               *sourceType, *tmpCnt );
       
   591                         }
       
   592                     }
       
   593                 }
       
   594             if ( tmpCnt->Fields().FieldCount() > 0 )
       
   595                 {
       
   596                 // If there were reading fields in the source then copy
       
   597                 // the formatted reading to SIM's last name reading
       
   598                 CopyTitleFieldDataL( *tmpCnt, aTarget,
       
   599                     iCopyToSimFieldInfoArray.LastNameReadingType() );
       
   600                 }
       
   601             CleanupStack::PopAndDestroy(); // tmpCnt
       
   602             }
       
   603         }
       
   604     }
       
   605 
       
   606 // --------------------------------------------------------------------------
       
   607 // CPsu2SimContactProcessor::AppendSupportedFieldsL
       
   608 //
       
   609 // Appends fields that are in included properties and supported
       
   610 // by the SIM store.
       
   611 // --------------------------------------------------------------------------
       
   612 //
       
   613 void CPsu2SimContactProcessor::AppendSupportedFieldsL( 
       
   614         MVPbkBaseContact& aSource, MVPbkStoreContact& aTarget)
       
   615     {
       
   616     const MVPbkBaseContactFieldCollection& fields = aSource.Fields();
       
   617     const TInt fieldCount = fields.FieldCount();
       
   618     const TInt typeCount = iIncludedTypes.Count();
       
   619     const TInt maxPriority = iMasterFieldTypeList.MaxMatchPriority();
       
   620     const MVPbkFieldTypeList& supportedTypes =
       
   621         iTargetStore.StoreProperties().SupportedFields();
       
   622 
       
   623     TBool contactCopyFailed = EFalse;
       
   624     for (TInt i = 0; i < fieldCount && !contactCopyFailed; ++i)
       
   625         {
       
   626         // Get the source field
       
   627         const MVPbkBaseContactField& field = fields.FieldAt(i);
       
   628         // Get the source field type
       
   629         const MVPbkFieldType* type = field.BestMatchingFieldType();
       
   630 
       
   631         TBool fieldCopied = EFalse;
       
   632         for (TInt j = 0; j < typeCount && type && !fieldCopied
       
   633                 && !contactCopyFailed; ++j)
       
   634             {
       
   635             // Check if the field is one of the fields that can be copied
       
   636             // to the SIM
       
   637             if (type->IsSame(*iIncludedTypes[j]))
       
   638                 {
       
   639                 // The field possible can be copied to the SIM
       
   640                 // Get the target(=SIM) field type for source type
       
   641                 const MVPbkFieldType* simType =
       
   642                     iCopyToSimFieldInfoArray.ConvertToSimType(*type);
       
   643                 // Check if the sim store supports the converted field
       
   644                 if (simType && supportedTypes.ContainsSame(*simType))
       
   645                     {
       
   646                     if ( !CopyFieldL(field, aTarget, *simType) )
       
   647                         {
       
   648                         // If copying of one field fails then copying
       
   649                         // the contact fails
       
   650                         contactCopyFailed = ETrue;
       
   651 
       
   652                         PBK2_DEBUG_PRINT(PBK2_DEBUG_STRING
       
   653                             ("CPsu2SimContactProcessor::AppendSupportedFieldsL contact copy failed"));
       
   654                         }
       
   655                     fieldCopied = ETrue;
       
   656                     }
       
   657                 }
       
   658             }
       
   659         }
       
   660 
       
   661     if ( contactCopyFailed )
       
   662         {
       
   663         // Remove all fields from the target contact
       
   664         aTarget.RemoveAllFields();
       
   665         }
       
   666     }
       
   667 
       
   668 // --------------------------------------------------------------------------
       
   669 // CPsu2SimContactProcessor::SplitToSimContactsL
       
   670 //
       
   671 // Tries to split the source contact, return ETrue if splitted.
       
   672 // --------------------------------------------------------------------------
       
   673 //
       
   674 TBool CPsu2SimContactProcessor::SplitToSimContactsL( 
       
   675         MVPbkStoreContact* aSourceContact )
       
   676     {
       
   677     __ASSERT_DEBUG(iNewSimContacts.Count() == 0,
       
   678         Panic(EPreCond_SplitToSimContactsL));
       
   679 
       
   680     MVPbkStoreContact* contact = aSourceContact;
       
   681     CleanupDeletePushL(contact);
       
   682     TBool result = EFalse;
       
   683 
       
   684     MVPbkStoreContact* splitted = SplitContactLC(*contact);
       
   685     if (splitted)
       
   686         {
       
   687         // Contact is splitted at least into two contacts
       
   688         iNewSimContacts.AppendL(contact);
       
   689         CleanupStack::Pop(2); // contact, splitted
       
   690         contact = splitted;
       
   691         CleanupDeletePushL(contact);
       
   692         while (contact)
       
   693             {
       
   694             // Split as long as must
       
   695             splitted = SplitContactLC(*contact);
       
   696             iNewSimContacts.AppendL(contact);
       
   697             if (splitted)
       
   698                 {
       
   699                 CleanupStack::Pop(2); // contact, splitted
       
   700                 contact = splitted;
       
   701                 CleanupDeletePushL(contact);
       
   702                 }
       
   703             else
       
   704                 {
       
   705                 CleanupStack::Pop(); // contact
       
   706                 contact = NULL;
       
   707                 }
       
   708             }
       
   709         result = ETrue;
       
   710         }
       
   711     // Contact can be empty if the CopyFieldL failed
       
   712     else if ( contact->Fields().FieldCount() > 0 )
       
   713         {
       
   714         // Contact is not splitted
       
   715         iNewSimContacts.AppendL(contact);
       
   716         CleanupStack::Pop(); // contact
       
   717         }
       
   718 
       
   719     return result;
       
   720     }
       
   721 
       
   722 // --------------------------------------------------------------------------
       
   723 // CPsu2SimContactProcessor::SplitContactLC
       
   724 //
       
   725 // Splits the master contact if there is too much fields to one
       
   726 // SIM contact.
       
   727 // --------------------------------------------------------------------------
       
   728 //
       
   729 MVPbkStoreContact* CPsu2SimContactProcessor::SplitContactLC( 
       
   730         MVPbkStoreContact& aSimContact )
       
   731     {
       
   732     MVPbkStoreContactFieldCollection& fields = aSimContact.Fields();
       
   733     TInt count =  fields.FieldCount();
       
   734 
       
   735     RArray<TFieldTypeCounter> fieldTypeCounterArray;
       
   736     CleanupClosePushL(fieldTypeCounterArray);
       
   737 
       
   738     RArray<TInt> removedIndexes;
       
   739     CleanupClosePushL(removedIndexes);
       
   740 
       
   741     MVPbkStoreContact* splittedContact = NULL;
       
   742     // Loop all fields of the source contact
       
   743     for (TInt i = 0; i < count; ++i)
       
   744         {
       
   745         const MVPbkFieldType* type =
       
   746             fields.FieldAt(i).BestMatchingFieldType();
       
   747         // Check all the data fields
       
   748         if (type && !iNameFormatter.IsTitleFieldType( *type ) )
       
   749             {
       
   750             TInt maxNumber = MaxNumberOfFieldL( aSimContact, *type );
       
   751             // Compare the max amount fields to the current amount of the
       
   752             // fields in the contact
       
   753             if ( CheckNumberOfFieldsL
       
   754                     ( maxNumber, *type, fieldTypeCounterArray ) )
       
   755                 {
       
   756                 // Create a new contact for the fields that can not fit to
       
   757                 // the source contact
       
   758                 if (!splittedContact)
       
   759                     {
       
   760                     splittedContact = iTargetStore.CreateNewContactLC();
       
   761                     AddNameFieldsL(aSimContact, *splittedContact);
       
   762                     }
       
   763                 // Copy field to the new contact
       
   764                 // and remove it from the source
       
   765                 CopyFieldL( fields.FieldAt(i), *splittedContact, *type );
       
   766                 removedIndexes.AppendL(i);
       
   767                 }
       
   768             }
       
   769         }
       
   770 
       
   771     count = removedIndexes.Count();
       
   772     for (TInt k = count - 1; k >= 0; --k)
       
   773         {
       
   774         aSimContact.RemoveField(removedIndexes[k]);
       
   775         }
       
   776 
       
   777     if (splittedContact)
       
   778         {
       
   779         CleanupStack::Pop(); // splittedContact
       
   780         CleanupStack::PopAndDestroy(2); // removedIndexes,
       
   781                                         // fieldTypeCounterArray
       
   782         CleanupDeletePushL(splittedContact);
       
   783         }
       
   784     else
       
   785         {
       
   786         CleanupStack::PopAndDestroy(2); // removedIndexes,
       
   787                                         // fieldTypeCounterArray
       
   788         }
       
   789     return splittedContact;
       
   790     }
       
   791 
       
   792 // --------------------------------------------------------------------------
       
   793 // CPsu2SimContactProcessor::CopyFieldL
       
   794 //
       
   795 // Copies the source field to the target contact, EFalse if not copied.
       
   796 // --------------------------------------------------------------------------
       
   797 //
       
   798 TBool CPsu2SimContactProcessor::CopyFieldL( 
       
   799         const MVPbkBaseContactField& aFieldToCopy,
       
   800         MVPbkStoreContact& aTarget,
       
   801         const MVPbkFieldType& aSimType )
       
   802     {
       
   803     TBool result = ETrue;
       
   804     if ( aFieldToCopy.FieldData().DataType() == EVPbkFieldStorageTypeText )
       
   805         {
       
   806         // Get source data
       
   807         const MVPbkContactFieldTextData& sourceData =
       
   808             MVPbkContactFieldTextData::Cast( aFieldToCopy.FieldData() );
       
   809         // Create target field
       
   810         MVPbkStoreContactField* field = aTarget.CreateFieldLC( aSimType );
       
   811         // Get target data
       
   812         MVPbkContactFieldTextData& targetData =
       
   813             MVPbkContactFieldTextData::Cast( field->FieldData() );
       
   814 
       
   815         // Invalid characters must be removed before copying if the
       
   816         // field is number
       
   817         if ( IsNumberType( aSimType ) )
       
   818             {
       
   819             HBufC* number = CreateValidNumberLC( sourceData.Text(),
       
   820                 iCopyToSimFieldInfoArray.NumberKeyMap() );
       
   821             result = CopyFieldDataL( *number, targetData, aSimType );
       
   822             CleanupStack::PopAndDestroy( number );
       
   823             }
       
   824         else
       
   825             {
       
   826             result = CopyFieldDataL
       
   827                 ( sourceData.Text(), targetData, aSimType );
       
   828             }
       
   829 
       
   830         if ( result )
       
   831             {
       
   832             aTarget.AddFieldL(field);
       
   833             CleanupStack::Pop(); // field
       
   834             }
       
   835         else
       
   836             {
       
   837             CleanupStack::PopAndDestroy(); // field
       
   838             }
       
   839         }
       
   840     return result;
       
   841     }
       
   842 
       
   843 // --------------------------------------------------------------------------
       
   844 // CPsu2SimContactProcessor::CopyTitleFieldDataL
       
   845 //
       
   846 // Copies title field from source contact to given field type.
       
   847 // --------------------------------------------------------------------------
       
   848 //
       
   849 void CPsu2SimContactProcessor::CopyTitleFieldDataL( 
       
   850         MVPbkStoreContact& aSource, MVPbkStoreContact& aTarget,
       
   851         const MVPbkFieldType& aSimTitleFieldType )
       
   852     {
       
   853     HBufC* title = iNameFormatter.GetContactTitleOrNullL
       
   854         ( aSource.Fields(),
       
   855           MPbk2ContactNameFormatter::EPreserveLeadingSpaces );
       
   856 
       
   857     if (title)
       
   858         {
       
   859         CleanupStack::PushL( title );
       
   860         MVPbkStoreContactField* field =
       
   861             aTarget.CreateFieldLC( aSimTitleFieldType );
       
   862         MVPbkContactFieldTextData& data =
       
   863             MVPbkContactFieldTextData::Cast( field->FieldData() );
       
   864         TruncateAndCopyFieldDataL( *title, data );
       
   865         aTarget.AddFieldL(field);
       
   866         CleanupStack::Pop(field);
       
   867         CleanupStack::PopAndDestroy(title);
       
   868         }
       
   869     }
       
   870 
       
   871 
       
   872 // --------------------------------------------------------------------------
       
   873 // CPsu2SimContactProcessor::CopyFieldDataL
       
   874 // --------------------------------------------------------------------------
       
   875 //
       
   876 TBool CPsu2SimContactProcessor::CopyFieldDataL( 
       
   877         const TDesC& aSource, MVPbkContactFieldTextData& aTarget,
       
   878         const MVPbkFieldType& aSimType )
       
   879     {
       
   880     TBool result = ETrue;
       
   881     // If truncation is allowed for the field type then truncate
       
   882     // and copy
       
   883     if ( iCopyToSimFieldInfoArray.TruncationAllowed( aSimType ) )
       
   884         {
       
   885         TruncateAndCopyFieldDataL( aSource, aTarget );
       
   886         }
       
   887     // otherwise check if there is enough space for data
       
   888     else if ( aSource.Length() <=
       
   889             aTarget.MaxLength() )
       
   890         {
       
   891         aTarget.SetTextL( aSource );
       
   892         }
       
   893     else
       
   894         {
       
   895         result = EFalse;
       
   896         }
       
   897     return result;
       
   898     }
       
   899 
       
   900 // --------------------------------------------------------------------------
       
   901 // CPsu2SimContactProcessor::TruncateAndCopyFieldDataL
       
   902 // --------------------------------------------------------------------------
       
   903 //
       
   904 void CPsu2SimContactProcessor::TruncateAndCopyFieldDataL( 
       
   905         const TDesC& aSource, 
       
   906         MVPbkContactFieldTextData& aTarget )
       
   907     {
       
   908     PBK2_DEBUG_PRINT( PBK2_DEBUG_STRING
       
   909         ("CPsu2SimContactProcessor::TruncateAndCopyFieldDataL max len: [%i]"),
       
   910         aTarget.MaxLength() );
       
   911 
       
   912     // Try first with SMS 7-bit encoding
       
   913     TInt unconvertedCount(0);
       
   914     TPtrC data = iCharConvSms7Bit->CheckFieldLengthL
       
   915             ( aSource, aTarget.MaxLength(), unconvertedCount );
       
   916 
       
   917     PBK2_DEBUG_PRINT( PBK2_DEBUG_STRING
       
   918         ("CPsu2SimContactProcessor:: max converted len: [%i], unconv: [%i]"),
       
   919         data.Length(), unconvertedCount );
       
   920 
       
   921     // If any characters could not be converted with SMS 7-bit encoding we need
       
   922     // to check length using UCS-2 encoding which requires more space per a
       
   923     // character.
       
   924     // With some character sets that contain less than 128 characters the
       
   925     // resulting data is shorter than the maximum field length allowed by the
       
   926     // (U)SIM. In other words we may truncate the text fields stored to the
       
   927     // (U)SIM more than would be required by the (U)SIM. This is because
       
   928     // there are actually three possible coding schemes for such character
       
   929     // sets available. See Annex B in document 3GPP TS 11.11.
       
   930     // The conversion is made by SIM ATK TSY so we would need to have some
       
   931     // SAT server API available to be able to determine the actual maximum
       
   932     // length for the data unambiguously.
       
   933     if( unconvertedCount > 0 )
       
   934         {
       
   935         // Leave one character extra space in case of non-7-bit conversions
       
   936         // This seems to be a feature of (U)SIM that it requires one extra byte
       
   937         // for unicode contact name (see also the 3GPP reference mentioned in
       
   938         // the comment above).
       
   939         data.Set( iCharConvUcs2->CheckFieldLengthL
       
   940                 ( aSource, aTarget.MaxLength() - 1, unconvertedCount ) );
       
   941         PBK2_DEBUG_PRINT( PBK2_DEBUG_STRING
       
   942             ("CPsu2SimContactProcessor:: max converted len: [%i], unconv: [%i]"),
       
   943             data.Length(), unconvertedCount );
       
   944         }
       
   945 
       
   946     aTarget.SetTextL( data );
       
   947     }
       
   948 
       
   949 // --------------------------------------------------------------------------
       
   950 // CPsu2SimContactProcessor::MaxNumberOfFieldL
       
   951 //
       
   952 // Returns the maximum amount of field of given type that
       
   953 // can be added to the contact.
       
   954 // --------------------------------------------------------------------------
       
   955 //
       
   956 TInt CPsu2SimContactProcessor::MaxNumberOfFieldL( 
       
   957         MVPbkStoreContact& aContact, const MVPbkFieldType& aType )
       
   958     {
       
   959     TInt maxAmount( aContact.MaxNumberOfFieldL( aType ) );
       
   960     // For numbers it must be checked that is ANR file(s) of USIM full.
       
   961     if ( IsNumberType( aType ) )
       
   962         {
       
   963         // Reduce the max according to amount of ANR errors from TSY
       
   964         maxAmount -= iNumOfAdditionalNumberErrors;
       
   965         }
       
   966     return maxAmount;
       
   967     }
       
   968 
       
   969 // --------------------------------------------------------------------------
       
   970 // CPsu2SimContactProcessor::RemoveContactsThatHaveOnlyNameL
       
   971 // --------------------------------------------------------------------------
       
   972 //
       
   973 void CPsu2SimContactProcessor::RemoveContactsThatHaveOnlyNameL()
       
   974     {
       
   975     const TInt cntCount = iNewSimContacts.Count();
       
   976     for ( TInt i = cntCount - 1; i >= 0; --i )
       
   977         {
       
   978         if ( !IsValidContactToSaveL( *iNewSimContacts[i] ) )
       
   979             {
       
   980             delete iNewSimContacts[i];
       
   981             iNewSimContacts.Remove( i );
       
   982             }
       
   983         }
       
   984     }
       
   985 
       
   986 // --------------------------------------------------------------------------
       
   987 // CPsu2SimContactProcessor::IsValidContactToSaveL
       
   988 // --------------------------------------------------------------------------
       
   989 //
       
   990 TBool CPsu2SimContactProcessor::IsValidContactToSaveL( 
       
   991         MVPbkStoreContact& aSimContact )
       
   992     {
       
   993     // Specification says that "If the contact does not contain any number
       
   994     // it is not copied to SIM.". This is valid for 2G SIMs but it's assumed
       
   995     // that in USIM this means that contact that don't have any data fields
       
   996     // is not copied. In other words contact is valid if it has other than
       
   997     // title fields.
       
   998     MVPbkStoreContactFieldCollection& fields = aSimContact.Fields();
       
   999     const TInt fieldCount = fields.FieldCount();
       
  1000     for ( TInt i = 0; i < fieldCount; ++i )
       
  1001         {
       
  1002         if ( !iNameFormatter.IsTitleField( fields.FieldAt( i ) ) )
       
  1003             {
       
  1004             return ETrue;
       
  1005             }
       
  1006         }
       
  1007     return EFalse;
       
  1008     }
       
  1009 
       
  1010 //  End of File