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