1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: CommandDispatch.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_chart2.hxx"
34 #include "CommandDispatch.hxx"
35 #include "CommonFunctors.hxx"
41 using namespace ::com::sun::star
;
43 using ::com::sun::star::uno::Reference
;
44 using ::com::sun::star::uno::Sequence
;
45 using ::rtl::OUString
;
50 struct lcl_DisposeAndClearAndDeleteMapElement
:
51 public ::std::unary_function
< typename
Map::value_type
, void >
53 lcl_DisposeAndClearAndDeleteMapElement( const Reference
< uno::XInterface
> & xEventSource
) :
54 m_aEvent( xEventSource
)
56 void operator() ( typename
Map::value_type
& rElement
)
60 rElement
.second
->disposeAndClear( m_aEvent
);
61 delete rElement
.second
;
65 lang::EventObject m_aEvent
;
69 void lcl_DisposeAndClearAndDeleteAllMapElements(
71 const Reference
< uno::XInterface
> & xEventSource
)
73 ::std::for_each( rMap
.begin(), rMap
.end(),
74 lcl_DisposeAndClearAndDeleteMapElement
< Map
>( xEventSource
));
77 } // anonymous namespace
82 CommandDispatch::CommandDispatch(
83 const Reference
< uno::XComponentContext
> & xContext
) :
84 impl::CommandDispatch_Base( m_aMutex
),
85 m_xContext( xContext
)
89 CommandDispatch::~CommandDispatch()
92 void CommandDispatch::initialize()
95 // ____ WeakComponentImplHelperBase ____
96 /// is called when this is disposed
97 void SAL_CALL
CommandDispatch::disposing()
99 lcl_DisposeAndClearAndDeleteAllMapElements( m_aListeners
, static_cast< cppu::OWeakObject
* >( this ));
100 m_aListeners
.clear();
103 // ____ XDispatch ____
104 void SAL_CALL
CommandDispatch::dispatch( const util::URL
& /* URL */, const Sequence
< beans::PropertyValue
>& /* Arguments */ )
105 throw (uno::RuntimeException
)
108 void SAL_CALL
CommandDispatch::addStatusListener( const Reference
< frame::XStatusListener
>& Control
, const util::URL
& URL
)
109 throw (uno::RuntimeException
)
111 tListenerMap::iterator
aIt( m_aListeners
.find( URL
.Complete
));
112 if( aIt
== m_aListeners
.end())
114 aIt
= m_aListeners
.insert(
115 m_aListeners
.begin(),
116 tListenerMap::value_type( URL
.Complete
, new ::cppu::OInterfaceContainerHelper( m_aMutex
)));
118 OSL_ASSERT( aIt
!= m_aListeners
.end());
120 aIt
->second
->addInterface( Control
);
121 fireStatusEvent( URL
.Complete
, Control
);
124 void SAL_CALL
CommandDispatch::removeStatusListener( const Reference
< frame::XStatusListener
>& Control
, const util::URL
& URL
)
125 throw (uno::RuntimeException
)
127 tListenerMap::iterator
aIt( m_aListeners
.find( URL
.Complete
));
128 if( aIt
!= m_aListeners
.end())
129 (*aIt
).second
->removeInterface( Control
);
132 // ____ XModifyListener ____
133 void SAL_CALL
CommandDispatch::modified( const lang::EventObject
& /* aEvent */ )
134 throw (uno::RuntimeException
)
136 fireAllStatusEvents( 0 );
139 // ____ XEventListener (base of XModifyListener) ____
140 void SAL_CALL
CommandDispatch::disposing( const lang::EventObject
& /* Source */ )
141 throw (uno::RuntimeException
)
144 void CommandDispatch::fireAllStatusEvents(
145 const ::com::sun::star::uno::Reference
< ::com::sun::star::frame::XStatusListener
> & xSingleListener
)
147 fireStatusEvent( OUString(), xSingleListener
);
150 void CommandDispatch::fireStatusEventForURL(
151 const OUString
& rURL
,
152 const uno::Any
& rState
,
154 const Reference
< frame::XStatusListener
> & xSingleListener
, /* = 0 */
155 const OUString
& rFeatureDescriptor
/* = OUString() */ )
157 // prepare event to send
159 aURL
.Complete
= rURL
;
160 if( !m_xURLTransformer
.is())
162 m_xURLTransformer
.set(
163 m_xContext
->getServiceManager()->createInstanceWithContext(
164 C2U( "com.sun.star.util.URLTransformer" ),
168 if( m_xURLTransformer
.is())
169 m_xURLTransformer
->parseStrict( aURL
);
171 frame::FeatureStateEvent
aEventToSend(
172 static_cast< cppu::OWeakObject
* >( this ), // Source
174 rFeatureDescriptor
, // FeatureDescriptor
175 bEnabled
, // IsEnabled
180 // send event either to single listener or all registered ones
181 if( xSingleListener
.is())
182 xSingleListener
->statusChanged( aEventToSend
);
185 tListenerMap::iterator
aIt( m_aListeners
.find( aURL
.Complete
));
186 if( aIt
!= m_aListeners
.end())
188 // ::cppu::OInterfaceContainerHelper * pCntHlp = rBHelper.getContainer(
189 // ::getCppuType( reinterpret_cast< Reference< frame::XStatusListener > * >(0)));
192 ::cppu::OInterfaceIteratorHelper
aIntfIt( *((*aIt
).second
) );
194 while( aIntfIt
.hasMoreElements())
196 Reference
< frame::XStatusListener
> xListener( aIntfIt
.next(), uno::UNO_QUERY
);
200 xListener
->statusChanged( aEventToSend
);
202 catch( const uno::Exception
& ex
)
204 ASSERT_EXCEPTION( ex
);