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_sfx2.hxx"
23 
24 #include "sfx2/sidebar/ContextChangeBroadcaster.hxx"
25 #include "sfx2/sidebar/EnumContext.hxx"
26 #include <com/sun/star/ui/ContextChangeEventObject.hpp>
27 #include <com/sun/star/ui/ContextChangeEventMultiplexer.hpp>
28 #include <com/sun/star/frame/XModuleManager.hpp>
29 #include <comphelper/componentcontext.hxx>
30 #include <comphelper/processfactory.hxx>
31 
32 
33 using ::rtl::OUString;
34 using namespace css;
35 using namespace cssu;
36 
37 namespace sfx2 { namespace sidebar {
38 
39 
ContextChangeBroadcaster(void)40 ContextChangeBroadcaster::ContextChangeBroadcaster (void)
41     : msContextName(),
42       mbIsContextActive(false),
43       mbIsBroadcasterEnabled(true)
44 {
45 }
46 
47 
48 
~ContextChangeBroadcaster(void)49 ContextChangeBroadcaster::~ContextChangeBroadcaster (void)
50 {
51 }
52 
53 
54 
55 
Initialize(const::rtl::OUString & rsContextName)56 void ContextChangeBroadcaster::Initialize (const ::rtl::OUString& rsContextName)
57 {
58     OSL_ASSERT( ! mbIsContextActive);
59 
60     msContextName = rsContextName;
61 }
62 
63 
64 
65 
Activate(const cssu::Reference<css::frame::XFrame> & rxFrame)66 void ContextChangeBroadcaster::Activate (const cssu::Reference<css::frame::XFrame>& rxFrame)
67 {
68     if (msContextName.getLength() > 0)
69         BroadcastContextChange(rxFrame, GetModuleName(rxFrame), msContextName);
70 }
71 
72 
73 
74 
Deactivate(const cssu::Reference<css::frame::XFrame> & rxFrame)75 void ContextChangeBroadcaster::Deactivate (const cssu::Reference<css::frame::XFrame>& rxFrame)
76 {
77     if (msContextName.getLength() > 0)
78     {
79         BroadcastContextChange(
80             rxFrame,
81             GetModuleName(rxFrame),
82             EnumContext::GetContextName(EnumContext::Context_Default));
83     }
84 }
85 
86 
87 
88 
SetBroadcasterEnabled(const bool bIsEnabled)89 bool ContextChangeBroadcaster::SetBroadcasterEnabled (const bool bIsEnabled)
90 {
91     const bool bWasEnabled (mbIsBroadcasterEnabled);
92     mbIsBroadcasterEnabled = bIsEnabled;
93     return bWasEnabled;
94 }
95 
96 
97 
98 
BroadcastContextChange(const cssu::Reference<css::frame::XFrame> & rxFrame,const::rtl::OUString & rsModuleName,const::rtl::OUString & rsContextName)99 void ContextChangeBroadcaster::BroadcastContextChange (
100     const cssu::Reference<css::frame::XFrame>& rxFrame,
101     const ::rtl::OUString& rsModuleName,
102     const ::rtl::OUString& rsContextName)
103 {
104     if ( ! mbIsBroadcasterEnabled)
105         return;
106 
107     if (rsContextName.getLength() == 0)
108         return;
109 
110     if ( ! rxFrame.is() || ! rxFrame->getController().is())
111     {
112         // Frame is (probably) being deleted.  Broadcasting context
113         // changes is not necessary anymore.
114         return;
115     }
116 
117     const css::ui::ContextChangeEventObject aEvent(
118         rxFrame->getController(),
119         rsModuleName,
120         rsContextName);
121 
122     cssu::Reference<css::ui::XContextChangeEventMultiplexer> xMultiplexer (
123         css::ui::ContextChangeEventMultiplexer::get(
124             ::comphelper::getProcessComponentContext()));
125     if (xMultiplexer.is())
126         xMultiplexer->broadcastContextChangeEvent(aEvent, rxFrame->getController());
127 }
128 
129 
130 
131 
GetModuleName(const cssu::Reference<css::frame::XFrame> & rxFrame)132 OUString ContextChangeBroadcaster::GetModuleName (const cssu::Reference<css::frame::XFrame>& rxFrame)
133 {
134     if ( ! rxFrame.is() || ! rxFrame->getController().is())
135         return OUString();
136     OUString sModuleName;
137     try
138     {
139         const ::comphelper::ComponentContext aContext (::comphelper::getProcessServiceFactory());
140         const Reference<frame::XModuleManager> xModuleManager (
141             aContext.createComponent("com.sun.star.frame.ModuleManager" ),
142             UNO_QUERY_THROW );
143         return xModuleManager->identify(rxFrame);
144     }
145     catch (const Exception&)
146     {
147         OSL_ENSURE(false, "can not determine module name");
148     }
149     return OUString();
150 }
151 
152 
153 
154 } } // end of namespace ::sd::sidebar
155