phonebookengines_old/contactsmodel/cntmodel/src/cviewiterator.cpp
changeset 40 b46a585f6909
equal deleted inserted replaced
37:fd64c38c277d 40:b46a585f6909
       
     1 // Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 /**
       
    17  @file
       
    18  @internalComponent
       
    19  @released
       
    20 */
       
    21 
       
    22 
       
    23 #include "cviewiterator.h"
       
    24 
       
    25 
       
    26 /**
       
    27 Constructs CViewIterator object and initialises all its members.
       
    28 
       
    29 @param aManager MLplViewIteratorManager interface implemented by the Persistence
       
    30 Layer.
       
    31 @param aTextDef CContactTextDef object describing the set of fields which will
       
    32 be added to each CViewContact object during iteration.
       
    33 @param aViewPreferences Iteration preferences.
       
    34 */
       
    35 CViewIterator::CViewIterator(MLplViewIteratorManager& aManager,const CContactTextDef& aTextDef,TContactViewPreferences aViewPreferences)
       
    36 	:
       
    37 	iManager(aManager),
       
    38 	iViewPreferences(aViewPreferences),
       
    39 	iTextDef(aTextDef)
       
    40 	{
       
    41 	iTableMask = TableMask(aTextDef);	
       
    42 	}
       
    43 
       
    44 
       
    45 /**
       
    46 CViewIterator destructor.
       
    47 */
       
    48 CViewIterator::~CViewIterator()
       
    49 	{
       
    50 	}
       
    51 
       
    52 
       
    53 /**
       
    54 Move the view iterator to the first view contact.
       
    55 */
       
    56 void CViewIterator::GoFirstL()
       
    57 	{
       
    58 	iCurrentItemId = -1;
       
    59 	}
       
    60 
       
    61 
       
    62 /**
       
    63 Move the view iterator to the next view contact and return the view contact
       
    64 at the new position.
       
    65 
       
    66 @return View contact at the next position in the view.  The return value will be
       
    67 NULL if no view contact can be obtained.
       
    68 */
       
    69 CViewContact* CViewIterator::NextItemL()
       
    70 	{
       
    71 	// Parcel up parameters in 'T' class.
       
    72 	TItemAtLParams params;
       
    73 	params.iGreaterEqual = ETrue;
       
    74 	params.iIndex = ++iCurrentItemId;
       
    75 	params.iMask = iTableMask;
       
    76 	params.iViewPrefs = iViewPreferences;
       
    77 	
       
    78 	// Could be calling proxy if used on client side.
       
    79 	CViewContact *viewContact = iManager.ItemAtL(params,iTextDef, KIgnoreSessionId);
       
    80 
       
    81 	if (viewContact)
       
    82 		{
       
    83 		iCurrentItemId = viewContact->Id();
       
    84 		}
       
    85 
       
    86 	return viewContact;
       
    87 	}
       
    88 
       
    89 
       
    90 /** 
       
    91 Get the view contact with the given Contact ID.
       
    92 
       
    93 @return View contact with the given Contact ID.
       
    94 */
       
    95 CViewContact* CViewIterator::ItemAtL(TInt aCntItemId)
       
    96 	{
       
    97 	iCurrentItemId = aCntItemId;
       
    98 	
       
    99 	// Parcel up parameters in 'T' class.
       
   100 	TItemAtLParams params;
       
   101 	params.iGreaterEqual = EFalse;
       
   102 	params.iIndex = iCurrentItemId;
       
   103 	params.iMask = iTableMask;
       
   104 	params.iViewPrefs = iViewPreferences;
       
   105 
       
   106 	return iManager.ItemAtL(params, iTextDef, KIgnoreSessionId);
       
   107 	}
       
   108 
       
   109 
       
   110 /**
       
   111 Get a table mask using the given text definition.
       
   112 
       
   113 @param aTextDef Text definition from which to create the table mask.
       
   114 
       
   115 @return MLplViewIteratorManager::EIdentityTableOnly if the text definition only
       
   116 requires access to the Identity table, MLplViewIteratorManager::EAllTables
       
   117 otherwise.
       
   118 */
       
   119 MLplViewIteratorManager::TTableMask CViewIterator::TableMask(const CContactTextDef& aTextDef)
       
   120 	{
       
   121 	TInt columns=0;
       
   122 	TInt sortDefFlags=0;
       
   123 	// Find out whether we can do a fast sort using the Identity table by
       
   124 	// looking at the text definition.
       
   125 	CViewIterator::ConstructBitwiseFlagsFromTextDef(sortDefFlags,columns,&aTextDef);
       
   126 	if( UsesIdentityTableOnly(sortDefFlags) )
       
   127 		{
       
   128 		return MLplViewIteratorManager::EIdentityTableOnly;
       
   129 		}
       
   130 	else
       
   131 		{
       
   132 		return MLplViewIteratorManager::EAllTables;
       
   133 		}
       
   134 	}
       
   135 
       
   136 
       
   137 /**
       
   138 Update a set of find flags and an Identity table column count from the field
       
   139 type UIDs in the given field definition.
       
   140 
       
   141 @param aFindFlags Updated with those flags which map to the field type UIDs in
       
   142 aFieldDef.
       
   143 @param aIdentityColumnsCount Updated with the number of field type UIDs in
       
   144 aFieldDef which map to columns in the Identity table.
       
   145 @param aFieldDef Field definition from which to create set of find flags and
       
   146 Identity table column count.
       
   147 */
       
   148 void CViewIterator::ConstructBitwiseFindFlags(TInt& aFindFlags,TInt& aIdentityColumnsCount,const CContactItemFieldDef* aFieldDef)
       
   149 	{
       
   150 	if(aFieldDef!=NULL && aFieldDef->Count()>0)
       
   151 		{
       
   152 		for(TInt ii=0;ii<aFieldDef->Count();ii++)
       
   153 			{
       
   154 			SetFindFlagsAndColumnsCount(aFieldDef->At(ii).iUid,aFindFlags,aIdentityColumnsCount);
       
   155 			}
       
   156 		}
       
   157 	else
       
   158 		{
       
   159 		aFindFlags|=EFindInAllFields|EFindInAnyIdentityField;
       
   160 		}
       
   161 	}
       
   162 
       
   163 
       
   164 /**
       
   165 Update a set of find flags and an Identity table column count from the field
       
   166 type UIDs in the given text definition.  This can be used to tell the find
       
   167 method what tables need to be searched for a given text definition.  If the text
       
   168 definition is NULL we search in all tables.
       
   169 
       
   170 @param aFindFlags Updated with those flags which map to the field type UIDs in
       
   171 aTextDef.
       
   172 @param aIdentityColumnsCount Updated with the number of field type UIDs in
       
   173 aTextDef which map to columns in the Identity table.
       
   174 @param aTextDef Text definition from which to create set of find flags and
       
   175 Identity table column count.
       
   176 */
       
   177 void CViewIterator::ConstructBitwiseFlagsFromTextDef(TInt& aFindFlags,TInt& aIdentityColumnsCount,const CContactTextDef* aTextDef)
       
   178 	{
       
   179 	if(aTextDef!=NULL && aTextDef->Count()>0)
       
   180 		{
       
   181 		for(TInt ii=0;ii<aTextDef->Count();ii++)
       
   182 			{
       
   183 			SetFindFlagsAndColumnsCount(aTextDef->At(ii).iFieldType.iUid,aFindFlags,aIdentityColumnsCount);
       
   184 			}
       
   185 		TFieldType fallback = aTextDef->FallbackField();
       
   186 		if (fallback!=KUidContactFieldNone)
       
   187 			{
       
   188 			SetFindFlagsAndColumnsCount(fallback.iUid,aFindFlags,aIdentityColumnsCount);
       
   189 			}
       
   190 		}
       
   191 	else
       
   192 		{
       
   193 		aFindFlags|=EFindInAllFields|EFindInAnyIdentityField;
       
   194 		}
       
   195 	}
       
   196 
       
   197 
       
   198 /**
       
   199 For the given field type UID, increase aIdentityColumnsCount if the field maps
       
   200 to one of the columns in the Identity table and add the relevant find flag.
       
   201 
       
   202 @param aUid Field type UID.
       
   203 @param aFindFlags Updated with find flag which maps to the given field type UID.
       
   204 @param aIdentityColumnsCount Incremented if the field type UID maps to a column
       
   205 in the Identity table.
       
   206 */
       
   207 void CViewIterator::SetFindFlagsAndColumnsCount(TInt32 aUid,TInt& aFindFlags,TInt& aIdentityColumnsCount)
       
   208 	{
       
   209 	switch(aUid)
       
   210 		{
       
   211 		case KUidContactFieldGivenNameValue:
       
   212 			{
       
   213 			aIdentityColumnsCount++;
       
   214 			aFindFlags|=EFindFirstName;
       
   215 			break;
       
   216 			}
       
   217 		case KUidContactFieldFamilyNameValue:
       
   218 			{
       
   219 			aIdentityColumnsCount++;
       
   220 			aFindFlags|=EFindLastName;
       
   221 			break;
       
   222 			}
       
   223 		case KUidContactFieldCompanyNameValue:
       
   224 			{
       
   225 			aIdentityColumnsCount++;
       
   226 			aFindFlags|=EFindCompanyName;
       
   227 			break;
       
   228 			}
       
   229 		case KUidContactFieldGivenNamePronunciationValue:
       
   230 			{
       
   231 			aIdentityColumnsCount++;
       
   232 			aFindFlags|=EFindFirstNamePronunciation;
       
   233 			break;
       
   234 			}
       
   235 		case KUidContactFieldFamilyNamePronunciationValue:
       
   236 			{
       
   237 			aIdentityColumnsCount++;
       
   238 			aFindFlags|=EFindLastNamePronunciation;
       
   239 			break;
       
   240 			}
       
   241 		case KUidContactFieldCompanyNamePronunciationValue:
       
   242 			{
       
   243 			aIdentityColumnsCount++;
       
   244 			aFindFlags|=EFindCompanyNamePronunciation;
       
   245 			break;
       
   246 			}
       
   247 		case KUidContactFieldEMailValue:
       
   248 			{
       
   249 			aFindFlags|=EFindInEmailTableOnly;
       
   250 			break;
       
   251 			}
       
   252 		case KUidContactFieldMatchAllValue:
       
   253 			{	
       
   254 			aFindFlags|=EFindInAllFields|EFindInAnyIdentityField;
       
   255 			break;
       
   256 			}
       
   257 		default:
       
   258 			aFindFlags|=EFindInAllFields;
       
   259 		}
       
   260 	}
       
   261 
       
   262 
       
   263 /**
       
   264 Determine if the Identity table requires to be searched for the given find
       
   265 flags.
       
   266 
       
   267 @param aFindFlags Set of find flags describing which fields will be searched.
       
   268 
       
   269 @return ETrue If one of the flags in aFindFlags maps to a column in the Identity
       
   270 table, EFalse otherwise.
       
   271 */
       
   272 TBool CViewIterator::SearchIdentityTableRequired(TInt aFindFlags)
       
   273 	{
       
   274 	return aFindFlags & 
       
   275 		( EFindInAnyIdentityField | EFindFirstName | EFindLastName | EFindCompanyName |
       
   276 			EFindFirstNamePronunciation | EFindLastNamePronunciation | EFindCompanyNamePronunciation);
       
   277 	}
       
   278 
       
   279 
       
   280 /**
       
   281 Determine if only the Identity table requires to be searched for the given find
       
   282 flags.
       
   283 
       
   284 @param aFindFlags Set of find flags describing which fields will be searched.
       
   285 
       
   286 @return ETrue If one of the flags in aFindFlags maps to a column in the Identity
       
   287 table and no columns in any other table, EFalse otherwise.
       
   288 */
       
   289 TBool CViewIterator::UsesIdentityTableOnly(TInt aFindFlags)
       
   290 	{
       
   291 	return (aFindFlags &  (EFindFirstName | EFindLastName | EFindCompanyName |
       
   292 			EFindFirstNamePronunciation |EFindLastNamePronunciation |EFindCompanyNamePronunciation) ) &&
       
   293 		! (aFindFlags & (EFindInAllFields | EFindInEmailTableOnly) );
       
   294 	}