applayerpluginsandutils/bookmarksupport/src/propertyreg.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 // Classes for registering custom properties.
       
    15 // @internalComponent
       
    16 // 
       
    17 //
       
    18 
       
    19 #include "propertyreg.h"
       
    20 
       
    21 CPropertyReg* CPropertyReg::NewL()
       
    22 	{
       
    23 	CPropertyReg* self = new (ELeave) CPropertyReg();
       
    24 	CleanupStack::PushL(self);
       
    25 	self->ConstructL();
       
    26 	CleanupStack::Pop(self);
       
    27 	return self;
       
    28 	}
       
    29 	
       
    30 CPropertyReg::CPropertyReg()
       
    31 	{
       
    32 	}
       
    33 
       
    34 CPropertyReg::~CPropertyReg()
       
    35 	{
       
    36 	Reset();
       
    37 	delete iDatabaseRepository;
       
    38 	}
       
    39 	
       
    40 void CPropertyReg::Reset()
       
    41 	{
       
    42 	TInt index = iDatabaseProperties.Count() - 1;
       
    43 	for (;index >= 0; --index)
       
    44 		{
       
    45 		delete iDatabaseProperties[index];
       
    46 		}
       
    47 	iDatabaseProperties.Reset();
       
    48 	
       
    49 	index = iFolderProperties.Count() - 1;
       
    50 	for (;index >= 0; --index)
       
    51 		{
       
    52 		delete iFolderProperties[index];
       
    53 		}
       
    54 	iFolderProperties.Reset();
       
    55 	
       
    56 	index = iBookmarkProperties.Count() - 1;
       
    57 	for (;index >= 0; --index)
       
    58 		{
       
    59 		delete iBookmarkProperties[index];
       
    60 		}
       
    61 	iBookmarkProperties.Reset();
       
    62 
       
    63 	index = iDeletedList.Count() - 1;
       
    64 	for (;index >= 0; --index)
       
    65 		{
       
    66 		delete iDeletedList[index];
       
    67 		}
       
    68 	iDeletedList.Reset();
       
    69 	}
       
    70 	
       
    71 void CPropertyReg::ConstructL()
       
    72 	{
       
    73 	iDatabaseRepository = CRepository::NewL(KUidBookmarkDatabaseRepository);
       
    74 	LoadPropertiesL();
       
    75 	}
       
    76 	
       
    77 void CPropertyReg::RefreshL()
       
    78 	{
       
    79 	Reset();
       
    80 	LoadPropertiesL();
       
    81 	}
       
    82 	
       
    83 void CPropertyReg::CommitL()
       
    84 	{
       
    85 	TInt index = iDatabaseProperties.Count() - 1;
       
    86 	for (;index >= 0; --index)
       
    87 		{
       
    88 		if (iDatabaseProperties[index])
       
    89 			{
       
    90 			iDatabaseProperties[index]->CommitL();
       
    91 			}
       
    92 		}
       
    93 		
       
    94 	index = iFolderProperties.Count() - 1;
       
    95 	for (;index >= 0; --index)
       
    96 		{
       
    97 		if (iFolderProperties[index])
       
    98 			{
       
    99 			iFolderProperties[index]->CommitL();
       
   100 			}
       
   101 		}
       
   102 		
       
   103 	index = iBookmarkProperties.Count() - 1;
       
   104 	for (;index >= 0; --index)
       
   105 		{
       
   106 		if (iBookmarkProperties[index])
       
   107 			{
       
   108 			iBookmarkProperties[index]->CommitL();
       
   109 			}
       
   110 		}
       
   111 		
       
   112 	index = iDeletedList.Count() - 1;
       
   113 	for (;index >= 0; --index)
       
   114 		{
       
   115 		iDeletedList[index]->CommitL();
       
   116 		delete iDeletedList[index];
       
   117 		}
       
   118 	iDeletedList.Reset();
       
   119 	}
       
   120 
       
   121 TUint32 CPropertyReg::AssignIdL(TPropertyGroup aGroup)
       
   122 	{
       
   123 	TUint32 nextEntryId = 0;
       
   124 	switch(aGroup)
       
   125 		{
       
   126 	case EGroupDatabase:
       
   127 		nextEntryId = KRepDbPropertyStart;
       
   128 		break;
       
   129 	case EGroupFolder:
       
   130 		nextEntryId = KRepFldrPropertyStart;
       
   131 		break;
       
   132 	case EGroupBookmark:
       
   133 		nextEntryId = KRepBkmrkPropertyStart;
       
   134 		break;
       
   135 	default:
       
   136 		// not a TPropertyGroup value so should not get to here
       
   137 		User::Invariant();	
       
   138 		break;
       
   139 		}
       
   140 		
       
   141 	TInt nextIndex = 0;
       
   142 	User::LeaveIfError(iDatabaseRepository->Get(nextEntryId, nextIndex));
       
   143 		if (nextIndex >= Bookmark::KMaxCustomProperties)
       
   144 		{
       
   145 		User::Leave(Bookmark::KErrNoMoreSpace);
       
   146 		}
       
   147 	
       
   148 	// The repository index for this property will be the property type start position
       
   149 	// plus 1 plus the newly assigned index value. The plus one is because the the start
       
   150 	// position index holds the next index that can be assigned.
       
   151 	TUint32 entryIndex = (nextEntryId + 1) + nextIndex;
       
   152 	++nextIndex;
       
   153 	User::LeaveIfError(iDatabaseRepository->Set(nextEntryId, nextIndex));
       
   154 	
       
   155 	return entryIndex;
       
   156 	}
       
   157 
       
   158 void CPropertyReg::RegisterPropertyL(TPropertyGroup aGroup, TUid aCustomId, Bookmark::TPropertyType aDataType)
       
   159 	{
       
   160 	TPropertyList& list = List(aGroup);
       
   161 	if (FindProperty(list, aCustomId) != KErrNotFound)
       
   162 		{
       
   163 		User::Leave(Bookmark::KErrUidAlreadyUsed);
       
   164 		}
       
   165 
       
   166 	CCustomProperty* property = CCustomProperty::NewL(*iDatabaseRepository);
       
   167 	CleanupStack::PushL(property);
       
   168 	TUint32 newEntryId = AssignIdL(aGroup);		
       
   169 	property->SetIdFromIndexBase(newEntryId);
       
   170 	property->SetUid(aCustomId);
       
   171 	property->SetType(aDataType);
       
   172 	list.AppendL(property);
       
   173 	CleanupStack::Pop(property);
       
   174 	}
       
   175 
       
   176 TInt CPropertyReg::DeregisterPropertyL(TPropertyGroup aGroup, TUid aCustomId)
       
   177 	{
       
   178 	TPropertyList& list = List(aGroup);
       
   179 	TInt index = FindProperty(list, aCustomId);
       
   180 	if (index == KErrNotFound)
       
   181 		{
       
   182 		return KErrNotFound;
       
   183 		}
       
   184 	
       
   185 	list[index]->DeleteL();
       
   186 	iDeletedList.AppendL(list[index]);
       
   187 	list.Remove(index);
       
   188 	
       
   189 	return KErrNone;
       
   190 	}
       
   191 
       
   192 TInt CPropertyReg::PropertyIndex(TPropertyGroup aGroup, TUid aPropertyId, Bookmark::TPropertyType aType)
       
   193 	{
       
   194 	TPropertyList& list = List(aGroup);
       
   195 	TInt index = FindProperty(list, aPropertyId);
       
   196 	if (index != KErrNotFound)
       
   197 		{
       
   198 		if (list[index]->Type() != aType)
       
   199 			{
       
   200 			index = KErrNotFound;
       
   201 			}
       
   202 		}
       
   203 	return index;
       
   204 	}
       
   205 
       
   206 TInt CPropertyReg::GetPropertyType(TPropertyGroup aGroup, TInt aPropertyIndex, Bookmark::TPropertyType& aType)
       
   207 	{
       
   208 	TInt error = KErrNone;
       
   209 	TPropertyList& list = List(aGroup);
       
   210 	if (aPropertyIndex >= list.Count() || !list[aPropertyIndex])
       
   211 		{
       
   212 		error = Bookmark::KErrNotRegistered;
       
   213 		}
       
   214 	else
       
   215 		{
       
   216 		aType = list[aPropertyIndex]->Type();
       
   217 		}
       
   218 		
       
   219 	return error;
       
   220 	}
       
   221 	
       
   222 TInt CPropertyReg::FindProperty(TPropertyList& aList, TUid aPropertyId)
       
   223 	{
       
   224 	TInt index = aList.Count() -1;
       
   225 	for (;index >= 0; --index)
       
   226 		{
       
   227 		if (aList[index] && aList[index]->Uid() == aPropertyId)
       
   228 			{
       
   229 			break;
       
   230 			}
       
   231 		}
       
   232 		
       
   233 	return index;
       
   234 	}
       
   235 	
       
   236 TPropertyList& CPropertyReg::List(TPropertyGroup aGroup)
       
   237 	{
       
   238 	TPropertyList* list = NULL;
       
   239 
       
   240 	switch(aGroup)
       
   241 		{
       
   242 	case EGroupDatabase:
       
   243 		list = &iDatabaseProperties;
       
   244 		break;
       
   245 	case EGroupFolder:
       
   246 		list = &iFolderProperties;
       
   247 		break;
       
   248 	case EGroupBookmark:
       
   249 		list = &iBookmarkProperties;
       
   250 		break;
       
   251 	default:
       
   252 		// not a TPropertyGroup value so should not get to here
       
   253 		User::Invariant();	
       
   254 		break;
       
   255 		}
       
   256 		
       
   257 	return *list;
       
   258 	}
       
   259 
       
   260 
       
   261 void CPropertyReg::LoadPropertyListL(TPropertyList& aList, TUint32 aPropertyStart)
       
   262 	{
       
   263 	RArray<TUint32> entryList;
       
   264 	CleanupClosePushL(entryList);
       
   265 	iDatabaseRepository->FindL(aPropertyStart, KRepPropertyMask, entryList);	
       
   266 	
       
   267 	TInt count = 0;
       
   268 	iDatabaseRepository->Get(aPropertyStart, count);
       
   269 	count++;
       
   270 	
       
   271 	CCustomProperty* newProperty;
       
   272 	
       
   273 	TInt listEntry = 1;
       
   274 	for (TInt index = 1; index < count; ++index)
       
   275 		{
       
   276 		if (entryList.Count() > 1 && entryList[listEntry] == aPropertyStart + index)
       
   277 			{
       
   278 			newProperty = CCustomProperty::NewL(*iDatabaseRepository);
       
   279 			CleanupStack::PushL(newProperty);
       
   280 			newProperty->SetIdFromIndexBase(entryList[listEntry]);
       
   281 			newProperty->TransactionL(CRepositoryAccessor::ETransLoad);
       
   282 			aList.AppendL(newProperty);
       
   283 			CleanupStack::Pop(newProperty);
       
   284 			listEntry++;
       
   285 			}
       
   286 		else
       
   287 			{
       
   288 			aList.AppendL(NULL);
       
   289 			}
       
   290 		}
       
   291 	CleanupStack::PopAndDestroy(&entryList);
       
   292 	}
       
   293 
       
   294 
       
   295 void CPropertyReg::LoadPropertiesL()
       
   296 	{
       
   297 	LoadPropertyListL(iDatabaseProperties, KRepDbPropertyStart);
       
   298 	LoadPropertyListL(iFolderProperties, KRepFldrPropertyStart);
       
   299 	LoadPropertyListL(iBookmarkProperties, KRepBkmrkPropertyStart);
       
   300 	}
       
   301 
       
   302