build fix: no comphelper/profilezone.hxx in this branch
[LibreOffice.git] / dbaccess / source / core / misc / ContainerMediator.cxx
blob802b802fc57544200fc1e750a288a3147aba5b09
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 "dbastrings.hrc"
22 #include "PropertyForward.hxx"
24 #include <com/sun/star/beans/PropertyAttribute.hpp>
25 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
26 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
27 #include <com/sun/star/sdbcx/XRename.hpp>
28 #include <connectivity/dbtools.hxx>
29 #include <comphelper/property.hxx>
30 #include <tools/debug.hxx>
31 #include <tools/diagnose_ex.h>
33 namespace dbaccess
35 using namespace ::com::sun::star::uno;
36 using namespace ::com::sun::star::lang;
37 using namespace ::com::sun::star::sdbc;
38 using namespace ::com::sun::star::sdbcx;
39 using namespace ::com::sun::star::beans;
40 using namespace ::com::sun::star::container;
42 OContainerMediator::OContainerMediator( const Reference< XContainer >& _xContainer, const Reference< XNameAccess >& _xSettings )
43 : m_xSettings( _xSettings )
44 , m_xContainer( _xContainer )
47 if ( _xSettings.is() && _xContainer.is() )
49 osl_atomic_increment(&m_refCount);
50 try
52 m_xContainer->addContainerListener(this);
53 Reference< XContainer > xContainer(_xSettings, UNO_QUERY);
54 if ( xContainer.is() )
55 xContainer->addContainerListener(this);
57 catch(Exception&)
59 OSL_FAIL("OContainerMediator::OContainerMediator: caught an exception!");
61 osl_atomic_decrement( &m_refCount );
63 else
65 m_xSettings.clear();
66 m_xContainer.clear();
70 OContainerMediator::~OContainerMediator()
72 acquire();
73 impl_cleanup_nothrow();
76 void OContainerMediator::impl_cleanup_nothrow()
78 try
80 Reference< XContainer > xContainer( m_xSettings, UNO_QUERY );
81 if ( xContainer.is() )
82 xContainer->removeContainerListener( this );
83 m_xSettings.clear();
85 xContainer = m_xContainer;
86 if ( xContainer.is() )
87 xContainer->removeContainerListener( this );
88 m_xContainer.clear();
90 m_aForwardList.clear();
92 catch( const Exception& )
94 DBG_UNHANDLED_EXCEPTION();
98 void SAL_CALL OContainerMediator::elementInserted( const ContainerEvent& _rEvent ) throw(RuntimeException, std::exception)
100 ::osl::MutexGuard aGuard(m_aMutex);
101 if ( _rEvent.Source == m_xSettings && m_xSettings.is() )
103 OUString sElementName;
104 _rEvent.Accessor >>= sElementName;
105 PropertyForwardList::const_iterator aFind = m_aForwardList.find(sElementName);
106 if ( aFind != m_aForwardList.end() )
108 Reference< XPropertySet> xDest(_rEvent.Element,UNO_QUERY);
109 aFind->second->setDefinition( xDest );
114 void SAL_CALL OContainerMediator::elementRemoved( const ContainerEvent& _rEvent ) throw(RuntimeException, std::exception)
116 ::osl::MutexGuard aGuard(m_aMutex);
117 Reference< XContainer > xContainer = m_xContainer;
118 if ( _rEvent.Source == xContainer && xContainer.is() )
120 OUString sElementName;
121 _rEvent.Accessor >>= sElementName;
122 m_aForwardList.erase(sElementName);
125 Reference<XNameContainer> xNameContainer( m_xSettings, UNO_QUERY );
126 if ( xNameContainer.is() && m_xSettings->hasByName( sElementName ) )
127 xNameContainer->removeByName( sElementName );
129 catch( const Exception& )
131 DBG_UNHANDLED_EXCEPTION();
136 void SAL_CALL OContainerMediator::elementReplaced( const ContainerEvent& _rEvent ) throw(RuntimeException, std::exception)
138 Reference< XContainer > xContainer = m_xContainer;
139 if ( _rEvent.Source == xContainer && xContainer.is() )
141 OUString sElementName;
142 _rEvent.ReplacedElement >>= sElementName;
144 PropertyForwardList::const_iterator aFind = m_aForwardList.find(sElementName);
145 if ( aFind != m_aForwardList.end() )
147 OUString sNewName;
148 _rEvent.Accessor >>= sNewName;
151 Reference<XNameContainer> xNameContainer( m_xSettings, UNO_QUERY_THROW );
152 if ( xNameContainer.is() && m_xSettings->hasByName( sElementName ) )
154 Reference<XRename> xSource(m_xSettings->getByName(sElementName),UNO_QUERY_THROW);
155 xSource->rename(sNewName);
158 catch( const Exception& )
160 DBG_UNHANDLED_EXCEPTION();
163 aFind->second->setName(sNewName);
168 void SAL_CALL OContainerMediator::disposing( const EventObject& /*Source*/ ) throw(RuntimeException, std::exception)
170 ::osl::MutexGuard aGuard(m_aMutex);
172 impl_cleanup_nothrow();
175 void OContainerMediator::impl_initSettings_nothrow( const OUString& _rName, const Reference< XPropertySet >& _rxDestination )
179 if ( m_xSettings.is() && m_xSettings->hasByName( _rName ) )
181 Reference< XPropertySet > xSettings( m_xSettings->getByName( _rName ), UNO_QUERY_THROW );
182 ::comphelper::copyProperties( xSettings, _rxDestination );
185 catch( const Exception& )
187 DBG_UNHANDLED_EXCEPTION();
191 void OContainerMediator::notifyElementCreated( const OUString& _sName, const Reference< XPropertySet >& _xDest )
193 if ( !m_xSettings.is() )
194 return;
196 PropertyForwardList::const_iterator aFind = m_aForwardList.find( _sName );
197 if ( aFind != m_aForwardList.end()
198 && aFind->second->getDefinition().is()
201 OSL_FAIL( "OContainerMediator::notifyElementCreated: is this really a valid case?" );
202 return;
205 ::std::vector< OUString > aPropertyList;
208 // initially copy from the settings object (if existent) to the newly created object
209 impl_initSettings_nothrow( _sName, _xDest );
211 // collect the to-be-monitored properties
212 Reference< XPropertySetInfo > xPSI( _xDest->getPropertySetInfo(), UNO_QUERY_THROW );
213 Sequence< Property > aProperties( xPSI->getProperties() );
214 const Property* property = aProperties.getConstArray();
215 const Property* propertyEnd = aProperties.getConstArray() + aProperties.getLength();
216 for ( ; property != propertyEnd; ++property )
218 if ( ( property->Attributes & PropertyAttribute::READONLY ) != 0 )
219 continue;
220 if ( ( property->Attributes & PropertyAttribute::BOUND ) == 0 )
221 continue;
223 aPropertyList.push_back( property->Name );
226 catch( const Exception& )
228 DBG_UNHANDLED_EXCEPTION();
231 ::rtl::Reference< OPropertyForward > pForward( new OPropertyForward( _xDest, m_xSettings, _sName, aPropertyList ) );
232 m_aForwardList[ _sName ] = pForward;
235 } // namespace dbaccess
237 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */