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