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 <unordered_map>
30 :
31 : namespace
32 : {
33 :
34 : struct ShapePropertyHash
35 : {
36 880722 : size_t operator()( svx::ShapeProperty __x ) const
37 : {
38 880722 : 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 std::unordered_map< ShapeProperty, PPropertyValueProvider, ShapePropertyHash > PropertyProviders;
65 :
66 : typedef cppu::OMultiTypeInterfaceContainerHelperVar<OUString>
67 : PropertyChangeListenerContainer;
68 :
69 340311 : IPropertyValueProvider::~IPropertyValueProvider()
70 : {
71 340311 : }
72 :
73 166705 : struct PropertyChangeNotifier_Data
74 : {
75 : ::cppu::OWeakObject& m_rContext;
76 : PropertyProviders m_aProviders;
77 : PropertyChangeListenerContainer m_aPropertyChangeListeners;
78 :
79 166977 : PropertyChangeNotifier_Data( ::cppu::OWeakObject& _rContext, ::osl::Mutex& _rMutex )
80 : :m_rContext( _rContext )
81 166977 : ,m_aPropertyChangeListeners( _rMutex )
82 : {
83 166977 : }
84 : };
85 :
86 : //= PropertyValueProvider
87 :
88 :
89 539633 : OUString PropertyValueProvider::getPropertyName() const
90 : {
91 539633 : 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 166977 : PropertyChangeNotifier::PropertyChangeNotifier( ::cppu::OWeakObject& _rOwner, ::osl::Mutex& _rMutex )
102 166977 : :m_xData( new PropertyChangeNotifier_Data( _rOwner, _rMutex ) )
103 : {
104 166977 : }
105 :
106 166705 : PropertyChangeNotifier::~PropertyChangeNotifier()
107 : {
108 166705 : }
109 :
110 340858 : void PropertyChangeNotifier::registerProvider(const ShapeProperty _eProperty, const PPropertyValueProvider& _rProvider)
111 : {
112 340858 : ENSURE_OR_THROW( _eProperty != eInvalidShapeProperty, "Illegal ShapeProperty value!" );
113 340858 : ENSURE_OR_THROW( !!_rProvider, "NULL factory not allowed." );
114 :
115 : OSL_ENSURE( m_xData->m_aProviders.find( _eProperty ) == m_xData->m_aProviders.end(),
116 : "PropertyChangeNotifier::registerProvider: factory for this ID already present!" );
117 :
118 340858 : m_xData->m_aProviders[ _eProperty ] = _rProvider;
119 340858 : }
120 :
121 539864 : void PropertyChangeNotifier::notifyPropertyChange( const ShapeProperty _eProperty ) const
122 : {
123 539864 : ENSURE_OR_THROW( _eProperty != eInvalidShapeProperty, "Illegal ShapeProperty value!" );
124 :
125 539864 : PropertyProviders::const_iterator provPos = m_xData->m_aProviders.find( _eProperty );
126 : OSL_ENSURE( provPos != m_xData->m_aProviders.end(), "PropertyChangeNotifier::notifyPropertyChange: no factory!" );
127 539864 : if ( provPos == m_xData->m_aProviders.end() )
128 540095 : return;
129 :
130 539633 : OUString sPropertyName( provPos->second->getPropertyName() );
131 :
132 539633 : ::cppu::OInterfaceContainerHelper* pPropListeners = m_xData->m_aPropertyChangeListeners.getContainer( sPropertyName );
133 539633 : ::cppu::OInterfaceContainerHelper* pAllListeners = m_xData->m_aPropertyChangeListeners.getContainer( OUString() );
134 539633 : if ( !pPropListeners && !pAllListeners )
135 539633 : return;
136 :
137 : try
138 : {
139 0 : PropertyChangeEvent aEvent;
140 0 : aEvent.Source = m_xData->m_rContext;
141 : // Handle/OldValue not supported
142 0 : aEvent.PropertyName = provPos->second->getPropertyName();
143 0 : provPos->second->getCurrentValue( aEvent.NewValue );
144 :
145 0 : if ( pPropListeners )
146 0 : pPropListeners->notifyEach( &XPropertyChangeListener::propertyChange, aEvent );
147 0 : if ( pAllListeners )
148 0 : pAllListeners->notifyEach( &XPropertyChangeListener::propertyChange, aEvent );
149 : }
150 0 : catch( const Exception& )
151 : {
152 : DBG_UNHANDLED_EXCEPTION();
153 0 : }
154 : }
155 :
156 :
157 0 : void PropertyChangeNotifier::addPropertyChangeListener( const OUString& _rPropertyName, const Reference< XPropertyChangeListener >& _rxListener )
158 : {
159 0 : m_xData->m_aPropertyChangeListeners.addInterface( _rPropertyName, _rxListener );
160 0 : }
161 :
162 :
163 0 : void PropertyChangeNotifier::removePropertyChangeListener( const OUString& _rPropertyName, const Reference< XPropertyChangeListener >& _rxListener )
164 : {
165 0 : m_xData->m_aPropertyChangeListeners.removeInterface( _rPropertyName, _rxListener );
166 0 : }
167 :
168 :
169 4976 : void PropertyChangeNotifier::disposing()
170 : {
171 4976 : EventObject aEvent;
172 4976 : aEvent.Source = m_xData->m_rContext;
173 4976 : m_xData->m_aPropertyChangeListeners.disposeAndClear( aEvent );
174 4976 : }
175 :
176 :
177 : }
178 :
179 :
180 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|