contentmgmt/contentaccessfwfordrm/source/cafutils/stringattributeset.cpp
changeset 0 2c201484c85f
child 8 35751d3474b7
equal deleted inserted replaced
-1:000000000000 0:2c201484c85f
       
     1 /*
       
     2 * Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 * attributeset.cpp
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 #include <s32strm.h>
       
    21 #include "stringattributeset.h"
       
    22 #include "caferr.h"
       
    23 #include "cafutils.h"
       
    24 #include "stringAttribute.h"
       
    25 
       
    26 using namespace ContentAccess;
       
    27 
       
    28 EXPORT_C RStringAttributeSet::RStringAttributeSet()
       
    29 	{
       
    30 	}
       
    31 
       
    32 EXPORT_C void RStringAttributeSet::Close()
       
    33 	{
       
    34 	// Clean up array
       
    35 	iAttributes.ResetAndDestroy();
       
    36 	iAttributes.Close();
       
    37 	}
       
    38 
       
    39 EXPORT_C void RStringAttributeSet::AddL(TInt aAttribute)
       
    40 	{
       
    41 	// Can't have duplicates so if the attribute already exists just reset its
       
    42 	// value back to KErrCANotSupported
       
    43 	TInt err = SetValue(aAttribute, KNullDesC(), KErrCANotSupported);
       
    44 	if(err != KErrNone)
       
    45 		{
       
    46 		// Doesn't exist so add it
       
    47 		AddL(aAttribute, KNullDesC(), KErrCANotSupported);
       
    48 		}
       
    49 	}
       
    50 
       
    51 EXPORT_C TInt RStringAttributeSet::GetValue(TInt aAttribute, TDes& aValue) const
       
    52 	{
       
    53 	TInt i = 0;
       
    54 	// Search through the set of attributes until the correct one is found
       
    55 	TInt count = iAttributes.Count();
       
    56 	for(i  = 0; i < count; i++)
       
    57 		{
       
    58 		if(iAttributes[i]->Attribute() == aAttribute)
       
    59 			{
       
    60 			TPtrC value = iAttributes[i]->Value();
       
    61 			if(value.Length() > aValue.MaxLength())
       
    62 				{
       
    63 				return KErrOverflow;
       
    64 				}
       
    65 			else
       
    66 				{
       
    67 				aValue.Copy(iAttributes[i]->Value());
       
    68 				return iAttributes[i]->Error();
       
    69 				}
       
    70 			}
       
    71 		}
       
    72 	// attribute not found 
       
    73 	return KErrNotFound;
       
    74 	}
       
    75 
       
    76 EXPORT_C TInt RStringAttributeSet::GetValueLength(TInt aAttribute) const
       
    77 	{
       
    78 	TInt i = 0;
       
    79 	
       
    80 	// Search through the set of attributes until the correct one is found
       
    81 	TInt count = iAttributes.Count();
       
    82 	for(i  = 0; i < count; i++)
       
    83 		{
       
    84 		if(iAttributes[i]->Attribute() == aAttribute)
       
    85 			{
       
    86 			return iAttributes[i]->Value().Length();
       
    87 			}
       
    88 		}
       
    89 	return 0;
       
    90 	}
       
    91 
       
    92 
       
    93 EXPORT_C TInt RStringAttributeSet::SetValue(TInt aAttribute, const TDesC& aValue, TInt aErrorCode)
       
    94 	{
       
    95 	TInt i;
       
    96 	TInt err = KErrNone;
       
    97 	
       
    98 	// search through the attributes see if the attribute exists
       
    99 	TInt count = iAttributes.Count();
       
   100 	for(i  = 0; i < count; i++)
       
   101 		{
       
   102 		if(iAttributes[i]->Attribute() == aAttribute)
       
   103 			{
       
   104 			CStringAttribute* temp = NULL;
       
   105 			// if it exists replace the current value with the new one
       
   106 			TRAP(err, temp = CStringAttribute::NewL(aAttribute, aValue, aErrorCode));
       
   107 			if(err == KErrNone)
       
   108 				{
       
   109 				delete iAttributes[i];
       
   110 				iAttributes[i] = temp;
       
   111 				}
       
   112 			return err;
       
   113 			}
       
   114 		}
       
   115 	return KErrNotFound;
       
   116 	}
       
   117 		
       
   118 EXPORT_C TInt RStringAttributeSet::operator [] (TInt aIndex) const
       
   119 		{
       
   120 		//  The attribute at a particular index..... NOT the attribute's value
       
   121 		return iAttributes[aIndex]->Attribute();
       
   122 		}
       
   123 
       
   124 EXPORT_C TInt RStringAttributeSet::Count() const
       
   125 		{
       
   126 		// Number of attributes in the set
       
   127 		return iAttributes.Count();
       
   128 		}
       
   129 		
       
   130 EXPORT_C void RStringAttributeSet::ExternalizeL(RWriteStream& aStream) const
       
   131 	{
       
   132 	TInt i;
       
   133 	// Write the number of attributes to the stream
       
   134 	TInt count = iAttributes.Count();
       
   135 	aStream.WriteInt32L(count);
       
   136 	
       
   137 	// loop through and write each attribute and value to the stream
       
   138 	for(i = 0; i < count; i++)
       
   139 		{
       
   140 		// write the attribute and it's value to the stream
       
   141 		aStream.WriteInt32L(iAttributes[i]->Attribute());
       
   142 		TCafUtils::WriteDescriptor16L(aStream, iAttributes[i]->Value());
       
   143 		aStream.WriteInt32L(iAttributes[i]->Error());
       
   144 		}
       
   145 	}
       
   146 
       
   147 EXPORT_C void RStringAttributeSet::InternalizeL(RReadStream& aStream)
       
   148 	{
       
   149 	TInt i;
       
   150 	TInt attribute;
       
   151 	TInt errorCode;
       
   152 	TInt err;
       
   153 	HBufC *value = NULL;
       
   154 
       
   155 	// Read the number of attributes to internalize
       
   156 	TInt count = aStream.ReadInt32L();
       
   157 	
       
   158 	// loop through all the attributes
       
   159 	for(i = 0; i < count; i++)
       
   160 		{
       
   161 		// Read the attribute and value from the stream
       
   162 		attribute = aStream.ReadInt32L();
       
   163 		value = TCafUtils::ReadDescriptor16L(aStream);
       
   164 		CleanupStack::PushL(value);
       
   165 		errorCode = aStream.ReadInt32L();
       
   166 		
       
   167 		// try setting the attribute value first in case it already exists
       
   168 		err = SetValue(attribute, *value, errorCode);
       
   169 		if(err != KErrNone)
       
   170 			{
       
   171 			// Doesn't exist so set a new values
       
   172 			AddL(attribute,  *value, errorCode);
       
   173 			}
       
   174 
       
   175 		CleanupStack::PopAndDestroy(value);
       
   176 		value = NULL;
       
   177 		}
       
   178 	}
       
   179 
       
   180 void RStringAttributeSet::AddL(TInt aAttribute, const TDesC& aValue, TInt aErrorCode)
       
   181 	{
       
   182 	CStringAttribute* element = CStringAttribute::NewL(aAttribute, aValue, aErrorCode);
       
   183 	CleanupStack::PushL(element);
       
   184 	User::LeaveIfError(iAttributes.Append(element));
       
   185 	CleanupStack::Pop(element);
       
   186 	}