phonebookui/Phonebook2/ServerApplication/src/CPbk2ContactImppDataAssigner.cpp
changeset 0 e686773b3f54
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     1 /*
       
     2 * Copyright (c) 2005-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:  Phonebook 2 textual contact data assigner.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "CPbk2ContactImppDataAssigner.h"
       
    20 
       
    21 // Phonebook 2
       
    22 #include "MPbk2ContactAssignerObserver.h"
       
    23 
       
    24 // Virtual Phonebook
       
    25 #include <MVPbkStoreContact.h>
       
    26 #include <MVPbkContactFieldTextData.h>
       
    27 #include <MVPbkContactFieldUriData.h>
       
    28 #include <MVPbkFieldType.h>
       
    29 #include <MVPbkContactStore.h>
       
    30 #include <MVPbkContactStoreProperties.h>
       
    31 #include <TVPbkFieldVersitProperty.h>
       
    32 
       
    33 // --------------------------------------------------------------------------
       
    34 // CPbk2ContactImppDataAssigner::CPbk2ContactImppDataAssigner
       
    35 // --------------------------------------------------------------------------
       
    36 //
       
    37 CPbk2ContactImppDataAssigner::CPbk2ContactImppDataAssigner
       
    38         ( MPbk2ContactAssignerObserver& aObserver ):
       
    39             CActive( EPriorityIdle ), iObserver( aObserver ),
       
    40             iIndex( KErrNotSupported )
       
    41     {
       
    42     CActiveScheduler::Add( this );
       
    43     }
       
    44 
       
    45 // --------------------------------------------------------------------------
       
    46 // CPbk2ContactImppDataAssigner::~CPbk2ContactImppDataAssigner
       
    47 // --------------------------------------------------------------------------
       
    48 //
       
    49 CPbk2ContactImppDataAssigner::~CPbk2ContactImppDataAssigner()
       
    50     {
       
    51     Cancel();
       
    52     }
       
    53 
       
    54 // --------------------------------------------------------------------------
       
    55 // CPbk2ContactImppDataAssigner::NewL
       
    56 // --------------------------------------------------------------------------
       
    57 //
       
    58 CPbk2ContactImppDataAssigner* CPbk2ContactImppDataAssigner::NewL
       
    59         ( MPbk2ContactAssignerObserver& aObserver )
       
    60     {
       
    61     CPbk2ContactImppDataAssigner* self =
       
    62         new ( ELeave ) CPbk2ContactImppDataAssigner( aObserver );
       
    63     return self;
       
    64     }
       
    65 
       
    66 // --------------------------------------------------------------------------
       
    67 // CPbk2ContactImppDataAssigner::AssignDataL
       
    68 // --------------------------------------------------------------------------
       
    69 //
       
    70 void CPbk2ContactImppDataAssigner::AssignDataL(
       
    71         MVPbkStoreContact& aStoreContact,
       
    72         MVPbkStoreContactField* aContactField,
       
    73         const MVPbkFieldType* aFieldType, const HBufC* aDataBuffer )
       
    74     {
       
    75     TPtrC xSP;
       
    76     TPtrC firstName;
       
    77     TPtrC lastName;
       
    78     TPtrC nickname;
       
    79 
       
    80     ParseDataBuffer(&xSP, &firstName, &lastName, &nickname,
       
    81             TPtrC(*aDataBuffer));
       
    82 
       
    83     if ( !aContactField )
       
    84         {
       
    85         MVPbkStoreContactField* field =
       
    86             aStoreContact.CreateFieldLC( *aFieldType );
       
    87         MVPbkContactFieldUriData::Cast(field->FieldData()).
       
    88                 SetUriL(xSP);
       
    89         iIndex = aStoreContact.AddFieldL( field ); // takes ownership
       
    90         CleanupStack::Pop(); // field
       
    91         }
       
    92     else
       
    93         {
       
    94         MVPbkContactFieldUriData::Cast(aContactField->FieldData()).
       
    95                 SetUriL(xSP);
       
    96 
       
    97         // Find out the field index
       
    98         MVPbkStoreContactFieldCollection& fields = aStoreContact.Fields();
       
    99         const TInt fieldCount = fields.FieldCount();
       
   100         for (TInt i = 0; i < fieldCount; ++i )
       
   101             {
       
   102             MVPbkStoreContactField* compareField = fields.FieldAtLC( i );
       
   103             if ( compareField && compareField->IsSame( *aContactField ) )
       
   104                 {
       
   105                 iIndex = i;
       
   106                 CleanupStack::PopAndDestroy(); // compareField
       
   107                 break;
       
   108                 }
       
   109             CleanupStack::PopAndDestroy(); // compareField
       
   110             }
       
   111         }
       
   112 
       
   113     if (firstName.Length())
       
   114         {
       
   115         TVPbkFieldVersitProperty prop;
       
   116         prop.SetName(EVPbkVersitNameN);
       
   117         prop.SetSubField(EVPbkVersitSubFieldGivenName);
       
   118         UpdateField(prop, firstName, &aStoreContact);
       
   119         }
       
   120 
       
   121     if (lastName.Length())
       
   122         {
       
   123         TVPbkFieldVersitProperty prop;
       
   124         prop.SetName(EVPbkVersitNameN);
       
   125         prop.SetSubField(EVPbkVersitSubFieldFamilyName);
       
   126         UpdateField(prop, lastName, &aStoreContact);
       
   127         }
       
   128 
       
   129     if (nickname.Length())
       
   130         {
       
   131         _LIT8(KXNickname, "X-NICKNAME");
       
   132         TVPbkFieldVersitProperty prop;
       
   133         prop.SetName(EVPbkVersitNameX);
       
   134         prop.SetExtensionName(KXNickname);
       
   135         UpdateField(prop, nickname, &aStoreContact);
       
   136         }
       
   137 
       
   138     // Notify observer asynchronously
       
   139     IssueRequest();
       
   140     }
       
   141 
       
   142 // --------------------------------------------------------------------------
       
   143 // CPbk2ContactImppDataAssigner::AssignAttributeL
       
   144 // --------------------------------------------------------------------------
       
   145 //
       
   146 void CPbk2ContactImppDataAssigner::AssignAttributeL
       
   147         ( MVPbkStoreContact& /*aStoreContact*/,
       
   148           MVPbkStoreContactField* /*aContactField*/,
       
   149           TPbk2AttributeAssignData /*aAttributeAssignData*/ )
       
   150     {
       
   151     // Not supported
       
   152     User::Leave( KErrNotSupported );
       
   153     }
       
   154 
       
   155 // --------------------------------------------------------------------------
       
   156 // CPbk2ContactImppDataAssigner::RunL
       
   157 // --------------------------------------------------------------------------
       
   158 //
       
   159 void CPbk2ContactImppDataAssigner::RunL()
       
   160     {
       
   161     // Just notify the observer
       
   162     iObserver.AssignComplete( *this, iIndex );
       
   163     }
       
   164 
       
   165 // --------------------------------------------------------------------------
       
   166 // CPbk2ContactImppDataAssigner::DoCancel
       
   167 // --------------------------------------------------------------------------
       
   168 //
       
   169 void CPbk2ContactImppDataAssigner::DoCancel()
       
   170     {
       
   171     // Nothing to do
       
   172     }
       
   173 
       
   174 // --------------------------------------------------------------------------
       
   175 // CPbk2ContactImppDataAssigner::RunError
       
   176 // --------------------------------------------------------------------------
       
   177 //
       
   178 TInt CPbk2ContactImppDataAssigner::RunError( TInt /*aError*/ )
       
   179     {
       
   180     // No leaving code in RunL
       
   181     return KErrNone;
       
   182     }
       
   183 
       
   184 // --------------------------------------------------------------------------
       
   185 // CPbk2ContactImppDataAssigner::IssueRequest
       
   186 // --------------------------------------------------------------------------
       
   187 //
       
   188 void CPbk2ContactImppDataAssigner::IssueRequest()
       
   189     {
       
   190     TRequestStatus* status = &iStatus;
       
   191     User::RequestComplete( status, KErrNone );
       
   192     SetActive();
       
   193     }
       
   194 
       
   195 // --------------------------------------------------------------------------
       
   196 // CPbk2ContactImppDataAssigner::ParseDataBuffer
       
   197 // --------------------------------------------------------------------------
       
   198 //
       
   199 void CPbk2ContactImppDataAssigner::ParseDataBuffer(TPtrC* axSP,
       
   200         TPtrC* aFirstName, TPtrC* aLastName, TPtrC* aNickname,
       
   201         TPtrC aDataBuffer)
       
   202     {
       
   203     GetDataBufferPart(&aDataBuffer, axSP) &&
       
   204     GetDataBufferPart(&aDataBuffer, aFirstName) &&
       
   205     GetDataBufferPart(&aDataBuffer, aLastName) &&
       
   206     GetDataBufferPart(&aDataBuffer, aNickname);
       
   207     }
       
   208 
       
   209 // --------------------------------------------------------------------------
       
   210 // CPbk2ContactImppDataAssigner::UpdateField
       
   211 // --------------------------------------------------------------------------
       
   212 //
       
   213 void CPbk2ContactImppDataAssigner::UpdateField(
       
   214         const TVPbkFieldVersitProperty& aVersitProp, const TDesC& aValue,
       
   215         MVPbkStoreContact* aStoreContact)
       
   216     {
       
   217     MVPbkStoreContactFieldCollection& fields = aStoreContact->Fields();
       
   218     TInt count = fields.FieldCount();
       
   219     TInt i = 0;
       
   220     for (; i < count; i++)
       
   221         {
       
   222         MVPbkStoreContactField& field = fields.FieldAt(i);
       
   223         if (field.BestMatchingFieldType()->Matches(aVersitProp, 0))
       
   224             {
       
   225             MVPbkContactFieldTextData& data = MVPbkContactFieldTextData::Cast(
       
   226                     field.FieldData());
       
   227             if (!data.Text().Length())
       
   228                 {
       
   229                 data.SetTextL(aValue);
       
   230                 }
       
   231             // contact contains field of this type so finish loop
       
   232             break;
       
   233             }
       
   234         }
       
   235     
       
   236     // if field was not found then create it
       
   237     if (i == count)
       
   238         {
       
   239         const MVPbkFieldTypeList& suportedTypes = aStoreContact->ParentStore().
       
   240                 StoreProperties().SupportedFields();
       
   241         count = suportedTypes.FieldTypeCount();
       
   242         for (i = 0; i < count; i++)
       
   243             {
       
   244             const MVPbkFieldType& fieldType = suportedTypes.FieldTypeAt(i);
       
   245             if (fieldType.Matches(aVersitProp, 0))
       
   246                 {
       
   247                 MVPbkStoreContactField* field =
       
   248                     aStoreContact->CreateFieldLC(fieldType);
       
   249                 MVPbkContactFieldTextData::Cast(field->FieldData()).
       
   250                         SetTextL(aValue);
       
   251                 aStoreContact->AddFieldL(field); // takes ownership
       
   252                 CleanupStack::Pop(); // field
       
   253                 break;
       
   254                 }
       
   255             }
       
   256         }
       
   257     }
       
   258 
       
   259 // End of File