1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #include "precompiled_sd.hxx"
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir #include "ConfigurationControllerBroadcaster.hxx"
31*cdf0e10cSrcweir #include <com/sun/star/lang/IllegalArgumentException.hpp>
32*cdf0e10cSrcweir #include <com/sun/star/lang/DisposedException.hpp>
33*cdf0e10cSrcweir #include <tools/debug.hxx>
34*cdf0e10cSrcweir #include <tools/diagnose_ex.h>
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir using namespace ::com::sun::star;
37*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
38*cdf0e10cSrcweir using namespace ::com::sun::star::drawing::framework;
39*cdf0e10cSrcweir using rtl::OUString;
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir namespace sd { namespace framework {
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir ConfigurationControllerBroadcaster::ConfigurationControllerBroadcaster (
44*cdf0e10cSrcweir     const Reference<XConfigurationController>& rxController)
45*cdf0e10cSrcweir     : mxConfigurationController(rxController),
46*cdf0e10cSrcweir     maListenerMap()
47*cdf0e10cSrcweir {
48*cdf0e10cSrcweir }
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir void ConfigurationControllerBroadcaster::AddListener(
54*cdf0e10cSrcweir     const Reference<XConfigurationChangeListener>& rxListener,
55*cdf0e10cSrcweir     const ::rtl::OUString& rsEventType,
56*cdf0e10cSrcweir     const Any& rUserData)
57*cdf0e10cSrcweir {
58*cdf0e10cSrcweir     if ( ! rxListener.is())
59*cdf0e10cSrcweir         throw lang::IllegalArgumentException(
60*cdf0e10cSrcweir             OUString::createFromAscii("invalid listener"),
61*cdf0e10cSrcweir             mxConfigurationController,
62*cdf0e10cSrcweir             0);
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir     if (maListenerMap.find(rsEventType) == maListenerMap.end())
65*cdf0e10cSrcweir         maListenerMap[rsEventType] = ListenerList();
66*cdf0e10cSrcweir     ListenerDescriptor aDescriptor;
67*cdf0e10cSrcweir     aDescriptor.mxListener = rxListener;
68*cdf0e10cSrcweir     aDescriptor.maUserData = rUserData;
69*cdf0e10cSrcweir     maListenerMap[rsEventType].push_back(aDescriptor);
70*cdf0e10cSrcweir }
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir void ConfigurationControllerBroadcaster::RemoveListener(
76*cdf0e10cSrcweir     const Reference<XConfigurationChangeListener>& rxListener)
77*cdf0e10cSrcweir {
78*cdf0e10cSrcweir     if ( ! rxListener.is())
79*cdf0e10cSrcweir         throw lang::IllegalArgumentException(
80*cdf0e10cSrcweir             OUString::createFromAscii("invalid listener"),
81*cdf0e10cSrcweir             mxConfigurationController,
82*cdf0e10cSrcweir             0);
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir     ListenerMap::iterator iMap;
85*cdf0e10cSrcweir     ListenerList::iterator iList;
86*cdf0e10cSrcweir     for (iMap=maListenerMap.begin(); iMap!=maListenerMap.end(); ++iMap)
87*cdf0e10cSrcweir     {
88*cdf0e10cSrcweir         for (iList=iMap->second.begin(); iList!=iMap->second.end(); ++iList)
89*cdf0e10cSrcweir         {
90*cdf0e10cSrcweir             if (iList->mxListener == rxListener)
91*cdf0e10cSrcweir             {
92*cdf0e10cSrcweir                 iMap->second.erase(iList);
93*cdf0e10cSrcweir                 break;
94*cdf0e10cSrcweir             }
95*cdf0e10cSrcweir         }
96*cdf0e10cSrcweir     }
97*cdf0e10cSrcweir }
98*cdf0e10cSrcweir 
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir void ConfigurationControllerBroadcaster::NotifyListeners (
103*cdf0e10cSrcweir     const ListenerList& rList,
104*cdf0e10cSrcweir     const ConfigurationChangeEvent& rEvent)
105*cdf0e10cSrcweir {
106*cdf0e10cSrcweir     // Create a local copy of the event in which the user data is modified
107*cdf0e10cSrcweir     // for every listener.
108*cdf0e10cSrcweir     ConfigurationChangeEvent aEvent (rEvent);
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir     ListenerList::const_iterator iListener;
111*cdf0e10cSrcweir     for (iListener=rList.begin(); iListener!=rList.end(); ++iListener)
112*cdf0e10cSrcweir     {
113*cdf0e10cSrcweir         try
114*cdf0e10cSrcweir         {
115*cdf0e10cSrcweir             aEvent.UserData = iListener->maUserData;
116*cdf0e10cSrcweir             iListener->mxListener->notifyConfigurationChange(aEvent);
117*cdf0e10cSrcweir         }
118*cdf0e10cSrcweir         catch (lang::DisposedException& rException)
119*cdf0e10cSrcweir         {
120*cdf0e10cSrcweir             // When the exception comes from the listener itself then
121*cdf0e10cSrcweir             // unregister it.
122*cdf0e10cSrcweir             if (rException.Context == iListener->mxListener)
123*cdf0e10cSrcweir                 RemoveListener(iListener->mxListener);
124*cdf0e10cSrcweir         }
125*cdf0e10cSrcweir         catch(RuntimeException&)
126*cdf0e10cSrcweir         {
127*cdf0e10cSrcweir             DBG_UNHANDLED_EXCEPTION();
128*cdf0e10cSrcweir         }
129*cdf0e10cSrcweir     }
130*cdf0e10cSrcweir }
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir 
135*cdf0e10cSrcweir void ConfigurationControllerBroadcaster::NotifyListeners (const ConfigurationChangeEvent& rEvent)
136*cdf0e10cSrcweir {
137*cdf0e10cSrcweir     // Notify the specialized listeners.
138*cdf0e10cSrcweir     ListenerMap::const_iterator iMap (maListenerMap.find(rEvent.Type));
139*cdf0e10cSrcweir     if (iMap != maListenerMap.end())
140*cdf0e10cSrcweir     {
141*cdf0e10cSrcweir         // Create a local list of the listeners to avoid problems with
142*cdf0e10cSrcweir         // concurrent changes and to be able to remove disposed listeners.
143*cdf0e10cSrcweir         ListenerList aList (iMap->second.begin(), iMap->second.end());
144*cdf0e10cSrcweir         NotifyListeners(aList,rEvent);
145*cdf0e10cSrcweir     }
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir     // Notify the universal listeners.
148*cdf0e10cSrcweir     iMap = maListenerMap.find(OUString());
149*cdf0e10cSrcweir     if (iMap != maListenerMap.end())
150*cdf0e10cSrcweir     {
151*cdf0e10cSrcweir         // Create a local list of the listeners to avoid problems with
152*cdf0e10cSrcweir         // concurrent changes and to be able to remove disposed listeners.
153*cdf0e10cSrcweir         ListenerList aList (iMap->second.begin(), iMap->second.end());
154*cdf0e10cSrcweir         NotifyListeners(aList,rEvent);
155*cdf0e10cSrcweir     }
156*cdf0e10cSrcweir }
157*cdf0e10cSrcweir 
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir void ConfigurationControllerBroadcaster::NotifyListeners (
162*cdf0e10cSrcweir     const OUString& rsEventType,
163*cdf0e10cSrcweir     const Reference<XResourceId>& rxResourceId,
164*cdf0e10cSrcweir     const Reference<XResource>& rxResourceObject)
165*cdf0e10cSrcweir {
166*cdf0e10cSrcweir     ConfigurationChangeEvent aEvent;
167*cdf0e10cSrcweir     aEvent.Type = rsEventType;
168*cdf0e10cSrcweir     aEvent.ResourceId = rxResourceId;
169*cdf0e10cSrcweir     aEvent.ResourceObject = rxResourceObject;
170*cdf0e10cSrcweir     try
171*cdf0e10cSrcweir     {
172*cdf0e10cSrcweir         NotifyListeners(aEvent);
173*cdf0e10cSrcweir     }
174*cdf0e10cSrcweir     catch (lang::DisposedException)
175*cdf0e10cSrcweir     {
176*cdf0e10cSrcweir     }
177*cdf0e10cSrcweir }
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir 
183*cdf0e10cSrcweir void ConfigurationControllerBroadcaster::DisposeAndClear (void)
184*cdf0e10cSrcweir {
185*cdf0e10cSrcweir     lang::EventObject aEvent;
186*cdf0e10cSrcweir     aEvent.Source = mxConfigurationController;
187*cdf0e10cSrcweir     while (!maListenerMap.empty())
188*cdf0e10cSrcweir     {
189*cdf0e10cSrcweir         ListenerMap::iterator iMap (maListenerMap.begin());
190*cdf0e10cSrcweir         if (iMap == maListenerMap.end())
191*cdf0e10cSrcweir             break;
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir         // When the first vector is empty then remove it from the map.
194*cdf0e10cSrcweir         if (iMap->second.empty())
195*cdf0e10cSrcweir         {
196*cdf0e10cSrcweir             maListenerMap.erase(iMap);
197*cdf0e10cSrcweir             continue;
198*cdf0e10cSrcweir         }
199*cdf0e10cSrcweir         else
200*cdf0e10cSrcweir         {
201*cdf0e10cSrcweir             Reference<lang::XEventListener> xListener (
202*cdf0e10cSrcweir                 iMap->second.front().mxListener, UNO_QUERY);
203*cdf0e10cSrcweir             if (xListener.is())
204*cdf0e10cSrcweir             {
205*cdf0e10cSrcweir                 // Tell the listener that the configuration controller is
206*cdf0e10cSrcweir                 // being disposed and remove the listener (for all event
207*cdf0e10cSrcweir                 // types).
208*cdf0e10cSrcweir                 try
209*cdf0e10cSrcweir                 {
210*cdf0e10cSrcweir                     RemoveListener(iMap->second.front().mxListener);
211*cdf0e10cSrcweir                     xListener->disposing(aEvent);
212*cdf0e10cSrcweir                 }
213*cdf0e10cSrcweir                 catch (RuntimeException&)
214*cdf0e10cSrcweir                 {
215*cdf0e10cSrcweir                     DBG_UNHANDLED_EXCEPTION();
216*cdf0e10cSrcweir                 }
217*cdf0e10cSrcweir             }
218*cdf0e10cSrcweir             else
219*cdf0e10cSrcweir             {
220*cdf0e10cSrcweir                 // Remove just this reference to the listener.
221*cdf0e10cSrcweir                 iMap->second.erase(iMap->second.begin());
222*cdf0e10cSrcweir             }
223*cdf0e10cSrcweir         }
224*cdf0e10cSrcweir     }
225*cdf0e10cSrcweir }
226*cdf0e10cSrcweir 
227*cdf0e10cSrcweir 
228*cdf0e10cSrcweir 
229*cdf0e10cSrcweir 
230*cdf0e10cSrcweir } } // end of namespace sd::framework
231*cdf0e10cSrcweir 
232