1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef __FRAMEWORK_SERVICES_CONTEXT_CHANGE_EVENT_MULTIPLEXER_HXX_
25 #define __FRAMEWORK_SERVICES_CONTEXT_CHANGE_EVENT_MULTIPLEXER_HXX_
26 
27 #include <com/sun/star/ui/XContextChangeEventMultiplexer.hpp>
28 
29 #include <cppuhelper/compbase3.hxx>
30 #include <cppuhelper/basemutex.hxx>
31 
32 #include "macros/xserviceinfo.hxx"
33 
34 #include <map>
35 #include <boost/noncopyable.hpp>
36 
37 
38 namespace
39 {
40     typedef ::cppu::WeakComponentImplHelper3 <
41         css::ui::XContextChangeEventMultiplexer,
42         css::lang::XSingleComponentFactory,
43         css::lang::XServiceInfo
44         > ContextChangeEventMultiplexerInterfaceBase;
45 }
46 
47 
48 namespace css = ::com::sun::star;
49 namespace cssu = ::com::sun::star::uno;
50 namespace cssl = ::com::sun::star::lang;
51 
52 namespace framework {
53 
54 class ContextChangeEventMultiplexer
55     : private ::boost::noncopyable,
56       private ::cppu::BaseMutex,
57       public ContextChangeEventMultiplexerInterfaceBase
58 {
59 public:
60     ContextChangeEventMultiplexer(const cssu::Reference<css::uno::XComponentContext>& rxContext);
61     virtual ~ContextChangeEventMultiplexer (void);
62 
63     virtual void SAL_CALL disposing (void);
64 
65     // XContextChangeEventMultiplexer
66     virtual void SAL_CALL addContextChangeEventListener (
67         const cssu::Reference<css::ui::XContextChangeEventListener>& rxListener,
68         const cssu::Reference<cssu::XInterface>& rxEventFocus)
69         throw(cssu::RuntimeException, cssl::IllegalArgumentException);
70     virtual void SAL_CALL removeContextChangeEventListener (
71         const cssu::Reference<css::ui::XContextChangeEventListener>& rxListener,
72         const cssu::Reference<cssu::XInterface>& rxEventFocus)
73         throw(cssu::RuntimeException, cssl::IllegalArgumentException);
74     virtual void SAL_CALL removeAllContextChangeEventListeners (
75         const cssu::Reference<css::ui::XContextChangeEventListener>& rxListener)
76         throw(cssu::RuntimeException, cssl::IllegalArgumentException);
77     virtual void SAL_CALL broadcastContextChangeEvent (
78         const css::ui::ContextChangeEventObject& rContextChangeEventObject,
79         const cssu::Reference<cssu::XInterface>& rxEventFocus)
80         throw(cssu::RuntimeException);
81 
82     // XSingleComponentFactory
83     virtual cssu::Reference<cssu::XInterface> SAL_CALL createInstanceWithContext (
84         const cssu::Reference<cssu::XComponentContext>& rxContext)
85         throw (cssu::Exception, cssu::RuntimeException);
86     virtual cssu::Reference<cssu::XInterface > SAL_CALL createInstanceWithArgumentsAndContext (
87         const cssu::Sequence<cssu::Any>& rArguments,
88         const cssu::Reference<cssu::XComponentContext>& rxContext)
89         throw (cssu::Exception, cssu::RuntimeException);
90 
91     // XServiceInfo
92     virtual ::rtl::OUString SAL_CALL getImplementationName (void)
93         throw (cssu::RuntimeException);
94     virtual sal_Bool SAL_CALL supportsService  (
95         const ::rtl::OUString& rsServiceName)
96         throw (cssu::RuntimeException);
97     virtual cssu::Sequence< ::rtl::OUString> SAL_CALL getSupportedServiceNames (void)
98         throw (cssu::RuntimeException);
99 
100     static ::rtl::OUString SAL_CALL impl_getStaticImplementationName (void);
101     static cssu::Reference<cssu::XInterface> SAL_CALL impl_createFactory (
102         const cssu::Reference<cssl::XMultiServiceFactory>& xServiceManager);
103 
104 private:
105     typedef ::std::vector<cssu::Reference<css::ui::XContextChangeEventListener> > ListenerContainer;
106     class FocusDescriptor
107     {
108     public:
109         ListenerContainer maListeners;
110         ::rtl::OUString msCurrentApplicationName;
111         ::rtl::OUString msCurrentContextName;
112     };
113     typedef ::std::map<cssu::Reference<cssu::XInterface>, FocusDescriptor> ListenerMap;
114     ListenerMap maListeners;
115 
116     /** Notify all listeners in the container that is associated with
117         the given event focus.
118 
119         Typically called twice from broadcastEvent(), once for the
120         given event focus and onece for NULL.
121     */
122     void BroadcastEventToSingleContainer (
123         const css::ui::ContextChangeEventObject& rEventObject,
124         const cssu::Reference<cssu::XInterface>& rxEventFocus);
125 
126     static cssu::Sequence< ::rtl::OUString > SAL_CALL static_GetSupportedServiceNames (void);
127     static cssu::Reference<cssu::XInterface> SAL_CALL static_CreateInstance (
128         const cssu::Reference<cssu::XComponentContext>& rxComponentContext)
129         throw (cssu::Exception);
130 };
131 
132 } // end of namespace framework
133 
134 #endif
135 
136