1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "definitioncontainer.hxx"
21 #include "dbastrings.hrc"
22 #include "apitools.hxx"
23 #include "core_resource.hxx"
24 #include "core_resource.hrc"
26 #include <tools/debug.hxx>
27 #include <tools/diagnose_ex.h>
28 #include <osl/diagnose.h>
29 #include <comphelper/sequence.hxx>
30 #include <comphelper/enumhelper.hxx>
31 #include <comphelper/extract.hxx>
32 #include <cppuhelper/exc_hlp.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>
40 using namespace ::com::sun::star::uno
;
41 using namespace ::com::sun::star::lang
;
42 using namespace ::com::sun::star::util
;
43 using namespace ::com::sun::star::beans
;
44 using namespace ::com::sun::star::container
;
45 using namespace ::com::sun::star::sdbcx
;
46 using namespace ::com::sun::star::sdb
;
47 using namespace ::osl
;
48 using namespace ::comphelper
;
49 using namespace ::cppu
;
50 using namespace ::com::sun::star::ucb
;
55 // ODefinitionContainer_Impl
56 void ODefinitionContainer_Impl::erase( const TContentPtr
& _pDefinition
)
58 NamedDefinitions::const_iterator aPos
= find( _pDefinition
);
60 m_aDefinitions
.erase( aPos
);
63 ODefinitionContainer_Impl::const_iterator
ODefinitionContainer_Impl::find( const TContentPtr
& _pDefinition
) const
65 return ::std::find_if(
66 m_aDefinitions
.begin(),
68 [&_pDefinition
] (const NamedDefinitions::value_type
& namedDef
) {
69 return namedDef
.second
== _pDefinition
;
73 ODefinitionContainer_Impl::iterator
ODefinitionContainer_Impl::find( const TContentPtr
& _pDefinition
)
75 return ::std::find_if(
76 m_aDefinitions
.begin(),
78 [&_pDefinition
] (const NamedDefinitions::value_type
& namedDef
) {
79 return namedDef
.second
== _pDefinition
;
83 // ODefinitionContainer
85 ODefinitionContainer::ODefinitionContainer( const Reference
< XComponentContext
>& _xORB
86 , const Reference
< XInterface
>& _xParentContainer
87 , const TContentPtr
& _pImpl
90 :OContentHelper(_xORB
,_xParentContainer
,_pImpl
)
91 ,m_aApproveListeners(m_aMutex
)
92 ,m_aContainerListeners(m_aMutex
)
93 ,m_bInPropertyChange(false)
94 ,m_bCheckSlash(_bCheckSlash
)
96 m_pImpl
->m_aProps
.bIsDocument
= false;
97 m_pImpl
->m_aProps
.bIsFolder
= true;
99 const ODefinitionContainer_Impl
& rDefinitions( getDefinitions() );
100 ODefinitionContainer_Impl::const_iterator aEnd
= rDefinitions
.end();
101 for ( ODefinitionContainer_Impl::const_iterator aDefinition
= rDefinitions
.begin();
105 m_aDocuments
.push_back(
106 m_aDocumentMap
.insert(
107 Documents::value_type( aDefinition
->first
, Documents::mapped_type() ) ).first
);
111 void SAL_CALL
ODefinitionContainer::disposing()
113 OContentHelper::disposing();
115 MutexGuard
aGuard(m_aMutex
);
117 // say goodbye to our listeners
118 EventObject
aEvt(*this);
119 m_aApproveListeners
.disposeAndClear(aEvt
);
120 m_aContainerListeners
.disposeAndClear(aEvt
);
122 // dispose our elements
123 Documents::const_iterator aIter
= m_aDocumentMap
.begin();
124 Documents::const_iterator aEnd
= m_aDocumentMap
.end();
126 for (; aIter
!= aEnd
; ++aIter
)
128 Reference
<XContent
> xProp
= aIter
->second
;
131 removeObjectListener(xProp
);
132 ::comphelper::disposeComponent(xProp
);
136 // remove our elements
137 m_aDocuments
.clear();
138 // !!! do this before clearing the map which the vector elements refer to !!!
139 m_aDocumentMap
.clear();
142 ODefinitionContainer::~ODefinitionContainer()
146 IMPLEMENT_FORWARD_XINTERFACE2( ODefinitionContainer
,OContentHelper
,ODefinitionContainer_Base
)
147 IMPLEMENT_GETTYPES2(ODefinitionContainer
,OContentHelper
,ODefinitionContainer_Base
);
149 css::uno::Sequence
<sal_Int8
> ODefinitionContainer::getImplementationId()
150 throw (css::uno::RuntimeException
, std::exception
)
152 return css::uno::Sequence
<sal_Int8
>();
156 OUString SAL_CALL
ODefinitionContainer::getImplementationName( ) throw(RuntimeException
, std::exception
)
158 return OUString("com.sun.star.sdb.ODefinitionContainer");
161 Sequence
< OUString
> SAL_CALL
ODefinitionContainer::getSupportedServiceNames( ) throw(RuntimeException
, std::exception
)
163 Sequence
< OUString
> aReturn(2);
164 aReturn
.getArray()[0] = "com.sun.star.sdb.DefinitionContainer";
165 aReturn
.getArray()[1] = "com.sun.star.ucb.Content";
170 void SAL_CALL
ODefinitionContainer::insertByName( const OUString
& _rName
, const Any
& aElement
) throw(IllegalArgumentException
, ElementExistException
, WrappedTargetException
, RuntimeException
, std::exception
)
172 ResettableMutexGuard
aGuard(m_aMutex
);
174 // approve the new object
175 Reference
< XContent
> xNewElement(aElement
,UNO_QUERY
);
176 approveNewObject( _rName
, xNewElement
); // will throw if necessary
178 notifyByName( aGuard
, _rName
, xNewElement
, nullptr, E_INSERTED
, ApproveListeners
);
179 implAppend( _rName
, xNewElement
);
180 notifyByName( aGuard
, _rName
, xNewElement
, nullptr, E_INSERTED
, ContainerListemers
);
183 void SAL_CALL
ODefinitionContainer::removeByName( const OUString
& _rName
) throw(NoSuchElementException
, WrappedTargetException
, RuntimeException
, std::exception
)
185 ResettableMutexGuard
aGuard(m_aMutex
);
187 // check the arguments
188 if (_rName
.isEmpty())
189 throw IllegalArgumentException();
191 if (!checkExistence(_rName
))
192 throw NoSuchElementException(_rName
,*this);
194 // the old element (for the notifications)
195 Reference
< XContent
> xOldElement
= implGetByName( _rName
, impl_haveAnyListeners_nothrow() );
198 notifyByName( aGuard
, _rName
, nullptr, xOldElement
, E_REMOVED
, ApproveListeners
);
199 implRemove( _rName
);
200 notifyByName( aGuard
, _rName
, nullptr, xOldElement
, E_REMOVED
, ContainerListemers
);
202 removeObjectListener( xOldElement
);
203 disposeComponent(xOldElement
);
207 void SAL_CALL
ODefinitionContainer::replaceByName( const OUString
& _rName
, const Any
& aElement
) throw(IllegalArgumentException
, NoSuchElementException
, WrappedTargetException
, RuntimeException
, std::exception
)
209 ResettableMutexGuard
aGuard(m_aMutex
);
213 // let derived classes approve the new object
214 Reference
< XContent
> xNewElement(aElement
,UNO_QUERY
);
215 approveNewObject( _rName
, xNewElement
); // will throw if necessary
217 // the old element (for the notifications)
218 Reference
< XContent
> xOldElement
= implGetByName( _rName
, impl_haveAnyListeners_nothrow() );
220 notifyByName( aGuard
, _rName
, xNewElement
, xOldElement
, E_REPLACED
, ApproveListeners
);
221 implReplace( _rName
, xNewElement
);
222 notifyByName( aGuard
, _rName
, xNewElement
, xOldElement
, E_REPLACED
, ContainerListemers
);
225 disposeComponent(xOldElement
);
227 catch (const RuntimeException
&)
231 catch (const NoSuchElementException
&)
235 catch (const WrappedTargetException
&)
239 catch (const Exception
& e
)
241 css::uno::Any
a(cppu::getCaughtException());
242 throw css::lang::WrappedTargetException(
243 "wrapped Exception " + e
.Message
,
244 css::uno::Reference
<css::uno::XInterface
>(), a
);
250 typedef Reference
< XVeto
> ( SAL_CALL
XContainerApproveListener::*ContainerApprovalMethod
)( const ContainerEvent
& );
252 struct RaiseExceptionFromVeto
255 ContainerApprovalMethod m_pMethod
;
256 const ContainerEvent
& m_rEvent
;
259 explicit RaiseExceptionFromVeto( ContainerApprovalMethod _pMethod
, const ContainerEvent
& _rEvent
)
260 :m_pMethod( _pMethod
)
265 void operator()( const Reference
< XContainerApproveListener
>& Listener
) const
267 Reference
< XVeto
> xVeto
= (Listener
.get()->*m_pMethod
)( m_rEvent
);
271 Any eVetoDetails
= xVeto
->getDetails();
273 IllegalArgumentException aIllegalArgumentError
;
274 if ( eVetoDetails
>>= aIllegalArgumentError
)
275 throw aIllegalArgumentError
;
277 WrappedTargetException aWrappedError
;
278 if ( eVetoDetails
>>= aWrappedError
)
281 throw WrappedTargetException( xVeto
->getReason(), Listener
.get(), eVetoDetails
);
286 void ODefinitionContainer::notifyByName( ResettableMutexGuard
& _rGuard
, const OUString
& _rName
,
287 const Reference
< XContent
>& _xNewElement
, const Reference
< XContent
>& _xOldElement
,
288 ContainerOperation _eOperation
, ListenerType _eType
)
290 bool bApprove
= ( _eType
== ApproveListeners
);
292 ::comphelper::OInterfaceContainerHelper2
& rContainer( bApprove
? m_aApproveListeners
: m_aContainerListeners
);
293 if ( !rContainer
.getLength() )
296 ContainerEvent
aEvent( *this, makeAny( _rName
), makeAny( _xNewElement
), makeAny( _xOldElement
) );
299 switch ( _eOperation
)
303 rContainer
.forEach
< XContainerApproveListener
, RaiseExceptionFromVeto
>(
304 RaiseExceptionFromVeto( &XContainerApproveListener::approveInsertElement
, aEvent
) );
306 rContainer
.notifyEach( &XContainerListener::elementInserted
, aEvent
);
310 rContainer
.forEach
< XContainerApproveListener
, RaiseExceptionFromVeto
>(
311 RaiseExceptionFromVeto( &XContainerApproveListener::approveReplaceElement
, aEvent
) );
313 rContainer
.notifyEach( &XContainerListener::elementReplaced
, aEvent
);
317 rContainer
.forEach
< XContainerApproveListener
, RaiseExceptionFromVeto
>(
318 RaiseExceptionFromVeto( &XContainerApproveListener::approveRemoveElement
, aEvent
) );
320 rContainer
.notifyEach( &XContainerListener::elementRemoved
, aEvent
);
328 void SAL_CALL
ODefinitionContainer::addContainerListener( const Reference
< XContainerListener
>& _rxListener
) throw(RuntimeException
, std::exception
)
330 if (_rxListener
.is())
331 m_aContainerListeners
.addInterface(_rxListener
);
334 void SAL_CALL
ODefinitionContainer::removeContainerListener( const Reference
< XContainerListener
>& _rxListener
) throw(RuntimeException
, std::exception
)
336 if (_rxListener
.is())
337 m_aContainerListeners
.removeInterface(_rxListener
);
340 void SAL_CALL
ODefinitionContainer::addContainerApproveListener( const Reference
< XContainerApproveListener
>& Listener
) throw (RuntimeException
, std::exception
)
343 m_aApproveListeners
.addInterface( Listener
);
346 void SAL_CALL
ODefinitionContainer::removeContainerApproveListener( const Reference
< XContainerApproveListener
>& Listener
) throw (RuntimeException
, std::exception
)
349 m_aApproveListeners
.removeInterface( Listener
);
353 Type SAL_CALL
ODefinitionContainer::getElementType( ) throw (RuntimeException
, std::exception
)
355 return cppu::UnoType
<XContent
>::get();
358 sal_Bool SAL_CALL
ODefinitionContainer::hasElements( ) throw (RuntimeException
, std::exception
)
360 MutexGuard
aGuard(m_aMutex
);
361 return !m_aDocuments
.empty();
364 // XEnumerationAccess
365 Reference
< XEnumeration
> SAL_CALL
ODefinitionContainer::createEnumeration( ) throw(RuntimeException
, std::exception
)
367 MutexGuard
aGuard(m_aMutex
);
368 return new ::comphelper::OEnumerationByIndex(static_cast<XIndexAccess
*>(this));
372 sal_Int32 SAL_CALL
ODefinitionContainer::getCount( ) throw(RuntimeException
, std::exception
)
374 MutexGuard
aGuard(m_aMutex
);
375 return m_aDocuments
.size();
378 Any SAL_CALL
ODefinitionContainer::getByIndex( sal_Int32 _nIndex
) throw(IndexOutOfBoundsException
, WrappedTargetException
, RuntimeException
, std::exception
)
380 MutexGuard
aGuard(m_aMutex
);
382 if ((_nIndex
< 0) || (_nIndex
>= (sal_Int32
)m_aDocuments
.size()))
383 throw IndexOutOfBoundsException();
385 Documents::iterator aPos
= m_aDocuments
[_nIndex
];
386 Reference
<XContent
> xProp
= aPos
->second
;
388 { // that's the first access to the object
390 xProp
= createObject(aPos
->first
);
391 aPos
->second
= Documents::mapped_type();
392 // and update the name-access map
395 return makeAny(xProp
);
398 Any SAL_CALL
ODefinitionContainer::getByName( const OUString
& _rName
) throw(NoSuchElementException
, WrappedTargetException
, RuntimeException
, std::exception
)
400 MutexGuard
aGuard(m_aMutex
);
402 return makeAny( implGetByName( _rName
, true ) );
405 Reference
< XContent
> ODefinitionContainer::implGetByName(const OUString
& _rName
, bool _bReadIfNecessary
) throw (NoSuchElementException
)
407 Documents::iterator aMapPos
= m_aDocumentMap
.find(_rName
);
408 if (aMapPos
== m_aDocumentMap
.end())
409 throw NoSuchElementException(_rName
,*this);
411 Reference
< XContent
> xProp
= aMapPos
->second
;
413 if (_bReadIfNecessary
&& !xProp
.is())
414 { // the object has never been accessed before, so we have to read it now
415 // (that's the expensive part)
417 // create the object and insert it into the map
418 xProp
= createObject(_rName
);
419 aMapPos
->second
= xProp
;
420 addObjectListener(xProp
);
426 Sequence
< OUString
> SAL_CALL
ODefinitionContainer::getElementNames( ) throw(RuntimeException
, std::exception
)
428 MutexGuard
aGuard(m_aMutex
);
430 Sequence
< OUString
> aNames(m_aDocumentMap
.size());
431 OUString
* pNames
= aNames
.getArray();
432 Documents::const_iterator aEnd
= m_aDocumentMap
.end();
433 for ( Documents::const_iterator aNameIter
= m_aDocumentMap
.begin();
435 ++pNames
, ++aNameIter
438 *pNames
= aNameIter
->first
;
444 sal_Bool SAL_CALL
ODefinitionContainer::hasByName( const OUString
& _rName
) throw(RuntimeException
, std::exception
)
446 MutexGuard
aGuard(m_aMutex
);
448 return checkExistence(_rName
);
451 void SAL_CALL
ODefinitionContainer::disposing( const EventObject
& _rSource
) throw(RuntimeException
, std::exception
)
453 MutexGuard
aGuard(m_aMutex
);
454 Reference
< XContent
> xSource(_rSource
.Source
, UNO_QUERY
);
455 // it's one of our documents ....
456 Documents::iterator aIter
= m_aDocumentMap
.begin();
457 Documents::const_iterator aEnd
= m_aDocumentMap
.end();
458 for (;aIter
!= aEnd
;++aIter
)
460 if ( xSource
== aIter
->second
.get() )
462 removeObjectListener(xSource
);
463 // and clear our document map/vector, so the object will be recreated on next access
464 aIter
->second
= Documents::mapped_type();
469 void ODefinitionContainer::implRemove(const OUString
& _rName
)
471 // from the object maps
472 Documents::const_iterator aFind
= m_aDocumentMap
.find(_rName
);
473 if ( aFind
!= m_aDocumentMap
.end() )
475 m_aDocuments
.erase( ::std::find(m_aDocuments
.begin(),m_aDocuments
.end(),aFind
));
476 m_aDocumentMap
.erase(aFind
);
478 getDefinitions().erase( _rName
);
480 notifyDataSourceModified();
486 bool lcl_ensureName( const Reference
< XContent
>& _rxContent
, const OUString
& _rName
)
488 if ( !_rxContent
.is() )
491 // obtain the current name. If it's the same as the new one,
495 Reference
< XPropertySet
> xProps( _rxContent
, UNO_QUERY
);
498 OUString sCurrentName
;
499 OSL_VERIFY( xProps
->getPropertyValue( PROPERTY_NAME
) >>= sCurrentName
);
500 if ( sCurrentName
.equals( _rName
) )
504 catch( const Exception
& )
506 OSL_FAIL( "lcl_ensureName: caught an exception while obtaining the current name!" );
510 Reference
< XRename
> xRename( _rxContent
, UNO_QUERY
);
511 OSL_ENSURE( xRename
.is(), "lcl_ensureName: invalid content (not renameable)!" );
516 xRename
->rename( _rName
);
519 catch( const Exception
& )
521 OSL_FAIL( "lcl_ensureName: caught an exception!" );
527 void ODefinitionContainer::implAppend(const OUString
& _rName
, const Reference
< XContent
>& _rxNewObject
)
529 MutexGuard
aGuard(m_aMutex
);
532 Reference
<XChild
> xChild(_rxNewObject
,UNO_QUERY
);
534 xChild
->setParent(static_cast<OWeakObject
*>(this));
536 ODefinitionContainer_Impl
& rDefinitions( getDefinitions() );
537 ODefinitionContainer_Impl::const_iterator aFind
= rDefinitions
.find( _rName
);
538 if ( aFind
== rDefinitions
.end() )
540 // ensure that the new object has the proper name.
541 // Somebody could create an object with name "foo", and insert it as "bar"
542 // into a container. In this case, we need to ensure that the object name
545 lcl_ensureName( _rxNewObject
, _rName
);
547 ::rtl::Reference
< OContentHelper
> pContent
= OContentHelper::getImplementation( _rxNewObject
);
550 TContentPtr pImpl
= pContent
->getImpl();
551 rDefinitions
.erase( pImpl
);
552 pImpl
->m_aProps
.aTitle
= _rName
;
553 rDefinitions
.insert( _rName
, pImpl
);
557 m_aDocuments
.push_back(m_aDocumentMap
.insert(Documents::value_type(_rName
,_rxNewObject
)).first
);
558 notifyDataSourceModified();
559 // now update our structures
560 if ( _rxNewObject
.is() )
561 addObjectListener(_rxNewObject
);
565 OSL_FAIL("ODefinitionContainer::implAppend: caught something !");
569 void ODefinitionContainer::implReplace(const OUString
& _rName
, const Reference
< XContent
>& _rxNewObject
)
571 OSL_ENSURE(checkExistence(_rName
), "ODefinitionContainer::implReplace : invalid name !");
573 Documents::iterator aFind
= m_aDocumentMap
.find(_rName
);
574 removeObjectListener(aFind
->second
);
575 aFind
->second
= _rxNewObject
;
576 addObjectListener(aFind
->second
);
579 void ODefinitionContainer::approveNewObject(const OUString
& _sName
,const Reference
< XContent
>& _rxObject
) const
581 // check the arguments
582 if ( _sName
.isEmpty() )
583 throw IllegalArgumentException(
584 DBA_RES( RID_STR_NAME_MUST_NOT_BE_EMPTY
),
588 if ( m_bCheckSlash
&& _sName
.indexOf( '/' ) != -1 )
589 throw IllegalArgumentException(
590 m_aErrorHelper
.getErrorMessage( ErrorCondition::DB_OBJECT_NAME_WITH_SLASHES
),
594 if ( !_rxObject
.is() )
595 throw IllegalArgumentException(
596 DBA_RES( RID_STR_NO_NULL_OBJECTS_IN_CONTAINER
),
600 const ODefinitionContainer_Impl
& rDefinitions( getDefinitions() );
601 if ( rDefinitions
.find( _sName
) != rDefinitions
.end() )
602 throw ElementExistException(
603 DBA_RES( RID_STR_NAME_ALREADY_USED
),
606 ::rtl::Reference
< OContentHelper
> pContent( OContentHelper::getImplementation( _rxObject
) );
607 if ( !pContent
.is() )
608 throw IllegalArgumentException(
609 DBA_RES( RID_STR_OBJECT_CONTAINER_MISMATCH
),
613 if ( rDefinitions
.find( pContent
->getImpl() ) != rDefinitions
.end() )
614 throw ElementExistException(
615 DBA_RES( RID_STR_OBJECT_ALREADY_CONTAINED
),
619 // XPropertyChangeListener
620 void SAL_CALL
ODefinitionContainer::propertyChange( const PropertyChangeEvent
& evt
) throw (RuntimeException
, std::exception
)
622 ClearableMutexGuard
aGuard(m_aMutex
);
623 if( evt
.PropertyName
== PROPERTY_NAME
|| evt
.PropertyName
== "Title" )
625 m_bInPropertyChange
= true;
628 OUString sNewName
,sOldName
;
629 evt
.OldValue
>>= sOldName
;
630 evt
.NewValue
>>= sNewName
;
631 Reference
<XContent
> xContent( evt
.Source
, UNO_QUERY
);
632 removeObjectListener( xContent
);
633 implRemove( sOldName
);
634 implAppend( sNewName
, xContent
);
636 catch(const Exception
&)
638 DBG_UNHANDLED_EXCEPTION();
639 throw RuntimeException();
641 m_bInPropertyChange
= false;
645 // XVetoableChangeListener
646 void SAL_CALL
ODefinitionContainer::vetoableChange( const PropertyChangeEvent
& aEvent
) throw (PropertyVetoException
, RuntimeException
, std::exception
)
648 MutexGuard
aGuard(m_aMutex
);
650 if( aEvent
.PropertyName
== PROPERTY_NAME
|| aEvent
.PropertyName
== "Title" )
653 aEvent
.NewValue
>>= sNewName
;
654 if(hasByName(sNewName
))
655 throw PropertyVetoException();
659 void ODefinitionContainer::addObjectListener(const Reference
< XContent
>& _xNewObject
)
661 OSL_ENSURE(_xNewObject
.is(),"ODefinitionContainer::addObjectListener: Object is null!");
662 Reference
<XPropertySet
> xProp(_xNewObject
,UNO_QUERY
);
665 xProp
->addPropertyChangeListener(PROPERTY_NAME
, this);
666 xProp
->addVetoableChangeListener(PROPERTY_NAME
, this);
670 void ODefinitionContainer::removeObjectListener(const Reference
< XContent
>& _xNewObject
)
672 Reference
<XPropertySet
> xProp(_xNewObject
,UNO_QUERY
);
675 xProp
->removePropertyChangeListener(PROPERTY_NAME
, this);
676 xProp
->removeVetoableChangeListener(PROPERTY_NAME
, this);
680 bool ODefinitionContainer::checkExistence(const OUString
& _rName
)
682 return m_aDocumentMap
.find(_rName
) != m_aDocumentMap
.end();
687 // namespace dbaccess
688 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */