1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <comphelper/propertystatecontainer.hxx>
21 #include <rtl/ustrbuf.hxx>
28 using namespace ::com::sun::star::uno
;
29 using namespace ::com::sun::star::beans
;
30 using namespace ::com::sun::star::lang
;
34 static OUString
lcl_getUnknownPropertyErrorMessage( const OUString
& _rPropertyName
)
36 // TODO: perhaps it's time to think about resources in the comphelper module?
37 // Would be nice to have localized exception strings (a simply resource file containing
38 // strings only would suffice, and could be realized with an UNO service, so we do not
39 // need the dependency to the Tools project)
40 OUStringBuffer sMessage
;
41 sMessage
.appendAscii( "The property \"" );
42 sMessage
.append( _rPropertyName
);
43 sMessage
.appendAscii( "\" is unknown." );
44 return sMessage
.makeStringAndClear();
48 OPropertyStateContainer::OPropertyStateContainer( ::cppu::OBroadcastHelper
& _rBHelper
)
49 :OPropertyContainer( _rBHelper
)
54 Any SAL_CALL
OPropertyStateContainer::queryInterface( const Type
& _rType
) throw (RuntimeException
, std::exception
)
56 Any aReturn
= OPropertyContainer::queryInterface( _rType
);
57 if ( !aReturn
.hasValue() )
58 aReturn
= OPropertyStateContainer_TBase::queryInterface( _rType
);
63 IMPLEMENT_FORWARD_XTYPEPROVIDER2( OPropertyStateContainer
, OPropertyContainer
, OPropertyStateContainer_TBase
)
66 sal_Int32
OPropertyStateContainer::getHandleForName( const OUString
& _rPropertyName
)
68 // look up the handle for the name
69 ::cppu::IPropertyArrayHelper
& rPH
= getInfoHelper();
70 sal_Int32 nHandle
= rPH
.getHandleByName( _rPropertyName
);
73 throw UnknownPropertyException( lcl_getUnknownPropertyErrorMessage( _rPropertyName
), static_cast< XPropertyState
* >( this ) );
79 PropertyState SAL_CALL
OPropertyStateContainer::getPropertyState( const OUString
& _rPropertyName
) throw (UnknownPropertyException
, RuntimeException
, std::exception
)
81 return getPropertyStateByHandle( getHandleForName( _rPropertyName
) );
85 Sequence
< PropertyState
> SAL_CALL
OPropertyStateContainer::getPropertyStates( const Sequence
< OUString
>& _rPropertyNames
) throw (UnknownPropertyException
, RuntimeException
, std::exception
)
87 sal_Int32 nProperties
= _rPropertyNames
.getLength();
88 Sequence
< PropertyState
> aStates( nProperties
);
93 // precondition: property sequence is sorted (the algorithm below relies on this)
95 const OUString
* pNames
= _rPropertyNames
.getConstArray();
96 const OUString
* pNamesCompare
= pNames
+ 1;
97 const OUString
* pNamesEnd
= _rPropertyNames
.getConstArray() + _rPropertyNames
.getLength();
98 for ( ; pNamesCompare
!= pNamesEnd
; ++pNames
, ++pNamesCompare
)
99 OSL_PRECOND( pNames
->compareTo( *pNamesCompare
) < 0,
100 "OPropertyStateContainer::getPropertyStates: property sequence not sorted!" );
104 const OUString
* pLookup
= _rPropertyNames
.getConstArray();
105 const OUString
* pLookupEnd
= pLookup
+ nProperties
;
106 PropertyState
* pStates
= aStates
.getArray();
108 cppu::IPropertyArrayHelper
& rHelper
= getInfoHelper();
109 Sequence
< Property
> aAllProperties
= rHelper
.getProperties();
110 sal_Int32 nAllProperties
= aAllProperties
.getLength();
111 const Property
* pAllProperties
= aAllProperties
.getConstArray();
112 const Property
* pAllPropertiesEnd
= pAllProperties
+ nAllProperties
;
114 osl::MutexGuard
aGuard( rBHelper
.rMutex
);
115 for ( ; ( pAllProperties
!= pAllPropertiesEnd
) && ( pLookup
!= pLookupEnd
); ++pAllProperties
)
118 if ( pAllProperties
< pAllPropertiesEnd
- 1 )
119 OSL_ENSURE( pAllProperties
->Name
.compareTo( (pAllProperties
+ 1)->Name
) < 0,
120 "OPropertyStateContainer::getPropertyStates: all-properties not sorted!" );
122 if ( pAllProperties
->Name
.equals( *pLookup
) )
124 *pStates
++ = getPropertyState( *pLookup
);
129 if ( pLookup
!= pLookupEnd
)
130 // we run out of properties from the IPropertyArrayHelper, but still have properties to lookup
131 // -> we were asked for a nonexistent property
132 throw UnknownPropertyException( lcl_getUnknownPropertyErrorMessage( *pLookup
), static_cast< XPropertyState
* >( this ) );
138 void SAL_CALL
OPropertyStateContainer::setPropertyToDefault( const OUString
& _rPropertyName
) throw (UnknownPropertyException
, RuntimeException
, std::exception
)
140 setPropertyToDefaultByHandle( getHandleForName( _rPropertyName
) );
144 Any SAL_CALL
OPropertyStateContainer::getPropertyDefault( const OUString
& _rPropertyName
) throw (UnknownPropertyException
, WrappedTargetException
, RuntimeException
, std::exception
)
147 getPropertyDefaultByHandle( getHandleForName( _rPropertyName
), aDefault
);
152 PropertyState
OPropertyStateContainer::getPropertyStateByHandle( sal_Int32 _nHandle
)
154 // simply compare the current and the default value
155 Any aCurrentValue
; getFastPropertyValue( aCurrentValue
, _nHandle
);
156 Any aDefaultValue
; getPropertyDefaultByHandle( _nHandle
, aDefaultValue
);
158 bool bEqual
= uno_type_equalData(
159 const_cast< void* >( aCurrentValue
.getValue() ), aCurrentValue
.getValueType().getTypeLibType(),
160 const_cast< void* >( aDefaultValue
.getValue() ), aDefaultValue
.getValueType().getTypeLibType(),
161 reinterpret_cast< uno_QueryInterfaceFunc
>(cpp_queryInterface
),
162 reinterpret_cast< uno_ReleaseFunc
>(cpp_release
)
165 return PropertyState_DEFAULT_VALUE
;
167 return PropertyState_DIRECT_VALUE
;
171 void OPropertyStateContainer::setPropertyToDefaultByHandle( sal_Int32 _nHandle
)
174 getPropertyDefaultByHandle( _nHandle
, aDefault
);
175 setFastPropertyValue( _nHandle
, aDefault
);
179 } // namespace comphelper
182 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */