ccservices/cmsservices/cmsengine/Server/src/cmssetvoicecalldefault.cpp
branchRCL_3
changeset 5 81f8547efd4f
child 6 e8e3147d53eb
equal deleted inserted replaced
3:04ab22b956c2 5:81f8547efd4f
       
     1 /*
       
     2 * Copyright (c) 2007-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:  Implementation of auto setting default for voice call
       
    15 *
       
    16 */
       
    17 
       
    18 #include <mvpbkcontactstoreproperties.h>
       
    19 #include <mvpbkcontactstore.h>
       
    20 #include <mvpbkstorecontact.h>
       
    21 
       
    22 #include <cvpbkcontactmanager.h>
       
    23 #include <cvpbkdefaultattribute.h>
       
    24 #include <CVPbkFieldTypeSelector.h>
       
    25 #include <vpbkcontactstoreuris.h>
       
    26 #include <MVPbkFieldType.h>
       
    27 #include <CVPbkContactFieldIterator.h>
       
    28 #include <VPbkFieldTypeSelectorFactory.h>
       
    29 #include <VPbkEng.rsg>
       
    30 
       
    31 #include "cmssetvoicecalldefault.h"
       
    32 
       
    33 // ---------------------------------------------------------------------------
       
    34 // CmsSetVoiceCallDefault::CmsSetVoiceCallDefault
       
    35 // ---------------------------------------------------------------------------
       
    36 //
       
    37 CmsSetVoiceCallDefault::CmsSetVoiceCallDefault()
       
    38     {
       
    39     
       
    40     }
       
    41     
       
    42 // ---------------------------------------------------------------------------
       
    43 // CmsSetVoiceCallDefault::~CmsSetVoiceCallDefault
       
    44 // ---------------------------------------------------------------------------
       
    45 //
       
    46 CmsSetVoiceCallDefault::~CmsSetVoiceCallDefault()
       
    47 	{
       
    48 	delete iSetAttributeOperation;	
       
    49 	iFieldIndexArray.Close();
       
    50 	}
       
    51 
       
    52 // ---------------------------------------------------------------------------
       
    53 // CmsSetVoiceCallDefault::NewL
       
    54 // ---------------------------------------------------------------------------
       
    55 //
       
    56 CmsSetVoiceCallDefault* CmsSetVoiceCallDefault::NewL()
       
    57     {
       
    58     CmsSetVoiceCallDefault* self =
       
    59         new( ELeave ) CmsSetVoiceCallDefault();
       
    60     CleanupStack::PushL( self );
       
    61     self->ConstructL();
       
    62     CleanupStack::Pop( self );
       
    63 
       
    64     return self;
       
    65 	}
       
    66 
       
    67 // ---------------------------------------------------------------------------
       
    68 // CmsSetVoiceCallDefault::ConstructL
       
    69 // ---------------------------------------------------------------------------
       
    70 //
       
    71 void CmsSetVoiceCallDefault::ConstructL()
       
    72     {  
       
    73 
       
    74     }
       
    75 
       
    76 // --------------------------------------------------------------------------
       
    77 // CmsSetVoiceCallDefault::AttributeProcessCompleted
       
    78 // --------------------------------------------------------------------------
       
    79 //
       
    80 void CmsSetVoiceCallDefault::AttributeOperationComplete( MVPbkContactOperationBase& aOperation )
       
    81     {
       
    82     TInt err = KErrNone;
       
    83 
       
    84     if ( &aOperation == iSetAttributeOperation )
       
    85         {
       
    86         TRAP( err, DefaultSettingCompleteL() );
       
    87         }
       
    88             
       
    89     // Handle error
       
    90     if ( err != KErrNone )
       
    91          {
       
    92          ProcessFinished( err );
       
    93          }
       
    94     }
       
    95 
       
    96 // --------------------------------------------------------------------------
       
    97 // CmsSetVoiceCallDefault::AttributeProcessFailed
       
    98 // --------------------------------------------------------------------------
       
    99 //
       
   100 void CmsSetVoiceCallDefault::AttributeOperationFailed(
       
   101         MVPbkContactOperationBase& aOperation,
       
   102         TInt aError )
       
   103     {
       
   104     ProcessFinished( aError );
       
   105     } 
       
   106 
       
   107 // --------------------------------------------------------------------------
       
   108 // CmsSetVoiceCallDefault::ContactOperationCompleted
       
   109 // --------------------------------------------------------------------------
       
   110 //
       
   111 void CmsSetVoiceCallDefault::ContactOperationCompleted
       
   112         (TContactOpResult aResult)
       
   113     {
       
   114     if (aResult.iOpCode == EContactLock)
       
   115         {
       
   116         TRAPD( err, ToSetVoiceCallDefaultL() );
       
   117         if ( err != KErrNone )
       
   118             {
       
   119             ProcessFinished( err );
       
   120             }
       
   121         }
       
   122     else if (aResult.iOpCode == EContactCommit)
       
   123         {      
       
   124         ProcessFinished( KErrNone );
       
   125         }
       
   126     }
       
   127 
       
   128 // --------------------------------------------------------------------------
       
   129 // CmsSetVoiceCallDefault::ContactOperationFailed
       
   130 // --------------------------------------------------------------------------
       
   131 //
       
   132 void CmsSetVoiceCallDefault::ContactOperationFailed
       
   133         ( TContactOp /*aOpCode*/, TInt aErrorCode,
       
   134           TBool /*aErrorNotified*/ )
       
   135     {
       
   136     ProcessFinished( aErrorCode );
       
   137     }
       
   138 
       
   139 // ----------------------------------------------------------
       
   140 // CmsSetVoiceCallDefault::LockContactL
       
   141 // ----------------------------------------------------------
       
   142 //
       
   143 void CmsSetVoiceCallDefault::LockContactL()
       
   144     {
       
   145     // Lock the contact before changing it
       
   146     iStoreContact->LockL(*this);
       
   147     }
       
   148 
       
   149 // --------------------------------------------------------------------------
       
   150 // CmsSetVoiceCallDefault::DoSetDefaultL
       
   151 // --------------------------------------------------------------------------
       
   152 //
       
   153 void CmsSetVoiceCallDefault::DoSetDefaultL( TVPbkDefaultType aDefaultType, TInt aIndex )
       
   154     {
       
   155     CVPbkDefaultAttribute* attr =
       
   156         CVPbkDefaultAttribute::NewL( aDefaultType );
       
   157     CleanupStack::PushL( attr );
       
   158 
       
   159     iSetAttributeOperation =
       
   160     iContactManager->ContactAttributeManagerL().SetFieldAttributeL
       
   161             ( iStoreContact->Fields().FieldAt( aIndex ), *attr, *this );
       
   162 
       
   163     CleanupStack::PopAndDestroy( attr );
       
   164     }
       
   165 
       
   166 // --------------------------------------------------------------------------
       
   167 // CmsSetVoiceCallDefault::SetVoiceCallDefaultL
       
   168 // --------------------------------------------------------------------------
       
   169 //
       
   170 void CmsSetVoiceCallDefault::SetVoiceCallDefaultL( MVPbkStoreContact* aContact, CVPbkContactManager* aContactManager )
       
   171 	{
       
   172 	iFieldIndexArray.Reset();
       
   173     iStoreContact = aContact;
       
   174     iContactManager = aContactManager;
       
   175     
       
   176     if ( iStoreContact && iContactManager && IsSettingDefaultNeededL() )
       
   177     	{
       
   178     	LockContactL();
       
   179     	}
       
   180 	}
       
   181 
       
   182 // --------------------------------------------------------------------------
       
   183 // CmsSetVoiceCallDefault::SetVoiceCallDefaultL
       
   184 // --------------------------------------------------------------------------
       
   185 //
       
   186 void CmsSetVoiceCallDefault::ToSetVoiceCallDefaultL()
       
   187     {
       
   188     
       
   189     TInt count = iFieldIndexArray.Count();
       
   190     
       
   191     if ( count > 0 )
       
   192     	{
       
   193     	TInt index = KErrNotFound;
       
   194     	
       
   195     	for ( TInt i=0; i<ECCACommLauncherCallLast; i++ )
       
   196     		{
       
   197     		TInt aResourceId = GetCallFieldResourceId(i);
       
   198     		if ( FieldIndex( aResourceId, index ) )
       
   199     		    {
       
   200     		    break;
       
   201     		    }
       
   202     		}
       
   203     	
       
   204     	if ( index != KErrNotFound )
       
   205     		{
       
   206     		DoSetDefaultL( EVPbkDefaultTypePhoneNumber, index );
       
   207     		}
       
   208     	else
       
   209     		{
       
   210     		DefaultSettingCompleteL();
       
   211     		}
       
   212     	}
       
   213     else
       
   214     	{
       
   215     	// setting complete
       
   216     	DefaultSettingCompleteL();
       
   217     	}   
       
   218     }
       
   219 
       
   220 // --------------------------------------------------------------------------
       
   221 // CmsSetVoiceCallDefault::IsSettingDefaultNeeded
       
   222 // --------------------------------------------------------------------------
       
   223 //
       
   224 TBool CmsSetVoiceCallDefault::IsSettingDefaultNeededL()
       
   225 	{
       
   226 	// If the contact is not in phone memory, don't set default
       
   227 	if ( !IsPhoneMemoryContact( *iStoreContact ) )
       
   228 		{
       
   229 		return EFalse;
       
   230 		}
       
   231 	
       
   232     CVPbkFieldTypeSelector* selector = VPbkFieldTypeSelectorFactory
       
   233         ::BuildContactActionTypeSelectorL(
       
   234         		VPbkFieldTypeSelectorFactory::EVoiceCallSelector, iContactManager->FieldTypes());    
       
   235     
       
   236     const MVPbkStoreContactFieldCollection& fields = iStoreContact->Fields();
       
   237     TBool result = EFalse;
       
   238     TInt count = fields.FieldCount();
       
   239     
       
   240     // Voice call default attribute
       
   241     CVPbkDefaultAttribute* attr =
       
   242         CVPbkDefaultAttribute::NewL( EVPbkDefaultTypePhoneNumber );
       
   243     CleanupStack::PushL( attr );
       
   244     
       
   245 		for (TInt i = 0; i < count; ++i)
       
   246             {
       
   247             const MVPbkStoreContactField& field = fields.FieldAt(i);       
       
   248             
       
   249             // If this field is voice call field
       
   250             if ( selector->IsFieldIncluded( field ) )
       
   251                 {              
       
   252                 // Checking if the field has been set as voice call default, 
       
   253                 // if hasn't, keep field index in an array
       
   254                 if ( !iContactManager->ContactAttributeManagerL().HasFieldAttributeL
       
   255                         ( *attr, field ) )
       
   256                 	{
       
   257                 	result = ETrue;
       
   258                 	iFieldIndexArray.AppendL(i);
       
   259                 	}
       
   260                 // it has been set as voice call default, no need to set again
       
   261                 else
       
   262                 	{
       
   263                 	result = EFalse;
       
   264                 	iFieldIndexArray.Reset();
       
   265                 	break;
       
   266                 	}
       
   267                 }
       
   268             }
       
   269 		
       
   270 	CleanupStack::PopAndDestroy( attr );
       
   271     delete selector;
       
   272     return result;
       
   273 	}
       
   274 
       
   275 // ---------------------------------------------------------------------------
       
   276 // CmsSetVoiceCallDefault::FieldIndex
       
   277 // ---------------------------------------------------------------------------
       
   278 //
       
   279 TBool CmsSetVoiceCallDefault::FieldIndex( TInt aResourceId, TInt& aIndex )
       
   280     {
       
   281 	TBool result = EFalse;
       
   282 	
       
   283 	if( iStoreContact )
       
   284 		{
       
   285     	const MVPbkBaseContactFieldCollection& fields = iStoreContact->Fields();	
       
   286     	TInt count = iFieldIndexArray.Count();
       
   287         
       
   288         for (TInt i = 0; i < count; ++i)
       
   289             {
       
   290             const MVPbkFieldType* fieldType = 
       
   291                 fields.FieldAt(iFieldIndexArray[i]).BestMatchingFieldType();          
       
   292             
       
   293             if ( fieldType && fieldType->FieldTypeResId() == aResourceId )
       
   294                 {
       
   295                 result = ETrue;
       
   296                 aIndex = iFieldIndexArray[i];
       
   297                 break;
       
   298                 }
       
   299             }		
       
   300 		}
       
   301 	return result;
       
   302     }
       
   303 
       
   304 // ---------------------------------------------------------------------------
       
   305 // CmsSetVoiceCallDefault::NextAttribute
       
   306 // ---------------------------------------------------------------------------
       
   307 //
       
   308 TInt CmsSetVoiceCallDefault::GetCallFieldResourceId( TInt aPriority )
       
   309     {
       
   310     
       
   311 	TInt resourceId;
       
   312 	switch ( aPriority )
       
   313         {
       
   314 		case ECCACommLauncherCallMobile:
       
   315 			resourceId = R_VPBK_FIELD_TYPE_MOBILEPHONEGEN;
       
   316 			break;
       
   317 		case ECCACommLauncherCallMobileWork:
       
   318 			resourceId = R_VPBK_FIELD_TYPE_MOBILEPHONEWORK;
       
   319 			break;
       
   320 		case ECCACommLauncherCallMobileHome:
       
   321 			resourceId = R_VPBK_FIELD_TYPE_MOBILEPHONEHOME;
       
   322 			break;
       
   323 		case ECCACommLauncherCallTelephone:
       
   324 			resourceId = R_VPBK_FIELD_TYPE_LANDPHONEGEN;
       
   325 			break;
       
   326 		case ECCACommLauncherCallTelephoneWork:
       
   327 			resourceId = R_VPBK_FIELD_TYPE_LANDPHONEWORK;
       
   328 			break;
       
   329 		case ECCACommLauncherCallTelephoneHome:
       
   330 			resourceId = R_VPBK_FIELD_TYPE_LANDPHONEHOME;
       
   331 			break;
       
   332 		case ECCACommLauncherCallCarPhone:
       
   333 			resourceId = R_VPBK_FIELD_TYPE_CARPHONE;
       
   334 			break;
       
   335 		case ECCACommLauncherCallAssistant:
       
   336 			resourceId = R_VPBK_FIELD_TYPE_ASSTPHONE;
       
   337 			break;
       
   338 		default:
       
   339 			break;
       
   340         }
       
   341 	return resourceId;
       
   342     }
       
   343 
       
   344 // ---------------------------------------------------------------------------
       
   345 // CmsSetVoiceCallDefault::DefaultSettingCompleteL
       
   346 // ---------------------------------------------------------------------------
       
   347 //
       
   348 void CmsSetVoiceCallDefault::DefaultSettingCompleteL()
       
   349 	{
       
   350 	// commit the contact
       
   351 	iStoreContact->CommitL( *this );
       
   352 	}
       
   353 
       
   354 // --------------------------------------------------------------------------
       
   355 // CmsSetVoiceCallDefault::ProcessFinished
       
   356 // --------------------------------------------------------------------------
       
   357 //
       
   358 void CmsSetVoiceCallDefault::ProcessFinished( TInt aError )
       
   359     {       
       
   360     }
       
   361 
       
   362 // ---------------------------------------------------------------------------
       
   363 // CmsSetVoiceCallDefault::HandleError
       
   364 // ---------------------------------------------------------------------------
       
   365 //
       
   366 void CmsSetVoiceCallDefault::HandleError( TInt aError )
       
   367     {
       
   368     }
       
   369 
       
   370 // --------------------------------------------------------------------------
       
   371 // CmsSetVoiceCallDefault::IsPhoneMemoryContact
       
   372 // --------------------------------------------------------------------------
       
   373 //
       
   374 TBool CmsSetVoiceCallDefault::IsPhoneMemoryContact(
       
   375         const MVPbkStoreContact& aContact) const
       
   376     {
       
   377     TBool ret = EFalse;
       
   378 
       
   379     TVPbkContactStoreUriPtr uri =
       
   380         aContact.ParentStore().StoreProperties().Uri();
       
   381 
       
   382     TVPbkContactStoreUriPtr phoneMemoryUri
       
   383         ( VPbkContactStoreUris::DefaultCntDbUri() );
       
   384 
       
   385     if (uri.Compare( phoneMemoryUri,
       
   386         TVPbkContactStoreUriPtr::EContactStoreUriStoreType ) == 0)
       
   387         {
       
   388         ret = ETrue;
       
   389         }
       
   390 
       
   391     return ret;
       
   392     }