applayerpluginsandutils/bookmarksupport/src/bkmrkfolder.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 manipulating the bookmark folders.
       
    15 // @internalComponent
       
    16 // 
       
    17 //
       
    18 
       
    19 #include "bkmrk.h"
       
    20 #include "bkmrkfolder.h"
       
    21 #include "bkmrkdb.h"
       
    22 #include "repository.h"
       
    23 
       
    24 CBookmarkFolder* CBookmarkFolder::NewL(CBookmarkFolder* aParent, CBookmarkDb& aDb)
       
    25 	{
       
    26 	CBookmarkFolder* self = new (ELeave) CBookmarkFolder(aDb);
       
    27 	CleanupStack::PushL(self);
       
    28 	self->ConstructL(aParent);
       
    29 	CleanupStack::Pop(self);
       
    30 	return self;
       
    31 	}
       
    32 
       
    33 CBookmarkFolder::CBookmarkFolder(CBookmarkDb& aDb)
       
    34 	: CBookmarkBase(aDb, aDb.FolderRepository())
       
    35 	{
       
    36 	}
       
    37 
       
    38 CBookmarkFolder::~CBookmarkFolder()
       
    39 	{
       
    40 	Reset();
       
    41 
       
    42 	delete iTitle;
       
    43 	delete iProperties;
       
    44 	}
       
    45 
       
    46 void CBookmarkFolder::DeleteL()
       
    47 	{
       
    48 	TInt index = iChildren.Count() - 1;
       
    49 	for (;index >= 0; --index)
       
    50 		{
       
    51 		// there should be no open handles to any bookmarks
       
    52 		__ASSERT_ALWAYS(iChildren[index]->RefCount() == 0,
       
    53 						User::Panic(Bookmark::KBookmarkErrOpenHandles, KErrGeneral));
       
    54 		iChildren[index]->DeleteL();
       
    55 		}
       
    56 	iChildren.Reset();
       
    57 
       
    58 	iDatabase.RemoveFolderL(this);
       
    59   	iParent->Remove(*this);
       
    60   	iParent = NULL;
       
    61 
       
    62 	iStatus = EStatusDeleted;
       
    63 	}
       
    64 
       
    65 void CBookmarkFolder::Reset()
       
    66 	{
       
    67 	TInt index = iChildren.Count() - 1;
       
    68 	for (;index >= 0; --index)
       
    69 		{
       
    70 		// there should be no open handles to any bookmarks
       
    71 		__ASSERT_ALWAYS(iChildren[index]->RefCount() == 0,
       
    72 						User::Panic(Bookmark::KBookmarkErrOpenHandles, KErrGeneral));
       
    73 		delete iChildren[index];
       
    74 		}
       
    75 	iChildren.Reset();
       
    76 	}
       
    77 
       
    78 void CBookmarkFolder::ConstructL(CBookmarkFolder* aParent)
       
    79 	{
       
    80 	if (aParent)
       
    81 		{
       
    82 		aParent->AppendL(*this);
       
    83 		}
       
    84 	}
       
    85 
       
    86 Bookmark::TType CBookmarkFolder::Type() const
       
    87 	{
       
    88 	return Bookmark::ETypeFolder;
       
    89 	}
       
    90 
       
    91 RBkFolder CBookmarkFolder::OpenFolder()
       
    92 	{
       
    93 	RBkFolder folder;
       
    94 	folder.SetItem(*this);
       
    95 	return folder;
       
    96 	}
       
    97 
       
    98 CBkmrkProperties& CBookmarkFolder::BkmrkPropertiesL()
       
    99 	{
       
   100 	if (!iProperties)
       
   101 		{
       
   102 		iProperties = CBkmrkProperties::NewL(*this, iDatabase, *iRepository);
       
   103 		if (iStatus != EStatusCreating)
       
   104 			{
       
   105 			iProperties->TransactionL(ETransLoad);
       
   106 			}
       
   107 		}
       
   108 
       
   109 	return *iProperties;
       
   110 	}
       
   111 
       
   112 void CBookmarkFolder::SendChildrenToFolderL(CBookmarkFolder& aFolder)
       
   113 	{
       
   114 	TInt index = iChildren.Count() - 1;
       
   115 	for (;index >= 0; --index)
       
   116 		{
       
   117 		aFolder.AppendL(*(iChildren[index]));
       
   118 		}
       
   119 	iChildren.Reset();
       
   120 	}
       
   121 
       
   122 void CBookmarkFolder::AppendL(CBookmarkBase& aBookmarkItem)
       
   123 	{
       
   124 	LeaveIfNotWritableL();
       
   125 	CBookmarkFolder* parent = aBookmarkItem.Parent();
       
   126 	aBookmarkItem.SetParentL(*this);
       
   127 	if (parent)
       
   128 		{
       
   129 		parent->Remove(aBookmarkItem);
       
   130 		}
       
   131 	iChildren.AppendL(&aBookmarkItem);
       
   132 	aBookmarkItem.SetDirty();
       
   133 	}
       
   134 
       
   135 void CBookmarkFolder::InsertL(CBookmarkBase& aBookmarkItem, TInt aPosition)
       
   136 	{
       
   137 	LeaveIfNotWritableL();
       
   138 	CBookmarkFolder* parent = aBookmarkItem.Parent();
       
   139 
       
   140 	if (parent == this)
       
   141 		{
       
   142 		if (Index(aBookmarkItem) == aPosition)
       
   143 			{
       
   144 			// If the item is already in the right position in this folder
       
   145 			// then there is nothing to do.
       
   146 			return;
       
   147 			}
       
   148 		}
       
   149 	else
       
   150 		{
       
   151 		aBookmarkItem.SetParentL(*this);
       
   152 		}
       
   153 
       
   154 	if (parent)
       
   155 		{
       
   156 		parent->Remove(aBookmarkItem);
       
   157 		}
       
   158 	iChildren.InsertL(&aBookmarkItem, aPosition);
       
   159 	aBookmarkItem.SetDirty();
       
   160 
       
   161 	// This changes the rank of all children after the insertion. Touch each
       
   162 	// child so that this is saved on a commit
       
   163 	TInt index = iChildren.Count() - 1;
       
   164 	for (;index >= aPosition; --index)
       
   165 		{
       
   166 		if (!iChildren[index]->IsOrphaned())
       
   167 			{
       
   168 			iChildren[index]->SetDirty();
       
   169 			}
       
   170 		}
       
   171 	}
       
   172 
       
   173 TInt CBookmarkFolder::Move(TInt aOldPosition, TInt aNewPosition)
       
   174 	{
       
   175 	TRAPD( err, LeaveIfNotWritableL() );
       
   176 	if (err != KErrNone)
       
   177 		{
       
   178 		return err;
       
   179 		}
       
   180 	TInt maxIndex = iChildren.Count() -1;
       
   181 	if (aOldPosition < 0 || aOldPosition > maxIndex ||
       
   182 		aNewPosition < 0 || aNewPosition > maxIndex)
       
   183 		{
       
   184 		return KErrArgument;
       
   185 		}
       
   186 	if (iChildren[aOldPosition]->IsOrphaned())
       
   187 		{
       
   188 		return Bookmark::KErrOrphanedItem;
       
   189 		}
       
   190 	CBookmarkBase* item = iChildren[aOldPosition];
       
   191 	iChildren.Remove(aOldPosition);
       
   192 	iChildren.Insert(item, aNewPosition);
       
   193 	return KErrNone;
       
   194 	}
       
   195 
       
   196 TInt CBookmarkFolder::Index(CBookmarkBase& aItem) const
       
   197 	{
       
   198 	return iChildren.Find(&aItem);
       
   199 	}
       
   200 
       
   201 TInt CBookmarkFolder::Find(Bookmark::TItemId aId) const
       
   202 	{
       
   203 	TInt index = iChildren.Count() - 1;
       
   204 	for (; index >= 0; --index)
       
   205 		{
       
   206 		if (iChildren[index]->Id() == aId)
       
   207 			{
       
   208 			break;
       
   209 			}
       
   210 		}
       
   211 	return index;
       
   212 	}
       
   213 
       
   214 TInt CBookmarkFolder::Count() const
       
   215 	{
       
   216 	return iChildren.Count();
       
   217 	}
       
   218 
       
   219 CBookmarkBase& CBookmarkFolder::At(TInt aIndex)
       
   220 	{
       
   221 	return *(iChildren[aIndex]);
       
   222 	}
       
   223 
       
   224 void CBookmarkFolder::Remove(CBookmarkBase& aBookmarkItem)
       
   225 	{
       
   226 	TInt index = iChildren.Find(&aBookmarkItem);
       
   227 	if (index != KErrNotFound)
       
   228 		{
       
   229 		iChildren.Remove(index);
       
   230 		}
       
   231 	}
       
   232 
       
   233 TUint32 CBookmarkFolder::IndexBase()
       
   234 	{
       
   235 	// Folder ids have the top bit set. This ensures that bookmark ids and folder ids don't
       
   236 	// clash. This bit needs to be removed to get the actual index value 
       
   237 	TUint32 indexBase = iId & ~KFolderIdMaskID;
       
   238 	return (indexBase << KBookmarkIndexShift);
       
   239 	}
       
   240 
       
   241 void CBookmarkFolder::SetIdFromIndexBase(TUint32 aIndexBase)
       
   242 	{
       
   243 	iId = aIndexBase >> KBookmarkIndexShift;
       
   244 	iId |= KFolderIdMaskID;
       
   245 	}
       
   246 
       
   247 void CBookmarkFolder::TransNewL()
       
   248 	{
       
   249 	CBookmarkBase::TransNewL();
       
   250 	BkmrkPropertiesL().TransNewL();
       
   251 	}
       
   252 
       
   253 void CBookmarkFolder::TransSaveL()
       
   254 	{
       
   255 	CBookmarkBase::TransSaveL();
       
   256 	if (iProperties)
       
   257 		{
       
   258 		BkmrkPropertiesL().TransSaveL();
       
   259 		}
       
   260 	}
       
   261 
       
   262 void CBookmarkFolder::TransLoadL()
       
   263 	{
       
   264 	CBookmarkBase::TransLoadL();
       
   265 	if (iProperties)
       
   266 		{
       
   267 		BkmrkPropertiesL().TransLoadL();
       
   268 		}
       
   269 	SetClean();
       
   270 	}
       
   271 
       
   272 void CBookmarkFolder::TransRemoveL()
       
   273 	{
       
   274 	CBookmarkBase::TransRemoveL();
       
   275 
       
   276 	// ensure extended properties are loaded
       
   277 	BkmrkPropertiesL();
       
   278 	iProperties->TransRemoveL();
       
   279 	}
       
   280