messagingfw/msgsrvnstore/server/src/CMsvAttributeManager.cpp
changeset 0 8e480a14352b
equal deleted inserted replaced
-1:000000000000 0:8e480a14352b
       
     1 // Copyright (c) 2004-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 "CMsvAttributeManager.h"
       
    17 #include <cmsvattachment.h>
       
    18 
       
    19 CDesC8Attribute::CDesC8Attribute()
       
    20 	{
       
    21 	}
       
    22 	
       
    23 CDesC8Attribute::~CDesC8Attribute()
       
    24 	{
       
    25 	delete iAttribute;
       
    26 	}
       
    27 
       
    28 CMsvAttributeManager* CMsvAttributeManager::NewL(CMsvAttachment& aAttachment)
       
    29 	{
       
    30 	CMsvAttributeManager* self = new(ELeave) CMsvAttributeManager(aAttachment);
       
    31 	return self;
       
    32 	}
       
    33 
       
    34 CMsvAttributeManager::CMsvAttributeManager(CMsvAttachment& aAttachment)
       
    35 	: iAttachment(aAttachment)
       
    36 	{
       
    37 	}	
       
    38 
       
    39 CMsvAttributeManager::~CMsvAttributeManager()
       
    40 	{
       
    41 	iDesC8Attributes.ResetAndDestroy();
       
    42 	iIntAttributes.Reset();
       
    43 	}
       
    44 
       
    45 void CMsvAttributeManager::CloneL(CMsvAttributeManager& aAttributeManager)
       
    46 	{
       
    47 	iDesC8Attributes.ResetAndDestroy();
       
    48 	for( TInt ii=0; ii<aAttributeManager.iDesC8Attributes.Count(); ++ii )
       
    49 		{
       
    50 		CDesC8Attribute* attrib = new(ELeave) CDesC8Attribute();
       
    51 		CleanupStack::PushL(attrib);
       
    52 		CDesC8Attribute* attribToCopy = aAttributeManager.iDesC8Attributes[ii];
       
    53 		attrib->iUid = attribToCopy->iUid;
       
    54 		attrib->iAttribute = attribToCopy->iAttribute->AllocL();
       
    55 		User::LeaveIfError(iDesC8Attributes.Append(attrib));
       
    56 		CleanupStack::Pop(attrib);
       
    57 		}
       
    58 		
       
    59 	iIntAttributes.Reset();
       
    60 	for( TInt ii=0; ii<aAttributeManager.iIntAttributes.Count(); ++ii )
       
    61 		{
       
    62 		TIntAttribute attrib = aAttributeManager.iIntAttributes[ii];
       
    63 		User::LeaveIfError(iIntAttributes.Append(attrib));
       
    64 		}
       
    65 	}
       
    66 
       
    67 void CMsvAttributeManager::InternalizeL(RReadStream& aStream)
       
    68 	{
       
    69 	// DesC8 attributes
       
    70 	iDesC8Attributes.ResetAndDestroy();
       
    71 	TInt attributeCount = aStream.ReadInt32L();
       
    72 	for( TInt ii=0; ii<attributeCount; ++ii )
       
    73 		{
       
    74 		CDesC8Attribute* attrib = new(ELeave) CDesC8Attribute();
       
    75 		CleanupStack::PushL(attrib);
       
    76 		attrib->iUid = TUid::Uid(aStream.ReadInt32L());
       
    77 		attrib->iAttribute = HBufC8::NewL(aStream, KMaxTInt);
       
    78 		User::LeaveIfError(iDesC8Attributes.Append(attrib));
       
    79 		CleanupStack::Pop(attrib);
       
    80 		}
       
    81 		
       
    82 	// Int attributes
       
    83 	iIntAttributes.Reset();
       
    84 	attributeCount = aStream.ReadInt32L();
       
    85 	for( TInt ii=0; ii<attributeCount; ++ii )
       
    86 		{
       
    87 		TIntAttribute attrib;
       
    88 		attrib.iUid = TUid::Uid(aStream.ReadInt32L());
       
    89 		attrib.iAttribute = aStream.ReadInt32L();
       
    90 		User::LeaveIfError(iIntAttributes.Append(attrib));
       
    91 		}
       
    92 	}
       
    93 	
       
    94 void CMsvAttributeManager::ExternalizeL(RWriteStream& aStream) const
       
    95 	{
       
    96 	// DesC8 attributes
       
    97 	TInt attributeCount = iDesC8Attributes.Count();
       
    98 	aStream.WriteInt32L(attributeCount);
       
    99 	for( TInt ii=0; ii<attributeCount; ++ii )
       
   100 		{
       
   101 		CDesC8Attribute* attrib = iDesC8Attributes[ii];
       
   102 		aStream.WriteInt32L(attrib->iUid.iUid);
       
   103 		aStream << *(attrib->iAttribute);
       
   104 		}
       
   105 		
       
   106 	// Int attributes
       
   107 	attributeCount = iIntAttributes.Count();
       
   108 	aStream.WriteInt32L(attributeCount);
       
   109 	for( TInt ii=0; ii<attributeCount; ++ii )
       
   110 		{
       
   111 		TIntAttribute attrib = iIntAttributes[ii];
       
   112 		aStream.WriteInt32L(attrib.iUid.iUid);
       
   113 		aStream.WriteInt32L(attrib.iAttribute);
       
   114 		}
       
   115 	}
       
   116 	
       
   117 void CMsvAttributeManager::SetDesC8AttributeL(TUid aAttributeId, const TDesC8& aAttribute)
       
   118 	{
       
   119 	TInt attribIndex = FindDesC8Attribute(aAttributeId);
       
   120 	if( attribIndex != KErrNotFound )
       
   121 		{
       
   122 		delete iDesC8Attributes[attribIndex];
       
   123 		iDesC8Attributes.Remove(attribIndex);
       
   124 		}
       
   125 	
       
   126 	CDesC8Attribute* attrib = new(ELeave) CDesC8Attribute();
       
   127 	CleanupStack::PushL(attrib);
       
   128 	User::LeaveIfError(iDesC8Attributes.Append(attrib));
       
   129 	attrib->iUid = aAttributeId;
       
   130 	attrib->iAttribute = aAttribute.AllocL();
       
   131 	CleanupStack::Pop(attrib);
       
   132 	}
       
   133 	
       
   134 TInt CMsvAttributeManager::GetDesC8Attribute(TUid aAttributeId, TPtrC8& aAttribute) const
       
   135 	{
       
   136 	TInt attribIndex = FindDesC8Attribute(aAttributeId);
       
   137 	if( attribIndex != KErrNotFound )
       
   138 		{
       
   139 		aAttribute.Set(*(iDesC8Attributes[attribIndex]->iAttribute));
       
   140 		return KErrNone;
       
   141 		}
       
   142 	
       
   143 	return KErrNotFound;
       
   144 	}
       
   145 	
       
   146 void CMsvAttributeManager::RemoveDesC8Attribute(TUid aAttributeId)
       
   147 	{
       
   148 	TInt attribIndex = FindDesC8Attribute(aAttributeId);
       
   149 	if( attribIndex != KErrNotFound )
       
   150 		{
       
   151 		delete iDesC8Attributes[attribIndex];
       
   152 		iDesC8Attributes.Remove(attribIndex);
       
   153 		}
       
   154 	}
       
   155 	
       
   156 TInt CMsvAttributeManager::FindDesC8Attribute(TUid aUid) const
       
   157 	{
       
   158 	for( TInt ii=0; ii<iDesC8Attributes.Count(); ++ii )
       
   159 		{
       
   160 		if( iDesC8Attributes[ii]->iUid == aUid )
       
   161 			return ii;
       
   162 		}
       
   163 	
       
   164 	return KErrNotFound;
       
   165 	}
       
   166 	
       
   167 void CMsvAttributeManager::SetIntAttributeL(TUid aAttributeId, TInt aAttribute)
       
   168 	{
       
   169 	TInt attribIndex = FindIntAttribute(aAttributeId);
       
   170 	if( attribIndex != KErrNotFound )
       
   171 		iIntAttributes.Remove(attribIndex);
       
   172 	
       
   173 	TIntAttribute attrib;
       
   174 	attrib.iUid = aAttributeId;
       
   175 	attrib.iAttribute = aAttribute;
       
   176 	User::LeaveIfError(iIntAttributes.Append(attrib));
       
   177 	}
       
   178 	
       
   179 TInt CMsvAttributeManager::GetIntAttribute(TUid aAttributeId, TInt& aAttribute) const
       
   180 	{
       
   181 	TInt attribIndex = FindIntAttribute(aAttributeId);
       
   182 	if( attribIndex != KErrNotFound )
       
   183 		{
       
   184 		aAttribute = iIntAttributes[attribIndex].iAttribute;
       
   185 		return KErrNone;
       
   186 		}
       
   187 	
       
   188 	return KErrNotFound;
       
   189 	}
       
   190 	
       
   191 void CMsvAttributeManager::RemoveIntAttribute(TUid aAttributeId)
       
   192 	{
       
   193 	TInt attribIndex = FindIntAttribute(aAttributeId);
       
   194 	if( attribIndex != KErrNotFound )
       
   195 		iIntAttributes.Remove(attribIndex);
       
   196 	}
       
   197 	
       
   198 TInt CMsvAttributeManager::FindIntAttribute(TUid aUid) const
       
   199 	{
       
   200 	for( TInt ii=0; ii<iIntAttributes.Count(); ++ii )
       
   201 		{
       
   202 		if( iIntAttributes[ii].iUid == aUid )
       
   203 			return ii;
       
   204 		}
       
   205 	
       
   206 	return KErrNotFound;
       
   207 	}
       
   208