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