1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef SD_FRAMEWORK_CONFIGURATION_CONTROLLER_BROADCASTER_HXX
29 #define SD_FRAMEWORK_CONFIGURATION_CONTROLLER_BROADCASTER_HXX
30 
31 #include <com/sun/star/drawing/framework/XConfigurationChangeListener.hpp>
32 #include <com/sun/star/drawing/framework/XConfigurationController.hpp>
33 #include <com/sun/star/drawing/framework/ConfigurationChangeEvent.hpp>
34 
35 #include <comphelper/stl_types.hxx>
36 #include <vector>
37 #include <hash_map>
38 
39 namespace css = ::com::sun::star;
40 
41 namespace sd { namespace framework {
42 
43 /** This class manages the set of XConfigurationChangeListeners and
44     calls them when the ConfigurationController wants to broadcast an
45     event.
46 
47     For every registered combination of listener and event type a user data
48     object is stored.  This user data object is then given to the listener
49     whenever it is called for an event.  With this the listener can use
50     a switch statement to handle different event types.
51 */
52 class ConfigurationControllerBroadcaster
53 {
54 public:
55     /** The given controller is used as origin of thrown exceptions.
56     */
57     ConfigurationControllerBroadcaster (
58         const css::uno::Reference<
59             css::drawing::framework::XConfigurationController>& rxController);
60 
61     /** Add a listener for one type of event.  When one listener is
62         interested in more than one event type this method has to be called
63         once for every event type.  Alternatively it can register as
64         universal listener that will be called for all event types.
65         @param rxListener
66             A valid reference to a listener.
67         @param rsEventType
68             The type of event that the listener will be called for.  The
69             empty string is a special value in that the listener will be
70             called for all event types.
71         @param rUserData
72             This object is passed to the listener whenever it is called for
73             the specified event type.  For different event types different
74             user data objects can be provided.
75         @throws IllegalArgumentException
76             when an empty listener reference is given.
77     */
78     void AddListener(
79         const css::uno::Reference<
80             css::drawing::framework::XConfigurationChangeListener>& rxListener,
81         const ::rtl::OUString& rsEventType,
82         const css::uno::Any& rUserData);
83 
84     /** Remove all references to the given listener.  When one listener has
85         been registered for more than one type of event then it is removed
86         for all of them.
87         @param rxListener
88             A valid reference to a listener.
89         @throws IllegalArgumentException
90             when an empty listener reference is given.
91     */
92     void RemoveListener(
93         const css::uno::Reference<
94             css::drawing::framework::XConfigurationChangeListener>& rxListener);
95 
96     /** Broadcast the given event to all listeners that have been registered
97         for its type of event as well as all universal listeners.
98 
99         When calling a listener results in a DisposedException being thrown
100         the listener is unregistered automatically.
101     */
102     void NotifyListeners (
103         const css::drawing::framework::ConfigurationChangeEvent& rEvent);
104 
105     /** This convenience variant of NotifyListeners create the event from
106         the given arguments.
107     */
108     void NotifyListeners (
109         const ::rtl::OUString& rsEventType,
110         const ::css::uno::Reference<css::drawing::framework::XResourceId>& rxResourceId,
111         const ::css::uno::Reference<css::drawing::framework::XResource>& rxResourceObject);
112 
113     /** Call all listeners and inform them that the
114         ConfigurationController is being disposed.  When this method returns
115         the list of registered listeners is empty.  Further calls to
116         RemoveListener() are not necessary but do not result in an error.
117     */
118     void DisposeAndClear (void);
119 
120 private:
121     css::uno::Reference<
122         com::sun::star::drawing::framework::XConfigurationController> mxConfigurationController;
123     class ListenerDescriptor {public:
124         css::uno::Reference<
125             css::drawing::framework::XConfigurationChangeListener> mxListener;
126         css::uno::Any maUserData;
127     };
128     typedef ::std::vector<ListenerDescriptor> ListenerList;
129     typedef ::std::hash_map
130         <rtl::OUString,
131          ListenerList,
132          ::comphelper::UStringHash,
133          ::comphelper::UStringEqual> ListenerMap;
134     ListenerMap maListenerMap;
135 
136     /** Broadcast the given event to all listeners in the given list.
137 
138         When calling a listener results in a DisposedException being thrown
139         the listener is unregistered automatically.
140     */
141     void NotifyListeners (
142         const ListenerList& rList,
143         const css::drawing::framework::ConfigurationChangeEvent& rEvent);
144 };
145 
146 
147 
148 
149 } } // end of namespace sd::framework
150 
151 #endif
152