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 650281 : size_t operator()( ::svx::ShapeProperty __x ) const
37 : {
38 650281 : 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 : PropertyChangeListenerContainer;
68 :
69 268176 : IPropertyValueProvider::~IPropertyValueProvider()
70 : {
71 268176 : }
72 :
73 127926 : struct PropertyChangeNotifier_Data
74 : {
75 : ::cppu::OWeakObject& m_rContext;
76 : PropertyProviders m_aProviders;
77 : PropertyChangeListenerContainer m_aPropertyChangeListeners;
78 :
79 128386 : PropertyChangeNotifier_Data( ::cppu::OWeakObject& _rContext, ::osl::Mutex& _rMutex )
80 : :m_rContext( _rContext )
81 128386 : ,m_aPropertyChangeListeners( _rMutex )
82 : {
83 128386 : }
84 : };
85 :
86 : //= PropertyValueProvider
87 :
88 :
89 380741 : OUString PropertyValueProvider::getPropertyName() const
90 : {
91 380741 : return m_sPropertyName;
92 : }
93 :
94 :
95 0 : void PropertyValueProvider::getCurrentValue( Any& _out_rValue ) const
96 : {
97 0 : Reference< XPropertySet > xContextProps( const_cast< PropertyValueProvider* >( this )->m_rContext, UNO_QUERY_THROW );
98 0 : _out_rValue = xContextProps->getPropertyValue( getPropertyName() );
99 0 : }
100 :
101 128386 : PropertyChangeNotifier::PropertyChangeNotifier( ::cppu::OWeakObject& _rOwner, ::osl::Mutex& _rMutex )
102 128386 : :m_pData( new PropertyChangeNotifier_Data( _rOwner, _rMutex ) )
103 : {
104 128386 : }
105 :
106 :
107 127926 : PropertyChangeNotifier::~PropertyChangeNotifier()
108 : {
109 127926 : }
110 :
111 :
112 269102 : void PropertyChangeNotifier::registerProvider( const ShapeProperty _eProperty, const PPropertyValueProvider _pProvider )
113 : {
114 269102 : ENSURE_OR_THROW( _eProperty != eInvalidShapeProperty, "Illegal ShapeProperty value!" );
115 269102 : ENSURE_OR_THROW( !!_pProvider, "NULL factory not allowed." );
116 :
117 : OSL_ENSURE( m_pData->m_aProviders.find( _eProperty ) == m_pData->m_aProviders.end(),
118 : "PropertyChangeNotifier::registerProvider: factory for this ID already present!" );
119 :
120 269102 : m_pData->m_aProviders[ _eProperty ] = _pProvider;
121 269102 : }
122 :
123 :
124 381179 : void PropertyChangeNotifier::notifyPropertyChange( const ShapeProperty _eProperty ) const
125 : {
126 381179 : ENSURE_OR_THROW( _eProperty != eInvalidShapeProperty, "Illegal ShapeProperty value!" );
127 :
128 381179 : PropertyProviders::const_iterator provPos = m_pData->m_aProviders.find( _eProperty );
129 : OSL_ENSURE( provPos != m_pData->m_aProviders.end(), "PropertyChangeNotifier::notifyPropertyChange: no factory!" );
130 381179 : if ( provPos == m_pData->m_aProviders.end() )
131 381617 : return;
132 :
133 380741 : OUString sPropertyName( provPos->second->getPropertyName() );
134 :
135 380741 : ::cppu::OInterfaceContainerHelper* pPropListeners = m_pData->m_aPropertyChangeListeners.getContainer( sPropertyName );
136 380741 : ::cppu::OInterfaceContainerHelper* pAllListeners = m_pData->m_aPropertyChangeListeners.getContainer( OUString() );
137 380741 : if ( !pPropListeners && !pAllListeners )
138 380741 : return;
139 :
140 : try
141 : {
142 0 : PropertyChangeEvent aEvent;
143 0 : aEvent.Source = m_pData->m_rContext;
144 : // Handle/OldValue not supported
145 0 : aEvent.PropertyName = provPos->second->getPropertyName();
146 0 : provPos->second->getCurrentValue( aEvent.NewValue );
147 :
148 0 : if ( pPropListeners )
149 0 : pPropListeners->notifyEach( &XPropertyChangeListener::propertyChange, aEvent );
150 0 : if ( pAllListeners )
151 0 : pAllListeners->notifyEach( &XPropertyChangeListener::propertyChange, aEvent );
152 : }
153 0 : catch( const Exception& )
154 : {
155 : DBG_UNHANDLED_EXCEPTION();
156 0 : }
157 : }
158 :
159 :
160 0 : void PropertyChangeNotifier::addPropertyChangeListener( const OUString& _rPropertyName, const Reference< XPropertyChangeListener >& _rxListener )
161 : {
162 0 : m_pData->m_aPropertyChangeListeners.addInterface( _rPropertyName, _rxListener );
163 0 : }
164 :
165 :
166 0 : void PropertyChangeNotifier::removePropertyChangeListener( const OUString& _rPropertyName, const Reference< XPropertyChangeListener >& _rxListener )
167 : {
168 0 : m_pData->m_aPropertyChangeListeners.removeInterface( _rPropertyName, _rxListener );
169 0 : }
170 :
171 :
172 4314 : void PropertyChangeNotifier::disposing()
173 : {
174 4314 : EventObject aEvent;
175 4314 : aEvent.Source = m_pData->m_rContext;
176 4314 : m_pData->m_aPropertyChangeListeners.disposeAndClear( aEvent );
177 4314 : }
178 :
179 :
180 : } // namespace svx
181 :
182 :
183 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|