1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20 #include <svx/sidebar/ContextChangeEventMultiplexer.hxx>
21
22 #include <com/sun/star/ui/ContextChangeEventObject.hpp>
23 #include <com/sun/star/ui/XContextChangeEventMultiplexer.hpp>
24 #include <com/sun/star/ui/ContextChangeEventMultiplexer.hpp>
25 #include <com/sun/star/frame/ModuleManager.hpp>
26 #include <comphelper/lok.hxx>
27 #include <comphelper/processfactory.hxx>
28 #include <sfx2/lokhelper.hxx>
29 #include <sfx2/viewsh.hxx>
30 #include <tools/diagnose_ex.h>
31
32 using namespace css;
33 using namespace css::uno;
34
35
NotifyContextChange(const css::uno::Reference<css::frame::XController> & rxController,const vcl::EnumContext::Context eContext)36 void ContextChangeEventMultiplexer::NotifyContextChange (
37 const css::uno::Reference<css::frame::XController>& rxController,
38 const vcl::EnumContext::Context eContext)
39 {
40 if (rxController.is() && rxController->getFrame().is())
41 {
42 // notify the LOK too
43 if (comphelper::LibreOfficeKit::isActive())
44 {
45 if (SfxViewShell* pViewShell = SfxViewShell::Get(rxController))
46 SfxLokHelper::notifyContextChange(pViewShell, GetModuleName(rxController->getFrame()), vcl::EnumContext::GetContextName(eContext));
47 }
48
49 const css::ui::ContextChangeEventObject aEvent(
50 rxController,
51 GetModuleName(rxController->getFrame()),
52 vcl::EnumContext::GetContextName(eContext));
53
54 css::uno::Reference<css::ui::XContextChangeEventMultiplexer> xMultiplexer (
55 css::ui::ContextChangeEventMultiplexer::get(
56 ::comphelper::getProcessComponentContext()));
57 if (xMultiplexer.is())
58 xMultiplexer->broadcastContextChangeEvent(aEvent, rxController);
59 }
60 }
61
62
NotifyContextChange(const SfxViewShell * pViewShell,const vcl::EnumContext::Context eContext)63 void ContextChangeEventMultiplexer::NotifyContextChange (
64 const SfxViewShell* pViewShell,
65 const vcl::EnumContext::Context eContext)
66 {
67 if (pViewShell != nullptr)
68 NotifyContextChange(pViewShell->GetController(), eContext);
69 }
70
71
GetModuleName(const css::uno::Reference<css::frame::XFrame> & rxFrame)72 OUString ContextChangeEventMultiplexer::GetModuleName (
73 const css::uno::Reference<css::frame::XFrame>& rxFrame)
74 {
75 try
76 {
77 const Reference<frame::XModuleManager> xModuleManager =
78 frame::ModuleManager::create( comphelper::getProcessComponentContext() );
79 return xModuleManager->identify(rxFrame);
80 }
81 catch (const Exception&)
82 {
83 // An exception typically means that a context change is notified
84 // during initialization or destruction of a view.
85 // Ignore it.
86 }
87 return vcl::EnumContext::GetApplicationName(
88 vcl::EnumContext::Application::NONE);
89 }
90
91 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
92