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 0 : OContainerMediator::OContainerMediator( const Reference< XContainer >& _xContainer, const Reference< XNameAccess >& _xSettings,
43 : const Reference< XConnection >& _rxConnection )
44 : : m_xSettings( _xSettings )
45 : , m_xContainer( _xContainer )
46 0 : , m_aConnection( _rxConnection )
47 : {
48 :
49 0 : if ( _xSettings.is() && _xContainer.is() )
50 : {
51 0 : osl_atomic_increment(&m_refCount);
52 : try
53 : {
54 0 : m_xContainer->addContainerListener(this);
55 0 : Reference< XContainer > xContainer(_xSettings, UNO_QUERY);
56 0 : if ( xContainer.is() )
57 0 : xContainer->addContainerListener(this);
58 : }
59 0 : catch(Exception&)
60 : {
61 : OSL_FAIL("OContainerMediator::OContainerMediator: caught an exception!");
62 : }
63 0 : osl_atomic_decrement( &m_refCount );
64 : }
65 : else
66 : {
67 0 : m_xSettings.clear();
68 0 : m_xContainer.clear();
69 : }
70 0 : }
71 :
72 0 : OContainerMediator::~OContainerMediator()
73 : {
74 0 : acquire();
75 0 : impl_cleanup_nothrow();
76 0 : }
77 :
78 0 : void OContainerMediator::impl_cleanup_nothrow()
79 : {
80 : try
81 : {
82 0 : Reference< XContainer > xContainer( m_xSettings, UNO_QUERY );
83 0 : if ( xContainer.is() )
84 0 : xContainer->removeContainerListener( this );
85 0 : m_xSettings.clear();
86 :
87 0 : xContainer = m_xContainer;
88 0 : if ( xContainer.is() )
89 0 : xContainer->removeContainerListener( this );
90 0 : m_xContainer.clear();
91 :
92 0 : m_aForwardList.clear();
93 : }
94 0 : catch( const Exception& )
95 : {
96 : DBG_UNHANDLED_EXCEPTION();
97 : }
98 0 : }
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 0 : void SAL_CALL OContainerMediator::disposing( const EventObject& /*Source*/ ) throw(RuntimeException, std::exception)
171 : {
172 0 : ::osl::MutexGuard aGuard(m_aMutex);
173 :
174 0 : impl_cleanup_nothrow();
175 0 : }
176 :
177 0 : void OContainerMediator::impl_initSettings_nothrow( const OUString& _rName, const Reference< XPropertySet >& _rxDestination )
178 : {
179 : try
180 : {
181 0 : if ( m_xSettings.is() && m_xSettings->hasByName( _rName ) )
182 : {
183 0 : Reference< XPropertySet > xSettings( m_xSettings->getByName( _rName ), UNO_QUERY_THROW );
184 0 : ::comphelper::copyProperties( xSettings, _rxDestination );
185 : }
186 : }
187 0 : catch( const Exception& )
188 : {
189 : DBG_UNHANDLED_EXCEPTION();
190 : }
191 0 : }
192 :
193 0 : void OContainerMediator::notifyElementCreated( const OUString& _sName, const Reference< XPropertySet >& _xDest )
194 : {
195 0 : if ( !m_xSettings.is() )
196 0 : return;
197 :
198 0 : PropertyForwardList::iterator aFind = m_aForwardList.find( _sName );
199 0 : if ( aFind != m_aForwardList.end()
200 0 : && aFind->second->getDefinition().is()
201 : )
202 : {
203 : OSL_FAIL( "OContainerMediator::notifyElementCreated: is this really a valid case?" );
204 0 : return;
205 : }
206 :
207 0 : ::std::vector< OUString > aPropertyList;
208 : try
209 : {
210 : // initially copy from the settings object (if existent) to the newly created object
211 0 : impl_initSettings_nothrow( _sName, _xDest );
212 :
213 : // collect the to-be-monitored properties
214 0 : Reference< XPropertySetInfo > xPSI( _xDest->getPropertySetInfo(), UNO_QUERY_THROW );
215 0 : Sequence< Property > aProperties( xPSI->getProperties() );
216 0 : const Property* property = aProperties.getConstArray();
217 0 : const Property* propertyEnd = aProperties.getConstArray() + aProperties.getLength();
218 0 : for ( ; property != propertyEnd; ++property )
219 : {
220 0 : if ( ( property->Attributes & PropertyAttribute::READONLY ) != 0 )
221 0 : continue;
222 0 : if ( ( property->Attributes & PropertyAttribute::BOUND ) == 0 )
223 0 : continue;
224 :
225 0 : aPropertyList.push_back( property->Name );
226 0 : }
227 : }
228 0 : catch( const Exception& )
229 : {
230 : DBG_UNHANDLED_EXCEPTION();
231 : }
232 :
233 0 : ::rtl::Reference< OPropertyForward > pForward( new OPropertyForward( _xDest, m_xSettings, _sName, aPropertyList ) );
234 0 : m_aForwardList[ _sName ] = pForward;
235 : }
236 :
237 : } // namespace dbaccess
238 :
239 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|