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