1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef INCLUDED_CANVAS_PROPERTYSETHELPER_HXX
25 #define INCLUDED_CANVAS_PROPERTYSETHELPER_HXX
26 
27 #include <com/sun/star/beans/XPropertySetInfo.hpp>
28 #include <com/sun/star/beans/XPropertySet.hpp>
29 #include <canvas/canvastools.hxx>
30 
31 #include <boost/function.hpp>
32 #include <vector>
33 #include <memory>
34 
35 #include <canvas/canvastoolsdllapi.h>
36 
37 namespace canvas
38 {
39     /** Really simplistic XPropertySet helper for properties.
40 
41         This class provides easy access to properties, referenced via
42         ASCII strings. The name/property modification callbacks pairs
43         are passed into this class via a vector. Each time a property
44         is set or queried, the corresponding getter or setter callback
45         is called.
46 
47         Use this class as a delegate for the corresponding
48         XPropertySet methods, and take care of UNO XInterface and lock
49         handling by yourself.
50 
51         The core responsibility of this this class is the name/value
52         mapping for property sets.
53      */
54     class CANVASTOOLS_DLLPUBLIC PropertySetHelper
55     {
56     public:
57         typedef boost::function0< ::com::sun::star::uno::Any >            GetterType;
58         typedef boost::function1<void, const ::com::sun::star::uno::Any&> SetterType;
59         struct Callbacks
60         {
61             GetterType getter;
62             SetterType setter;
63         };
64         typedef tools::ValueMap< Callbacks >     MapType;
65         typedef std::vector< MapType::MapEntry > InputMap;
66 
67         class MakeMap : public InputMap
68         {
69         public:
MakeMap(const char * name,const GetterType & getter,const SetterType & setter)70             MakeMap(const char*        name,
71                     const GetterType&  getter,
72                     const SetterType&  setter)
73             {
74                 MapType::MapEntry aEntry={name, {getter, setter}};
75                 this->push_back(aEntry);
76             }
MakeMap(const char * name,const GetterType & getter)77             MakeMap(const char*       name,
78                     const GetterType& getter)
79             {
80                 MapType::MapEntry aEntry={name, {getter, SetterType()}};
81                 this->push_back(aEntry);
82             }
operator ()(const char * name,const GetterType & getter,const SetterType & setter)83             MakeMap& operator()(const char*        name,
84                                 const GetterType&  getter,
85                                 const SetterType&  setter)
86             {
87                 MapType::MapEntry aEntry={name, {getter, setter}};
88                 this->push_back(aEntry);
89                 return *this;
90             }
operator ()(const char * name,const GetterType & getter)91             MakeMap& operator()(const char*       name,
92                                 const GetterType& getter)
93             {
94                 MapType::MapEntry aEntry={name, {getter, SetterType()}};
95                 this->push_back(aEntry);
96                 return *this;
97             }
98         };
99 
100         /** Create helper with zero properties
101          */
102         PropertySetHelper();
103 
104         /** Create helper with given name/value map
105          */
106         explicit PropertySetHelper( const InputMap& rMap );
107 
108         /** Init helper with new name/value map
109 
110             @param rMap
111             Vector of name/function pointers. Each name is offered as
112             a property, and reading/writing to this property is passed
113             on to the given function pointer.
114          */
115         void initProperties( const InputMap& rMap );
116 
117         /** Add given properties to helper
118 
119             @param rMap
120             Vector of name/function pointers. Each name is offered as
121             a property, and reading/writing to this property is passed
122             on to the given function pointer. These name/function
123             pairs are added to the already existing ones.
124          */
125         void addProperties( const InputMap& rMap );
126 
127         /** Checks whether the given string corresponds to a valid
128             property name.
129 
130             @return true, if the given name maps to a known property.
131          */
132         bool isPropertyName( const ::rtl::OUString& aPropertyName ) const;
133 
134         /** Request the currently active map
135          */
getPropertyMap() const136         const InputMap& getPropertyMap() const { return maMapEntries; }
137 
138         // XPropertySet implementation
139         ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySetInfo > getPropertySetInfo() const;
140         void setPropertyValue( const ::rtl::OUString&            aPropertyName,
141                                const ::com::sun::star::uno::Any& aValue );
142         ::com::sun::star::uno::Any getPropertyValue( const ::rtl::OUString& PropertyName ) const;
143         void addPropertyChangeListener( const ::rtl::OUString& aPropertyName,
144                                         const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertyChangeListener >& xListener );
145         void removePropertyChangeListener( const ::rtl::OUString& aPropertyName,
146                                            const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertyChangeListener >& xListener );
147         void addVetoableChangeListener( const ::rtl::OUString& aPropertyName,
148                                         const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XVetoableChangeListener >& xListener );
149         void removeVetoableChangeListener( const ::rtl::OUString& aPropertyName,
150                                            const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XVetoableChangeListener >& xListener );
151 
152     private:
153         std::auto_ptr<MapType>  mpMap;
154         InputMap                maMapEntries;
155     };
156 }
157 
158 #endif /* INCLUDED_CANVAS_PROPERTYSETHELPER_HXX */
159