Line data Source code
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 "dbastrings.hrc"
22 : #include "PropertyForward.hxx"
23 :
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>
32 :
33 :
34 : //........................................................................
35 : namespace dbaccess
36 : {
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;
44 :
45 : DBG_NAME(OContainerMediator)
46 0 : OContainerMediator::OContainerMediator( const Reference< XContainer >& _xContainer, const Reference< XNameAccess >& _xSettings,
47 : const Reference< XConnection >& _rxConnection )
48 : : m_xSettings( _xSettings )
49 : , m_xContainer( _xContainer )
50 0 : , m_aConnection( _rxConnection )
51 : {
52 : DBG_CTOR(OContainerMediator,NULL);
53 :
54 0 : if ( _xSettings.is() && _xContainer.is() )
55 : {
56 0 : osl_atomic_increment(&m_refCount);
57 : try
58 : {
59 0 : m_xContainer->addContainerListener(this);
60 0 : Reference< XContainer > xContainer(_xSettings, UNO_QUERY);
61 0 : if ( xContainer.is() )
62 0 : xContainer->addContainerListener(this);
63 : }
64 0 : catch(Exception&)
65 : {
66 : OSL_FAIL("OContainerMediator::OContainerMediator: caught an exception!");
67 : }
68 0 : osl_atomic_decrement( &m_refCount );
69 : }
70 : else
71 : {
72 0 : m_xSettings.clear();
73 0 : m_xContainer.clear();
74 : }
75 0 : }
76 : // -----------------------------------------------------------------------------
77 0 : OContainerMediator::~OContainerMediator()
78 : {
79 : DBG_DTOR(OContainerMediator,NULL);
80 0 : acquire();
81 0 : impl_cleanup_nothrow();
82 0 : }
83 :
84 : // -----------------------------------------------------------------------------
85 0 : void OContainerMediator::impl_cleanup_nothrow()
86 : {
87 : try
88 : {
89 0 : Reference< XContainer > xContainer( m_xSettings, UNO_QUERY );
90 0 : if ( xContainer.is() )
91 0 : xContainer->removeContainerListener( this );
92 0 : m_xSettings.clear();
93 :
94 0 : xContainer = m_xContainer;
95 0 : if ( xContainer.is() )
96 0 : xContainer->removeContainerListener( this );
97 0 : m_xContainer.clear();
98 :
99 0 : m_aForwardList.clear();
100 : }
101 0 : catch( const Exception& )
102 : {
103 : DBG_UNHANDLED_EXCEPTION();
104 : }
105 0 : }
106 :
107 : // -----------------------------------------------------------------------------
108 0 : void SAL_CALL OContainerMediator::elementInserted( const ContainerEvent& _rEvent ) throw(RuntimeException)
109 : {
110 0 : ::osl::MutexGuard aGuard(m_aMutex);
111 0 : if ( _rEvent.Source == m_xSettings && m_xSettings.is() )
112 : {
113 0 : ::rtl::OUString sElementName;
114 0 : _rEvent.Accessor >>= sElementName;
115 0 : PropertyForwardList::iterator aFind = m_aForwardList.find(sElementName);
116 0 : if ( aFind != m_aForwardList.end() )
117 : {
118 0 : Reference< XPropertySet> xDest(_rEvent.Element,UNO_QUERY);
119 0 : aFind->second->setDefinition( xDest );
120 0 : }
121 0 : }
122 0 : }
123 : // -----------------------------------------------------------------------------
124 0 : void SAL_CALL OContainerMediator::elementRemoved( const ContainerEvent& _rEvent ) throw(RuntimeException)
125 : {
126 0 : ::osl::MutexGuard aGuard(m_aMutex);
127 0 : Reference< XContainer > xContainer = m_xContainer;
128 0 : if ( _rEvent.Source == xContainer && xContainer.is() )
129 : {
130 0 : ::rtl::OUString sElementName;
131 0 : _rEvent.Accessor >>= sElementName;
132 0 : m_aForwardList.erase(sElementName);
133 : try
134 : {
135 0 : Reference<XNameContainer> xNameContainer( m_xSettings, UNO_QUERY );
136 0 : if ( xNameContainer.is() && m_xSettings->hasByName( sElementName ) )
137 0 : xNameContainer->removeByName( sElementName );
138 : }
139 0 : catch( const Exception& )
140 : {
141 : DBG_UNHANDLED_EXCEPTION();
142 0 : }
143 0 : }
144 0 : }
145 : // -----------------------------------------------------------------------------
146 0 : void SAL_CALL OContainerMediator::elementReplaced( const ContainerEvent& _rEvent ) throw(RuntimeException)
147 : {
148 0 : Reference< XContainer > xContainer = m_xContainer;
149 0 : if ( _rEvent.Source == xContainer && xContainer.is() )
150 : {
151 0 : ::rtl::OUString sElementName;
152 0 : _rEvent.ReplacedElement >>= sElementName;
153 :
154 0 : PropertyForwardList::iterator aFind = m_aForwardList.find(sElementName);
155 0 : if ( aFind != m_aForwardList.end() )
156 : {
157 0 : ::rtl::OUString sNewName;
158 0 : _rEvent.Accessor >>= sNewName;
159 : try
160 : {
161 0 : Reference<XNameContainer> xNameContainer( m_xSettings, UNO_QUERY_THROW );
162 0 : if ( xNameContainer.is() && m_xSettings->hasByName( sElementName ) )
163 : {
164 0 : Reference<XRename> xSource(m_xSettings->getByName(sElementName),UNO_QUERY_THROW);
165 0 : xSource->rename(sNewName);
166 0 : }
167 : }
168 0 : catch( const Exception& )
169 : {
170 : DBG_UNHANDLED_EXCEPTION();
171 : }
172 :
173 0 : aFind->second->setName(sNewName);
174 0 : }
175 0 : }
176 0 : }
177 :
178 : // -----------------------------------------------------------------------------
179 0 : void SAL_CALL OContainerMediator::disposing( const EventObject& /*Source*/ ) throw(RuntimeException)
180 : {
181 0 : ::osl::MutexGuard aGuard(m_aMutex);
182 :
183 0 : impl_cleanup_nothrow();
184 0 : }
185 :
186 : // -----------------------------------------------------------------------------
187 0 : void OContainerMediator::impl_initSettings_nothrow( const ::rtl::OUString& _rName, const Reference< XPropertySet >& _rxDestination )
188 : {
189 : try
190 : {
191 0 : if ( m_xSettings.is() && m_xSettings->hasByName( _rName ) )
192 : {
193 0 : Reference< XPropertySet > xSettings( m_xSettings->getByName( _rName ), UNO_QUERY_THROW );
194 0 : ::comphelper::copyProperties( xSettings, _rxDestination );
195 : }
196 : }
197 0 : catch( const Exception& )
198 : {
199 : DBG_UNHANDLED_EXCEPTION();
200 : }
201 0 : }
202 :
203 : // -----------------------------------------------------------------------------
204 0 : void OContainerMediator::notifyElementCreated( const ::rtl::OUString& _sName, const Reference< XPropertySet >& _xDest )
205 : {
206 0 : if ( !m_xSettings.is() )
207 : return;
208 :
209 0 : PropertyForwardList::iterator aFind = m_aForwardList.find( _sName );
210 0 : if ( aFind != m_aForwardList.end()
211 0 : && aFind->second->getDefinition().is()
212 : )
213 : {
214 : OSL_FAIL( "OContainerMediator::notifyElementCreated: is this really a valid case?" );
215 : return;
216 : }
217 :
218 0 : ::std::vector< ::rtl::OUString > aPropertyList;
219 : try
220 : {
221 : // initially copy from the settings object (if existent) to the newly created object
222 0 : impl_initSettings_nothrow( _sName, _xDest );
223 :
224 : // collect the to-be-monitored properties
225 0 : Reference< XPropertySetInfo > xPSI( _xDest->getPropertySetInfo(), UNO_QUERY_THROW );
226 0 : Sequence< Property > aProperties( xPSI->getProperties() );
227 0 : const Property* property = aProperties.getConstArray();
228 0 : const Property* propertyEnd = aProperties.getConstArray() + aProperties.getLength();
229 0 : for ( ; property != propertyEnd; ++property )
230 : {
231 0 : if ( ( property->Attributes & PropertyAttribute::READONLY ) != 0 )
232 0 : continue;
233 0 : if ( ( property->Attributes & PropertyAttribute::BOUND ) == 0 )
234 0 : continue;
235 :
236 0 : aPropertyList.push_back( property->Name );
237 0 : }
238 : }
239 0 : catch( const Exception& )
240 : {
241 : DBG_UNHANDLED_EXCEPTION();
242 : }
243 :
244 0 : ::rtl::Reference< OPropertyForward > pForward( new OPropertyForward( _xDest, m_xSettings, _sName, aPropertyList ) );
245 0 : m_aForwardList[ _sName ] = pForward;
246 : }
247 : // -----------------------------------------------------------------------------
248 : //........................................................................
249 : } // namespace dbaccess
250 : //........................................................................
251 :
252 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|