applayerpluginsandutils/bookmarksupport/src/bkmrkdb.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 // Internal class for opening and manipulating the bookmark database.
       
    15 // @internalComponent
       
    16 // 
       
    17 //
       
    18 
       
    19 #include <e32test.h>
       
    20 
       
    21 #include "bkmrk.h"
       
    22 #include "bkmrkfolder.h"
       
    23 #include "bkmrkdb.h"
       
    24 #include "repository.h"
       
    25 
       
    26 _LIT8(KEmptyIconData, "");
       
    27 
       
    28 const TInt KVersionMajor = 1;
       
    29 const TInt KVersionMinor = 0;
       
    30 const TInt KVersionBuild = 1;
       
    31 
       
    32 const TInt 	KFirstIndex	= 1;
       
    33 
       
    34 CBookmarkDb* CBookmarkDb::NewL(Bookmark::TVisibility aVisibility, MBookmarkObserver* aObserver)
       
    35 	{
       
    36 	CBookmarkDb* self = new (ELeave) CBookmarkDb(aObserver);
       
    37 	CleanupStack::PushL(self);
       
    38 	self->ConstructL(aVisibility);
       
    39 	CleanupStack::Pop(self);
       
    40 	return self;
       
    41 	}
       
    42 
       
    43 CBookmarkDb::~CBookmarkDb()
       
    44 	{
       
    45 	delete iHomeText;
       
    46 	delete iSearchUri;
       
    47 	delete iDatabaseWatcher;
       
    48 	delete iDatabaseRepository;
       
    49 	delete iFolderWatcher;
       
    50 	delete iFolderRepository;
       
    51 	delete iBookmarkWatcher;
       
    52 	delete iBookmarkRepository;
       
    53 	delete iIconWatcher;
       
    54 	delete iIconRepository;
       
    55 	Reset();
       
    56 	delete iCustomProperties;
       
    57 	delete iPropertyRegister;
       
    58 
       
    59 	delete iUnloadedFolderList;
       
    60 	}
       
    61 
       
    62 CBookmarkDb::CBookmarkDb(MBookmarkObserver* aObserver)
       
    63 	: iObserver(aObserver)
       
    64 	{
       
    65 	}
       
    66 
       
    67 void CBookmarkDb::ConstructL(Bookmark::TVisibility aVisibility)
       
    68 	{
       
    69 	RThread thread;
       
    70 	TSecurityInfo secInfo(thread);
       
    71 	TBool hasWriteUserData = secInfo.iCaps.HasCapability(ECapabilityWriteUserData);
       
    72 	if (aVisibility == Bookmark::EVisibilityManager && !hasWriteUserData)
       
    73 		{
       
    74 		User::Leave(KErrPermissionDenied);
       
    75 		}
       
    76 
       
    77 	iVisibility = (aVisibility == Bookmark::EVisibilityDefault) ? Bookmark::EVisibilityAll : aVisibility;
       
    78 
       
    79 	iDatabaseRepository = CRepository::NewL(KUidBookmarkDatabaseRepository);
       
    80 	SetRepository(*iDatabaseRepository);
       
    81 	iFolderRepository = CRepository::NewL(KUidFolderRepository);
       
    82 	iBookmarkRepository = CRepository::NewL(KUidBookmarkRepository);
       
    83 	iIconRepository = CRepository::NewL(KUidIconRepository);
       
    84 
       
    85 	iPropertyRegister = CPropertyReg::NewL();
       
    86 	iCustomProperties = CPropertyList::NewL(*iDatabaseRepository, *iPropertyRegister, CPropertyReg::EGroupDatabase, KDbCusPropertyStart);
       
    87 
       
    88 	// Create the repositories if they don't exist
       
    89 	TPckgBuf<TVersion> pckg;
       
    90 	TInt err = iDatabaseRepository->Get(KVersionIndex, pckg);
       
    91 	if (err == KErrNone)
       
    92 		{
       
    93 		TransactionL(ETransLoad);
       
    94 		iBkmrkDbInitialized = ETrue;
       
    95 		}
       
    96 	else if (err == KErrNotFound)
       
    97 		{
       
    98 		// Set all as default empty values
       
    99 		iHomeId 		= Bookmark::KNullItemID;
       
   100 		iHomeText 		= KNullDesC().AllocL();
       
   101 		iSearchUri 		= KNullDesC8().AllocL();
       
   102 		iDefaultProxy 	= 0;
       
   103 		iDefaultNap 	= 0;
       
   104 		}
       
   105 	else
       
   106 		{
       
   107 		User::Leave(err);
       
   108 		}
       
   109 
       
   110 	if (iObserver)
       
   111 		{
       
   112 		iDatabaseWatcher = new (ELeave) CBkmrkWatcher(EBkmrkWatcherDatabase, *this, *iDatabaseRepository);
       
   113 		iDatabaseWatcher->WatchL();
       
   114 		iFolderWatcher = new (ELeave) CBkmrkWatcher(EBkmrkWatcherFolder, *this, *iFolderRepository);
       
   115 		iFolderWatcher->WatchL();
       
   116 		iBookmarkWatcher = new (ELeave) CBkmrkWatcher(EBkmrkWatcherBookmark, *this, *iBookmarkRepository);
       
   117 		iBookmarkWatcher->WatchL();
       
   118 		iIconWatcher = new (ELeave) CBkmrkWatcher(EBkmrkWatcherIcon, *this, *iIconRepository);
       
   119 		iIconWatcher->WatchL();
       
   120 		}
       
   121 
       
   122 	iTreeSync = EFalse;
       
   123 	iConfigSync = EFalse;
       
   124 	iIconSync = EFalse;
       
   125 
       
   126 	iUnloadedFolderList = new (ELeave) CDesCArrayFlat(10);
       
   127 	SetClean();
       
   128 	}
       
   129 
       
   130 void CBookmarkDb::Reset()
       
   131 	{
       
   132 	ResetTree();
       
   133 	ResetIcons();
       
   134 	if (iCustomProperties)
       
   135 		{
       
   136 		iCustomProperties->Reset();
       
   137 		}
       
   138 	}
       
   139 
       
   140 void CBookmarkDb::ResetTree()
       
   141 	{
       
   142 	if (iRoot)
       
   143 		{
       
   144 		iRoot->Reset();
       
   145 		delete iRoot;
       
   146 		iRoot = NULL;
       
   147 		iTreeConstructed = EFalse;
       
   148 		}
       
   149 
       
   150 	if (iUnloadedFolderList)
       
   151 		{
       
   152 		iUnloadedFolderList->Reset();
       
   153 		}
       
   154 
       
   155 	iFolderList.Reset();
       
   156 	iBookmarkList.Reset();
       
   157 
       
   158 	for (TInt index = iDeletedList.Count() - 1; index >= 0; --index)
       
   159 		{
       
   160 		delete iDeletedList[index];
       
   161 		}
       
   162 	iDeletedList.Reset();
       
   163 	}
       
   164 
       
   165 void CBookmarkDb::ResetIcons()
       
   166 	{
       
   167 	for (TInt index = iIconList.Count() - 1; index >= 0; --index)
       
   168 		{
       
   169 		delete iIconList[index];
       
   170 		}
       
   171 	iIconList.Reset();
       
   172 
       
   173 	iIconSync = EFalse;
       
   174 	}
       
   175 
       
   176 void CBookmarkDb::RefreshL()
       
   177 	{
       
   178 	// If no repository files( no bookmark data ) exists, it is not required to
       
   179 	// refresh the bookmark view. Check whether the repository has data or not
       
   180 	// 
       
   181 	if ( HasDataInRepositoryL () )
       
   182 		{
       
   183 		TransactionL(ETransLoad);
       
   184 		ResetTree();
       
   185 		ConstructTreeL();
       
   186 		ResetIcons();
       
   187 		LoadIconsL();				
       
   188 		}		
       
   189 	}
       
   190 
       
   191 void CBookmarkDb::InitialiseRepL(CRepository& aRepository)
       
   192 	{
       
   193 	TPckgBuf<TVersion> pckg;
       
   194 	TInt err = aRepository.Get(KVersionIndex, pckg);
       
   195 	if (err == KErrNotFound)
       
   196 		{
       
   197 		TInt result = KErrLocked;
       
   198 		do
       
   199 			{
       
   200 			aRepository.StartTransaction(CRepository::EConcurrentReadWriteTransaction);
       
   201 
       
   202 			// create entries in the repository
       
   203 			TVersion version(KVersionMajor, KVersionMinor, KVersionBuild);
       
   204 			TPckgBuf<TVersion> pckg(version);
       
   205 			aRepository.Create(KVersionIndex, pckg);
       
   206 			aRepository.Create(KRepNextIndex, KFirstIndex);
       
   207 		
       
   208 			// commit the transaction
       
   209 			TUint32 failedIndex;
       
   210 			result = aRepository.CommitTransaction(failedIndex);
       
   211 			} while (result == KErrLocked);
       
   212 		User::LeaveIfError(result);
       
   213 		}
       
   214 	else if (err != KErrNone)
       
   215 		{
       
   216 		User::Leave(err);
       
   217 		}
       
   218 	}
       
   219 
       
   220 
       
   221 void CBookmarkDb::CommitL()
       
   222 	{
       
   223 	CreateDefaultRepositoriesL ();
       
   224 	
       
   225 	CRepositoryAccessor::CommitL();
       
   226 
       
   227 	iPropertyRegister->CommitL();
       
   228 	iCustomProperties->CommitL();
       
   229 
       
   230 	TInt index = iFolderList.Count() - 1;
       
   231 	for (;index >= 0; --index)
       
   232 		{
       
   233 		iFolderList[index]->CommitL();
       
   234 		}
       
   235 
       
   236 	index = iBookmarkList.Count() - 1;
       
   237 	for (;index >= 0; --index)
       
   238 		{
       
   239 		iBookmarkList[index]->CommitL();
       
   240 		}
       
   241 
       
   242 	index = iIconList.Count() - 1;
       
   243 	for (;index >= 0; --index)
       
   244 		{
       
   245 		iIconList[index]->CommitL();
       
   246 		}
       
   247 
       
   248 	index = iDeletedList.Count() - 1;
       
   249 	for (;index >= 0; --index)
       
   250 		{
       
   251 		iDeletedList[index]->CommitL();
       
   252 		delete iDeletedList[index];
       
   253 		iDeletedList[index] = NULL;
       
   254 		}
       
   255 	iDeletedList.Reset();
       
   256 	}
       
   257 
       
   258 CBookmarkFolder& CBookmarkDb::RootL()
       
   259 	{
       
   260 	ConstructTreeL();
       
   261 	return *iRoot;
       
   262 	}
       
   263 
       
   264 CBookmarkFolder* CBookmarkDb::FindFolder(const TDesC& aFolderName)
       
   265 	{
       
   266 	TInt index = iFolderList.Count() - 1;
       
   267 	for (;index >=0; --index)
       
   268 		{
       
   269 		if (iFolderList[index] != NULL &&
       
   270 			aFolderName.Compare(iFolderList[index]->Title()) == 0)
       
   271 			{
       
   272 			return static_cast<CBookmarkFolder*>(iFolderList[index]);
       
   273 			}
       
   274 		}
       
   275 
       
   276 	return NULL;
       
   277 	}
       
   278 
       
   279 CBookmark& CBookmarkDb::CreateBookmarkL(CBookmarkFolder& aParent)
       
   280 	{
       
   281 	CreateDefaultRepositoriesL ();
       
   282 	ConstructTreeL();
       
   283 	CBookmarkFolder* parent = &aParent;
       
   284 	CBookmark* bookmark = CBookmark::NewL(*parent, *this);
       
   285 	CleanupStack::PushL(bookmark);
       
   286 	bookmark->AssignIdL();
       
   287 	bookmark->SetTitleL(KNullDesC);
       
   288 	iBookmarkList.AppendL(bookmark);
       
   289 	CleanupStack::Pop(bookmark);
       
   290 
       
   291 	return *bookmark;
       
   292 	}
       
   293 
       
   294 CBookmarkFolder& CBookmarkDb::CreateFolderL(const TDesC& aFolderName, CBookmarkFolder& aParent)
       
   295 	{
       
   296 	CreateDefaultRepositoriesL ();
       
   297 	if (FindFolder(aFolderName) || FindUnloadedFolder(aFolderName))
       
   298 		{
       
   299 		User::Leave(Bookmark::KErrTitleAlreadyUsed);
       
   300 		}
       
   301 
       
   302 	ConstructTreeL();
       
   303 	CBookmarkFolder* parent = &aParent;
       
   304 	CBookmarkFolder* folder = CBookmarkFolder::NewL(parent, *this);
       
   305 	CleanupStack::PushL(folder);
       
   306 	folder->AssignIdL();
       
   307 	folder->SetTitleL(aFolderName);
       
   308 	iFolderList.AppendL(folder);
       
   309 	CleanupStack::Pop(folder);
       
   310 
       
   311 	return *folder;
       
   312 	}
       
   313 
       
   314 void CBookmarkDb::DeleteItemL(Bookmark::TItemId aItem, TBool aRecursive)
       
   315 	{
       
   316 	if (aItem == Bookmark::KRootItemID)
       
   317 		{
       
   318 		User::Leave(Bookmark::KErrOperationDenied);
       
   319 		}
       
   320 
       
   321 	CBookmarkBase* item = FindItem(aItem);
       
   322 	if (!item)
       
   323 		{
       
   324 		User::Leave(KErrNotFound);
       
   325 		}
       
   326 
       
   327   	if (item->Type() == Bookmark::ETypeFolder && !aRecursive)
       
   328 		{
       
   329 		CBookmarkFolder* folder = static_cast<CBookmarkFolder*>(item);
       
   330 		folder->SendChildrenToFolderL(*iRoot);
       
   331 		}
       
   332 
       
   333 	// Bookmark items are not deleted until the database is commited
       
   334 	// Deleted items are removed from the folder tree and go into a
       
   335 	// 'deleted' list. On a commit the deleted list is deleted from memory
       
   336 	// and from the repository.
       
   337   	item->DeleteL();
       
   338 	}
       
   339 
       
   340 void CBookmarkDb::RemoveBookmarkL(CBookmark* aBookmark)
       
   341 	{
       
   342 	TInt index = iBookmarkList.Find(aBookmark);
       
   343 	if (index != KErrNotFound)
       
   344 		{
       
   345 		if (Home() == aBookmark)
       
   346 			{
       
   347 			SetHome(NULL);
       
   348 			}
       
   349 		iBookmarkList.Remove(index);
       
   350 		iDeletedList.AppendL(aBookmark);
       
   351 		}
       
   352 	}
       
   353 
       
   354 void CBookmarkDb::RemoveFolderL(CBookmarkFolder* aFolder)
       
   355 	{
       
   356 	TInt index = iFolderList.Find(aFolder);
       
   357 	if (index != KErrNotFound)
       
   358 		{
       
   359 		iFolderList.Remove(index);
       
   360 		iDeletedList.AppendL(aFolder);
       
   361 		}
       
   362 	}
       
   363 
       
   364 const TDesC8& CBookmarkDb::IconL(Bookmark::TAttachmentId aIconId)
       
   365 	{
       
   366 	LoadIconsL();
       
   367 	TInt index = FindIcon(aIconId);
       
   368 	if (index == KErrNotFound)
       
   369 		{
       
   370 		User::Leave(KErrNotFound);
       
   371 		}
       
   372 	return iIconList[index]->Data();
       
   373 	}
       
   374 
       
   375 Bookmark::TAttachmentId CBookmarkDb::CreateIconL(const TDesC8& aData)
       
   376 	{
       
   377 	CreateDefaultRepositoriesL ();
       
   378 	
       
   379 	LoadIconsL();
       
   380 	CBkmrkAttachment* icon = CBkmrkAttachment::NewL(*iIconRepository, aData);
       
   381 	CleanupStack::PushL(icon);
       
   382 	icon->SetIdL();
       
   383 	iIconList.AppendL(icon);
       
   384 	CleanupStack::Pop(icon);
       
   385 	return icon->Id();
       
   386 	}
       
   387 
       
   388 TInt CBookmarkDb::DeleteIconL(Bookmark::TAttachmentId aIconId)
       
   389 	{
       
   390 	LoadIconsL();
       
   391 	TInt index = FindIcon(aIconId);
       
   392 	if (index == KErrNotFound)
       
   393 		{
       
   394 		return KErrNotFound;
       
   395 		}
       
   396 
       
   397 	iDeletedList.AppendL(iIconList[index]);
       
   398 	iIconList[index]->DeleteL();
       
   399 	iIconList.Remove(index);
       
   400 
       
   401 	return KErrNone;
       
   402 	}
       
   403 
       
   404 TVersion CBookmarkDb::Version()
       
   405 	{
       
   406 	TVersion version(KVersionMajor, KVersionMinor, KVersionBuild);
       
   407 	return version;
       
   408 	}
       
   409 
       
   410 Bookmark::TVisibility CBookmarkDb::Visibility()
       
   411 	{
       
   412 	return iVisibility;
       
   413 	}
       
   414 
       
   415 void CBookmarkDb::SetHome(CBookmark* aHome)
       
   416 	{
       
   417 	iHome = aHome;
       
   418 	if (iHome)
       
   419 		{
       
   420 		iHomeId = iHome->Id();
       
   421 		}
       
   422 	else
       
   423 		{
       
   424 		iHomeId = Bookmark::KNullItemID;
       
   425 		}
       
   426 	SetDirty();
       
   427 	}
       
   428 
       
   429 CBookmark* CBookmarkDb::Home()
       
   430 	{
       
   431 	return iHome;
       
   432 	}
       
   433 
       
   434 const TDesC& CBookmarkDb::HomePageText() const
       
   435 	{
       
   436 	return *iHomeText;
       
   437 	}
       
   438 
       
   439 void CBookmarkDb::SetHomePageTextL(const TDesC& aHomePageText)
       
   440 	{
       
   441 	// The aHomePageText must be smaller that the maximim descriptor storage size
       
   442 	__ASSERT_ALWAYS(aHomePageText.Length() <= Bookmark::KMaxDescriptorLength, User::Panic(Bookmark::KBookmarkErrTooLong, KErrArgument));
       
   443 
       
   444 	delete iHomeText;
       
   445 	iHomeText = NULL;
       
   446 	iHomeText = aHomePageText.AllocL();
       
   447 	SetDirty();
       
   448 	}
       
   449 
       
   450 const TDesC8& CBookmarkDb::SearchUri() const
       
   451 	{
       
   452 	return *iSearchUri;
       
   453 	}
       
   454 
       
   455 void CBookmarkDb::SetSearchUriL(const TDesC8& aUri)
       
   456 	{
       
   457 	// The aUri must be smaller that the maximim uri storage size
       
   458 	__ASSERT_ALWAYS(aUri.Length() <= Bookmark::KMaxUriLength, User::Panic(Bookmark::KBookmarkErrTooLong, KErrArgument));
       
   459 
       
   460 	delete iSearchUri;
       
   461 	iSearchUri = NULL;
       
   462 	iSearchUri = aUri.AllocL();
       
   463 	SetDirty();
       
   464 	}
       
   465 
       
   466 TUint32 CBookmarkDb::DefaultProxy() const
       
   467 	{
       
   468 	return iDefaultProxy;
       
   469 	}
       
   470 
       
   471 void CBookmarkDb::SetDefaultProxy(TUint32 aServiceId)
       
   472 	{
       
   473 	iDefaultProxy = aServiceId;
       
   474 	SetDirty();
       
   475 	}
       
   476 
       
   477 TUint32 CBookmarkDb::DefaultNap() const
       
   478 	{
       
   479 	return iDefaultNap;
       
   480 	}
       
   481 
       
   482 void CBookmarkDb::SetDefaultNap(TUint32 aNetworkId)
       
   483 	{
       
   484 	iDefaultNap = aNetworkId;
       
   485 	SetDirty();
       
   486 	}
       
   487 
       
   488 TBool CBookmarkDb::IsTreeConstructed()
       
   489 	{
       
   490 	return (iRoot != NULL && iTreeConstructed);
       
   491 	}
       
   492 
       
   493 void CBookmarkDb::ConstructTreeL()
       
   494 	{
       
   495 	if (!iRoot)
       
   496 		{
       
   497 		iRoot = CBookmarkFolder::NewL(NULL, *this);
       
   498 		iRoot->SetTitleL(Bookmark::KTxtRootTitle);
       
   499 		iRoot->SetId(Bookmark::KRootItemID);
       
   500 		}
       
   501 	if ( !IsTreeConstructed() && HasDataInRepositoryL () )
       
   502 		{
       
   503 		// Load the tree only if the bookmark db is initialized
       
   504 		// and the tree is not already constructed.
       
   505 		LoadTreeL ();			
       
   506  		}
       
   507 	}
       
   508 
       
   509 CBookmarkBase* CBookmarkDb::FindItem(Bookmark::TItemId aItemId)
       
   510 	{
       
   511 	if (aItemId == Bookmark::KRootItemID)
       
   512 		{
       
   513 		return iRoot;
       
   514 		}
       
   515 
       
   516 	CBookmarkBase* item = NULL;
       
   517 
       
   518 	TInt index = 0;
       
   519 	TInt count = 0;
       
   520 	// the top bit of the id indicates whether the id is a folder or bookmark
       
   521 	TInt topBitSet = aItemId & KFolderIdMaskID;
       
   522 	if (topBitSet == 0)
       
   523 		{
       
   524 		// item is a bookmark
       
   525 		count = iBookmarkList.Count();
       
   526 		for (index = 0; index < count; ++index)
       
   527 			{
       
   528 			if (iBookmarkList[index]->Id() == aItemId)
       
   529 				{
       
   530 				// bookmark item found
       
   531 				item = iBookmarkList[index];
       
   532 				break;
       
   533 				}
       
   534 			}
       
   535 		}
       
   536 	else
       
   537 		{
       
   538 		// item is a folder
       
   539 		Bookmark::TItemId folderId;
       
   540 		count = iFolderList.Count();
       
   541 		for (index = 0; index < count; ++index)
       
   542 			{
       
   543 			folderId = iFolderList[index]->Id();
       
   544 			if (folderId == aItemId)
       
   545 				{
       
   546 				// folder item found
       
   547 				item = iFolderList[index];
       
   548 				break;
       
   549 				}
       
   550 			}
       
   551 		}
       
   552 
       
   553 	return item;
       
   554 	}
       
   555 
       
   556 CRepository& CBookmarkDb::FolderRepository()
       
   557 	{
       
   558 	return *iFolderRepository;
       
   559 	}
       
   560 
       
   561 CRepository& CBookmarkDb::BookmarkRepository()
       
   562 	{
       
   563 	return *iBookmarkRepository;
       
   564 	}
       
   565 
       
   566 CPropertyList& CBookmarkDb::CustomProperties()
       
   567 	{
       
   568 	return *iCustomProperties;
       
   569 	}
       
   570 
       
   571 CPropertyReg& CBookmarkDb::PropertyRegister()
       
   572 	{
       
   573 	return *iPropertyRegister;
       
   574 	}
       
   575 
       
   576 void CBookmarkDb::BWONotify(TBkmrkWatcherType aType)
       
   577 	{
       
   578 	switch(aType)
       
   579 		{
       
   580 	case EBkmrkWatcherFolder:
       
   581 	case EBkmrkWatcherBookmark:
       
   582 		iObserver->MBONotify(MBookmarkObserver::EEventUpdatedBookmarkTree);
       
   583 		iTreeSync = EFalse;
       
   584 		break;
       
   585 	case EBkmrkWatcherDatabase:
       
   586 		iObserver->MBONotify(MBookmarkObserver::EEventUpdatedConfig);
       
   587 		iConfigSync = EFalse;
       
   588 		break;
       
   589 	case EBkmrkWatcherIcon:
       
   590 		iObserver->MBONotify(MBookmarkObserver::EEventUpdatedIcons);
       
   591 		iIconSync = EFalse;
       
   592 		break;
       
   593 	default:
       
   594 		break;
       
   595 		}
       
   596 	}
       
   597 
       
   598 void CBookmarkDb::LoadBookmarkListL(CRepository& aRepository, TBkmrkList& aList, Bookmark::TType aType, TBool aLoadAll)
       
   599 	{
       
   600 	RArray<TUint32> entryList;
       
   601 	CleanupClosePushL(entryList);
       
   602 	aRepository.FindL(KRepIndexFind, KRepIndexMask, entryList);
       
   603 	CBookmarkBase* newItem;
       
   604 	TInt count = entryList.Count();
       
   605 	for (TInt index = 0;index < count; ++index)
       
   606 		{
       
   607 		if (aType == Bookmark::ETypeFolder)
       
   608 			{
       
   609 			newItem = CBookmarkFolder::NewL(NULL, *this);
       
   610 			}
       
   611 		else
       
   612 			{
       
   613 			newItem = CBookmark::NewL(*iRoot, *this);
       
   614 			}
       
   615 		CleanupStack::PushL(newItem);
       
   616 		newItem->SetIdFromIndexBase(entryList[index]);
       
   617 		newItem->TransactionL(ETransLoad);
       
   618 		if (aLoadAll || CheckAccessL(*newItem))
       
   619 			{
       
   620 			aList.AppendL(newItem);
       
   621 			CleanupStack::Pop(newItem);
       
   622 			}
       
   623 		else
       
   624 			{
       
   625 			if (aType == Bookmark::ETypeFolder)
       
   626 				{
       
   627 				iUnloadedFolderList->AppendL(newItem->Title());
       
   628 				}
       
   629 			CleanupStack::PopAndDestroy(newItem);
       
   630 			}
       
   631 		}
       
   632 	CleanupStack::PopAndDestroy(&entryList);
       
   633 	}
       
   634 
       
   635 void CBookmarkDb::AddItemsToFolderTreeL(TBkmrkList& aList)
       
   636 	{
       
   637 	TInt rank = 0;
       
   638 	CBookmarkFolder* parent;
       
   639 	CBookmarkBase* item;
       
   640 	TInt count = aList.Count();
       
   641 	for (TInt index = 0; index < count; ++index)
       
   642 		{
       
   643 		item = aList[index];
       
   644 		parent = static_cast<CBookmarkFolder*>(FindItem(item->ParentId()));
       
   645 		if (!parent)
       
   646 			{
       
   647 			item->SetOrphaned(ETrue);
       
   648 			iRoot->AppendL(*item);
       
   649 			}
       
   650 		else
       
   651 			{
       
   652 			rank = item->Rank();
       
   653 			if (rank < parent->Count())
       
   654 				{
       
   655 				parent->InsertL(*item, rank);
       
   656 				}
       
   657 			else
       
   658 				{
       
   659 				parent->AppendL(*item);
       
   660 				}
       
   661 			}
       
   662 		item->SetClean();
       
   663 		}
       
   664 	}
       
   665 
       
   666 TBool CBookmarkDb::RemoveFolderIfPublicL(CBookmarkFolder& aFolder)
       
   667 	{
       
   668 	// This method removes public folders in private-only mode.
       
   669 	// If not in this mode this method should not be called
       
   670 	__ASSERT_DEBUG(iVisibility == Bookmark::EVisibilityPrivate, User::Invariant());
       
   671 
       
   672 	TBool containsPrivate = EFalse;
       
   673 	for (TInt index = aFolder.Count() - 1; index >= 0; --index)
       
   674 		{
       
   675 		if (aFolder.At(index).Type() == Bookmark::ETypeFolder)
       
   676 			{
       
   677 			if (RemoveFolderIfPublicL(static_cast<CBookmarkFolder&>(aFolder.At(index))))
       
   678 				{
       
   679 				containsPrivate = ETrue;
       
   680 				}
       
   681 			}
       
   682 		else
       
   683 			{
       
   684 			// child is a bookmark which will only exist if it is private
       
   685 			containsPrivate = ETrue;
       
   686 			}
       
   687 		}
       
   688 
       
   689 	if (&aFolder != iRoot && !containsPrivate && !CheckAccessL(aFolder))
       
   690 		{
       
   691 		// The folder is not the root, does not contain any private items and is not private itself
       
   692 		// we can remove the folder
       
   693 		TInt index = iFolderList.Find(&aFolder);
       
   694 		iFolderList.Remove(index);
       
   695 		iUnloadedFolderList->AppendL(aFolder.Title());
       
   696 		delete &aFolder;
       
   697 		}
       
   698 
       
   699 	return containsPrivate;
       
   700 	}
       
   701 
       
   702 void CBookmarkDb::LoadTreeL()
       
   703 	{
       
   704 	if (HasOpenHandles())
       
   705 		{
       
   706 		User::Leave(Bookmark::KErrOpenHandle);
       
   707 		}
       
   708 
       
   709 	iLoading = ETrue;
       
   710 
       
   711 	// If we are in private-only mode then we need to open all the folders initially.
       
   712 	// We need to include public folders that contain private bookmarks
       
   713 	TBool privateMode = (Visibility() == Bookmark::EVisibilityPrivate);
       
   714 
       
   715 	// The pointers inside the folder list will not get deleted if LoadBookmarkListL or
       
   716 	// AddItemsToFolderTreeL functions leave. Attach the folder list into TCleanupItem
       
   717 	CleanupStack::PushL ( TCleanupItem ( CBookmarkDb::ResetAndDestroyBkmrkList, &iFolderList ) );
       
   718 	// Load folders into folder list
       
   719 	LoadBookmarkListL(*iFolderRepository, iFolderList, Bookmark::ETypeFolder, privateMode);
       
   720 	AddItemsToFolderTreeL(iFolderList);
       
   721 	CleanupStack::Pop (); // Pop the cleanup item object
       
   722 	
       
   723 	// Load bookmarks into bookmark list
       
   724 	CleanupStack::PushL ( TCleanupItem ( CBookmarkDb::ResetAndDestroyBkmrkList, &iBookmarkList ) );
       
   725 	LoadBookmarkListL(*iBookmarkRepository, iBookmarkList, Bookmark::ETypeBookmark, EFalse);
       
   726 	AddItemsToFolderTreeL(iBookmarkList);
       
   727 	CleanupStack::Pop (); // Pop the cleanup item object	
       
   728 	if (privateMode)
       
   729 		{
       
   730 		// remove folders that are not private and contain no private items.
       
   731 		RemoveFolderIfPublicL(*iRoot);
       
   732 		}
       
   733 
       
   734 	SetHome(static_cast<CBookmark*>(FindItem(iHomeId)));
       
   735 
       
   736 	iLoading = EFalse;
       
   737 	iTreeSync = ETrue;
       
   738 	iTreeConstructed = ETrue;
       
   739 	}
       
   740 
       
   741 TBool CBookmarkDb::IsLoading()
       
   742 	{
       
   743 	return iLoading;
       
   744 	}
       
   745 
       
   746 void CBookmarkDb::LoadIconsL()
       
   747 	{
       
   748 	if (!iIconSync)
       
   749 		{
       
   750 		RArray<TUint32> entryList;
       
   751 		CleanupClosePushL ( entryList );
       
   752 		iIconRepository->FindL(KRepIndexFind, KRepIconIndexMask, entryList);
       
   753 		CBkmrkAttachment* newIcon;
       
   754 		TInt index = 0;
       
   755 		TInt count = entryList.Count();
       
   756 		for (;index < count; ++index)
       
   757 			{
       
   758 			newIcon = CBkmrkAttachment::NewL(*iIconRepository, KEmptyIconData);
       
   759 			CleanupStack::PushL(newIcon);
       
   760 			newIcon->SetIdFromIndexBase(entryList[index]);
       
   761 			newIcon->TransactionL(ETransLoad);
       
   762 			iIconList.AppendL(newIcon);
       
   763 			CleanupStack::Pop(newIcon);
       
   764 			newIcon->SetClean();
       
   765 			}
       
   766 		CleanupStack::PopAndDestroy ( &entryList );
       
   767 
       
   768 		iIconSync = ETrue;
       
   769 		}
       
   770 
       
   771 	}
       
   772 
       
   773 TBool CBookmarkDb::HasOpenHandles()
       
   774 	{
       
   775 	if (!iRoot)
       
   776 		{
       
   777 		return EFalse;
       
   778 		}
       
   779 
       
   780 	TInt index = iFolderList.Count() -1;
       
   781 	for (; index >= 0; --index)
       
   782 		{
       
   783 		if (iFolderList[index]->RefCount() != 0)
       
   784 			{
       
   785 			return ETrue;
       
   786 			}
       
   787 		}
       
   788 
       
   789 	index = iBookmarkList.Count() -1;
       
   790 	for (; index >= 0; --index)
       
   791 		{
       
   792 		if (iBookmarkList[index]->RefCount() != 0)
       
   793 			{
       
   794 			return ETrue;
       
   795 			}
       
   796 		}
       
   797 
       
   798 	return EFalse;
       
   799 	}
       
   800 
       
   801 TInt CBookmarkDb::FindIcon(Bookmark::TAttachmentId aIconId)
       
   802 	{
       
   803 	TInt index = iIconList.Count() - 1;
       
   804 	for (;index >= 0; --index)
       
   805 		{
       
   806 		if (iIconList[index]->Id() == aIconId)
       
   807 			{
       
   808 			break;
       
   809 			}
       
   810 		}
       
   811 	return index;
       
   812 	}
       
   813 
       
   814 TBool CBookmarkDb::FindUnloadedFolder(const TDesC& aFolderName)
       
   815 	{
       
   816 	TInt index = iUnloadedFolderList->Count() - 1;
       
   817 	for (;index >=0; --index)
       
   818 		{
       
   819 		if (aFolderName.Compare((*iUnloadedFolderList)[index]) == 0)
       
   820 			{
       
   821 			return ETrue;
       
   822 			}
       
   823 		}
       
   824 
       
   825 	return EFalse;
       
   826 	}
       
   827 
       
   828 TBool CBookmarkDb::CheckAccessL(CBookmarkBase& aItem)
       
   829 	{
       
   830 	TBool accessible = EFalse;
       
   831 
       
   832 	// Always accessible if the item is the home page
       
   833 	if (aItem.Type() == Bookmark::ETypeBookmark && aItem.Id() == iHomeId)
       
   834 	{
       
   835 		return ETrue;
       
   836 	}
       
   837 
       
   838 	TBool owned = EFalse;
       
   839 	if (!aItem.IsPublic())
       
   840 		{
       
   841 		RThread thread;
       
   842 		TSecurityInfo secInfo(thread);
       
   843 		TSecureId id;
       
   844 		aItem.GetOwnerL(id);
       
   845 		if (id == secInfo.iSecureId)
       
   846 			{
       
   847 			owned = ETrue;
       
   848 			}
       
   849 		}
       
   850 
       
   851 	switch(iVisibility)
       
   852 		{
       
   853 		case Bookmark::EVisibilityPublic:
       
   854 			accessible = aItem.IsPublic();
       
   855 			break;
       
   856 		case Bookmark::EVisibilityPrivate:
       
   857 			accessible = owned;
       
   858 			break;
       
   859 		case Bookmark::EVisibilityAll:
       
   860 			accessible = (owned || aItem.IsPublic());
       
   861 			break;
       
   862 		case Bookmark::EVisibilityManager:
       
   863 			accessible = ETrue;
       
   864 			break;
       
   865 		case Bookmark::EVisibilityDefault:
       
   866 		default:
       
   867 			// should have been assigned to one of the values above
       
   868 			// during construction so shouldn't get here
       
   869 			User::Invariant();
       
   870 			break;
       
   871 		}
       
   872 
       
   873 	return accessible;
       
   874 	}
       
   875 
       
   876 
       
   877 void CBookmarkDb::TransNewL()
       
   878 	{
       
   879 	TInt customPropertyCount = 0;
       
   880 	// create entries in the repository
       
   881 	TVersion version(KVersionMajor, KVersionMinor, KVersionBuild);
       
   882 	TPckgBuf<TVersion> pckg(version);
       
   883 	iRepository->Create(KVersionIndex, pckg);
       
   884 	iRepository->Create(KDbHomeIdIndex, iHomeId);
       
   885 	iRepository->Create(KDbHomeTextIndex, *iHomeText);
       
   886 	iRepository->Create(KDbSearchUriIndex, *iSearchUri);
       
   887 	iRepository->Create(KDbDefaultProxyIndex, iDefaultProxy);
       
   888 	iRepository->Create(KDbDefaultNapIndex, iDefaultNap);
       
   889 	iRepository->Create(KRepBkmrkPropertyStart, customPropertyCount);
       
   890 	iRepository->Create(KRepFldrPropertyStart, customPropertyCount);
       
   891 	iRepository->Create(KRepDbPropertyStart, customPropertyCount);
       
   892 	}
       
   893 
       
   894 void CBookmarkDb::TransSaveL()
       
   895 	{
       
   896 	// create entries in the repository
       
   897 	TVersion version(KVersionMajor, KVersionMinor, KVersionBuild);
       
   898 	TPckgBuf<TVersion> pckg(version);
       
   899 	iRepository->Set(KVersionIndex, pckg);
       
   900 	iRepository->Set(KDbHomeIdIndex, iHomeId);
       
   901 	iRepository->Set(KDbHomeTextIndex, *iHomeText);
       
   902 	iRepository->Set(KDbSearchUriIndex, *iSearchUri);
       
   903 	iRepository->Set(KDbDefaultProxyIndex, iDefaultProxy);
       
   904 	iRepository->Set(KDbDefaultNapIndex, iDefaultNap);
       
   905 	}
       
   906 
       
   907 void CBookmarkDb::TransLoadL()
       
   908 	{
       
   909 	TPckgBuf<TVersion> pckg;
       
   910 	User::LeaveIfError(iRepository->Get(KVersionIndex, pckg));
       
   911 	TVersion version = pckg();
       
   912 	if (version.iMajor != KVersionMajor ||
       
   913 		version.iMinor != KVersionMinor )		
       
   914 		{
       
   915 		User::Leave(Bookmark::KErrBadVersion);
       
   916 		}
       
   917 
       
   918 	User::LeaveIfError(iRepository->Get(KDbHomeIdIndex, iHomeId));
       
   919 	HBufC* homeText = HBufC::NewLC(Bookmark::KMaxDescriptorLength);
       
   920 	TPtr16 ptr = homeText->Des();
       
   921 	User::LeaveIfError(iRepository->Get(KDbHomeTextIndex, ptr));
       
   922 	SetHomePageTextL(*homeText);
       
   923 	CleanupStack::PopAndDestroy(homeText);
       
   924 
       
   925 
       
   926 	HBufC8* searchUri = HBufC8::NewLC(Bookmark::KMaxDataLength);
       
   927 	TPtr8 ptr8 = searchUri->Des();
       
   928 	User::LeaveIfError(iRepository->Get(KDbSearchUriIndex, ptr8));
       
   929 	SetSearchUriL(*searchUri);
       
   930 	CleanupStack::PopAndDestroy(searchUri);
       
   931 
       
   932 	User::LeaveIfError(iRepository->Get(KDbDefaultProxyIndex, iDefaultProxy));
       
   933 	User::LeaveIfError(iRepository->Get(KDbDefaultNapIndex, iDefaultNap));
       
   934 
       
   935 	iCustomProperties->TransLoadL();
       
   936 	SetClean();
       
   937 	iTreeSync = ETrue;
       
   938 	}
       
   939 
       
   940 void CBookmarkDb::TransRemoveL()
       
   941 	{
       
   942 	iDatabaseRepository->Delete(KVersionIndex);
       
   943 	iDatabaseRepository->Delete(KDbHomeIdIndex);
       
   944 	iDatabaseRepository->Delete(KDbHomeTextIndex);
       
   945 	iDatabaseRepository->Delete(KDbSearchUriIndex);
       
   946 	iDatabaseRepository->Delete(KDbDefaultProxyIndex);
       
   947 	iDatabaseRepository->Delete(KDbDefaultNapIndex);
       
   948 	iDatabaseRepository->Delete(KRepBkmrkPropertyStart);
       
   949 	iDatabaseRepository->Delete(KRepFldrPropertyStart);
       
   950 	iDatabaseRepository->Delete(KRepDbPropertyStart);
       
   951 	}
       
   952 
       
   953 TUint32 CBookmarkDb::IndexBase()
       
   954 	{
       
   955 	return KIndexBaseZero;
       
   956 	}
       
   957 
       
   958 void CBookmarkDb::SetIdFromIndexBase(TUint32 /*aIndexBase*/)
       
   959 	{
       
   960 	// the id and index base is always 0 so do nothing
       
   961 	}
       
   962 
       
   963 void CBookmarkDb::ResetAndDestroyBkmrkList ( TAny* aPtr )
       
   964 	{
       
   965 	TBkmrkList& bkList = *( TBkmrkList* )aPtr;
       
   966  	
       
   967 	// Delete all items added. Otherwise these items will not get deleted and there will
       
   968 	// be a memory leak.
       
   969 	for ( TInt index = bkList.Count() - 1; index >= 0; --index )
       
   970 		{
       
   971 		delete bkList[ index ];			
       
   972 		}
       
   973 	bkList.Reset ();	
       
   974 	}
       
   975 
       
   976 void CBookmarkDb::CreateDefaultRepositoriesL ()
       
   977 	{
       
   978 	if ( !iBkmrkDbInitialized )
       
   979 		{		
       
   980 		TransactionL ( ETransNew );		
       
   981 		InitialiseRepL ( *iFolderRepository );
       
   982 		InitialiseRepL ( *iBookmarkRepository );
       
   983 		InitialiseRepL ( *iIconRepository );		
       
   984 		iBkmrkDbInitialized = ETrue;		
       
   985 		}
       
   986 	}
       
   987 
       
   988 TBool CBookmarkDb::HasDataInRepositoryL ()
       
   989 	{	
       
   990 	if ( !iBkmrkDbInitialized )
       
   991 		{
       
   992 		const TUint32 KNullKey = 0x00000000;
       
   993 		RArray<TUint32> foundKeys;
       
   994 		CleanupClosePushL ( foundKeys );	
       
   995 		iDatabaseRepository->FindL ( KNullKey, KNullKey, foundKeys );	
       
   996 		if ( foundKeys.Count () > 0 )
       
   997 			{
       
   998 			// Repository has data.
       
   999 			iBkmrkDbInitialized = ETrue;			
       
  1000 			}	
       
  1001 		CleanupStack::PopAndDestroy (); // foundKeys		
       
  1002 		}	
       
  1003 	return iBkmrkDbInitialized;
       
  1004 	}
       
  1005 	
       
  1006 #ifndef EKA2
       
  1007 GLDEF_C TInt E32Dll(TDllReason /*aReason*/)
       
  1008 	{
       
  1009 	return(KErrNone);
       
  1010 	}
       
  1011 #endif // EKA2