1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #include "CommandDispatch.hxx"
31 #include "CommonFunctors.hxx"
37 using namespace ::com::sun::star
;
39 using ::com::sun::star::uno::Reference
;
40 using ::com::sun::star::uno::Sequence
;
41 using ::rtl::OUString
;
46 struct lcl_DisposeAndClearAndDeleteMapElement
:
47 public ::std::unary_function
< typename
Map::value_type
, void >
49 lcl_DisposeAndClearAndDeleteMapElement( const Reference
< uno::XInterface
> & xEventSource
) :
50 m_aEvent( xEventSource
)
52 void operator() ( typename
Map::value_type
& rElement
)
56 rElement
.second
->disposeAndClear( m_aEvent
);
57 delete rElement
.second
;
61 lang::EventObject m_aEvent
;
65 void lcl_DisposeAndClearAndDeleteAllMapElements(
67 const Reference
< uno::XInterface
> & xEventSource
)
69 ::std::for_each( rMap
.begin(), rMap
.end(),
70 lcl_DisposeAndClearAndDeleteMapElement
< Map
>( xEventSource
));
73 } // anonymous namespace
78 CommandDispatch::CommandDispatch(
79 const Reference
< uno::XComponentContext
> & xContext
) :
80 impl::CommandDispatch_Base( m_aMutex
),
81 m_xContext( xContext
)
85 CommandDispatch::~CommandDispatch()
88 void CommandDispatch::initialize()
91 // ____ WeakComponentImplHelperBase ____
92 /// is called when this is disposed
93 void SAL_CALL
CommandDispatch::disposing()
95 lcl_DisposeAndClearAndDeleteAllMapElements( m_aListeners
, static_cast< cppu::OWeakObject
* >( this ));
99 // ____ XDispatch ____
100 void SAL_CALL
CommandDispatch::dispatch( const util::URL
& /* URL */, const Sequence
< beans::PropertyValue
>& /* Arguments */ )
101 throw (uno::RuntimeException
)
104 void SAL_CALL
CommandDispatch::addStatusListener( const Reference
< frame::XStatusListener
>& Control
, const util::URL
& URL
)
105 throw (uno::RuntimeException
)
107 tListenerMap::iterator
aIt( m_aListeners
.find( URL
.Complete
));
108 if( aIt
== m_aListeners
.end())
110 aIt
= m_aListeners
.insert(
111 m_aListeners
.begin(),
112 tListenerMap::value_type( URL
.Complete
, new ::cppu::OInterfaceContainerHelper( m_aMutex
)));
114 OSL_ASSERT( aIt
!= m_aListeners
.end());
116 aIt
->second
->addInterface( Control
);
117 fireStatusEvent( URL
.Complete
, Control
);
120 void SAL_CALL
CommandDispatch::removeStatusListener( const Reference
< frame::XStatusListener
>& Control
, const util::URL
& URL
)
121 throw (uno::RuntimeException
)
123 tListenerMap::iterator
aIt( m_aListeners
.find( URL
.Complete
));
124 if( aIt
!= m_aListeners
.end())
125 (*aIt
).second
->removeInterface( Control
);
128 // ____ XModifyListener ____
129 void SAL_CALL
CommandDispatch::modified( const lang::EventObject
& /* aEvent */ )
130 throw (uno::RuntimeException
)
132 fireAllStatusEvents( 0 );
135 // ____ XEventListener (base of XModifyListener) ____
136 void SAL_CALL
CommandDispatch::disposing( const lang::EventObject
& /* Source */ )
137 throw (uno::RuntimeException
)
140 void CommandDispatch::fireAllStatusEvents(
141 const ::com::sun::star::uno::Reference
< ::com::sun::star::frame::XStatusListener
> & xSingleListener
)
143 fireStatusEvent( OUString(), xSingleListener
);
146 void CommandDispatch::fireStatusEventForURL(
147 const OUString
& rURL
,
148 const uno::Any
& rState
,
150 const Reference
< frame::XStatusListener
> & xSingleListener
, /* = 0 */
151 const OUString
& rFeatureDescriptor
/* = OUString() */ )
153 // prepare event to send
155 aURL
.Complete
= rURL
;
156 if( !m_xURLTransformer
.is())
158 m_xURLTransformer
.set(
159 m_xContext
->getServiceManager()->createInstanceWithContext(
160 C2U( "com.sun.star.util.URLTransformer" ),
164 if( m_xURLTransformer
.is())
165 m_xURLTransformer
->parseStrict( aURL
);
167 frame::FeatureStateEvent
aEventToSend(
168 static_cast< cppu::OWeakObject
* >( this ), // Source
170 rFeatureDescriptor
, // FeatureDescriptor
171 bEnabled
, // IsEnabled
176 // send event either to single listener or all registered ones
177 if( xSingleListener
.is())
178 xSingleListener
->statusChanged( aEventToSend
);
181 tListenerMap::iterator
aIt( m_aListeners
.find( aURL
.Complete
));
182 if( aIt
!= m_aListeners
.end())
186 ::cppu::OInterfaceIteratorHelper
aIntfIt( *((*aIt
).second
) );
188 while( aIntfIt
.hasMoreElements())
190 Reference
< frame::XStatusListener
> xListener( aIntfIt
.next(), uno::UNO_QUERY
);
194 xListener
->statusChanged( aEventToSend
);
196 catch( const uno::Exception
& ex
)
198 ASSERT_EXCEPTION( ex
);
210 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */