1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef COMPHELPER_OPROPERTYBAG_HXX
29 #define COMPHELPER_OPROPERTYBAG_HXX
30 
31 /** === begin UNO includes === **/
32 #include <com/sun/star/lang/XInitialization.hpp>
33 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
34 #include <com/sun/star/lang/XServiceInfo.hpp>
35 #include <com/sun/star/util/XModifiable.hpp>
36 #include <com/sun/star/beans/XPropertySet.hpp>
37 #include <com/sun/star/beans/XPropertyContainer.hpp>
38 #include <com/sun/star/beans/XPropertyAccess.hpp>
39 #include <com/sun/star/uno/XComponentContext.hpp>
40 #include <com/sun/star/container/XSet.hpp>
41 /** === end UNO includes === **/
42 
43 #include <cppuhelper/implbase6.hxx>
44 #include <comphelper/propstate.hxx>
45 #include <comphelper/broadcasthelper.hxx>
46 #include <comphelper/propertybag.hxx>
47 #include <comphelper/componentcontext.hxx>
48 #include <comphelper/uno3.hxx>
49 
50 #include <map>
51 #include <set>
52 #include <memory>
53 
54 //........................................................................
55 namespace comphelper
56 {
57 //........................................................................
58 
59     struct SAL_DLLPRIVATE UnoTypeLess : public ::std::unary_function< ::com::sun::star::uno::Type, bool >
60     {
61         inline bool operator()( const ::com::sun::star::uno::Type& _rLHS, const ::com::sun::star::uno::Type& _rRHS ) const
62         {
63             return rtl_ustr_compare(
64                 _rLHS.getTypeLibType()->pTypeName->buffer,
65                 _rRHS.getTypeLibType()->pTypeName->buffer
66             ) < 0;
67         }
68     };
69 
70     typedef ::std::map< sal_Int32, ::com::sun::star::uno::Any >     MapInt2Any;
71     typedef ::std::set< ::com::sun::star::uno::Type, UnoTypeLess >  TypeBag;
72 
73     //====================================================================
74 	//= OPropertyBag
75 	//====================================================================
76     typedef ::cppu::WeakAggImplHelper6  <   ::com::sun::star::beans::XPropertyContainer
77                                         ,   ::com::sun::star::beans::XPropertyAccess
78                                         ,   ::com::sun::star::util::XModifiable
79                                         ,   ::com::sun::star::lang::XServiceInfo
80                                         ,   ::com::sun::star::lang::XInitialization
81                                         ,   ::com::sun::star::container::XSet
82                                         >   OPropertyBag_Base;
83     typedef ::comphelper::OPropertyStateHelper  OPropertyBag_PBase;
84 
85     class OPropertyBag  :public ::comphelper::OMutexAndBroadcastHelper  // must be before OPropertyBag_PBase
86                         ,public OPropertyBag_PBase
87                         ,public OPropertyBag_Base
88                         ,public ::cppu::IEventNotificationHook
89 	{
90     private:
91         ::comphelper::ComponentContext
92                         m_aContext;
93 
94         /// our IPropertyArrayHelper implementation
95         ::std::auto_ptr< ::cppu::OPropertyArrayHelper >
96                         m_pArrayHelper;
97         ::comphelper::PropertyBag
98                         m_aDynamicProperties;
99         /// set of allowed property types
100         TypeBag         m_aAllowedTypes;
101         /// should we automatically add properties which are tried to set, if they don't exist previously?
102         bool            m_bAutoAddProperties;
103 
104         /// for notification
105         ::cppu::OInterfaceContainerHelper m_NotifyListeners;
106         /// modify flag
107         bool            m_isModified;
108 
109     public:
110         OPropertyBag( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& _rxContext );
111 
112         // XServiceInfo - static versions
113 		static ::com::sun::star::uno::Sequence< ::rtl::OUString > getSupportedServiceNames_static(void) throw( ::com::sun::star::uno::RuntimeException );
114 		static ::rtl::OUString getImplementationName_static(void) throw( ::com::sun::star::uno::RuntimeException );
115 		static ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >
116                 SAL_CALL Create(const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >&);
117 
118     protected:
119         virtual ~OPropertyBag();
120         DECLARE_XINTERFACE()
121         DECLARE_XTYPEPROVIDER()
122 
123         /** === begin UNO interface implementations == **/
124         // XInitialization
125         virtual void SAL_CALL initialize( const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& aArguments ) throw (::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException);
126 
127         // XServiceInfo
128         virtual ::rtl::OUString SAL_CALL getImplementationName(  ) throw (::com::sun::star::uno::RuntimeException);
129         virtual ::sal_Bool SAL_CALL supportsService( const ::rtl::OUString& ServiceName ) throw (::com::sun::star::uno::RuntimeException);
130         virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames(  ) throw (::com::sun::star::uno::RuntimeException);
131 
132         // XModifiable:
133         virtual ::sal_Bool SAL_CALL isModified(  )
134             throw (::com::sun::star::uno::RuntimeException);
135         virtual void SAL_CALL setModified( ::sal_Bool bModified )
136             throw (::com::sun::star::beans::PropertyVetoException,
137                     ::com::sun::star::uno::RuntimeException);
138 
139         // XModifyBroadcaster
140         virtual void SAL_CALL addModifyListener(
141             const ::com::sun::star::uno::Reference<
142                     ::com::sun::star::util::XModifyListener > & xListener)
143             throw (::com::sun::star::uno::RuntimeException);
144         virtual void SAL_CALL removeModifyListener(
145             const ::com::sun::star::uno::Reference<
146                     ::com::sun::star::util::XModifyListener > & xListener)
147             throw (::com::sun::star::uno::RuntimeException);
148 
149         // XPropertyContainer
150         virtual void SAL_CALL addProperty( const ::rtl::OUString& Name, ::sal_Int16 Attributes, const ::com::sun::star::uno::Any& DefaultValue ) throw (::com::sun::star::beans::PropertyExistException, ::com::sun::star::beans::IllegalTypeException, ::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException);
151         virtual void SAL_CALL removeProperty( const ::rtl::OUString& Name ) throw (::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::beans::NotRemoveableException, ::com::sun::star::uno::RuntimeException);
152 
153         // XPropertyAccess
154         virtual ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue > SAL_CALL getPropertyValues(  ) throw (::com::sun::star::uno::RuntimeException);
155         virtual void SAL_CALL setPropertyValues( const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >& aProps ) throw (::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::beans::PropertyVetoException, ::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException);
156 
157         // XPropertySet
158         virtual ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySetInfo > SAL_CALL getPropertySetInfo(  ) throw(::com::sun::star::uno::RuntimeException);
159 
160         // XSet
161         virtual ::sal_Bool SAL_CALL has( const ::com::sun::star::uno::Any& aElement ) throw (::com::sun::star::uno::RuntimeException);
162         virtual void SAL_CALL insert( const ::com::sun::star::uno::Any& aElement ) throw (::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::container::ElementExistException, ::com::sun::star::uno::RuntimeException);
163         virtual void SAL_CALL remove( const ::com::sun::star::uno::Any& aElement ) throw (::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::container::NoSuchElementException, ::com::sun::star::uno::RuntimeException);
164 
165         // XEnumerationAccess (base of XSet)
166         virtual ::com::sun::star::uno::Reference< ::com::sun::star::container::XEnumeration > SAL_CALL createEnumeration(  ) throw (::com::sun::star::uno::RuntimeException);
167 
168         // XElementAccess (basf of XEnumerationAccess
169         virtual ::com::sun::star::uno::Type SAL_CALL getElementType(  ) throw (::com::sun::star::uno::RuntimeException);
170         virtual ::sal_Bool SAL_CALL hasElements(  ) throw (::com::sun::star::uno::RuntimeException);
171         /** === UNO interface implementations == **/
172 
173         // XPropertyState
174 	    virtual ::com::sun::star::uno::Any  getPropertyDefaultByHandle( sal_Int32 _nHandle ) const;
175 
176         // OPropertyStateHelper
177         virtual ::com::sun::star::beans::PropertyState  getPropertyStateByHandle( sal_Int32 _nHandle );
178 
179         // OPropertySetHelper
180 	    virtual void SAL_CALL getFastPropertyValue( ::com::sun::star::uno::Any& rValue, sal_Int32 nHandle ) const;
181         virtual sal_Bool SAL_CALL convertFastPropertyValue( ::com::sun::star::uno::Any & rConvertedValue, ::com::sun::star::uno::Any & rOldValue, sal_Int32 nHandle, const ::com::sun::star::uno::Any& rValue ) throw (::com::sun::star::lang::IllegalArgumentException);
182         virtual void SAL_CALL setFastPropertyValue_NoBroadcast( sal_Int32 nHandle, const ::com::sun::star::uno::Any& rValue ) throw (::com::sun::star::uno::Exception);
183         virtual ::cppu::IPropertyArrayHelper& SAL_CALL getInfoHelper();
184 
185         // IEventNotificationHook
186         virtual void fireEvents(
187             sal_Int32 * pnHandles,
188             sal_Int32 nCount,
189             sal_Bool bVetoable,
190             bool bIgnoreRuntimeExceptionsWhileFiring);
191 
192         void SAL_CALL setModifiedImpl( ::sal_Bool bModified,
193             bool bIgnoreRuntimeExceptionsWhileFiring);
194 
195     private:
196         /** finds a free property handle
197             @precond
198                 our mutex is locked
199         */
200         sal_Int32   findFreeHandle() const;
201 
202         /** implements the setPropertyValues method
203             @param _rProps
204                 the property values to set
205 
206             @throws PropertyVetoException
207                 if the XMultiPropertySet::setPropertyValues call does so
208 
209             @throws ::com::sun::star::lang::IllegalArgumentException
210                 if the XMultiPropertySet::setPropertyValues call does so
211 
212             @throws ::com::sun::star::lang::WrappedTargetException
213                 if the XMultiPropertySet::setPropertyValues call does so
214 
215             @throws ::com::sun::star::uno::RuntimeException
216                 if the XMultiPropertySet::setPropertyValues call does so
217 
218             @throws ::com::sun::star::beans::UnknownPropertyException
219                 if the XMultiPropertySet::setPropertyValues call does so, and <arg>_bTolerateUnknownProperties</arg>
220                 was set to <FALSE/>
221 
222             @throws ::com::sun::star::lang::WrappedTargetException
223                 if the XMultiPropertySet::setPropertyValues call did throw an exception not listed
224                 above
225         */
226         void impl_setPropertyValues_throw( const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >& _rProps );
227 
228     private:
229         OPropertyBag();                                 // never implemented
230         OPropertyBag( const OPropertyBag& );            // never implemented
231         OPropertyBag& operator=( const OPropertyBag& ); // never implemented
232     protected:
233         using ::cppu::OPropertySetHelper::getPropertyValues;
234         using ::cppu::OPropertySetHelper::setPropertyValues;
235         using ::cppu::OPropertySetHelper::getFastPropertyValue;
236     };
237 
238 //........................................................................
239 } // namespace comphelper
240 //........................................................................
241 
242 #endif // COMPHELPER_OPROPERTYBAG_HXX
243