Version 4.0.0.1, tag libreoffice-4.0.0.1
[LibreOffice.git] / comphelper / source / property / propertystatecontainer.cxx
blob257c3eb821dc1052ee4bc1d37bde34854fd2b49b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
23 //.........................................................................
24 namespace comphelper
26 //.........................................................................
28 using namespace ::com::sun::star::uno;
29 using namespace ::com::sun::star::beans;
30 using namespace ::com::sun::star::lang;
32 namespace
34 static ::rtl::OUString lcl_getUnknownPropertyErrorMessage( const ::rtl::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 ::rtl::OUStringBuffer sMessage;
41 sMessage.appendAscii( "The property \"" );
42 sMessage.append( _rPropertyName );
43 sMessage.appendAscii( "\" is unknown." );
44 return sMessage.makeStringAndClear();
48 //=====================================================================
49 //= OPropertyStateContainer
50 //=====================================================================
51 //---------------------------------------------------------------------
52 OPropertyStateContainer::OPropertyStateContainer( ::cppu::OBroadcastHelper& _rBHelper )
53 :OPropertyContainer( _rBHelper )
57 //--------------------------------------------------------------------
58 Any SAL_CALL OPropertyStateContainer::queryInterface( const Type& _rType ) throw (RuntimeException)
60 Any aReturn = OPropertyContainer::queryInterface( _rType );
61 if ( !aReturn.hasValue() )
62 aReturn = OPropertyStateContainer_TBase::queryInterface( _rType );
63 return aReturn;
66 //--------------------------------------------------------------------
67 IMPLEMENT_FORWARD_XTYPEPROVIDER2( OPropertyStateContainer, OPropertyContainer, OPropertyStateContainer_TBase )
69 //--------------------------------------------------------------------
70 sal_Int32 OPropertyStateContainer::getHandleForName( const ::rtl::OUString& _rPropertyName ) SAL_THROW( ( UnknownPropertyException ) )
72 // look up the handle for the name
73 ::cppu::IPropertyArrayHelper& rPH = getInfoHelper();
74 sal_Int32 nHandle = rPH.getHandleByName( _rPropertyName );
76 if ( -1 == nHandle )
77 throw UnknownPropertyException( lcl_getUnknownPropertyErrorMessage( _rPropertyName ), static_cast< XPropertyState* >( this ) );
79 return nHandle;
82 //--------------------------------------------------------------------
83 PropertyState SAL_CALL OPropertyStateContainer::getPropertyState( const ::rtl::OUString& _rPropertyName ) throw (UnknownPropertyException, RuntimeException)
85 return getPropertyStateByHandle( getHandleForName( _rPropertyName ) );
88 //--------------------------------------------------------------------
89 Sequence< PropertyState > SAL_CALL OPropertyStateContainer::getPropertyStates( const Sequence< ::rtl::OUString >& _rPropertyNames ) throw (UnknownPropertyException, RuntimeException)
91 sal_Int32 nProperties = _rPropertyNames.getLength();
92 Sequence< PropertyState> aStates( nProperties );
93 if ( !nProperties )
94 return aStates;
96 #ifdef _DEBUG
97 // precondition: property sequence is sorted (the algorythm below relies on this)
99 const ::rtl::OUString* pNames = _rPropertyNames.getConstArray();
100 const ::rtl::OUString* pNamesCompare = pNames + 1;
101 const ::rtl::OUString* pNamesEnd = _rPropertyNames.getConstArray() + _rPropertyNames.getLength();
102 for ( ; pNamesCompare != pNamesEnd; ++pNames, ++pNamesCompare )
103 OSL_PRECOND( pNames->compareTo( *pNamesCompare ) < 0,
104 "OPropertyStateContainer::getPropertyStates: property sequence not sorted!" );
106 #endif
108 const ::rtl::OUString* pLookup = _rPropertyNames.getConstArray();
109 const ::rtl::OUString* pLookupEnd = pLookup + nProperties;
110 PropertyState* pStates = aStates.getArray();
112 cppu::IPropertyArrayHelper& rHelper = getInfoHelper();
113 Sequence< Property> aAllProperties = rHelper.getProperties();
114 sal_Int32 nAllProperties = aAllProperties.getLength();
115 const Property* pAllProperties = aAllProperties.getConstArray();
116 const Property* pAllPropertiesEnd = pAllProperties + nAllProperties;
118 osl::MutexGuard aGuard( rBHelper.rMutex );
119 for ( ; ( pAllProperties != pAllPropertiesEnd ) && ( pLookup != pLookupEnd ); ++pAllProperties )
121 #ifdef _DEBUG
122 if ( pAllProperties < pAllPropertiesEnd - 1 )
123 OSL_ENSURE( pAllProperties->Name.compareTo( (pAllProperties + 1)->Name ) < 0,
124 "OPropertyStateContainer::getPropertyStates: all-properties not sorted!" );
125 #endif
126 if ( pAllProperties->Name.equals( *pLookup ) )
128 *pStates++ = getPropertyState( *pLookup );
129 ++pLookup;
133 if ( pLookup != pLookupEnd )
134 // we run out of properties from the IPropertyArrayHelper, but still have properties to lookup
135 // -> we were asked for a nonexistent property
136 throw UnknownPropertyException( lcl_getUnknownPropertyErrorMessage( *pLookup ), static_cast< XPropertyState* >( this ) );
138 return aStates;
141 //--------------------------------------------------------------------
142 void SAL_CALL OPropertyStateContainer::setPropertyToDefault( const ::rtl::OUString& _rPropertyName ) throw (UnknownPropertyException, RuntimeException)
144 setPropertyToDefaultByHandle( getHandleForName( _rPropertyName ) );
147 //--------------------------------------------------------------------
148 Any SAL_CALL OPropertyStateContainer::getPropertyDefault( const ::rtl::OUString& _rPropertyName ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException)
150 Any aDefault;
151 getPropertyDefaultByHandle( getHandleForName( _rPropertyName ), aDefault );
152 return aDefault;
155 //--------------------------------------------------------------------
156 PropertyState OPropertyStateContainer::getPropertyStateByHandle( sal_Int32 _nHandle )
158 // simply compare the current and the default value
159 Any aCurrentValue; getFastPropertyValue( aCurrentValue, _nHandle );
160 Any aDefaultValue; getPropertyDefaultByHandle( _nHandle, aDefaultValue );
162 sal_Bool bEqual = uno_type_equalData(
163 const_cast< void* >( aCurrentValue.getValue() ), aCurrentValue.getValueType().getTypeLibType(),
164 const_cast< void* >( aDefaultValue.getValue() ), aDefaultValue.getValueType().getTypeLibType(),
165 reinterpret_cast< uno_QueryInterfaceFunc >(cpp_queryInterface),
166 reinterpret_cast< uno_ReleaseFunc >(cpp_release)
168 if ( bEqual )
169 return PropertyState_DEFAULT_VALUE;
170 else
171 return PropertyState_DIRECT_VALUE;
174 //--------------------------------------------------------------------
175 void OPropertyStateContainer::setPropertyToDefaultByHandle( sal_Int32 _nHandle )
177 Any aDefault;
178 getPropertyDefaultByHandle( _nHandle, aDefault );
179 setFastPropertyValue( _nHandle, aDefault );
182 //.........................................................................
183 } // namespace comphelper
184 //.........................................................................
186 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */