Avoid potential negative array index access to cached text.
[LibreOffice.git] / framework / source / uifactory / menubarfactory.cxx
blobfebc330668521766f0e65ac2b80ce5b73d99bbea
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <uifactory/menubarfactory.hxx>
22 #include <uielement/menubarwrapper.hxx>
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>
32 #include <utility>
33 #include <vcl/svapp.hxx>
35 using namespace com::sun::star::uno;
36 using namespace com::sun::star::lang;
37 using namespace com::sun::star::frame;
38 using namespace com::sun::star::beans;
39 using namespace com::sun::star::util;
40 using namespace ::com::sun::star::ui;
42 namespace framework
45 MenuBarFactory::MenuBarFactory( css::uno::Reference< css::uno::XComponentContext > xContext )
46 : m_xContext(std::move( xContext ))
50 MenuBarFactory::~MenuBarFactory()
54 // XUIElementFactory
55 Reference< XUIElement > SAL_CALL MenuBarFactory::createUIElement(
56 const OUString& ResourceURL,
57 const Sequence< PropertyValue >& Args )
59 Reference< css::ui::XUIElement > xMenuBar = new MenuBarWrapper(m_xContext);
60 CreateUIElement(ResourceURL, Args, u"private:resource/menubar/", xMenuBar, m_xContext);
61 return xMenuBar;
64 void 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)
70 sal_Int32 nConfigPropertyIndex( Args.getLength() );
71 sal_Int32 nURLPropertyIndex( Args.getLength() );
72 Reference< XUIConfigurationManager > xCfgMgr;
73 Reference< XFrame > xFrame;
74 OUString aResourceURL( ResourceURL );
76 for ( sal_Int32 n = 0; n < Args.getLength(); n++ )
78 if ( Args[n].Name == "ConfigurationSource" )
80 nConfigPropertyIndex = n;
81 Args[n].Value >>= xCfgMgr;
83 else if ( Args[n].Name == "ResourceURL" )
85 nURLPropertyIndex = n;
86 Args[n].Value >>= aResourceURL;
88 else if ( Args[n].Name == "Frame" )
89 Args[n].Value >>= xFrame;
91 if (!aResourceURL.startsWith(ResourceType))
92 throw IllegalArgumentException();
94 // Identify frame and determine document based ui configuration manager/module ui configuration manager
95 if ( xFrame.is() && !xCfgMgr.is() )
97 bool bHasSettings( false );
98 Reference< XModel > xModel;
100 Reference< XController > xController = xFrame->getController();
101 if ( xController.is() )
102 xModel = xController->getModel();
104 if ( xModel.is() )
106 Reference< XUIConfigurationManagerSupplier > xUIConfigurationManagerSupplier( xModel, UNO_QUERY );
107 if ( xUIConfigurationManagerSupplier.is() )
109 xCfgMgr = xUIConfigurationManagerSupplier->getUIConfigurationManager();
110 bHasSettings = xCfgMgr->hasSettings( aResourceURL );
114 if ( !bHasSettings )
116 Reference< css::frame::XModuleManager2 > xModuleManager =
117 ModuleManager::create( _rxContext );
118 OUString aModuleIdentifier = xModuleManager->identify( Reference<XInterface>( xFrame, UNO_QUERY ) );
119 if ( !aModuleIdentifier.isEmpty() )
121 Reference< XModuleUIConfigurationManagerSupplier > xModuleCfgSupplier =
122 theModuleUIConfigurationManagerSupplier::get( _rxContext );
123 xCfgMgr = xModuleCfgSupplier->getUIConfigurationManager( aModuleIdentifier );
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++;
136 Sequence< Any > aPropSeq( nSeqLength );
137 auto aPropSeqRange = asNonConstRange(aPropSeq);
138 for ( sal_Int32 n = 0; n < aPropSeq.getLength(); n++ )
140 PropertyValue aPropValue;
141 if ( n == nURLPropertyIndex )
143 aPropValue.Name = "ResourceURL";
144 aPropValue.Value <<= aResourceURL;
146 else if ( n == nConfigPropertyIndex )
148 aPropValue.Name = "ConfigurationSource";
149 aPropValue.Value <<= xCfgMgr;
151 else
152 aPropValue = Args[n];
154 aPropSeqRange[n] <<= aPropValue;
157 SolarMutexGuard aGuard;
158 Reference< XInitialization > xInit( _xMenuBar, UNO_QUERY );
159 xInit->initialize( aPropSeq );
162 } // namespace framework
164 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
165 com_sun_star_comp_framework_MenuBarFactory_get_implementation(
166 css::uno::XComponentContext *context,
167 css::uno::Sequence<css::uno::Any> const &)
169 return cppu::acquire(new framework::MenuBarFactory(context));
172 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */