metadataengine/client/src/mdeobject.cpp
changeset 0 c53acadfccc6
child 8 6752808b2036
equal deleted inserted replaced
-1:000000000000 0:c53acadfccc6
       
     1 /*
       
     2 * Copyright (c) 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:  ?Description
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <mdeobject.h>
       
    20 
       
    21 #include <mdeproperty.h>
       
    22 #include <mdeobjectdef.h>
       
    23 #include <mdenamespacedef.h>
       
    24 #include <mdesession.h>
       
    25 #include "mdcitem.h"
       
    26 #include "mdscommoninternal.h"
       
    27 #include "mdcserializationbuffer.h"
       
    28 
       
    29 //////////////////////////////
       
    30 //                          //
       
    31 //        TMdEObject        //
       
    32 //                          //
       
    33 //////////////////////////////
       
    34 
       
    35 EXPORT_C TMdEObject::TMdEObject() : 
       
    36 	iDef( NULL )
       
    37 	{
       
    38 	}
       
    39 
       
    40 EXPORT_C TItemId TMdEObject::Id() const
       
    41 	{
       
    42 	return iId;
       
    43 	}
       
    44 
       
    45 EXPORT_C const CMdEObjectDef& TMdEObject::DefL() const
       
    46 	{
       
    47 	if( !iDef )
       
    48 		{
       
    49 		User::Leave( KErrNotFound );
       
    50 		}
       
    51 
       
    52 	return *iDef;
       
    53 	}
       
    54 
       
    55 EXPORT_C TBool TMdEObject::Confidential() const
       
    56 	{
       
    57     return iFlags & EMdEObjectFlagConfidential ? ETrue : EFalse;
       
    58 	}
       
    59 
       
    60 EXPORT_C TBool TMdEObject::Placeholder() const
       
    61 	{
       
    62 	return iFlags & EMdEObjectFlagPlaceholder ? ETrue : EFalse;
       
    63 	}
       
    64 
       
    65 EXPORT_C TBool TMdEObject::Removed() const
       
    66 	{
       
    67 	return iFlags & EMdEObjectFlagRemoved ? ETrue : EFalse;
       
    68 	}
       
    69 
       
    70 EXPORT_C TBool TMdEObject::NotPresent() const
       
    71 	{
       
    72 	return iFlags & EMdEObjectFlagNotPresent ? ETrue : EFalse;
       
    73 	}
       
    74 
       
    75 TUint32 TMdEObject::RequiredBufferSize()
       
    76 	{
       
    77 	return sizeof(TMdCObject);
       
    78 	}
       
    79 
       
    80 void TMdEObject::DeSerializeL(CMdCSerializationBuffer& aBuffer, 
       
    81 		CMdENamespaceDef& aNamespaceDef)
       
    82 	{
       
    83 	const TMdCObject& object = TMdCObject::GetFromBufferL(aBuffer);
       
    84 
       
    85 	// ID
       
    86 	if ( object.iId == KNoId )
       
    87 		{
       
    88 		User::Leave( KErrNotFound );
       
    89 		}
       
    90 	iId = object.iId;
       
    91 
       
    92 	// definition
       
    93 	if ( object.iDefId == KNoDefId )
       
    94 		{
       
    95 		User::Leave( KErrNotFound );
       
    96 		}
       
    97 	iDef = aNamespaceDef.GetObjectDefL( object.iDefId );
       
    98 
       
    99 	// object flags	
       
   100 	iFlags = object.iFlags;
       
   101 	}
       
   102 
       
   103 //////////////////////////////
       
   104 //                          //
       
   105 //        CMdEObject        //
       
   106 //                          //
       
   107 //////////////////////////////
       
   108 
       
   109 CMdEObject* CMdEObject::NewL( CMdEObjectDef& aDef, const TDesC& aUri, TUint32 aMediaId )
       
   110 	{
       
   111 	CMdEObject* self = CMdEObject::NewLC( aDef, aUri, aMediaId );
       
   112     CleanupStack::Pop( self );
       
   113     return self;
       
   114 	}
       
   115 
       
   116 CMdEObject* CMdEObject::NewLC( CMdEObjectDef& aDef, const TDesC& aUri, TUint32 aMediaId )
       
   117 	{
       
   118 	CMdEObject* self = new ( ELeave ) CMdEObject( NULL, KNoId, aDef );
       
   119     CleanupStack::PushL( self );
       
   120     self->ConstructL( aUri, aMediaId );
       
   121     return self;
       
   122 	}
       
   123 
       
   124 CMdEObject::CMdEObject(CMdESession* aSession, TItemId aId, CMdEObjectDef& aDef)
       
   125 		: CMdEInstanceItem( aSession, aId ), iDef( &aDef )
       
   126 	{
       
   127 	}
       
   128 
       
   129 void CMdEObject::ConstructL( const TDesC& aUri, TUint32 aMediaId )
       
   130 	{
       
   131 	InstanceItemBaseConstruct();
       
   132 
       
   133 	DoSetUriL( aUri );
       
   134 	
       
   135 	iMediaId = aMediaId;
       
   136 
       
   137     iFlags = EMdEObjectFlagModOpen;
       
   138    	}
       
   139 
       
   140 CMdEObject::~CMdEObject()
       
   141 	{
       
   142 	if ( OpenForModifications() && BelongsToSession() )
       
   143 		{
       
   144 		TRAP_IGNORE( Session().CancelObjectL( *this ) );
       
   145 		}
       
   146 
       
   147 	delete iUri;
       
   148 
       
   149 	iPropertyArray.ResetAndDestroy();
       
   150 	iPropertyArray.Close();
       
   151 
       
   152 	iFreeTextArray.ResetAndDestroy();
       
   153 	iFreeTextArray.Close();
       
   154 	}
       
   155 
       
   156 EXPORT_C CMdEObjectDef& CMdEObject::Def() const
       
   157 	{
       
   158 	__ASSERT_DEBUG( iDef, User::Panic( _L("Object def is NULL!"), KErrBadHandle ) );
       
   159 	
       
   160 	return *iDef;
       
   161 	}
       
   162 
       
   163 EXPORT_C void CMdEObject::SetDefL(CMdEObjectDef& aDef)
       
   164 	{
       
   165 	__ASSERT_DEBUG( iDef, User::Panic( _L("Object def is NULL!"), KErrBadHandle ) );
       
   166 
       
   167 	if( BelongsToSession() || ( iDef->Id() != KBaseObjectDefId ) )
       
   168 		{
       
   169 		User::Leave( KErrNotSupported );
       
   170 		}
       
   171 
       
   172 	iDef = &aDef;
       
   173 	}
       
   174 
       
   175 EXPORT_C const TDesC& CMdEObject::Uri() const
       
   176 	{
       
   177 	return *iUri;
       
   178 	}
       
   179 
       
   180 EXPORT_C void CMdEObject::SetUriL(const TDesC& aUri)
       
   181 	{
       
   182 	DoSetUriL(aUri);
       
   183 	}
       
   184 
       
   185 EXPORT_C TUint32 CMdEObject::MediaId() const
       
   186 	{
       
   187 	return iMediaId;
       
   188 	}
       
   189 
       
   190 EXPORT_C void CMdEObject::SetMediaId(TUint32 aMediaId)
       
   191 	{
       
   192 	iFlags |= EMdEObjectFlagModObject;
       
   193 	iMediaId = aMediaId;
       
   194 	}
       
   195 
       
   196 EXPORT_C TUint32 CMdEObject::UsageCount() const
       
   197 	{
       
   198 	return iUsageCount;
       
   199 	}
       
   200 
       
   201 EXPORT_C void CMdEObject::Guid( TInt64& aGuidHigh, TInt64& aGuidLow ) const
       
   202 	{
       
   203 	aGuidHigh = iGuidHigh;
       
   204 	aGuidLow  = iGuidLow;
       
   205 	}
       
   206 	
       
   207 EXPORT_C void CMdEObject::SetGuid( const TInt64& aGuidHigh, const TInt64& aGuidLow )
       
   208 	{
       
   209 	iFlags |= EMdEObjectFlagModObject;
       
   210 	iGuidHigh = aGuidHigh;
       
   211 	iGuidLow  = aGuidLow;
       
   212 	}
       
   213 
       
   214 
       
   215 EXPORT_C TBool CMdEObject::Confidential() const
       
   216 	{
       
   217     return iFlags & EMdEObjectFlagConfidential ? ETrue : EFalse;
       
   218 	}
       
   219 
       
   220 EXPORT_C void CMdEObject::SetConfidential( TBool aValue )
       
   221 	{
       
   222 	//check if no change to value
       
   223 	if (Confidential() && aValue || !Confidential() && !aValue) // Confidential() == aValue
       
   224 		{
       
   225 		return;
       
   226 		}
       
   227 
       
   228 	if ( aValue )
       
   229 		{
       
   230 	    iFlags |= EMdEObjectFlagConfidential;
       
   231 		}
       
   232 	else
       
   233 		{
       
   234 	    iFlags &= ~EMdEObjectFlagConfidential;
       
   235 		}
       
   236 	iFlags |= EMdEObjectFlagModObject;
       
   237 	}
       
   238 
       
   239 EXPORT_C TBool CMdEObject::Placeholder() const
       
   240 	{
       
   241 	return iFlags & EMdEObjectFlagPlaceholder ? ETrue : EFalse;
       
   242 	}
       
   243 
       
   244 EXPORT_C void CMdEObject::SetPlaceholder( TBool aValue )
       
   245 	{
       
   246 	//check if no change to value
       
   247 	if ( Placeholder() && aValue || !Placeholder() && !aValue) // Placeholder() == aValue
       
   248 		{
       
   249 		return;
       
   250 		}
       
   251 
       
   252 	if ( aValue )
       
   253 		{
       
   254 		iFlags |= EMdEObjectFlagPlaceholder;
       
   255 		}
       
   256 	else
       
   257 		{
       
   258 		iFlags &= ~EMdEObjectFlagPlaceholder;
       
   259 		}
       
   260 	iFlags |= EMdEObjectFlagModObject;
       
   261 	}
       
   262 
       
   263 void CMdEObject::DoSetUriL(const TDesC& aUri)
       
   264 	{
       
   265 	if ( iUri )
       
   266 		{
       
   267 		if ( *iUri == aUri )
       
   268 			{
       
   269 			return;
       
   270 			}
       
   271 		else
       
   272 			{
       
   273 			delete iUri;
       
   274 			iUri = NULL;
       
   275 			}
       
   276 		}
       
   277 
       
   278 	iUri = HBufC::NewL( aUri.Length() );
       
   279 	iUri->Des().CopyLC( aUri );
       
   280 	iFlags |= EMdEObjectFlagModObject;
       
   281 	}
       
   282 
       
   283 EXPORT_C TInt CMdEObject::PropertyCount() const
       
   284 	{
       
   285 	const TInt propertyCount = iPropertyArray.Count();
       
   286 	if (!(iFlags & EMdEObjectFlagModObject))
       
   287 		{
       
   288 		return propertyCount;
       
   289 		}
       
   290 
       
   291 	TInt c = 0;
       
   292 	for (TInt i = 0; i < propertyCount; ++i)
       
   293 		{
       
   294 		if ( ! iPropertyArray[i]->Removed() )
       
   295 			{
       
   296 			++c;
       
   297 			}
       
   298 		}
       
   299 	return c;
       
   300 	}
       
   301 
       
   302 EXPORT_C CMdEProperty& CMdEObject::PropertyL(TInt aIndex) const
       
   303 	{
       
   304 	if ( aIndex < 0 || aIndex > PropertyCount() )
       
   305 		{
       
   306 		User::Leave( KErrArgument );
       
   307 		}
       
   308 	if (!(iFlags & EMdEObjectFlagModObject))
       
   309 		{
       
   310 		return *iPropertyArray[aIndex];
       
   311 		}
       
   312 	
       
   313 	TInt j = 0;
       
   314 	const TInt count = iPropertyArray.Count();
       
   315 	
       
   316 	for (TInt i = 0; i < count; ++i)
       
   317 		{
       
   318 		if ( ! iPropertyArray[i]->Removed() )
       
   319 			{
       
   320 			if (j == aIndex)
       
   321 				{
       
   322 				return *iPropertyArray[j];
       
   323 				}
       
   324 			++j;
       
   325 			}
       
   326 		}
       
   327 	User::Leave( KErrNotFound );
       
   328 	return *iPropertyArray[0]; // <-- just to stop compiler warnings!!
       
   329 	}
       
   330 
       
   331 EXPORT_C TInt CMdEObject::PropertyCount(const CMdEPropertyDef& aDef) const
       
   332 	{
       
   333 	TInt propCount = 0;
       
   334 	
       
   335 	const TInt count = iPropertyArray.Count();
       
   336 	
       
   337 	for (TInt i = 0; i < count; i++)
       
   338 		{
       
   339         if( ! iPropertyArray[i]->Removed() &&
       
   340             iPropertyArray[i]->Def().Compare( aDef ) == 0 )
       
   341         	{
       
   342         	++propCount;
       
   343         	}
       
   344 		}
       
   345     return propCount;
       
   346 	}
       
   347 
       
   348 EXPORT_C TInt CMdEObject::Property(const CMdEPropertyDef& aDef, CMdEProperty*& aProperty, TInt aStartAt) const
       
   349     {
       
   350     if (aStartAt <= 0)
       
   351         {
       
   352         const TInt i = iPropertyArray.FindInOrder(aDef, CMdEObject::CompareProperties );
       
   353         if (i >= 0 && i < iPropertyArray.Count() && !iPropertyArray[i]->Removed())
       
   354             {
       
   355             aProperty = iPropertyArray[i];
       
   356             return i;
       
   357             }
       
   358         }
       
   359     return KErrNotFound;
       
   360     }
       
   361 
       
   362 EXPORT_C CMdEProperty& CMdEObject::AddBoolPropertyL(CMdEPropertyDef& aDef, TBool aValue)
       
   363 	{
       
   364     if( aDef.PropertyType() != EPropertyBool )
       
   365     	{
       
   366     	User::Leave( KErrArgument );
       
   367     	}	
       
   368 	
       
   369 	CMdEProperty* property = CMdEProperty::NewLC(aDef, *this);
       
   370 	property->SetBoolValueL( aValue );
       
   371 	DoAddPropertyL(*property);
       
   372 	CleanupStack::Pop(property);
       
   373 
       
   374 	return *property;
       
   375 	}
       
   376 
       
   377 EXPORT_C CMdEProperty& CMdEObject::AddInt8PropertyL(CMdEPropertyDef& aDef, TInt8 aValue)
       
   378 	{
       
   379     if( aDef.PropertyType() != EPropertyInt8 )
       
   380     	{
       
   381     	User::Leave( KErrArgument );
       
   382     	}	
       
   383 	
       
   384     CMdEProperty* property = CMdEProperty::NewLC(aDef, *this);
       
   385     property->SetInt8ValueL( aValue );
       
   386 	DoAddPropertyL(*property);
       
   387 	CleanupStack::Pop(property);
       
   388 
       
   389 	return *property;
       
   390 	}
       
   391 
       
   392 EXPORT_C CMdEProperty& CMdEObject::AddUint8PropertyL(CMdEPropertyDef& aDef, TUint8 aValue)
       
   393 	{
       
   394     if( aDef.PropertyType() != EPropertyUint8 )
       
   395     	{
       
   396     	User::Leave( KErrArgument );
       
   397     	}	
       
   398 	
       
   399     CMdEProperty* property = CMdEProperty::NewLC(aDef, *this);
       
   400     property->SetUint8ValueL( aValue );
       
   401 	DoAddPropertyL(*property);
       
   402 	CleanupStack::Pop(property);
       
   403 
       
   404 	return *property;
       
   405 	}
       
   406 
       
   407 EXPORT_C CMdEProperty& CMdEObject::AddInt16PropertyL(CMdEPropertyDef& aDef, TInt16 aValue)
       
   408 	{
       
   409     if( aDef.PropertyType() != EPropertyInt16 )
       
   410     	{
       
   411     	User::Leave( KErrArgument );
       
   412     	}	
       
   413 	
       
   414     CMdEProperty* property = CMdEProperty::NewLC(aDef, *this);
       
   415     property->SetInt16ValueL( aValue );
       
   416 	DoAddPropertyL(*property);
       
   417 	CleanupStack::Pop(property);
       
   418 
       
   419 	return *property;
       
   420 	}
       
   421 
       
   422 EXPORT_C CMdEProperty& CMdEObject::AddUint16PropertyL(CMdEPropertyDef& aDef, TUint16 aValue)
       
   423 	{
       
   424     if( aDef.PropertyType() != EPropertyUint16 )
       
   425     	{
       
   426     	User::Leave( KErrArgument );
       
   427     	}	
       
   428 	
       
   429     CMdEProperty* property = CMdEProperty::NewLC(aDef, *this);
       
   430     property->SetUint16ValueL( aValue );
       
   431 	DoAddPropertyL(*property);
       
   432 	CleanupStack::Pop(property);
       
   433 
       
   434 	return *property;
       
   435 	}
       
   436 
       
   437 EXPORT_C CMdEProperty& CMdEObject::AddInt32PropertyL(CMdEPropertyDef& aDef, TInt32 aValue)
       
   438 	{
       
   439     if( aDef.PropertyType() != EPropertyInt32 )
       
   440     	{
       
   441     	User::Leave( KErrArgument );
       
   442     	}	
       
   443 
       
   444     CMdEProperty* property = CMdEProperty::NewLC(aDef, *this);
       
   445     property->SetInt32ValueL( aValue );
       
   446 	DoAddPropertyL(*property);
       
   447 	CleanupStack::Pop(property);
       
   448 
       
   449 	return *property;
       
   450 	}
       
   451 
       
   452 EXPORT_C CMdEProperty& CMdEObject::AddUint32PropertyL(CMdEPropertyDef& aDef, TUint32 aValue)
       
   453 	{
       
   454     if( aDef.PropertyType() != EPropertyUint32 )
       
   455     	{
       
   456     	User::Leave( KErrArgument );
       
   457     	}	
       
   458 
       
   459     CMdEProperty* property = CMdEProperty::NewLC(aDef, *this);
       
   460     property->SetUint32ValueL( aValue );
       
   461 	DoAddPropertyL(*property);
       
   462 	CleanupStack::Pop(property);
       
   463 
       
   464 	return *property;
       
   465 	}
       
   466 
       
   467 EXPORT_C CMdEProperty& CMdEObject::AddInt64PropertyL(CMdEPropertyDef& aDef, TInt64 aValue)
       
   468 	{
       
   469     if( aDef.PropertyType() != EPropertyInt64 )
       
   470 	   	{
       
   471     	User::Leave( KErrArgument );
       
   472     	}
       
   473 
       
   474     CMdEProperty* property = CMdEProperty::NewLC(aDef, *this);
       
   475     property->SetInt64ValueL( aValue );
       
   476 	DoAddPropertyL(*property);
       
   477 	CleanupStack::Pop(property);
       
   478 
       
   479 	return *property;
       
   480 	}
       
   481 
       
   482 EXPORT_C CMdEProperty& CMdEObject::AddReal32PropertyL(CMdEPropertyDef& aDef, TReal32 aValue)
       
   483 	{
       
   484     if( aDef.PropertyType() != EPropertyReal32 )
       
   485     	{
       
   486     	User::Leave( KErrArgument );
       
   487     	}
       
   488 
       
   489     CMdEProperty* property = CMdEProperty::NewLC(aDef, *this);
       
   490     property->SetReal32ValueL( aValue );
       
   491 	DoAddPropertyL(*property);
       
   492 	CleanupStack::Pop(property);
       
   493 
       
   494 	return *property;
       
   495 	}
       
   496 
       
   497 EXPORT_C CMdEProperty& CMdEObject::AddReal64PropertyL(CMdEPropertyDef& aDef, TReal64 aValue)
       
   498 	{
       
   499     if( aDef.PropertyType() != EPropertyReal64 )
       
   500     	{
       
   501     	User::Leave( KErrArgument );
       
   502     	}
       
   503 
       
   504     CMdEProperty* property = CMdEProperty::NewLC(aDef, *this);
       
   505     property->SetReal64ValueL( aValue );
       
   506 	DoAddPropertyL(*property);
       
   507 	CleanupStack::Pop(property);
       
   508 
       
   509 	return *property;
       
   510 	}
       
   511 
       
   512 EXPORT_C CMdEProperty& CMdEObject::AddTextPropertyL(CMdEPropertyDef& aDef, const TDesC& aValue)
       
   513 	{
       
   514     if( aDef.PropertyType() != EPropertyText )
       
   515     	{
       
   516     	User::Leave( KErrArgument );
       
   517     	}
       
   518 	
       
   519     CMdEProperty* property = CMdEProperty::NewLC(aDef, *this);
       
   520     property->SetTextValueL( aValue );
       
   521 	DoAddPropertyL(*property);
       
   522 	CleanupStack::Pop(property);
       
   523 
       
   524 	return *property;
       
   525 	}
       
   526 
       
   527 EXPORT_C CMdEProperty& CMdEObject::AddTimePropertyL(CMdEPropertyDef& aDef, TTime aValue)
       
   528 	{
       
   529     if( aDef.PropertyType() != EPropertyTime )
       
   530     	{
       
   531     	User::Leave( KErrArgument );
       
   532     	}	
       
   533 
       
   534     CMdEProperty* property = CMdEProperty::NewLC(aDef, *this);
       
   535     property->SetTimeValueL( aValue );
       
   536 	DoAddPropertyL(*property);
       
   537 	CleanupStack::Pop(property);
       
   538 
       
   539 	return *property;
       
   540 	}
       
   541 
       
   542 void CMdEObject::DoAddPropertyL(CMdEProperty& aProperty)
       
   543 	{
       
   544 	TInt err = iPropertyArray.InsertInOrder(&aProperty, 
       
   545 								TLinearOrder<CMdEProperty>(CMdEObject::CompareProperties));
       
   546 	if (err == KErrAlreadyExists)
       
   547 		{
       
   548 		TInt f = iPropertyArray.FindInOrder(&aProperty,
       
   549 								TLinearOrder<CMdEProperty>(CMdEObject::CompareProperties));
       
   550 
       
   551 		// this must never happen
       
   552 		__ASSERT_DEBUG( f >= KErrNone, User::Panic( _L("AddProperty find!"), KErrGeneral ) );
       
   553 
       
   554 		if( ! iPropertyArray[f]->Removed() )
       
   555 			{
       
   556 			User::LeaveIfError( err );
       
   557 			}
       
   558 
       
   559 		CMdEProperty* oldProperty = iPropertyArray[f];
       
   560 		iPropertyArray[f] = &aProperty;
       
   561 		delete oldProperty;
       
   562 		}
       
   563 	else if (err < KErrNone)
       
   564 		{
       
   565 		User::Leave(err);
       
   566 		}
       
   567 
       
   568 	iFlags |= EMdEObjectFlagModProperty;
       
   569 	}
       
   570 
       
   571 EXPORT_C void CMdEObject::RemoveProperty(TInt aIndex)
       
   572 	{
       
   573 	const TInt propertyCount = iPropertyArray.Count();
       
   574 	if ( aIndex < 0 || aIndex >= propertyCount )
       
   575 		{
       
   576 		return;
       
   577 		}
       
   578 
       
   579 	TInt j = 0;
       
   580 	TBool found = EFalse;
       
   581 	for (TInt i = 0; i < propertyCount; ++i)
       
   582 		{
       
   583 		if ( ! iPropertyArray[i]->Removed() )
       
   584 			{
       
   585 			if (j == aIndex)
       
   586 				{
       
   587 				found = ETrue;
       
   588 				break;
       
   589 				}
       
   590 			++j;
       
   591 			}
       
   592 		}
       
   593 
       
   594 	if ( !found )
       
   595 		{
       
   596 		return;
       
   597 		}
       
   598 
       
   599 	CMdEProperty* property = iPropertyArray[j];
       
   600 	if( property->Def().ReadOnly() )
       
   601     	{
       
   602     	return;
       
   603     	}
       
   604 
       
   605 	property->SetRemoved();
       
   606 
       
   607 	iFlags |= EMdEObjectFlagModProperty;
       
   608 	}
       
   609 
       
   610 TInt CMdEObject::CompareProperties(const CMdEProperty& aFirst, const CMdEProperty& aSecond)
       
   611 	{
       
   612 	return aFirst.Def().Compare( aSecond.Def() );
       
   613 	}
       
   614 
       
   615 EXPORT_C TInt CMdEObject::FreeTextCount() const
       
   616 	{
       
   617 	return iFreeTextArray.Count();
       
   618 	}
       
   619 
       
   620 EXPORT_C TPtrC CMdEObject::FreeText(TInt aIndex) const
       
   621 	{
       
   622 	return *iFreeTextArray[aIndex];
       
   623 	}
       
   624 
       
   625 EXPORT_C TInt CMdEObject::FreeTextIndex(const TDesC& aFreeText) const
       
   626 	{
       
   627 	const TInt count = iFreeTextArray.Count();
       
   628 	
       
   629 	for (TInt i = 0; i < count; i++)
       
   630 		{
       
   631 		if (aFreeText.Compare(*iFreeTextArray[i]) == 0)
       
   632 			{
       
   633 			return i;
       
   634 			}
       
   635 		}
       
   636 
       
   637 	return KErrNotFound;
       
   638 	}
       
   639 
       
   640 EXPORT_C void CMdEObject::AddFreeTextL(const TDesC& aFreeText)
       
   641 	{
       
   642 	HBufC* freeText = aFreeText.AllocLC();
       
   643 	if (DoAddFreeTextL(*freeText) == KErrNone)
       
   644 		{
       
   645 		CleanupStack::Pop(freeText);
       
   646 		}
       
   647 	else
       
   648 		{
       
   649 		CleanupStack::PopAndDestroy(freeText);
       
   650 		}
       
   651 	}
       
   652 
       
   653 TInt CMdEObject::DoAddFreeTextL(const HBufC& aFreeText)
       
   654 	{
       
   655 	TInt err = iFreeTextArray.InsertInOrder(&aFreeText, TLinearOrder<HBufC>(CMdEObject::CompareFreeTexts));
       
   656 
       
   657 	if (err == KErrNone)
       
   658 		{
       
   659 		iFlags |= EMdEObjectFlagModFreeText;
       
   660 		}
       
   661 	return err;
       
   662 	}
       
   663 
       
   664 EXPORT_C void CMdEObject::RemoveFreeText(TInt aIndex)
       
   665 	{
       
   666 	delete iFreeTextArray[aIndex];
       
   667 	iFreeTextArray[aIndex] = NULL;
       
   668 	iFreeTextArray.Remove(aIndex);
       
   669 	iFlags |= EMdEObjectFlagModFreeText;
       
   670 	}
       
   671 
       
   672 TInt CMdEObject::CompareFreeTexts(const HBufC& aFirst, const HBufC& aSecond)
       
   673 	{
       
   674 	return aFirst.Compare(aSecond);
       
   675 	}
       
   676 
       
   677 EXPORT_C void CMdEObject::MovePropertiesL(CMdEObject& aObject)
       
   678 	{
       
   679 	__ASSERT_DEBUG( iDef, User::Panic( _L("Object def is NULL!"), KErrBadHandle ) );
       
   680 	
       
   681 	// definitions must match
       
   682 	if ( iDef->Compare( *aObject.iDef ) )
       
   683 		{
       
   684 		User::Leave( KErrArgument );
       
   685 		}
       
   686 
       
   687 	// object must be open
       
   688 	if ( !OpenForModifications() )
       
   689 		{
       
   690 		User::Leave( KErrAccessDenied );
       
   691 		}
       
   692 
       
   693 	// and cannot contain any properties
       
   694 	ClearObject( EFalse );
       
   695 	if ( iPropertyArray.Count() )
       
   696 		{
       
   697 		User::Leave( KErrAlreadyExists );
       
   698 		}
       
   699 
       
   700 	const TInt arraySize = aObject.iPropertyArray.Count();
       
   701 	iPropertyArray.ReserveL( arraySize );
       
   702 
       
   703 	// add all properties to this object, 
       
   704 	// but don't remove yet from other object
       
   705 	for ( TInt i = 0; i < arraySize; ++i )
       
   706 		{
       
   707 		TRAPD( err, iPropertyArray.AppendL( aObject.iPropertyArray[i] ) );
       
   708 		if (err != KErrNone)
       
   709 			{
       
   710 			// restore this objects to original state
       
   711 			iPropertyArray.Reset();
       
   712 			User::Leave( err );
       
   713 			}
       
   714 		}
       
   715 
       
   716 	// remove properties from other object
       
   717 	aObject.iPropertyArray.Reset();
       
   718 
       
   719 	// mark all moved properties as modified
       
   720 	for ( TInt i = 0; i < arraySize; ++i )
       
   721 		{
       
   722 		CMdEProperty* property = iPropertyArray[i];
       
   723 		property->SetModified();
       
   724 		}
       
   725 
       
   726 	iFlags |= EMdEObjectFlagModProperty;
       
   727 	}
       
   728 
       
   729 
       
   730 TBool CMdEObject::ObjectModified() const
       
   731     {
       
   732     return iFlags & EMdEObjectFlagModObject ? ETrue : EFalse;
       
   733     }
       
   734 
       
   735 TBool CMdEObject::FreeTextModified() const
       
   736     {
       
   737     return iFlags & EMdEObjectFlagModFreeText ? ETrue : EFalse;
       
   738     }
       
   739 
       
   740 TBool CMdEObject::PropertyModified() const
       
   741     {
       
   742     return iFlags & EMdEObjectFlagModProperty ? ETrue : EFalse;
       
   743     }
       
   744 
       
   745 void CMdEObject::AutoLockL() const
       
   746 	{
       
   747 	if( BelongsToSession() )
       
   748 		{
       
   749 		User::Leave( KErrAlreadyExists );
       
   750 		}
       
   751 	
       
   752 	iFlags |= EMdEObjectFlagAutoLock;
       
   753 	}
       
   754 
       
   755 EXPORT_C TBool CMdEObject::OpenForModifications() const
       
   756 	{
       
   757 	return iFlags & EMdEObjectFlagModOpen ? ETrue : EFalse;
       
   758 	}
       
   759 
       
   760 void CMdEObject::ClearObject( TBool aClearFlags )
       
   761 	{
       
   762 	// clear mod flags
       
   763 	if (aClearFlags)
       
   764 		{
       
   765 		iFlags &= ~(EMdEObjectFlagModObject | EMdEObjectFlagModProperty | EMdEObjectFlagModFreeText | EMdEObjectFlagModOpen);
       
   766 
       
   767 		// auto locked object's are marked back to open for modifications
       
   768 		if ( iFlags & EMdEObjectFlagAutoLock )
       
   769 			{
       
   770 			iFlags |= EMdEObjectFlagModOpen;
       
   771 			}
       
   772 		}
       
   773 
       
   774 	// clear mod flags in properties
       
   775 	for (TInt i = iPropertyArray.Count() - 1; i >= 0 ; --i)
       
   776 		{
       
   777     	CMdEProperty* prop = iPropertyArray[i];
       
   778         if ( prop->Removed() )
       
   779         	{
       
   780         	delete prop;
       
   781         	iPropertyArray.Remove( i );
       
   782         	continue;
       
   783         	}
       
   784         if (aClearFlags)
       
   785         	{
       
   786         	prop->ClearFlags();
       
   787         	}
       
   788 		}
       
   789 	}
       
   790 
       
   791 void CMdEObject::SetNotOpenForModifications()
       
   792 	{
       
   793 	iFlags &= ~EMdEObjectFlagModOpen;
       
   794 	}
       
   795 
       
   796 TUint32 CMdEObject::ModifiedPropertiesCount() const
       
   797 	{
       
   798 	TUint32 modCount = 0;
       
   799 	const TInt count = iPropertyArray.Count();
       
   800 	
       
   801 	for ( TInt i = 0; i < count; ++i )
       
   802 		{
       
   803 		if (iPropertyArray[i]->Modified())
       
   804 			{
       
   805 			++modCount;
       
   806 			}
       
   807 		}
       
   808 	return modCount;
       
   809 	}
       
   810 
       
   811 TUint32 CMdEObject::RequiredBufferSize() const
       
   812 	{
       
   813 	TUint32 bufferSize = sizeof( TMdCObject );
       
   814 
       
   815 	if (iUri)
       
   816 		{
       
   817 		bufferSize += CMdCSerializationBuffer::RequiredSize( *iUri );
       
   818 		}
       
   819 
       
   820 	TUint32 bufSave = bufferSize;
       
   821 	// properties
       
   822 	if ( iPropertyArray.Count() > 0 )
       
   823 		{
       
   824 		const TInt count = iPropertyArray.Count();
       
   825 		for ( TInt i = 0; i < count; ++i )
       
   826 			{
       
   827 			if (iPropertyArray[i]->Modified())
       
   828 				{
       
   829 				bufferSize += iPropertyArray[i]->RequiredBufferSize();
       
   830 				iFlags |= EMdEObjectFlagModProperty;
       
   831 				}
       
   832 			}
       
   833 		}
       
   834 	
       
   835 	if (!(iFlags & EMdEObjectFlagModProperty))
       
   836 		{
       
   837 		bufferSize = bufSave;
       
   838 		}
       
   839 	
       
   840 	// freetext
       
   841 	if ( iFlags & EMdEObjectFlagModFreeText && iFreeTextArray.Count() > 0 )
       
   842 		{
       
   843 		
       
   844 		const TInt count = iFreeTextArray.Count();
       
   845 		for ( TInt i = 0; i < count; ++i )
       
   846 			{
       
   847 			bufferSize += CMdCSerializationBuffer::RequiredSize( *iFreeTextArray[i] );
       
   848 			}
       
   849 		}
       
   850 
       
   851 	return bufferSize;
       
   852 	}
       
   853 
       
   854 TMdCOffset CMdEObject::SerializeL(CMdCSerializationBuffer& aBuffer, TMdCOffset aFreespaceOffset) const
       
   855 	{
       
   856 	__ASSERT_DEBUG( iDef, User::Panic( _L("Object def is NULL!"), KErrBadHandle ) );
       
   857 
       
   858 	const TMdCOffset objectOffset = aBuffer.Position();
       
   859 	TMdCObject object;
       
   860 	
       
   861 	// objectId
       
   862 	object.iId = Id();
       
   863 	// objectDefId
       
   864 	object.iDefId = iDef->Id();
       
   865 	// object flags
       
   866 	object.iFlags = iFlags;
       
   867 	// mediaId
       
   868 	object.iMediaId = iMediaId;
       
   869 	// usageCount
       
   870 	object.iUsageCount = iUsageCount;
       
   871 	// guid low
       
   872 	object.iGuidHigh = iGuidHigh;
       
   873 	// guid high
       
   874 	object.iGuidLow = iGuidLow;
       
   875 	// uri
       
   876 	object.iUri.iPtr.iCount = iUri->Length();
       
   877 	object.iUri.iPtr.iOffset = aFreespaceOffset;
       
   878 	
       
   879 	aBuffer.PositionL( aFreespaceOffset );
       
   880 	aFreespaceOffset = aBuffer.InsertL( *iUri );
       
   881 
       
   882 	// adding properties
       
   883 	if ( iFlags & EMdEObjectFlagModProperty && iPropertyArray.Count() > 0 )
       
   884 		{
       
   885 		// first set up offset and count to properties
       
   886 		object.iProperties.iPtr.iOffset = aFreespaceOffset;
       
   887 		object.iProperties.iPtr.iCount = ModifiedPropertiesCount();
       
   888 
       
   889 		// move after properties table
       
   890 		aFreespaceOffset += object.iProperties.iPtr.iCount * sizeof( TMdCProperty );
       
   891 
       
   892 		const TInt count = iPropertyArray.Count();
       
   893 		TInt modifiedPropertyCounter = 0;
       
   894 		// insert properties
       
   895 		for ( TInt i = 0; i < count; ++i )
       
   896 			{
       
   897 			if (!iPropertyArray[i]->Modified())
       
   898 				{
       
   899 				continue;
       
   900 				}
       
   901 			aBuffer.PositionL( object.iProperties.iPtr.iOffset + modifiedPropertyCounter * sizeof( TMdCProperty ) );
       
   902 			aFreespaceOffset = iPropertyArray[i]->SerializeL(aBuffer, aFreespaceOffset);
       
   903 			++modifiedPropertyCounter;
       
   904 			}
       
   905 		}
       
   906 	else
       
   907 		{
       
   908 		object.iProperties.iPtr.iOffset = KNoOffset;
       
   909 		object.iProperties.iPtr.iCount = 0;
       
   910 		}
       
   911 
       
   912 	// adding freetext
       
   913 	if ( iFlags & EMdEObjectFlagModFreeText && iFreeTextArray.Count() > 0 )
       
   914 		{
       
   915 		// first set up offset to freetext
       
   916 		object.iFreeTexts.iPtr.iOffset = aFreespaceOffset;
       
   917 		object.iFreeTexts.iPtr.iCount = iFreeTextArray.Count();
       
   918 
       
   919 		const TInt count = iFreeTextArray.Count();
       
   920 		
       
   921 		// insert freetext
       
   922 		aBuffer.PositionL( aFreespaceOffset );
       
   923 		for ( TInt i = 0; i < count; ++i )
       
   924 			{
       
   925 			aFreespaceOffset = aBuffer.InsertL( *iFreeTextArray[i] );
       
   926 			}
       
   927 		}
       
   928 	else
       
   929 		{
       
   930 		object.iFreeTexts.iPtr.iOffset = KNoOffset;
       
   931 		object.iFreeTexts.iPtr.iCount = 0;
       
   932 		}
       
   933 	
       
   934 	// store object informations
       
   935 	aBuffer.PositionL( objectOffset );
       
   936 	object.SerializeL( aBuffer );
       
   937 
       
   938 	return aFreespaceOffset;
       
   939 	}
       
   940 
       
   941 CMdEObject* CMdEObject::NewL( CMdESession* aSession, CMdCSerializationBuffer& aBuffer, CMdENamespaceDef& aNamespaceDef )
       
   942 	{
       
   943 	CMdEObject* ret = CMdEObject::NewLC( aSession, aBuffer, aNamespaceDef );
       
   944 	CleanupStack::Pop( ret );
       
   945 	return ret;
       
   946 	}
       
   947 
       
   948 
       
   949 CMdEObject* CMdEObject::NewLC( CMdESession* aSession, CMdCSerializationBuffer& aBuffer, CMdENamespaceDef& aNamespaceDef )
       
   950 	{
       
   951 	const TMdCObject& serializedObject = TMdCObject::GetFromBufferL( aBuffer );
       
   952 	
       
   953 	// objectid
       
   954 	if (serializedObject.iId == KNoId)
       
   955 		{
       
   956 		User::Leave( KErrNotFound );
       
   957 		}
       
   958 
       
   959 	// objectDefId
       
   960 	if (serializedObject.iDefId == KNoDefId)
       
   961 		{
       
   962 		User::Leave( KErrNotFound );
       
   963 		}
       
   964 	CMdEObjectDef* newObjectDef = aNamespaceDef.GetObjectDefL( serializedObject.iDefId );
       
   965 	if( !newObjectDef )
       
   966 		{
       
   967 		User::Leave( KErrNotFound );
       
   968 		}
       
   969 
       
   970 	CMdEObject* newObject = new (ELeave) CMdEObject( aSession, serializedObject.iId, *newObjectDef );
       
   971 	CleanupStack::PushL( newObject );
       
   972 
       
   973 	// set correct variables
       
   974 	// object flags
       
   975 	newObject->iFlags = serializedObject.iFlags;
       
   976 	// mediaId
       
   977 	newObject->iMediaId = serializedObject.iMediaId;
       
   978 	// usage count
       
   979 	newObject->iUsageCount = serializedObject.iUsageCount;
       
   980 	// guid low
       
   981 	newObject->iGuidHigh = serializedObject.iGuidHigh;
       
   982 	// guid high
       
   983 	newObject->iGuidLow = serializedObject.iGuidLow;
       
   984 	// uri
       
   985 	if ( serializedObject.iUri.iPtr.iCount == 0 )
       
   986 		{
       
   987 		User::Leave( KErrNotFound );
       
   988 		}
       
   989 	aBuffer.PositionL( serializedObject.iUri.iPtr.iOffset );
       
   990 	newObject->iUri = aBuffer.ReceiveDes16L();
       
   991 
       
   992 	// read properties array
       
   993 	if ( serializedObject.iProperties.iPtr.iOffset != KNoOffset )
       
   994 		{
       
   995 		newObject->iPropertyArray.ReserveL( serializedObject.iProperties.iPtr.iCount );
       
   996 
       
   997 		for ( TUint32 i = 0; i < serializedObject.iProperties.iPtr.iCount; ++i )
       
   998 			{
       
   999 			aBuffer.PositionL( serializedObject.iProperties.iPtr.iOffset
       
  1000 					+ i * sizeof(TMdCProperty) );
       
  1001 
       
  1002 			CMdEProperty* property = CMdEProperty::NewLC( *newObject, aBuffer );
       
  1003 			newObject->iPropertyArray.AppendL( property );
       
  1004 			CleanupStack::Pop( property );
       
  1005 			}
       
  1006 		}
       
  1007 
       
  1008 	// read freetext array
       
  1009 	if ( serializedObject.iFreeTexts.iPtr.iOffset != KNoOffset )
       
  1010 		{
       
  1011 		aBuffer.PositionL( serializedObject.iFreeTexts.iPtr.iOffset );
       
  1012 
       
  1013 		newObject->iFreeTextArray.ReserveL( serializedObject.iFreeTexts.iPtr.iCount );
       
  1014 		for ( TUint32 i = 0; i < serializedObject.iFreeTexts.iPtr.iCount; ++i )
       
  1015 			{
       
  1016 			HBufC16* hbuf = aBuffer.ReceiveDes16L();
       
  1017 			CleanupStack::PushL( hbuf );
       
  1018 			newObject->iFreeTextArray.AppendL( hbuf );
       
  1019 			CleanupStack::Pop( hbuf );
       
  1020 			}
       
  1021 		}
       
  1022 
       
  1023 	return newObject;
       
  1024 	}
       
  1025 
       
  1026 TMdEInstanceType CMdEObject::InstanceType() const
       
  1027 	{
       
  1028 	return EMdETypeObject;
       
  1029 	}
       
  1030 
       
  1031 TInt CMdEObject::CompareProperties(const CMdEPropertyDef* aPropertyDef, const CMdEProperty& aProperty)
       
  1032     {
       
  1033     return aPropertyDef->Compare( aProperty.Def() );
       
  1034     }
       
  1035