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