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 #include <sfx2/viewsh.hxx>
33 #include <tools/diagnose_ex.h>
34 
35 using namespace css;
36 using namespace cssu;
37 
38 
NotifyContextChange(const cssu::Reference<css::frame::XController> & rxController,const::sfx2::sidebar::EnumContext::Context eContext)39 void ContextChangeEventMultiplexer::NotifyContextChange (
40     const cssu::Reference<css::frame::XController>& rxController,
41     const ::sfx2::sidebar::EnumContext::Context eContext)
42 {
43     if (rxController.is() && rxController->getFrame().is())
44     {
45         const css::ui::ContextChangeEventObject aEvent(
46             rxController,
47             GetModuleName(rxController->getFrame()),
48             ::sfx2::sidebar::EnumContext::GetContextName(eContext));
49 
50         cssu::Reference<css::ui::XContextChangeEventMultiplexer> xMultiplexer (
51             css::ui::ContextChangeEventMultiplexer::get(
52                 ::comphelper::getProcessComponentContext()));
53         if (xMultiplexer.is())
54             xMultiplexer->broadcastContextChangeEvent(aEvent, rxController);
55     }
56 }
57 
58 
59 
60 
NotifyContextChange(SfxViewShell * pViewShell,const::sfx2::sidebar::EnumContext::Context eContext)61 void ContextChangeEventMultiplexer::NotifyContextChange (
62     SfxViewShell* pViewShell,
63     const ::sfx2::sidebar::EnumContext::Context eContext)
64 {
65     if (pViewShell != NULL)
66         NotifyContextChange(pViewShell->GetController(), eContext);
67 }
68 
69 
70 
71 
GetModuleName(const cssu::Reference<css::frame::XFrame> & rxFrame)72 ::rtl::OUString ContextChangeEventMultiplexer::GetModuleName (
73     const cssu::Reference<css::frame::XFrame>& rxFrame)
74 {
75     try
76     {
77         const ::comphelper::ComponentContext aContext (::comphelper::getProcessServiceFactory());
78         const Reference<frame::XModuleManager> xModuleManager (
79             aContext.createComponent("com.sun.star.frame.ModuleManager" ),
80             UNO_QUERY_THROW );
81         return xModuleManager->identify(rxFrame);
82     }
83     catch (const Exception&)
84     {
85         // An exception typically means that a context change is notified
86         // during initialization or destruction of a view.
87         // Ignore it.
88     }
89     return ::sfx2::sidebar::EnumContext::GetApplicationName(
90         ::sfx2::sidebar::EnumContext::Application_None);
91 }
92