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 #include "precompiled_svx.hxx"
23 
24 #include "svx/sidebar/ContextChangeEventMultiplexer.hxx"
25 
26 #include <com/sun/star/ui/ContextChangeEventObject.hpp>
27 #include <com/sun/star/ui/XContextChangeEventMultiplexer.hpp>
28 #include <com/sun/star/ui/ContextChangeEventMultiplexer.hpp>
29 #include <com/sun/star/frame/XModuleManager.hpp>
30 #include <comphelper/processfactory.hxx>
31 #include <comphelper/componentcontext.hxx>
32 
33 #include <tools/diagnose_ex.h>
34 
35 using namespace css;
36 using namespace cssu;
37 
38 #define A2S(pString) (::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(pString)))
39 
40 void ContextChangeEventMultiplexer::NotifyContextChange (
41     const cssu::Reference<css::frame::XController>& rxController,
42     const ::sfx2::sidebar::EnumContext::Context eContext)
43 {
44     if (rxController.is() && rxController->getFrame().is())
45     {
46         const css::ui::ContextChangeEventObject aEvent(
47             rxController,
48             GetModuleName(rxController->getFrame()),
49             ::sfx2::sidebar::EnumContext::GetContextName(eContext));
50 
51         cssu::Reference<css::ui::XContextChangeEventMultiplexer> xMultiplexer (
52             css::ui::ContextChangeEventMultiplexer::get(
53                 ::comphelper::getProcessComponentContext()));
54         if (xMultiplexer.is())
55             xMultiplexer->broadcastContextChangeEvent(aEvent, rxController);
56     }
57 }
58 
59 
60 
61 
62 ::rtl::OUString ContextChangeEventMultiplexer::GetModuleName (
63     const cssu::Reference<css::frame::XFrame>& rxFrame)
64 {
65     try
66     {
67         const ::comphelper::ComponentContext aContext (::comphelper::getProcessServiceFactory());
68         const Reference<frame::XModuleManager> xModuleManager (
69             aContext.createComponent("com.sun.star.frame.ModuleManager" ),
70             UNO_QUERY_THROW );
71         return xModuleManager->identify(rxFrame);
72     }
73     catch (const Exception&)
74     {
75         // An exception typically means that a context change is notified
76         // during initialization or destruction of a view.
77         // Ignore it.
78     }
79     return ::sfx2::sidebar::EnumContext::GetApplicationName(
80         ::sfx2::sidebar::EnumContext::Application_Unknown);
81 }
82