LCOV - code coverage report
Current view: top level - dbaccess/source/core/misc - ContainerMediator.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 63 107 58.9 %
Date: 2015-06-13 12:38:46 Functions: 8 10 80.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.11