LibreOffice Module comphelper (master)  1
propertysethelper.cxx
Go to the documentation of this file.
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 
22 #include <osl/diagnose.h>
23 #include <rtl/ref.hxx>
24 
25 #include <memory>
26 
27 using namespace ::comphelper;
28 using namespace ::com::sun::star;
29 using namespace ::com::sun::star::uno;
30 using namespace ::com::sun::star::beans;
31 using namespace ::com::sun::star::lang;
32 
33 static PropertyMapEntry const * find( rtl::Reference<PropertySetInfo>& mxInfo, const OUString& aName ) throw()
34 {
35  PropertyMap::const_iterator aIter = mxInfo->getPropertyMap().find( aName );
36 
37  if( mxInfo->getPropertyMap().end() != aIter )
38  return (*aIter).second;
39  else
40  return nullptr;
41 }
42 
43 
44 PropertySetHelper::PropertySetHelper( rtl::Reference<comphelper::PropertySetInfo> const & xInfo ) throw()
45  : mxInfo(xInfo)
46 {
47 }
48 
49 PropertySetHelper::~PropertySetHelper() throw()
50 {
51 }
52 
53 // XPropertySet
54 Reference< XPropertySetInfo > SAL_CALL PropertySetHelper::getPropertySetInfo( )
55 {
56  return mxInfo.get();
57 }
58 
59 void SAL_CALL PropertySetHelper::setPropertyValue( const OUString& aPropertyName, const Any& aValue )
60 {
61  PropertyMapEntry const * aEntries[2];
62  aEntries[0] = find( mxInfo, aPropertyName );
63 
64  if( nullptr == aEntries[0] )
65  throw UnknownPropertyException( aPropertyName, static_cast< XPropertySet* >( this ) );
66 
67  aEntries[1] = nullptr;
68 
69  _setPropertyValues( aEntries, &aValue );
70 }
71 
72 Any SAL_CALL PropertySetHelper::getPropertyValue( const OUString& PropertyName )
73 {
74  PropertyMapEntry const * aEntries[2];
75  aEntries[0] = find( mxInfo, PropertyName );
76 
77  if( nullptr == aEntries[0] )
78  throw UnknownPropertyException( PropertyName, static_cast< XPropertySet* >( this ) );
79 
80  aEntries[1] = nullptr;
81 
82  Any aAny;
83  _getPropertyValues( aEntries, &aAny );
84 
85  return aAny;
86 }
87 
88 void SAL_CALL PropertySetHelper::addPropertyChangeListener( const OUString&, const Reference< XPropertyChangeListener >& )
89 {
90  // todo
91 }
92 
93 void SAL_CALL PropertySetHelper::removePropertyChangeListener( const OUString&, const Reference< XPropertyChangeListener >& )
94 {
95  // todo
96 }
97 
98 void SAL_CALL PropertySetHelper::addVetoableChangeListener( const OUString&, const Reference< XVetoableChangeListener >& )
99 {
100  // todo
101 }
102 
103 void SAL_CALL PropertySetHelper::removeVetoableChangeListener( const OUString&, const Reference< XVetoableChangeListener >& )
104 {
105  // todo
106 }
107 
108 // XMultiPropertySet
109 void SAL_CALL PropertySetHelper::setPropertyValues( const Sequence< OUString >& rPropertyNames, const Sequence< Any >& rValues )
110 {
111  const sal_Int32 nCount = rPropertyNames.getLength();
112 
113  if( nCount != rValues.getLength() )
114  throw IllegalArgumentException();
115 
116  if( !nCount )
117  return;
118 
119  std::unique_ptr<PropertyMapEntry const *[]> pEntries(new PropertyMapEntry const *[nCount+1]);
120  pEntries[nCount] = nullptr;
121  const OUString* pNames = rPropertyNames.getConstArray();
122 
123  bool bUnknown = false;
124  sal_Int32 n;
125  for( n = 0; !bUnknown && ( n < nCount ); n++, pNames++ )
126  {
127  pEntries[n] = find( mxInfo, *pNames );
128  bUnknown = nullptr == pEntries[n];
129  }
130 
131  if( !bUnknown )
132  _setPropertyValues( pEntries.get(), rValues.getConstArray() );
133 
134  if( bUnknown )
135  throw RuntimeException( *pNames, static_cast< XPropertySet* >( this ) );
136 }
137 
138 Sequence< Any > SAL_CALL PropertySetHelper::getPropertyValues(const Sequence< OUString >& rPropertyNames)
139 {
140  const sal_Int32 nCount = rPropertyNames.getLength();
141 
142  if( !nCount )
143  return Sequence< Any >();
144 
145  std::unique_ptr<PropertyMapEntry const *[]> pEntries(new PropertyMapEntry const *[nCount+1]);
146  const OUString* pNames = rPropertyNames.getConstArray();
147 
148  bool bUnknown = false;
149  sal_Int32 n;
150  for( n = 0; !bUnknown && ( n < nCount ); n++, pNames++ )
151  {
152  pEntries[n] = find( mxInfo, *pNames );
153  bUnknown = nullptr == pEntries[n];
154  }
155 
156  if( !bUnknown )
157  {
158  pEntries[nCount] = nullptr;
159  Sequence< Any > aValues(nCount);
160  aValues.realloc(nCount);
161  _getPropertyValues( pEntries.get(), aValues.getArray() );
162  return aValues;
163  }
164  else
165  throw RuntimeException( *pNames, static_cast< XPropertySet* >( this ) );
166 }
167 
168 void SAL_CALL PropertySetHelper::addPropertiesChangeListener( const Sequence< OUString >&, const Reference< XPropertiesChangeListener >& )
169 {
170  // todo
171 }
172 
173 void SAL_CALL PropertySetHelper::removePropertiesChangeListener( const Reference< XPropertiesChangeListener >& )
174 {
175  // todo
176 }
177 
178 void SAL_CALL PropertySetHelper::firePropertiesChangeEvent( const Sequence< OUString >&, const Reference< XPropertiesChangeListener >& )
179 {
180  // todo
181 }
182 
183 // XPropertyState
184 PropertyState SAL_CALL PropertySetHelper::getPropertyState( const OUString& PropertyName )
185 {
186  PropertyMapEntry const * aEntries[2];
187 
188  aEntries[0] = find( mxInfo, PropertyName );
189  if( aEntries[0] == nullptr )
190  throw UnknownPropertyException( PropertyName, static_cast< XPropertySet* >( this ) );
191 
192  aEntries[1] = nullptr;
193 
194  PropertyState aState(PropertyState_AMBIGUOUS_VALUE);
195  _getPropertyStates( aEntries, &aState );
196 
197  return aState;
198 }
199 
200 Sequence< PropertyState > SAL_CALL PropertySetHelper::getPropertyStates( const Sequence< OUString >& aPropertyName )
201 {
202  const sal_Int32 nCount = aPropertyName.getLength();
203 
204  Sequence< PropertyState > aStates( nCount );
205 
206  if( nCount )
207  {
208  const OUString* pNames = aPropertyName.getConstArray();
209 
210  bool bUnknown = false;
211 
212  std::unique_ptr<PropertyMapEntry const *[]> pEntries(new PropertyMapEntry const *[nCount+1]);
213 
214  sal_Int32 n;
215  for( n = 0; !bUnknown && (n < nCount); n++, pNames++ )
216  {
217  pEntries[n] = find( mxInfo, *pNames );
218  bUnknown = nullptr == pEntries[n];
219  }
220 
221  pEntries[nCount] = nullptr;
222 
223  if( !bUnknown )
224  _getPropertyStates( pEntries.get(), aStates.getArray() );
225 
226  if( bUnknown )
227  throw UnknownPropertyException( *pNames, static_cast< XPropertySet* >( this ) );
228  }
229 
230  return aStates;
231 }
232 
233 void SAL_CALL PropertySetHelper::setPropertyToDefault( const OUString& PropertyName )
234 {
235  PropertyMapEntry const *pEntry = find(mxInfo, PropertyName );
236  if( nullptr == pEntry )
237  throw UnknownPropertyException( PropertyName, static_cast< XPropertySet* >( this ) );
238 
239  _setPropertyToDefault( pEntry );
240 }
241 
242 Any SAL_CALL PropertySetHelper::getPropertyDefault( const OUString& aPropertyName )
243 {
244  PropertyMapEntry const * pEntry = find(mxInfo, aPropertyName );
245  if( nullptr == pEntry )
246  throw UnknownPropertyException( aPropertyName, static_cast< XPropertySet* >( this ) );
247 
248  return _getPropertyDefault( pEntry );
249 }
250 
251 void PropertySetHelper::_getPropertyStates(
252  const comphelper::PropertyMapEntry**, PropertyState*)
253 {
254  OSL_FAIL( "you have to implement this yourself!");
255 }
256 
257 void
258 PropertySetHelper::_setPropertyToDefault(const comphelper::PropertyMapEntry*)
259 {
260  OSL_FAIL( "you have to implement this yourself!");
261 }
262 
263 Any PropertySetHelper::_getPropertyDefault(const comphelper::PropertyMapEntry*)
264 {
265  OSL_FAIL( "you have to implement this yourself!");
266 
267  Any aAny;
268  return aAny;
269 }
270 
271 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_Int64 n
int nCount
std::pair< OUString const, DataAccessDescriptorProperty > PropertyMapEntry
static PropertyMapEntry const * find(rtl::Reference< PropertySetInfo > &mxInfo, const OUString &aName)
OUString aName
ScXMLEditAttributeMap::Entry const aEntries[]