phonebookengines/contactsmodel/src/cntitemdef.cpp
changeset 0 e686773b3f54
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     1 // Copyright (c) 2004-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 #include <cntfield.h>
       
    17 #include <cntitem.h>
       
    18 #include "CNTSTD.H"
       
    19 
       
    20 //
       
    21 // TContactTextDefItem
       
    22 //
       
    23 
       
    24 EXPORT_C TContactTextDefItem::TContactTextDefItem() : iFieldType(KNullUid)
       
    25 /** Default constructor; initialises the field type to KNullUid. */
       
    26 	{}
       
    27 
       
    28 EXPORT_C TContactTextDefItem::TContactTextDefItem(TFieldType aFieldType) : iFieldType(aFieldType)
       
    29 /** Constructs the text definition item with a field type. 
       
    30 
       
    31 @param aFieldType The field type. */	
       
    32 	{}
       
    33 
       
    34 EXPORT_C TContactTextDefItem::TContactTextDefItem(TFieldType aFieldType, const TDesC &aSeperator) : iFieldType(aFieldType)
       
    35 /** C++ constructor with a field type and a separator string. 
       
    36 
       
    37 @param aFieldType The field type.
       
    38 @param aSeperator The string used to separate fields in the text definition. */
       
    39 	{
       
    40 	iSeperator.Copy(aSeperator);
       
    41 	}
       
    42 
       
    43 //
       
    44 // CContactTextDef
       
    45 //
       
    46 
       
    47 CContactTextDef::CContactTextDef()
       
    48 	: CArrayFixFlat<TContactTextDefItem>(5),
       
    49 	iFallbackFieldType(KUidContactFieldNone),
       
    50 	iExactMatchOnly(EFalse)
       
    51 	{}
       
    52 
       
    53 EXPORT_C CContactTextDef* CContactTextDef::NewL()
       
    54 /** Allocates and constructs a new text definition. The fallback field type is 
       
    55 initialised to KUidContactFieldNone.
       
    56 
       
    57 @return Pointer to the newly created text definition. */
       
    58 	{
       
    59 	return new(ELeave) CContactTextDef;
       
    60 	}
       
    61 
       
    62 EXPORT_C CContactTextDef* CContactTextDef::NewLC()
       
    63 /** Allocates and constructs a new text definition. If the text definition is successfully 
       
    64 constructed, it is left on the cleanup stack. The fallback field type is initialised 
       
    65 to KUidContactFieldNone.
       
    66 
       
    67 @return Pointer to the newly created text definition. */
       
    68 	{
       
    69 	CContactTextDef* textDef=CContactTextDef::NewL();
       
    70 	CleanupStack::PushL(textDef);
       
    71 	return textDef;
       
    72 	}
       
    73 
       
    74 /** Allocates and constructs a new text definition based on a RReadStream. If the text definition is successfully 
       
    75 constructed, it is left on the cleanup stack. The fallback field type is initialised 
       
    76 to KUidContactFieldNone.
       
    77 
       
    78 @param aStream RReadStream containing object to internalize.
       
    79 @return Pointer to the newly created text definition. 
       
    80 @internalTechnology 
       
    81 */	
       
    82 CContactTextDef* CContactTextDef::NewLC(RReadStream& aStream)
       
    83 	{
       
    84 	CContactTextDef* textDef=CContactTextDef::NewLC();
       
    85 	textDef->InternalizeL(aStream);	
       
    86 	return textDef;
       
    87 	}	
       
    88 
       
    89 EXPORT_C void CContactTextDef::SetFallbackField(TFieldType aFieldType)
       
    90 /** Sets the fallback field type.
       
    91 
       
    92 @param aFieldType The fallback field type. */
       
    93 	{
       
    94 	iFallbackFieldType=aFieldType;
       
    95 	}
       
    96 
       
    97 EXPORT_C TFieldType CContactTextDef::FallbackField() const
       
    98 /** Gets the fallback field type.
       
    99 
       
   100 @return The fallback field type. */
       
   101 	{
       
   102 	return(iFallbackFieldType);
       
   103 	}
       
   104 
       
   105 EXPORT_C TBool CContactTextDef::ExactMatchOnly()
       
   106 /** Gets the exact match for contact fields
       
   107 @return ETrue if it gets the exact match   */
       
   108 	{
       
   109 	return iExactMatchOnly;
       
   110 	}
       
   111 
       
   112 EXPORT_C void CContactTextDef::SetExactMatchOnly(TBool aExactMatchOnly)
       
   113 /** Sets the exact match for contact fields
       
   114 @param aExactMatchOnly Contains the value for exact match. It takes ETrue when an exact match is required, EFalse otherwise.   */
       
   115 	{
       
   116 	iExactMatchOnly=aExactMatchOnly;
       
   117 	}
       
   118 
       
   119 void CContactTextDef::InternalizeL(RReadStream& aStream)
       
   120 /** Internalises a CContactItem object from a read stream. 
       
   121 @param aStream Stream from which the object should be internalised. */
       
   122 	{
       
   123 	this->Reset();
       
   124 	TInt count = aStream.ReadInt32L();
       
   125 
       
   126 	TInt separatorLength = 0;
       
   127 	for (TInt i=0; i<count; ++i)
       
   128 		{
       
   129 		TContactTextDefItem textDefItem;
       
   130 		textDefItem.iFieldType.iUid = aStream.ReadInt32L();
       
   131 		separatorLength = aStream.ReadInt32L();
       
   132 		if(separatorLength)
       
   133 			{
       
   134 			aStream>>textDefItem.iSeperator;
       
   135 			}
       
   136 
       
   137 		this->AppendL(textDefItem);
       
   138 		}
       
   139 	
       
   140 	TFieldType fieldType;
       
   141 	fieldType.iUid = aStream.ReadInt32L();
       
   142 	SetFallbackField(fieldType);
       
   143 	SetExactMatchOnly(aStream.ReadInt32L());
       
   144 		
       
   145 	}
       
   146 
       
   147 void CContactTextDef::ExternalizeL(RWriteStream& aStream) const 
       
   148 /** Externalises a CContactItem object to a write stream.
       
   149 @param aStream Stream to which the object should be externalised. */
       
   150 	{
       
   151 	TInt count = this->Count();
       
   152 	aStream.WriteInt32L(count);	
       
   153 	
       
   154 	TInt separatorLength = 0;
       
   155 	for(TInt i=0; i<count;++i)
       
   156 		{
       
   157 		aStream.WriteInt32L(this->At(i).iFieldType.iUid);
       
   158 		separatorLength = this->At(i).iSeperator.Length();
       
   159 		aStream.WriteInt32L(separatorLength);
       
   160 		if(separatorLength)
       
   161 			{
       
   162 			aStream << this->At(i).iSeperator;
       
   163 			}			
       
   164 		}
       
   165 	aStream.WriteInt32L(this->FallbackField().iUid);	
       
   166 	aStream.WriteInt32L(const_cast<CContactTextDef*>(this)->ExactMatchOnly());
       
   167 		
       
   168 	}
       
   169 
       
   170 EXPORT_C CContactItemViewDef* CContactItemViewDef::NewL(TUse aUse, TMode aMode)
       
   171 /** Allocates and constructs a new CContactItemViewDef, with a use and a mode.
       
   172 
       
   173 @param aUse Specifies whether to include or exclude specified fields.
       
   174 @param aMode Specifies whether to include or exclude hidden fields.
       
   175 @return Pointer to the newly created view definition. */
       
   176 	{ // static
       
   177 	return new(ELeave) CContactItemViewDef(aUse, aMode);
       
   178 	}
       
   179 
       
   180 EXPORT_C CContactItemViewDef* CContactItemViewDef::NewLC(TUse aUse, TMode aMode)
       
   181 /** Allocates and constructs a new CContactItemViewDef, specifying a use and a 
       
   182 mode, leaving the object on the cleanup stack.
       
   183 
       
   184 @param aUse Specifies whether to include or exclude specified fields.
       
   185 @param aMode Specifies whether to include or exclude hidden fields.
       
   186 @return Pointer to the newly created view definition. */
       
   187 	{ // static
       
   188 	CContactItemViewDef* viewDef=CContactItemViewDef::NewL(aUse, aMode);
       
   189 	CleanupStack::PushL(viewDef);
       
   190 	return viewDef;
       
   191 	}
       
   192 
       
   193 /** Allocates and constructs a new CContactItemViewDef based on a RReadStream, 
       
   194 specifying a use and a mode, leaving the object on the cleanup stack.
       
   195 
       
   196 @param aStream RReadStream containing object to internalize.
       
   197 @return Pointer to the newly created view definition. 
       
   198 @internalTechnology
       
   199 */
       
   200 CContactItemViewDef* CContactItemViewDef::NewLC(RReadStream& aStream)
       
   201 	{
       
   202 	CContactItemViewDef::TUse aUse = (CContactItemViewDef::TUse)aStream.ReadInt32L();
       
   203 	CContactItemViewDef::TMode aMode = (CContactItemViewDef::TMode)aStream.ReadInt32L();	
       
   204 	CContactItemViewDef* viewDef=CContactItemViewDef::NewLC(aUse, aMode);
       
   205 	viewDef->InternalizeL(aStream);
       
   206 	
       
   207 	return viewDef;
       
   208 	}	
       
   209 
       
   210 CContactItemViewDef::CContactItemViewDef(TUse aUse, TMode aMode)
       
   211 	: iFieldTypes(5),iUse(aUse),iMode(aMode)
       
   212 	{}
       
   213 
       
   214 EXPORT_C TInt CContactItemViewDef::Find(TFieldType aFieldType) const
       
   215 /** Searches the view definition for the specified field type.
       
   216 
       
   217 @param aFieldType The field type to search for.
       
   218 @return The index in the view definition of the matching field type, or KErrNotFound. */
       
   219 	{
       
   220 	const TInt count=Count();
       
   221 	for (TInt ii=0;ii<count;ii++)
       
   222 		{
       
   223 		TUid fieldType=iFieldTypes[ii];
       
   224 		if (fieldType==KUidContactFieldMatchAll || aFieldType==KUidContactFieldMatchAll || fieldType==aFieldType)
       
   225 			return ii;
       
   226 		}
       
   227 	return KErrNotFound;
       
   228 	}
       
   229 
       
   230 EXPORT_C TInt CContactItemViewDef::Find(const CContentType &aContentType) const
       
   231 /** Searches the view definition for any field type contained in the specified 
       
   232 content type.
       
   233 
       
   234 @param aContentType Content type containing the field types to search for.
       
   235 @return The index in the view definition of the first matching field type or 
       
   236 KErrNotFound. */
       
   237 	{
       
   238 	TInt ret=KErrNotFound;
       
   239 	for(TInt loop=0;loop<aContentType.FieldTypeCount();loop++)
       
   240 		{
       
   241 		ret=Find(aContentType.FieldType(loop));
       
   242 		if (ret!=KErrNotFound)
       
   243 			break;
       
   244 		}
       
   245 	return(ret);
       
   246 	}
       
   247 
       
   248 EXPORT_C TBool CContactItemViewDef::MatchesAll() const
       
   249 /** Tests whether the view definition contains a field type with the value KUidContactFieldMatchAll. 
       
   250 
       
   251 If this is the case, all fields in the contact item are retrieved, regardless 
       
   252 of the other field types specified in the view definition.
       
   253 
       
   254 @return ETrue if all field types are matched, EFalse if not. */
       
   255 	{
       
   256 	const TInt count=Count();
       
   257 	for (TInt ii=0;ii<count;ii++)
       
   258 		{
       
   259 		TUid fieldType=iFieldTypes[ii];
       
   260 		if (fieldType==KUidContactFieldMatchAll)
       
   261 			return(ETrue);
       
   262 		}
       
   263 	return(EFalse);
       
   264 	}
       
   265 
       
   266 EXPORT_C void CContactItemViewDef::AddL(TFieldType aFieldType)
       
   267 /** Appends a field type to the view definition's array of field types.
       
   268 
       
   269 @param aFieldType The field type to append to the array of field types. */
       
   270 	{
       
   271 	__ASSERT_DEBUG(Find(aFieldType)<=KErrNotFound,Panic(ECntPanicDuplicateViewFields));
       
   272 	iFieldTypes.AppendL(aFieldType);
       
   273 	}
       
   274 
       
   275 EXPORT_C void CContactItemViewDef::Remove(TFieldType aFieldType)
       
   276 /** Removes a field type, identified by its UID.
       
   277 
       
   278 @param aFieldType The field type to remove from the list. This must be 
       
   279 present in the CContactItemViewDef object, or the function raises a panic. */
       
   280 	{
       
   281 	const TInt index=Find(aFieldType);
       
   282 	__ASSERT_ALWAYS(index!=KErrNotFound,Panic(ECntPanicFieldDoesNotExist));
       
   283 	iFieldTypes.Delete(index);
       
   284 	}
       
   285 
       
   286 EXPORT_C void CContactItemViewDef::Remove(TInt aIndex)
       
   287 /** Removes a field type, identified by its index into the array of field types.
       
   288 
       
   289 @param aIndex The index of the element to delete. The position is relative 
       
   290 to zero. This value must not be negative and must not be greater than the 
       
   291 number of elements currently in the list, or the function raises a panic. */
       
   292 	{ // inline this ?? !!!
       
   293 	iFieldTypes.Delete(aIndex);
       
   294 	}
       
   295 
       
   296 EXPORT_C void CContactItemViewDef::InternalizeL(RReadStream& aStream)
       
   297 /** Internalises the item view definition from a read stream. The presence of this 
       
   298 function means that the standard templated operator>>() (defined in s32strm.h) 
       
   299 is available to internalise objects of this class.
       
   300 
       
   301 @param aStream Stream from which the item view definition should be internalised. */
       
   302 	{
       
   303 	iUse=STATIC_CAST(CContactItemViewDef::TUse,aStream.ReadInt32L());
       
   304 	TInt count;
       
   305 	count=aStream.ReadInt32L();
       
   306 	Reset();
       
   307 	for(TInt ii=0;ii<count;ii++)
       
   308 		{
       
   309 		TUid fieldType;
       
   310 		aStream>>fieldType;
       
   311 		iFieldTypes.AppendL(fieldType);
       
   312 		}
       
   313 	}
       
   314 
       
   315 EXPORT_C void CContactItemViewDef::ExternalizeL(RWriteStream& aStream) const
       
   316 /** Externalises the item view definition to a write stream. The presence of this 
       
   317 function means that the standard templated operator<<() (defined in s32strm.h) 
       
   318 is available to externalise objects of this class.
       
   319 
       
   320 @param aStream Stream to which the item view definition should be externalised. */
       
   321 	{
       
   322 	aStream.WriteInt32L(iUse);
       
   323 	TInt count=Count();
       
   324 	aStream.WriteInt32L(count);
       
   325 	for(TInt ii=0;ii<count;ii++)
       
   326 		aStream << iFieldTypes[ii];
       
   327 	}
       
   328 
       
   329 EXPORT_C TBool CContactItemViewDef::Contains(const CContactItem& aItem)
       
   330 /** Tests whether a contact item complies with the view definition.
       
   331 
       
   332 This indicates whether or not the item will be included in the view. 
       
   333 
       
   334 @param aItem The contact item to test.
       
   335 @return ETrue if any of the item's fields are contained in the view definition 
       
   336 (so that the item will be included in the view), EFalse if none of the item's 
       
   337 fields are contained in the view definition (so that the item will not be 
       
   338 included in the view). */
       
   339 	{
       
   340 	CContactItemFieldSet& fieldSet=aItem.CardFields();
       
   341 	const TInt count=fieldSet.Count();
       
   342 	for (TInt ii=0;ii<count;ii++)
       
   343 		if (Find(iFieldTypes[ii])!=KErrNotFound)
       
   344 			return ETrue;
       
   345 	return EFalse;
       
   346 	}
       
   347 
       
   348 //
       
   349 // class CContactViewDef
       
   350 //
       
   351 
       
   352 EXPORT_C CContactViewDef* CContactViewDef::NewL()
       
   353 /**  Allocates and constructs a view definition.
       
   354 
       
   355 @return Pointer to the new view definition. */
       
   356 	{
       
   357 	CContactViewDef* viewDef=CContactViewDef::NewLC();
       
   358 	CleanupStack::Pop(viewDef);
       
   359 	return viewDef;
       
   360 	}
       
   361 
       
   362 EXPORT_C CContactViewDef* CContactViewDef::NewLC()
       
   363 /** Allocates and constructs a view definition, leaving it on the cleanup stack.
       
   364 
       
   365 @return Pointer to the new view definition. */
       
   366 	{
       
   367 	CContactViewDef* viewDef=new(ELeave) CContactViewDef();
       
   368 	CleanupStack::PushL(viewDef);
       
   369 	return(viewDef);
       
   370 	}
       
   371 
       
   372 EXPORT_C CContactViewDef* CContactViewDef::NewL(CContactItemViewDef* aItemDef)
       
   373 /** Allocates and constructs a view definition. 
       
   374 
       
   375 @param aItemDef Pointer to the item view definition. This contains the view definition's 
       
   376 field types, use and mode. The view definition takes ownership of this.
       
   377 @return Pointer to the new view definition. */
       
   378 	{ // static
       
   379 	CContactViewDef* viewDef=CContactViewDef::NewLC(aItemDef);
       
   380 	CleanupStack::Pop(viewDef);
       
   381 	return viewDef;
       
   382 	}
       
   383 
       
   384 EXPORT_C CContactViewDef* CContactViewDef::NewLC(CContactItemViewDef* aItemDef)
       
   385 /** Allocates and constructs a view definition. 
       
   386 
       
   387 @param aItemDef Pointer to the item view definition. This contains the view definition's 
       
   388 field types, use and mode. The view definition takes ownership of this.
       
   389 @return Pointer to the new view definition. This is left on the cleanup 
       
   390 stack.*/
       
   391 	{ // static
       
   392 	CContactViewDef* viewDef=new(ELeave) CContactViewDef();
       
   393 	CleanupStack::PushL(viewDef);
       
   394 	viewDef->ConstructL(aItemDef);
       
   395 	return viewDef;
       
   396 	}
       
   397 	
       
   398 /** Allocates and constructs a view definition based on a RReadStream.
       
   399 
       
   400 @param aStream RReadStream containing object to internalize.
       
   401 @return Pointer to the new view definition. This is left on the cleanup stack. 
       
   402 @internalTechnology 
       
   403 */
       
   404 CContactViewDef* CContactViewDef::NewLC(RReadStream& aStream)
       
   405 	{
       
   406 	CContactViewDef* viewDef=CContactViewDef::NewLC();
       
   407 	CContactItemViewDef* itemDef=CContactItemViewDef::NewLC(aStream);	
       
   408 	viewDef->ConstructL(itemDef);
       
   409 	CleanupStack::Pop(itemDef);
       
   410 	return viewDef;
       
   411 	}		
       
   412 
       
   413 EXPORT_C CContactViewDef::~CContactViewDef()
       
   414 /** Destroys the view definition object and deletes its owned item view definition. */
       
   415 	{
       
   416 	delete iItemDef;
       
   417 	}
       
   418 
       
   419 EXPORT_C void CContactViewDef::InternalizeL(RReadStream& aStream)
       
   420 /** Internalises the object's item view definition from a read stream.
       
   421 
       
   422 @param aStream Stream from which the object should be internalised. */
       
   423 	{
       
   424 	CContactItemViewDef* itemDef=CContactItemViewDef::NewLC(CContactItemViewDef::EIncludeFields,CContactItemViewDef::EIncludeHiddenFields);
       
   425 	itemDef->InternalizeL(aStream);
       
   426 	CleanupStack::Pop(itemDef);
       
   427 	delete iItemDef;
       
   428 	iItemDef=itemDef;
       
   429 	}
       
   430 
       
   431 EXPORT_C void CContactViewDef::ExternalizeL(RWriteStream& aStream) const
       
   432 /** Externalises the object's item view definition to a write stream.
       
   433 
       
   434 @param aStream Stream to which the object should be externalised. */
       
   435 	{
       
   436 	iItemDef->ExternalizeL(aStream);
       
   437 	}
       
   438 	
       
   439 CContactViewDef::CContactViewDef()
       
   440 	{}
       
   441 
       
   442 void CContactViewDef::ConstructL(CContactItemViewDef* aItemDef)
       
   443 	{
       
   444 	iItemDef=aItemDef;
       
   445 	}
       
   446 	
       
   447 EXPORT_C CContactItemViewDef& CContactViewDef::ItemDef() const
       
   448 /** Gets the object's item view definition.
       
   449 
       
   450 @return Reference to the item view definition. */
       
   451 	{
       
   452 	return *iItemDef;
       
   453 	}
       
   454 
       
   455 EXPORT_C CContactIdArray& CContactViewDef::Groups() const
       
   456 /** This function is not supported. 
       
   457 
       
   458 If called, it leaves with KErrNotSupported. */
       
   459 	{
       
   460 	User::Leave(KErrNotSupported); // leaves...
       
   461 	CContactIdArray* contactIds=NULL;
       
   462 	return *contactIds;
       
   463 	}