applayerpluginsandutils/bookmarksupport/src/propertylist.cpp
changeset 0 b16258d2340f
equal deleted inserted replaced
-1:000000000000 0:b16258d2340f
       
     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 // Class for managing custom property lists
       
    15 // @internalComponent
       
    16 // 
       
    17 //
       
    18 
       
    19 #include "propertylist.h"
       
    20 #include "repository.h"
       
    21 
       
    22 CPropertyList* CPropertyList::NewL(CRepository& iRepository, CPropertyReg& aPropertyRegister, CPropertyReg::TPropertyGroup aGroup, TUint32 aIndexBase)
       
    23 	{
       
    24 	CPropertyList* self = new (ELeave) CPropertyList(iRepository, aPropertyRegister, aGroup, aIndexBase);
       
    25 	return self;
       
    26 	}
       
    27 
       
    28 CPropertyList::CPropertyList(CRepository& iRepository, CPropertyReg& aPropertyRegister, CPropertyReg::TPropertyGroup aGroup, TUint32 aIndexBase)
       
    29 	: CRepositoryAccessor(), iPropertyRegister(aPropertyRegister), iGroup(aGroup), iIndexBase(aIndexBase)
       
    30 	{
       
    31 	SetRepository(iRepository);
       
    32 	}
       
    33 
       
    34 CPropertyList::~CPropertyList()
       
    35 	{
       
    36 	Reset();
       
    37 	}
       
    38 
       
    39 void CPropertyList::Reset()
       
    40 	{
       
    41 	TInt index = iPropertyValues.Count() - 1;
       
    42 	for (;index >= 0; --index)
       
    43 		{
       
    44 		delete iPropertyValues[index];
       
    45 		}
       
    46 	iPropertyValues.Reset();
       
    47 	}
       
    48 	
       
    49 TInt CPropertyList::PropertyIndex(TUid aPropertyId, Bookmark::TPropertyType aType) const
       
    50 	{
       
    51 	TInt index = iPropertyRegister.PropertyIndex(iGroup, aPropertyId, aType);
       
    52 	TInt count = iPropertyValues.Count();
       
    53 	if (index == KErrNotFound || index >= count || !iPropertyValues[index])
       
    54 		{
       
    55 		index = KErrNotFound;
       
    56 		}
       
    57 		
       
    58 	return index;
       
    59 	}
       
    60 
       
    61 TInt CPropertyList::GetCustomProperty(TUid aPropertyId, TInt& aValue) const
       
    62 	{
       
    63 	TInt index = PropertyIndex(aPropertyId, Bookmark::EDataTypeInt);
       
    64 	if (index == KErrNotFound)
       
    65 		{
       
    66 		return KErrNotFound;
       
    67 		}
       
    68 
       
    69 	TInt* intPtr = static_cast<TInt*>(iPropertyValues[index]);
       
    70 	aValue = *intPtr;
       
    71 	return KErrNone;
       
    72 	}
       
    73 
       
    74 TInt CPropertyList::GetCustomProperty(TUid aPropertyId, TReal& aValue) const
       
    75 	{
       
    76 	TInt index = PropertyIndex(aPropertyId, Bookmark::EDataTypeReal);
       
    77 	if (index == KErrNotFound)
       
    78 		{
       
    79 		return KErrNotFound;
       
    80 		}
       
    81 
       
    82 	TReal* realPtr = static_cast<TReal*>(iPropertyValues[index]);
       
    83 	aValue = *realPtr;
       
    84 	return KErrNone;
       
    85 	}
       
    86 
       
    87 TInt CPropertyList::GetCustomProperty(TUid aPropertyId, TDes& aValue) const
       
    88 	{
       
    89 	TInt index = PropertyIndex(aPropertyId, Bookmark::EDataTypeDes16);
       
    90 	if (index == KErrNotFound)
       
    91 		{
       
    92 		return KErrNotFound;
       
    93 		}
       
    94 
       
    95 	TDes* descPtr = static_cast<TDes*>(iPropertyValues[index]);
       
    96 	if (aValue.MaxLength() < descPtr->Length())
       
    97 		{
       
    98 		return KErrArgument;
       
    99 		}
       
   100 	aValue = *descPtr;
       
   101 	return KErrNone;
       
   102 	}
       
   103 	
       
   104 TInt CPropertyList::GetCustomProperty(TUid aPropertyId, TDes8& aValue) const
       
   105 	{
       
   106 	TInt index = PropertyIndex(aPropertyId, Bookmark::EDataTypeDes8);
       
   107 	if (index == KErrNotFound)
       
   108 		{
       
   109 		return KErrNotFound;
       
   110 		}
       
   111 
       
   112 	TDes8* desc8Ptr = static_cast<TDes8*>(iPropertyValues[index]);
       
   113 	if (aValue.MaxLength() < desc8Ptr->Length())
       
   114 		{
       
   115 		return KErrArgument;
       
   116 		}
       
   117 	aValue = *desc8Ptr;
       
   118 	return KErrNone;
       
   119 	}
       
   120 	
       
   121 void CPropertyList::SetCustomPropertyL(TUid aPropertyId, TInt aValue)
       
   122 	{
       
   123 	TInt index = iPropertyRegister.PropertyIndex(iGroup, aPropertyId, Bookmark::EDataTypeInt);
       
   124 	if (index == KErrNotFound)
       
   125 		{
       
   126 		User::Leave(Bookmark::KErrNotRegistered);
       
   127 		}
       
   128 	UpdateValueListItemL(index, Bookmark::EDataTypeInt, &aValue);
       
   129 	SetDirty();
       
   130 	}
       
   131 
       
   132 void CPropertyList::SetCustomPropertyL(TUid aPropertyId, TReal aValue)
       
   133 	{
       
   134 	TInt index = iPropertyRegister.PropertyIndex(iGroup, aPropertyId, Bookmark::EDataTypeReal);
       
   135 	if (index == KErrNotFound)
       
   136 		{
       
   137 		User::Leave(Bookmark::KErrNotRegistered);
       
   138 		}
       
   139 	UpdateValueListItemL(index, Bookmark::EDataTypeReal, &aValue);
       
   140 	SetDirty();
       
   141 	}
       
   142 
       
   143 void CPropertyList::SetCustomPropertyL(TUid aPropertyId, const TDesC& aValue)
       
   144 	{
       
   145 	// The aValue must be smaller that the maximim descriptor storage size
       
   146 	__ASSERT_ALWAYS(aValue.Length() <= Bookmark::KMaxDescriptorLength, User::Panic(Bookmark::KBookmarkErrTooLong, KErrArgument));
       
   147 
       
   148 	TInt index = iPropertyRegister.PropertyIndex(iGroup, aPropertyId, Bookmark::EDataTypeDes16);
       
   149 	if (index == KErrNotFound)
       
   150 		{
       
   151 		User::Leave(Bookmark::KErrNotRegistered);
       
   152 		}
       
   153 	UpdateValueListItemL(index, Bookmark::EDataTypeDes16, &aValue);
       
   154 	SetDirty();
       
   155 	}
       
   156 
       
   157 void CPropertyList::SetCustomPropertyL(TUid aPropertyId, const TDesC8& aValue)
       
   158 	{
       
   159 	// The aValue must be smaller that the maximim data storage size
       
   160 	__ASSERT_ALWAYS(aValue.Length() <= Bookmark::KMaxDataLength, User::Panic(Bookmark::KBookmarkErrTooLong, KErrArgument));
       
   161 
       
   162 	TInt index = iPropertyRegister.PropertyIndex(iGroup, aPropertyId, Bookmark::EDataTypeDes8);
       
   163 	if (index == KErrNotFound)
       
   164 		{
       
   165 		User::Leave(Bookmark::KErrNotRegistered);
       
   166 		}
       
   167 	UpdateValueListItemL(index, Bookmark::EDataTypeDes8, &aValue);
       
   168 	SetDirty();
       
   169 	}
       
   170 
       
   171 void CPropertyList::UpdateValueListItemL(TInt aIndex, Bookmark::TPropertyType aType, const TAny* aValue)
       
   172 	{
       
   173 	TInt lastIndex = iPropertyValues.Count() - 1;
       
   174 	if (aIndex > lastIndex)
       
   175 		{
       
   176 		// create Null values in the property list.
       
   177 		for (; lastIndex < aIndex; ++lastIndex)
       
   178 			{
       
   179 			iPropertyValues.AppendL(NULL);
       
   180 			}
       
   181 		}
       
   182 		
       
   183 	//Delete any previously allocated memory
       
   184 	delete iPropertyValues[aIndex];
       
   185 	iPropertyValues[aIndex] = NULL;	
       
   186 
       
   187 	switch (aType)
       
   188 		{
       
   189 		case Bookmark::EDataTypeInt:
       
   190 			{
       
   191 			const TInt* intPtr = static_cast<const TInt*>(aValue);
       
   192 			TInt* newInt = new (ELeave) TInt;
       
   193 			*newInt = *intPtr;
       
   194 			iPropertyValues[aIndex] = newInt;
       
   195 			break;
       
   196 			}
       
   197 		case Bookmark::EDataTypeReal:
       
   198 			{
       
   199 			const TReal* realPtr = static_cast<const TReal*>(aValue);
       
   200 			TReal* newReal = new (ELeave) TReal;
       
   201 			*newReal = *realPtr;
       
   202 			iPropertyValues[aIndex] = newReal;
       
   203 			break;
       
   204 			}
       
   205 		case Bookmark::EDataTypeDes16:
       
   206 			{
       
   207 			const TDes* buffer16 = static_cast<const TDes*>(aValue);
       
   208 			iPropertyValues[aIndex] = buffer16->AllocL();
       
   209 			break;
       
   210 			}
       
   211 		case Bookmark::EDataTypeDes8:
       
   212 			{
       
   213 			const TDes8* buffer8 = static_cast<const TDes8*>(aValue);
       
   214 			iPropertyValues[aIndex] = buffer8->AllocL();
       
   215 			break;
       
   216 			}
       
   217 		default:
       
   218 			// Should not get to here
       
   219 			User::Leave(Bookmark::KErrCorrupt);
       
   220 			break;
       
   221 		}
       
   222 	}
       
   223 
       
   224 void CPropertyList::TransNewL()
       
   225 	{
       
   226 	TransSaveL();
       
   227 	}
       
   228 
       
   229 void CPropertyList::TransSaveL()
       
   230 	{
       
   231 	RArray<TUint32> entryList;
       
   232 	TBool creating;
       
   233 	TInt* intPtr = NULL;
       
   234 	TReal* realPtr = NULL;
       
   235 	TDesC* desc16Ptr = NULL;
       
   236 	TDesC8* desc8Ptr = NULL;
       
   237 	TInt index;
       
   238 	Bookmark::TPropertyType type;
       
   239 	
       
   240 	for (index = iPropertyValues.Count() - 1; index >= 0; --index)
       
   241 		{
       
   242 		if (iPropertyValues[index])
       
   243 			{
       
   244 			if (iPropertyRegister.GetPropertyType(iGroup, index, type) != KErrNone)
       
   245 				{
       
   246 				User::Leave(Bookmark::KErrNotRegistered);
       
   247 				}
       
   248 				
       
   249 			// if the index does not yet exist in the database we need to create it.
       
   250 			creating = EFalse;
       
   251 			if (KErrNotFound == iRepository->FindL(iIndexBase + index, KRepExactMask, entryList))
       
   252 				{
       
   253 				creating = ETrue;
       
   254 				}
       
   255 			entryList.Reset();
       
   256 				
       
   257 			switch (type)
       
   258 				{
       
   259 			case Bookmark::EDataTypeInt:
       
   260 				intPtr = static_cast<TInt*>(iPropertyValues[index]);
       
   261 				if (creating)
       
   262 					{
       
   263 					iRepository->Create(iIndexBase + index, *intPtr);
       
   264 					}
       
   265 				else
       
   266 					{
       
   267 					iRepository->Set(iIndexBase + index, *intPtr);
       
   268 					}
       
   269 				break;
       
   270 			case Bookmark::EDataTypeReal:
       
   271 				realPtr = static_cast<TReal*>(iPropertyValues[index]);
       
   272 				if (creating)
       
   273 					{
       
   274 					iRepository->Create(iIndexBase + index, *realPtr);
       
   275 					}
       
   276 				else
       
   277 					{
       
   278 					iRepository->Set(iIndexBase + index, *realPtr);
       
   279 					}
       
   280 				break;
       
   281 			case Bookmark::EDataTypeDes16:
       
   282 				desc16Ptr = static_cast<TDes*>(iPropertyValues[index]);
       
   283 				if (creating)
       
   284 					{
       
   285 					iRepository->Create(iIndexBase + index, *desc16Ptr);
       
   286 					}
       
   287 				else
       
   288 					{
       
   289 					iRepository->Set(iIndexBase + index, *desc16Ptr);
       
   290 					}
       
   291 				break;
       
   292 			case Bookmark::EDataTypeDes8:
       
   293 				desc8Ptr = static_cast<TDes8*>(iPropertyValues[index]);
       
   294 				if (creating)
       
   295 					{
       
   296 					iRepository->Create(iIndexBase + index, *desc8Ptr);
       
   297 					}
       
   298 				else
       
   299 					{
       
   300 					iRepository->Set(iIndexBase + index, *desc8Ptr);
       
   301 					}
       
   302 				break;
       
   303 			default:
       
   304 				// Should not get to here
       
   305 				User::Leave(Bookmark::KErrCorrupt);
       
   306 				break;
       
   307 				}
       
   308 			}
       
   309 		}
       
   310 	}
       
   311 
       
   312 void CPropertyList::TransLoadL()
       
   313 	{
       
   314 	RArray<TUint32> entryList;
       
   315 	CleanupClosePushL ( entryList );
       
   316 	iRepository->FindL(iIndexBase, KCusPropertyMask, entryList);
       
   317 
       
   318 	TInt intValue = 0;
       
   319 	TReal realValue = 0.0;
       
   320 	HBufC* desc16Buffer = HBufC::NewLC(Bookmark::KMaxDescriptorLength);
       
   321 	TPtr desc16Ptr = desc16Buffer->Des();
       
   322 	HBufC8* desc8Buffer = HBufC8::NewLC(Bookmark::KMaxDataLength);
       
   323 	TPtr8 desc8Ptr = desc8Buffer->Des();
       
   324 
       
   325 	TInt valueIndex = 0;
       
   326 	TInt index = 0;
       
   327 	Bookmark::TPropertyType type;
       
   328 
       
   329 	TInt count = entryList.Count();
       
   330 	for (;index < count; ++index)
       
   331 		{
       
   332 		valueIndex = entryList[index] - iIndexBase;
       
   333 		if (iPropertyRegister.GetPropertyType(iGroup, valueIndex, type) != KErrNone)
       
   334 			{
       
   335 			// remove redundant entries
       
   336 			User::LeaveIfError(iRepository->Delete(entryList[index]));
       
   337 			}
       
   338 		else
       
   339 			{
       
   340 			switch (type)
       
   341 				{
       
   342 			case Bookmark::EDataTypeInt:
       
   343 				User::LeaveIfError(iRepository->Get(entryList[index], intValue));
       
   344 				UpdateValueListItemL(valueIndex, type, &intValue);
       
   345 				break;
       
   346 			case Bookmark::EDataTypeReal:
       
   347 				User::LeaveIfError(iRepository->Get(entryList[index], realValue));
       
   348 				UpdateValueListItemL(valueIndex, type, &realValue);
       
   349 				break;
       
   350 			case Bookmark::EDataTypeDes16:
       
   351 				User::LeaveIfError(iRepository->Get(entryList[index], desc16Ptr));
       
   352 				UpdateValueListItemL(valueIndex, type, &desc16Ptr);
       
   353 				break;
       
   354 			case Bookmark::EDataTypeDes8:
       
   355 				User::LeaveIfError(iRepository->Get(entryList[index], desc8Ptr));
       
   356 				UpdateValueListItemL(valueIndex, type, &desc8Ptr);
       
   357 				break;
       
   358 			default:
       
   359 				// Should not get to here
       
   360 				User::Leave(Bookmark::KErrCorrupt);
       
   361 				break;
       
   362 				}
       
   363 			}
       
   364 		}	
       
   365 	
       
   366 	CleanupStack::PopAndDestroy(desc8Buffer);
       
   367 	CleanupStack::PopAndDestroy(desc16Buffer);
       
   368 	CleanupStack::PopAndDestroy ( &entryList );
       
   369 	
       
   370 	SetClean();
       
   371 	}
       
   372 	
       
   373 void CPropertyList::TransRemoveL()
       
   374 	{
       
   375 	TInt index = iPropertyValues.Count() - 1;
       
   376 	for (;index >= 0; --index)
       
   377 		{
       
   378 		if (iPropertyValues[index])
       
   379 			{
       
   380 			iRepository->Delete(iIndexBase + index);
       
   381 			}
       
   382 		}
       
   383 	}
       
   384 
       
   385 TUint32 CPropertyList::IndexBase()
       
   386 	{
       
   387 	return iIndexBase;
       
   388 	}
       
   389 
       
   390 void CPropertyList::SetIdFromIndexBase(TUint32 aIndexBase)
       
   391 	{
       
   392 	iIndexBase = aIndexBase;
       
   393 	}