Version 4.0.0.1, tag libreoffice-4.0.0.1
[LibreOffice.git] / dbaccess / source / core / misc / ContainerMediator.cxx
blobde4dfa5bc710714bd5c5057a20828dc70827b2d2
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>
34 //........................................................................
35 namespace dbaccess
37 //........................................................................
38 using namespace ::com::sun::star::uno;
39 using namespace ::com::sun::star::lang;
40 using namespace ::com::sun::star::sdbc;
41 using namespace ::com::sun::star::sdbcx;
42 using namespace ::com::sun::star::beans;
43 using namespace ::com::sun::star::container;
45 DBG_NAME(OContainerMediator)
46 OContainerMediator::OContainerMediator( const Reference< XContainer >& _xContainer, const Reference< XNameAccess >& _xSettings,
47 const Reference< XConnection >& _rxConnection )
48 : m_xSettings( _xSettings )
49 , m_xContainer( _xContainer )
50 , m_aConnection( _rxConnection )
52 DBG_CTOR(OContainerMediator,NULL);
54 if ( _xSettings.is() && _xContainer.is() )
56 osl_atomic_increment(&m_refCount);
57 try
59 m_xContainer->addContainerListener(this);
60 Reference< XContainer > xContainer(_xSettings, UNO_QUERY);
61 if ( xContainer.is() )
62 xContainer->addContainerListener(this);
64 catch(Exception&)
66 OSL_FAIL("OContainerMediator::OContainerMediator: caught an exception!");
68 osl_atomic_decrement( &m_refCount );
70 else
72 m_xSettings.clear();
73 m_xContainer.clear();
76 // -----------------------------------------------------------------------------
77 OContainerMediator::~OContainerMediator()
79 DBG_DTOR(OContainerMediator,NULL);
80 acquire();
81 impl_cleanup_nothrow();
84 // -----------------------------------------------------------------------------
85 void OContainerMediator::impl_cleanup_nothrow()
87 try
89 Reference< XContainer > xContainer( m_xSettings, UNO_QUERY );
90 if ( xContainer.is() )
91 xContainer->removeContainerListener( this );
92 m_xSettings.clear();
94 xContainer = m_xContainer;
95 if ( xContainer.is() )
96 xContainer->removeContainerListener( this );
97 m_xContainer.clear();
99 m_aForwardList.clear();
101 catch( const Exception& )
103 DBG_UNHANDLED_EXCEPTION();
107 // -----------------------------------------------------------------------------
108 void SAL_CALL OContainerMediator::elementInserted( const ContainerEvent& _rEvent ) throw(RuntimeException)
110 ::osl::MutexGuard aGuard(m_aMutex);
111 if ( _rEvent.Source == m_xSettings && m_xSettings.is() )
113 ::rtl::OUString sElementName;
114 _rEvent.Accessor >>= sElementName;
115 PropertyForwardList::iterator aFind = m_aForwardList.find(sElementName);
116 if ( aFind != m_aForwardList.end() )
118 Reference< XPropertySet> xDest(_rEvent.Element,UNO_QUERY);
119 aFind->second->setDefinition( xDest );
123 // -----------------------------------------------------------------------------
124 void SAL_CALL OContainerMediator::elementRemoved( const ContainerEvent& _rEvent ) throw(RuntimeException)
126 ::osl::MutexGuard aGuard(m_aMutex);
127 Reference< XContainer > xContainer = m_xContainer;
128 if ( _rEvent.Source == xContainer && xContainer.is() )
130 ::rtl::OUString sElementName;
131 _rEvent.Accessor >>= sElementName;
132 m_aForwardList.erase(sElementName);
135 Reference<XNameContainer> xNameContainer( m_xSettings, UNO_QUERY );
136 if ( xNameContainer.is() && m_xSettings->hasByName( sElementName ) )
137 xNameContainer->removeByName( sElementName );
139 catch( const Exception& )
141 DBG_UNHANDLED_EXCEPTION();
145 // -----------------------------------------------------------------------------
146 void SAL_CALL OContainerMediator::elementReplaced( const ContainerEvent& _rEvent ) throw(RuntimeException)
148 Reference< XContainer > xContainer = m_xContainer;
149 if ( _rEvent.Source == xContainer && xContainer.is() )
151 ::rtl::OUString sElementName;
152 _rEvent.ReplacedElement >>= sElementName;
154 PropertyForwardList::iterator aFind = m_aForwardList.find(sElementName);
155 if ( aFind != m_aForwardList.end() )
157 ::rtl::OUString sNewName;
158 _rEvent.Accessor >>= sNewName;
161 Reference<XNameContainer> xNameContainer( m_xSettings, UNO_QUERY_THROW );
162 if ( xNameContainer.is() && m_xSettings->hasByName( sElementName ) )
164 Reference<XRename> xSource(m_xSettings->getByName(sElementName),UNO_QUERY_THROW);
165 xSource->rename(sNewName);
168 catch( const Exception& )
170 DBG_UNHANDLED_EXCEPTION();
173 aFind->second->setName(sNewName);
178 // -----------------------------------------------------------------------------
179 void SAL_CALL OContainerMediator::disposing( const EventObject& /*Source*/ ) throw(RuntimeException)
181 ::osl::MutexGuard aGuard(m_aMutex);
183 impl_cleanup_nothrow();
186 // -----------------------------------------------------------------------------
187 void OContainerMediator::impl_initSettings_nothrow( const ::rtl::OUString& _rName, const Reference< XPropertySet >& _rxDestination )
191 if ( m_xSettings.is() && m_xSettings->hasByName( _rName ) )
193 Reference< XPropertySet > xSettings( m_xSettings->getByName( _rName ), UNO_QUERY_THROW );
194 ::comphelper::copyProperties( xSettings, _rxDestination );
197 catch( const Exception& )
199 DBG_UNHANDLED_EXCEPTION();
203 // -----------------------------------------------------------------------------
204 void OContainerMediator::notifyElementCreated( const ::rtl::OUString& _sName, const Reference< XPropertySet >& _xDest )
206 if ( !m_xSettings.is() )
207 return;
209 PropertyForwardList::iterator aFind = m_aForwardList.find( _sName );
210 if ( aFind != m_aForwardList.end()
211 && aFind->second->getDefinition().is()
214 OSL_FAIL( "OContainerMediator::notifyElementCreated: is this really a valid case?" );
215 return;
218 ::std::vector< ::rtl::OUString > aPropertyList;
221 // initially copy from the settings object (if existent) to the newly created object
222 impl_initSettings_nothrow( _sName, _xDest );
224 // collect the to-be-monitored properties
225 Reference< XPropertySetInfo > xPSI( _xDest->getPropertySetInfo(), UNO_QUERY_THROW );
226 Sequence< Property > aProperties( xPSI->getProperties() );
227 const Property* property = aProperties.getConstArray();
228 const Property* propertyEnd = aProperties.getConstArray() + aProperties.getLength();
229 for ( ; property != propertyEnd; ++property )
231 if ( ( property->Attributes & PropertyAttribute::READONLY ) != 0 )
232 continue;
233 if ( ( property->Attributes & PropertyAttribute::BOUND ) == 0 )
234 continue;
236 aPropertyList.push_back( property->Name );
239 catch( const Exception& )
241 DBG_UNHANDLED_EXCEPTION();
244 ::rtl::Reference< OPropertyForward > pForward( new OPropertyForward( _xDest, m_xSettings, _sName, aPropertyList ) );
245 m_aForwardList[ _sName ] = pForward;
247 // -----------------------------------------------------------------------------
248 //........................................................................
249 } // namespace dbaccess
250 //........................................................................
252 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */