phonebookui/Phonebook2/spbcontentprovider/src/spbphonenumberparser.cpp
branchRCL_3
changeset 20 f4a778e096c2
child 21 9da50d567e3c
equal deleted inserted replaced
19:5b6f26637ad3 20:f4a778e096c2
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  .
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "spbphonenumberparser.h"
       
    20 #include "spbcontent.h"
       
    21 
       
    22 #include <MVPbkContactOperationBase.h>
       
    23 #include <CVPbkContactManager.h>
       
    24 #include <MVPbkStoreContact.h>
       
    25 #include <MVPbkStoreContactFieldCollection.h>
       
    26 #include <CVPbkDefaultAttribute.h>
       
    27 #include <MVPbkContactFieldTextData.h>
       
    28 #include <MVPbkFieldType.h>
       
    29 #include <VPbkEng.rsg>						// resource ids for numbers
       
    30 
       
    31 
       
    32 // ---------------------------------------------------------------------------
       
    33 // CSpbPhoneNumberParser::NewL
       
    34 // ---------------------------------------------------------------------------
       
    35 //
       
    36 CSpbPhoneNumberParser* CSpbPhoneNumberParser::NewL( 
       
    37         CVPbkContactManager& aContactManager,
       
    38         CSpbContent& aContent)
       
    39     {
       
    40     CSpbPhoneNumberParser* self = 
       
    41             new (ELeave) CSpbPhoneNumberParser(aContactManager, aContent);
       
    42     CleanupStack::PushL( self );
       
    43     self->ConstructL();
       
    44     CleanupStack::Pop( self );
       
    45     return self;
       
    46     }
       
    47 
       
    48 // ---------------------------------------------------------------------------
       
    49 // CSpbPhoneNumberParser::~CSpbPhoneNumberParser
       
    50 // ---------------------------------------------------------------------------
       
    51 //
       
    52 CSpbPhoneNumberParser::~CSpbPhoneNumberParser()
       
    53     {
       
    54     delete iOperation;
       
    55     iOperation = NULL;
       
    56     }
       
    57 
       
    58 // ---------------------------------------------------------------------------
       
    59 // CSpbPhoneNumberParser::CSpbPhoneNumberParser
       
    60 // ---------------------------------------------------------------------------
       
    61 //
       
    62 inline CSpbPhoneNumberParser::CSpbPhoneNumberParser(
       
    63         CVPbkContactManager& aContactManager,
       
    64         CSpbContent& aContent)
       
    65 : iContactManager(aContactManager),
       
    66   iContent(aContent)
       
    67     {
       
    68     
       
    69     }
       
    70 
       
    71 // ---------------------------------------------------------------------------
       
    72 // CSpbPhoneNumberParser::ConstructL
       
    73 // ---------------------------------------------------------------------------
       
    74 //
       
    75 inline void CSpbPhoneNumberParser::ConstructL()
       
    76     {
       
    77     }
       
    78 
       
    79 // ---------------------------------------------------------------------------
       
    80 // CSpbPhoneNumberParser::VPbkSingleContactOperationComplete
       
    81 // ---------------------------------------------------------------------------
       
    82 //
       
    83 void CSpbPhoneNumberParser::VPbkSingleContactOperationComplete(
       
    84         MVPbkContactOperationBase& /*aOperation*/,
       
    85         MVPbkStoreContact* aContact )
       
    86     {
       
    87     delete iOperation;
       
    88     iOperation = NULL;
       
    89     TRAP_IGNORE( SolvePhoneNumberL( *aContact ) );
       
    90 	delete aContact;
       
    91     }
       
    92 
       
    93 // ---------------------------------------------------------------------------
       
    94 // CSpbPhoneNumberParser::VPbkSingleContactOperationFailed
       
    95 // ---------------------------------------------------------------------------
       
    96 //
       
    97 void CSpbPhoneNumberParser::VPbkSingleContactOperationFailed(
       
    98         MVPbkContactOperationBase& /*aOperation*/, 
       
    99         TInt /*aError*/ )
       
   100     {
       
   101     delete iOperation;
       
   102     iOperation = NULL;
       
   103     TRAP_IGNORE( iContent.PhoneNumberUpdatedL( 
       
   104             KNullDesC, CSpbContentProvider::ETypePhoneNumber ) );
       
   105     }
       
   106 
       
   107 // ---------------------------------------------------------------------------
       
   108 // CSpbPhoneNumberParser::SolvePhoneNumberL
       
   109 // ---------------------------------------------------------------------------
       
   110 //
       
   111 void CSpbPhoneNumberParser::SolvePhoneNumberL( MVPbkStoreContact& aContact )
       
   112     {
       
   113     RBuf number;
       
   114     TInt numberCount = 0;
       
   115 	
       
   116     // check if the contact has default a number
       
   117     MVPbkContactAttributeManager& attributeManager = 
       
   118             iContactManager.ContactAttributeManagerL();
       
   119 	
       
   120     CVPbkDefaultAttribute* attribute = 
       
   121             CVPbkDefaultAttribute::NewL( EVPbkDefaultTypePhoneNumber );
       
   122     CleanupStack::PushL( attribute );
       
   123 
       
   124     TBool hasDefaultNumberField = EFalse;
       
   125     CDesC16Array* phoneNumberArray = new (ELeave) CDesC16ArraySeg(8);
       
   126     CleanupStack::PushL( phoneNumberArray );
       
   127 
       
   128     // get contact fields
       
   129     MVPbkStoreContactFieldCollection& fields = aContact.Fields();
       
   130     const TInt fieldCount = fields.FieldCount();
       
   131     // check fields
       
   132     for ( TInt i = 0; i < fieldCount; ++i )
       
   133         {
       
   134         const MVPbkStoreContactField& field = fields.FieldAt( i );
       
   135         // if correct data type
       
   136         if( field.FieldData().DataType() == EVPbkFieldStorageTypeText )
       
   137             {
       
   138             const MVPbkFieldType* fieldType = field.BestMatchingFieldType();
       
   139             if( fieldType )
       
   140                 {
       
   141                 const TInt fieldTypeResId = fieldType->FieldTypeResId();
       
   142                 // if one of the number fields
       
   143                 if( fieldTypeResId == R_VPBK_FIELD_TYPE_LANDPHONEGEN    ||
       
   144                     fieldTypeResId == R_VPBK_FIELD_TYPE_LANDPHONEHOME   ||
       
   145                     fieldTypeResId == R_VPBK_FIELD_TYPE_LANDPHONEWORK   ||
       
   146                     fieldTypeResId == R_VPBK_FIELD_TYPE_MOBILEPHONEGEN  ||
       
   147                     fieldTypeResId == R_VPBK_FIELD_TYPE_MOBILEPHONEHOME ||
       
   148                     fieldTypeResId == R_VPBK_FIELD_TYPE_MOBILEPHONEWORK ||
       
   149                     fieldTypeResId == R_VPBK_FIELD_TYPE_CARPHONE        ||
       
   150                     fieldTypeResId == R_VPBK_FIELD_TYPE_ASSTPHONE       ||
       
   151                     fieldTypeResId == R_VPBK_FIELD_TYPE_PAGERNUMBER )
       
   152                     {
       
   153                     const MVPbkContactFieldTextData* textData =
       
   154                             &MVPbkContactFieldTextData::Cast( field.FieldData() );
       
   155                     if( attributeManager.HasFieldAttributeL( *attribute, field ) )
       
   156                         {
       
   157                         //default number found
       
   158                         number.CreateL( textData->Text() );
       
   159                         hasDefaultNumberField = ETrue;
       
   160                         }
       
   161                     const TPtrC phoneNumber( textData->Text() );
       
   162                     // we need count phonenumbers same way how this is implemented in CCA
       
   163                     // so that we show same count for phonenumbers in names list
       
   164                     // CCA uses descriptor folded compare for phonenumbers
       
   165                     TInt dummy = 0;
       
   166                     if( 0 != phoneNumberArray->FindIsq( phoneNumber, dummy, ECmpFolded ) )
       
   167                         {
       
   168                         // phone number doesn't exist
       
   169                         phoneNumberArray->InsertIsqL( phoneNumber, ECmpFolded );
       
   170                         }
       
   171                     }
       
   172                 }
       
   173             }
       
   174         }
       
   175     numberCount = phoneNumberArray->Count();
       
   176     
       
   177     //default number not found, and only one number
       
   178     if( EFalse == hasDefaultNumberField && numberCount == 1 )
       
   179         {
       
   180             number.CreateL( (*phoneNumberArray)[0] );
       
   181         }
       
   182     
       
   183     CleanupStack::PopAndDestroy( phoneNumberArray );
       
   184     CleanupStack::PopAndDestroy( attribute );
       
   185     
       
   186     if( hasDefaultNumberField || numberCount <= 1 )
       
   187         {
       
   188         // inform the observer
       
   189         iContent.PhoneNumberUpdatedL( 
       
   190             number, CSpbContentProvider::ETypePhoneNumber );
       
   191         }
       
   192     else
       
   193         {
       
   194         // contact has multiple numbers and no default
       
   195         TBuf<12> count;
       
   196         count.Num( numberCount );
       
   197         iContent.PhoneNumberUpdatedL( 
       
   198             count, CSpbContentProvider::ETypePhoneNumberMultiple );
       
   199         }
       
   200     
       
   201     number.Close();
       
   202     }
       
   203 
       
   204 // ---------------------------------------------------------------------------
       
   205 // CSpbPhoneNumberParser::FetchPhoneNumber
       
   206 // ---------------------------------------------------------------------------
       
   207 //
       
   208 void CSpbPhoneNumberParser::FetchPhoneNumberL(
       
   209             const MVPbkContactLink& aLink)
       
   210     {
       
   211     delete iOperation;
       
   212     iOperation = NULL;
       
   213                    
       
   214     iOperation = iContactManager.RetrieveContactL(aLink, *this);
       
   215     }
       
   216         
       
   217 // end of file