1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <svx/shapepropertynotifier.hxx>
23 #include <com/sun/star/beans/XPropertySet.hpp>
25 #include <cppuhelper/interfacecontainer.hxx>
26 #include <cppuhelper/weak.hxx>
27 #include <tools/diagnose_ex.h>
29 #include <unordered_map>
34 struct ShapePropertyHash
36 size_t operator()( svx::ShapeProperty x
) const
48 using ::com::sun::star::uno::Reference
;
49 using ::com::sun::star::uno::UNO_QUERY_THROW
;
50 using ::com::sun::star::uno::Exception
;
51 using ::com::sun::star::uno::Any
;
52 using ::com::sun::star::beans::PropertyChangeEvent
;
53 using ::com::sun::star::beans::XPropertyChangeListener
;
54 using ::com::sun::star::lang::EventObject
;
55 using ::com::sun::star::beans::XPropertySet
;
57 typedef std::unordered_map
< ShapeProperty
, std::shared_ptr
<IPropertyValueProvider
>, ShapePropertyHash
> PropertyProviders
;
59 typedef cppu::OMultiTypeInterfaceContainerHelperVar
<OUString
>
60 PropertyChangeListenerContainer
;
62 IPropertyValueProvider::~IPropertyValueProvider()
66 struct PropertyChangeNotifier_Data
68 ::cppu::OWeakObject
& m_rContext
;
69 PropertyProviders m_aProviders
;
70 PropertyChangeListenerContainer m_aPropertyChangeListeners
;
72 PropertyChangeNotifier_Data( ::cppu::OWeakObject
& _rContext
, ::osl::Mutex
& _rMutex
)
73 :m_rContext( _rContext
)
74 ,m_aPropertyChangeListeners( _rMutex
)
79 //= PropertyValueProvider
82 OUString
PropertyValueProvider::getPropertyName() const
84 return m_sPropertyName
;
88 void PropertyValueProvider::getCurrentValue( Any
& _out_rValue
) const
90 Reference
< XPropertySet
> xContextProps( const_cast< PropertyValueProvider
* >( this )->m_rContext
, UNO_QUERY_THROW
);
91 _out_rValue
= xContextProps
->getPropertyValue( getPropertyName() );
94 PropertyChangeNotifier::PropertyChangeNotifier( ::cppu::OWeakObject
& _rOwner
, ::osl::Mutex
& _rMutex
)
95 :m_xData( new PropertyChangeNotifier_Data( _rOwner
, _rMutex
) )
99 PropertyChangeNotifier::~PropertyChangeNotifier()
103 void PropertyChangeNotifier::registerProvider(const ShapeProperty _eProperty
, const std::shared_ptr
<IPropertyValueProvider
>& _rProvider
)
105 ENSURE_OR_THROW( !!_rProvider
, "NULL factory not allowed." );
107 OSL_ENSURE( m_xData
->m_aProviders
.find( _eProperty
) == m_xData
->m_aProviders
.end(),
108 "PropertyChangeNotifier::registerProvider: factory for this ID already present!" );
110 m_xData
->m_aProviders
[ _eProperty
] = _rProvider
;
113 void PropertyChangeNotifier::notifyPropertyChange( const ShapeProperty _eProperty
) const
115 PropertyProviders::const_iterator provPos
= m_xData
->m_aProviders
.find( _eProperty
);
116 OSL_ENSURE( provPos
!= m_xData
->m_aProviders
.end(), "PropertyChangeNotifier::notifyPropertyChange: no factory!" );
117 if ( provPos
== m_xData
->m_aProviders
.end() )
120 OUString
sPropertyName( provPos
->second
->getPropertyName() );
122 ::cppu::OInterfaceContainerHelper
* pPropListeners
= m_xData
->m_aPropertyChangeListeners
.getContainer( sPropertyName
);
123 ::cppu::OInterfaceContainerHelper
* pAllListeners
= m_xData
->m_aPropertyChangeListeners
.getContainer( OUString() );
124 if ( !pPropListeners
&& !pAllListeners
)
129 PropertyChangeEvent aEvent
;
130 aEvent
.Source
= m_xData
->m_rContext
;
131 // Handle/OldValue not supported
132 aEvent
.PropertyName
= provPos
->second
->getPropertyName();
133 provPos
->second
->getCurrentValue( aEvent
.NewValue
);
135 if ( pPropListeners
)
136 pPropListeners
->notifyEach( &XPropertyChangeListener::propertyChange
, aEvent
);
138 pAllListeners
->notifyEach( &XPropertyChangeListener::propertyChange
, aEvent
);
140 catch( const Exception
& )
142 DBG_UNHANDLED_EXCEPTION("svx");
147 void PropertyChangeNotifier::addPropertyChangeListener( const OUString
& _rPropertyName
, const Reference
< XPropertyChangeListener
>& _rxListener
)
149 m_xData
->m_aPropertyChangeListeners
.addInterface( _rPropertyName
, _rxListener
);
153 void PropertyChangeNotifier::removePropertyChangeListener( const OUString
& _rPropertyName
, const Reference
< XPropertyChangeListener
>& _rxListener
)
155 m_xData
->m_aPropertyChangeListeners
.removeInterface( _rPropertyName
, _rxListener
);
159 void PropertyChangeNotifier::disposing()
162 aEvent
.Source
= m_xData
->m_rContext
;
163 m_xData
->m_aPropertyChangeListeners
.disposeAndClear( aEvent
);
170 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */