Branch libreoffice-5-0-4
[LibreOffice.git] / sfx2 / source / sidebar / ContextChangeBroadcaster.cxx
blob5ede4a575ad6048e092e38d044e3c1f226748956
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 #include <sidebar/ContextChangeBroadcaster.hxx>
20 #include <sfx2/sidebar/EnumContext.hxx>
21 #include <com/sun/star/ui/ContextChangeEventObject.hpp>
22 #include <com/sun/star/ui/ContextChangeEventMultiplexer.hpp>
23 #include <com/sun/star/frame/ModuleManager.hpp>
24 #include <osl/diagnose.h>
25 #include <comphelper/processfactory.hxx>
27 using ::rtl::OUString;
28 using namespace css;
29 using namespace css::uno;
31 namespace sfx2 { namespace sidebar {
33 ContextChangeBroadcaster::ContextChangeBroadcaster()
34 : msContextName(),
35 mbIsBroadcasterEnabled(true)
39 ContextChangeBroadcaster::~ContextChangeBroadcaster()
43 void ContextChangeBroadcaster::Initialize (const ::rtl::OUString& rsContextName)
45 msContextName = rsContextName;
48 void ContextChangeBroadcaster::Activate (const css::uno::Reference<css::frame::XFrame>& rxFrame)
50 if (msContextName.getLength() > 0)
51 BroadcastContextChange(rxFrame, GetModuleName(rxFrame), msContextName);
54 void ContextChangeBroadcaster::Deactivate (const css::uno::Reference<css::frame::XFrame>& rxFrame)
56 if (msContextName.getLength() > 0)
58 BroadcastContextChange(
59 rxFrame,
60 GetModuleName(rxFrame),
61 EnumContext::GetContextName(EnumContext::Context_Default));
65 bool ContextChangeBroadcaster::SetBroadcasterEnabled (const bool bIsEnabled)
67 const bool bWasEnabled (mbIsBroadcasterEnabled);
68 mbIsBroadcasterEnabled = bIsEnabled;
69 return bWasEnabled;
72 void ContextChangeBroadcaster::BroadcastContextChange (
73 const css::uno::Reference<css::frame::XFrame>& rxFrame,
74 const ::rtl::OUString& rsModuleName,
75 const ::rtl::OUString& rsContextName)
77 if ( ! mbIsBroadcasterEnabled)
78 return;
80 if (rsContextName.getLength() == 0)
81 return;
83 if ( ! rxFrame.is() || ! rxFrame->getController().is())
85 // Frame is (probably) being deleted. Broadcasting context
86 // changes is not necessary anymore.
87 return;
90 const css::ui::ContextChangeEventObject aEvent(
91 rxFrame->getController(),
92 rsModuleName,
93 rsContextName);
95 css::uno::Reference<css::ui::XContextChangeEventMultiplexer> xMultiplexer (
96 css::ui::ContextChangeEventMultiplexer::get(
97 ::comphelper::getProcessComponentContext()));
98 if (xMultiplexer.is())
99 xMultiplexer->broadcastContextChangeEvent(aEvent, rxFrame->getController());
102 OUString ContextChangeBroadcaster::GetModuleName (const css::uno::Reference<css::frame::XFrame>& rxFrame)
104 if ( ! rxFrame.is() || ! rxFrame->getController().is())
105 return OUString();
108 const Reference<XComponentContext> xContext (::comphelper::getProcessComponentContext() );
109 const Reference<frame::XModuleManager> xModuleManager = frame::ModuleManager::create( xContext );
110 return xModuleManager->identify(rxFrame);
112 catch (const Exception&)
114 OSL_ENSURE(false, "can not determine module name");
116 return OUString();
119 } } // end of namespace ::sd::sidebar
121 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */