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 .
19 #include "UndoActions.hxx"
20 #include "UndoEnv.hxx"
21 #include "formatnormalizer.hxx"
22 #include "conditionupdater.hxx"
23 #include "corestrings.hrc"
24 #include "rptui_slotid.hrc"
26 #include "ModuleHelper.hxx"
27 #include "RptObject.hxx"
28 #include "RptPage.hxx"
29 #include "RptResId.hrc"
30 #include "RptModel.hxx"
32 #include <boost/noncopyable.hpp>
33 #include <com/sun/star/script/XEventAttacherManager.hpp>
34 #include <com/sun/star/container/XChild.hpp>
35 #include <com/sun/star/container/XNameContainer.hpp>
36 #include <com/sun/star/beans/theIntrospection.hpp>
37 #include <com/sun/star/beans/PropertyAttribute.hpp>
38 #include <com/sun/star/util/XModifyBroadcaster.hpp>
39 #include <com/sun/star/beans/XIntrospectionAccess.hpp>
40 #include <com/sun/star/beans/XIntrospection.hpp>
42 #include <connectivity/dbtools.hxx>
43 #include <svl/smplhint.hxx>
44 #include <tools/diagnose_ex.h>
45 #include <comphelper/stl_types.hxx>
46 #include <vcl/svapp.hxx>
47 #include <dbaccess/dbsubcomponentcontroller.hxx>
48 #include <svx/unoshape.hxx>
49 #include <osl/mutex.hxx>
53 using namespace ::com::sun::star
;
56 using namespace script
;
57 using namespace beans
;
60 using namespace container
;
61 using namespace report
;
67 bool bIsReadonlyOrTransient
;
69 PropertyInfo( const bool i_bIsTransientOrReadOnly
)
70 :bIsReadonlyOrTransient( i_bIsTransientOrReadOnly
)
75 typedef std::unordered_map
< OUString
, PropertyInfo
, OUStringHash
> PropertiesInfo
;
79 PropertiesInfo aProperties
;
80 Reference
< XPropertySet
> xPropertyIntrospection
;
84 ,xPropertyIntrospection()
89 typedef ::std::map
< Reference
< XPropertySet
>, ObjectInfo
, ::comphelper::OInterfaceCompare
< XPropertySet
> > PropertySetInfoCache
;
93 class OXUndoEnvironmentImpl
: private boost::noncopyable
96 OReportModel
& m_rModel
;
97 PropertySetInfoCache m_aPropertySetCache
;
98 FormatNormalizer m_aFormatNormalizer
;
99 ConditionUpdater m_aConditionUpdater
;
100 ::osl::Mutex m_aMutex
;
101 ::std::vector
< uno::Reference
< container::XChild
> > m_aSections
;
102 Reference
< XIntrospection
> m_xIntrospection
;
103 oslInterlockedCount m_nLocks
;
107 OXUndoEnvironmentImpl(OReportModel
& _rModel
);
110 OXUndoEnvironmentImpl::OXUndoEnvironmentImpl(OReportModel
& _rModel
) : m_rModel(_rModel
)
111 ,m_aFormatNormalizer( _rModel
)
112 ,m_aConditionUpdater()
121 OXUndoEnvironment::OXUndoEnvironment(OReportModel
& _rModel
)
122 :m_pImpl(new OXUndoEnvironmentImpl(_rModel
) )
124 StartListening(m_pImpl
->m_rModel
);
128 OXUndoEnvironment::~OXUndoEnvironment()
132 void OXUndoEnvironment::Lock()
134 OSL_ENSURE(m_refCount
,"Illegal call to dead object!");
135 osl_atomic_increment( &m_pImpl
->m_nLocks
);
137 void OXUndoEnvironment::UnLock()
139 OSL_ENSURE(m_refCount
,"Illegal call to dead object!");
141 osl_atomic_decrement( &m_pImpl
->m_nLocks
);
143 bool OXUndoEnvironment::IsLocked() const { return m_pImpl
->m_nLocks
!= 0; }
145 void OXUndoEnvironment::RemoveSection(OReportPage
* _pPage
)
149 Reference
< XInterface
> xSection(_pPage
->getSection());
151 RemoveElement( xSection
);
155 void OXUndoEnvironment::Clear(const Accessor
& /*_r*/)
157 OUndoEnvLock
aLock(*this);
159 m_pImpl
->m_aPropertySetCache
.clear();
161 sal_uInt16 nCount
= m_pImpl
->m_rModel
.GetPageCount();
163 for (i
= 0; i
< nCount
; i
++)
165 OReportPage
* pPage
= PTR_CAST( OReportPage
, m_pImpl
->m_rModel
.GetPage(i
) );
166 RemoveSection(pPage
);
169 nCount
= m_pImpl
->m_rModel
.GetMasterPageCount();
170 for (i
= 0; i
< nCount
; i
++)
172 OReportPage
* pPage
= PTR_CAST( OReportPage
, m_pImpl
->m_rModel
.GetMasterPage(i
) );
173 RemoveSection(pPage
);
176 m_pImpl
->m_aSections
.clear();
178 if (IsListening(m_pImpl
->m_rModel
))
179 EndListening(m_pImpl
->m_rModel
);
183 void OXUndoEnvironment::ModeChanged()
185 m_pImpl
->m_bReadOnly
= !m_pImpl
->m_bReadOnly
;
187 if (!m_pImpl
->m_bReadOnly
)
188 StartListening(m_pImpl
->m_rModel
);
190 EndListening(m_pImpl
->m_rModel
);
194 void OXUndoEnvironment::Notify( SfxBroadcaster
& /*rBC*/, const SfxHint
& rHint
)
196 const SfxSimpleHint
* pSimpleHint
= dynamic_cast<const SfxSimpleHint
*>(&rHint
);
197 if (pSimpleHint
&& pSimpleHint
->GetId() == SFX_HINT_MODECHANGED
)
203 void SAL_CALL
OXUndoEnvironment::disposing(const EventObject
& e
) throw( RuntimeException
, std::exception
)
205 // check if it's an object we have cached information about
206 Reference
< XPropertySet
> xSourceSet(e
.Source
, UNO_QUERY
);
207 if ( xSourceSet
.is() )
209 uno::Reference
< report::XSection
> xSection(xSourceSet
,uno::UNO_QUERY
);
211 RemoveSection(xSection
);
213 RemoveElement(xSourceSet
);
217 // XPropertyChangeListener
219 void SAL_CALL
OXUndoEnvironment::propertyChange( const PropertyChangeEvent
& _rEvent
) throw(uno::RuntimeException
, std::exception
)
222 ::osl::ClearableMutexGuard
aGuard( m_pImpl
->m_aMutex
);
227 Reference
< XPropertySet
> xSet( _rEvent
.Source
, UNO_QUERY
);
231 dbaui::DBSubComponentController
* pController
= m_pImpl
->m_rModel
.getController();
235 // no Undo for transient and readonly props.
236 // let's see if we know something about the set
237 PropertySetInfoCache::iterator objectPos
= m_pImpl
->m_aPropertySetCache
.find(xSet
);
238 if (objectPos
== m_pImpl
->m_aPropertySetCache
.end())
240 objectPos
= m_pImpl
->m_aPropertySetCache
.insert( PropertySetInfoCache::value_type(
243 DBG_ASSERT(objectPos
!= m_pImpl
->m_aPropertySetCache
.end(), "OXUndoEnvironment::propertyChange : just inserted it ... why it's not there ?");
245 if ( objectPos
== m_pImpl
->m_aPropertySetCache
.end() )
248 // now we have access to the cached info about the set
249 // let's see what we know about the property
250 ObjectInfo
& rObjectInfo
= objectPos
->second
;
251 PropertiesInfo::iterator aPropertyPos
= rObjectInfo
.aProperties
.find( _rEvent
.PropertyName
);
252 if ( aPropertyPos
== rObjectInfo
.aProperties
.end() )
253 { // nothing 'til now ... have to change this ....
255 Reference
< XPropertySetInfo
> xPSI( xSet
->getPropertySetInfo(), UNO_SET_THROW
);
256 sal_Int32 nPropertyAttributes
= 0;
259 if ( xPSI
->hasPropertyByName( _rEvent
.PropertyName
) )
261 nPropertyAttributes
= xPSI
->getPropertyByName( _rEvent
.PropertyName
).Attributes
;
265 // it's perfectly valid for a component to notify a change in a property which it doesn't have - as long
266 // as it has an attribute with this name
267 if ( !rObjectInfo
.xPropertyIntrospection
.is() )
269 if ( !m_pImpl
->m_xIntrospection
.is() )
271 m_pImpl
->m_xIntrospection
= theIntrospection::get( m_pImpl
->m_rModel
.getController()->getORB() );
273 Reference
< XIntrospectionAccess
> xIntrospection(
274 m_pImpl
->m_xIntrospection
->inspect( makeAny( _rEvent
.Source
) ),
277 rObjectInfo
.xPropertyIntrospection
.set( xIntrospection
->queryAdapter( cppu::UnoType
<XPropertySet
>::get() ), UNO_QUERY_THROW
);
279 if ( rObjectInfo
.xPropertyIntrospection
.is() )
281 xPSI
.set( rObjectInfo
.xPropertyIntrospection
->getPropertySetInfo(), UNO_SET_THROW
);
282 nPropertyAttributes
= xPSI
->getPropertyByName( _rEvent
.PropertyName
).Attributes
;
286 catch( const Exception
& )
288 DBG_UNHANDLED_EXCEPTION();
290 const bool bTransReadOnly
=
291 ( ( nPropertyAttributes
& PropertyAttribute::READONLY
) != 0 )
292 || ( ( nPropertyAttributes
& PropertyAttribute::TRANSIENT
) != 0 );
294 // insert the new entry
295 aPropertyPos
= rObjectInfo
.aProperties
.insert( PropertiesInfo::value_type(
296 _rEvent
.PropertyName
,
297 PropertyInfo( bTransReadOnly
)
299 DBG_ASSERT(aPropertyPos
!= rObjectInfo
.aProperties
.end(), "OXUndoEnvironment::propertyChange : just inserted it ... why it's not there ?");
304 // now we have access to the cached info about the property affected
305 // and are able to decide whether or not we need an undo action
307 // no UNDO for transient/readonly properties
308 if ( aPropertyPos
->second
.bIsReadonlyOrTransient
)
311 // give components with sub responsibilities a chance
312 m_pImpl
->m_aFormatNormalizer
.notifyPropertyChange( _rEvent
);
313 m_pImpl
->m_aConditionUpdater
.notifyPropertyChange( _rEvent
);
316 // TODO: this is a potential race condition: two threads here could in theory
317 // add their undo actions out-of-order
319 SolarMutexGuard aSolarGuard
;
320 ORptUndoPropertyAction
* pUndo
= NULL
;
323 uno::Reference
< report::XSection
> xSection( xSet
, uno::UNO_QUERY
);
326 uno::Reference
< report::XGroup
> xGroup
= xSection
->getGroup();
328 pUndo
= new OUndoPropertyGroupSectionAction( m_pImpl
->m_rModel
, _rEvent
, OGroupHelper::getMemberFunction( xSection
), xGroup
);
330 pUndo
= new OUndoPropertyReportSectionAction( m_pImpl
->m_rModel
, _rEvent
, OReportHelper::getMemberFunction( xSection
), xSection
->getReportDefinition() );
333 catch(const Exception
&)
335 DBG_UNHANDLED_EXCEPTION();
339 pUndo
= new ORptUndoPropertyAction( m_pImpl
->m_rModel
, _rEvent
);
341 m_pImpl
->m_rModel
.GetSdrUndoManager()->AddUndoAction( pUndo
);
342 pController
->InvalidateAll();
345 ::std::vector
< uno::Reference
< container::XChild
> >::const_iterator
OXUndoEnvironment::getSection(const Reference
<container::XChild
>& _xContainer
) const
347 ::std::vector
< uno::Reference
< container::XChild
> >::const_iterator aFind
= m_pImpl
->m_aSections
.end();
348 if ( _xContainer
.is() )
350 aFind
= ::std::find(m_pImpl
->m_aSections
.begin(),m_pImpl
->m_aSections
.end(),_xContainer
);
352 if ( aFind
== m_pImpl
->m_aSections
.end() )
354 Reference
<container::XChild
> xParent(_xContainer
->getParent(),uno::UNO_QUERY
);
355 aFind
= getSection(xParent
);
360 // XContainerListener
362 void SAL_CALL
OXUndoEnvironment::elementInserted(const ContainerEvent
& evt
) throw(uno::RuntimeException
, std::exception
)
364 SolarMutexGuard aSolarGuard
;
365 ::osl::MutexGuard
aGuard( m_pImpl
->m_aMutex
);
367 // neues Object zum lauschen
368 Reference
< uno::XInterface
> xIface( evt
.Element
, UNO_QUERY
);
371 Reference
< report::XReportComponent
> xReportComponent( xIface
, UNO_QUERY
);
372 if ( xReportComponent
.is() )
374 Reference
< report::XSection
> xContainer(evt
.Source
,uno::UNO_QUERY
);
376 ::std::vector
< uno::Reference
< container::XChild
> >::const_iterator aFind
= getSection(xContainer
.get());
378 if ( aFind
!= m_pImpl
->m_aSections
.end() )
380 OUndoEnvLock
aLock(*this);
383 OReportPage
* pPage
= m_pImpl
->m_rModel
.getPage(uno::Reference
< report::XSection
>(*aFind
,uno::UNO_QUERY
));
384 OSL_ENSURE(pPage
,"No page could be found for section!");
386 pPage
->insertObject(xReportComponent
);
388 catch(uno::Exception
&)
390 DBG_UNHANDLED_EXCEPTION();
397 uno::Reference
< report::XFunctions
> xContainer(evt
.Source
,uno::UNO_QUERY
);
398 if ( xContainer
.is() )
400 m_pImpl
->m_rModel
.GetSdrUndoManager()->AddUndoAction(
401 new OUndoContainerAction( m_pImpl
->m_rModel
, rptui::Inserted
, xContainer
.get(),
402 xIface
, RID_STR_UNDO_ADDFUNCTION
) );
413 void OXUndoEnvironment::implSetModified()
415 m_pImpl
->m_rModel
.SetModified( true );
419 void SAL_CALL
OXUndoEnvironment::elementReplaced(const ContainerEvent
& evt
) throw(uno::RuntimeException
, std::exception
)
421 SolarMutexGuard aSolarGuard
;
422 ::osl::MutexGuard
aGuard( m_pImpl
->m_aMutex
);
424 Reference
< XInterface
> xIface(evt
.ReplacedElement
,uno::UNO_QUERY
);
425 OSL_ENSURE(xIface
.is(), "OXUndoEnvironment::elementReplaced: invalid container notification!");
426 RemoveElement(xIface
);
428 xIface
.set(evt
.Element
,uno::UNO_QUERY
);
435 void SAL_CALL
OXUndoEnvironment::elementRemoved(const ContainerEvent
& evt
) throw(uno::RuntimeException
, std::exception
)
437 SolarMutexGuard aSolarGuard
;
438 ::osl::MutexGuard
aGuard( m_pImpl
->m_aMutex
);
440 Reference
< uno::XInterface
> xIface( evt
.Element
, UNO_QUERY
);
443 Reference
< report::XSection
> xContainer(evt
.Source
,uno::UNO_QUERY
);
444 ::std::vector
< uno::Reference
< container::XChild
> >::const_iterator aFind
= getSection(xContainer
.get());
446 Reference
< report::XReportComponent
> xReportComponent( xIface
, UNO_QUERY
);
447 if ( aFind
!= m_pImpl
->m_aSections
.end() && xReportComponent
.is() )
449 OXUndoEnvironment::OUndoEnvLock
aLock(*this);
452 OReportPage
* pPage
= m_pImpl
->m_rModel
.getPage(uno::Reference
< report::XSection
>( *aFind
, uno::UNO_QUERY_THROW
) );
453 OSL_ENSURE( pPage
, "OXUndoEnvironment::elementRemoved: no page for the section!" );
455 pPage
->removeSdrObject(xReportComponent
);
457 catch(const uno::Exception
&)
459 DBG_UNHANDLED_EXCEPTION();
464 uno::Reference
< report::XFunctions
> xFunctions(evt
.Source
,uno::UNO_QUERY
);
465 if ( xFunctions
.is() )
467 m_pImpl
->m_rModel
.GetSdrUndoManager()->AddUndoAction( new OUndoContainerAction(
468 m_pImpl
->m_rModel
, rptui::Removed
, xFunctions
.get(), xIface
, RID_STR_UNDO_ADDFUNCTION
) );
474 RemoveElement(xIface
);
480 void SAL_CALL
OXUndoEnvironment::modified( const EventObject
& /*aEvent*/ ) throw (RuntimeException
, std::exception
)
486 void OXUndoEnvironment::AddSection(const Reference
< report::XSection
> & _xSection
)
488 OUndoEnvLock
aLock(*this);
491 uno::Reference
<container::XChild
> xChild
= _xSection
.get();
492 uno::Reference
<report::XGroup
> xGroup(xChild
->getParent(),uno::UNO_QUERY
);
493 m_pImpl
->m_aSections
.push_back(xChild
);
494 Reference
< XInterface
> xInt(_xSection
);
497 catch(const uno::Exception
&)
499 DBG_UNHANDLED_EXCEPTION();
504 void OXUndoEnvironment::RemoveSection(const Reference
< report::XSection
> & _xSection
)
506 OUndoEnvLock
aLock(*this);
509 uno::Reference
<container::XChild
> xChild(_xSection
.get());
510 m_pImpl
->m_aSections
.erase(::std::remove(m_pImpl
->m_aSections
.begin(),m_pImpl
->m_aSections
.end(),
511 xChild
), m_pImpl
->m_aSections
.end());
512 Reference
< XInterface
> xInt(_xSection
);
515 catch(uno::Exception
&){}
519 void OXUndoEnvironment::TogglePropertyListening(const Reference
< XInterface
> & Element
)
521 // am Container horchen
522 Reference
< XIndexAccess
> xContainer(Element
, UNO_QUERY
);
525 Reference
< XInterface
> xInterface
;
526 sal_Int32 nCount
= xContainer
->getCount();
527 for(sal_Int32 i
= 0;i
!= nCount
;++i
)
529 xInterface
.set(xContainer
->getByIndex( i
),uno::UNO_QUERY
);
530 TogglePropertyListening(xInterface
);
534 Reference
< XPropertySet
> xSet(Element
, UNO_QUERY
);
537 if (!m_pImpl
->m_bReadOnly
)
538 xSet
->addPropertyChangeListener( OUString(), this );
540 xSet
->removePropertyChangeListener( OUString(), this );
546 void OXUndoEnvironment::switchListening( const Reference
< XIndexAccess
>& _rxContainer
, bool _bStartListening
)
548 OSL_PRECOND( _rxContainer
.is(), "OXUndoEnvironment::switchListening: invalid container!" );
549 if ( !_rxContainer
.is() )
554 // also handle all children of this element
555 Reference
< XInterface
> xInterface
;
556 sal_Int32 nCount
= _rxContainer
->getCount();
557 for(sal_Int32 i
= 0;i
!= nCount
;++i
)
559 xInterface
.set(_rxContainer
->getByIndex( i
),uno::UNO_QUERY
);
560 if ( _bStartListening
)
561 AddElement( xInterface
);
563 RemoveElement( xInterface
);
566 // be notified of any changes in the container elements
567 Reference
< XContainer
> xSimpleContainer( _rxContainer
, UNO_QUERY
);
568 if ( xSimpleContainer
.is() )
570 if ( _bStartListening
)
571 xSimpleContainer
->addContainerListener( this );
573 xSimpleContainer
->removeContainerListener( this );
576 catch( const Exception
& )
578 DBG_UNHANDLED_EXCEPTION();
583 void OXUndoEnvironment::switchListening( const Reference
< XInterface
>& _rxObject
, bool _bStartListening
)
585 OSL_PRECOND( _rxObject
.is(), "OXUndoEnvironment::switchListening: how should I listen at a NULL object?" );
589 if ( !m_pImpl
->m_bReadOnly
)
591 Reference
< XPropertySet
> xProps( _rxObject
, UNO_QUERY
);
594 if ( _bStartListening
)
595 xProps
->addPropertyChangeListener( OUString(), this );
597 xProps
->removePropertyChangeListener( OUString(), this );
601 Reference
< XModifyBroadcaster
> xBroadcaster( _rxObject
, UNO_QUERY
);
602 if ( xBroadcaster
.is() )
604 if ( _bStartListening
)
605 xBroadcaster
->addModifyListener( this );
607 xBroadcaster
->removeModifyListener( this );
610 catch( const Exception
& )
616 void OXUndoEnvironment::AddElement(const Reference
< XInterface
>& _rxElement
)
619 m_pImpl
->m_aFormatNormalizer
.notifyElementInserted( _rxElement
);
621 // if it's a container, start listening at all elements
622 Reference
< XIndexAccess
> xContainer( _rxElement
, UNO_QUERY
);
623 if ( xContainer
.is() )
624 switchListening( xContainer
, true );
626 switchListening( _rxElement
, true );
630 void OXUndoEnvironment::RemoveElement(const Reference
< XInterface
>& _rxElement
)
632 uno::Reference
<beans::XPropertySet
> xProp(_rxElement
,uno::UNO_QUERY
);
633 if (!m_pImpl
->m_aPropertySetCache
.empty())
634 m_pImpl
->m_aPropertySetCache
.erase(xProp
);
635 switchListening( _rxElement
, false );
637 Reference
< XIndexAccess
> xContainer( _rxElement
, UNO_QUERY
);
638 if ( xContainer
.is() )
639 switchListening( xContainer
, false );
642 void OXUndoEnvironment::SetUndoMode(bool _bUndo
)
644 m_pImpl
->m_bIsUndo
= _bUndo
;
647 bool OXUndoEnvironment::IsUndoMode() const
649 return m_pImpl
->m_bIsUndo
;
655 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */