xref: /AOO41X/main/svx/source/sidebar/ContextChangeEventMultiplexer.cxx (revision 95a1859439d31511e7ca45b278e1eb5d6c4b3a93)
122de8995SAndre Fischer /**************************************************************
222de8995SAndre Fischer  *
322de8995SAndre Fischer  * Licensed to the Apache Software Foundation (ASF) under one
422de8995SAndre Fischer  * or more contributor license agreements.  See the NOTICE file
522de8995SAndre Fischer  * distributed with this work for additional information
622de8995SAndre Fischer  * regarding copyright ownership.  The ASF licenses this file
722de8995SAndre Fischer  * to you under the Apache License, Version 2.0 (the
822de8995SAndre Fischer  * "License"); you may not use this file except in compliance
922de8995SAndre Fischer  * with the License.  You may obtain a copy of the License at
1022de8995SAndre Fischer  *
1122de8995SAndre Fischer  *   http://www.apache.org/licenses/LICENSE-2.0
1222de8995SAndre Fischer  *
1322de8995SAndre Fischer  * Unless required by applicable law or agreed to in writing,
1422de8995SAndre Fischer  * software distributed under the License is distributed on an
1522de8995SAndre Fischer  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1622de8995SAndre Fischer  * KIND, either express or implied.  See the License for the
1722de8995SAndre Fischer  * specific language governing permissions and limitations
1822de8995SAndre Fischer  * under the License.
1922de8995SAndre Fischer  *
2022de8995SAndre Fischer  *************************************************************/
2122de8995SAndre Fischer 
2222de8995SAndre Fischer #include "precompiled_svx.hxx"
23b9e67834SAndre Fischer 
24b9e67834SAndre Fischer #include "svx/sidebar/ContextChangeEventMultiplexer.hxx"
25b9e67834SAndre Fischer 
2622de8995SAndre Fischer #include <com/sun/star/ui/ContextChangeEventObject.hpp>
2722de8995SAndre Fischer #include <com/sun/star/ui/XContextChangeEventMultiplexer.hpp>
2822de8995SAndre Fischer #include <com/sun/star/ui/ContextChangeEventMultiplexer.hpp>
29b9e67834SAndre Fischer #include <com/sun/star/frame/XModuleManager.hpp>
3022de8995SAndre Fischer #include <comphelper/processfactory.hxx>
31b9e67834SAndre Fischer #include <comphelper/componentcontext.hxx>
3222de8995SAndre Fischer 
33b9e67834SAndre Fischer #include <tools/diagnose_ex.h>
3422de8995SAndre Fischer 
3522de8995SAndre Fischer using namespace css;
3622de8995SAndre Fischer using namespace cssu;
3722de8995SAndre Fischer 
3822de8995SAndre Fischer #define A2S(pString) (::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(pString)))
3922de8995SAndre Fischer 
4022de8995SAndre Fischer void ContextChangeEventMultiplexer::NotifyContextChange (
4122de8995SAndre Fischer     const cssu::Reference<css::frame::XController>& rxController,
42b9e67834SAndre Fischer     const ::sfx2::sidebar::EnumContext::Context eContext)
43b9e67834SAndre Fischer {
44*95a18594SAndre Fischer     if (rxController.is() && rxController->getFrame().is())
4522de8995SAndre Fischer     {
4622de8995SAndre Fischer         const css::ui::ContextChangeEventObject aEvent(
4722de8995SAndre Fischer             rxController,
48b9e67834SAndre Fischer             GetModuleName(rxController->getFrame()),
49b9e67834SAndre Fischer             ::sfx2::sidebar::EnumContext::GetContextName(eContext));
5022de8995SAndre Fischer 
5122de8995SAndre Fischer         cssu::Reference<css::ui::XContextChangeEventMultiplexer> xMultiplexer (
5222de8995SAndre Fischer             css::ui::ContextChangeEventMultiplexer::get(
5322de8995SAndre Fischer                 ::comphelper::getProcessComponentContext()));
5422de8995SAndre Fischer         if (xMultiplexer.is())
5522de8995SAndre Fischer             xMultiplexer->broadcastContextChangeEvent(aEvent, rxController);
5622de8995SAndre Fischer     }
57b9e67834SAndre Fischer }
58b9e67834SAndre Fischer 
59b9e67834SAndre Fischer 
60b9e67834SAndre Fischer 
61b9e67834SAndre Fischer 
62b9e67834SAndre Fischer ::rtl::OUString ContextChangeEventMultiplexer::GetModuleName (
63b9e67834SAndre Fischer     const cssu::Reference<css::frame::XFrame>& rxFrame)
64b9e67834SAndre Fischer {
65b9e67834SAndre Fischer     try
66b9e67834SAndre Fischer     {
67b9e67834SAndre Fischer         const ::comphelper::ComponentContext aContext (::comphelper::getProcessServiceFactory());
68b9e67834SAndre Fischer         const Reference<frame::XModuleManager> xModuleManager (
69b9e67834SAndre Fischer             aContext.createComponent("com.sun.star.frame.ModuleManager" ),
70b9e67834SAndre Fischer             UNO_QUERY_THROW );
71b9e67834SAndre Fischer         return xModuleManager->identify(rxFrame);
72b9e67834SAndre Fischer     }
73b9e67834SAndre Fischer     catch (const Exception&)
74b9e67834SAndre Fischer     {
75*95a18594SAndre Fischer         // An exception typically means that a context change is notified
76*95a18594SAndre Fischer         // during initialization or destruction of a view.
77*95a18594SAndre Fischer         // Ignore it.
78b9e67834SAndre Fischer     }
79b9e67834SAndre Fischer     return ::sfx2::sidebar::EnumContext::GetApplicationName(
80*95a18594SAndre Fischer         ::sfx2::sidebar::EnumContext::Application_Unknown);
81b9e67834SAndre Fischer }
82