Line data Source code
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 <sfx2/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 <comphelper/processfactory.hxx>
25 :
26 :
27 : using ::rtl::OUString;
28 : using namespace css;
29 : using namespace cssu;
30 :
31 : namespace sfx2 { namespace sidebar {
32 :
33 :
34 0 : ContextChangeBroadcaster::ContextChangeBroadcaster (void)
35 : : msContextName(),
36 0 : mbIsBroadcasterEnabled(true)
37 : {
38 0 : }
39 :
40 :
41 :
42 0 : ContextChangeBroadcaster::~ContextChangeBroadcaster (void)
43 : {
44 0 : }
45 :
46 :
47 :
48 :
49 0 : void ContextChangeBroadcaster::Initialize (const ::rtl::OUString& rsContextName)
50 : {
51 0 : msContextName = rsContextName;
52 0 : }
53 :
54 :
55 :
56 :
57 0 : void ContextChangeBroadcaster::Activate (const cssu::Reference<css::frame::XFrame>& rxFrame)
58 : {
59 0 : if (msContextName.getLength() > 0)
60 0 : BroadcastContextChange(rxFrame, GetModuleName(rxFrame), msContextName);
61 0 : }
62 :
63 :
64 :
65 :
66 0 : void ContextChangeBroadcaster::Deactivate (const cssu::Reference<css::frame::XFrame>& rxFrame)
67 : {
68 0 : if (msContextName.getLength() > 0)
69 : {
70 : BroadcastContextChange(
71 : rxFrame,
72 : GetModuleName(rxFrame),
73 0 : EnumContext::GetContextName(EnumContext::Context_Default));
74 : }
75 0 : }
76 :
77 :
78 :
79 :
80 0 : bool ContextChangeBroadcaster::SetBroadcasterEnabled (const bool bIsEnabled)
81 : {
82 0 : const bool bWasEnabled (mbIsBroadcasterEnabled);
83 0 : mbIsBroadcasterEnabled = bIsEnabled;
84 0 : return bWasEnabled;
85 : }
86 :
87 :
88 :
89 :
90 0 : void ContextChangeBroadcaster::BroadcastContextChange (
91 : const cssu::Reference<css::frame::XFrame>& rxFrame,
92 : const ::rtl::OUString& rsModuleName,
93 : const ::rtl::OUString& rsContextName)
94 : {
95 0 : if ( ! mbIsBroadcasterEnabled)
96 0 : return;
97 :
98 0 : if (rsContextName.getLength() == 0)
99 0 : return;
100 :
101 0 : if ( ! rxFrame.is() || ! rxFrame->getController().is())
102 : {
103 : // Frame is (probably) being deleted. Broadcasting context
104 : // changes is not necessary anymore.
105 0 : return;
106 : }
107 :
108 : const css::ui::ContextChangeEventObject aEvent(
109 0 : rxFrame->getController(),
110 : rsModuleName,
111 0 : rsContextName);
112 :
113 : cssu::Reference<css::ui::XContextChangeEventMultiplexer> xMultiplexer (
114 : css::ui::ContextChangeEventMultiplexer::get(
115 0 : ::comphelper::getProcessComponentContext()));
116 0 : if (xMultiplexer.is())
117 0 : xMultiplexer->broadcastContextChangeEvent(aEvent, rxFrame->getController());
118 : }
119 :
120 :
121 :
122 :
123 0 : OUString ContextChangeBroadcaster::GetModuleName (const cssu::Reference<css::frame::XFrame>& rxFrame)
124 : {
125 0 : if ( ! rxFrame.is() || ! rxFrame->getController().is())
126 0 : return OUString();
127 : try
128 : {
129 0 : const Reference<XComponentContext> xContext (::comphelper::getProcessComponentContext() );
130 0 : const Reference<frame::XModuleManager> xModuleManager = frame::ModuleManager::create( xContext );
131 0 : return xModuleManager->identify(rxFrame);
132 : }
133 0 : catch (const Exception&)
134 : {
135 : OSL_ENSURE(false, "can not determine module name");
136 : }
137 0 : return OUString();
138 : }
139 :
140 :
141 :
142 : } } // end of namespace ::sd::sidebar
143 :
144 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|