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 <sal/config.h>
24 #include <ReportControllerObserver.hxx>
25 #include <ReportController.hxx>
26 #include <osl/mutex.hxx>
27 #include <vcl/svapp.hxx>
28 #include <vcl/settings.hxx>
30 #include <FormattedFieldBeautifier.hxx>
32 // DBG_UNHANDLED_EXCEPTION
33 #include <tools/diagnose_ex.h>
38 using namespace ::com::sun::star
;
40 typedef std::map
<OUString
, bool> AllProperties
;
41 typedef std::map
<uno::Reference
< beans::XPropertySet
>, AllProperties
> PropertySetInfoCache
;
43 class OXReportControllerObserverImpl
46 ::std::vector
< uno::Reference
< container::XChild
> > m_aSections
;
47 ::osl::Mutex m_aMutex
;
48 oslInterlockedCount m_nLocks
;
50 explicit OXReportControllerObserverImpl();
51 OXReportControllerObserverImpl(const OXReportControllerObserverImpl
&) = delete;
52 OXReportControllerObserverImpl
& operator=(const OXReportControllerObserverImpl
&) = delete;
56 OXReportControllerObserverImpl::OXReportControllerObserverImpl()
62 OXReportControllerObserver::OXReportControllerObserver(const OReportController
& _rController
)
63 :m_pImpl(new OXReportControllerObserverImpl
)
64 ,m_aFormattedFieldBeautifier(_rController
)
65 ,m_aFixedTextColor(_rController
)
68 Application::AddEventListener(LINK( this, OXReportControllerObserver
, SettingsChanged
) );
71 OXReportControllerObserver::~OXReportControllerObserver()
73 Application::RemoveEventListener(LINK( this, OXReportControllerObserver
, SettingsChanged
) );
77 IMPL_LINK(OXReportControllerObserver
, SettingsChanged
, VclSimpleEvent
&, _rEvt
, void)
79 VclEventId nEvent
= _rEvt
.GetId();
81 if (nEvent
!= VclEventId::ApplicationDataChanged
)
84 DataChangedEvent
* pData
= static_cast<DataChangedEvent
*>(static_cast<VclWindowEvent
&>(_rEvt
).GetData());
85 if ( !(pData
&& ((( pData
->GetType() == DataChangedEventType::SETTINGS
) ||
86 ( pData
->GetType() == DataChangedEventType::DISPLAY
)) &&
87 ( pData
->GetFlags() & AllSettingsFlags::STYLE
))))
90 OEnvLock
aLock(*this);
92 // send all Section Objects a 'tingle'
93 // maybe they need a change in format, color, etc
94 for (const uno::Reference
<container::XChild
>& xChild
: m_pImpl
->m_aSections
)
98 uno::Reference
<report::XSection
> xSection(xChild
, uno::UNO_QUERY
);
101 const sal_Int32 nCount
= xSection
->getCount();
102 for (sal_Int32 i
= 0; i
< nCount
; ++i
)
104 const uno::Any aObj
= xSection
->getByIndex(i
);
105 uno::Reference
< report::XReportComponent
> xReportComponent(aObj
, uno::UNO_QUERY
);
106 if (xReportComponent
.is())
108 m_aFormattedFieldBeautifier
.handle(xReportComponent
);
109 m_aFixedTextColor
.handle(xReportComponent
);
118 void SAL_CALL
OXReportControllerObserver::disposing(const lang::EventObject
& e
)
120 // check if it's an object we have cached information about
121 uno::Reference
< beans::XPropertySet
> xSourceSet(e
.Source
, uno::UNO_QUERY
);
122 if ( xSourceSet
.is() )
124 uno::Reference
< report::XSection
> xSection(xSourceSet
,uno::UNO_QUERY
);
126 RemoveSection(xSection
);
128 RemoveElement(xSourceSet
);
132 void OXReportControllerObserver::Clear()
134 OEnvLock
aLock(*this);
135 m_pImpl
->m_aSections
.clear();
138 // XPropertyChangeListener
139 void SAL_CALL
OXReportControllerObserver::propertyChange(const beans::PropertyChangeEvent
& _rEvent
)
141 osl::MutexGuard
aGuard( m_pImpl
->m_aMutex
);
143 if ( m_pImpl
->m_nLocks
!= 0 )
146 m_aFormattedFieldBeautifier
.notifyPropertyChange(_rEvent
);
147 m_aFixedTextColor
.notifyPropertyChange(_rEvent
);
151 void OXReportControllerObserver::Lock()
153 OSL_ENSURE(m_refCount
,"Illegal call to dead object!");
154 osl_atomic_increment( &m_pImpl
->m_nLocks
);
157 void OXReportControllerObserver::UnLock()
159 OSL_ENSURE(m_refCount
,"Illegal call to dead object!");
161 osl_atomic_decrement( &m_pImpl
->m_nLocks
);
164 void OXReportControllerObserver::AddSection(const uno::Reference
< report::XSection
> & _xSection
)
166 OEnvLock
aLock(*this);
169 uno::Reference
<container::XChild
> xChild
= _xSection
;
170 m_pImpl
->m_aSections
.push_back(xChild
);
171 uno::Reference
< uno::XInterface
> xInt(_xSection
);
174 catch(const uno::Exception
&)
176 DBG_UNHANDLED_EXCEPTION("reportdesign");
181 void OXReportControllerObserver::RemoveSection(const uno::Reference
< report::XSection
> & _xSection
)
183 OEnvLock
aLock(*this);
186 uno::Reference
<container::XChild
> xChild(_xSection
);
187 m_pImpl
->m_aSections
.erase(::std::remove(m_pImpl
->m_aSections
.begin(),m_pImpl
->m_aSections
.end(),
188 xChild
), m_pImpl
->m_aSections
.end());
189 uno::Reference
< uno::XInterface
> xInt(_xSection
);
192 catch(uno::Exception
&)
194 DBG_UNHANDLED_EXCEPTION("reportdesign");
199 void OXReportControllerObserver::switchListening( const uno::Reference
< container::XIndexAccess
>& _rxContainer
, bool _bStartListening
)
201 OSL_PRECOND( _rxContainer
.is(), "OXReportControllerObserver::switchListening: invalid container!" );
202 if ( !_rxContainer
.is() )
207 // also handle all children of this element
208 uno::Reference
< uno::XInterface
> xInterface
;
209 sal_Int32 nCount
= _rxContainer
->getCount();
210 for(sal_Int32 i
= 0;i
!= nCount
;++i
)
212 xInterface
.set(_rxContainer
->getByIndex( i
),uno::UNO_QUERY
);
213 if ( _bStartListening
)
214 AddElement( xInterface
);
216 RemoveElement( xInterface
);
219 // be notified of any changes in the container elements
220 uno::Reference
< container::XContainer
> xSimpleContainer( _rxContainer
, uno::UNO_QUERY
);
221 if ( xSimpleContainer
.is() )
223 if ( _bStartListening
)
224 xSimpleContainer
->addContainerListener( this );
226 xSimpleContainer
->removeContainerListener( this );
229 catch( const uno::Exception
& )
231 DBG_UNHANDLED_EXCEPTION("reportdesign");
236 void OXReportControllerObserver::switchListening( const uno::Reference
< uno::XInterface
>& _rxObject
, bool _bStartListening
)
238 OSL_PRECOND( _rxObject
.is(), "OXReportControllerObserver::switchListening: how should I listen at a NULL object?" );
242 uno::Reference
< beans::XPropertySet
> xProps( _rxObject
, uno::UNO_QUERY
);
245 if ( _bStartListening
)
246 xProps
->addPropertyChangeListener( OUString(), this );
248 xProps
->removePropertyChangeListener( OUString(), this );
251 uno::Reference
< util::XModifyBroadcaster
> xBroadcaster( _rxObject
, uno::UNO_QUERY
);
252 if ( xBroadcaster
.is() )
254 if ( _bStartListening
)
255 xBroadcaster
->addModifyListener( this );
257 xBroadcaster
->removeModifyListener( this );
260 catch( const uno::Exception
& )
262 DBG_UNHANDLED_EXCEPTION("reportdesign");
267 void SAL_CALL
OXReportControllerObserver::modified( const lang::EventObject
& /*aEvent*/ )
272 void OXReportControllerObserver::AddElement(const uno::Reference
< uno::XInterface
>& _rxElement
)
274 m_aFormattedFieldBeautifier
.notifyElementInserted(_rxElement
);
275 m_aFixedTextColor
.notifyElementInserted(_rxElement
);
277 // if it's a container, start listening at all elements
278 uno::Reference
< container::XIndexAccess
> xContainer( _rxElement
, uno::UNO_QUERY
);
279 if ( xContainer
.is() )
280 switchListening( xContainer
, true );
282 switchListening( _rxElement
, true );
286 void OXReportControllerObserver::RemoveElement(const uno::Reference
< uno::XInterface
>& _rxElement
)
288 switchListening( _rxElement
, false );
290 uno::Reference
< container::XIndexAccess
> xContainer( _rxElement
, uno::UNO_QUERY
);
291 if ( xContainer
.is() )
292 switchListening( xContainer
, false );
296 // XContainerListener
298 void SAL_CALL
OXReportControllerObserver::elementInserted(const container::ContainerEvent
& evt
)
300 SolarMutexGuard aSolarGuard
;
301 ::osl::MutexGuard
aGuard( m_pImpl
->m_aMutex
);
303 // new listener object
304 uno::Reference
< uno::XInterface
> xIface( evt
.Element
, uno::UNO_QUERY
);
312 void SAL_CALL
OXReportControllerObserver::elementReplaced(const container::ContainerEvent
& evt
)
314 SolarMutexGuard aSolarGuard
;
315 ::osl::MutexGuard
aGuard( m_pImpl
->m_aMutex
);
317 uno::Reference
< uno::XInterface
> xIface(evt
.ReplacedElement
,uno::UNO_QUERY
);
318 OSL_ENSURE(xIface
.is(), "OXReportControllerObserver::elementReplaced: invalid container notification!");
319 RemoveElement(xIface
);
321 xIface
.set(evt
.Element
,uno::UNO_QUERY
);
326 void SAL_CALL
OXReportControllerObserver::elementRemoved(const container::ContainerEvent
& evt
)
328 SolarMutexGuard aSolarGuard
;
329 ::osl::MutexGuard
aGuard( m_pImpl
->m_aMutex
);
331 uno::Reference
< uno::XInterface
> xIface( evt
.Element
, uno::UNO_QUERY
);
334 RemoveElement(xIface
);
342 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */