bump product version to 5.0.4.1
[LibreOffice.git] / reportdesign / source / core / sdr / UndoEnv.cxx
blobbb9287f690a06cf9125f78892ef93f8bc5200880
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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"
25 #include "RptDef.hxx"
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>
51 namespace rptui
53 using namespace ::com::sun::star;
54 using namespace uno;
55 using namespace lang;
56 using namespace script;
57 using namespace beans;
58 using namespace awt;
59 using namespace util;
60 using namespace container;
61 using namespace report;
65 struct PropertyInfo
67 bool bIsReadonlyOrTransient;
69 PropertyInfo( const bool i_bIsTransientOrReadOnly )
70 :bIsReadonlyOrTransient( i_bIsTransientOrReadOnly )
75 typedef std::unordered_map< OUString, PropertyInfo, OUStringHash > PropertiesInfo;
77 struct ObjectInfo
79 PropertiesInfo aProperties;
80 Reference< XPropertySet > xPropertyIntrospection;
82 ObjectInfo()
83 :aProperties()
84 ,xPropertyIntrospection()
89 typedef ::std::map< Reference< XPropertySet >, ObjectInfo, ::comphelper::OInterfaceCompare< XPropertySet > > PropertySetInfoCache;
93 class OXUndoEnvironmentImpl: private boost::noncopyable
95 public:
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;
104 bool m_bReadOnly;
105 bool m_bIsUndo;
107 OXUndoEnvironmentImpl(OReportModel& _rModel);
110 OXUndoEnvironmentImpl::OXUndoEnvironmentImpl(OReportModel& _rModel) : m_rModel(_rModel)
111 ,m_aFormatNormalizer( _rModel )
112 ,m_aConditionUpdater()
113 ,m_nLocks(0)
114 ,m_bReadOnly(false)
115 ,m_bIsUndo(false)
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)
147 if ( _pPage )
149 Reference< XInterface > xSection(_pPage->getSection());
150 if ( xSection.is() )
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();
162 sal_uInt16 i;
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);
189 else
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 )
198 ModeChanged();
201 // XEventListener
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);
210 if ( xSection.is() )
211 RemoveSection(xSection);
212 else
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 );
224 if ( IsLocked() )
225 return;
227 Reference< XPropertySet > xSet( _rEvent.Source, UNO_QUERY );
228 if (!xSet.is())
229 return;
231 dbaui::DBSubComponentController* pController = m_pImpl->m_rModel.getController();
232 if ( !pController )
233 return;
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(
241 xSet, ObjectInfo()
242 ) ).first;
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() )
246 return;
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 ....
254 // the attributes
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;
263 else
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 ) ),
275 UNO_SET_THROW
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 )
298 ) ).first;
299 DBG_ASSERT(aPropertyPos != rObjectInfo.aProperties.end(), "OXUndoEnvironment::propertyChange : just inserted it ... why it's not there ?");
302 implSetModified();
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 )
309 return;
311 // give components with sub responsibilities a chance
312 m_pImpl->m_aFormatNormalizer.notifyPropertyChange( _rEvent );
313 m_pImpl->m_aConditionUpdater.notifyPropertyChange( _rEvent );
315 aGuard.clear();
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 );
324 if ( xSection.is() )
326 uno::Reference< report::XGroup> xGroup = xSection->getGroup();
327 if ( xGroup.is() )
328 pUndo = new OUndoPropertyGroupSectionAction( m_pImpl->m_rModel, _rEvent, OGroupHelper::getMemberFunction( xSection ), xGroup );
329 else
330 pUndo = new OUndoPropertyReportSectionAction( m_pImpl->m_rModel, _rEvent, OReportHelper::getMemberFunction( xSection ), xSection->getReportDefinition() );
333 catch(const Exception&)
335 DBG_UNHANDLED_EXCEPTION();
338 if ( pUndo == NULL )
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);
358 return aFind;
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 );
369 if ( !IsLocked() )
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!");
385 if ( pPage )
386 pPage->insertObject(xReportComponent);
388 catch(uno::Exception&)
390 DBG_UNHANDLED_EXCEPTION();
395 else
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 ) );
407 AddElement(xIface);
409 implSetModified();
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);
429 AddElement(xIface);
431 implSetModified();
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 );
441 if ( !IsLocked() )
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!" );
454 if ( pPage )
455 pPage->removeSdrObject(xReportComponent);
457 catch(const uno::Exception&)
459 DBG_UNHANDLED_EXCEPTION();
462 else
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 ) );
473 if ( xIface.is() )
474 RemoveElement(xIface);
476 implSetModified();
480 void SAL_CALL OXUndoEnvironment::modified( const EventObject& /*aEvent*/ ) throw (RuntimeException, std::exception)
482 implSetModified();
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);
495 AddElement(xInt);
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);
513 RemoveElement(xInt);
515 catch(uno::Exception&){}
519 void OXUndoEnvironment::TogglePropertyListening(const Reference< XInterface > & Element)
521 // am Container horchen
522 Reference< XIndexAccess > xContainer(Element, UNO_QUERY);
523 if (xContainer.is())
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);
535 if (xSet.is())
537 if (!m_pImpl->m_bReadOnly)
538 xSet->addPropertyChangeListener( OUString(), this );
539 else
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() )
550 return;
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 );
562 else
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 );
572 else
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 );
592 if ( xProps.is() )
594 if ( _bStartListening )
595 xProps->addPropertyChangeListener( OUString(), this );
596 else
597 xProps->removePropertyChangeListener( OUString(), this );
601 Reference< XModifyBroadcaster > xBroadcaster( _rxObject, UNO_QUERY );
602 if ( xBroadcaster.is() )
604 if ( _bStartListening )
605 xBroadcaster->addModifyListener( this );
606 else
607 xBroadcaster->removeModifyListener( this );
610 catch( const Exception& )
616 void OXUndoEnvironment::AddElement(const Reference< XInterface >& _rxElement )
618 if ( !IsLocked() )
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;
652 } // rptui
655 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */