1*b1cdbd2cSJim Jagielski /**************************************************************
2*b1cdbd2cSJim Jagielski *
3*b1cdbd2cSJim Jagielski * Licensed to the Apache Software Foundation (ASF) under one
4*b1cdbd2cSJim Jagielski * or more contributor license agreements. See the NOTICE file
5*b1cdbd2cSJim Jagielski * distributed with this work for additional information
6*b1cdbd2cSJim Jagielski * regarding copyright ownership. The ASF licenses this file
7*b1cdbd2cSJim Jagielski * to you under the Apache License, Version 2.0 (the
8*b1cdbd2cSJim Jagielski * "License"); you may not use this file except in compliance
9*b1cdbd2cSJim Jagielski * with the License. You may obtain a copy of the License at
10*b1cdbd2cSJim Jagielski *
11*b1cdbd2cSJim Jagielski * http://www.apache.org/licenses/LICENSE-2.0
12*b1cdbd2cSJim Jagielski *
13*b1cdbd2cSJim Jagielski * Unless required by applicable law or agreed to in writing,
14*b1cdbd2cSJim Jagielski * software distributed under the License is distributed on an
15*b1cdbd2cSJim Jagielski * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b1cdbd2cSJim Jagielski * KIND, either express or implied. See the License for the
17*b1cdbd2cSJim Jagielski * specific language governing permissions and limitations
18*b1cdbd2cSJim Jagielski * under the License.
19*b1cdbd2cSJim Jagielski *
20*b1cdbd2cSJim Jagielski *************************************************************/
21*b1cdbd2cSJim Jagielski
22*b1cdbd2cSJim Jagielski #include "precompiled_framework.hxx"
23*b1cdbd2cSJim Jagielski
24*b1cdbd2cSJim Jagielski #include "services/EventMultiplexer.hxx"
25*b1cdbd2cSJim Jagielski #include "services.h"
26*b1cdbd2cSJim Jagielski
27*b1cdbd2cSJim Jagielski using ::rtl::OUString;
28*b1cdbd2cSJim Jagielski
29*b1cdbd2cSJim Jagielski #define A2S(s) ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(s))
30*b1cdbd2cSJim Jagielski
31*b1cdbd2cSJim Jagielski namespace framework {
32*b1cdbd2cSJim Jagielski
33*b1cdbd2cSJim Jagielski #define IMPLEMENTATION_NAME "org.apache.openoffice.comp.framework.EventMultiplexer"
34*b1cdbd2cSJim Jagielski #define SERVICE_NAME "com.sun.star.ui.ContextChangeEventMultiplexer"
35*b1cdbd2cSJim Jagielski #define SINGLETON_NAME "org.apache.openoffice.comp.framework.EventMultiplexer"
36*b1cdbd2cSJim Jagielski
37*b1cdbd2cSJim Jagielski
EventMultiplexer(const cssu::Reference<css::uno::XComponentContext> & rxContext)38*b1cdbd2cSJim Jagielski EventMultiplexer::EventMultiplexer (const cssu::Reference<css::uno::XComponentContext>& rxContext)
39*b1cdbd2cSJim Jagielski : EventMultiplexerInterfaceBase(m_aMutex),
40*b1cdbd2cSJim Jagielski maListeners()
41*b1cdbd2cSJim Jagielski {
42*b1cdbd2cSJim Jagielski (void)rxContext;
43*b1cdbd2cSJim Jagielski }
44*b1cdbd2cSJim Jagielski
45*b1cdbd2cSJim Jagielski
46*b1cdbd2cSJim Jagielski
47*b1cdbd2cSJim Jagielski
~EventMultiplexer(void)48*b1cdbd2cSJim Jagielski EventMultiplexer::~EventMultiplexer (void)
49*b1cdbd2cSJim Jagielski {
50*b1cdbd2cSJim Jagielski maListeners.clear();
51*b1cdbd2cSJim Jagielski }
52*b1cdbd2cSJim Jagielski
53*b1cdbd2cSJim Jagielski
54*b1cdbd2cSJim Jagielski
55*b1cdbd2cSJim Jagielski
56*b1cdbd2cSJim Jagielski // XEventMultiplexer
57*b1cdbd2cSJim Jagielski
addEventListener(const cssu::Reference<css::util::XEventListener> & rxListener,const cssu::Reference<cssu::XInterface> & rxEventFocus)58*b1cdbd2cSJim Jagielski void SAL_CALL EventMultiplexer::addEventListener (
59*b1cdbd2cSJim Jagielski const cssu::Reference<css::util::XEventListener>& rxListener,
60*b1cdbd2cSJim Jagielski const cssu::Reference<cssu::XInterface>& rxEventFocus)
61*b1cdbd2cSJim Jagielski throw(cssu::RuntimeException,cssl::IllegalArgumentException)
62*b1cdbd2cSJim Jagielski {
63*b1cdbd2cSJim Jagielski if ( ! rxListener.is())
64*b1cdbd2cSJim Jagielski throw css::lang::IllegalArgumentException(A2S("can not add an empty reference"), static_cast<XWeak*>(this), 0);
65*b1cdbd2cSJim Jagielski
66*b1cdbd2cSJim Jagielski ListenerMap::iterator iListenerContainer (maListeners.find(rxEventFocus));
67*b1cdbd2cSJim Jagielski if (iListenerContainer == maListeners.end())
68*b1cdbd2cSJim Jagielski {
69*b1cdbd2cSJim Jagielski // Create a new listener container for the event focus.
70*b1cdbd2cSJim Jagielski iListenerContainer = maListeners.insert(
71*b1cdbd2cSJim Jagielski ListenerMap::value_type(
72*b1cdbd2cSJim Jagielski rxEventFocus,
73*b1cdbd2cSJim Jagielski ListenerContainer())).first;
74*b1cdbd2cSJim Jagielski }
75*b1cdbd2cSJim Jagielski if (iListenerContainer != maListeners.end())
76*b1cdbd2cSJim Jagielski {
77*b1cdbd2cSJim Jagielski ListenerContainer& rContainer (iListenerContainer->second);
78*b1cdbd2cSJim Jagielski if (::std::find(rContainer.begin(), rContainer.end(), rxListener) == rContainer.end())
79*b1cdbd2cSJim Jagielski rContainer.push_back(rxListener);
80*b1cdbd2cSJim Jagielski else
81*b1cdbd2cSJim Jagielski {
82*b1cdbd2cSJim Jagielski // The listener was added for the same event focus
83*b1cdbd2cSJim Jagielski // previously. That is an error.
84*b1cdbd2cSJim Jagielski throw cssl::IllegalArgumentException(A2S("listener added twice"), static_cast<XWeak*>(this), 0);
85*b1cdbd2cSJim Jagielski }
86*b1cdbd2cSJim Jagielski }
87*b1cdbd2cSJim Jagielski }
88*b1cdbd2cSJim Jagielski
89*b1cdbd2cSJim Jagielski
90*b1cdbd2cSJim Jagielski
91*b1cdbd2cSJim Jagielski
removeEventListener(const cssu::Reference<css::util::XEventListener> & rxListener,const cssu::Reference<cssu::XInterface> & rxEventFocus)92*b1cdbd2cSJim Jagielski void SAL_CALL EventMultiplexer::removeEventListener (
93*b1cdbd2cSJim Jagielski const cssu::Reference<css::util::XEventListener>& rxListener,
94*b1cdbd2cSJim Jagielski const cssu::Reference<cssu::XInterface>& rxEventFocus)
95*b1cdbd2cSJim Jagielski throw(cssu::RuntimeException,cssl::IllegalArgumentException)
96*b1cdbd2cSJim Jagielski {
97*b1cdbd2cSJim Jagielski if ( ! rxListener.is())
98*b1cdbd2cSJim Jagielski throw cssl::IllegalArgumentException(A2S("can not remove an empty reference"), static_cast<XWeak*>(this), 0);
99*b1cdbd2cSJim Jagielski
100*b1cdbd2cSJim Jagielski ListenerMap::iterator iListenerContainer (maListeners.find(rxEventFocus));
101*b1cdbd2cSJim Jagielski if (iListenerContainer != maListeners.end())
102*b1cdbd2cSJim Jagielski {
103*b1cdbd2cSJim Jagielski ListenerContainer& rContainer (iListenerContainer->second);
104*b1cdbd2cSJim Jagielski const ListenerContainer::iterator iListener (::std::find(rContainer.begin(), rContainer.end(), rxListener));
105*b1cdbd2cSJim Jagielski if (iListener != rContainer.end())
106*b1cdbd2cSJim Jagielski rContainer.erase(iListener);
107*b1cdbd2cSJim Jagielski }
108*b1cdbd2cSJim Jagielski
109*b1cdbd2cSJim Jagielski }
110*b1cdbd2cSJim Jagielski
111*b1cdbd2cSJim Jagielski
112*b1cdbd2cSJim Jagielski
113*b1cdbd2cSJim Jagielski
removeAllEventListeners(const cssu::Reference<css::util::XEventListener> & rxListener)114*b1cdbd2cSJim Jagielski void SAL_CALL EventMultiplexer::removeAllEventListeners (
115*b1cdbd2cSJim Jagielski const cssu::Reference<css::util::XEventListener>& rxListener)
116*b1cdbd2cSJim Jagielski throw(cssu::RuntimeException,cssl::IllegalArgumentException)
117*b1cdbd2cSJim Jagielski {
118*b1cdbd2cSJim Jagielski if ( ! rxListener.is())
119*b1cdbd2cSJim Jagielski throw cssl::IllegalArgumentException(A2S("can not remove an empty reference"), static_cast<XWeak*>(this), 0);
120*b1cdbd2cSJim Jagielski
121*b1cdbd2cSJim Jagielski for (ListenerMap::iterator
122*b1cdbd2cSJim Jagielski iContainer(maListeners.begin()),
123*b1cdbd2cSJim Jagielski iEnd(maListeners.end());
124*b1cdbd2cSJim Jagielski iContainer!=iEnd;
125*b1cdbd2cSJim Jagielski ++iContainer)
126*b1cdbd2cSJim Jagielski {
127*b1cdbd2cSJim Jagielski const ListenerContainer::iterator iListener (::std::find(iContainer->second.begin(), iContainer->second.end(), rxListener));
128*b1cdbd2cSJim Jagielski if (iListener != iContainer->second.end())
129*b1cdbd2cSJim Jagielski iContainer->second.erase(iListener);
130*b1cdbd2cSJim Jagielski }
131*b1cdbd2cSJim Jagielski }
132*b1cdbd2cSJim Jagielski
133*b1cdbd2cSJim Jagielski
134*b1cdbd2cSJim Jagielski
135*b1cdbd2cSJim Jagielski
136*b1cdbd2cSJim Jagielski
broadcastEvent(const cssl::EventObject & rEventObject,const cssu::Reference<cssu::XInterface> & rxEventFocus)137*b1cdbd2cSJim Jagielski void SAL_CALL EventMultiplexer::broadcastEvent (
138*b1cdbd2cSJim Jagielski const cssl::EventObject& rEventObject,
139*b1cdbd2cSJim Jagielski const cssu::Reference<cssu::XInterface>& rxEventFocus)
140*b1cdbd2cSJim Jagielski throw(cssu::RuntimeException)
141*b1cdbd2cSJim Jagielski {
142*b1cdbd2cSJim Jagielski BroadcastEventToSingleContainer(rEventObject, rxEventFocus);
143*b1cdbd2cSJim Jagielski if (rxEventFocus.is())
144*b1cdbd2cSJim Jagielski BroadcastEventToSingleContainer(rEventObject, NULL);
145*b1cdbd2cSJim Jagielski }
146*b1cdbd2cSJim Jagielski
147*b1cdbd2cSJim Jagielski
148*b1cdbd2cSJim Jagielski
149*b1cdbd2cSJim Jagielski
BroadcastEventToSingleContainer(const cssl::EventObject & rEventObject,const cssu::Reference<cssu::XInterface> & rxEventFocus)150*b1cdbd2cSJim Jagielski void EventMultiplexer::BroadcastEventToSingleContainer (
151*b1cdbd2cSJim Jagielski const cssl::EventObject& rEventObject,
152*b1cdbd2cSJim Jagielski const cssu::Reference<cssu::XInterface>& rxEventFocus)
153*b1cdbd2cSJim Jagielski {
154*b1cdbd2cSJim Jagielski ListenerMap::iterator iListenerContainer (maListeners.find(rxEventFocus));
155*b1cdbd2cSJim Jagielski if (iListenerContainer != maListeners.end())
156*b1cdbd2cSJim Jagielski {
157*b1cdbd2cSJim Jagielski // Create a copy of the listener container to avoid problems
158*b1cdbd2cSJim Jagielski // when one of the called listeners calls add... or remove...
159*b1cdbd2cSJim Jagielski ListenerContainer aContainer (iListenerContainer->second);
160*b1cdbd2cSJim Jagielski for (ListenerContainer::const_iterator
161*b1cdbd2cSJim Jagielski iListener(aContainer.begin()),
162*b1cdbd2cSJim Jagielski iEnd(aContainer.end());
163*b1cdbd2cSJim Jagielski iListener!=iEnd;
164*b1cdbd2cSJim Jagielski ++iListener)
165*b1cdbd2cSJim Jagielski {
166*b1cdbd2cSJim Jagielski (*iListener)->notifyEvent(rEventObject);
167*b1cdbd2cSJim Jagielski }
168*b1cdbd2cSJim Jagielski }
169*b1cdbd2cSJim Jagielski }
170*b1cdbd2cSJim Jagielski
171*b1cdbd2cSJim Jagielski
172*b1cdbd2cSJim Jagielski
173*b1cdbd2cSJim Jagielski
174*b1cdbd2cSJim Jagielski // XSingleComponentFactory
createInstanceWithContext(const cssu::Reference<cssu::XComponentContext> & rxContext)175*b1cdbd2cSJim Jagielski cssu::Reference<cssu::XInterface> SAL_CALL EventMultiplexer::createInstanceWithContext (
176*b1cdbd2cSJim Jagielski const cssu::Reference<cssu::XComponentContext>& rxContext)
177*b1cdbd2cSJim Jagielski throw (cssu::Exception, cssu::RuntimeException)
178*b1cdbd2cSJim Jagielski {
179*b1cdbd2cSJim Jagielski return cssu::Reference<cssu::XInterface>();
180*b1cdbd2cSJim Jagielski }
181*b1cdbd2cSJim Jagielski
182*b1cdbd2cSJim Jagielski
183*b1cdbd2cSJim Jagielski
184*b1cdbd2cSJim Jagielski
createInstanceWithArgumentsAndContext(const cssu::Sequence<cssu::Any> & rArguments,const cssu::Reference<cssu::XComponentContext> & rxContext)185*b1cdbd2cSJim Jagielski cssu::Reference<cssu::XInterface > SAL_CALL EventMultiplexer::createInstanceWithArgumentsAndContext (
186*b1cdbd2cSJim Jagielski const cssu::Sequence<cssu::Any>& rArguments,
187*b1cdbd2cSJim Jagielski const cssu::Reference<cssu::XComponentContext>& rxContext)
188*b1cdbd2cSJim Jagielski throw (cssu::Exception, cssu::RuntimeException)
189*b1cdbd2cSJim Jagielski {
190*b1cdbd2cSJim Jagielski return cssu::Reference<cssu::XInterface>();
191*b1cdbd2cSJim Jagielski }
192*b1cdbd2cSJim Jagielski
193*b1cdbd2cSJim Jagielski
194*b1cdbd2cSJim Jagielski
195*b1cdbd2cSJim Jagielski
196*b1cdbd2cSJim Jagielski // XServiceInfo
197*b1cdbd2cSJim Jagielski
getImplementationName(void)198*b1cdbd2cSJim Jagielski ::rtl::OUString SAL_CALL EventMultiplexer::getImplementationName (void)
199*b1cdbd2cSJim Jagielski throw(cssu::RuntimeException)
200*b1cdbd2cSJim Jagielski {
201*b1cdbd2cSJim Jagielski return impl_getStaticImplementationName();
202*b1cdbd2cSJim Jagielski }
203*b1cdbd2cSJim Jagielski
204*b1cdbd2cSJim Jagielski
205*b1cdbd2cSJim Jagielski
206*b1cdbd2cSJim Jagielski
207*b1cdbd2cSJim Jagielski
supportsService(const::rtl::OUString & rsServiceName)208*b1cdbd2cSJim Jagielski sal_Bool SAL_CALL EventMultiplexer::supportsService (
209*b1cdbd2cSJim Jagielski const ::rtl::OUString& rsServiceName)
210*b1cdbd2cSJim Jagielski throw (cssu::RuntimeException)
211*b1cdbd2cSJim Jagielski {
212*b1cdbd2cSJim Jagielski return ::comphelper::findValue(static_GetSupportedServiceNames(), rsServiceName, sal_True).getLength() != 0;
213*b1cdbd2cSJim Jagielski }
214*b1cdbd2cSJim Jagielski
215*b1cdbd2cSJim Jagielski
216*b1cdbd2cSJim Jagielski
217*b1cdbd2cSJim Jagielski
getSupportedServiceNames(void)218*b1cdbd2cSJim Jagielski cssu::Sequence<OUString> SAL_CALL EventMultiplexer::getSupportedServiceNames (void)
219*b1cdbd2cSJim Jagielski throw (cssu::RuntimeException)
220*b1cdbd2cSJim Jagielski {
221*b1cdbd2cSJim Jagielski return static_GetSupportedServiceNames();
222*b1cdbd2cSJim Jagielski }
223*b1cdbd2cSJim Jagielski
224*b1cdbd2cSJim Jagielski
225*b1cdbd2cSJim Jagielski
226*b1cdbd2cSJim Jagielski
227*b1cdbd2cSJim Jagielski // Local and static methods.
228*b1cdbd2cSJim Jagielski
impl_getStaticImplementationName(void)229*b1cdbd2cSJim Jagielski OUString SAL_CALL EventMultiplexer::impl_getStaticImplementationName (void)
230*b1cdbd2cSJim Jagielski {
231*b1cdbd2cSJim Jagielski return A2S(IMPLEMENTATION_NAME);
232*b1cdbd2cSJim Jagielski }
233*b1cdbd2cSJim Jagielski
234*b1cdbd2cSJim Jagielski
235*b1cdbd2cSJim Jagielski
236*b1cdbd2cSJim Jagielski
static_GetSupportedServiceNames(void)237*b1cdbd2cSJim Jagielski cssu::Sequence<OUString> SAL_CALL EventMultiplexer::static_GetSupportedServiceNames (void)
238*b1cdbd2cSJim Jagielski {
239*b1cdbd2cSJim Jagielski cssu::Sequence<OUString> aServiceNames (2);
240*b1cdbd2cSJim Jagielski aServiceNames[0] = A2S(SERVICE_NAME);
241*b1cdbd2cSJim Jagielski aServiceNames[1] = A2S(SINGLETON_NAME);
242*b1cdbd2cSJim Jagielski return aServiceNames;
243*b1cdbd2cSJim Jagielski }
244*b1cdbd2cSJim Jagielski
245*b1cdbd2cSJim Jagielski
246*b1cdbd2cSJim Jagielski
247*b1cdbd2cSJim Jagielski
impl_createFactory(const cssu::Reference<cssl::XMultiServiceFactory> & rxServiceManager)248*b1cdbd2cSJim Jagielski cssu::Reference<cssu::XInterface> EventMultiplexer::impl_createFactory (
249*b1cdbd2cSJim Jagielski const cssu::Reference<cssl::XMultiServiceFactory>& rxServiceManager)
250*b1cdbd2cSJim Jagielski {
251*b1cdbd2cSJim Jagielski return cppu::createSingleComponentFactory(
252*b1cdbd2cSJim Jagielski EventMultiplexer::static_CreateInstance,
253*b1cdbd2cSJim Jagielski EventMultiplexer::impl_getStaticImplementationName(),
254*b1cdbd2cSJim Jagielski EventMultiplexer::static_GetSupportedServiceNames()
255*b1cdbd2cSJim Jagielski );
256*b1cdbd2cSJim Jagielski }
257*b1cdbd2cSJim Jagielski
258*b1cdbd2cSJim Jagielski
259*b1cdbd2cSJim Jagielski
260*b1cdbd2cSJim Jagielski
static_CreateInstance(const cssu::Reference<cssu::XComponentContext> & rxComponentContext)261*b1cdbd2cSJim Jagielski cssu::Reference<cssu::XInterface> SAL_CALL EventMultiplexer::static_CreateInstance (
262*b1cdbd2cSJim Jagielski const cssu::Reference<cssu::XComponentContext>& rxComponentContext)
263*b1cdbd2cSJim Jagielski throw (cssu::Exception)
264*b1cdbd2cSJim Jagielski {
265*b1cdbd2cSJim Jagielski EventMultiplexer* pObject = new EventMultiplexer(rxComponentContext);
266*b1cdbd2cSJim Jagielski cssu::Reference<cssu::XInterface> xService (static_cast<XWeak*>(pObject), cssu::UNO_QUERY);
267*b1cdbd2cSJim Jagielski return xService;
268*b1cdbd2cSJim Jagielski }
269*b1cdbd2cSJim Jagielski
270*b1cdbd2cSJim Jagielski } // end of namespace framework
271