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/weak.hxx>
26 #include <comphelper/diagnose_ex.hxx>
38 using ::com::sun::star::uno::Reference
;
39 using ::com::sun::star::uno::UNO_QUERY_THROW
;
40 using ::com::sun::star::uno::Exception
;
41 using ::com::sun::star::uno::Any
;
42 using ::com::sun::star::beans::PropertyChangeEvent
;
43 using ::com::sun::star::beans::XPropertyChangeListener
;
44 using ::com::sun::star::lang::EventObject
;
45 using ::com::sun::star::beans::XPropertySet
;
47 PropertyValueProvider::~PropertyValueProvider()
51 //= PropertyValueProvider
54 const OUString
& PropertyValueProvider::getPropertyName() const
56 return m_sPropertyName
;
60 void PropertyValueProvider::getCurrentValue( Any
& _out_rValue
) const
62 Reference
< XPropertySet
> xContextProps( const_cast< PropertyValueProvider
* >( this )->m_rContext
, UNO_QUERY_THROW
);
63 _out_rValue
= xContextProps
->getPropertyValue( getPropertyName() );
66 PropertyChangeNotifier::PropertyChangeNotifier( ::cppu::OWeakObject
& _rOwner
)
67 :m_rContext( _rOwner
)
71 PropertyChangeNotifier::~PropertyChangeNotifier()
75 void PropertyChangeNotifier::registerProvider(const ShapePropertyProviderId _eProperty
, std::unique_ptr
<PropertyValueProvider
> _rProvider
)
77 assert( _rProvider
&& "NULL factory not allowed." );
79 assert( ! m_aProviders
[_eProperty
] &&
80 "PropertyChangeNotifier::registerProvider: factory for this ID already present!" );
82 m_aProviders
[ _eProperty
] = std::move(_rProvider
);
85 void PropertyChangeNotifier::notifyPropertyChange( std::unique_lock
<std::mutex
>& rGuard
, const ShapePropertyProviderId _eProperty
) const
87 auto & provPos
= m_aProviders
[ _eProperty
];
88 OSL_ENSURE( provPos
, "PropertyChangeNotifier::notifyPropertyChange: no factory!" );
92 const OUString
& sPropertyName( provPos
->getPropertyName() );
94 ::comphelper::OInterfaceContainerHelper4
<XPropertyChangeListener
>* pPropListeners
= m_aPropertyChangeListeners
.getContainer( rGuard
, sPropertyName
);
95 ::comphelper::OInterfaceContainerHelper4
<XPropertyChangeListener
>* pAllListeners
= m_aPropertyChangeListeners
.getContainer( rGuard
, OUString() );
96 if ( !pPropListeners
&& !pAllListeners
)
101 PropertyChangeEvent aEvent
;
102 aEvent
.Source
= m_rContext
;
103 // Handle/OldValue not supported
104 aEvent
.PropertyName
= provPos
->getPropertyName();
105 provPos
->getCurrentValue( aEvent
.NewValue
);
107 if ( pPropListeners
)
108 pPropListeners
->notifyEach( rGuard
, &XPropertyChangeListener::propertyChange
, aEvent
);
110 pAllListeners
->notifyEach( rGuard
, &XPropertyChangeListener::propertyChange
, aEvent
);
112 catch( const Exception
& )
114 DBG_UNHANDLED_EXCEPTION("svx");
119 void PropertyChangeNotifier::addPropertyChangeListener( std::unique_lock
<std::mutex
>& rGuard
, const OUString
& _rPropertyName
, const Reference
< XPropertyChangeListener
>& _rxListener
)
121 m_aPropertyChangeListeners
.addInterface( rGuard
, _rPropertyName
, _rxListener
);
125 void PropertyChangeNotifier::removePropertyChangeListener( std::unique_lock
<std::mutex
>& rGuard
, const OUString
& _rPropertyName
, const Reference
< XPropertyChangeListener
>& _rxListener
)
127 m_aPropertyChangeListeners
.removeInterface( rGuard
, _rPropertyName
, _rxListener
);
131 void PropertyChangeNotifier::disposing(std::unique_lock
<std::mutex
>& rGuard
)
134 aEvent
.Source
= m_rContext
;
135 m_aPropertyChangeListeners
.disposeAndClear( rGuard
, aEvent
);
142 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */