LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / sfx2 / source / sidebar / ContextChangeBroadcaster.cxx
blob0350929eedf18eb0a8169fde93e64dcbcb3b6e58
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 : mbIsBroadcasterEnabled(true)
40 ContextChangeBroadcaster::~ContextChangeBroadcaster()
44 void ContextChangeBroadcaster::Initialize (const OUString& rsContextName)
46 msContextName = rsContextName;
49 void ContextChangeBroadcaster::Activate (const css::uno::Reference<css::frame::XFrame>& rxFrame)
51 if (msContextName.getLength() > 0)
52 BroadcastContextChange(rxFrame, GetModuleName(rxFrame), msContextName);
55 void ContextChangeBroadcaster::Deactivate (const css::uno::Reference<css::frame::XFrame>& rxFrame)
57 if (msContextName.getLength() > 0 && !comphelper::LibreOfficeKit::isActive())
59 BroadcastContextChange(rxFrame, GetModuleName(rxFrame),
60 vcl::EnumContext::GetContextName(vcl::EnumContext::Context::Default));
64 bool ContextChangeBroadcaster::SetBroadcasterEnabled (const bool bIsEnabled)
66 const bool bWasEnabled (mbIsBroadcasterEnabled);
67 mbIsBroadcasterEnabled = bIsEnabled;
68 return bWasEnabled;
71 void ContextChangeBroadcaster::BroadcastContextChange (
72 const css::uno::Reference<css::frame::XFrame>& rxFrame,
73 const OUString& rsModuleName,
74 const OUString& rsContextName)
76 if ( ! mbIsBroadcasterEnabled)
77 return;
79 if (rsContextName.getLength() == 0)
80 return;
82 if ( ! rxFrame.is() || ! rxFrame->getController().is())
84 // Frame is (probably) being deleted. Broadcasting context
85 // changes is not necessary anymore.
86 return;
89 // notify the LOK too
90 if (comphelper::LibreOfficeKit::isActive())
92 if (SfxViewShell* pViewShell = SfxViewShell::Get(rxFrame->getController()))
93 SfxLokHelper::notifyContextChange(pViewShell, rsModuleName, rsContextName);
96 const css::ui::ContextChangeEventObject aEvent(
97 rxFrame->getController(),
98 rsModuleName,
99 rsContextName);
101 css::uno::Reference<css::ui::XContextChangeEventMultiplexer> xMultiplexer (
102 css::ui::ContextChangeEventMultiplexer::get(
103 ::comphelper::getProcessComponentContext()));
104 if (xMultiplexer.is())
105 xMultiplexer->broadcastContextChangeEvent(aEvent, rxFrame->getController());
108 OUString ContextChangeBroadcaster::GetModuleName (const css::uno::Reference<css::frame::XFrame>& rxFrame)
110 if ( ! rxFrame.is() || ! rxFrame->getController().is())
111 return OUString();
114 const Reference<XComponentContext> xContext (::comphelper::getProcessComponentContext() );
115 const Reference<frame::XModuleManager> xModuleManager = frame::ModuleManager::create( xContext );
116 return xModuleManager->identify(rxFrame);
118 catch (const Exception&)
120 OSL_ENSURE(false, "can not determine module name");
122 return OUString();
125 } // end of namespace ::sfx2::sidebar
127 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */