LibreOffice Module framework (master) 1
menubarfactory.cxx
Go to the documentation of this file.
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
21
23
24#include <com/sun/star/frame/ModuleManager.hpp>
25#include <com/sun/star/frame/XFrame.hpp>
26#include <com/sun/star/frame/XModel.hpp>
27#include <com/sun/star/lang/XInitialization.hpp>
28#include <com/sun/star/ui/theModuleUIConfigurationManagerSupplier.hpp>
29#include <com/sun/star/ui/XUIConfigurationManagerSupplier.hpp>
30#include <com/sun/star/uno/XComponentContext.hpp>
31
32#include <utility>
33#include <vcl/svapp.hxx>
34
35using namespace com::sun::star::uno;
36using namespace com::sun::star::lang;
37using namespace com::sun::star::frame;
38using namespace com::sun::star::beans;
39using namespace com::sun::star::util;
40using namespace ::com::sun::star::ui;
41
42namespace framework
43{
44
45MenuBarFactory::MenuBarFactory( css::uno::Reference< css::uno::XComponentContext > xContext )
46 : m_xContext(std::move( xContext ))
47{
48}
49
51{
52}
53
54// XUIElementFactory
56 const OUString& ResourceURL,
57 const Sequence< PropertyValue >& Args )
58{
60 CreateUIElement(ResourceURL, Args, u"private:resource/menubar/", xMenuBar, m_xContext);
61 return xMenuBar;
62}
63
64void MenuBarFactory::CreateUIElement(const OUString& ResourceURL
65 ,const Sequence< PropertyValue >& Args
66 ,std::u16string_view ResourceType
67 ,const Reference< css::ui::XUIElement >& _xMenuBar
68 ,const css::uno::Reference< css::uno::XComponentContext >& _rxContext)
69{
70 sal_Int32 nConfigPropertyIndex( Args.getLength() );
71 sal_Int32 nURLPropertyIndex( Args.getLength() );
74 OUString aResourceURL( ResourceURL );
75
76 for ( sal_Int32 n = 0; n < Args.getLength(); n++ )
77 {
78 if ( Args[n].Name == "ConfigurationSource" )
79 {
80 nConfigPropertyIndex = n;
81 Args[n].Value >>= xCfgMgr;
82 }
83 else if ( Args[n].Name == "ResourceURL" )
84 {
85 nURLPropertyIndex = n;
86 Args[n].Value >>= aResourceURL;
87 }
88 else if ( Args[n].Name == "Frame" )
89 Args[n].Value >>= xFrame;
90 }
91 if (!aResourceURL.startsWith(ResourceType))
92 throw IllegalArgumentException();
93
94 // Identify frame and determine document based ui configuration manager/module ui configuration manager
95 if ( xFrame.is() && !xCfgMgr.is() )
96 {
97 bool bHasSettings( false );
99
101 if ( xController.is() )
102 xModel = xController->getModel();
103
104 if ( xModel.is() )
105 {
106 Reference< XUIConfigurationManagerSupplier > xUIConfigurationManagerSupplier( xModel, UNO_QUERY );
107 if ( xUIConfigurationManagerSupplier.is() )
108 {
109 xCfgMgr = xUIConfigurationManagerSupplier->getUIConfigurationManager();
110 bHasSettings = xCfgMgr->hasSettings( aResourceURL );
111 }
112 }
113
114 if ( !bHasSettings )
115 {
117 ModuleManager::create( _rxContext );
118 OUString aModuleIdentifier = xModuleManager->identify( Reference<XInterface>( xFrame, UNO_QUERY ) );
119 if ( !aModuleIdentifier.isEmpty() )
120 {
122 theModuleUIConfigurationManagerSupplier::get( _rxContext );
123 xCfgMgr = xModuleCfgSupplier->getUIConfigurationManager( aModuleIdentifier );
124 }
125 }
126 }
127
128 sal_Int32 nSeqLength( Args.getLength() );
129 if ( Args.getLength() == nConfigPropertyIndex )
130 nSeqLength++;
131 if ( Args.getLength() == nURLPropertyIndex )
132 nSeqLength++;
133 if ( nConfigPropertyIndex == nURLPropertyIndex )
134 nURLPropertyIndex++;
135
136 Sequence< Any > aPropSeq( nSeqLength );
137 auto aPropSeqRange = asNonConstRange(aPropSeq);
138 for ( sal_Int32 n = 0; n < aPropSeq.getLength(); n++ )
139 {
140 PropertyValue aPropValue;
141 if ( n == nURLPropertyIndex )
142 {
143 aPropValue.Name = "ResourceURL";
144 aPropValue.Value <<= aResourceURL;
145 }
146 else if ( n == nConfigPropertyIndex )
147 {
148 aPropValue.Name = "ConfigurationSource";
149 aPropValue.Value <<= xCfgMgr;
150 }
151 else
152 aPropValue = Args[n];
153
154 aPropSeqRange[n] <<= aPropValue;
155 }
156
157 SolarMutexGuard aGuard;
158 Reference< XInitialization > xInit( _xMenuBar, UNO_QUERY );
159 xInit->initialize( aPropSeq );
160}
161
162} // namespace framework
163
164extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
166 css::uno::XComponentContext *context,
167 css::uno::Sequence<css::uno::Any> const &)
168{
169 return cppu::acquire(new framework::MenuBarFactory(context));
170}
171
172/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual css::uno::Reference< css::ui::XUIElement > SAL_CALL createUIElement(const OUString &ResourceURL, const css::uno::Sequence< css::beans::PropertyValue > &Args) override
css::uno::Reference< css::uno::XComponentContext > m_xContext
virtual ~MenuBarFactory() override
MenuBarFactory(css::uno::Reference< css::uno::XComponentContext > xContext)
static void CreateUIElement(const OUString &ResourceURL, const css::uno::Sequence< css::beans::PropertyValue > &Args, std::u16string_view ResourceType, const css::uno::Reference< css::ui::XUIElement > &_xMenuBar, const css::uno::Reference< css::uno::XComponentContext > &_rxContext)
float u
css::uno::Reference< css::uno::XComponentContext > m_xContext
sal_Int64 n
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_comp_framework_MenuBarFactory_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
ResourceType
Reference< XController > xController
Reference< XFrame > xFrame
Reference< XModel > xModel
OUString Name