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