LibreOffice Module framework (master) 1
macrosmenucontroller.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#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>
28#include <osl/mutex.hxx>
31
32using namespace com::sun::star::uno;
33using namespace com::sun::star::lang;
34using namespace com::sun::star::frame;
35using namespace com::sun::star::beans;
36using namespace com::sun::star::util;
37using namespace com::sun::star::style;
38using namespace com::sun::star::container;
39
40namespace framework
41{
42
43// XInterface, XTypeProvider, XServiceInfo
44
46{
47 return "com.sun.star.comp.framework.MacrosMenuController";
48}
49
50sal_Bool SAL_CALL MacrosMenuController::supportsService( const OUString& sServiceName )
51{
53}
54
55css::uno::Sequence< OUString > SAL_CALL MacrosMenuController::getSupportedServiceNames()
56{
58}
59
60MacrosMenuController::MacrosMenuController( const css::uno::Reference< css::uno::XComponentContext >& xContext ) :
61 svt::PopupMenuControllerBase( xContext ),
62 m_xContext( xContext)
63{
64}
65
67{
68}
69
70// private function
72{
73 bool bMacrosDisabled = officecfg::Office::Common::Security::Scripting::DisableMacrosExecution::get();
74 if (bMacrosDisabled)
75 return;
76
77 SolarMutexGuard aSolarMutexGuard;
78
79 resetPopupMenu(rPopupMenu);
80 assert(rPopupMenu->getItemCount() == 0);
81
82 // insert basic
83 OUString aCommand(".uno:MacroDialog");
86 rPopupMenu->insertItem(2, aDisplayName, 0, 0);
87 rPopupMenu->setCommand(2, aCommand);
88
89 // insert providers but not basic or java
90 addScriptItems(rPopupMenu, 4);
91}
92
93// XEventListener
94void SAL_CALL MacrosMenuController::disposing( const EventObject& )
95{
97
98 std::unique_lock aLock( m_aMutex );
99 m_xFrame.clear();
100 m_xDispatch.clear();
101 m_xContext.clear();
102
103 if ( m_xPopupMenu.is() )
104 {
105 m_xPopupMenu->removeMenuListener( Reference< css::awt::XMenuListener >(this) );
106 }
107 m_xPopupMenu.clear();
108}
109
110// XStatusListener
111void SAL_CALL MacrosMenuController::statusChanged( const FeatureStateEvent& )
112{
113 std::unique_lock aLock( m_aMutex );
114 if ( m_xPopupMenu.is() )
115 {
116 fillPopupMenu( m_xPopupMenu );
117 }
118}
119
120void MacrosMenuController::addScriptItems(const Reference<css::awt::XPopupMenu>& rPopupMenu, sal_uInt16 startItemId)
121{
122 static constexpr OUStringLiteral aCmdBase(u".uno:ScriptOrganizer?ScriptOrganizer.Language:string=");
123 static constexpr OUStringLiteral ellipsis( u"..." );
124 static constexpr OUStringLiteral providerKey(u"com.sun.star.script.provider.ScriptProviderFor");
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" );
128
129 sal_Int16 nPos = rPopupMenu->getItemCount();
130
131 while ( xEnum->hasMoreElements() )
132 {
133 Reference< XServiceInfo > xServiceInfo;
134 if ( !( xEnum->nextElement() >>= xServiceInfo ) )
135 {
136 break;
137 }
138 const Sequence< OUString > serviceNames = xServiceInfo->getSupportedServiceNames();
139
140 for ( OUString const & serviceName : serviceNames )
141 {
142 if ( serviceName.startsWith( providerKey ) )
143 {
144 OUString aCommand = aCmdBase;
145 OUString aDisplayName = serviceName.copy( providerKey.getLength() );
146 if( aDisplayName == "Java" || aDisplayName == "Basic" )
147 {
148 // no entries for Java & Basic added elsewhere
149 break;
150 }
151 aCommand += aDisplayName;
152 aDisplayName += ellipsis;
153 rPopupMenu->insertItem(itemId, aDisplayName, 0, nPos++);
154 rPopupMenu->setCommand(itemId, aCommand);
155 itemId++;
156 break;
157 }
158 }
159 }
160}
161
162}
163
164extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
166 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const& )
167{
168 return cppu::acquire(new framework::MacrosMenuController(context));
169}
170
171/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
PropertiesInfo aProperties
constexpr OUStringLiteral sServiceName
css::uno::Reference< css::lang::XComponent > m_xFrame
css::uno::Reference< css::frame::XDispatch > m_xDispatch
MacrosMenuController(const css::uno::Reference< css::uno::XComponentContext > &xContext)
void addScriptItems(const css::uno::Reference< css::awt::XPopupMenu > &rPopupMenu, sal_uInt16 startItemId)
virtual sal_Bool SAL_CALL supportsService(const OUString &sServiceName) override
css::uno::Reference< css::uno::XComponentContext > m_xContext
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
virtual void SAL_CALL disposing(const css::lang::EventObject &Source) override
virtual void SAL_CALL statusChanged(const css::frame::FeatureStateEvent &Event) override
void fillPopupMenu(const css::uno::Reference< css::awt::XPopupMenu > &rPopupMenu)
virtual OUString SAL_CALL getImplementationName() override
float u
css::uno::Reference< css::uno::XComponentContext > m_xContext
sal_uInt16 nPos
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * framework_MacrosMenuController_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
constexpr OUStringLiteral SERVICENAME_POPUPMENUCONTROLLER
Definition: services.h:33
Sequence< beans::PropertyValue > GetCommandProperties(const OUString &rsCommandName, const OUString &rsModuleName)
OUString GetMenuLabelForCommand(const css::uno::Sequence< css::beans::PropertyValue > &rProperties)
OUString aCommand
unsigned char sal_Bool
std::mutex m_aMutex