os/security/contentmgmt/contentaccessfwfordrm/source/cafutils/attributeset.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
/*
sl@0
     2
* Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     3
* All rights reserved.
sl@0
     4
* This component and the accompanying materials are made available
sl@0
     5
* under the terms of the License "Eclipse Public License v1.0"
sl@0
     6
* which accompanies this distribution, and is available
sl@0
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     8
*
sl@0
     9
* Initial Contributors:
sl@0
    10
* Nokia Corporation - initial contribution.
sl@0
    11
*
sl@0
    12
* Contributors:
sl@0
    13
*
sl@0
    14
* Description: 
sl@0
    15
*
sl@0
    16
*/
sl@0
    17
sl@0
    18
sl@0
    19
#include <s32strm.h>
sl@0
    20
#include <caf/attributeset.h>
sl@0
    21
#include <caf/caferr.h>
sl@0
    22
sl@0
    23
using namespace ContentAccess;
sl@0
    24
sl@0
    25
EXPORT_C RAttributeSet::RAttributeSet()
sl@0
    26
	{
sl@0
    27
	}
sl@0
    28
sl@0
    29
EXPORT_C void RAttributeSet::Close()
sl@0
    30
	{
sl@0
    31
	// Clean up array
sl@0
    32
	iAttributes.Reset();
sl@0
    33
	iAttributes.Close();
sl@0
    34
	}
sl@0
    35
sl@0
    36
EXPORT_C void RAttributeSet::AddL(TInt aAttribute)
sl@0
    37
	{
sl@0
    38
	// Can't have duplicates so if the attribute already exists just reset its
sl@0
    39
	// value back to KErrCANotSupported
sl@0
    40
	if(SetValue(aAttribute, 0, KErrCANotSupported) != KErrNone)
sl@0
    41
		{
sl@0
    42
		// Doesn't exist so add it
sl@0
    43
		AddL(aAttribute, 0, KErrCANotSupported);
sl@0
    44
		}
sl@0
    45
	}
sl@0
    46
sl@0
    47
EXPORT_C TInt RAttributeSet::GetValue(TInt aAttribute, TInt& aValue) const
sl@0
    48
	{
sl@0
    49
	TInt i;
sl@0
    50
	TInt count = iAttributes.Count();
sl@0
    51
	for(i  = 0; i < count; i++)
sl@0
    52
		{
sl@0
    53
		if(iAttributes[i].iAttribute == aAttribute)
sl@0
    54
			{
sl@0
    55
			// Set the value and return the associated error code
sl@0
    56
			aValue = iAttributes[i].iValue;
sl@0
    57
			return iAttributes[i].iError;
sl@0
    58
			}
sl@0
    59
		}
sl@0
    60
	return KErrNotFound;
sl@0
    61
	}
sl@0
    62
sl@0
    63
EXPORT_C TInt RAttributeSet::SetValue(TInt aAttribute, TInt aValue, TInt aErrorCode)
sl@0
    64
	{
sl@0
    65
	TInt i;
sl@0
    66
	TInt count = iAttributes.Count();
sl@0
    67
	for(i  = 0; i < count; i++)
sl@0
    68
		{
sl@0
    69
		if(iAttributes[i].iAttribute == aAttribute)
sl@0
    70
			{
sl@0
    71
			iAttributes[i].iValue = aValue;
sl@0
    72
			iAttributes[i].iError = aErrorCode;
sl@0
    73
			return KErrNone;
sl@0
    74
			}
sl@0
    75
		}
sl@0
    76
	return KErrNotFound;
sl@0
    77
	}
sl@0
    78
		
sl@0
    79
EXPORT_C TInt RAttributeSet::operator [] (TInt aIndex) const
sl@0
    80
		{
sl@0
    81
		return iAttributes[aIndex].iAttribute;
sl@0
    82
		}
sl@0
    83
sl@0
    84
EXPORT_C TInt RAttributeSet::Count() const
sl@0
    85
		{
sl@0
    86
		return iAttributes.Count();
sl@0
    87
		}
sl@0
    88
		
sl@0
    89
EXPORT_C void RAttributeSet::ExternalizeL(RWriteStream& aStream) const
sl@0
    90
	{
sl@0
    91
	TInt i;
sl@0
    92
	TInt count = iAttributes.Count();
sl@0
    93
	aStream.WriteInt32L(count);
sl@0
    94
	for(i = 0; i < count; i++)
sl@0
    95
		{
sl@0
    96
		aStream.WriteInt32L(iAttributes[i].iAttribute);
sl@0
    97
		aStream.WriteInt32L(iAttributes[i].iValue);
sl@0
    98
		aStream.WriteInt32L(iAttributes[i].iError);
sl@0
    99
		}
sl@0
   100
	}
sl@0
   101
sl@0
   102
EXPORT_C void RAttributeSet::InternalizeL(RReadStream& aStream)
sl@0
   103
	{
sl@0
   104
	TInt i;
sl@0
   105
	TInt attribute;
sl@0
   106
	TInt value;
sl@0
   107
	TInt errorCode;
sl@0
   108
sl@0
   109
	// Read the number of attributes to internalize
sl@0
   110
	TInt count = aStream.ReadInt32L();
sl@0
   111
	
sl@0
   112
	// loop through all the attributes
sl@0
   113
	for(i = 0; i < count; i++)
sl@0
   114
		{
sl@0
   115
		// Read the attribute and value from the stream
sl@0
   116
		attribute = aStream.ReadInt32L();
sl@0
   117
		value = aStream.ReadInt32L();
sl@0
   118
		errorCode = aStream.ReadInt32L();
sl@0
   119
		
sl@0
   120
		// try setting the attribute value first in case it already exists
sl@0
   121
		if(SetValue(attribute, value, errorCode) != KErrNone)
sl@0
   122
			{
sl@0
   123
			// Doesn't exist so set a new values
sl@0
   124
			AddL(attribute, value, errorCode);
sl@0
   125
			}
sl@0
   126
		}
sl@0
   127
	}
sl@0
   128
sl@0
   129
void RAttributeSet::AddL(TInt aAttribute, TInt aValue, TInt aErrorCode)
sl@0
   130
	{
sl@0
   131
	TAttributeValue attribute;
sl@0
   132
	attribute.iAttribute = aAttribute;
sl@0
   133
	attribute.iValue = aValue;
sl@0
   134
	attribute.iError = aErrorCode;
sl@0
   135
		
sl@0
   136
	// Append the new values to the iAttributes array
sl@0
   137
	User::LeaveIfError(iAttributes.Append(attribute));
sl@0
   138
	}