/src/libreoffice/sfx2/source/sidebar/ContextChangeBroadcaster.cxx
Line | Count | Source |
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 | | #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/diagnose_ex.hxx> |
26 | | #include <comphelper/lok.hxx> |
27 | | #include <comphelper/processfactory.hxx> |
28 | | #include <sfx2/lokhelper.hxx> |
29 | | #include <sfx2/viewsh.hxx> |
30 | | |
31 | | using namespace css; |
32 | | using namespace css::uno; |
33 | | |
34 | | namespace sfx2::sidebar { |
35 | | |
36 | | ContextChangeBroadcaster::ContextChangeBroadcaster() |
37 | 204k | : mbIsBroadcasterEnabled(true) |
38 | 204k | { |
39 | 204k | } |
40 | | |
41 | | ContextChangeBroadcaster::~ContextChangeBroadcaster() |
42 | 204k | { |
43 | 204k | } |
44 | | |
45 | | void ContextChangeBroadcaster::Initialize (const OUString& rsContextName) |
46 | 3.29k | { |
47 | 3.29k | msContextName = rsContextName; |
48 | 3.29k | } |
49 | | |
50 | | void ContextChangeBroadcaster::Activate (const css::uno::Reference<css::frame::XFrame>& rxFrame) |
51 | 19.7k | { |
52 | 19.7k | if (msContextName.getLength() > 0) |
53 | 3.29k | BroadcastContextChange(rxFrame, GetModuleName(rxFrame), msContextName); |
54 | 19.7k | } |
55 | | |
56 | | void ContextChangeBroadcaster::Deactivate (const css::uno::Reference<css::frame::XFrame>& rxFrame) |
57 | 13.1k | { |
58 | 13.1k | if (msContextName.getLength() > 0 && !comphelper::LibreOfficeKit::isActive()) |
59 | 3.29k | { |
60 | 3.29k | BroadcastContextChange(rxFrame, GetModuleName(rxFrame), |
61 | 3.29k | vcl::EnumContext::GetContextName(vcl::EnumContext::Context::Default)); |
62 | 3.29k | } |
63 | 13.1k | } |
64 | | |
65 | | bool ContextChangeBroadcaster::SetBroadcasterEnabled (const bool bIsEnabled) |
66 | 0 | { |
67 | 0 | const bool bWasEnabled (mbIsBroadcasterEnabled); |
68 | 0 | mbIsBroadcasterEnabled = bIsEnabled; |
69 | 0 | return bWasEnabled; |
70 | 0 | } |
71 | | |
72 | | void ContextChangeBroadcaster::BroadcastContextChange ( |
73 | | const css::uno::Reference<css::frame::XFrame>& rxFrame, |
74 | | const OUString& rsModuleName, |
75 | | const OUString& rsContextName) |
76 | 6.59k | { |
77 | 6.59k | if ( ! mbIsBroadcasterEnabled) |
78 | 0 | return; |
79 | | |
80 | 6.59k | if (rsContextName.getLength() == 0) |
81 | 0 | return; |
82 | | |
83 | 6.59k | if ( ! rxFrame.is() || ! rxFrame->getController().is()) |
84 | 3.29k | { |
85 | | // Frame is (probably) being deleted. Broadcasting context |
86 | | // changes is not necessary anymore. |
87 | 3.29k | return; |
88 | 3.29k | } |
89 | | |
90 | 3.30k | const css::ui::ContextChangeEventObject aEvent( |
91 | 3.30k | rxFrame->getController(), |
92 | 3.30k | rsModuleName, |
93 | 3.30k | rsContextName); |
94 | | |
95 | | // notify the LOK too |
96 | 3.30k | if (comphelper::LibreOfficeKit::isActive()) |
97 | 0 | { |
98 | 0 | SfxLokHelper::notifyContextChange(aEvent); |
99 | 0 | } |
100 | | |
101 | 3.30k | css::uno::Reference<css::ui::XContextChangeEventMultiplexer> xMultiplexer ( |
102 | 3.30k | css::ui::ContextChangeEventMultiplexer::get( |
103 | 3.30k | ::comphelper::getProcessComponentContext())); |
104 | 3.30k | if (xMultiplexer.is()) |
105 | 3.30k | xMultiplexer->broadcastContextChangeEvent(aEvent, rxFrame->getController()); |
106 | 3.30k | } |
107 | | |
108 | | OUString ContextChangeBroadcaster::GetModuleName (const css::uno::Reference<css::frame::XFrame>& rxFrame) |
109 | 6.59k | { |
110 | 6.59k | if ( ! rxFrame.is() || ! rxFrame->getController().is()) |
111 | 3.29k | return OUString(); |
112 | 3.30k | try |
113 | 3.30k | { |
114 | 3.30k | const Reference<XComponentContext>& xContext (::comphelper::getProcessComponentContext() ); |
115 | 3.30k | const Reference<frame::XModuleManager> xModuleManager = frame::ModuleManager::create( xContext ); |
116 | 3.30k | return xModuleManager->identify(rxFrame); |
117 | 3.30k | } |
118 | 3.30k | catch (const Exception&) |
119 | 3.30k | { |
120 | 0 | TOOLS_WARN_EXCEPTION("sfx.sidebar", "can not determine module name"); |
121 | 0 | } |
122 | 0 | return OUString(); |
123 | 3.30k | } |
124 | | |
125 | | } // end of namespace ::sfx2::sidebar |
126 | | |
127 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |