Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <comphelper/propertystatecontainer.hxx>
21 : #include <rtl/ustrbuf.hxx>
22 :
23 :
24 : namespace comphelper
25 : {
26 :
27 :
28 : using namespace ::com::sun::star::uno;
29 : using namespace ::com::sun::star::beans;
30 : using namespace ::com::sun::star::lang;
31 :
32 : namespace
33 : {
34 0 : static OUString lcl_getUnknownPropertyErrorMessage( const OUString& _rPropertyName )
35 : {
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 0 : OUStringBuffer sMessage;
41 0 : sMessage.appendAscii( "The property \"" );
42 0 : sMessage.append( _rPropertyName );
43 0 : sMessage.appendAscii( "\" is unknown." );
44 0 : return sMessage.makeStringAndClear();
45 : }
46 : }
47 :
48 677 : OPropertyStateContainer::OPropertyStateContainer( ::cppu::OBroadcastHelper& _rBHelper )
49 677 : :OPropertyContainer( _rBHelper )
50 : {
51 677 : }
52 :
53 :
54 4435 : Any SAL_CALL OPropertyStateContainer::queryInterface( const Type& _rType ) throw (RuntimeException, std::exception)
55 : {
56 4435 : Any aReturn = OPropertyContainer::queryInterface( _rType );
57 4435 : if ( !aReturn.hasValue() )
58 3055 : aReturn = OPropertyStateContainer_TBase::queryInterface( _rType );
59 4435 : return aReturn;
60 : }
61 :
62 :
63 2 : IMPLEMENT_FORWARD_XTYPEPROVIDER2( OPropertyStateContainer, OPropertyContainer, OPropertyStateContainer_TBase )
64 :
65 :
66 122 : sal_Int32 OPropertyStateContainer::getHandleForName( const OUString& _rPropertyName )
67 : {
68 : // look up the handle for the name
69 122 : ::cppu::IPropertyArrayHelper& rPH = getInfoHelper();
70 122 : sal_Int32 nHandle = rPH.getHandleByName( _rPropertyName );
71 :
72 122 : if ( -1 == nHandle )
73 0 : throw UnknownPropertyException( lcl_getUnknownPropertyErrorMessage( _rPropertyName ), static_cast< XPropertyState* >( this ) );
74 :
75 122 : return nHandle;
76 : }
77 :
78 :
79 122 : PropertyState SAL_CALL OPropertyStateContainer::getPropertyState( const OUString& _rPropertyName ) throw (UnknownPropertyException, RuntimeException, std::exception)
80 : {
81 122 : return getPropertyStateByHandle( getHandleForName( _rPropertyName ) );
82 : }
83 :
84 :
85 12 : Sequence< PropertyState > SAL_CALL OPropertyStateContainer::getPropertyStates( const Sequence< OUString >& _rPropertyNames ) throw (UnknownPropertyException, RuntimeException, std::exception)
86 : {
87 12 : sal_Int32 nProperties = _rPropertyNames.getLength();
88 12 : Sequence< PropertyState> aStates( nProperties );
89 12 : if ( !nProperties )
90 0 : return aStates;
91 :
92 : #ifdef _DEBUG
93 : // precondition: property sequence is sorted (the algorithm below relies on this)
94 : {
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!" );
101 : }
102 : #endif
103 :
104 12 : const OUString* pLookup = _rPropertyNames.getConstArray();
105 12 : const OUString* pLookupEnd = pLookup + nProperties;
106 12 : PropertyState* pStates = aStates.getArray();
107 :
108 12 : cppu::IPropertyArrayHelper& rHelper = getInfoHelper();
109 24 : Sequence< Property> aAllProperties = rHelper.getProperties();
110 12 : sal_Int32 nAllProperties = aAllProperties.getLength();
111 12 : const Property* pAllProperties = aAllProperties.getConstArray();
112 12 : const Property* pAllPropertiesEnd = pAllProperties + nAllProperties;
113 :
114 24 : osl::MutexGuard aGuard( rBHelper.rMutex );
115 351 : for ( ; ( pAllProperties != pAllPropertiesEnd ) && ( pLookup != pLookupEnd ); ++pAllProperties )
116 : {
117 : #ifdef _DEBUG
118 : if ( pAllProperties < pAllPropertiesEnd - 1 )
119 : OSL_ENSURE( pAllProperties->Name.compareTo( (pAllProperties + 1)->Name ) < 0,
120 : "OPropertyStateContainer::getPropertyStates: all-properties not sorted!" );
121 : #endif
122 339 : if ( pAllProperties->Name.equals( *pLookup ) )
123 : {
124 108 : *pStates++ = getPropertyState( *pLookup );
125 108 : ++pLookup;
126 : }
127 : }
128 :
129 12 : 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 0 : throw UnknownPropertyException( lcl_getUnknownPropertyErrorMessage( *pLookup ), static_cast< XPropertyState* >( this ) );
133 :
134 12 : return aStates;
135 : }
136 :
137 :
138 0 : void SAL_CALL OPropertyStateContainer::setPropertyToDefault( const OUString& _rPropertyName ) throw (UnknownPropertyException, RuntimeException, std::exception)
139 : {
140 0 : setPropertyToDefaultByHandle( getHandleForName( _rPropertyName ) );
141 0 : }
142 :
143 :
144 0 : Any SAL_CALL OPropertyStateContainer::getPropertyDefault( const OUString& _rPropertyName ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException, std::exception)
145 : {
146 0 : Any aDefault;
147 0 : getPropertyDefaultByHandle( getHandleForName( _rPropertyName ), aDefault );
148 0 : return aDefault;
149 : }
150 :
151 :
152 122 : PropertyState OPropertyStateContainer::getPropertyStateByHandle( sal_Int32 _nHandle )
153 : {
154 : // simply compare the current and the default value
155 122 : Any aCurrentValue; getFastPropertyValue( aCurrentValue, _nHandle );
156 244 : Any aDefaultValue; getPropertyDefaultByHandle( _nHandle, aDefaultValue );
157 :
158 : bool bEqual = uno_type_equalData(
159 244 : const_cast< void* >( aCurrentValue.getValue() ), aCurrentValue.getValueType().getTypeLibType(),
160 244 : const_cast< void* >( aDefaultValue.getValue() ), aDefaultValue.getValueType().getTypeLibType(),
161 : reinterpret_cast< uno_QueryInterfaceFunc >(cpp_queryInterface),
162 : reinterpret_cast< uno_ReleaseFunc >(cpp_release)
163 366 : );
164 122 : if ( bEqual )
165 120 : return PropertyState_DEFAULT_VALUE;
166 : else
167 124 : return PropertyState_DIRECT_VALUE;
168 : }
169 :
170 :
171 0 : void OPropertyStateContainer::setPropertyToDefaultByHandle( sal_Int32 _nHandle )
172 : {
173 0 : Any aDefault;
174 0 : getPropertyDefaultByHandle( _nHandle, aDefault );
175 0 : setFastPropertyValue( _nHandle, aDefault );
176 0 : }
177 :
178 :
179 : } // namespace comphelper
180 :
181 :
182 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|