LibreOffice Module dbaccess (master) 1
ContainerMediator.cxx
Go to the documentation of this file.
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#include <ContainerMediator.hxx>
21#include <PropertyForward.hxx>
22
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>
29
30namespace dbaccess
31{
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;
37
38OContainerMediator::OContainerMediator( const Reference< XContainer >& _xContainer, const Reference< XNameAccess >& _xSettings )
39 : m_xSettings( _xSettings )
40 , m_xContainer( _xContainer )
41{
42
43 if ( _xSettings.is() && _xContainer.is() )
44 {
45 osl_atomic_increment(&m_refCount);
46 try
47 {
48 m_xContainer->addContainerListener(this);
49 Reference< XContainer > xContainer(_xSettings, UNO_QUERY);
50 if ( xContainer.is() )
51 xContainer->addContainerListener(this);
52 }
53 catch(Exception&)
54 {
55 TOOLS_WARN_EXCEPTION("dbaccess", "OContainerMediator::OContainerMediator");
56 }
57 osl_atomic_decrement( &m_refCount );
58 }
59 else
60 {
61 m_xSettings.clear();
62 m_xContainer.clear();
63 }
64}
65
67{
68 acquire();
70}
71
73{
74 try
75 {
76 Reference< XContainer > xContainer( m_xSettings, UNO_QUERY );
77 if ( xContainer.is() )
78 xContainer->removeContainerListener( this );
79 m_xSettings.clear();
80
81 xContainer = m_xContainer;
82 if ( xContainer.is() )
83 xContainer->removeContainerListener( this );
84 m_xContainer.clear();
85
86 m_aForwardList.clear();
87 }
88 catch( const Exception& )
89 {
90 DBG_UNHANDLED_EXCEPTION("dbaccess");
91 }
92}
93
94void SAL_CALL OContainerMediator::elementInserted( const ContainerEvent& _rEvent )
95{
96 ::osl::MutexGuard aGuard(m_aMutex);
97 if ( _rEvent.Source == m_xSettings && m_xSettings.is() )
98 {
99 OUString sElementName;
100 _rEvent.Accessor >>= sElementName;
101 PropertyForwardList::const_iterator aFind = m_aForwardList.find(sElementName);
102 if ( aFind != m_aForwardList.end() )
103 {
104 Reference< XPropertySet> xDest(_rEvent.Element,UNO_QUERY);
105 aFind->second->setDefinition( xDest );
106 }
107 }
108}
109
110void SAL_CALL OContainerMediator::elementRemoved( const ContainerEvent& _rEvent )
111{
112 ::osl::MutexGuard aGuard(m_aMutex);
113 Reference< XContainer > xContainer = m_xContainer;
114 if ( !(_rEvent.Source == xContainer && xContainer.is()) )
115 return;
116
117 OUString sElementName;
118 _rEvent.Accessor >>= sElementName;
119 m_aForwardList.erase(sElementName);
120 try
121 {
122 Reference<XNameContainer> xNameContainer( m_xSettings, UNO_QUERY );
123 if ( xNameContainer.is() && m_xSettings->hasByName( sElementName ) )
124 xNameContainer->removeByName( sElementName );
125 }
126 catch( const Exception& )
127 {
128 DBG_UNHANDLED_EXCEPTION("dbaccess");
129 }
130}
131
132void SAL_CALL OContainerMediator::elementReplaced( const ContainerEvent& _rEvent )
133{
134 Reference< XContainer > xContainer = m_xContainer;
135 if ( !(_rEvent.Source == xContainer && xContainer.is()) )
136 return;
137
138 OUString sElementName;
139 _rEvent.ReplacedElement >>= sElementName;
140
141 PropertyForwardList::const_iterator aFind = m_aForwardList.find(sElementName);
142 if ( aFind == m_aForwardList.end() )
143 return;
144
145 OUString sNewName;
146 _rEvent.Accessor >>= sNewName;
147 try
148 {
149 Reference<XNameContainer> xNameContainer( m_xSettings, UNO_QUERY_THROW );
150 if ( xNameContainer.is() && m_xSettings->hasByName( sElementName ) )
151 {
152 Reference<XRename> xSource(m_xSettings->getByName(sElementName),UNO_QUERY_THROW);
153 xSource->rename(sNewName);
154 }
155 }
156 catch( const Exception& )
157 {
158 DBG_UNHANDLED_EXCEPTION("dbaccess");
159 }
160
161 aFind->second->setName(sNewName);
162}
163
164void SAL_CALL OContainerMediator::disposing( const EventObject& /*Source*/ )
165{
166 ::osl::MutexGuard aGuard(m_aMutex);
167
169}
170
171void OContainerMediator::impl_initSettings_nothrow( const OUString& _rName, const Reference< XPropertySet >& _rxDestination )
172{
173 try
174 {
175 if ( m_xSettings.is() && m_xSettings->hasByName( _rName ) )
176 {
177 Reference< XPropertySet > xSettings( m_xSettings->getByName( _rName ), UNO_QUERY_THROW );
178 ::comphelper::copyProperties( xSettings, _rxDestination );
179 }
180 }
181 catch( const Exception& )
182 {
183 DBG_UNHANDLED_EXCEPTION("dbaccess");
184 }
185}
186
187void OContainerMediator::notifyElementCreated( const OUString& _sName, const Reference< XPropertySet >& _xDest )
188{
189 if ( !m_xSettings.is() )
190 return;
191
192 PropertyForwardList::const_iterator aFind = m_aForwardList.find( _sName );
193 if ( aFind != m_aForwardList.end()
194 && aFind->second->getDefinition().is()
195 )
196 {
197 OSL_FAIL( "OContainerMediator::notifyElementCreated: is this really a valid case?" );
198 return;
199 }
200
201 std::vector< OUString > aPropertyList;
202 try
203 {
204 // initially copy from the settings object (if existent) to the newly created object
205 impl_initSettings_nothrow( _sName, _xDest );
206
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 )
211 {
212 if ( ( property.Attributes & PropertyAttribute::READONLY ) != 0 )
213 continue;
214 if ( ( property.Attributes & PropertyAttribute::BOUND ) == 0 )
215 continue;
216
217 aPropertyList.push_back( property.Name );
218 }
219 }
220 catch( const Exception& )
221 {
222 DBG_UNHANDLED_EXCEPTION("dbaccess");
223 }
224
225 ::rtl::Reference pForward( new OPropertyForward( _xDest, m_xSettings, _sName, aPropertyList ) );
226 m_aForwardList[ _sName ] = pForward;
227}
228
229} // namespace dbaccess
230
231/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
PropertiesInfo aProperties
mutable::osl::Mutex m_aMutex
void impl_initSettings_nothrow(const OUString &_rName, const css::uno::Reference< css::beans::XPropertySet > &_rxDestination)
initializes the properties of the given object from its counterpart in our settings container
virtual void SAL_CALL elementRemoved(const css::container::ContainerEvent &_rEvent) override
virtual ~OContainerMediator() override
void impl_cleanup_nothrow()
cleans up the instance, by deregistering as listener at the containers, and resetting them to <NULL>
css::uno::Reference< css::container::XContainer > m_xContainer
virtual void SAL_CALL disposing(const css::lang::EventObject &Source) override
PropertyForwardList m_aForwardList
virtual void SAL_CALL elementReplaced(const css::container::ContainerEvent &_rEvent) override
virtual void SAL_CALL elementInserted(const css::container::ContainerEvent &_rEvent) override
css::uno::Reference< css::container::XNameAccess > m_xSettings
void notifyElementCreated(const OUString &_sElementName, const css::uno::Reference< css::beans::XPropertySet > &_xElement)
OContainerMediator(const css::uno::Reference< css::container::XContainer > &_xContainer, const css::uno::Reference< css::container::XNameAccess > &_xSettings)
#define TOOLS_WARN_EXCEPTION(area, stream)
#define DBG_UNHANDLED_EXCEPTION(...)
ULONG m_refCount
@ Exception
Reference< XNameAccess > m_xContainer
Definition: objectnames.cxx:80