phonebookui/Phonebook2/UIControls/src/CPbk2DefaultAttributeProcess.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 default attribute modification processor.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "CPbk2DefaultAttributeProcess.h"
       
    20 
       
    21 // Phonebook 2
       
    22 #include <MPbk2DefaultAttributeProcessObserver.h>
       
    23 
       
    24 // Virtual Phonebook
       
    25 #include <MVPbkContactOperationBase.h>
       
    26 #include <MVPbkStoreContactField.h>
       
    27 #include <CVPbkDefaultAttribute.h>
       
    28 #include <CVPbkContactManager.h>
       
    29 #include <MVPbkStoreContact.h>
       
    30 
       
    31 // Debugging headers
       
    32 #include <Pbk2Debug.h>
       
    33 
       
    34 
       
    35 /// Unnamed namespace for local definitions
       
    36 namespace {
       
    37 
       
    38 const TInt KGranularity = 4;
       
    39 
       
    40 #ifdef _DEBUG
       
    41 
       
    42 enum TPanicCode_Process
       
    43     {
       
    44     EPanicLogic_AttributeOperationComplete,
       
    45     EPanicLogic_AttributeOperationFailed,
       
    46     EPanicPreCond_SetDefaultsL,
       
    47     EPanicPreCond_RemoveDefaultsL,
       
    48     };
       
    49 
       
    50 static void Panic( TPanicCode_Process aReason )
       
    51     {
       
    52     _LIT(KPanicText, "CPbk2DefaultAttributeProcess");
       
    53     User::Panic(KPanicText, aReason);
       
    54     }
       
    55 
       
    56 #endif // _DEBUG
       
    57 
       
    58 } /// namespace
       
    59 
       
    60 // --------------------------------------------------------------------------
       
    61 // CPbk2DefaultAttributeProcess::CPbk2DefaultAttributeProcess
       
    62 // --------------------------------------------------------------------------
       
    63 //
       
    64 CPbk2DefaultAttributeProcess::CPbk2DefaultAttributeProcess
       
    65         ( CVPbkContactManager& aManager, MVPbkStoreContact& aContact,
       
    66           MPbk2DefaultAttributeProcessObserver& aObserver ):
       
    67             iManager( aManager ), iContact( aContact ),
       
    68             iObserver( aObserver )
       
    69     {
       
    70     }
       
    71 
       
    72 // --------------------------------------------------------------------------
       
    73 // CPbk2DefaultAttributeProcess::CPbk2DefaultAttributeProcess
       
    74 // --------------------------------------------------------------------------
       
    75 //
       
    76 CPbk2DefaultAttributeProcess::~CPbk2DefaultAttributeProcess()
       
    77     {
       
    78     delete iDefaultAttributes;
       
    79     delete iSetAttributeOperation;
       
    80     delete iRemoveAttributeOperation;
       
    81     delete iRemovePreviousAttributeOperation;
       
    82     delete iRemoveAttributeField;
       
    83     }
       
    84 
       
    85 // --------------------------------------------------------------------------
       
    86 // CPbk2DefaultAttributeProcess::NewL
       
    87 // --------------------------------------------------------------------------
       
    88 //
       
    89 EXPORT_C  CPbk2DefaultAttributeProcess* CPbk2DefaultAttributeProcess::NewL
       
    90         ( CVPbkContactManager& aManager, MVPbkStoreContact& aContact,
       
    91         MPbk2DefaultAttributeProcessObserver& aObserver )
       
    92     {
       
    93     CPbk2DefaultAttributeProcess* self =
       
    94         new ( ELeave ) CPbk2DefaultAttributeProcess
       
    95             ( aManager, aContact, aObserver );
       
    96     return self;
       
    97     }
       
    98 
       
    99 // --------------------------------------------------------------------------
       
   100 // CPbk2DefaultAttributeProcess::SetDefaultL
       
   101 // --------------------------------------------------------------------------
       
   102 //
       
   103 EXPORT_C void CPbk2DefaultAttributeProcess::SetDefaultL
       
   104         ( TVPbkDefaultType aDefaultType, MVPbkStoreContactField& aField )
       
   105     {
       
   106     iAttributeType = aDefaultType;
       
   107     iSetAttributeField = &aField;
       
   108 
       
   109     delete iDefaultAttributes;
       
   110     iDefaultAttributes = NULL;
       
   111     iDefaultAttributes =
       
   112         new( ELeave ) CArrayFixFlat<TVPbkDefaultType>( KGranularity );
       
   113     iDefaultAttributes->AppendL( aDefaultType );
       
   114 
       
   115     SetNextL();
       
   116     }
       
   117 
       
   118 // --------------------------------------------------------------------------
       
   119 // CPbk2DefaultAttributeProcess::RemoveDefaultL
       
   120 // --------------------------------------------------------------------------
       
   121 //
       
   122 EXPORT_C void CPbk2DefaultAttributeProcess::RemoveDefaultL
       
   123         ( TVPbkDefaultType aDefaultType  )
       
   124     {
       
   125     iAttributeType = aDefaultType;
       
   126 
       
   127     delete iDefaultAttributes;
       
   128     iDefaultAttributes = NULL;
       
   129     iDefaultAttributes =
       
   130         new( ELeave ) CArrayFixFlat<TVPbkDefaultType>( KGranularity );
       
   131     iDefaultAttributes->AppendL( aDefaultType );
       
   132 
       
   133 
       
   134     RemoveNextL();
       
   135     }
       
   136 
       
   137 // --------------------------------------------------------------------------
       
   138 // CPbk2DefaultAttributeProcess::SetDefaultsL
       
   139 // --------------------------------------------------------------------------
       
   140 //
       
   141 EXPORT_C void CPbk2DefaultAttributeProcess::SetDefaultsL
       
   142         ( CArrayFixFlat<TVPbkDefaultType>* aDefaultProperties,
       
   143           MVPbkStoreContactField& aField )
       
   144     {
       
   145     __ASSERT_DEBUG( aDefaultProperties->Count() > 0,
       
   146         Panic( EPanicPreCond_SetDefaultsL ) );
       
   147 
       
   148     iSetAttributeField = &aField;
       
   149 
       
   150     // Take ownership
       
   151     delete iDefaultAttributes;
       
   152     iDefaultAttributes = aDefaultProperties;
       
   153 
       
   154     SetNextL();
       
   155     }
       
   156 
       
   157 // --------------------------------------------------------------------------
       
   158 // CPbk2DefaultAttributeProcess::RemoveDefaultsL
       
   159 // --------------------------------------------------------------------------
       
   160 //
       
   161 EXPORT_C void CPbk2DefaultAttributeProcess::RemoveDefaultsL
       
   162         ( CArrayFixFlat<TVPbkDefaultType>* aDefaultProperties )
       
   163     {
       
   164     __ASSERT_DEBUG( aDefaultProperties->Count() > 0,
       
   165         Panic( EPanicPreCond_RemoveDefaultsL ) );
       
   166 
       
   167     // Take ownership
       
   168     delete iDefaultAttributes;
       
   169     iDefaultAttributes = aDefaultProperties;
       
   170 
       
   171     RemoveNextL();
       
   172     }
       
   173 
       
   174 // --------------------------------------------------------------------------
       
   175 // CPbk2DefaultAttributeProcess::AttributeOperationComplete
       
   176 // --------------------------------------------------------------------------
       
   177 //
       
   178 void CPbk2DefaultAttributeProcess::AttributeOperationComplete
       
   179         ( MVPbkContactOperationBase& aOperation )
       
   180     {
       
   181     __ASSERT_DEBUG( &aOperation == iSetAttributeOperation ||
       
   182         &aOperation == iRemoveAttributeOperation ||
       
   183         &aOperation == iRemovePreviousAttributeOperation,
       
   184             Panic( EPanicLogic_AttributeOperationComplete ) );
       
   185 
       
   186     TInt err = KErrNone;
       
   187 
       
   188     if ( &aOperation == iSetAttributeOperation )
       
   189         {
       
   190         // Move to next attribute
       
   191         TRAP( err, SetNextL() );
       
   192         }
       
   193     else if ( &aOperation == iRemoveAttributeOperation )
       
   194         {
       
   195         // Move to next attribute
       
   196         TRAP( err, RemoveNextL() );
       
   197         }
       
   198     else if ( &aOperation == iRemovePreviousAttributeOperation )
       
   199         {
       
   200         // Previous attribute was removed, set the new attribute
       
   201         TRAP( err, DoSetL( iAttributeType ) );
       
   202         }
       
   203 
       
   204     // Handle error
       
   205     if ( err != KErrNone )
       
   206         {
       
   207         iObserver.AttributeProcessFailed( err );
       
   208         }
       
   209     }
       
   210 
       
   211 // --------------------------------------------------------------------------
       
   212 // CPbk2DefaultAttributeProcess::AttributeOperationFailed
       
   213 // --------------------------------------------------------------------------
       
   214 //
       
   215 void CPbk2DefaultAttributeProcess::AttributeOperationFailed
       
   216         ( MVPbkContactOperationBase& aOperation, TInt aError )
       
   217     {
       
   218     __ASSERT_DEBUG( &aOperation == iSetAttributeOperation ||
       
   219         &aOperation == iRemoveAttributeOperation ||
       
   220         &aOperation == iRemovePreviousAttributeOperation,
       
   221             Panic( EPanicLogic_AttributeOperationFailed ) );
       
   222 
       
   223     iObserver.AttributeProcessFailed( aError );
       
   224     }
       
   225 
       
   226 // --------------------------------------------------------------------------
       
   227 // CPbk2DefaultAttributeProcess::FindContactFieldWithAttributeL
       
   228 // --------------------------------------------------------------------------
       
   229 //
       
   230 MVPbkStoreContactField*
       
   231         CPbk2DefaultAttributeProcess::FindContactFieldWithAttributeL
       
   232             ( TVPbkDefaultType aDefaultType )
       
   233     {
       
   234     MVPbkStoreContactField* field = NULL;
       
   235 
       
   236     // Loop through contact's fields and find the specified field
       
   237     const TInt fieldCount = iContact.Fields().FieldCount();
       
   238     CVPbkDefaultAttribute* attr =
       
   239         CVPbkDefaultAttribute::NewL( aDefaultType );
       
   240     CleanupStack::PushL( attr );
       
   241 
       
   242     for ( TInt i=0; i < fieldCount; ++i )
       
   243         {
       
   244         MVPbkStoreContactField* candidate =
       
   245             iContact.Fields().FieldAtLC( i );
       
   246 
       
   247         // Check if field has default attribute defaultType
       
   248         if ( iManager.ContactAttributeManagerL().HasFieldAttributeL
       
   249                 ( *attr, *candidate ) )
       
   250             {
       
   251             field = candidate;
       
   252             CleanupStack::Pop(); // candicate
       
   253             break;
       
   254             }
       
   255         else
       
   256             {
       
   257             CleanupStack::PopAndDestroy(); // candidate
       
   258             }
       
   259         }
       
   260 
       
   261     CleanupStack::PopAndDestroy( attr );
       
   262     return field;
       
   263     }
       
   264 
       
   265 // --------------------------------------------------------------------------
       
   266 // CPbk2DefaultAttributeProcess::DoSetL
       
   267 // --------------------------------------------------------------------------
       
   268 //
       
   269 void CPbk2DefaultAttributeProcess::DoSetL( TVPbkDefaultType aDefaultType )
       
   270     {
       
   271     // Set given attribute to given field
       
   272     CVPbkDefaultAttribute* attr =
       
   273         CVPbkDefaultAttribute::NewL( aDefaultType );
       
   274     CleanupStack::PushL( attr );
       
   275 
       
   276     delete iSetAttributeOperation;
       
   277     iSetAttributeOperation = NULL;
       
   278     iSetAttributeOperation =
       
   279         iManager.ContactAttributeManagerL().SetFieldAttributeL
       
   280             ( *iSetAttributeField, *attr, *this );
       
   281 
       
   282     CleanupStack::PopAndDestroy( attr );
       
   283     }
       
   284 
       
   285 // --------------------------------------------------------------------------
       
   286 // CPbk2DefaultAttributeProcess::DoRemoveL
       
   287 // --------------------------------------------------------------------------
       
   288 //
       
   289 TBool CPbk2DefaultAttributeProcess::DoRemoveL
       
   290         ( TVPbkDefaultType aDefaultType )
       
   291     {
       
   292     TBool found = EFalse;
       
   293 
       
   294     delete iRemoveAttributeField;
       
   295     iRemoveAttributeField = NULL;
       
   296     iRemoveAttributeField = FindContactFieldWithAttributeL( aDefaultType );
       
   297 
       
   298     if ( iRemoveAttributeField )
       
   299         {
       
   300         found = ETrue;
       
   301 
       
   302         CVPbkDefaultAttribute* attr =
       
   303             CVPbkDefaultAttribute::NewL( aDefaultType );
       
   304         CleanupStack::PushL( attr );
       
   305 
       
   306         delete iRemoveAttributeOperation;
       
   307         iRemoveAttributeOperation = NULL;
       
   308         iRemoveAttributeOperation = iManager.ContactAttributeManagerL().
       
   309             RemoveFieldAttributeL( *iRemoveAttributeField, *attr, *this );
       
   310 
       
   311         CleanupStack::PopAndDestroy( attr );
       
   312         }
       
   313 
       
   314     return found;
       
   315     }
       
   316 
       
   317 // --------------------------------------------------------------------------
       
   318 // CPbk2DefaultAttributeProcess::DoRemovePreviousL
       
   319 // --------------------------------------------------------------------------
       
   320 //
       
   321 TBool CPbk2DefaultAttributeProcess::DoRemovePreviousL
       
   322         ( TVPbkDefaultType aDefaultType )
       
   323     {
       
   324     TBool found = EFalse;
       
   325 
       
   326     delete iRemoveAttributeField;
       
   327     iRemoveAttributeField = NULL;
       
   328     iRemoveAttributeField = FindContactFieldWithAttributeL( aDefaultType );
       
   329 
       
   330     if ( iRemoveAttributeField )
       
   331         {
       
   332         found = ETrue;
       
   333 
       
   334         CVPbkDefaultAttribute* attr =
       
   335             CVPbkDefaultAttribute::NewL( aDefaultType );
       
   336         CleanupStack::PushL( attr );
       
   337 
       
   338         delete iRemovePreviousAttributeOperation;
       
   339         iRemovePreviousAttributeOperation = NULL;
       
   340         iRemovePreviousAttributeOperation =
       
   341             iManager.ContactAttributeManagerL().RemoveFieldAttributeL
       
   342                 ( *iRemoveAttributeField, *attr, *this );
       
   343 
       
   344         CleanupStack::PopAndDestroy( attr );
       
   345         }
       
   346 
       
   347     return found;
       
   348     }
       
   349 
       
   350 // --------------------------------------------------------------------------
       
   351 // CPbk2DefaultAttributeProcess::SetNextL
       
   352 // --------------------------------------------------------------------------
       
   353 //
       
   354 void CPbk2DefaultAttributeProcess::SetNextL()
       
   355     {
       
   356     TVPbkDefaultType attributeType = NextAttribute();
       
   357     if ( attributeType != EVPbkDefaultTypeUndefined )
       
   358         {
       
   359         iAttributeType = attributeType;
       
   360         // Remove previous, if any
       
   361         if ( !DoRemovePreviousL( attributeType ) )
       
   362             {
       
   363             DoSetL( attributeType );
       
   364             }
       
   365         }
       
   366     else
       
   367         {
       
   368         // Finished
       
   369         iObserver.AttributeProcessCompleted();
       
   370         }
       
   371     }
       
   372 
       
   373 // --------------------------------------------------------------------------
       
   374 // CPbk2DefaultAttributeProcess::RemoveNextL
       
   375 // --------------------------------------------------------------------------
       
   376 //
       
   377 void CPbk2DefaultAttributeProcess::RemoveNextL()
       
   378     {
       
   379     TVPbkDefaultType attributeType = NextAttribute();
       
   380     if ( attributeType != EVPbkDefaultTypeUndefined )
       
   381         {
       
   382         if ( !DoRemoveL( attributeType ) )
       
   383             {
       
   384             RemoveNextL();
       
   385             }
       
   386         }
       
   387     else
       
   388         {
       
   389         // Finished
       
   390         iObserver.AttributeProcessCompleted();
       
   391         }
       
   392     }
       
   393 
       
   394 // --------------------------------------------------------------------------
       
   395 // CPbk2DefaultAttributeProcess::NextAttribute
       
   396 // Gets next attribute from the array. The array is processed backwards.
       
   397 // --------------------------------------------------------------------------
       
   398 //
       
   399 inline TVPbkDefaultType CPbk2DefaultAttributeProcess::NextAttribute()
       
   400     {
       
   401     TVPbkDefaultType attribute = EVPbkDefaultTypeUndefined;
       
   402     TInt count = 0;
       
   403     if ( iDefaultAttributes )
       
   404         {
       
   405         count = iDefaultAttributes->Count();
       
   406         }
       
   407 
       
   408     if ( count > 0 )
       
   409         {
       
   410         attribute = iDefaultAttributes->At( count - 1 ); // zero-based
       
   411         iDefaultAttributes->Delete( count -1 ); // zero-based
       
   412         }
       
   413     return attribute;
       
   414     }
       
   415 
       
   416 // End of File