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 
36 namespace canvas
37 {
38     /** Really simplistic XPropertySet helper for properties.
39 
40         This class provides easy access to properties, referenced via
41         ASCII strings. The name/property modification callbacks pairs
42         are passed into this class via a vector. Each time a property
43         is set or queried, the corresponding getter or setter callback
44         is called.
45 
46         Use this class as a delegate for the corresponding
47         XPropertySet methods, and take care of UNO XInterface and lock
48         handling by yourself.
49 
50         The core responsibility of this this class is the name/value
51         mapping for property sets.
52      */
53     class PropertySetHelper
54     {
55     public:
56         typedef boost::function0< ::com::sun::star::uno::Any >            GetterType;
57         typedef boost::function1<void, const ::com::sun::star::uno::Any&> SetterType;
58         struct Callbacks
59         {
60             GetterType getter;
61             SetterType setter;
62         };
63         typedef tools::ValueMap< Callbacks >     MapType;
64         typedef std::vector< MapType::MapEntry > InputMap;
65 
66         class MakeMap : public InputMap
67         {
68         public:
MakeMap(const char * name,const GetterType & getter,const SetterType & setter)69             MakeMap(const char*        name,
70                     const GetterType&  getter,
71                     const SetterType&  setter)
72             {
73                 MapType::MapEntry aEntry={name, {getter, setter}};
74                 this->push_back(aEntry);
75             }
MakeMap(const char * name,const GetterType & getter)76             MakeMap(const char*       name,
77                     const GetterType& getter)
78             {
79                 MapType::MapEntry aEntry={name, {getter, SetterType()}};
80                 this->push_back(aEntry);
81             }
operator ()(const char * name,const GetterType & getter,const SetterType & setter)82             MakeMap& operator()(const char*        name,
83                                 const GetterType&  getter,
84                                 const SetterType&  setter)
85             {
86                 MapType::MapEntry aEntry={name, {getter, setter}};
87                 this->push_back(aEntry);
88                 return *this;
89             }
operator ()(const char * name,const GetterType & getter)90             MakeMap& operator()(const char*       name,
91                                 const GetterType& getter)
92             {
93                 MapType::MapEntry aEntry={name, {getter, SetterType()}};
94                 this->push_back(aEntry);
95                 return *this;
96             }
97         };
98 
99         /** Create helper with zero properties
100          */
101         PropertySetHelper();
102 
103         /** Create helper with given name/value map
104          */
105         explicit PropertySetHelper( const InputMap& rMap );
106 
107         /** Init helper with new name/value map
108 
109             @param rMap
110             Vector of name/function pointers. Each name is offered as
111             a property, and reading/writing to this property is passed
112             on to the given function pointer.
113          */
114         void initProperties( const InputMap& rMap );
115 
116         /** Add given properties to helper
117 
118             @param rMap
119             Vector of name/function pointers. Each name is offered as
120             a property, and reading/writing to this property is passed
121             on to the given function pointer. These name/function
122             pairs are added to the already existing ones.
123          */
124         void addProperties( const InputMap& rMap );
125 
126         /** Checks whether the given string corresponds to a valid
127             property name.
128 
129             @return true, if the given name maps to a known property.
130          */
131         bool isPropertyName( const ::rtl::OUString& aPropertyName ) const;
132 
133         /** Request the currently active map
134          */
getPropertyMap() const135         const InputMap& getPropertyMap() const { return maMapEntries; }
136 
137         // XPropertySet implementation
138         ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySetInfo > getPropertySetInfo() const;
139         void setPropertyValue( const ::rtl::OUString&            aPropertyName,
140                                const ::com::sun::star::uno::Any& aValue );
141         ::com::sun::star::uno::Any getPropertyValue( const ::rtl::OUString& PropertyName ) const;
142         void addPropertyChangeListener( const ::rtl::OUString& aPropertyName,
143                                         const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertyChangeListener >& xListener );
144         void removePropertyChangeListener( const ::rtl::OUString& aPropertyName,
145                                            const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertyChangeListener >& xListener );
146         void addVetoableChangeListener( const ::rtl::OUString& aPropertyName,
147                                         const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XVetoableChangeListener >& xListener );
148         void removeVetoableChangeListener( const ::rtl::OUString& aPropertyName,
149                                            const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XVetoableChangeListener >& xListener );
150 
151     private:
152         std::auto_ptr<MapType>  mpMap;
153         InputMap                maMapEntries;
154     };
155 }
156 
157 #endif /* INCLUDED_CANVAS_PROPERTYSETHELPER_HXX */
158