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