1*5b190011SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*5b190011SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*5b190011SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*5b190011SAndrew Rist * distributed with this work for additional information 6*5b190011SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*5b190011SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*5b190011SAndrew Rist * "License"); you may not use this file except in compliance 9*5b190011SAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 11*5b190011SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 13*5b190011SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*5b190011SAndrew Rist * software distributed under the License is distributed on an 15*5b190011SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*5b190011SAndrew Rist * KIND, either express or implied. See the License for the 17*5b190011SAndrew Rist * specific language governing permissions and limitations 18*5b190011SAndrew Rist * under the License. 19cdf0e10cSrcweir * 20*5b190011SAndrew Rist *************************************************************/ 21*5b190011SAndrew Rist 22*5b190011SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir #include "precompiled_sd.hxx" 25cdf0e10cSrcweir 26cdf0e10cSrcweir #include "ConfigurationControllerBroadcaster.hxx" 27cdf0e10cSrcweir #include <com/sun/star/lang/IllegalArgumentException.hpp> 28cdf0e10cSrcweir #include <com/sun/star/lang/DisposedException.hpp> 29cdf0e10cSrcweir #include <tools/debug.hxx> 30cdf0e10cSrcweir #include <tools/diagnose_ex.h> 31cdf0e10cSrcweir 32cdf0e10cSrcweir using namespace ::com::sun::star; 33cdf0e10cSrcweir using namespace ::com::sun::star::uno; 34cdf0e10cSrcweir using namespace ::com::sun::star::drawing::framework; 35cdf0e10cSrcweir using rtl::OUString; 36cdf0e10cSrcweir 37cdf0e10cSrcweir namespace sd { namespace framework { 38cdf0e10cSrcweir 39cdf0e10cSrcweir ConfigurationControllerBroadcaster::ConfigurationControllerBroadcaster ( 40cdf0e10cSrcweir const Reference<XConfigurationController>& rxController) 41cdf0e10cSrcweir : mxConfigurationController(rxController), 42cdf0e10cSrcweir maListenerMap() 43cdf0e10cSrcweir { 44cdf0e10cSrcweir } 45cdf0e10cSrcweir 46cdf0e10cSrcweir 47cdf0e10cSrcweir 48cdf0e10cSrcweir 49cdf0e10cSrcweir void ConfigurationControllerBroadcaster::AddListener( 50cdf0e10cSrcweir const Reference<XConfigurationChangeListener>& rxListener, 51cdf0e10cSrcweir const ::rtl::OUString& rsEventType, 52cdf0e10cSrcweir const Any& rUserData) 53cdf0e10cSrcweir { 54cdf0e10cSrcweir if ( ! rxListener.is()) 55cdf0e10cSrcweir throw lang::IllegalArgumentException( 56cdf0e10cSrcweir OUString::createFromAscii("invalid listener"), 57cdf0e10cSrcweir mxConfigurationController, 58cdf0e10cSrcweir 0); 59cdf0e10cSrcweir 60cdf0e10cSrcweir if (maListenerMap.find(rsEventType) == maListenerMap.end()) 61cdf0e10cSrcweir maListenerMap[rsEventType] = ListenerList(); 62cdf0e10cSrcweir ListenerDescriptor aDescriptor; 63cdf0e10cSrcweir aDescriptor.mxListener = rxListener; 64cdf0e10cSrcweir aDescriptor.maUserData = rUserData; 65cdf0e10cSrcweir maListenerMap[rsEventType].push_back(aDescriptor); 66cdf0e10cSrcweir } 67cdf0e10cSrcweir 68cdf0e10cSrcweir 69cdf0e10cSrcweir 70cdf0e10cSrcweir 71cdf0e10cSrcweir void ConfigurationControllerBroadcaster::RemoveListener( 72cdf0e10cSrcweir const Reference<XConfigurationChangeListener>& rxListener) 73cdf0e10cSrcweir { 74cdf0e10cSrcweir if ( ! rxListener.is()) 75cdf0e10cSrcweir throw lang::IllegalArgumentException( 76cdf0e10cSrcweir OUString::createFromAscii("invalid listener"), 77cdf0e10cSrcweir mxConfigurationController, 78cdf0e10cSrcweir 0); 79cdf0e10cSrcweir 80cdf0e10cSrcweir ListenerMap::iterator iMap; 81cdf0e10cSrcweir ListenerList::iterator iList; 82cdf0e10cSrcweir for (iMap=maListenerMap.begin(); iMap!=maListenerMap.end(); ++iMap) 83cdf0e10cSrcweir { 84cdf0e10cSrcweir for (iList=iMap->second.begin(); iList!=iMap->second.end(); ++iList) 85cdf0e10cSrcweir { 86cdf0e10cSrcweir if (iList->mxListener == rxListener) 87cdf0e10cSrcweir { 88cdf0e10cSrcweir iMap->second.erase(iList); 89cdf0e10cSrcweir break; 90cdf0e10cSrcweir } 91cdf0e10cSrcweir } 92cdf0e10cSrcweir } 93cdf0e10cSrcweir } 94cdf0e10cSrcweir 95cdf0e10cSrcweir 96cdf0e10cSrcweir 97cdf0e10cSrcweir 98cdf0e10cSrcweir void ConfigurationControllerBroadcaster::NotifyListeners ( 99cdf0e10cSrcweir const ListenerList& rList, 100cdf0e10cSrcweir const ConfigurationChangeEvent& rEvent) 101cdf0e10cSrcweir { 102cdf0e10cSrcweir // Create a local copy of the event in which the user data is modified 103cdf0e10cSrcweir // for every listener. 104cdf0e10cSrcweir ConfigurationChangeEvent aEvent (rEvent); 105cdf0e10cSrcweir 106cdf0e10cSrcweir ListenerList::const_iterator iListener; 107cdf0e10cSrcweir for (iListener=rList.begin(); iListener!=rList.end(); ++iListener) 108cdf0e10cSrcweir { 109cdf0e10cSrcweir try 110cdf0e10cSrcweir { 111cdf0e10cSrcweir aEvent.UserData = iListener->maUserData; 112cdf0e10cSrcweir iListener->mxListener->notifyConfigurationChange(aEvent); 113cdf0e10cSrcweir } 114cdf0e10cSrcweir catch (lang::DisposedException& rException) 115cdf0e10cSrcweir { 116cdf0e10cSrcweir // When the exception comes from the listener itself then 117cdf0e10cSrcweir // unregister it. 118cdf0e10cSrcweir if (rException.Context == iListener->mxListener) 119cdf0e10cSrcweir RemoveListener(iListener->mxListener); 120cdf0e10cSrcweir } 121cdf0e10cSrcweir catch(RuntimeException&) 122cdf0e10cSrcweir { 123cdf0e10cSrcweir DBG_UNHANDLED_EXCEPTION(); 124cdf0e10cSrcweir } 125cdf0e10cSrcweir } 126cdf0e10cSrcweir } 127cdf0e10cSrcweir 128cdf0e10cSrcweir 129cdf0e10cSrcweir 130cdf0e10cSrcweir 131cdf0e10cSrcweir void ConfigurationControllerBroadcaster::NotifyListeners (const ConfigurationChangeEvent& rEvent) 132cdf0e10cSrcweir { 133cdf0e10cSrcweir // Notify the specialized listeners. 134cdf0e10cSrcweir ListenerMap::const_iterator iMap (maListenerMap.find(rEvent.Type)); 135cdf0e10cSrcweir if (iMap != maListenerMap.end()) 136cdf0e10cSrcweir { 137cdf0e10cSrcweir // Create a local list of the listeners to avoid problems with 138cdf0e10cSrcweir // concurrent changes and to be able to remove disposed listeners. 139cdf0e10cSrcweir ListenerList aList (iMap->second.begin(), iMap->second.end()); 140cdf0e10cSrcweir NotifyListeners(aList,rEvent); 141cdf0e10cSrcweir } 142cdf0e10cSrcweir 143cdf0e10cSrcweir // Notify the universal listeners. 144cdf0e10cSrcweir iMap = maListenerMap.find(OUString()); 145cdf0e10cSrcweir if (iMap != maListenerMap.end()) 146cdf0e10cSrcweir { 147cdf0e10cSrcweir // Create a local list of the listeners to avoid problems with 148cdf0e10cSrcweir // concurrent changes and to be able to remove disposed listeners. 149cdf0e10cSrcweir ListenerList aList (iMap->second.begin(), iMap->second.end()); 150cdf0e10cSrcweir NotifyListeners(aList,rEvent); 151cdf0e10cSrcweir } 152cdf0e10cSrcweir } 153cdf0e10cSrcweir 154cdf0e10cSrcweir 155cdf0e10cSrcweir 156cdf0e10cSrcweir 157cdf0e10cSrcweir void ConfigurationControllerBroadcaster::NotifyListeners ( 158cdf0e10cSrcweir const OUString& rsEventType, 159cdf0e10cSrcweir const Reference<XResourceId>& rxResourceId, 160cdf0e10cSrcweir const Reference<XResource>& rxResourceObject) 161cdf0e10cSrcweir { 162cdf0e10cSrcweir ConfigurationChangeEvent aEvent; 163cdf0e10cSrcweir aEvent.Type = rsEventType; 164cdf0e10cSrcweir aEvent.ResourceId = rxResourceId; 165cdf0e10cSrcweir aEvent.ResourceObject = rxResourceObject; 166cdf0e10cSrcweir try 167cdf0e10cSrcweir { 168cdf0e10cSrcweir NotifyListeners(aEvent); 169cdf0e10cSrcweir } 170cdf0e10cSrcweir catch (lang::DisposedException) 171cdf0e10cSrcweir { 172cdf0e10cSrcweir } 173cdf0e10cSrcweir } 174cdf0e10cSrcweir 175cdf0e10cSrcweir 176cdf0e10cSrcweir 177cdf0e10cSrcweir 178cdf0e10cSrcweir 179cdf0e10cSrcweir void ConfigurationControllerBroadcaster::DisposeAndClear (void) 180cdf0e10cSrcweir { 181cdf0e10cSrcweir lang::EventObject aEvent; 182cdf0e10cSrcweir aEvent.Source = mxConfigurationController; 183cdf0e10cSrcweir while (!maListenerMap.empty()) 184cdf0e10cSrcweir { 185cdf0e10cSrcweir ListenerMap::iterator iMap (maListenerMap.begin()); 186cdf0e10cSrcweir if (iMap == maListenerMap.end()) 187cdf0e10cSrcweir break; 188cdf0e10cSrcweir 189cdf0e10cSrcweir // When the first vector is empty then remove it from the map. 190cdf0e10cSrcweir if (iMap->second.empty()) 191cdf0e10cSrcweir { 192cdf0e10cSrcweir maListenerMap.erase(iMap); 193cdf0e10cSrcweir continue; 194cdf0e10cSrcweir } 195cdf0e10cSrcweir else 196cdf0e10cSrcweir { 197cdf0e10cSrcweir Reference<lang::XEventListener> xListener ( 198cdf0e10cSrcweir iMap->second.front().mxListener, UNO_QUERY); 199cdf0e10cSrcweir if (xListener.is()) 200cdf0e10cSrcweir { 201cdf0e10cSrcweir // Tell the listener that the configuration controller is 202cdf0e10cSrcweir // being disposed and remove the listener (for all event 203cdf0e10cSrcweir // types). 204cdf0e10cSrcweir try 205cdf0e10cSrcweir { 206cdf0e10cSrcweir RemoveListener(iMap->second.front().mxListener); 207cdf0e10cSrcweir xListener->disposing(aEvent); 208cdf0e10cSrcweir } 209cdf0e10cSrcweir catch (RuntimeException&) 210cdf0e10cSrcweir { 211cdf0e10cSrcweir DBG_UNHANDLED_EXCEPTION(); 212cdf0e10cSrcweir } 213cdf0e10cSrcweir } 214cdf0e10cSrcweir else 215cdf0e10cSrcweir { 216cdf0e10cSrcweir // Remove just this reference to the listener. 217cdf0e10cSrcweir iMap->second.erase(iMap->second.begin()); 218cdf0e10cSrcweir } 219cdf0e10cSrcweir } 220cdf0e10cSrcweir } 221cdf0e10cSrcweir } 222cdf0e10cSrcweir 223cdf0e10cSrcweir 224cdf0e10cSrcweir 225cdf0e10cSrcweir 226cdf0e10cSrcweir } } // end of namespace sd::framework 227cdf0e10cSrcweir 228