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