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 "CommandDispatch.hxx"
21 #include "CommonFunctors.hxx"
23 #include <com/sun/star/util/URLTransformer.hpp>
28 using namespace ::com::sun::star
;
30 using ::com::sun::star::uno::Reference
;
31 using ::com::sun::star::uno::Sequence
;
36 struct lcl_DisposeAndClearAndDeleteMapElement
:
37 public ::std::unary_function
< typename
Map::value_type
, void >
39 lcl_DisposeAndClearAndDeleteMapElement( const Reference
< uno::XInterface
> & xEventSource
) :
40 m_aEvent( xEventSource
)
42 void operator() ( typename
Map::value_type
& rElement
)
46 rElement
.second
->disposeAndClear( m_aEvent
);
47 delete rElement
.second
;
51 lang::EventObject m_aEvent
;
55 void lcl_DisposeAndClearAndDeleteAllMapElements(
57 const Reference
< uno::XInterface
> & xEventSource
)
59 ::std::for_each( rMap
.begin(), rMap
.end(),
60 lcl_DisposeAndClearAndDeleteMapElement
< Map
>( xEventSource
));
63 } // anonymous namespace
68 CommandDispatch::CommandDispatch(
69 const Reference
< uno::XComponentContext
> & xContext
) :
70 impl::CommandDispatch_Base( m_aMutex
),
71 m_xContext( xContext
)
75 CommandDispatch::~CommandDispatch()
78 void CommandDispatch::initialize()
81 // ____ WeakComponentImplHelperBase ____
82 /// is called when this is disposed
83 void SAL_CALL
CommandDispatch::disposing()
85 lcl_DisposeAndClearAndDeleteAllMapElements( m_aListeners
, static_cast< cppu::OWeakObject
* >( this ));
89 // ____ XDispatch ____
90 void SAL_CALL
CommandDispatch::dispatch( const util::URL
& /* URL */, const Sequence
< beans::PropertyValue
>& /* Arguments */ )
91 throw (uno::RuntimeException
, std::exception
)
94 void SAL_CALL
CommandDispatch::addStatusListener( const Reference
< frame::XStatusListener
>& Control
, const util::URL
& URL
)
95 throw (uno::RuntimeException
, std::exception
)
97 tListenerMap::iterator
aIt( m_aListeners
.find( URL
.Complete
));
98 if( aIt
== m_aListeners
.end())
100 aIt
= m_aListeners
.insert(
101 m_aListeners
.begin(),
102 tListenerMap::value_type( URL
.Complete
, new ::cppu::OInterfaceContainerHelper( m_aMutex
)));
104 OSL_ASSERT( aIt
!= m_aListeners
.end());
106 aIt
->second
->addInterface( Control
);
107 fireStatusEvent( URL
.Complete
, Control
);
110 void SAL_CALL
CommandDispatch::removeStatusListener( const Reference
< frame::XStatusListener
>& Control
, const util::URL
& URL
)
111 throw (uno::RuntimeException
, std::exception
)
113 tListenerMap::iterator
aIt( m_aListeners
.find( URL
.Complete
));
114 if( aIt
!= m_aListeners
.end())
115 (*aIt
).second
->removeInterface( Control
);
118 // ____ XModifyListener ____
119 void SAL_CALL
CommandDispatch::modified( const lang::EventObject
& /* aEvent */ )
120 throw (uno::RuntimeException
, std::exception
)
122 fireAllStatusEvents( 0 );
125 // ____ XEventListener (base of XModifyListener) ____
126 void SAL_CALL
CommandDispatch::disposing( const lang::EventObject
& /* Source */ )
127 throw (uno::RuntimeException
, std::exception
)
130 void CommandDispatch::fireAllStatusEvents(
131 const ::com::sun::star::uno::Reference
< ::com::sun::star::frame::XStatusListener
> & xSingleListener
)
133 fireStatusEvent( OUString(), xSingleListener
);
136 void CommandDispatch::fireStatusEventForURL(
137 const OUString
& rURL
,
138 const uno::Any
& rState
,
140 const Reference
< frame::XStatusListener
> & xSingleListener
, /* = 0 */
141 const OUString
& rFeatureDescriptor
/* = OUString() */ )
143 // prepare event to send
145 aURL
.Complete
= rURL
;
146 if( !m_xURLTransformer
.is())
148 m_xURLTransformer
.set( util::URLTransformer::create(m_xContext
) );
150 m_xURLTransformer
->parseStrict( aURL
);
152 frame::FeatureStateEvent
aEventToSend(
153 static_cast< cppu::OWeakObject
* >( this ), // Source
155 rFeatureDescriptor
, // FeatureDescriptor
156 bEnabled
, // IsEnabled
161 // send event either to single listener or all registered ones
162 if( xSingleListener
.is())
163 xSingleListener
->statusChanged( aEventToSend
);
166 tListenerMap::iterator
aIt( m_aListeners
.find( aURL
.Complete
));
167 if( aIt
!= m_aListeners
.end())
171 ::cppu::OInterfaceIteratorHelper
aIntfIt( *((*aIt
).second
) );
173 while( aIntfIt
.hasMoreElements())
175 Reference
< frame::XStatusListener
> xListener( aIntfIt
.next(), uno::UNO_QUERY
);
179 xListener
->statusChanged( aEventToSend
);
181 catch( const uno::Exception
& ex
)
183 ASSERT_EXCEPTION( ex
);
193 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */