phonebookui/Phonebook2/USIMExtension/src/CPsu2SimContactProcessor.cpp
branchRCL_3
changeset 20 f4a778e096c2
child 21 9da50d567e3c
equal deleted inserted replaced
19:5b6f26637ad3 20:f4a778e096c2
       
     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 MVPbkFieldTypeList& supportedTypes =
       
   257             iTargetStore.StoreProperties().SupportedFields();
       
   258     // Remove the unsupported fieldInfo from array first.
       
   259     iCopyToSimFieldInfoArray.RemoveUnSupportedFieldInfo( supportedTypes );
       
   260         
       
   261     const TInt count = iCopyToSimFieldInfoArray.Count();
       
   262     for ( TInt i = 0; i < count; ++i )
       
   263         {
       
   264         iIncludedTypes.AppendL( 
       
   265             &iCopyToSimFieldInfoArray[i].SourceType() );
       
   266         }
       
   267 
       
   268 
       
   269     iCharConvUcs2 = CPsu2CharConv::NewL( aFs, KCharacterSetIdentifierUcs2 );
       
   270     // Symbian character converter uses CR/LF by default. (U)SIM uses CR.
       
   271     // (JustLineFeed is ok here because the converter is used just to check
       
   272     // SIM conversion lengths)
       
   273     iCharConvUcs2->SetDownGradeLf( CCnvCharacterSetConverter::
       
   274             EDowngradeExoticLineTerminatingCharactersToJustLineFeed );
       
   275 
       
   276     iCharConvSms7Bit = CPsu2CharConv::NewL( aFs, KCharacterSetIdentifierSms7Bit );
       
   277     iCharConvSms7Bit->SetDownGradeLf( CCnvCharacterSetConverter::
       
   278             EDowngradeExoticLineTerminatingCharactersToJustLineFeed );
       
   279     }
       
   280 
       
   281 // --------------------------------------------------------------------------
       
   282 // CPsu2SimContactProcessor::HandleSimError
       
   283 // --------------------------------------------------------------------------
       
   284 //
       
   285 TBool CPsu2SimContactProcessor::HandleSimError( TInt aError )
       
   286     {
       
   287     TBool result = EFalse;
       
   288     switch (aError)
       
   289         {
       
   290         case KErrGsmSimServAnrFull:
       
   291             {
       
   292             PBK2_DEBUG_PRINT(PBK2_DEBUG_STRING
       
   293             ("CPsu2SimContactProcessor::HandleSimError KErrGsmSimServAnrFull"));
       
   294 
       
   295             // There might be several EF ANR files in USIM. Save the number
       
   296             // of error messages and use it later to check how many numbers
       
   297             // can be put to one contact
       
   298             ++iNumOfAdditionalNumberErrors;
       
   299             result = ETrue;
       
   300             break;
       
   301             }
       
   302         case KErrGsmSimServEmailFull:
       
   303             {
       
   304             PBK2_DEBUG_PRINT(PBK2_DEBUG_STRING
       
   305                 ("CPsu2SimContactProcessor::HandleSimError KErrGsmSimServEmailFull"));
       
   306 
       
   307             // SIM contact can not have emails anymore because EF(email) is full
       
   308             iSimErrors |= KPsu2EMailFullError;
       
   309             RemoveFieldTypesFromIncludedTypes( KPsu2EMailFullError );
       
   310             result = ETrue;
       
   311             break;
       
   312             }
       
   313         case KErrGsmSimServSneFull:
       
   314             {
       
   315             PBK2_DEBUG_PRINT(PBK2_DEBUG_STRING
       
   316                 ("CPsu2SimContactProcessor::HandleSimError KErrGsmSimServSneFull"));
       
   317             iSimErrors |= KPsu2SecondNameFullError;
       
   318             RemoveFieldTypesFromIncludedTypes( KPsu2SecondNameFullError );
       
   319             result = ETrue;
       
   320             break;
       
   321             }
       
   322         default:
       
   323             {
       
   324             PBK2_DEBUG_PRINT(PBK2_DEBUG_STRING
       
   325                 ("CPsu2SimContactProcessor::HandleSimError unhandled error code %d"),
       
   326                     aError );
       
   327             break;
       
   328             }
       
   329         }
       
   330     return result;
       
   331     }
       
   332 
       
   333 // --------------------------------------------------------------------------
       
   334 // CPsu2SimContactProcessor::CreateSimContactsL
       
   335 //
       
   336 // This called to create SIM contacts from a source contact
       
   337 // The copying logic:
       
   338 //  1) Create a SIM contact and add all the fields from the source
       
   339 //     that possibly can be copied.
       
   340 //  2) If the created SIM contact has too many fields for one SIM contact,
       
   341 //     split the SIM contact. If the splitted contact has still too many
       
   342 //     fields for one SIM contact split the splitted contact. This is
       
   343 //     continued until the splitted contact doesn't need to be splitted
       
   344 //     again.
       
   345 // --------------------------------------------------------------------------
       
   346 //
       
   347 void CPsu2SimContactProcessor::CreateSimContactsL( 
       
   348         MVPbkStoreContact& aSourceContact,
       
   349         RPointerArray<MVPbkStoreContact>& aSimContacts )
       
   350     {
       
   351     iNewSimContacts.ResetAndDestroy();
       
   352     // Create a target contact
       
   353     MVPbkStoreContact* contact = iTargetStore.CreateNewContactLC();
       
   354     PBK2_DEBUG_PRINT(PBK2_DEBUG_STRING
       
   355         ("CPsu2SimContactProcessor::CreateSimContactsL target contact created"));
       
   356 
       
   357     // Add name to the new contact
       
   358     AddNameFieldsL(aSourceContact, *contact);
       
   359     PBK2_DEBUG_PRINT(PBK2_DEBUG_STRING
       
   360         ("CPsu2SimContactProcessor::CreateSimContactsL name fields added"));
       
   361 
       
   362     // Append fields that can possible be copied to the SIM
       
   363     AppendSupportedFieldsL(aSourceContact, *contact);
       
   364     PBK2_DEBUG_PRINT(PBK2_DEBUG_STRING
       
   365         ("CPsu2SimContactProcessor::CreateSimContactsL supported fields added"));
       
   366 
       
   367     // Split the contact
       
   368     CleanupStack::Pop(); // contact
       
   369     SplitToSimContactsL(contact);
       
   370     PBK2_DEBUG_PRINT(PBK2_DEBUG_STRING
       
   371         ("CPsu2SimContactProcessor::CreateSimContactsL contact splitted"));
       
   372 
       
   373     // Contacts that have only name are not copied to SIM
       
   374     RemoveContactsThatHaveOnlyNameL();
       
   375     PBK2_DEBUG_PRINT(PBK2_DEBUG_STRING
       
   376         ("CPsu2SimContactProcessor::CreateSimContactsL name only contacts removed"));
       
   377 
       
   378     const TInt firstPos = 0;
       
   379     const TInt count = iNewSimContacts.Count();
       
   380     for ( TInt i = count - 1; i >= 0; --i )
       
   381         {
       
   382         aSimContacts.InsertL( iNewSimContacts[i], firstPos );
       
   383         PBK2_DEBUG_PRINT(PBK2_DEBUG_STRING
       
   384             ("CPsu2SimContactProcessor::CreateSimContactsL contact added to new SIM contacts"));
       
   385 
       
   386         iNewSimContacts.Remove( i );
       
   387         }
       
   388     }
       
   389 
       
   390 // --------------------------------------------------------------------------
       
   391 // CPsu2SimContactProcessor::CreateFixedSimContactsL
       
   392 //
       
   393 // This is called when saving a SIM contact has failed and HandleSimError
       
   394 // has handled the error. It means that the state of this processor has
       
   395 // changed and the failed SIM contact can be splitted or there are fields
       
   396 // that must be removed from the failed contact. After this the resulted
       
   397 // SIM contacts will be copied again.
       
   398 // --------------------------------------------------------------------------
       
   399 //
       
   400 void CPsu2SimContactProcessor::CreateFixedSimContactsL( 
       
   401         MVPbkStoreContact& aSimContact,
       
   402         RPointerArray<MVPbkStoreContact>& aSimContacts )
       
   403     {
       
   404     iNewSimContacts.ResetAndDestroy();
       
   405     // Create a target contact
       
   406     MVPbkStoreContact* contact = iTargetStore.CreateNewContactLC();
       
   407     PBK2_DEBUG_PRINT(PBK2_DEBUG_STRING
       
   408         ("CPsu2SimContactProcessor::CreateFixedSimContactsL target contact created"));
       
   409 
       
   410     // Add name to the new contact
       
   411     AddNameFieldsL(aSimContact, *contact);
       
   412     PBK2_DEBUG_PRINT(PBK2_DEBUG_STRING
       
   413         ("CPsu2SimContactProcessor::CreateFixedSimContactsL name fields added"));
       
   414 
       
   415     // Append fields that can possible be copied to the SIM
       
   416     AppendSupportedFieldsL(aSimContact, *contact);
       
   417     PBK2_DEBUG_PRINT(PBK2_DEBUG_STRING
       
   418         ("CPsu2SimContactProcessor::CreateFixedSimContactsL supported fields added"));
       
   419 
       
   420     const TInt firstPos = 0;
       
   421     const TInt newCount = contact->Fields().FieldCount();
       
   422     if (newCount > 0 && newCount != aSimContact.Fields().FieldCount())
       
   423         {
       
   424         // After appending supported fields the amount of field is different
       
   425         // than in the original contact. This means that there has been
       
   426         // an error that has changed the included types list.
       
   427         if ( IsValidContactToSaveL( *contact ) )
       
   428             {
       
   429             PBK2_DEBUG_PRINT(PBK2_DEBUG_STRING
       
   430                 ("CPsu2SimContactProcessor::CreateFixedSimContactsL is valid contact to save"));
       
   431 
       
   432             aSimContacts.InsertL(contact, firstPos);
       
   433             CleanupStack::Pop(); // contact
       
   434 
       
   435             PBK2_DEBUG_PRINT(PBK2_DEBUG_STRING
       
   436                 ("CPsu2SimContactProcessor::CreateFixedSimContactsL contact added to new SIM contacts"));
       
   437             }
       
   438         else
       
   439             {
       
   440             PBK2_DEBUG_PRINT(PBK2_DEBUG_STRING
       
   441                 ("CPsu2SimContactProcessor::CreateFixedSimContactsL is not valid contact to save"));
       
   442 
       
   443             CleanupStack::PopAndDestroy(); // contact
       
   444             }
       
   445         }
       
   446     else
       
   447         {
       
   448         // Split the contact
       
   449         CleanupStack::Pop(); // contact
       
   450         // Takes ownership of the contact
       
   451         if ( SplitToSimContactsL( contact ) )
       
   452             {
       
   453             PBK2_DEBUG_PRINT(PBK2_DEBUG_STRING
       
   454                 ("CPsu2SimContactProcessor::CreateFixedSimContactsL contact splitted"));
       
   455 
       
   456             // Contacts that have only name are not copied to SIM
       
   457             RemoveContactsThatHaveOnlyNameL();
       
   458             PBK2_DEBUG_PRINT(PBK2_DEBUG_STRING
       
   459                 ("CPsu2SimContactProcessor::CreateFixedSimContactsL name only contacts removed"));
       
   460 
       
   461             const TInt count = iNewSimContacts.Count();
       
   462             for (TInt i = count - 1; i >= 0; --i)
       
   463                 {
       
   464                 aSimContacts.InsertL(iNewSimContacts[i], firstPos);
       
   465                 iNewSimContacts.Remove(i);
       
   466 
       
   467                 PBK2_DEBUG_PRINT(PBK2_DEBUG_STRING
       
   468                     ("CPsu2SimContactProcessor::CreateFixedSimContactsL contact added to new SIM contacts"));
       
   469                 }
       
   470             }
       
   471         else
       
   472             {
       
   473             PBK2_DEBUG_PRINT(PBK2_DEBUG_STRING
       
   474                 ("CPsu2SimContactProcessor::CreateFixedSimContactsL contact does not split"));
       
   475 
       
   476             iNewSimContacts.ResetAndDestroy();
       
   477             }
       
   478         }
       
   479     }
       
   480 
       
   481 // --------------------------------------------------------------------------
       
   482 // CPsu2SimContactProcessor::CreateFixedSimContactsL
       
   483 // --------------------------------------------------------------------------
       
   484 //
       
   485 TBool CPsu2SimContactProcessor::DetailsDropped()
       
   486     {
       
   487     TBool ret( EFalse );
       
   488     ret = iSimErrors & KPsu2EMailFullError;
       
   489     if ( !ret  )
       
   490         {
       
   491         ret = iSimErrors & KPsu2SecondNameFullError;
       
   492         }
       
   493     return ret;
       
   494     }
       
   495 
       
   496 // --------------------------------------------------------------------------
       
   497 // CPsu2SimContactProcessor::RemoveFieldTypesFromIncludedTypes
       
   498 //
       
   499 // Removes error related field types from the included types so those
       
   500 // types won't be copied to SIM contact anymore.
       
   501 // --------------------------------------------------------------------------
       
   502 //
       
   503 void CPsu2SimContactProcessor::RemoveFieldTypesFromIncludedTypes( 
       
   504         TPsu2ErrorCode aBlockingError )
       
   505     {
       
   506     const TInt includedCount = iIncludedTypes.Count();
       
   507     for ( TInt i = includedCount - 1; i >= 0; --i )
       
   508         {
       
   509         const TPsu2CopyToSimFieldInfo* info = 
       
   510             iCopyToSimFieldInfoArray.FindInfoForSourceType( 
       
   511                 *iIncludedTypes[i] );
       
   512         if ( info && info->BlockedByError( aBlockingError ) )
       
   513             {
       
   514             iIncludedTypes.Remove( i );
       
   515             }
       
   516         }
       
   517     }
       
   518 
       
   519 // --------------------------------------------------------------------------
       
   520 // CPsu2SimContactProcessor::AddNameFieldsL
       
   521 //
       
   522 // Adds name fields from source contact to the target.
       
   523 // --------------------------------------------------------------------------
       
   524 //
       
   525 void CPsu2SimContactProcessor::AddNameFieldsL( 
       
   526         MVPbkStoreContact& aSource, MVPbkStoreContact& aTarget )
       
   527     {
       
   528     // Copy formatted name always to SIM's name field
       
   529     CopyTitleFieldDataL( aSource, aTarget,
       
   530         iCopyToSimFieldInfoArray.SimNameType() );
       
   531     }
       
   532 
       
   533 // --------------------------------------------------------------------------
       
   534 // CPsu2SimContactProcessor::CopyReadingFieldsL
       
   535 //
       
   536 // Copies reading fields in Japanese variants.
       
   537 // --------------------------------------------------------------------------
       
   538 //
       
   539 void CPsu2SimContactProcessor::CopyReadingFieldsL( 
       
   540         MVPbkStoreContact& aSource, MVPbkStoreContact& aTarget )
       
   541     {
       
   542     // In case the name can be built without first name reading
       
   543     // and last name reading, the last name reading and first name reading
       
   544     // field data is combined according to name formatting rules and
       
   545     // the formatted name Reading is copied to Second name field
       
   546     // in Japanese variants.
       
   547 
       
   548     const MVPbkFieldTypeList& supportedTypes =
       
   549         iTargetStore.StoreProperties().SupportedFields();
       
   550     if ( supportedTypes.ContainsSame(
       
   551          iCopyToSimFieldInfoArray.LastNameReadingType() ) )
       
   552         {
       
   553         CVPbkFieldTypeRefsList* list = CVPbkFieldTypeRefsList::NewL();
       
   554         CleanupStack::PushL( list );
       
   555         // Get fields that actually a part of the formatted name
       
   556         CVPbkBaseContactFieldTypeListIterator* itr =
       
   557             iNameFormatter.ActualTitleFieldsLC( *list, aSource.Fields() );
       
   558         // Check if the title has reading fields
       
   559         TBool containsReading = EFalse;
       
   560         while ( itr->HasNext() )
       
   561             {
       
   562             TInt typeId =
       
   563                 itr->Next()->BestMatchingFieldType()->FieldTypeResId();
       
   564             if ( typeId == R_VPBK_FIELD_TYPE_LASTNAMEREADING ||
       
   565                  typeId == R_VPBK_FIELD_TYPE_FIRSTNAMEREADING )
       
   566                 {
       
   567                 containsReading = ETrue;
       
   568                 }
       
   569             }
       
   570         CleanupStack::PopAndDestroy(2, list);
       
   571 
       
   572         // If title doesn't contain reading fields then copy formatted
       
   573         // reading to SIM's reading (=second name) field
       
   574         if ( !containsReading )
       
   575             {
       
   576             // Create a temp contact from source store for getting
       
   577             // field collection of reading fields
       
   578             MVPbkStoreContact* tmpCnt =
       
   579                 aSource.ParentStore().CreateNewContactLC();
       
   580             MVPbkStoreContactFieldCollection& sourceFields =
       
   581                 aSource.Fields();
       
   582             const TInt fieldCount = sourceFields.FieldCount();
       
   583             for ( TInt i = 0; i < fieldCount; ++i )
       
   584                 {
       
   585                 const MVPbkFieldType* sourceType =
       
   586                     sourceFields.FieldAt( i ).BestMatchingFieldType();
       
   587                 if ( sourceType )
       
   588                     {
       
   589                     TInt typeId = sourceType->FieldTypeResId();
       
   590                     if ( typeId == R_VPBK_FIELD_TYPE_LASTNAMEREADING ||
       
   591                          typeId == R_VPBK_FIELD_TYPE_FIRSTNAMEREADING )
       
   592                         {
       
   593                         Pbk2ContactFieldCopy::CopyFieldL
       
   594                             ( sourceFields.FieldAt( i ),
       
   595                               *sourceType, *tmpCnt );
       
   596                         }
       
   597                     }
       
   598                 }
       
   599             if ( tmpCnt->Fields().FieldCount() > 0 )
       
   600                 {
       
   601                 // If there were reading fields in the source then copy
       
   602                 // the formatted reading to SIM's last name reading
       
   603                 CopyTitleFieldDataL( *tmpCnt, aTarget,
       
   604                     iCopyToSimFieldInfoArray.LastNameReadingType() );
       
   605                 }
       
   606             CleanupStack::PopAndDestroy(); // tmpCnt
       
   607             }
       
   608         }
       
   609     }
       
   610 
       
   611 // --------------------------------------------------------------------------
       
   612 // CPsu2SimContactProcessor::AppendSupportedFieldsL
       
   613 //
       
   614 // Appends fields that are in included properties and supported
       
   615 // by the SIM store.
       
   616 // --------------------------------------------------------------------------
       
   617 //
       
   618 void CPsu2SimContactProcessor::AppendSupportedFieldsL( 
       
   619         MVPbkBaseContact& aSource, MVPbkStoreContact& aTarget)
       
   620     {
       
   621     const MVPbkBaseContactFieldCollection& fields = aSource.Fields();
       
   622     const TInt fieldCount = fields.FieldCount();
       
   623     const TInt typeCount = iIncludedTypes.Count();
       
   624     const TInt maxPriority = iMasterFieldTypeList.MaxMatchPriority();
       
   625     const MVPbkFieldTypeList& supportedTypes =
       
   626         iTargetStore.StoreProperties().SupportedFields();
       
   627 
       
   628     TBool contactCopyFailed = EFalse;
       
   629     for (TInt i = 0; i < fieldCount && !contactCopyFailed; ++i)
       
   630         {
       
   631         // Get the source field
       
   632         const MVPbkBaseContactField& field = fields.FieldAt(i);
       
   633         // Get the source field type
       
   634         const MVPbkFieldType* type = field.BestMatchingFieldType();
       
   635 
       
   636         TBool fieldCopied = EFalse;
       
   637         for (TInt j = 0; j < typeCount && type && !fieldCopied
       
   638                 && !contactCopyFailed; ++j)
       
   639             {
       
   640             // Check if the field is one of the fields that can be copied
       
   641             // to the SIM
       
   642             if (type->IsSame(*iIncludedTypes[j]))
       
   643                 {
       
   644                 // The field possible can be copied to the SIM
       
   645                 // Get the target(=SIM) field type for source type
       
   646                 const MVPbkFieldType* simType =
       
   647                     iCopyToSimFieldInfoArray.ConvertToSimType(*type);
       
   648                 // Check if the sim store supports the converted field
       
   649                 if (simType && supportedTypes.ContainsSame(*simType))
       
   650                     {
       
   651                     if ( !CopyFieldL(field, aTarget, *simType) )
       
   652                         {
       
   653                         // If copying of one field fails then copying
       
   654                         // the contact fails
       
   655                         contactCopyFailed = ETrue;
       
   656 
       
   657                         PBK2_DEBUG_PRINT(PBK2_DEBUG_STRING
       
   658                             ("CPsu2SimContactProcessor::AppendSupportedFieldsL contact copy failed"));
       
   659                         }
       
   660                     fieldCopied = ETrue;
       
   661                     }
       
   662                 }
       
   663             }
       
   664         }
       
   665 
       
   666     if ( contactCopyFailed )
       
   667         {
       
   668         // Remove all fields from the target contact
       
   669         aTarget.RemoveAllFields();
       
   670         }
       
   671     }
       
   672 
       
   673 // --------------------------------------------------------------------------
       
   674 // CPsu2SimContactProcessor::SplitToSimContactsL
       
   675 //
       
   676 // Tries to split the source contact, return ETrue if splitted.
       
   677 // --------------------------------------------------------------------------
       
   678 //
       
   679 TBool CPsu2SimContactProcessor::SplitToSimContactsL( 
       
   680         MVPbkStoreContact* aSourceContact )
       
   681     {
       
   682     __ASSERT_DEBUG(iNewSimContacts.Count() == 0,
       
   683         Panic(EPreCond_SplitToSimContactsL));
       
   684 
       
   685     MVPbkStoreContact* contact = aSourceContact;
       
   686     CleanupDeletePushL(contact);
       
   687     TBool result = EFalse;
       
   688 
       
   689     MVPbkStoreContact* splitted = SplitContactLC(*contact);
       
   690     if (splitted)
       
   691         {
       
   692         // Contact is splitted at least into two contacts
       
   693         iNewSimContacts.AppendL(contact);
       
   694         CleanupStack::Pop(2); // contact, splitted
       
   695         contact = splitted;
       
   696         CleanupDeletePushL(contact);
       
   697         while (contact)
       
   698             {
       
   699             // Split as long as must
       
   700             splitted = SplitContactLC(*contact);
       
   701             iNewSimContacts.AppendL(contact);
       
   702             if (splitted)
       
   703                 {
       
   704                 CleanupStack::Pop(2); // contact, splitted
       
   705                 contact = splitted;
       
   706                 CleanupDeletePushL(contact);
       
   707                 }
       
   708             else
       
   709                 {
       
   710                 CleanupStack::Pop(); // contact
       
   711                 contact = NULL;
       
   712                 }
       
   713             }
       
   714         result = ETrue;
       
   715         }
       
   716     // Contact can be empty if the CopyFieldL failed
       
   717     else if ( contact->Fields().FieldCount() > 0 )
       
   718         {
       
   719         // Contact is not splitted
       
   720         iNewSimContacts.AppendL(contact);
       
   721         CleanupStack::Pop(); // contact
       
   722         }
       
   723 
       
   724     return result;
       
   725     }
       
   726 
       
   727 // --------------------------------------------------------------------------
       
   728 // CPsu2SimContactProcessor::SplitContactLC
       
   729 //
       
   730 // Splits the master contact if there is too much fields to one
       
   731 // SIM contact.
       
   732 // --------------------------------------------------------------------------
       
   733 //
       
   734 MVPbkStoreContact* CPsu2SimContactProcessor::SplitContactLC( 
       
   735         MVPbkStoreContact& aSimContact )
       
   736     {
       
   737     MVPbkStoreContactFieldCollection& fields = aSimContact.Fields();
       
   738     TInt count =  fields.FieldCount();
       
   739 
       
   740     RArray<TFieldTypeCounter> fieldTypeCounterArray;
       
   741     CleanupClosePushL(fieldTypeCounterArray);
       
   742 
       
   743     RArray<TInt> removedIndexes;
       
   744     CleanupClosePushL(removedIndexes);
       
   745 
       
   746     MVPbkStoreContact* splittedContact = NULL;
       
   747     // Loop all fields of the source contact
       
   748     for (TInt i = 0; i < count; ++i)
       
   749         {
       
   750         const MVPbkFieldType* type =
       
   751             fields.FieldAt(i).BestMatchingFieldType();
       
   752         // Check all the data fields
       
   753         if (type && !iNameFormatter.IsTitleFieldType( *type ) )
       
   754             {
       
   755             TInt maxNumber = MaxNumberOfFieldL( aSimContact, *type );
       
   756             // Compare the max amount fields to the current amount of the
       
   757             // fields in the contact
       
   758             if ( CheckNumberOfFieldsL
       
   759                     ( maxNumber, *type, fieldTypeCounterArray ) )
       
   760                 {
       
   761                 // Create a new contact for the fields that can not fit to
       
   762                 // the source contact
       
   763                 if (!splittedContact)
       
   764                     {
       
   765                     splittedContact = iTargetStore.CreateNewContactLC();
       
   766                     AddNameFieldsL(aSimContact, *splittedContact);
       
   767                     }
       
   768                 // Copy field to the new contact
       
   769                 // and remove it from the source
       
   770                 CopyFieldL( fields.FieldAt(i), *splittedContact, *type );
       
   771                 removedIndexes.AppendL(i);
       
   772                 }
       
   773             }
       
   774         }
       
   775 
       
   776     count = removedIndexes.Count();
       
   777     for (TInt k = count - 1; k >= 0; --k)
       
   778         {
       
   779         aSimContact.RemoveField(removedIndexes[k]);
       
   780         }
       
   781 
       
   782     if (splittedContact)
       
   783         {
       
   784         CleanupStack::Pop(); // splittedContact
       
   785         CleanupStack::PopAndDestroy(2); // removedIndexes,
       
   786                                         // fieldTypeCounterArray
       
   787         CleanupDeletePushL(splittedContact);
       
   788         }
       
   789     else
       
   790         {
       
   791         CleanupStack::PopAndDestroy(2); // removedIndexes,
       
   792                                         // fieldTypeCounterArray
       
   793         }
       
   794     return splittedContact;
       
   795     }
       
   796 
       
   797 // --------------------------------------------------------------------------
       
   798 // CPsu2SimContactProcessor::CopyFieldL
       
   799 //
       
   800 // Copies the source field to the target contact, EFalse if not copied.
       
   801 // --------------------------------------------------------------------------
       
   802 //
       
   803 TBool CPsu2SimContactProcessor::CopyFieldL( 
       
   804         const MVPbkBaseContactField& aFieldToCopy,
       
   805         MVPbkStoreContact& aTarget,
       
   806         const MVPbkFieldType& aSimType )
       
   807     {
       
   808     TBool result = ETrue;
       
   809     if ( aFieldToCopy.FieldData().DataType() == EVPbkFieldStorageTypeText )
       
   810         {
       
   811         // Get source data
       
   812         const MVPbkContactFieldTextData& sourceData =
       
   813             MVPbkContactFieldTextData::Cast( aFieldToCopy.FieldData() );
       
   814         // Create target field
       
   815         MVPbkStoreContactField* field = aTarget.CreateFieldLC( aSimType );
       
   816         // Get target data
       
   817         MVPbkContactFieldTextData& targetData =
       
   818             MVPbkContactFieldTextData::Cast( field->FieldData() );
       
   819 
       
   820         // Invalid characters must be removed before copying if the
       
   821         // field is number
       
   822         if ( IsNumberType( aSimType ) )
       
   823             {
       
   824             HBufC* number = CreateValidNumberLC( sourceData.Text(),
       
   825                 iCopyToSimFieldInfoArray.NumberKeyMap() );
       
   826             result = CopyFieldDataL( *number, targetData, aSimType );
       
   827             CleanupStack::PopAndDestroy( number );
       
   828             }
       
   829         else
       
   830             {
       
   831             result = CopyFieldDataL
       
   832                 ( sourceData.Text(), targetData, aSimType );
       
   833             }
       
   834 
       
   835         if ( result )
       
   836             {
       
   837             aTarget.AddFieldL(field);
       
   838             CleanupStack::Pop(); // field
       
   839             }
       
   840         else
       
   841             {
       
   842             CleanupStack::PopAndDestroy(); // field
       
   843             }
       
   844         }
       
   845     return result;
       
   846     }
       
   847 
       
   848 // --------------------------------------------------------------------------
       
   849 // CPsu2SimContactProcessor::CopyTitleFieldDataL
       
   850 //
       
   851 // Copies title field from source contact to given field type.
       
   852 // --------------------------------------------------------------------------
       
   853 //
       
   854 void CPsu2SimContactProcessor::CopyTitleFieldDataL( 
       
   855         MVPbkStoreContact& aSource, MVPbkStoreContact& aTarget,
       
   856         const MVPbkFieldType& aSimTitleFieldType )
       
   857     {
       
   858     HBufC* title = iNameFormatter.GetContactTitleOrNullL
       
   859         ( aSource.Fields(),
       
   860           MPbk2ContactNameFormatter::EPreserveLeadingSpaces );
       
   861 
       
   862     if (title)
       
   863         {
       
   864         CleanupStack::PushL( title );
       
   865         MVPbkStoreContactField* field =
       
   866             aTarget.CreateFieldLC( aSimTitleFieldType );
       
   867         MVPbkContactFieldTextData& data =
       
   868             MVPbkContactFieldTextData::Cast( field->FieldData() );
       
   869         TruncateAndCopyFieldDataL( *title, data );
       
   870         aTarget.AddFieldL(field);
       
   871         CleanupStack::Pop(field);
       
   872         CleanupStack::PopAndDestroy(title);
       
   873         }
       
   874     }
       
   875 
       
   876 
       
   877 // --------------------------------------------------------------------------
       
   878 // CPsu2SimContactProcessor::CopyFieldDataL
       
   879 // --------------------------------------------------------------------------
       
   880 //
       
   881 TBool CPsu2SimContactProcessor::CopyFieldDataL( 
       
   882         const TDesC& aSource, MVPbkContactFieldTextData& aTarget,
       
   883         const MVPbkFieldType& aSimType )
       
   884     {
       
   885     TBool result = ETrue;
       
   886     // If truncation is allowed for the field type then truncate
       
   887     // and copy
       
   888     if ( iCopyToSimFieldInfoArray.TruncationAllowed( aSimType ) )
       
   889         {
       
   890         TruncateAndCopyFieldDataL( aSource, aTarget );
       
   891         }
       
   892     // otherwise check if there is enough space for data
       
   893     else if ( aSource.Length() <=
       
   894             aTarget.MaxLength() )
       
   895         {
       
   896         aTarget.SetTextL( aSource );
       
   897         }
       
   898     else
       
   899         {
       
   900         result = EFalse;
       
   901         }
       
   902     return result;
       
   903     }
       
   904 
       
   905 // --------------------------------------------------------------------------
       
   906 // CPsu2SimContactProcessor::TruncateAndCopyFieldDataL
       
   907 // --------------------------------------------------------------------------
       
   908 //
       
   909 void CPsu2SimContactProcessor::TruncateAndCopyFieldDataL( 
       
   910         const TDesC& aSource, 
       
   911         MVPbkContactFieldTextData& aTarget )
       
   912     {
       
   913     PBK2_DEBUG_PRINT( PBK2_DEBUG_STRING
       
   914         ("CPsu2SimContactProcessor::TruncateAndCopyFieldDataL max len: [%i]"),
       
   915         aTarget.MaxLength() );
       
   916 
       
   917     // Try first with SMS 7-bit encoding
       
   918     TInt unconvertedCount(0);
       
   919     TPtrC data = iCharConvSms7Bit->CheckFieldLengthL
       
   920             ( aSource, aTarget.MaxLength(), unconvertedCount );
       
   921 
       
   922     PBK2_DEBUG_PRINT( PBK2_DEBUG_STRING
       
   923         ("CPsu2SimContactProcessor:: max converted len: [%i], unconv: [%i]"),
       
   924         data.Length(), unconvertedCount );
       
   925 
       
   926     // If any characters could not be converted with SMS 7-bit encoding we need
       
   927     // to check length using UCS-2 encoding which requires more space per a
       
   928     // character.
       
   929     // With some character sets that contain less than 128 characters the
       
   930     // resulting data is shorter than the maximum field length allowed by the
       
   931     // (U)SIM. In other words we may truncate the text fields stored to the
       
   932     // (U)SIM more than would be required by the (U)SIM. This is because
       
   933     // there are actually three possible coding schemes for such character
       
   934     // sets available. See Annex B in document 3GPP TS 11.11.
       
   935     // The conversion is made by SIM ATK TSY so we would need to have some
       
   936     // SAT server API available to be able to determine the actual maximum
       
   937     // length for the data unambiguously.
       
   938     if( unconvertedCount > 0 )
       
   939         {
       
   940         // Leave one character extra space in case of non-7-bit conversions
       
   941         // This seems to be a feature of (U)SIM that it requires one extra byte
       
   942         // for unicode contact name (see also the 3GPP reference mentioned in
       
   943         // the comment above).
       
   944         data.Set( iCharConvUcs2->CheckFieldLengthL
       
   945                 ( aSource, aTarget.MaxLength() - 1, unconvertedCount ) );
       
   946         PBK2_DEBUG_PRINT( PBK2_DEBUG_STRING
       
   947             ("CPsu2SimContactProcessor:: max converted len: [%i], unconv: [%i]"),
       
   948             data.Length(), unconvertedCount );
       
   949         }
       
   950 
       
   951     aTarget.SetTextL( data );
       
   952     }
       
   953 
       
   954 // --------------------------------------------------------------------------
       
   955 // CPsu2SimContactProcessor::MaxNumberOfFieldL
       
   956 //
       
   957 // Returns the maximum amount of field of given type that
       
   958 // can be added to the contact.
       
   959 // --------------------------------------------------------------------------
       
   960 //
       
   961 TInt CPsu2SimContactProcessor::MaxNumberOfFieldL( 
       
   962         MVPbkStoreContact& aContact, const MVPbkFieldType& aType )
       
   963     {
       
   964     TInt maxAmount( aContact.MaxNumberOfFieldL( aType ) );
       
   965     // For numbers it must be checked that is ANR file(s) of USIM full.
       
   966     if ( IsNumberType( aType ) )
       
   967         {
       
   968         // Reduce the max according to amount of ANR errors from TSY
       
   969         maxAmount -= iNumOfAdditionalNumberErrors;
       
   970         }
       
   971     return maxAmount;
       
   972     }
       
   973 
       
   974 // --------------------------------------------------------------------------
       
   975 // CPsu2SimContactProcessor::RemoveContactsThatHaveOnlyNameL
       
   976 // --------------------------------------------------------------------------
       
   977 //
       
   978 void CPsu2SimContactProcessor::RemoveContactsThatHaveOnlyNameL()
       
   979     {
       
   980     const TInt cntCount = iNewSimContacts.Count();
       
   981     for ( TInt i = cntCount - 1; i >= 0; --i )
       
   982         {
       
   983         if ( !IsValidContactToSaveL( *iNewSimContacts[i] ) )
       
   984             {
       
   985             delete iNewSimContacts[i];
       
   986             iNewSimContacts.Remove( i );
       
   987             }
       
   988         }
       
   989     }
       
   990 
       
   991 // --------------------------------------------------------------------------
       
   992 // CPsu2SimContactProcessor::IsValidContactToSaveL
       
   993 // --------------------------------------------------------------------------
       
   994 //
       
   995 TBool CPsu2SimContactProcessor::IsValidContactToSaveL( 
       
   996         MVPbkStoreContact& aSimContact )
       
   997     {
       
   998     // Specification says that "If the contact does not contain any number
       
   999     // it is not copied to SIM.". This is valid for 2G SIMs but it's assumed
       
  1000     // that in USIM this means that contact that don't have any data fields
       
  1001     // is not copied. In other words contact is valid if it has other than
       
  1002     // title fields.
       
  1003     MVPbkStoreContactFieldCollection& fields = aSimContact.Fields();
       
  1004     const TInt fieldCount = fields.FieldCount();
       
  1005     for ( TInt i = 0; i < fieldCount; ++i )
       
  1006         {
       
  1007         if ( !iNameFormatter.IsTitleField( fields.FieldAt( i ) ) )
       
  1008             {
       
  1009             return ETrue;
       
  1010             }
       
  1011         }
       
  1012     return EFalse;
       
  1013     }
       
  1014 
       
  1015 //  End of File