phonebookui/Phonebook/Engine/src/CPbkEntryCopyAddToExisting.cpp
changeset 0 e686773b3f54
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     1 /*
       
     2 * Copyright (c) 2002 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *		Copies information from Phonebook entry to an existing entry.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 
       
    22 // Phonebook copy
       
    23 #include    "CPbkEntryCopyAddToExisting.h"
       
    24 
       
    25 // Phonebook debug
       
    26 #include    <PbkDebug.h>
       
    27 
       
    28 // Phonebook engine
       
    29 #include    <CPbkContactEngine.h>
       
    30 #include    <CPbkContactItem.h>
       
    31 #include    <CPbkFieldsInfo.h>
       
    32 #include    <MPbkContactNameFormat.h>
       
    33 
       
    34 /// Unnamed namespace for local definitions
       
    35 namespace {
       
    36 
       
    37 // LOCAL CONSTANTS AND MACROS
       
    38 
       
    39 #ifdef _DEBUG
       
    40 enum TPanicCode
       
    41     {
       
    42     EPanicPreCond_FindExistingNameL = 0,
       
    43     EPanicPreCond_CopyL
       
    44     };
       
    45 
       
    46 void Panic(TInt aReason)
       
    47     {
       
    48     _LIT(KPanicText, "CPbkEntryCopyAddToExisting");
       
    49     User::Panic(KPanicText, aReason);
       
    50     }
       
    51 #endif // _DEBUG
       
    52 
       
    53 } // namespace
       
    54 
       
    55 
       
    56 // MODULE DATA STRUCTURES
       
    57 
       
    58 // ================= MEMBER FUNCTIONS =======================
       
    59 
       
    60 inline CPbkEntryCopyAddToExisting::CPbkEntryCopyAddToExisting
       
    61         (const CPbkContactItem& aEntry,
       
    62         CPbkContactEngine& aEngine,
       
    63         TContactItemId aContactId) :
       
    64     iEntry(aEntry),
       
    65     iEngine(aEngine),
       
    66     iContactId(aContactId)
       
    67     {
       
    68     }
       
    69 
       
    70 CPbkEntryCopyAddToExisting* CPbkEntryCopyAddToExisting::NewLC
       
    71         (const CPbkContactItem& aEntry,
       
    72         CPbkContactEngine& aEngine,
       
    73         TContactItemId aContactId)
       
    74     {
       
    75     CPbkEntryCopyAddToExisting* self = 
       
    76         new(ELeave) CPbkEntryCopyAddToExisting(aEntry, aEngine, aContactId);
       
    77     CleanupStack::PushL(self);
       
    78     return self;
       
    79     }
       
    80 
       
    81 TContactItemId CPbkEntryCopyAddToExisting::CopyL()
       
    82     {
       
    83     __ASSERT_DEBUG(iContactId != KNullContactId, 
       
    84             Panic(EPanicPreCond_CopyL));
       
    85 
       
    86     PBK_DEBUG_PRINT(PBK_DEBUG_STRING("CPbkEntryCopyAddToExisting::CopyL start"));
       
    87     
       
    88     CPbkContactItem* item = iEngine.OpenContactLCX(iContactId);
       
    89 
       
    90     TBool result = EFalse;
       
    91     // find if phone number already in contact    
       
    92     const TInt fieldCount = iEntry.CardFields().Count();
       
    93     for (TInt i = 0; i < fieldCount; ++i)
       
    94         {
       
    95         const TPbkContactItemField& field = iEntry.CardFields()[i];
       
    96         if (field.FieldInfo().IsPhoneNumberField())
       
    97             {
       
    98             const TDesC& number = field.Text();
       
    99             TInt fieldIndex = 0;
       
   100             TPbkContactItemField* numberField = 
       
   101                 item->FindNextFieldWithPhoneNumber(number, 0, fieldIndex);
       
   102             if (!numberField)
       
   103                 {
       
   104                 // number not found in entry -> copy number to entry
       
   105                 CPbkFieldInfo* stdNumField = iEngine.FieldsInfo().Find(
       
   106                     EPbkFieldIdPhoneNumberMobile);
       
   107 
       
   108                 TPbkContactItemField* field = item->AddOrReturnUnusedFieldL(*stdNumField);
       
   109                 if (field->StorageType() == KStorageTypeText)
       
   110                     { 
       
   111                     // Put the data to the field
       
   112                     field->TextStorage()->SetTextL(number);
       
   113                     result = ETrue;
       
   114                     PBK_DEBUG_PRINT(PBK_DEBUG_STRING("CPbkEntryCopyAddToExisting::CopyL number added to existing contact"));
       
   115                     }
       
   116                 }
       
   117             else
       
   118                 {
       
   119                 // identical entry is already in phonebook
       
   120                 // this is counted as copied
       
   121                 result = ETrue;
       
   122                 PBK_DEBUG_PRINT(PBK_DEBUG_STRING("CPbkEntryCopyAddToExisting::CopyL number duplicate existed, copy counted"));
       
   123                 }
       
   124             }
       
   125         if (field.FieldInfo().FieldId() == EPbkFieldIdEmailAddress)
       
   126             {
       
   127             const TDesC& emailAddr = field.Text();
       
   128             TInt fieldIndex = 0;
       
   129             TPbkContactItemField* emailField = 
       
   130                 item->FindNextFieldWithText(emailAddr, fieldIndex);
       
   131             if (!emailField)
       
   132                 {
       
   133                 // email address not found in entry -> copy number to entry
       
   134                 CPbkFieldInfo* stdEmailField = iEngine.FieldsInfo().Find(
       
   135                     EPbkFieldIdEmailAddress);
       
   136 
       
   137                 TPbkContactItemField* field = item->AddOrReturnUnusedFieldL(*stdEmailField);
       
   138                 if (field->StorageType() == KStorageTypeText)
       
   139                     { 
       
   140                     // Put the data to the field
       
   141                     field->TextStorage()->SetTextL(emailAddr);
       
   142                     result = ETrue;
       
   143                     PBK_DEBUG_PRINT(PBK_DEBUG_STRING("CPbkEntryCopyAddToExisting::CopyL email added to existing contact"));
       
   144                     }
       
   145                 }
       
   146             else
       
   147                 {
       
   148                 // identical entry is already in phonebook this is counted as copied
       
   149                 result = ETrue;
       
   150                 PBK_DEBUG_PRINT(PBK_DEBUG_STRING("CPbkEntryCopyAddToExisting::CopyL email duplicate existed, copy counted"));
       
   151                 }
       
   152             }
       
   153         }
       
   154 
       
   155     TContactItemId contactId = KNullContactId;
       
   156     if (result)
       
   157         {
       
   158         iEngine.CommitContactL(*item, ETrue);
       
   159         contactId = item->Id();
       
   160         }
       
   161 
       
   162     CleanupStack::PopAndDestroy(2); // lock, item
       
   163     
       
   164     return contactId;
       
   165     }
       
   166 
       
   167 //  End of File