Avoid potential negative array index access to cached text.
[LibreOffice.git] / framework / source / uielement / macrosmenucontroller.cxx
blob0cc8fba505c742e2e15e08727cb260507b33c4d1
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 <uielement/macrosmenucontroller.hxx>
21 #include <services.h>
22 #include <com/sun/star/container/XContentEnumerationAccess.hpp>
23 #include <com/sun/star/uno/XComponentContext.hpp>
24 #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
25 #include <officecfg/Office/Common.hxx>
26 #include <vcl/svapp.hxx>
27 #include <vcl/commandinfoprovider.hxx>
28 #include <osl/mutex.hxx>
29 #include <toolkit/awt/vclxmenu.hxx>
30 #include <cppuhelper/supportsservice.hxx>
32 using namespace com::sun::star::uno;
33 using namespace com::sun::star::lang;
34 using namespace com::sun::star::frame;
35 using namespace com::sun::star::beans;
36 using namespace com::sun::star::util;
37 using namespace com::sun::star::style;
38 using namespace com::sun::star::container;
40 namespace framework
43 // XInterface, XTypeProvider, XServiceInfo
45 OUString SAL_CALL MacrosMenuController::getImplementationName()
47 return "com.sun.star.comp.framework.MacrosMenuController";
50 sal_Bool SAL_CALL MacrosMenuController::supportsService( const OUString& sServiceName )
52 return cppu::supportsService(this, sServiceName);
55 css::uno::Sequence< OUString > SAL_CALL MacrosMenuController::getSupportedServiceNames()
57 return { SERVICENAME_POPUPMENUCONTROLLER };
60 MacrosMenuController::MacrosMenuController( const css::uno::Reference< css::uno::XComponentContext >& xContext ) :
61 svt::PopupMenuControllerBase( xContext ),
62 m_xContext( xContext)
66 MacrosMenuController::~MacrosMenuController()
70 // private function
71 void MacrosMenuController::fillPopupMenu( Reference< css::awt::XPopupMenu > const & rPopupMenu )
73 bool bMacrosDisabled = officecfg::Office::Common::Security::Scripting::DisableMacrosExecution::get();
74 if (bMacrosDisabled)
75 return;
77 SolarMutexGuard aSolarMutexGuard;
79 resetPopupMenu(rPopupMenu);
80 assert(rPopupMenu->getItemCount() == 0);
82 // insert basic
83 OUString aCommand(".uno:MacroDialog");
84 auto aProperties = vcl::CommandInfoProvider::GetCommandProperties(aCommand, m_aModuleName);
85 OUString aDisplayName = vcl::CommandInfoProvider::GetMenuLabelForCommand(aProperties);
86 rPopupMenu->insertItem(2, aDisplayName, 0, 0);
87 rPopupMenu->setCommand(2, aCommand);
89 // insert providers but not basic or java
90 addScriptItems(rPopupMenu, 4);
93 // XEventListener
94 void SAL_CALL MacrosMenuController::disposing( const EventObject& )
96 Reference< css::awt::XMenuListener > xHolder(this);
98 std::unique_lock aLock( m_aMutex );
99 m_xFrame.clear();
100 m_xDispatch.clear();
101 m_xContext.clear();
103 if ( m_xPopupMenu.is() )
105 m_xPopupMenu->removeMenuListener( Reference< css::awt::XMenuListener >(this) );
107 m_xPopupMenu.clear();
110 // XStatusListener
111 void SAL_CALL MacrosMenuController::statusChanged( const FeatureStateEvent& )
113 std::unique_lock aLock( m_aMutex );
114 if ( m_xPopupMenu.is() )
116 fillPopupMenu( m_xPopupMenu );
120 void MacrosMenuController::addScriptItems(const Reference<css::awt::XPopupMenu>& rPopupMenu, sal_uInt16 startItemId)
122 static constexpr OUStringLiteral aCmdBase(u".uno:ScriptOrganizer?ScriptOrganizer.Language:string=");
123 static constexpr OUStringLiteral ellipsis( u"..." );
124 static constexpr OUString providerKey(u"com.sun.star.script.provider.ScriptProviderFor"_ustr);
125 sal_uInt16 itemId = startItemId;
126 Reference< XContentEnumerationAccess > xEnumAccess( m_xContext->getServiceManager(), UNO_QUERY_THROW );
127 Reference< XEnumeration > xEnum = xEnumAccess->createContentEnumeration ( "com.sun.star.script.provider.LanguageScriptProvider" );
129 sal_Int16 nPos = rPopupMenu->getItemCount();
131 while ( xEnum->hasMoreElements() )
133 Reference< XServiceInfo > xServiceInfo;
134 if ( !( xEnum->nextElement() >>= xServiceInfo ) )
136 break;
138 const Sequence< OUString > serviceNames = xServiceInfo->getSupportedServiceNames();
140 for ( OUString const & serviceName : serviceNames )
142 if ( serviceName.startsWith( providerKey ) )
144 OUString aCommand = aCmdBase;
145 OUString aDisplayName = serviceName.copy( providerKey.getLength() );
146 if( aDisplayName == "Java" || aDisplayName == "Basic" )
148 // no entries for Java & Basic added elsewhere
149 break;
151 aCommand += aDisplayName;
152 aDisplayName += ellipsis;
153 rPopupMenu->insertItem(itemId, aDisplayName, 0, nPos++);
154 rPopupMenu->setCommand(itemId, aCommand);
155 itemId++;
156 break;
164 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
165 framework_MacrosMenuController_get_implementation(
166 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const& )
168 return cppu::acquire(new framework::MacrosMenuController(context));
171 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */