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()) 97 { 98 OSL_ENSURE(false, "Activate called with invalid frame"); 99 return; 100 } 101 102 const css::ui::ContextChangeEventObject aEvent( 103 rxFrame->getController(), 104 rsModuleName, 105 rsContextName); 106 107 cssu::Reference<css::ui::XContextChangeEventMultiplexer> xMultiplexer ( 108 css::ui::ContextChangeEventMultiplexer::get( 109 ::comphelper::getProcessComponentContext())); 110 if (xMultiplexer.is()) 111 xMultiplexer->broadcastContextChangeEvent(aEvent, rxFrame->getController()); 112 } 113 114 115 116 117 OUString ContextChangeBroadcaster::GetModuleName (const cssu::Reference<css::frame::XFrame>& rxFrame) 118 { 119 OUString sModuleName; 120 try 121 { 122 const ::comphelper::ComponentContext aContext (::comphelper::getProcessServiceFactory()); 123 const Reference<frame::XModuleManager> xModuleManager ( 124 aContext.createComponent("com.sun.star.frame.ModuleManager" ), 125 UNO_QUERY_THROW ); 126 return xModuleManager->identify(rxFrame); 127 } 128 catch (const Exception&) 129 { 130 OSL_ENSURE(false, "can not determine module name"); 131 } 132 return OUString(); 133 } 134 135 136 137 } } // end of namespace ::sd::sidebar 138