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 :
49 : //= OPropertyStateContainer
50 :
51 :
52 0 : OPropertyStateContainer::OPropertyStateContainer( ::cppu::OBroadcastHelper& _rBHelper )
53 0 : :OPropertyContainer( _rBHelper )
54 : {
55 0 : }
56 :
57 :
58 0 : Any SAL_CALL OPropertyStateContainer::queryInterface( const Type& _rType ) throw (RuntimeException, std::exception)
59 : {
60 0 : Any aReturn = OPropertyContainer::queryInterface( _rType );
61 0 : if ( !aReturn.hasValue() )
62 0 : aReturn = OPropertyStateContainer_TBase::queryInterface( _rType );
63 0 : return aReturn;
64 : }
65 :
66 :
67 0 : IMPLEMENT_FORWARD_XTYPEPROVIDER2( OPropertyStateContainer, OPropertyContainer, OPropertyStateContainer_TBase )
68 :
69 :
70 0 : sal_Int32 OPropertyStateContainer::getHandleForName( const OUString& _rPropertyName ) SAL_THROW( ( UnknownPropertyException ) )
71 : {
72 : // look up the handle for the name
73 0 : ::cppu::IPropertyArrayHelper& rPH = getInfoHelper();
74 0 : sal_Int32 nHandle = rPH.getHandleByName( _rPropertyName );
75 :
76 0 : if ( -1 == nHandle )
77 0 : throw UnknownPropertyException( lcl_getUnknownPropertyErrorMessage( _rPropertyName ), static_cast< XPropertyState* >( this ) );
78 :
79 0 : return nHandle;
80 : }
81 :
82 :
83 0 : PropertyState SAL_CALL OPropertyStateContainer::getPropertyState( const OUString& _rPropertyName ) throw (UnknownPropertyException, RuntimeException, std::exception)
84 : {
85 0 : return getPropertyStateByHandle( getHandleForName( _rPropertyName ) );
86 : }
87 :
88 :
89 0 : Sequence< PropertyState > SAL_CALL OPropertyStateContainer::getPropertyStates( const Sequence< OUString >& _rPropertyNames ) throw (UnknownPropertyException, RuntimeException, std::exception)
90 : {
91 0 : sal_Int32 nProperties = _rPropertyNames.getLength();
92 0 : Sequence< PropertyState> aStates( nProperties );
93 0 : if ( !nProperties )
94 0 : return aStates;
95 :
96 : #ifdef _DEBUG
97 : // precondition: property sequence is sorted (the algorithm below relies on this)
98 : {
99 : const OUString* pNames = _rPropertyNames.getConstArray();
100 : const OUString* pNamesCompare = pNames + 1;
101 : const 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!" );
105 : }
106 : #endif
107 :
108 0 : const OUString* pLookup = _rPropertyNames.getConstArray();
109 0 : const OUString* pLookupEnd = pLookup + nProperties;
110 0 : PropertyState* pStates = aStates.getArray();
111 :
112 0 : cppu::IPropertyArrayHelper& rHelper = getInfoHelper();
113 0 : Sequence< Property> aAllProperties = rHelper.getProperties();
114 0 : sal_Int32 nAllProperties = aAllProperties.getLength();
115 0 : const Property* pAllProperties = aAllProperties.getConstArray();
116 0 : const Property* pAllPropertiesEnd = pAllProperties + nAllProperties;
117 :
118 0 : osl::MutexGuard aGuard( rBHelper.rMutex );
119 0 : for ( ; ( pAllProperties != pAllPropertiesEnd ) && ( pLookup != pLookupEnd ); ++pAllProperties )
120 : {
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 0 : if ( pAllProperties->Name.equals( *pLookup ) )
127 : {
128 0 : *pStates++ = getPropertyState( *pLookup );
129 0 : ++pLookup;
130 : }
131 : }
132 :
133 0 : 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 0 : throw UnknownPropertyException( lcl_getUnknownPropertyErrorMessage( *pLookup ), static_cast< XPropertyState* >( this ) );
137 :
138 0 : return aStates;
139 : }
140 :
141 :
142 0 : void SAL_CALL OPropertyStateContainer::setPropertyToDefault( const OUString& _rPropertyName ) throw (UnknownPropertyException, RuntimeException, std::exception)
143 : {
144 0 : setPropertyToDefaultByHandle( getHandleForName( _rPropertyName ) );
145 0 : }
146 :
147 :
148 0 : Any SAL_CALL OPropertyStateContainer::getPropertyDefault( const OUString& _rPropertyName ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException, std::exception)
149 : {
150 0 : Any aDefault;
151 0 : getPropertyDefaultByHandle( getHandleForName( _rPropertyName ), aDefault );
152 0 : return aDefault;
153 : }
154 :
155 :
156 0 : PropertyState OPropertyStateContainer::getPropertyStateByHandle( sal_Int32 _nHandle )
157 : {
158 : // simply compare the current and the default value
159 0 : Any aCurrentValue; getFastPropertyValue( aCurrentValue, _nHandle );
160 0 : Any aDefaultValue; getPropertyDefaultByHandle( _nHandle, aDefaultValue );
161 :
162 : bool bEqual = uno_type_equalData(
163 0 : const_cast< void* >( aCurrentValue.getValue() ), aCurrentValue.getValueType().getTypeLibType(),
164 0 : const_cast< void* >( aDefaultValue.getValue() ), aDefaultValue.getValueType().getTypeLibType(),
165 : reinterpret_cast< uno_QueryInterfaceFunc >(cpp_queryInterface),
166 : reinterpret_cast< uno_ReleaseFunc >(cpp_release)
167 0 : );
168 0 : if ( bEqual )
169 0 : return PropertyState_DEFAULT_VALUE;
170 : else
171 0 : return PropertyState_DIRECT_VALUE;
172 : }
173 :
174 :
175 0 : void OPropertyStateContainer::setPropertyToDefaultByHandle( sal_Int32 _nHandle )
176 : {
177 0 : Any aDefault;
178 0 : getPropertyDefaultByHandle( _nHandle, aDefault );
179 0 : setFastPropertyValue( _nHandle, aDefault );
180 0 : }
181 :
182 :
183 : } // namespace comphelper
184 :
185 :
186 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|