Version 7.1.7.1, tag libreoffice-7.1.7.1
[LibreOffice.git] / sfx2 / source / sidebar / ContextChangeBroadcaster.cxx
blobb000ec3f8c97591321e0bc48e89fff5e891861b0
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 <vcl/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/lok.hxx>
26 #include <comphelper/processfactory.hxx>
27 #include <sfx2/lokhelper.hxx>
28 #include <sfx2/viewsh.hxx>
30 using namespace css;
31 using namespace css::uno;
33 namespace sfx2::sidebar {
35 ContextChangeBroadcaster::ContextChangeBroadcaster()
36 : msContextName(),
37 mbIsBroadcasterEnabled(true)
41 ContextChangeBroadcaster::~ContextChangeBroadcaster()
45 void ContextChangeBroadcaster::Initialize (const OUString& rsContextName)
47 msContextName = rsContextName;
50 void ContextChangeBroadcaster::Activate (const css::uno::Reference<css::frame::XFrame>& rxFrame)
52 if (msContextName.getLength() > 0)
53 BroadcastContextChange(rxFrame, GetModuleName(rxFrame), msContextName);
56 void ContextChangeBroadcaster::Deactivate (const css::uno::Reference<css::frame::XFrame>& rxFrame)
58 if (msContextName.getLength() > 0 && !comphelper::LibreOfficeKit::isActive())
60 BroadcastContextChange(rxFrame, GetModuleName(rxFrame),
61 vcl::EnumContext::GetContextName(vcl::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 OUString& rsModuleName,
75 const 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 // notify the LOK too
91 if (comphelper::LibreOfficeKit::isActive())
93 if (SfxViewShell* pViewShell = SfxViewShell::Get(rxFrame->getController()))
94 SfxLokHelper::notifyContextChange(pViewShell, rsModuleName, rsContextName);
97 const css::ui::ContextChangeEventObject aEvent(
98 rxFrame->getController(),
99 rsModuleName,
100 rsContextName);
102 css::uno::Reference<css::ui::XContextChangeEventMultiplexer> xMultiplexer (
103 css::ui::ContextChangeEventMultiplexer::get(
104 ::comphelper::getProcessComponentContext()));
105 if (xMultiplexer.is())
106 xMultiplexer->broadcastContextChangeEvent(aEvent, rxFrame->getController());
109 OUString ContextChangeBroadcaster::GetModuleName (const css::uno::Reference<css::frame::XFrame>& rxFrame)
111 if ( ! rxFrame.is() || ! rxFrame->getController().is())
112 return OUString();
115 const Reference<XComponentContext> xContext (::comphelper::getProcessComponentContext() );
116 const Reference<frame::XModuleManager> xModuleManager = frame::ModuleManager::create( xContext );
117 return xModuleManager->identify(rxFrame);
119 catch (const Exception&)
121 OSL_ENSURE(false, "can not determine module name");
123 return OUString();
126 } // end of namespace ::sfx2::sidebar
128 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */