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/compbase4.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::WeakComponentImplHelper4 <
41         css::ui::XContextChangeEventMultiplexer,
42         css::lang::XSingleComponentFactory,
43         css::lang::XServiceInfo,
44         css::lang::XEventListener
45         > ContextChangeEventMultiplexerInterfaceBase;
46 }
47 
48 
49 namespace css = ::com::sun::star;
50 namespace cssu = ::com::sun::star::uno;
51 namespace cssl = ::com::sun::star::lang;
52 
53 namespace framework {
54 
55 class ContextChangeEventMultiplexer
56     : private ::boost::noncopyable,
57       private ::cppu::BaseMutex,
58       public ContextChangeEventMultiplexerInterfaceBase
59 {
60 public:
61     ContextChangeEventMultiplexer(const cssu::Reference<css::uno::XComponentContext>& rxContext);
62     virtual ~ContextChangeEventMultiplexer (void);
63 
64     virtual void SAL_CALL disposing (void);
65 
66     // XContextChangeEventMultiplexer
67     virtual void SAL_CALL addContextChangeEventListener (
68         const cssu::Reference<css::ui::XContextChangeEventListener>& rxListener,
69         const cssu::Reference<cssu::XInterface>& rxEventFocus)
70         throw(cssu::RuntimeException, cssl::IllegalArgumentException);
71     virtual void SAL_CALL removeContextChangeEventListener (
72         const cssu::Reference<css::ui::XContextChangeEventListener>& rxListener,
73         const cssu::Reference<cssu::XInterface>& rxEventFocus)
74         throw(cssu::RuntimeException, cssl::IllegalArgumentException);
75     virtual void SAL_CALL removeAllContextChangeEventListeners (
76         const cssu::Reference<css::ui::XContextChangeEventListener>& rxListener)
77         throw(cssu::RuntimeException, cssl::IllegalArgumentException);
78     virtual void SAL_CALL broadcastContextChangeEvent (
79         const css::ui::ContextChangeEventObject& rContextChangeEventObject,
80         const cssu::Reference<cssu::XInterface>& rxEventFocus)
81         throw(cssu::RuntimeException);
82 
83     // XSingleComponentFactory
84     virtual cssu::Reference<cssu::XInterface> SAL_CALL createInstanceWithContext (
85         const cssu::Reference<cssu::XComponentContext>& rxContext)
86         throw (cssu::Exception, cssu::RuntimeException);
87     virtual cssu::Reference<cssu::XInterface > SAL_CALL createInstanceWithArgumentsAndContext (
88         const cssu::Sequence<cssu::Any>& rArguments,
89         const cssu::Reference<cssu::XComponentContext>& rxContext)
90         throw (cssu::Exception, cssu::RuntimeException);
91 
92     // XServiceInfo
93     virtual ::rtl::OUString SAL_CALL getImplementationName (void)
94         throw (cssu::RuntimeException);
95     virtual sal_Bool SAL_CALL supportsService  (
96         const ::rtl::OUString& rsServiceName)
97         throw (cssu::RuntimeException);
98     virtual cssu::Sequence< ::rtl::OUString> SAL_CALL getSupportedServiceNames (void)
99         throw (cssu::RuntimeException);
100 
101     // XEventListener
102     virtual void SAL_CALL disposing (
103         const css::lang::EventObject& rEvent)
104         throw (cssu::RuntimeException);
105 
106     static ::rtl::OUString SAL_CALL impl_getStaticImplementationName (void);
107     static cssu::Reference<cssu::XInterface> SAL_CALL impl_createFactory (
108         const cssu::Reference<cssl::XMultiServiceFactory>& xServiceManager);
109 
110 private:
111     typedef ::std::vector<cssu::Reference<css::ui::XContextChangeEventListener> > ListenerContainer;
112     class FocusDescriptor
113     {
114     public:
115         ListenerContainer maListeners;
116         ::rtl::OUString msCurrentApplicationName;
117         ::rtl::OUString msCurrentContextName;
118     };
119     typedef ::std::map<cssu::Reference<cssu::XInterface>, FocusDescriptor> ListenerMap;
120     ListenerMap maListeners;
121 
122     /** Notify all listeners in the container that is associated with
123         the given event focus.
124 
125         Typically called twice from broadcastEvent(), once for the
126         given event focus and onece for NULL.
127     */
128     void BroadcastEventToSingleContainer (
129         const css::ui::ContextChangeEventObject& rEventObject,
130         const cssu::Reference<cssu::XInterface>& rxEventFocus);
131     FocusDescriptor* GetFocusDescriptor (
132         const cssu::Reference<cssu::XInterface>& rxEventFocus,
133         const bool bCreateWhenMissing);
134 
135     static cssu::Sequence< ::rtl::OUString > SAL_CALL static_GetSupportedServiceNames (void);
136     static cssu::Reference<cssu::XInterface> SAL_CALL static_CreateInstance (
137         const cssu::Reference<cssu::XComponentContext>& rxComponentContext)
138         throw (cssu::Exception);
139 };
140 
141 } // end of namespace framework
142 
143 #endif
144 
145