Line data Source code
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 :
21 : #include "svx/shapepropertynotifier.hxx"
22 :
23 : #include <com/sun/star/beans/XPropertySet.hpp>
24 :
25 : #include <cppuhelper/interfacecontainer.hxx>
26 : #include <cppuhelper/weak.hxx>
27 : #include <tools/diagnose_ex.h>
28 :
29 : #include <boost/unordered_map.hpp>
30 :
31 : namespace
32 : {
33 :
34 : struct ShapePropertyHash
35 : {
36 0 : size_t operator()( ::svx::ShapeProperty __x ) const
37 : {
38 0 : return size_t( __x );
39 : }
40 : };
41 : }
42 :
43 :
44 : namespace svx
45 : {
46 :
47 :
48 : using ::com::sun::star::uno::Reference;
49 : using ::com::sun::star::uno::XInterface;
50 : using ::com::sun::star::uno::UNO_QUERY;
51 : using ::com::sun::star::uno::UNO_QUERY_THROW;
52 : using ::com::sun::star::uno::UNO_SET_THROW;
53 : using ::com::sun::star::uno::Exception;
54 : using ::com::sun::star::uno::RuntimeException;
55 : using ::com::sun::star::uno::Any;
56 : using ::com::sun::star::uno::makeAny;
57 : using ::com::sun::star::uno::Sequence;
58 : using ::com::sun::star::uno::Type;
59 : using ::com::sun::star::beans::PropertyChangeEvent;
60 : using ::com::sun::star::beans::XPropertyChangeListener;
61 : using ::com::sun::star::lang::EventObject;
62 : using ::com::sun::star::beans::XPropertySet;
63 :
64 : typedef ::boost::unordered_map< ShapeProperty, PPropertyValueProvider, ShapePropertyHash > PropertyProviders;
65 :
66 : typedef ::cppu::OMultiTypeInterfaceContainerHelperVar < OUString
67 : , OUStringHash
68 : > PropertyChangeListenerContainer;
69 :
70 :
71 : //= IPropertyValueProvider
72 :
73 0 : IPropertyValueProvider::~IPropertyValueProvider()
74 : {
75 0 : }
76 :
77 :
78 : //= PropertyChangeNotifier_Data
79 :
80 0 : struct PropertyChangeNotifier_Data
81 : {
82 : ::cppu::OWeakObject& m_rContext;
83 : PropertyProviders m_aProviders;
84 : PropertyChangeListenerContainer m_aPropertyChangeListeners;
85 :
86 0 : PropertyChangeNotifier_Data( ::cppu::OWeakObject& _rContext, ::osl::Mutex& _rMutex )
87 : :m_rContext( _rContext )
88 0 : ,m_aPropertyChangeListeners( _rMutex )
89 : {
90 0 : }
91 : };
92 :
93 : //= PropertyValueProvider
94 :
95 :
96 0 : OUString PropertyValueProvider::getPropertyName() const
97 : {
98 0 : return m_sPropertyName;
99 : }
100 :
101 :
102 0 : void PropertyValueProvider::getCurrentValue( Any& _out_rValue ) const
103 : {
104 0 : Reference< XPropertySet > xContextProps( const_cast< PropertyValueProvider* >( this )->m_rContext, UNO_QUERY_THROW );
105 0 : _out_rValue = xContextProps->getPropertyValue( getPropertyName() );
106 0 : }
107 :
108 :
109 : //= PropertyChangeNotifier
110 :
111 :
112 0 : PropertyChangeNotifier::PropertyChangeNotifier( ::cppu::OWeakObject& _rOwner, ::osl::Mutex& _rMutex )
113 0 : :m_pData( new PropertyChangeNotifier_Data( _rOwner, _rMutex ) )
114 : {
115 0 : }
116 :
117 :
118 0 : PropertyChangeNotifier::~PropertyChangeNotifier()
119 : {
120 0 : }
121 :
122 :
123 0 : void PropertyChangeNotifier::registerProvider( const ShapeProperty _eProperty, const PPropertyValueProvider _pProvider )
124 : {
125 0 : ENSURE_OR_THROW( _eProperty != eInvalidShapeProperty, "Illegal ShapeProperty value!" );
126 0 : ENSURE_OR_THROW( !!_pProvider, "NULL factory not allowed." );
127 :
128 : OSL_ENSURE( m_pData->m_aProviders.find( _eProperty ) == m_pData->m_aProviders.end(),
129 : "PropertyChangeNotifier::registerProvider: factory for this ID already present!" );
130 :
131 0 : m_pData->m_aProviders[ _eProperty ] = _pProvider;
132 0 : }
133 :
134 :
135 0 : void PropertyChangeNotifier::notifyPropertyChange( const ShapeProperty _eProperty ) const
136 : {
137 0 : ENSURE_OR_THROW( _eProperty != eInvalidShapeProperty, "Illegal ShapeProperty value!" );
138 :
139 0 : PropertyProviders::const_iterator provPos = m_pData->m_aProviders.find( _eProperty );
140 : OSL_ENSURE( provPos != m_pData->m_aProviders.end(), "PropertyChangeNotifier::notifyPropertyChange: no factory!" );
141 0 : if ( provPos == m_pData->m_aProviders.end() )
142 0 : return;
143 :
144 0 : OUString sPropertyName( provPos->second->getPropertyName() );
145 :
146 0 : ::cppu::OInterfaceContainerHelper* pPropListeners = m_pData->m_aPropertyChangeListeners.getContainer( sPropertyName );
147 0 : ::cppu::OInterfaceContainerHelper* pAllListeners = m_pData->m_aPropertyChangeListeners.getContainer( OUString() );
148 0 : if ( !pPropListeners && !pAllListeners )
149 0 : return;
150 :
151 : try
152 : {
153 0 : PropertyChangeEvent aEvent;
154 0 : aEvent.Source = m_pData->m_rContext;
155 : // Handle/OldValue not supported
156 0 : aEvent.PropertyName = provPos->second->getPropertyName();
157 0 : provPos->second->getCurrentValue( aEvent.NewValue );
158 :
159 0 : if ( pPropListeners )
160 0 : pPropListeners->notifyEach( &XPropertyChangeListener::propertyChange, aEvent );
161 0 : if ( pAllListeners )
162 0 : pAllListeners->notifyEach( &XPropertyChangeListener::propertyChange, aEvent );
163 : }
164 0 : catch( const Exception& )
165 : {
166 : DBG_UNHANDLED_EXCEPTION();
167 0 : }
168 : }
169 :
170 :
171 0 : void PropertyChangeNotifier::addPropertyChangeListener( const OUString& _rPropertyName, const Reference< XPropertyChangeListener >& _rxListener )
172 : {
173 0 : m_pData->m_aPropertyChangeListeners.addInterface( _rPropertyName, _rxListener );
174 0 : }
175 :
176 :
177 0 : void PropertyChangeNotifier::removePropertyChangeListener( const OUString& _rPropertyName, const Reference< XPropertyChangeListener >& _rxListener )
178 : {
179 0 : m_pData->m_aPropertyChangeListeners.removeInterface( _rPropertyName, _rxListener );
180 0 : }
181 :
182 :
183 0 : void PropertyChangeNotifier::disposing()
184 : {
185 0 : EventObject aEvent;
186 0 : aEvent.Source = m_pData->m_rContext;
187 0 : m_pData->m_aPropertyChangeListeners.disposeAndClear( aEvent );
188 0 : }
189 :
190 :
191 : } // namespace svx
192 :
193 :
194 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|