LCOV - code coverage report
Current view: top level - libreoffice/dbaccess/source/core/dataaccess - definitioncontainer.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 298 0.0 %
Date: 2012-12-27 Functions: 0 45 0.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             : 
      21             : #include "definitioncontainer.hxx"
      22             : #include "dbastrings.hrc"
      23             : #include "apitools.hxx"
      24             : #include "core_resource.hxx"
      25             : #include "core_resource.hrc"
      26             : 
      27             : #include <tools/debug.hxx>
      28             : #include <tools/diagnose_ex.h>
      29             : #include <osl/diagnose.h>
      30             : #include <comphelper/sequence.hxx>
      31             : #include <comphelper/enumhelper.hxx>
      32             : #include <comphelper/extract.hxx>
      33             : #include <com/sun/star/lang/XComponent.hpp>
      34             : #include <com/sun/star/ucb/CommandInfo.hpp>
      35             : #include <com/sun/star/beans/XPropertySet.hpp>
      36             : #include <com/sun/star/sdb/ErrorCondition.hpp>
      37             : #include <comphelper/types.hxx>
      38             : #include <ucbhelper/contentidentifier.hxx>
      39             : #include <o3tl/compat_functional.hxx>
      40             : 
      41             : 
      42             : using namespace ::com::sun::star::uno;
      43             : using namespace ::com::sun::star::lang;
      44             : using namespace ::com::sun::star::util;
      45             : using namespace ::com::sun::star::beans;
      46             : using namespace ::com::sun::star::container;
      47             : using namespace ::com::sun::star::sdbcx;
      48             : using namespace ::com::sun::star::sdb;
      49             : using namespace ::osl;
      50             : using namespace ::comphelper;
      51             : using namespace ::cppu;
      52             : using namespace ::com::sun::star::ucb;
      53             : 
      54             : namespace dbaccess
      55             : {
      56             : 
      57             : //==========================================================================
      58             : //= ODefinitionContainer_Impl
      59             : //==========================================================================
      60           0 : void ODefinitionContainer_Impl::erase( TContentPtr _pDefinition )
      61             : {
      62           0 :     NamedDefinitions::iterator aPos = find( _pDefinition );
      63           0 :     if ( aPos != end() )
      64           0 :         m_aDefinitions.erase( aPos );
      65           0 : }
      66             : 
      67           0 : ODefinitionContainer_Impl::const_iterator ODefinitionContainer_Impl::find( TContentPtr _pDefinition ) const
      68             : {
      69             :     return ::std::find_if(
      70             :         m_aDefinitions.begin(),
      71             :         m_aDefinitions.end(),
      72             :         ::o3tl::compose1(
      73             :             ::std::bind2nd( ::std::equal_to< TContentPtr >(), _pDefinition ),
      74             :             ::o3tl::select2nd< NamedDefinitions::value_type >()
      75             :         )
      76           0 :     );
      77             : }
      78             : 
      79           0 : ODefinitionContainer_Impl::iterator ODefinitionContainer_Impl::find( TContentPtr _pDefinition )
      80             : {
      81             :     return ::std::find_if(
      82             :         m_aDefinitions.begin(),
      83             :         m_aDefinitions.end(),
      84             :         ::o3tl::compose1(
      85             :             ::std::bind2nd( ::std::equal_to< TContentPtr >(), _pDefinition ),
      86             :             ::o3tl::select2nd< NamedDefinitions::value_type >()
      87             :         )
      88           0 :     );
      89             : }
      90             : 
      91             : //==========================================================================
      92             : //= ODefinitionContainer
      93             : //==========================================================================
      94             : DBG_NAME(ODefinitionContainer)
      95             : 
      96           0 : ODefinitionContainer::ODefinitionContainer(   const Reference< XMultiServiceFactory >& _xORB
      97             :                                             , const Reference< XInterface >&    _xParentContainer
      98             :                                             , const TContentPtr& _pImpl
      99             :                                             , bool _bCheckSlash
     100             :                                             )
     101             :     :OContentHelper(_xORB,_xParentContainer,_pImpl)
     102             :     ,m_aApproveListeners(m_aMutex)
     103             :     ,m_aContainerListeners(m_aMutex)
     104             :     ,m_bInPropertyChange(sal_False)
     105           0 :     ,m_bCheckSlash(_bCheckSlash)
     106             : {
     107           0 :     m_pImpl->m_aProps.bIsDocument = sal_False;
     108           0 :     m_pImpl->m_aProps.bIsFolder = sal_True;
     109             : 
     110           0 :     const ODefinitionContainer_Impl& rDefinitions( getDefinitions() );
     111           0 :     ODefinitionContainer_Impl::const_iterator aEnd = rDefinitions.end();
     112           0 :     for (   ODefinitionContainer_Impl::const_iterator aDefinition = rDefinitions.begin();
     113             :             aDefinition != aEnd;
     114             :             ++aDefinition
     115             :         )
     116             :         m_aDocuments.push_back(
     117             :             m_aDocumentMap.insert(
     118           0 :                 Documents::value_type( aDefinition->first, Documents::mapped_type() ) ).first );
     119             : 
     120             :     DBG_CTOR(ODefinitionContainer, NULL);
     121           0 : }
     122             : 
     123           0 : void SAL_CALL ODefinitionContainer::disposing()
     124             : {
     125           0 :     OContentHelper::disposing();
     126             : 
     127           0 :     MutexGuard aGuard(m_aMutex);
     128             : 
     129             :     // say goodbye to our listeners
     130           0 :     EventObject aEvt(*this);
     131           0 :     m_aApproveListeners.disposeAndClear(aEvt);
     132           0 :     m_aContainerListeners.disposeAndClear(aEvt);
     133             : 
     134             :     // dispose our elements
     135           0 :     Documents::iterator aIter = m_aDocumentMap.begin();
     136           0 :     Documents::iterator aEnd = m_aDocumentMap.end();
     137             : 
     138           0 :     for (; aIter != aEnd; ++aIter)
     139             :     {
     140           0 :         Reference<XContent> xProp = aIter->second;
     141           0 :         if ( xProp.is() )
     142             :         {
     143           0 :             removeObjectListener(xProp);
     144           0 :             ::comphelper::disposeComponent(xProp);
     145             :         }
     146           0 :     }
     147             : 
     148             :     // remove our elements
     149           0 :     m_aDocuments.clear();
     150             :     //  !!! do this before clearing the map which the vector elements refer to !!!
     151           0 :     m_aDocumentMap.clear();
     152           0 : }
     153             : 
     154           0 : ODefinitionContainer::~ODefinitionContainer()
     155             : {
     156             :     DBG_DTOR(ODefinitionContainer, NULL);
     157           0 : }
     158             : 
     159           0 : IMPLEMENT_FORWARD_XINTERFACE2( ODefinitionContainer,OContentHelper,ODefinitionContainer_Base)
     160           0 : IMPLEMENT_TYPEPROVIDER2(ODefinitionContainer,OContentHelper,ODefinitionContainer_Base);
     161             : // XServiceInfo
     162           0 : ::rtl::OUString SAL_CALL ODefinitionContainer::getImplementationName(  ) throw(RuntimeException)
     163             : {
     164           0 :     return ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.sdb.ODefinitionContainer"));
     165             : }
     166             : 
     167           0 : Sequence< ::rtl::OUString > SAL_CALL ODefinitionContainer::getSupportedServiceNames(  ) throw(RuntimeException)
     168             : {
     169           0 :     Sequence< ::rtl::OUString > aReturn(2);
     170           0 :     aReturn.getArray()[0] = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.sdb.DefinitionContainer"));
     171           0 :     aReturn.getArray()[1] = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.ucb.Content"));
     172           0 :     return aReturn;
     173             : }
     174             : 
     175             : // XNameContainer
     176           0 : void SAL_CALL ODefinitionContainer::insertByName( const ::rtl::OUString& _rName, const Any& aElement ) throw(IllegalArgumentException, ElementExistException, WrappedTargetException, RuntimeException)
     177             : {
     178           0 :     ResettableMutexGuard aGuard(m_aMutex);
     179             : 
     180             :     // approve the new object
     181           0 :     Reference< XContent > xNewElement(aElement,UNO_QUERY);
     182           0 :     approveNewObject( _rName, xNewElement );  // will throw if necessary
     183             : 
     184           0 :     notifyByName( aGuard, _rName, xNewElement, NULL, E_INSERTED, ApproveListeners );
     185           0 :     implAppend( _rName, xNewElement );
     186           0 :     notifyByName( aGuard, _rName, xNewElement, NULL, E_INSERTED, ContainerListemers );
     187           0 : }
     188             : 
     189           0 : void SAL_CALL ODefinitionContainer::removeByName( const ::rtl::OUString& _rName ) throw(NoSuchElementException, WrappedTargetException, RuntimeException)
     190             : {
     191           0 :     ResettableMutexGuard aGuard(m_aMutex);
     192             : 
     193             :     // check the arguments
     194           0 :     if (_rName.isEmpty())
     195           0 :         throw IllegalArgumentException();
     196             : 
     197           0 :     if (!checkExistence(_rName))
     198           0 :         throw NoSuchElementException(_rName,*this);
     199             : 
     200             :     // the old element (for the notifications)
     201           0 :     Reference< XContent > xOldElement = implGetByName( _rName, impl_haveAnyListeners_nothrow() );
     202             : 
     203             :     // do the removal
     204           0 :     notifyByName( aGuard, _rName, NULL, xOldElement, E_REMOVED, ApproveListeners );
     205           0 :     implRemove( _rName );
     206           0 :     notifyByName( aGuard, _rName, NULL, xOldElement, E_REMOVED, ContainerListemers );
     207             : 
     208           0 :     removeObjectListener( xOldElement );
     209           0 :     disposeComponent(xOldElement);
     210           0 : }
     211             : 
     212             : // XNameReplace
     213           0 : void SAL_CALL ODefinitionContainer::replaceByName( const ::rtl::OUString& _rName, const Any& aElement ) throw(IllegalArgumentException, NoSuchElementException, WrappedTargetException, RuntimeException)
     214             : {
     215           0 :     ResettableMutexGuard aGuard(m_aMutex);
     216             : 
     217             :     // let derived classes approve the new object
     218           0 :     Reference< XContent > xNewElement(aElement,UNO_QUERY);
     219           0 :     approveNewObject( _rName, xNewElement );    // will throw if necessary
     220             : 
     221             :     // the old element (for the notifications)
     222           0 :     Reference< XContent > xOldElement = implGetByName( _rName, impl_haveAnyListeners_nothrow() );
     223             : 
     224           0 :     notifyByName( aGuard, _rName, xNewElement, xOldElement, E_REPLACED, ApproveListeners );
     225           0 :     implReplace( _rName, xNewElement );
     226           0 :     notifyByName( aGuard, _rName, xNewElement, xOldElement, E_REPLACED, ContainerListemers );
     227             : 
     228             :     // and dispose it
     229           0 :     disposeComponent(xOldElement);
     230           0 : }
     231             : 
     232             : namespace
     233             : {
     234             :     typedef Reference< XVeto > ( SAL_CALL XContainerApproveListener::*ContainerApprovalMethod )( const ContainerEvent& );
     235             : 
     236             :     struct RaiseExceptionFromVeto
     237             :     {
     238             :     private:
     239             :         ContainerApprovalMethod m_pMethod;
     240             :         const ContainerEvent&   m_rEvent;
     241             : 
     242             :     public:
     243           0 :         RaiseExceptionFromVeto( ContainerApprovalMethod _pMethod, const ContainerEvent& _rEvent )
     244             :             :m_pMethod( _pMethod )
     245           0 :             ,m_rEvent( _rEvent )
     246             :         {
     247           0 :         }
     248             : 
     249           0 :         void operator()( const Reference< XContainerApproveListener >& _Listener ) const
     250             :         {
     251           0 :             Reference< XVeto > xVeto = (_Listener.get()->*m_pMethod)( m_rEvent );
     252           0 :             if ( !xVeto.is() )
     253           0 :                 return;
     254             : 
     255           0 :             Any eVetoDetails = xVeto->getDetails();
     256             : 
     257           0 :             IllegalArgumentException aIllegalArgumentError;
     258           0 :             if ( eVetoDetails >>= aIllegalArgumentError )
     259           0 :                 throw aIllegalArgumentError;
     260             : 
     261           0 :             WrappedTargetException aWrappedError;
     262           0 :             if ( eVetoDetails >>= aWrappedError )
     263           0 :                 throw aWrappedError;
     264             : 
     265           0 :             throw WrappedTargetException( xVeto->getReason(), _Listener.get(), eVetoDetails );
     266             :         }
     267             :     };
     268             : }
     269             : 
     270           0 : void ODefinitionContainer::notifyByName( ResettableMutexGuard& _rGuard, const ::rtl::OUString& _rName,
     271             :         const Reference< XContent >& _xNewElement, const Reference< XContent >& _xOldElement,
     272             :         ContainerOperation _eOperation, ListenerType _eType )
     273             : {
     274           0 :     bool bApprove = ( _eType == ApproveListeners );
     275             : 
     276           0 :     ::cppu::OInterfaceContainerHelper& rContainer( bApprove ? m_aApproveListeners : m_aContainerListeners );
     277           0 :     if ( !rContainer.getLength() )
     278           0 :         return;
     279             : 
     280           0 :     ContainerEvent aEvent( *this, makeAny( _rName ), makeAny( _xNewElement ), makeAny( _xOldElement ) );
     281             : 
     282           0 :     _rGuard.clear();
     283           0 :     switch ( _eOperation )
     284             :     {
     285             :         case E_INSERTED:
     286           0 :             if ( bApprove )
     287             :                 rContainer.forEach< XContainerApproveListener, RaiseExceptionFromVeto >(
     288           0 :                     RaiseExceptionFromVeto( &XContainerApproveListener::approveInsertElement, aEvent ) );
     289             :             else
     290           0 :                 rContainer.notifyEach( &XContainerListener::elementInserted, aEvent );
     291           0 :             break;
     292             :         case E_REPLACED:
     293           0 :             if ( bApprove )
     294             :                 rContainer.forEach< XContainerApproveListener, RaiseExceptionFromVeto >(
     295           0 :                     RaiseExceptionFromVeto( &XContainerApproveListener::approveReplaceElement, aEvent ) );
     296             :             else
     297           0 :                 rContainer.notifyEach( &XContainerListener::elementReplaced, aEvent );
     298           0 :             break;
     299             :         case E_REMOVED:
     300           0 :             if ( bApprove )
     301             :                 rContainer.forEach< XContainerApproveListener, RaiseExceptionFromVeto >(
     302           0 :                     RaiseExceptionFromVeto( &XContainerApproveListener::approveRemoveElement, aEvent ) );
     303             :             else
     304           0 :                 rContainer.notifyEach( &XContainerListener::elementRemoved, aEvent );
     305           0 :             break;
     306             :     }
     307             : 
     308           0 :     if ( bApprove )
     309           0 :         _rGuard.reset();
     310             : }
     311             : 
     312           0 : void SAL_CALL ODefinitionContainer::addContainerListener( const Reference< XContainerListener >& _rxListener ) throw(RuntimeException)
     313             : {
     314           0 :     if (_rxListener.is())
     315           0 :         m_aContainerListeners.addInterface(_rxListener);
     316           0 : }
     317             : 
     318           0 : void SAL_CALL ODefinitionContainer::removeContainerListener( const Reference< XContainerListener >& _rxListener ) throw(RuntimeException)
     319             : {
     320           0 :     if (_rxListener.is())
     321           0 :         m_aContainerListeners.removeInterface(_rxListener);
     322           0 : }
     323             : 
     324           0 : void SAL_CALL ODefinitionContainer::addContainerApproveListener( const Reference< XContainerApproveListener >& _Listener ) throw (RuntimeException)
     325             : {
     326           0 :     if ( _Listener.is() )
     327           0 :         m_aApproveListeners.addInterface( _Listener );
     328           0 : }
     329             : 
     330           0 : void SAL_CALL ODefinitionContainer::removeContainerApproveListener( const Reference< XContainerApproveListener >& _Listener ) throw (RuntimeException)
     331             : {
     332           0 :     if ( _Listener.is() )
     333           0 :         m_aApproveListeners.removeInterface( _Listener );
     334           0 : }
     335             : 
     336             : // XElementAccess
     337           0 : Type SAL_CALL ODefinitionContainer::getElementType( ) throw (RuntimeException)
     338             : {
     339           0 :     return ::getCppuType( static_cast< Reference< XContent >* >(NULL) );
     340             : }
     341             : 
     342           0 : sal_Bool SAL_CALL ODefinitionContainer::hasElements( ) throw (RuntimeException)
     343             : {
     344           0 :     MutexGuard aGuard(m_aMutex);
     345           0 :     return !m_aDocuments.empty();
     346             : }
     347             : 
     348             : // XEnumerationAccess
     349           0 : Reference< XEnumeration > SAL_CALL ODefinitionContainer::createEnumeration(  ) throw(RuntimeException)
     350             : {
     351           0 :     MutexGuard aGuard(m_aMutex);
     352           0 :     return new ::comphelper::OEnumerationByIndex(static_cast<XIndexAccess*>(this));
     353             : }
     354             : 
     355             : // XIndexAccess
     356           0 : sal_Int32 SAL_CALL ODefinitionContainer::getCount(  ) throw(RuntimeException)
     357             : {
     358           0 :     MutexGuard aGuard(m_aMutex);
     359           0 :     return m_aDocuments.size();
     360             : }
     361             : 
     362           0 : Any SAL_CALL ODefinitionContainer::getByIndex( sal_Int32 _nIndex ) throw(IndexOutOfBoundsException, WrappedTargetException, RuntimeException)
     363             : {
     364           0 :     MutexGuard aGuard(m_aMutex);
     365             : 
     366           0 :     if ((_nIndex < 0) || (_nIndex >= (sal_Int32)m_aDocuments.size()))
     367           0 :         throw IndexOutOfBoundsException();
     368             : 
     369           0 :     Documents::iterator aPos = m_aDocuments[_nIndex];
     370           0 :     Reference<XContent> xProp = aPos->second;
     371           0 :     if (!xProp.is())
     372             :     {   // that's the first access to the object
     373             :         // -> create it
     374           0 :         xProp = createObject(aPos->first);
     375           0 :         aPos->second = Documents::mapped_type();
     376             :         // and update the name-access map
     377             :     }
     378             : 
     379           0 :     return makeAny(xProp);
     380             : }
     381             : 
     382           0 : Any SAL_CALL ODefinitionContainer::getByName( const ::rtl::OUString& _rName ) throw(NoSuchElementException, WrappedTargetException, RuntimeException)
     383             : {
     384           0 :     MutexGuard aGuard(m_aMutex);
     385             : 
     386           0 :     return makeAny( implGetByName( _rName, sal_True ) );
     387             : }
     388             : 
     389           0 : Reference< XContent > ODefinitionContainer::implGetByName(const ::rtl::OUString& _rName, sal_Bool _bReadIfNeccessary) throw (NoSuchElementException)
     390             : {
     391           0 :     Documents::iterator aMapPos = m_aDocumentMap.find(_rName);
     392           0 :     if (aMapPos == m_aDocumentMap.end())
     393           0 :         throw NoSuchElementException(_rName,*this);
     394             : 
     395           0 :     Reference< XContent > xProp = aMapPos->second;
     396             : 
     397           0 :     if (_bReadIfNeccessary && !xProp.is())
     398             :     {   // the object has never been accessed before, so we have to read it now
     399             :         // (that's the expensive part)
     400             : 
     401             :         // create the object and insert it into the map
     402           0 :         xProp = createObject(_rName);
     403           0 :         aMapPos->second = xProp;
     404           0 :         addObjectListener(xProp);
     405             :     }
     406             : 
     407           0 :     return xProp;
     408             : }
     409             : 
     410           0 : Sequence< ::rtl::OUString > SAL_CALL ODefinitionContainer::getElementNames(  ) throw(RuntimeException)
     411             : {
     412           0 :     MutexGuard aGuard(m_aMutex);
     413             : 
     414           0 :     Sequence< ::rtl::OUString > aNames(m_aDocumentMap.size());
     415           0 :     ::rtl::OUString* pNames = aNames.getArray();
     416           0 :     Documents::iterator aEnd = m_aDocumentMap.end();
     417           0 :     for (   Documents::iterator aNameIter = m_aDocumentMap.begin();
     418             :             aNameIter != aEnd;
     419             :             ++pNames, ++aNameIter
     420             :         )
     421             :     {
     422           0 :         *pNames = aNameIter->first;
     423             :     }
     424             : 
     425           0 :     return aNames;
     426             : }
     427             : 
     428           0 : sal_Bool SAL_CALL ODefinitionContainer::hasByName( const ::rtl::OUString& _rName ) throw(RuntimeException)
     429             : {
     430           0 :     MutexGuard aGuard(m_aMutex);
     431             : 
     432           0 :     return checkExistence(_rName);
     433             : }
     434             : 
     435           0 : void SAL_CALL ODefinitionContainer::disposing( const EventObject& _rSource ) throw(RuntimeException)
     436             : {
     437           0 :     MutexGuard aGuard(m_aMutex);
     438           0 :     Reference< XContent > xSource(_rSource.Source, UNO_QUERY);
     439             :     // it's one of our documents ....
     440           0 :     Documents::iterator aIter = m_aDocumentMap.begin();
     441           0 :     Documents::iterator aEnd = m_aDocumentMap.end();
     442           0 :     for (;aIter != aEnd;++aIter )
     443             :     {
     444           0 :         if ( xSource == aIter->second.get() )
     445             :         {
     446           0 :             removeObjectListener(xSource);
     447             :             // and clear our document map/vector, so the object will be recreated on next access
     448           0 :             aIter->second = Documents::mapped_type();
     449             :         }
     450           0 :     }
     451           0 : }
     452             : 
     453           0 : void ODefinitionContainer::implRemove(const ::rtl::OUString& _rName)
     454             : {
     455             :     // from the object maps
     456           0 :     Documents::iterator aFind = m_aDocumentMap.find(_rName);
     457           0 :     if ( aFind != m_aDocumentMap.end() )
     458             :     {
     459           0 :         m_aDocuments.erase( ::std::find(m_aDocuments.begin(),m_aDocuments.end(),aFind));
     460           0 :         m_aDocumentMap.erase(aFind);
     461             : 
     462           0 :         getDefinitions().erase( _rName );
     463             : 
     464           0 :         notifyDataSourceModified();
     465             :     }
     466           0 : }
     467             : 
     468             : namespace
     469             : {
     470           0 :     bool    lcl_ensureName( const Reference< XContent >& _rxContent, const ::rtl::OUString& _rName )
     471             :     {
     472           0 :         if ( !_rxContent.is() )
     473           0 :             return true;
     474             : 
     475             :         // obtain the current name. If it's the same as the new one,
     476             :         // don't do anything
     477             :         try
     478             :         {
     479           0 :             Reference< XPropertySet > xProps( _rxContent, UNO_QUERY );
     480           0 :             if ( xProps.is() )
     481             :             {
     482           0 :                 ::rtl::OUString sCurrentName;
     483           0 :                 OSL_VERIFY( xProps->getPropertyValue( PROPERTY_NAME ) >>= sCurrentName );
     484           0 :                 if ( sCurrentName.equals( _rName ) )
     485           0 :                     return true;
     486           0 :             }
     487             :         }
     488           0 :         catch( const Exception& )
     489             :         {
     490             :             OSL_FAIL( "lcl_ensureName: caught an exception while obtaining the current name!" );
     491             :         }
     492             : 
     493             :         // set the new name
     494           0 :         Reference< XRename > xRename( _rxContent, UNO_QUERY );
     495             :         OSL_ENSURE( xRename.is(), "lcl_ensureName: invalid content (not renameable)!" );
     496           0 :         if ( !xRename.is() )
     497           0 :             return false;
     498             :         try
     499             :         {
     500           0 :             xRename->rename( _rName );
     501           0 :             return true;
     502             :         }
     503           0 :         catch( const Exception& )
     504             :         {
     505             :             OSL_FAIL( "lcl_ensureName: caught an exception!" );
     506             :         }
     507           0 :         return false;
     508             :     }
     509             : }
     510             : 
     511           0 : void ODefinitionContainer::implAppend(const ::rtl::OUString& _rName, const Reference< XContent >& _rxNewObject)
     512             : {
     513           0 :     MutexGuard aGuard(m_aMutex);
     514             :     try
     515             :     {
     516           0 :         Reference<XChild> xChild(_rxNewObject,UNO_QUERY);
     517           0 :         if ( xChild.is() )
     518           0 :             xChild->setParent(static_cast<OWeakObject*>(this));
     519             : 
     520           0 :         ODefinitionContainer_Impl& rDefinitions( getDefinitions() );
     521           0 :         ODefinitionContainer_Impl::const_iterator aFind = rDefinitions.find( _rName );
     522           0 :         if ( aFind == rDefinitions.end() )
     523             :         {
     524             :             // ensure that the new object has the proper name.
     525             :             // Somebody could create an object with name "foo", and insert it as "bar"
     526             :             // into a container. In this case, we need to ensure that the object name
     527             :             // is also "bar"
     528             :             // #i44786#
     529           0 :             lcl_ensureName( _rxNewObject, _rName );
     530             : 
     531           0 :             ::rtl::Reference< OContentHelper > pContent = OContentHelper::getImplementation( _rxNewObject );
     532           0 :             if ( pContent.is() )
     533             :             {
     534           0 :                 TContentPtr pImpl = pContent->getImpl();
     535           0 :                 rDefinitions.erase( pImpl );
     536           0 :                 pImpl->m_aProps.aTitle = _rName;
     537           0 :                 rDefinitions.insert( _rName, pImpl );
     538           0 :             }
     539             :         }
     540             : 
     541             : 
     542           0 :         m_aDocuments.push_back(m_aDocumentMap.insert(Documents::value_type(_rName,_rxNewObject)).first);
     543           0 :         notifyDataSourceModified();
     544             :         // now update our structures
     545           0 :         if ( _rxNewObject.is() )
     546           0 :             addObjectListener(_rxNewObject);
     547             :     }
     548           0 :     catch(Exception&)
     549             :     {
     550             :         OSL_FAIL("ODefinitionContainer::implAppend: caught something !");
     551           0 :     }
     552           0 : }
     553             : 
     554           0 : void ODefinitionContainer::implReplace(const ::rtl::OUString& _rName, const Reference< XContent >& _rxNewObject)
     555             : {
     556             :     OSL_ENSURE(checkExistence(_rName), "ODefinitionContainer::implReplace : invalid name !");
     557             : 
     558           0 :     Documents::iterator aFind = m_aDocumentMap.find(_rName);
     559           0 :     removeObjectListener(aFind->second);
     560           0 :     aFind->second = _rxNewObject;
     561           0 :     addObjectListener(aFind->second);
     562           0 : }
     563             : 
     564           0 : void ODefinitionContainer::approveNewObject(const ::rtl::OUString& _sName,const Reference< XContent >& _rxObject) const
     565             : {
     566             :     // check the arguments
     567           0 :     if ( _sName.isEmpty() )
     568             :         throw IllegalArgumentException(
     569             :             DBA_RES( RID_STR_NAME_MUST_NOT_BE_EMPTY ),
     570             :             *this,
     571           0 :             0 );
     572             : 
     573           0 :     if ( m_bCheckSlash && _sName.indexOf( '/' ) != -1 )
     574             :         throw IllegalArgumentException(
     575             :             m_aErrorHelper.getErrorMessage( ErrorCondition::DB_OBJECT_NAME_WITH_SLASHES ),
     576             :             *this,
     577           0 :             0 );
     578             : 
     579           0 :     if ( !_rxObject.is() )
     580             :         throw IllegalArgumentException(
     581             :             DBA_RES( RID_STR_NO_NULL_OBJECTS_IN_CONTAINER ),
     582             :             *this,
     583           0 :             0 );
     584             : 
     585           0 :     const ODefinitionContainer_Impl& rDefinitions( getDefinitions() );
     586           0 :     if ( rDefinitions.find( _sName ) != rDefinitions.end() )
     587             :         throw ElementExistException(
     588             :             DBA_RES( RID_STR_NAME_ALREADY_USED ),
     589           0 :             *this );
     590             : 
     591           0 :     ::rtl::Reference< OContentHelper > pContent( OContentHelper::getImplementation( _rxObject ) );
     592           0 :     if ( !pContent.is() )
     593             :         throw IllegalArgumentException(
     594             :             DBA_RES( RID_STR_OBJECT_CONTAINER_MISMATCH ),
     595             :             *this,
     596           0 :             1 );
     597             : 
     598           0 :     if ( rDefinitions.find( pContent->getImpl() ) != rDefinitions.end() )
     599             :         throw ElementExistException(
     600             :             DBA_RES( RID_STR_OBJECT_ALREADY_CONTAINED ),
     601           0 :             *this );
     602           0 : }
     603             : 
     604             : // XPropertyChangeListener
     605           0 : void SAL_CALL ODefinitionContainer::propertyChange( const PropertyChangeEvent& evt ) throw (RuntimeException)
     606             : {
     607           0 :     ClearableMutexGuard aGuard(m_aMutex);
     608           0 :     if( evt.PropertyName == (rtl::OUString) PROPERTY_NAME || evt.PropertyName ==  "Title" )
     609             :     {
     610           0 :         m_bInPropertyChange = sal_True;
     611             :         try
     612             :         {
     613           0 :             ::rtl::OUString sNewName,sOldName;
     614           0 :             evt.OldValue >>= sOldName;
     615           0 :             evt.NewValue >>= sNewName;
     616           0 :             Reference<XContent> xContent( evt.Source, UNO_QUERY );
     617           0 :             removeObjectListener( xContent );
     618           0 :             implRemove( sOldName );
     619           0 :             implAppend( sNewName, xContent );
     620             :         }
     621           0 :         catch(const Exception&)
     622             :         {
     623             :             DBG_UNHANDLED_EXCEPTION();
     624           0 :             throw RuntimeException();
     625             :         }
     626           0 :         m_bInPropertyChange = sal_False;
     627           0 :     }
     628           0 : }
     629             : 
     630             : // XVetoableChangeListener
     631           0 : void SAL_CALL ODefinitionContainer::vetoableChange( const PropertyChangeEvent& aEvent ) throw (PropertyVetoException, RuntimeException)
     632             : {
     633           0 :     MutexGuard aGuard(m_aMutex);
     634             : 
     635           0 :     if( aEvent.PropertyName == (rtl::OUString) PROPERTY_NAME || aEvent.PropertyName == "Title" )
     636             :     {
     637           0 :         ::rtl::OUString sNewName;
     638           0 :         aEvent.NewValue >>= sNewName;
     639           0 :         if(hasByName(sNewName))
     640           0 :             throw PropertyVetoException();
     641           0 :     }
     642           0 : }
     643             : 
     644           0 : void ODefinitionContainer::addObjectListener(const Reference< XContent >& _xNewObject)
     645             : {
     646             :     OSL_ENSURE(_xNewObject.is(),"ODefinitionContainer::addObjectListener: Object is null!");
     647           0 :     Reference<XPropertySet> xProp(_xNewObject,UNO_QUERY);
     648           0 :     if ( xProp.is() )
     649             :     {
     650           0 :         xProp->addPropertyChangeListener(PROPERTY_NAME, this);
     651           0 :         xProp->addVetoableChangeListener(PROPERTY_NAME, this);
     652           0 :     }
     653           0 : }
     654             : 
     655           0 : void ODefinitionContainer::removeObjectListener(const Reference< XContent >& _xNewObject)
     656             : {
     657           0 :     Reference<XPropertySet> xProp(_xNewObject,UNO_QUERY);
     658           0 :     if ( xProp.is() )
     659             :     {
     660           0 :         xProp->removePropertyChangeListener(PROPERTY_NAME, this);
     661           0 :         xProp->removeVetoableChangeListener(PROPERTY_NAME, this);
     662           0 :     }
     663           0 : }
     664             : 
     665           0 : sal_Bool ODefinitionContainer::checkExistence(const ::rtl::OUString& _rName)
     666             : {
     667           0 :     return m_aDocumentMap.find(_rName) != m_aDocumentMap.end();
     668             : }
     669             : 
     670             : }
     671             : // namespace dbaccess
     672             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10