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
20 #include <uielement/macrosmenucontroller.hxx>
21 #include <uielement/menubarmanager.hxx>
22 #include <services.h>
23 #include <com/sun/star/awt/MenuItemStyle.hpp>
24 #include <com/sun/star/beans/PropertyValue.hpp>
25 #include <com/sun/star/beans/XPropertySet.hpp>
26 #include <com/sun/star/container/XContentEnumerationAccess.hpp>
27 #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
28 #include <officecfg/Office/Common.hxx>
29 #include <toolkit/awt/vclxmenu.hxx>
30 #include <vcl/svapp.hxx>
31 #include <vcl/i18nhelp.hxx>
32 #include <vcl/commandinfoprovider.hxx>
33 #include <rtl/ustrbuf.hxx>
34 #include <helper/mischelper.hxx>
35 #include <osl/mutex.hxx>
36
37 using namespace com::sun::star::uno;
38 using namespace com::sun::star::lang;
39 using namespace com::sun::star::frame;
40 using namespace com::sun::star::beans;
41 using namespace com::sun::star::util;
42 using namespace com::sun::star::style;
43 using namespace com::sun::star::container;
44
45 namespace framework
46 {
47 class
DEFINE_XSERVICEINFO_MULTISERVICE_2(MacrosMenuController,OWeakObject,SERVICENAME_POPUPMENUCONTROLLER,IMPLEMENTATIONNAME_MACROSMENUCONTROLLER)48 DEFINE_XSERVICEINFO_MULTISERVICE_2 ( MacrosMenuController ,
49 OWeakObject ,
50 SERVICENAME_POPUPMENUCONTROLLER ,
51 IMPLEMENTATIONNAME_MACROSMENUCONTROLLER
52 )
53
54 DEFINE_INIT_SERVICE ( MacrosMenuController, {} )
55
56 MacrosMenuController::MacrosMenuController( const css::uno::Reference< css::uno::XComponentContext >& xContext ) :
57 svt::PopupMenuControllerBase( xContext ),
58 m_xContext( xContext)
59 {
60 }
61
~MacrosMenuController()62 MacrosMenuController::~MacrosMenuController()
63 {
64 }
65
66 // private function
fillPopupMenu(Reference<css::awt::XPopupMenu> const & rPopupMenu)67 void MacrosMenuController::fillPopupMenu( Reference< css::awt::XPopupMenu > const & rPopupMenu )
68 {
69 bool bMacrosDisabled = officecfg::Office::Common::Security::Scripting::DisableMacrosExecution::get();
70 if (bMacrosDisabled)
71 return;
72
73 VCLXPopupMenu* pVCLPopupMenu = static_cast<VCLXPopupMenu *>(comphelper::getUnoTunnelImplementation<VCLXMenu>( rPopupMenu ));
74 PopupMenu* pPopupMenu = nullptr;
75
76 SolarMutexGuard aSolarMutexGuard;
77
78 resetPopupMenu( rPopupMenu );
79 if ( pVCLPopupMenu )
80 pPopupMenu = static_cast<PopupMenu *>(pVCLPopupMenu->GetMenu());
81
82 if (!pPopupMenu)
83 return;
84
85 // insert basic
86 OUString aCommand(".uno:MacroDialog");
87 auto aProperties = vcl::CommandInfoProvider::GetCommandProperties(aCommand, m_aModuleName);
88 OUString aDisplayName = vcl::CommandInfoProvider::GetMenuLabelForCommand(aProperties);
89 pPopupMenu->InsertItem( 2, aDisplayName );
90 pPopupMenu->SetItemCommand( 2, aCommand );
91
92 // insert providers but not basic or java
93 addScriptItems( pPopupMenu, 4);
94 }
95
96 // XEventListener
disposing(const EventObject &)97 void SAL_CALL MacrosMenuController::disposing( const EventObject& )
98 {
99 Reference< css::awt::XMenuListener > xHolder(static_cast<OWeakObject *>(this), UNO_QUERY );
100
101 osl::MutexGuard aLock( m_aMutex );
102 m_xFrame.clear();
103 m_xDispatch.clear();
104 m_xContext.clear();
105
106 if ( m_xPopupMenu.is() )
107 {
108 m_xPopupMenu->removeMenuListener( Reference< css::awt::XMenuListener >(static_cast<OWeakObject *>(this), UNO_QUERY ));
109 }
110 m_xPopupMenu.clear();
111 }
112
113 // XStatusListener
statusChanged(const FeatureStateEvent &)114 void SAL_CALL MacrosMenuController::statusChanged( const FeatureStateEvent& )
115 {
116 osl::MutexGuard aLock( m_aMutex );
117 if ( m_xPopupMenu.is() )
118 {
119 fillPopupMenu( m_xPopupMenu );
120 }
121 }
122
addScriptItems(PopupMenu * pPopupMenu,sal_uInt16 startItemId)123 void MacrosMenuController::addScriptItems( PopupMenu* pPopupMenu, sal_uInt16 startItemId )
124 {
125 const OUString aCmdBase(".uno:ScriptOrganizer?ScriptOrganizer.Language:string=");
126 const OUString ellipsis( "..." );
127 const OUString providerKey("com.sun.star.script.provider.ScriptProviderFor");
128 const OUString languageProviderName("com.sun.star.script.provider.LanguageScriptProvider");
129 sal_uInt16 itemId = startItemId;
130 Reference< XContentEnumerationAccess > xEnumAccess( m_xContext->getServiceManager(), UNO_QUERY_THROW );
131 Reference< XEnumeration > xEnum = xEnumAccess->createContentEnumeration ( languageProviderName );
132
133 while ( xEnum->hasMoreElements() )
134 {
135 Reference< XServiceInfo > xServiceInfo;
136 if ( !( xEnum->nextElement() >>= xServiceInfo ) )
137 {
138 break;
139 }
140 Sequence< OUString > serviceNames = xServiceInfo->getSupportedServiceNames();
141
142 if ( serviceNames.getLength() > 0 )
143 {
144 for ( sal_Int32 index = 0; index < serviceNames.getLength(); index++ )
145 {
146 if ( serviceNames[ index ].startsWith( providerKey ) )
147 {
148 OUString serviceName = serviceNames[ index ];
149 OUString aCommand = aCmdBase;
150 OUString aDisplayName = serviceName.copy( providerKey.getLength() );
151 if( aDisplayName == "Java" || aDisplayName == "Basic" )
152 {
153 // no entries for Java & Basic added elsewhere
154 break;
155 }
156 aCommand += aDisplayName;
157 aDisplayName += ellipsis;
158 pPopupMenu->InsertItem( itemId, aDisplayName );
159 pPopupMenu->SetItemCommand( itemId, aCommand );
160 itemId++;
161 break;
162 }
163 }
164 }
165 }
166 }
167
168 }
169
170 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
171