phonebookui/Phonebook2/ServerApplication/src/CPbk2AssignSingleProperty.cpp
branchRCL_3
changeset 20 f4a778e096c2
parent 0 e686773b3f54
child 21 9da50d567e3c
equal deleted inserted replaced
19:5b6f26637ad3 20:f4a778e096c2
       
     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 contact field type selector for
       
    15 *              : single contact assign.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 #include "CPbk2AssignSingleProperty.h"
       
    21 
       
    22 // Phonebook 2
       
    23 #include "Pbk2AssignValidateField.h"
       
    24 #include "CPbk2ServerAppAppUi.h"
       
    25 #include <Pbk2ServerApp.rsg>
       
    26 #include <CPbk2FieldPropertyArray.h>
       
    27 #include <MPbk2FieldProperty.h>
       
    28 #include <TPbk2AddItemWrapper.h>
       
    29 #include <CPbk2PresentationContact.h>
       
    30 #include <MPbk2ApplicationServices.h>
       
    31 
       
    32 // Virtual Phonebook
       
    33 #include <MVPbkContactStore.h>
       
    34 #include <MVPbkContactStoreProperties.h>
       
    35 #include <CVPbkContactManager.h>
       
    36 
       
    37 
       
    38 /// Unnamed namespace for local definitions
       
    39 namespace {
       
    40 
       
    41 const TInt KFirstElement = 0;
       
    42 
       
    43 enum TPanicCode
       
    44     {
       
    45     ENullPointer,
       
    46     EInvalidParameter
       
    47     };
       
    48 
       
    49 void Panic(TPanicCode aReason)
       
    50     {
       
    51     _LIT(KPanicText, "CPbk2AssignSingleProperty");
       
    52     User::Panic(KPanicText,aReason);
       
    53     }
       
    54 
       
    55 } /// namespace
       
    56 
       
    57 
       
    58 // --------------------------------------------------------------------------
       
    59 // CPbk2AssignSingleProperty::CPbk2AssignSingleProperty
       
    60 // --------------------------------------------------------------------------
       
    61 //
       
    62 CPbk2AssignSingleProperty::CPbk2AssignSingleProperty
       
    63         ( HBufC8& aSelector, TInt aResourceId,
       
    64           MVPbkStoreContact& aStoreContact,
       
    65           CVPbkContactManager& aContactManager ) :
       
    66             CPbk2SelectFieldPropertyBase( aSelector, aResourceId ),
       
    67             iFieldIndex( KErrNotSupported ),
       
    68             iStoreContact( aStoreContact ),
       
    69             iContactManager( aContactManager )
       
    70     {
       
    71     }
       
    72 
       
    73 // --------------------------------------------------------------------------
       
    74 // CPbk2AssignSingleProperty::~CPbk2AssignSingleProperty
       
    75 // --------------------------------------------------------------------------
       
    76 //
       
    77 CPbk2AssignSingleProperty::~CPbk2AssignSingleProperty()
       
    78     {
       
    79     }
       
    80 
       
    81 // --------------------------------------------------------------------------
       
    82 // CPbk2AssignSingleProperty::NewL
       
    83 // --------------------------------------------------------------------------
       
    84 //
       
    85 CPbk2AssignSingleProperty* CPbk2AssignSingleProperty::NewL
       
    86         ( HBufC8& aSelector, MVPbkStoreContact& aStoreContact,
       
    87           CVPbkContactManager& aContactManager )
       
    88     {
       
    89     CPbk2AssignSingleProperty* self =
       
    90         new ( ELeave ) CPbk2AssignSingleProperty
       
    91             ( aSelector, R_QTN_PHOB_QTL_ENTRY_ADD_TO, aStoreContact,
       
    92               aContactManager );
       
    93     CleanupStack::PushL( self );
       
    94     self->ConstructL();
       
    95     CleanupStack::Pop( self );
       
    96     return self;
       
    97     }
       
    98 
       
    99 // --------------------------------------------------------------------------
       
   100 // CPbk2AssignSingleProperty::ConstructL
       
   101 // --------------------------------------------------------------------------
       
   102 //
       
   103 void CPbk2AssignSingleProperty::ConstructL()
       
   104     {
       
   105     BaseConstructL();
       
   106     }
       
   107 
       
   108 // --------------------------------------------------------------------------
       
   109 // CPbk2AssignSingleProperty::PrepareL
       
   110 // --------------------------------------------------------------------------
       
   111 //
       
   112 void CPbk2AssignSingleProperty::PrepareL()
       
   113     {
       
   114     // Create wrappers
       
   115     CreateWrappersL( iStoreContact.ContactStore().StoreProperties().Uri() );
       
   116 
       
   117     CPbk2ServerAppAppUi& appUi = static_cast<CPbk2ServerAppAppUi&>
       
   118         ( *CEikonEnv::Static()->EikAppUi() );
       
   119 
       
   120     CPbk2PresentationContact* contact = CPbk2PresentationContact::NewL
       
   121         ( iStoreContact, appUi.ApplicationServices().FieldProperties() );
       
   122     CleanupStack::PushL( contact );
       
   123 
       
   124     // Go through all the wrappers and validate them
       
   125     const TInt count( iWrappers.Count() );
       
   126     // Loop backwards
       
   127     for ( TInt i( count - 1 ); i >= 0; --i )
       
   128         {
       
   129         TPbk2AddItemWrapper wrapper = iWrappers[i];
       
   130 
       
   131         // Check is the field type used in contact
       
   132         iResult = Pbk2AssignValidateField::ValidateFieldTypeUsageInContactL
       
   133             ( *contact, wrapper.PropertyAt( KFirstElement ).FieldType(),
       
   134               iContactManager.FsSession(), iFieldIndex );
       
   135 
       
   136         if ( iResult == KErrAlreadyExists && count > 1)
       
   137             {
       
   138             // Field type is already fully used so remove
       
   139             // the corresponding wrapper. Do not do this if there
       
   140             // is only one wrapper left. In that case the user is
       
   141             // asked for replace confirmation.
       
   142             iWrappers.Remove( i );
       
   143             }
       
   144         else if ( iResult == KErrNotSupported && count > 1 )
       
   145             {
       
   146             // Field type not supported, remove corresponding wrapper
       
   147             iWrappers.Remove( i );
       
   148             }
       
   149         }
       
   150 
       
   151     // If there is just one wrapper, the result is already correct,
       
   152     // but in other cases update result according to wrapper count
       
   153     if ( iWrappers.Count() == 0 )
       
   154         {
       
   155         // No suitable wrappers -> field type is not
       
   156         // supported by this contact
       
   157         iResult = KErrNotSupported;
       
   158         iFieldIndex = KErrNotSupported;
       
   159         }
       
   160     else if ( iWrappers.Count() > 1 )
       
   161         {
       
   162         // Several wrappers exist
       
   163         iResult = KErrNone;
       
   164         }
       
   165 
       
   166     CleanupStack::PopAndDestroy( contact );
       
   167     }
       
   168 
       
   169 // --------------------------------------------------------------------------
       
   170 // CPbk2AssignSingleProperty::ExecuteL
       
   171 // --------------------------------------------------------------------------
       
   172 //
       
   173 TInt CPbk2AssignSingleProperty::ExecuteL()
       
   174     {
       
   175     __ASSERT_ALWAYS( iFieldProperties, Panic( ENullPointer ) );
       
   176 
       
   177     // Display select field query
       
   178     TInt index = ShowSelectFieldQueryL();
       
   179 
       
   180     if ( index > KErrNotFound && iWrappers.Count() > 0 &&
       
   181          iWrappers.Count() > index )
       
   182         {
       
   183         // Get selected field type
       
   184         iSelectedFieldType =
       
   185             &iWrappers[index].PropertyAt( KFirstElement ).FieldType();
       
   186         }
       
   187     else if ( index == KErrCancel )
       
   188         {
       
   189         // Cancel was pressed
       
   190         iResult = KErrCancel;
       
   191         }
       
   192 
       
   193     return iResult;
       
   194     }
       
   195 
       
   196 // --------------------------------------------------------------------------
       
   197 // CPbk2AssignSingleProperty::SelectedFieldIndex
       
   198 // --------------------------------------------------------------------------
       
   199 //
       
   200 TInt CPbk2AssignSingleProperty::SelectedFieldIndex() const
       
   201     {
       
   202     return iFieldIndex;
       
   203     }
       
   204 
       
   205 // End of File