Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 :
21 : #include "CommandDispatch.hxx"
22 : #include "CommonFunctors.hxx"
23 : #include "macros.hxx"
24 : #include <com/sun/star/util/URLTransformer.hpp>
25 :
26 : #include <algorithm>
27 : #include <functional>
28 :
29 : using namespace ::com::sun::star;
30 :
31 : using ::com::sun::star::uno::Reference;
32 : using ::com::sun::star::uno::Sequence;
33 : using ::rtl::OUString;
34 :
35 : namespace
36 : {
37 : template< class Map >
38 0 : struct lcl_DisposeAndClearAndDeleteMapElement :
39 : public ::std::unary_function< typename Map::value_type, void >
40 : {
41 0 : lcl_DisposeAndClearAndDeleteMapElement( const Reference< uno::XInterface > & xEventSource ) :
42 0 : m_aEvent( xEventSource )
43 0 : {}
44 0 : void operator() ( typename Map::value_type & rElement )
45 : {
46 0 : if( rElement.second )
47 : {
48 0 : rElement.second->disposeAndClear( m_aEvent );
49 0 : delete rElement.second;
50 : }
51 0 : }
52 : private:
53 : lang::EventObject m_aEvent;
54 : };
55 :
56 : template< class Map >
57 0 : void lcl_DisposeAndClearAndDeleteAllMapElements(
58 : Map & rMap,
59 : const Reference< uno::XInterface > & xEventSource )
60 : {
61 0 : ::std::for_each( rMap.begin(), rMap.end(),
62 : lcl_DisposeAndClearAndDeleteMapElement< Map >( xEventSource ));
63 0 : }
64 :
65 : } // anonymous namespace
66 :
67 : namespace chart
68 : {
69 :
70 0 : CommandDispatch::CommandDispatch(
71 : const Reference< uno::XComponentContext > & xContext ) :
72 : impl::CommandDispatch_Base( m_aMutex ),
73 0 : m_xContext( xContext )
74 : {
75 0 : }
76 :
77 0 : CommandDispatch::~CommandDispatch()
78 0 : {}
79 :
80 0 : void CommandDispatch::initialize()
81 0 : {}
82 :
83 : // ____ WeakComponentImplHelperBase ____
84 : /// is called when this is disposed
85 0 : void SAL_CALL CommandDispatch::disposing()
86 : {
87 0 : lcl_DisposeAndClearAndDeleteAllMapElements( m_aListeners, static_cast< cppu::OWeakObject* >( this ));
88 0 : m_aListeners.clear();
89 0 : }
90 :
91 : // ____ XDispatch ____
92 0 : void SAL_CALL CommandDispatch::dispatch( const util::URL& /* URL */, const Sequence< beans::PropertyValue >& /* Arguments */ )
93 : throw (uno::RuntimeException)
94 0 : {}
95 :
96 0 : void SAL_CALL CommandDispatch::addStatusListener( const Reference< frame::XStatusListener >& Control, const util::URL& URL )
97 : throw (uno::RuntimeException)
98 : {
99 0 : tListenerMap::iterator aIt( m_aListeners.find( URL.Complete ));
100 0 : if( aIt == m_aListeners.end())
101 : {
102 : aIt = m_aListeners.insert(
103 0 : m_aListeners.begin(),
104 0 : tListenerMap::value_type( URL.Complete, new ::cppu::OInterfaceContainerHelper( m_aMutex )));
105 : }
106 : OSL_ASSERT( aIt != m_aListeners.end());
107 :
108 0 : aIt->second->addInterface( Control );
109 0 : fireStatusEvent( URL.Complete, Control );
110 0 : }
111 :
112 0 : void SAL_CALL CommandDispatch::removeStatusListener( const Reference< frame::XStatusListener >& Control, const util::URL& URL )
113 : throw (uno::RuntimeException)
114 : {
115 0 : tListenerMap::iterator aIt( m_aListeners.find( URL.Complete ));
116 0 : if( aIt != m_aListeners.end())
117 0 : (*aIt).second->removeInterface( Control );
118 0 : }
119 :
120 : // ____ XModifyListener ____
121 0 : void SAL_CALL CommandDispatch::modified( const lang::EventObject& /* aEvent */ )
122 : throw (uno::RuntimeException)
123 : {
124 0 : fireAllStatusEvents( 0 );
125 0 : }
126 :
127 : // ____ XEventListener (base of XModifyListener) ____
128 0 : void SAL_CALL CommandDispatch::disposing( const lang::EventObject& /* Source */ )
129 : throw (uno::RuntimeException)
130 0 : {}
131 :
132 0 : void CommandDispatch::fireAllStatusEvents(
133 : const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XStatusListener > & xSingleListener )
134 : {
135 0 : fireStatusEvent( OUString(), xSingleListener );
136 0 : }
137 :
138 0 : void CommandDispatch::fireStatusEventForURL(
139 : const OUString & rURL,
140 : const uno::Any & rState,
141 : bool bEnabled,
142 : const Reference< frame::XStatusListener > & xSingleListener, /* = 0 */
143 : const OUString & rFeatureDescriptor /* = OUString() */ )
144 : {
145 : // prepare event to send
146 0 : util::URL aURL;
147 0 : aURL.Complete = rURL;
148 0 : if( !m_xURLTransformer.is())
149 : {
150 0 : m_xURLTransformer.set( util::URLTransformer::create(m_xContext) );
151 : }
152 0 : m_xURLTransformer->parseStrict( aURL );
153 :
154 : frame::FeatureStateEvent aEventToSend(
155 : static_cast< cppu::OWeakObject* >( this ), // Source
156 : aURL, // FeatureURL
157 : rFeatureDescriptor, // FeatureDescriptor
158 : bEnabled, // IsEnabled
159 : false, // Requery
160 : rState // State
161 0 : );
162 :
163 : // send event either to single listener or all registered ones
164 0 : if( xSingleListener.is())
165 0 : xSingleListener->statusChanged( aEventToSend );
166 : else
167 : {
168 0 : tListenerMap::iterator aIt( m_aListeners.find( aURL.Complete ));
169 0 : if( aIt != m_aListeners.end())
170 : {
171 0 : if( aIt->second )
172 : {
173 0 : ::cppu::OInterfaceIteratorHelper aIntfIt( *((*aIt).second) );
174 :
175 0 : while( aIntfIt.hasMoreElements())
176 : {
177 0 : Reference< frame::XStatusListener > xListener( aIntfIt.next(), uno::UNO_QUERY );
178 : try
179 : {
180 0 : if( xListener.is())
181 0 : xListener->statusChanged( aEventToSend );
182 : }
183 0 : catch( const uno::Exception & ex )
184 : {
185 : ASSERT_EXCEPTION( ex );
186 : }
187 0 : }
188 : }
189 : }
190 0 : }
191 0 : }
192 :
193 :
194 :
195 : } // namespace chart
196 :
197 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|