messagingfw/msgsrvnstore/server/src/msvcacheentry.cpp
changeset 62 db3f5fa34ec7
parent 0 8e480a14352b
equal deleted inserted replaced
60:9f5ae1728557 62:db3f5fa34ec7
       
     1 // Copyright (c) 2007-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 //
       
    15 
       
    16 #include "msvcacheentry.h"
       
    17 
       
    18 /**
       
    19  * NewLC()
       
    20  * @param None.
       
    21  * @return CMsvCacheEntry* : a handle to a new cache entry.
       
    22  * 
       
    23  * 
       
    24  */
       
    25 CMsvCacheEntry* CMsvCacheEntry::NewLC()
       
    26 	{
       
    27 	CMsvCacheEntry* self = new(ELeave) CMsvCacheEntry();
       
    28 	CleanupStack::PushL(self);
       
    29 	self->ConstructL();
       
    30 	return self;
       
    31 	}
       
    32 
       
    33 
       
    34 
       
    35 /**
       
    36  * NewL()
       
    37  * @param None.
       
    38  * @return CMsvCacheEntry* : a handle to a new cache entry.
       
    39  * 
       
    40  * 
       
    41  */
       
    42 CMsvCacheEntry* CMsvCacheEntry::NewL()
       
    43 	{
       
    44 	CMsvCacheEntry* self = NewLC();
       
    45 	CleanupStack::Pop(self);
       
    46 	return self;
       
    47 	}
       
    48 
       
    49 
       
    50 
       
    51 /**
       
    52  * ConstructL()
       
    53  * @param None.
       
    54  * @return None.
       
    55  * 
       
    56  * Second phase construction.
       
    57  */
       
    58 void CMsvCacheEntry::ConstructL()
       
    59 	{
       
    60 
       
    61 	}
       
    62 
       
    63 
       
    64 /**
       
    65  * CMsvCacheEntry()
       
    66  * @param None.
       
    67  * @return None.
       
    68  * 
       
    69  * Constructor.
       
    70  */
       
    71 CMsvCacheEntry::CMsvCacheEntry()
       
    72 	{
       
    73 	}
       
    74 
       
    75 
       
    76 /**
       
    77  * ~CMsvCacheEntry()
       
    78  * @param None.
       
    79  * @return None.
       
    80  * 
       
    81  * Destructor.
       
    82  */
       
    83 CMsvCacheEntry::~CMsvCacheEntry()
       
    84 	{
       
    85 	delete iDescriptionBuffer;
       
    86 	delete iDetailsBuffer;
       
    87 
       
    88 	if(iChildIdArray)
       
    89 		{
       
    90 		iChildIdArray->Close();
       
    91 		delete iChildIdArray;
       
    92 		iChildIdArray = NULL;
       
    93 		}
       
    94 	}
       
    95 
       
    96 
       
    97 /**
       
    98  * ResetEntry()
       
    99  * @param None.
       
   100  * @return None.
       
   101  * 
       
   102  * The function resets a cache entry to zero/default values.
       
   103  */
       
   104 void CMsvCacheEntry::ResetEntry()
       
   105 	{
       
   106 	delete iDescriptionBuffer;
       
   107 	iDescriptionBuffer = NULL;
       
   108 	
       
   109 	delete iDetailsBuffer;
       
   110 	iDetailsBuffer = NULL;
       
   111 	
       
   112 	iEntry = TMsvEntry();
       
   113 	iFlags = EMsvServerEntryClearFlag;
       
   114 
       
   115 	iStoreReaderCount = 0;
       
   116 	
       
   117 	if(iChildIdArray)
       
   118 		{
       
   119 		iChildIdArray->Close();
       
   120 		delete iChildIdArray;
       
   121 		iChildIdArray = NULL;
       
   122 		}
       
   123 	}
       
   124 
       
   125 
       
   126 
       
   127 /**
       
   128  * SetEntryL()
       
   129  * @param TMsvEntry& : the TMsvEntry with the details to be copied to the cache entry.
       
   130  * @return None.
       
   131  * 
       
   132  * The function sets the TMsvEntry, iDescription and iDetails fields of the cache entry
       
   133  * to that of the passed entry.
       
   134  */
       
   135 void CMsvCacheEntry::SetEntryL(TMsvEntry& aEntry)
       
   136 	{
       
   137 	iEntry = aEntry;
       
   138 	if(!iDescriptionBuffer)
       
   139 		{
       
   140 		iDescriptionBuffer = aEntry.iDescription.AllocL();
       
   141 		}
       
   142 	else if (aEntry.iDescription.Length() > iDescriptionBuffer->Des().MaxLength())
       
   143 		{
       
   144 		iDescriptionBuffer = iDescriptionBuffer->ReAllocL(aEntry.iDescription.Length());
       
   145 		}
       
   146 	
       
   147 	
       
   148 	if(!iDetailsBuffer)
       
   149 		{
       
   150 		iDetailsBuffer = aEntry.iDetails.AllocL();
       
   151 		}
       
   152 	else if (aEntry.iDetails.Length() > iDetailsBuffer->Des().MaxLength())
       
   153 		{
       
   154 		iDetailsBuffer = iDetailsBuffer->ReAllocL(aEntry.iDetails.Length());
       
   155 		
       
   156 		}
       
   157 
       
   158 	*iDescriptionBuffer = aEntry.iDescription;
       
   159 	*iDetailsBuffer = aEntry.iDetails;
       
   160 			
       
   161 	iEntry.iDescription.Set(*iDescriptionBuffer);
       
   162 	iEntry.iDetails.Set(*iDetailsBuffer);
       
   163 	}
       
   164 
       
   165 
       
   166 /*
       
   167  * DupNDestroyL()
       
   168  * 
       
   169  */
       
   170 void CMsvCacheEntry::DupNDestroyL(CMsvCacheEntry*& aEntry)
       
   171 	{
       
   172 	SetEntryL(aEntry->Entry());
       
   173 	iFlags = aEntry->iFlags;
       
   174 	iStoreReaderCount = aEntry->iStoreReaderCount;
       
   175 	iLastChange = aEntry->iLastChange;
       
   176 	iCreated = aEntry->iCreated;
       
   177 	iOwnerId = aEntry->iOwnerId;
       
   178 	iChildIdArray = aEntry->iChildIdArray;
       
   179 	aEntry->iChildIdArray = NULL;
       
   180 	}
       
   181 
       
   182 
       
   183  
       
   184 
       
   185 /**
       
   186  * LockEntry()
       
   187  * @param None.
       
   188  * @return TInt : KErrNone on success, KErrLocked if the entry is already locked.
       
   189  * 
       
   190  * The function locks the cache entry if not already locked.
       
   191  */
       
   192 TInt CMsvCacheEntry::LockEntry()
       
   193 	{
       
   194 	if (IsEntryLocked())
       
   195 		{
       
   196 		return KErrLocked;
       
   197 		}
       
   198 
       
   199 	iFlags |= EMsvServerEntryLockFlag;
       
   200 	return KErrNone;
       
   201 	}
       
   202 
       
   203 
       
   204 /**
       
   205  * LockStore()
       
   206  * @param None.
       
   207  * @return TInt : KErrNone on success, KErrLocked if the store entry is already locked.
       
   208  * 
       
   209  * The function locks the store entry if not already locked.
       
   210  */
       
   211 TInt CMsvCacheEntry::LockStore()
       
   212 	{
       
   213 	if (IsStoreLocked())
       
   214 		{
       
   215 		return KErrLocked;
       
   216 		}
       
   217 
       
   218 	iFlags |= EMsvServerEntryStoreLockFlag;
       
   219 	return KErrNone;
       
   220 	}
       
   221 
       
   222 
       
   223 
       
   224 /**
       
   225  * LockEntryAndStore()
       
   226  * @param None.
       
   227  * @return TInt : KErrNone on success, KErrLocked if the store and cache entry is already locked.
       
   228  * 
       
   229  * The function locks the store and cache entry if not already locked.
       
   230  */
       
   231 TInt CMsvCacheEntry::LockEntryAndStore()
       
   232 	{
       
   233 	if (IsEntryOrStoreLocked())
       
   234 		{
       
   235 		return KErrLocked;
       
   236 		}
       
   237 
       
   238 	iFlags |= (EMsvServerEntryStoreLockFlag | EMsvServerEntryLockFlag);
       
   239 	return KErrNone;
       
   240 	}
       
   241 
       
   242 
       
   243 
       
   244 /**
       
   245  * CopyEntryL()
       
   246  * @param const TMsvEntry& : the entry to be changed to.
       
   247  * @param TBool& : output parameter set to ETrue if any private details (iDesciption
       
   248  * 				   and iDetails) have been changed.
       
   249  * @return None.
       
   250  * 
       
   251  * The function copies details from the passed TMsvEntry to the cache entry's TMsvEntry.
       
   252  * It reallocates buffer sizes if there is difference between them.
       
   253  */
       
   254 void CMsvCacheEntry::CopyEntryL(const TMsvEntry& aEntry, TBool& aChangedPrivateInfo)
       
   255 	{
       
   256 	TBool changeDes = (aEntry.iDescription!=*iDescriptionBuffer);
       
   257 	TBool changeDet = (aEntry.iDetails!=*iDetailsBuffer);
       
   258 
       
   259 	aChangedPrivateInfo = changeDes || changeDet;
       
   260 
       
   261 	//Only increase the buffer size, not shrink so as to allow rollback
       
   262 	TBool isMemoryAllocationDone = EFalse;
       
   263 	HBufC* bkpDescriptionBuffer = iDescriptionBuffer;
       
   264 	if (changeDes && aEntry.iDescription.Length() > iDescriptionBuffer->Des().MaxLength())
       
   265 		{		
       
   266 		iDescriptionBuffer = HBufC::NewL(aEntry.iDescription.Length());
       
   267 		isMemoryAllocationDone = ETrue;
       
   268 		}
       
   269 
       
   270 	if (changeDet && aEntry.iDetails.Length() > iDetailsBuffer->Des().MaxLength())
       
   271 		{
       
   272 		TRAPD(err, iDetailsBuffer = iDetailsBuffer->ReAllocL(aEntry.iDetails.Length()));
       
   273 		if(err)
       
   274 		    {
       
   275 		    if(isMemoryAllocationDone)
       
   276 		        {
       
   277 		        delete iDescriptionBuffer;
       
   278 		        iDescriptionBuffer = bkpDescriptionBuffer; 
       
   279 		        }
       
   280 		    User::Leave(err);
       
   281 		    }
       
   282 		}
       
   283 
       
   284 	if(isMemoryAllocationDone)
       
   285 	    {
       
   286 	    delete bkpDescriptionBuffer;
       
   287 	    }
       
   288 	//Copy the data without leaving
       
   289 	if (changeDes)
       
   290 		{
       
   291 		*iDescriptionBuffer = aEntry.iDescription;
       
   292 		}
       
   293 
       
   294 	if (changeDet)
       
   295 		{
       
   296 		*iDetailsBuffer = aEntry.iDetails;
       
   297 		}
       
   298 
       
   299 	iEntry=aEntry;
       
   300 	iEntry.iDescription.Set(*iDescriptionBuffer);
       
   301 	iEntry.iDetails.Set(*iDetailsBuffer);
       
   302 	}
       
   303 
       
   304 
       
   305 
       
   306 /**
       
   307  * RollBackCopyEntry()
       
   308  * @param const TMsvEntry& : the entry to be changed to.
       
   309  * @return None.
       
   310  * 
       
   311  * 
       
   312  */
       
   313 void CMsvCacheEntry::RollBackCopyEntry(const TMsvEntry& aEntry)
       
   314 	{
       
   315 	__ASSERT_DEBUG(iDescriptionBuffer->Des().MaxLength() >= aEntry.iDescription.Length(), PanicServer(EMsvBadRollBackCopy1));
       
   316 	__ASSERT_DEBUG(iDetailsBuffer->Des().MaxLength() >= aEntry.iDetails.Length(), PanicServer(EMsvBadRollBackCopy2));
       
   317 
       
   318 	Mem::Copy(&iEntry, &aEntry, sizeof(TMsvEntry));
       
   319 
       
   320 	*iDescriptionBuffer = aEntry.iDescription;
       
   321 	*iDetailsBuffer = aEntry.iDetails;
       
   322 
       
   323 	iEntry.iDescription.Set(*iDescriptionBuffer);
       
   324 	iEntry.iDetails.Set(*iDetailsBuffer);
       
   325 	}