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