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 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_canvas.hxx"
26 
27 #include <canvas/propertysethelper.hxx>
28 
29 using namespace ::com::sun::star;
30 
31 namespace canvas
32 {
33     namespace
34     {
throwUnknown(const::rtl::OUString & aPropertyName)35         void throwUnknown( const ::rtl::OUString& aPropertyName )
36         {
37             throw beans::UnknownPropertyException(
38                 ::rtl::OUString::createFromAscii("PropertySetHelper: property ") +
39                 aPropertyName +
40                 ::rtl::OUString::createFromAscii(" not found."),
41                 uno::Reference< uno::XInterface >()
42                 );
43         }
44 
throwVeto(const::rtl::OUString & aPropertyName)45         void throwVeto( const ::rtl::OUString& aPropertyName )
46         {
47             throw beans::PropertyVetoException(
48                 ::rtl::OUString::createFromAscii("PropertySetHelper: property ") +
49                 aPropertyName +
50                 ::rtl::OUString::createFromAscii(" access was vetoed."),
51                 uno::Reference< uno::XInterface >() );
52         }
53 
54         struct EntryComparator
55         {
operator ()canvas::__anon1bb73f4b0111::EntryComparator56             bool operator()( const PropertySetHelper::MapType::MapEntry& rLHS,
57                              const PropertySetHelper::MapType::MapEntry& rRHS )
58             {
59                 return strcmp( rLHS.maKey,
60                                rRHS.maKey ) < 0;
61             }
62         };
63     }
64 
PropertySetHelper()65     PropertySetHelper::PropertySetHelper() :
66         mpMap(),
67         maMapEntries()
68     {
69     }
70 
PropertySetHelper(const InputMap & rMap)71     PropertySetHelper::PropertySetHelper( const InputMap& rMap ) :
72         mpMap(),
73         maMapEntries()
74     {
75         initProperties(rMap);
76     }
77 
initProperties(const InputMap & rMap)78     void PropertySetHelper::initProperties( const InputMap& rMap )
79     {
80         mpMap.reset();
81         maMapEntries = rMap;
82 
83         std::sort( maMapEntries.begin(),
84                    maMapEntries.end(),
85                    EntryComparator() );
86 
87         if( !maMapEntries.empty() )
88             mpMap.reset( new MapType(&maMapEntries[0],
89                                      maMapEntries.size(),
90                                      true) );
91     }
92 
addProperties(const InputMap & rMap)93     void PropertySetHelper::addProperties( const InputMap& rMap )
94     {
95         InputMap aMerged( getPropertyMap() );
96         aMerged.insert( aMerged.end(),
97                         rMap.begin(),
98                         rMap.end() );
99 
100         initProperties( aMerged );
101     }
102 
isPropertyName(const::rtl::OUString & aPropertyName) const103     bool PropertySetHelper::isPropertyName( const ::rtl::OUString& aPropertyName ) const
104     {
105         if( !mpMap.get() )
106             return false;
107 
108         Callbacks aDummy;
109         return mpMap->lookup( aPropertyName,
110                               aDummy );
111     }
112 
getPropertySetInfo() const113     uno::Reference< beans::XPropertySetInfo > PropertySetHelper::getPropertySetInfo() const
114     {
115         // we're a stealth property set
116         return uno::Reference< beans::XPropertySetInfo >();
117     }
118 
setPropertyValue(const::rtl::OUString & aPropertyName,const uno::Any & aValue)119     void PropertySetHelper::setPropertyValue( const ::rtl::OUString& aPropertyName,
120                                               const uno::Any&        aValue )
121     {
122         Callbacks aCallbacks;
123         if( !mpMap.get() ||
124             !mpMap->lookup( aPropertyName,
125                             aCallbacks ) )
126         {
127             throwUnknown( aPropertyName );
128         }
129 
130         if( aCallbacks.setter.empty() )
131             throwVeto( aPropertyName );
132 
133         aCallbacks.setter(aValue);
134     }
135 
getPropertyValue(const::rtl::OUString & aPropertyName) const136     uno::Any PropertySetHelper::getPropertyValue( const ::rtl::OUString& aPropertyName ) const
137     {
138         Callbacks aCallbacks;
139         if( !mpMap.get() ||
140             !mpMap->lookup( aPropertyName,
141                             aCallbacks ) )
142         {
143             throwUnknown( aPropertyName );
144         }
145 
146         if( !aCallbacks.getter.empty() )
147             return aCallbacks.getter();
148 
149         // TODO(Q1): subtlety, empty getter method silently returns
150         // the empty any
151         return uno::Any();
152     }
153 
addPropertyChangeListener(const::rtl::OUString & aPropertyName,const uno::Reference<beans::XPropertyChangeListener> &)154     void PropertySetHelper::addPropertyChangeListener( const ::rtl::OUString&                                  aPropertyName,
155                                                        const uno::Reference< beans::XPropertyChangeListener >& /*xListener*/ )
156     {
157         // check validity of property, but otherwise ignore the
158         // request
159         if( !isPropertyName( aPropertyName ) )
160             throwUnknown( aPropertyName );
161     }
162 
removePropertyChangeListener(const::rtl::OUString &,const uno::Reference<beans::XPropertyChangeListener> &)163     void PropertySetHelper::removePropertyChangeListener( const ::rtl::OUString&                                  /*aPropertyName*/,
164                                                           const uno::Reference< beans::XPropertyChangeListener >& /*xListener*/ )
165     {
166         // ignore request, no listener added in the first place
167     }
168 
addVetoableChangeListener(const::rtl::OUString & aPropertyName,const uno::Reference<beans::XVetoableChangeListener> &)169     void PropertySetHelper::addVetoableChangeListener( const ::rtl::OUString&                                  aPropertyName,
170                                                        const uno::Reference< beans::XVetoableChangeListener >& /*xListener*/ )
171     {
172         // check validity of property, but otherwise ignore the
173         // request
174         if( !isPropertyName( aPropertyName ) )
175             throwUnknown( aPropertyName );
176     }
177 
removeVetoableChangeListener(const::rtl::OUString &,const uno::Reference<beans::XVetoableChangeListener> &)178     void PropertySetHelper::removeVetoableChangeListener( const ::rtl::OUString&                                  /*aPropertyName*/,
179                                                           const uno::Reference< beans::XVetoableChangeListener >& /*xListener*/ )
180     {
181         // ignore request, no listener added in the first place
182     }
183 }
184