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 .
20 #include <ContainerMediator.hxx>
21 #include <PropertyForward.hxx>
23 #include <com/sun/star/beans/PropertyAttribute.hpp>
24 #include <com/sun/star/container/XNameContainer.hpp>
25 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
26 #include <com/sun/star/sdbcx/XRename.hpp>
27 #include <comphelper/property.hxx>
28 #include <comphelper/diagnose_ex.hxx>
32 using namespace ::com::sun::star::uno
;
33 using namespace ::com::sun::star::lang
;
34 using namespace ::com::sun::star::sdbcx
;
35 using namespace ::com::sun::star::beans
;
36 using namespace ::com::sun::star::container
;
38 OContainerMediator::OContainerMediator( const Reference
< XContainer
>& _xContainer
, const Reference
< XNameAccess
>& _xSettings
)
39 : m_xSettings( _xSettings
)
40 , m_xContainer( _xContainer
)
43 if ( _xSettings
.is() && _xContainer
.is() )
45 osl_atomic_increment(&m_refCount
);
48 m_xContainer
->addContainerListener(this);
49 Reference
< XContainer
> xContainer(_xSettings
, UNO_QUERY
);
50 if ( xContainer
.is() )
51 xContainer
->addContainerListener(this);
55 TOOLS_WARN_EXCEPTION("dbaccess", "OContainerMediator::OContainerMediator");
57 osl_atomic_decrement( &m_refCount
);
66 OContainerMediator::~OContainerMediator()
69 impl_cleanup_nothrow();
72 void OContainerMediator::impl_cleanup_nothrow()
76 Reference
< XContainer
> xContainer( m_xSettings
, UNO_QUERY
);
77 if ( xContainer
.is() )
78 xContainer
->removeContainerListener( this );
81 xContainer
= m_xContainer
;
82 if ( xContainer
.is() )
83 xContainer
->removeContainerListener( this );
86 m_aForwardList
.clear();
88 catch( const Exception
& )
90 DBG_UNHANDLED_EXCEPTION("dbaccess");
94 void SAL_CALL
OContainerMediator::elementInserted( const ContainerEvent
& _rEvent
)
96 ::osl::MutexGuard
aGuard(m_aMutex
);
97 if ( _rEvent
.Source
== m_xSettings
&& m_xSettings
.is() )
99 OUString sElementName
;
100 _rEvent
.Accessor
>>= sElementName
;
101 PropertyForwardList::const_iterator aFind
= m_aForwardList
.find(sElementName
);
102 if ( aFind
!= m_aForwardList
.end() )
104 Reference
< XPropertySet
> xDest(_rEvent
.Element
,UNO_QUERY
);
105 aFind
->second
->setDefinition( xDest
);
110 void SAL_CALL
OContainerMediator::elementRemoved( const ContainerEvent
& _rEvent
)
112 ::osl::MutexGuard
aGuard(m_aMutex
);
113 Reference
< XContainer
> xContainer
= m_xContainer
;
114 if ( !(_rEvent
.Source
== xContainer
&& xContainer
.is()) )
117 OUString sElementName
;
118 _rEvent
.Accessor
>>= sElementName
;
119 m_aForwardList
.erase(sElementName
);
122 Reference
<XNameContainer
> xNameContainer( m_xSettings
, UNO_QUERY
);
123 if ( xNameContainer
.is() && m_xSettings
->hasByName( sElementName
) )
124 xNameContainer
->removeByName( sElementName
);
126 catch( const Exception
& )
128 DBG_UNHANDLED_EXCEPTION("dbaccess");
132 void SAL_CALL
OContainerMediator::elementReplaced( const ContainerEvent
& _rEvent
)
134 Reference
< XContainer
> xContainer
= m_xContainer
;
135 if ( !(_rEvent
.Source
== xContainer
&& xContainer
.is()) )
138 OUString sElementName
;
139 _rEvent
.ReplacedElement
>>= sElementName
;
141 PropertyForwardList::const_iterator aFind
= m_aForwardList
.find(sElementName
);
142 if ( aFind
== m_aForwardList
.end() )
146 _rEvent
.Accessor
>>= sNewName
;
149 Reference
<XNameContainer
> xNameContainer( m_xSettings
, UNO_QUERY_THROW
);
150 if ( xNameContainer
.is() && m_xSettings
->hasByName( sElementName
) )
152 Reference
<XRename
> xSource(m_xSettings
->getByName(sElementName
),UNO_QUERY_THROW
);
153 xSource
->rename(sNewName
);
156 catch( const Exception
& )
158 DBG_UNHANDLED_EXCEPTION("dbaccess");
161 aFind
->second
->setName(sNewName
);
164 void SAL_CALL
OContainerMediator::disposing( const EventObject
& /*Source*/ )
166 ::osl::MutexGuard
aGuard(m_aMutex
);
168 impl_cleanup_nothrow();
171 void OContainerMediator::impl_initSettings_nothrow( const OUString
& _rName
, const Reference
< XPropertySet
>& _rxDestination
)
175 if ( m_xSettings
.is() && m_xSettings
->hasByName( _rName
) )
177 Reference
< XPropertySet
> xSettings( m_xSettings
->getByName( _rName
), UNO_QUERY_THROW
);
178 ::comphelper::copyProperties( xSettings
, _rxDestination
);
181 catch( const Exception
& )
183 DBG_UNHANDLED_EXCEPTION("dbaccess");
187 void OContainerMediator::notifyElementCreated( const OUString
& _sName
, const Reference
< XPropertySet
>& _xDest
)
189 if ( !m_xSettings
.is() )
192 PropertyForwardList::const_iterator aFind
= m_aForwardList
.find( _sName
);
193 if ( aFind
!= m_aForwardList
.end()
194 && aFind
->second
->getDefinition().is()
197 OSL_FAIL( "OContainerMediator::notifyElementCreated: is this really a valid case?" );
201 std::vector
< OUString
> aPropertyList
;
204 // initially copy from the settings object (if existent) to the newly created object
205 impl_initSettings_nothrow( _sName
, _xDest
);
207 // collect the to-be-monitored properties
208 Reference
< XPropertySetInfo
> xPSI( _xDest
->getPropertySetInfo(), UNO_SET_THROW
);
209 const Sequence
< Property
> aProperties( xPSI
->getProperties() );
210 for ( auto const & property
: aProperties
)
212 if ( ( property
.Attributes
& PropertyAttribute::READONLY
) != 0 )
214 if ( ( property
.Attributes
& PropertyAttribute::BOUND
) == 0 )
217 aPropertyList
.push_back( property
.Name
);
220 catch( const Exception
& )
222 DBG_UNHANDLED_EXCEPTION("dbaccess");
225 ::rtl::Reference
pForward( new OPropertyForward( _xDest
, m_xSettings
, _sName
, aPropertyList
) );
226 m_aForwardList
[ _sName
] = pForward
;
229 } // namespace dbaccess
231 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */