os/security/contentmgmt/contentaccessfwfordrm/source/cafutils/stringattributeset.cpp
First public contribution.
2 * Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
21 #include <caf/stringattributeset.h>
22 #include <caf/caferr.h>
24 #include "stringAttribute.h"
26 using namespace ContentAccess;
28 EXPORT_C RStringAttributeSet::RStringAttributeSet()
32 EXPORT_C void RStringAttributeSet::Close()
35 iAttributes.ResetAndDestroy();
39 EXPORT_C void RStringAttributeSet::AddL(TInt aAttribute)
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);
46 // Doesn't exist so add it
47 AddL(aAttribute, KNullDesC(), KErrCANotSupported);
51 EXPORT_C TInt RStringAttributeSet::GetValue(TInt aAttribute, TDes& aValue) const
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++)
58 if(iAttributes[i]->Attribute() == aAttribute)
60 TPtrC value = iAttributes[i]->Value();
61 if(value.Length() > aValue.MaxLength())
67 aValue.Copy(iAttributes[i]->Value());
68 return iAttributes[i]->Error();
72 // attribute not found
76 EXPORT_C TInt RStringAttributeSet::GetValueLength(TInt aAttribute) const
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++)
84 if(iAttributes[i]->Attribute() == aAttribute)
86 return iAttributes[i]->Value().Length();
93 EXPORT_C TInt RStringAttributeSet::SetValue(TInt aAttribute, const TDesC& aValue, TInt aErrorCode)
98 // search through the attributes see if the attribute exists
99 TInt count = iAttributes.Count();
100 for(i = 0; i < count; i++)
102 if(iAttributes[i]->Attribute() == aAttribute)
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));
109 delete iAttributes[i];
110 iAttributes[i] = temp;
118 EXPORT_C TInt RStringAttributeSet::operator [] (TInt aIndex) const
120 // The attribute at a particular index..... NOT the attribute's value
121 return iAttributes[aIndex]->Attribute();
124 EXPORT_C TInt RStringAttributeSet::Count() const
126 // Number of attributes in the set
127 return iAttributes.Count();
130 EXPORT_C void RStringAttributeSet::ExternalizeL(RWriteStream& aStream) const
133 // Write the number of attributes to the stream
134 TInt count = iAttributes.Count();
135 aStream.WriteInt32L(count);
137 // loop through and write each attribute and value to the stream
138 for(i = 0; i < count; i++)
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());
147 EXPORT_C void RStringAttributeSet::InternalizeL(RReadStream& aStream)
155 // Read the number of attributes to internalize
156 TInt count = aStream.ReadInt32L();
158 // loop through all the attributes
159 for(i = 0; i < count; i++)
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();
167 // try setting the attribute value first in case it already exists
168 err = SetValue(attribute, *value, errorCode);
171 // Doesn't exist so set a new values
172 AddL(attribute, *value, errorCode);
175 CleanupStack::PopAndDestroy(value);
180 void RStringAttributeSet::AddL(TInt aAttribute, const TDesC& aValue, TInt aErrorCode)
182 CStringAttribute* element = CStringAttribute::NewL(aAttribute, aValue, aErrorCode);
183 CleanupStack::PushL(element);
184 User::LeaveIfError(iAttributes.Append(element));
185 CleanupStack::Pop(element);