applayerpluginsandutils/bookmarksupport/src/bkmrkbase.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 the bookmark item base class.
       
    15 // @internalComponent
       
    16 // 
       
    17 //
       
    18 
       
    19 //#include <bookmarkdef.h>
       
    20 #include "bkmrk.h"
       
    21 #include "bkmrkfolder.h"
       
    22 
       
    23 CBookmarkBase::CBookmarkBase(CBookmarkDb& aDb, CRepository& aRepository)
       
    24 	: CRepositoryAccessor(), iDatabase(aDb), iRefCount(0), iId(Bookmark::KNullItemID), iParent(NULL), iFlags(EFlagPublic|EFlagWritable)
       
    25 	{
       
    26 	SetRepository(aRepository);
       
    27 	iStatus = EStatusCreating;
       
    28 	}
       
    29 
       
    30 CBookmarkBase::~CBookmarkBase()
       
    31 	{
       
    32 	if (iParent)
       
    33 		{
       
    34 		iParent->Remove(*this);
       
    35 		}
       
    36 	}
       
    37 
       
    38 Bookmark::TItemId CBookmarkBase::Id()
       
    39 	{
       
    40 	return iId;
       
    41 	}
       
    42 
       
    43 void CBookmarkBase::AssignIdL()
       
    44 	{
       
    45 	iId = NextIndexL();
       
    46 	if (Type() == Bookmark::ETypeFolder)
       
    47 		{
       
    48 		iId |= KFolderIdMaskID;
       
    49 		}
       
    50 	}
       
    51 
       
    52 void CBookmarkBase::SetId(Bookmark::TItemId aId)
       
    53 	{
       
    54 	iId = aId;
       
    55 	}
       
    56 
       
    57 RBkNode CBookmarkBase::OpenItemL()
       
    58 	{
       
    59 	RBkNode item;
       
    60 	item.SetItem(*this);
       
    61 	return item;
       
    62 	}
       
    63 
       
    64 void CBookmarkBase::SetDirty()
       
    65 	{
       
    66 	if (!iDatabase.IsLoading())
       
    67 		{
       
    68 		CRepositoryAccessor::SetDirty();
       
    69 		}
       
    70 	}
       
    71 
       
    72 Bookmark::TItemId CBookmarkBase::ParentId() const
       
    73 	{
       
    74 	return iParentId;
       
    75 	}
       
    76 
       
    77 CBookmarkFolder* CBookmarkBase::Parent() const
       
    78 	{
       
    79 	return iParent;
       
    80 	}
       
    81 
       
    82 // Should not be used. Use CBookmarkFolder::AppendL or InsertL
       
    83 // to an item to another folders.
       
    84 void CBookmarkBase::SetParentL(CBookmarkFolder& aNewParent)
       
    85 	{
       
    86 	// No item should set it's parent to itself
       
    87 	__ASSERT_ALWAYS(&aNewParent != this, User::Panic(Bookmark::KBookmarkErrParent, Bookmark::KErrCyclicLoop));
       
    88 	LeaveIfNotWritableL();
       
    89 
       
    90 	// Ensure we're not trying to set the parent of an item to a folder it owns.
       
    91 	if (this->Type() == Bookmark::ETypeFolder &&
       
    92 		aNewParent.IsAChild(static_cast<CBookmarkFolder*>(this)))
       
    93 		{
       
    94 		User::Leave(Bookmark::KErrCyclicLoop);
       
    95 		}
       
    96 
       
    97 	iParent = &aNewParent;
       
    98 	iParentId = aNewParent.Id();
       
    99 	SetDirty();
       
   100 	}
       
   101 
       
   102 void CBookmarkBase::SetTitleL(const TDesC& aTitle)
       
   103 	{
       
   104 	// The aTitle must be smaller that the maximim descriptor storage size
       
   105 	__ASSERT_ALWAYS(aTitle.Length() <= Bookmark::KMaxDescriptorLength, User::Panic(Bookmark::KBookmarkErrTooLong, KErrArgument));
       
   106 	
       
   107 	if (Type() == Bookmark::ETypeFolder && 
       
   108 		(iDatabase.FindFolder(aTitle) != NULL || 
       
   109 		iDatabase.FindUnloadedFolder(aTitle) != NULL))
       
   110 		{
       
   111 		User::Leave(Bookmark::KErrTitleAlreadyUsed);
       
   112 		}
       
   113 
       
   114 	LeaveIfNotWritableL();
       
   115 	if (iTitle)
       
   116 		{
       
   117 		delete iTitle;
       
   118 		}
       
   119 
       
   120 	iTitle = NULL;
       
   121 	iTitle = aTitle.AllocL();
       
   122 	SetDirty();
       
   123 	}
       
   124 
       
   125 const TDesC& CBookmarkBase::Title() const
       
   126 	{
       
   127 	if ( !iTitle )
       
   128 		{
       
   129 		return KNullDesC;
       
   130 		}
       
   131  	return *iTitle;
       
   132 	}
       
   133 
       
   134 void CBookmarkBase::IncRefCount()
       
   135 	{
       
   136 	++iRefCount;
       
   137 	}
       
   138 
       
   139 void CBookmarkBase::DecRefCount()
       
   140 	{
       
   141 	__ASSERT_ALWAYS(iRefCount > 0, User::Panic(Bookmark::KBookmarkErrNegativeRefCount, KErrGeneral));
       
   142 	--iRefCount;
       
   143 	}
       
   144 
       
   145 TUint CBookmarkBase::RefCount()
       
   146 	{
       
   147 	return iRefCount;
       
   148 	}
       
   149 
       
   150 TBool CBookmarkBase::IsPublic()
       
   151 	{
       
   152 	return iFlags & EFlagPublic;
       
   153 	}
       
   154 
       
   155 void CBookmarkBase::SetPublicL(TBool aPublic)
       
   156 	{
       
   157 	LeaveIfNotWritableL();
       
   158 	
       
   159 	// The home page cannot be set as private
       
   160 	if (!aPublic &&
       
   161 		Type() == Bookmark::ETypeBookmark &&		
       
   162 		static_cast<CBookmark*>(this)->IsHomePage())
       
   163 		{
       
   164 		User::Leave(Bookmark::KErrOperationDenied);
       
   165 		}
       
   166 	
       
   167 	// You can't change the public-ness of an item in public only and private only modes.
       
   168 	if (iDatabase.Visibility() == Bookmark::EVisibilityPublic || iDatabase.Visibility() == Bookmark::EVisibilityPrivate)
       
   169 		{
       
   170 		User::Leave(KErrPermissionDenied);
       
   171 		}
       
   172 		
       
   173 	if (aPublic)
       
   174 		{
       
   175 		iFlags = iFlags | EFlagPublic;
       
   176 		}
       
   177 	else
       
   178 		{
       
   179 		RThread thread;
       
   180 		TSecurityInfo secInfo(thread);
       
   181 		SetOwnerL(secInfo.iSecureId);
       
   182 		}
       
   183 	SetDirty();
       
   184 	}
       
   185 
       
   186 TBool CBookmarkBase::IsWritable()
       
   187 	{
       
   188 	return iFlags & EFlagWritable;
       
   189 	}
       
   190 
       
   191 void CBookmarkBase::SetWritableL(TBool aWritable)
       
   192 	{
       
   193 	if (aWritable)
       
   194 		{
       
   195 		LeaveIfNotInManagerModeL ();
       
   196 		iFlags = iFlags | EFlagWritable;
       
   197 		}
       
   198 	else
       
   199 		{
       
   200 		iFlags = iFlags & ~EFlagWritable;
       
   201 		}
       
   202 	SetDirty();
       
   203 	}
       
   204 
       
   205 TBool CBookmarkBase::IsOrphaned()
       
   206 	{
       
   207 	return iFlags & EFlagOrphaned;
       
   208 	}
       
   209 
       
   210 void CBookmarkBase::SetOrphaned(TBool aOrphaned)
       
   211 	{
       
   212 	if (aOrphaned)
       
   213 		{
       
   214 		iFlags = iFlags | EFlagOrphaned;
       
   215 		}
       
   216 	else
       
   217 		{
       
   218 		iFlags = iFlags & ~EFlagOrphaned;
       
   219 		}
       
   220 	}
       
   221 
       
   222 void CBookmarkBase::GetOwnerL(TSecureId& aOwner)
       
   223 	{
       
   224 	TInt id = Bookmark::KNoOwnerId;
       
   225 	TInt err = iRepository->Get(IndexBase() + KCmnOwnerIndex, id);
       
   226 	if (err != KErrNone && err != KErrNotFound)
       
   227 		{
       
   228 		User::Leave(err);
       
   229 		}
       
   230 	aOwner.iId = static_cast<TUint32>(id);
       
   231 	}
       
   232 
       
   233 void CBookmarkBase::SetOwnerL(TSecureId aOwner)
       
   234 	{
       
   235 	if ( iDatabase.Visibility() != Bookmark::EVisibilityManager )
       
   236 		{
       
   237 		RThread thread;
       
   238 		TSecurityInfo secInfo ( thread );
       
   239 		
       
   240 		if ( aOwner != secInfo.iSecureId )
       
   241 			{
       
   242 			User::Leave ( KErrPermissionDenied );
       
   243 			}
       
   244 		}
       
   245 	
       
   246 	// The owner is never stored in the class as a member variable
       
   247 	if (iStatus == EStatusCreating)
       
   248 		{
       
   249 		// the owner id is not stored in the class so it can't be set until the bookmark 
       
   250 		// had been committed at least once
       
   251 		User::Leave(Bookmark::KErrNotCommitted);
       
   252 		}
       
   253 	else
       
   254 		{
       
   255 		TInt id = Bookmark::KNoOwnerId;
       
   256 		if (aOwner != Bookmark::KNoOwnerId)
       
   257 			{
       
   258 			id = static_cast<TInt>(aOwner.iId);
       
   259 			}
       
   260 		User::LeaveIfError(iRepository->Set(IndexBase() + KCmnOwnerIndex, id));
       
   261 		}
       
   262 	iFlags = iFlags & ~EFlagPublic;
       
   263 	SetDirty();
       
   264 	}
       
   265 
       
   266 TInt CBookmarkBase::Rank()
       
   267 	{
       
   268 	return iRank;
       
   269 	}
       
   270 
       
   271 void CBookmarkBase::LeaveIfNotWritableL()
       
   272 	{
       
   273 	if ( iDatabase.Visibility() != Bookmark::EVisibilityManager && 
       
   274 		!iDatabase.IsLoading() && 
       
   275 		!IsWritable())
       
   276  		{
       
   277  		User::Leave(Bookmark::KErrReadOnly);
       
   278  		}	
       
   279  	}
       
   280 
       
   281 void CBookmarkBase::LeaveIfNotInManagerModeL ()
       
   282 	{
       
   283 	if ( iDatabase.Visibility() != Bookmark::EVisibilityManager )
       
   284  		{
       
   285 		User::Leave ( KErrPermissionDenied );
       
   286  		}
       
   287  	}
       
   288 
       
   289 TBool CBookmarkBase::IsAChild(CBookmarkFolder* aAncestor)
       
   290 	{
       
   291 	CBookmarkFolder* parent = iParent;
       
   292 	for (TInt folderDepth = 0; parent != NULL && folderDepth < Bookmark::KMaxFolderDepth; ++folderDepth)
       
   293 		{
       
   294 		if (parent == aAncestor)
       
   295 			{
       
   296 			return ETrue;
       
   297 			}
       
   298 		parent = parent->Parent();
       
   299 		}
       
   300 
       
   301 	return EFalse;
       
   302 	}
       
   303 
       
   304 CBkmrkProperties& CBookmarkBase::ExtendedPropertiesL()
       
   305 	{
       
   306 	if (Type() == Bookmark::ETypeFolder)
       
   307 		{
       
   308 		return static_cast<CBookmarkFolder*>(this)->BkmrkPropertiesL();
       
   309 		}
       
   310 	else
       
   311 		{
       
   312 		return static_cast<CBookmark*>(this)->BkmrkExtendedPropertiesL();
       
   313 		}		
       
   314 	}
       
   315 
       
   316 void CBookmarkBase::TransNewL()
       
   317 	{
       
   318 	TUint32 indexBase = IndexBase();
       
   319 	// create entries in the repository
       
   320 	iRepository->Create(indexBase + KCmnTitleIndex, Title());
       
   321 	iRepository->Create(indexBase + KCmnParentIndex, static_cast<TInt>(iParent->Id()));
       
   322 	iRepository->Create(indexBase + KCmnFlagsIndex, static_cast<TInt>(iFlags));
       
   323 	TInt rank = iParent->Index(*this);
       
   324 	iRepository->Create(indexBase + KCmnRankIndex, rank);
       
   325 
       
   326 	TInt id = Bookmark::KNoOwnerId;
       
   327 	if (!(iFlags & EFlagPublic))
       
   328 		{
       
   329 		RThread thread;
       
   330 		TSecurityInfo secInfo(thread);
       
   331 		id = static_cast<TInt>(secInfo.iSecureId);
       
   332 		}
       
   333 	iRepository->Create(indexBase + KCmnOwnerIndex, id);
       
   334 	}
       
   335 
       
   336 void CBookmarkBase::TransSaveL()
       
   337 	{
       
   338 	TUint32 indexBase = IndexBase();
       
   339 	// create entries in the repository
       
   340 	iRepository->Set(indexBase + KCmnTitleIndex, Title());
       
   341 	iRepository->Set(indexBase + KCmnParentIndex, static_cast<TInt>(iParent->Id()));
       
   342 	iRepository->Set(indexBase + KCmnFlagsIndex, static_cast<TInt>(iFlags));
       
   343 	TInt rank = iParent->Index(*this);
       
   344 	iRepository->Set(indexBase + KCmnRankIndex, rank);
       
   345 	}
       
   346 
       
   347 void CBookmarkBase::TransLoadL()
       
   348 	{
       
   349 	TUint32 indexBase = IndexBase();
       
   350 	HBufC* titleBuffer = HBufC::NewLC(Bookmark::KMaxDescriptorLength);
       
   351 	TPtr titlePtr = titleBuffer->Des();
       
   352 	User::LeaveIfError(iRepository->Get(indexBase + KCmnTitleIndex, titlePtr));
       
   353 	delete iTitle;
       
   354 	iTitle = NULL;
       
   355 	iTitle = titlePtr.AllocL();
       
   356 	CleanupStack::PopAndDestroy(titleBuffer);
       
   357 
       
   358 	TInt retInt = 0;
       
   359 	User::LeaveIfError(iRepository->Get(indexBase + KCmnParentIndex, retInt));
       
   360 	iParentId = static_cast<Bookmark::TItemId>(retInt);
       
   361 	User::LeaveIfError(iRepository->Get(indexBase + KCmnFlagsIndex, retInt));
       
   362 	iFlags = static_cast<TUint32>(retInt);
       
   363 	User::LeaveIfError(iRepository->Get(indexBase + KCmnRankIndex, iRank))	;
       
   364 	SetClean();
       
   365 	}
       
   366 
       
   367 void CBookmarkBase::TransRemoveL()
       
   368 	{
       
   369 	TUint32 indexBase = IndexBase();
       
   370 	iRepository->Delete(indexBase + KCmnTitleIndex);
       
   371 	iRepository->Delete(indexBase + KCmnParentIndex);
       
   372 	iRepository->Delete(indexBase + KCmnFlagsIndex);
       
   373 	iRepository->Delete(indexBase + KCmnRankIndex);
       
   374 	iRepository->Delete(indexBase + KCmnOwnerIndex);
       
   375 	}
       
   376