1*c45d927aSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*c45d927aSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*c45d927aSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*c45d927aSAndrew Rist  * distributed with this work for additional information
6*c45d927aSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*c45d927aSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*c45d927aSAndrew Rist  * "License"); you may not use this file except in compliance
9*c45d927aSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*c45d927aSAndrew Rist  *
11*c45d927aSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*c45d927aSAndrew Rist  *
13*c45d927aSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*c45d927aSAndrew Rist  * software distributed under the License is distributed on an
15*c45d927aSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*c45d927aSAndrew Rist  * KIND, either express or implied.  See the License for the
17*c45d927aSAndrew Rist  * specific language governing permissions and limitations
18*c45d927aSAndrew Rist  * under the License.
19*c45d927aSAndrew Rist  *
20*c45d927aSAndrew Rist  *************************************************************/
21*c45d927aSAndrew Rist 
22*c45d927aSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #ifndef SD_FRAMEWORK_CONFIGURATION_CONTROLLER_BROADCASTER_HXX
25cdf0e10cSrcweir #define SD_FRAMEWORK_CONFIGURATION_CONTROLLER_BROADCASTER_HXX
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <com/sun/star/drawing/framework/XConfigurationChangeListener.hpp>
28cdf0e10cSrcweir #include <com/sun/star/drawing/framework/XConfigurationController.hpp>
29cdf0e10cSrcweir #include <com/sun/star/drawing/framework/ConfigurationChangeEvent.hpp>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include <comphelper/stl_types.hxx>
32cdf0e10cSrcweir #include <vector>
33cdf0e10cSrcweir #include <hash_map>
34cdf0e10cSrcweir 
35cdf0e10cSrcweir namespace css = ::com::sun::star;
36cdf0e10cSrcweir 
37cdf0e10cSrcweir namespace sd { namespace framework {
38cdf0e10cSrcweir 
39cdf0e10cSrcweir /** This class manages the set of XConfigurationChangeListeners and
40cdf0e10cSrcweir     calls them when the ConfigurationController wants to broadcast an
41cdf0e10cSrcweir     event.
42cdf0e10cSrcweir 
43cdf0e10cSrcweir     For every registered combination of listener and event type a user data
44cdf0e10cSrcweir     object is stored.  This user data object is then given to the listener
45cdf0e10cSrcweir     whenever it is called for an event.  With this the listener can use
46cdf0e10cSrcweir     a switch statement to handle different event types.
47cdf0e10cSrcweir */
48cdf0e10cSrcweir class ConfigurationControllerBroadcaster
49cdf0e10cSrcweir {
50cdf0e10cSrcweir public:
51cdf0e10cSrcweir     /** The given controller is used as origin of thrown exceptions.
52cdf0e10cSrcweir     */
53cdf0e10cSrcweir     ConfigurationControllerBroadcaster (
54cdf0e10cSrcweir         const css::uno::Reference<
55cdf0e10cSrcweir             css::drawing::framework::XConfigurationController>& rxController);
56cdf0e10cSrcweir 
57cdf0e10cSrcweir     /** Add a listener for one type of event.  When one listener is
58cdf0e10cSrcweir         interested in more than one event type this method has to be called
59cdf0e10cSrcweir         once for every event type.  Alternatively it can register as
60cdf0e10cSrcweir         universal listener that will be called for all event types.
61cdf0e10cSrcweir         @param rxListener
62cdf0e10cSrcweir             A valid reference to a listener.
63cdf0e10cSrcweir         @param rsEventType
64cdf0e10cSrcweir             The type of event that the listener will be called for.  The
65cdf0e10cSrcweir             empty string is a special value in that the listener will be
66cdf0e10cSrcweir             called for all event types.
67cdf0e10cSrcweir         @param rUserData
68cdf0e10cSrcweir             This object is passed to the listener whenever it is called for
69cdf0e10cSrcweir             the specified event type.  For different event types different
70cdf0e10cSrcweir             user data objects can be provided.
71cdf0e10cSrcweir         @throws IllegalArgumentException
72cdf0e10cSrcweir             when an empty listener reference is given.
73cdf0e10cSrcweir     */
74cdf0e10cSrcweir     void AddListener(
75cdf0e10cSrcweir         const css::uno::Reference<
76cdf0e10cSrcweir             css::drawing::framework::XConfigurationChangeListener>& rxListener,
77cdf0e10cSrcweir         const ::rtl::OUString& rsEventType,
78cdf0e10cSrcweir         const css::uno::Any& rUserData);
79cdf0e10cSrcweir 
80cdf0e10cSrcweir     /** Remove all references to the given listener.  When one listener has
81cdf0e10cSrcweir         been registered for more than one type of event then it is removed
82cdf0e10cSrcweir         for all of them.
83cdf0e10cSrcweir         @param rxListener
84cdf0e10cSrcweir             A valid reference to a listener.
85cdf0e10cSrcweir         @throws IllegalArgumentException
86cdf0e10cSrcweir             when an empty listener reference is given.
87cdf0e10cSrcweir     */
88cdf0e10cSrcweir     void RemoveListener(
89cdf0e10cSrcweir         const css::uno::Reference<
90cdf0e10cSrcweir             css::drawing::framework::XConfigurationChangeListener>& rxListener);
91cdf0e10cSrcweir 
92cdf0e10cSrcweir     /** Broadcast the given event to all listeners that have been registered
93cdf0e10cSrcweir         for its type of event as well as all universal listeners.
94cdf0e10cSrcweir 
95cdf0e10cSrcweir         When calling a listener results in a DisposedException being thrown
96cdf0e10cSrcweir         the listener is unregistered automatically.
97cdf0e10cSrcweir     */
98cdf0e10cSrcweir     void NotifyListeners (
99cdf0e10cSrcweir         const css::drawing::framework::ConfigurationChangeEvent& rEvent);
100cdf0e10cSrcweir 
101cdf0e10cSrcweir     /** This convenience variant of NotifyListeners create the event from
102cdf0e10cSrcweir         the given arguments.
103cdf0e10cSrcweir     */
104cdf0e10cSrcweir     void NotifyListeners (
105cdf0e10cSrcweir         const ::rtl::OUString& rsEventType,
106cdf0e10cSrcweir         const ::css::uno::Reference<css::drawing::framework::XResourceId>& rxResourceId,
107cdf0e10cSrcweir         const ::css::uno::Reference<css::drawing::framework::XResource>& rxResourceObject);
108cdf0e10cSrcweir 
109cdf0e10cSrcweir     /** Call all listeners and inform them that the
110cdf0e10cSrcweir         ConfigurationController is being disposed.  When this method returns
111cdf0e10cSrcweir         the list of registered listeners is empty.  Further calls to
112cdf0e10cSrcweir         RemoveListener() are not necessary but do not result in an error.
113cdf0e10cSrcweir     */
114cdf0e10cSrcweir     void DisposeAndClear (void);
115cdf0e10cSrcweir 
116cdf0e10cSrcweir private:
117cdf0e10cSrcweir     css::uno::Reference<
118cdf0e10cSrcweir         com::sun::star::drawing::framework::XConfigurationController> mxConfigurationController;
119cdf0e10cSrcweir     class ListenerDescriptor {public:
120cdf0e10cSrcweir         css::uno::Reference<
121cdf0e10cSrcweir             css::drawing::framework::XConfigurationChangeListener> mxListener;
122cdf0e10cSrcweir         css::uno::Any maUserData;
123cdf0e10cSrcweir     };
124cdf0e10cSrcweir     typedef ::std::vector<ListenerDescriptor> ListenerList;
125cdf0e10cSrcweir     typedef ::std::hash_map
126cdf0e10cSrcweir         <rtl::OUString,
127cdf0e10cSrcweir          ListenerList,
128cdf0e10cSrcweir          ::comphelper::UStringHash,
129cdf0e10cSrcweir          ::comphelper::UStringEqual> ListenerMap;
130cdf0e10cSrcweir     ListenerMap maListenerMap;
131cdf0e10cSrcweir 
132cdf0e10cSrcweir     /** Broadcast the given event to all listeners in the given list.
133cdf0e10cSrcweir 
134cdf0e10cSrcweir         When calling a listener results in a DisposedException being thrown
135cdf0e10cSrcweir         the listener is unregistered automatically.
136cdf0e10cSrcweir     */
137cdf0e10cSrcweir     void NotifyListeners (
138cdf0e10cSrcweir         const ListenerList& rList,
139cdf0e10cSrcweir         const css::drawing::framework::ConfigurationChangeEvent& rEvent);
140cdf0e10cSrcweir };
141cdf0e10cSrcweir 
142cdf0e10cSrcweir 
143cdf0e10cSrcweir 
144cdf0e10cSrcweir 
145cdf0e10cSrcweir } } // end of namespace sd::framework
146cdf0e10cSrcweir 
147cdf0e10cSrcweir #endif
148