1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_framework.hxx"
30 
31 #include <uielement/macrosmenucontroller.hxx>
32 #include <uielement/menubarmanager.hxx>
33 #include <threadhelp/resetableguard.hxx>
34 #include "services.h"
35 #include <classes/resource.hrc>
36 #include <classes/fwkresid.hxx>
37 #include <framework/imageproducer.hxx>
38 #include <com/sun/star/awt/MenuItemStyle.hpp>
39 #include <com/sun/star/beans/PropertyValue.hpp>
40 #include <com/sun/star/beans/XPropertySet.hpp>
41 #include <com/sun/star/container/XContentEnumerationAccess.hpp>
42 #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
43 #include <com/sun/star/frame/XModuleManager.hpp>
44 #include <comphelper/processfactory.hxx>
45 #include <vcl/svapp.hxx>
46 #include <vcl/i18nhelp.hxx>
47 #include <tools/urlobj.hxx>
48 #include <rtl/ustrbuf.hxx>
49 #include <dispatch/uieventloghelper.hxx>
50 #include "helper/mischelper.hxx"
51 #include "helpid.hrc"
52 #include <vos/mutex.hxx>
53 
54 using namespace com::sun::star::uno;
55 using namespace com::sun::star::lang;
56 using namespace com::sun::star::frame;
57 using namespace com::sun::star::beans;
58 using namespace com::sun::star::util;
59 using namespace com::sun::star::style;
60 using namespace com::sun::star::container;
61 using namespace ::com::sun::star::frame;
62 
63 namespace framework
64 {
65 class
66 DEFINE_XSERVICEINFO_MULTISERVICE        (   MacrosMenuController				    ,
67                                             OWeakObject                             ,
68                                             SERVICENAME_POPUPMENUCONTROLLER		    ,
69 											IMPLEMENTATIONNAME_MACROSMENUCONTROLLER
70 										)
71 
72 DEFINE_INIT_SERVICE                     (   MacrosMenuController, {} )
73 
74 MacrosMenuController::MacrosMenuController( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xServiceManager ) :
75 	svt::PopupMenuControllerBase( xServiceManager ),
76     m_xServiceManager( xServiceManager)
77 {
78 }
79 
80 MacrosMenuController::~MacrosMenuController()
81 {
82     OSL_TRACE("calling dtor");
83 }
84 
85 // private function
86 void MacrosMenuController::fillPopupMenu( Reference< css::awt::XPopupMenu >& rPopupMenu )
87 {
88     VCLXPopupMenu* pVCLPopupMenu = (VCLXPopupMenu *)VCLXMenu::GetImplementation( rPopupMenu );
89     PopupMenu*     pPopupMenu    = 0;
90 
91     vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
92 
93     resetPopupMenu( rPopupMenu );
94     if ( pVCLPopupMenu )
95         pPopupMenu = (PopupMenu *)pVCLPopupMenu->GetMenu();
96 
97     if (!pPopupMenu)
98         return;
99 
100     // insert basic
101     String aCommand = String::CreateFromAscii( ".uno:MacroDialog" );
102     String aDisplayName = RetrieveLabelFromCommand( aCommand );
103     pPopupMenu->InsertItem( 2, aDisplayName );
104     pPopupMenu->SetItemCommand( 2, aCommand );
105 
106     // insert providers but not basic or java
107     addScriptItems( pPopupMenu, 4);
108 }
109 
110 // XEventListener
111 void SAL_CALL MacrosMenuController::disposing( const EventObject& ) throw ( RuntimeException )
112 {
113     Reference< css::awt::XMenuListener > xHolder(( OWeakObject *)this, UNO_QUERY );
114 
115     osl::MutexGuard aLock( m_aMutex );
116     OSL_TRACE("disposing");
117     m_xFrame.clear();
118     m_xDispatch.clear();
119     m_xServiceManager.clear();
120 
121     if ( m_xPopupMenu.is() )
122     {
123         m_xPopupMenu->removeMenuListener( Reference< css::awt::XMenuListener >(( OWeakObject *)this, UNO_QUERY ));
124         OSL_TRACE("removed listener");
125     }
126     m_xPopupMenu.clear();
127 }
128 
129 // XStatusListener
130 void SAL_CALL MacrosMenuController::statusChanged( const FeatureStateEvent& ) throw ( RuntimeException )
131 {
132     osl::MutexGuard aLock( m_aMutex );
133     if ( m_xPopupMenu.is() )
134     {
135         fillPopupMenu( m_xPopupMenu );
136     }
137 }
138 
139 // XMenuListener
140 void MacrosMenuController::impl_select(const Reference< XDispatch >& /*_xDispatch*/,const ::com::sun::star::util::URL& aTargetURL)
141 {
142     // need to requery, since we handle more than one type of Command
143     // if we don't do this only .uno:ScriptOrganizer commands are executed
144     Reference< XDispatchProvider > xDispatchProvider( m_xFrame, UNO_QUERY );
145     Reference< XDispatch > xDispatch = xDispatchProvider->queryDispatch( aTargetURL, ::rtl::OUString(), 0 );
146     if( xDispatch.is() )
147     {
148         ExecuteInfo* pExecuteInfo = new ExecuteInfo;
149         pExecuteInfo->xDispatch     = xDispatch;
150         pExecuteInfo->aTargetURL    = aTargetURL;
151         //pExecuteInfo->aArgs         = aArgs;
152         if(::comphelper::UiEventsLogger::isEnabled()) //#i88653#
153             UiEventLogHelper(::rtl::OUString::createFromAscii("MacrosMenuController")).log(m_xServiceManager, m_xFrame, aTargetURL, pExecuteInfo->aArgs);
154 //                xDispatch->dispatch( aTargetURL, aArgs );
155         Application::PostUserEvent( STATIC_LINK(0, MacrosMenuController , ExecuteHdl_Impl), pExecuteInfo );
156     }
157     else
158     {
159     }
160 }
161 
162 
163 IMPL_STATIC_LINK_NOINSTANCE( MacrosMenuController, ExecuteHdl_Impl, ExecuteInfo*, pExecuteInfo )
164 {
165    try
166    {
167        // Asynchronous execution as this can lead to our own destruction!
168        // Framework can recycle our current frame and the layout manager disposes all user interface
169        // elements if a component gets detached from its frame!
170        pExecuteInfo->xDispatch->dispatch( pExecuteInfo->aTargetURL, pExecuteInfo->aArgs );
171    }
172    catch ( Exception& )
173    {
174    }
175    delete pExecuteInfo;
176    return 0;
177 }
178 
179 String MacrosMenuController::RetrieveLabelFromCommand( const String& aCmdURL )
180 {
181     sal_Bool bModuleIdentified = m_aModuleIdentifier.getLength() != 0;
182     return framework::RetrieveLabelFromCommand(aCmdURL,m_xServiceManager,m_xUICommandLabels,m_xFrame,m_aModuleIdentifier,bModuleIdentified,"Label");
183 }
184 
185 void MacrosMenuController::addScriptItems( PopupMenu* pPopupMenu, sal_uInt16 startItemId )
186 {
187     const String aCmdBase = String::CreateFromAscii( ".uno:ScriptOrganizer?ScriptOrganizer.Language:string=" );
188     const String ellipsis = String::CreateFromAscii( "..." );
189     const ::rtl::OUString providerKey =
190     ::rtl::OUString::createFromAscii("com.sun.star.script.provider.ScriptProviderFor" );
191     const ::rtl::OUString languageProviderName =
192         ::rtl::OUString::createFromAscii("com.sun.star.script.provider.LanguageScriptProvider" );
193     sal_uInt16 itemId = startItemId;
194     Reference< XContentEnumerationAccess > xEnumAccess = Reference< XContentEnumerationAccess >( m_xServiceManager, UNO_QUERY_THROW );
195     Reference< XEnumeration > xEnum = xEnumAccess->createContentEnumeration ( languageProviderName );
196 
197     while ( xEnum->hasMoreElements() )
198     {
199         Reference< XServiceInfo > xServiceInfo;
200         if ( sal_False == ( xEnum->nextElement() >>= xServiceInfo ) )
201         {
202             break;
203         }
204         Sequence< ::rtl::OUString > serviceNames = xServiceInfo->getSupportedServiceNames();
205 
206         if ( serviceNames.getLength() > 0 )
207         {
208             for ( sal_Int32 index = 0; index < serviceNames.getLength(); index++ )
209             {
210                 if ( serviceNames[ index ].indexOf( providerKey ) == 0 )
211                 {
212                     ::rtl::OUString serviceName = serviceNames[ index ];
213                     String aCommand =  aCmdBase;
214                     String aDisplayName = String( serviceName.copy( providerKey.getLength() ) );
215                     if( aDisplayName.Equals( String::CreateFromAscii( "Java" ) ) || aDisplayName.Equals( String::CreateFromAscii( "Basic" ) ) )
216                     {
217                         // no entries for Java & Basic added elsewhere
218                         break;
219                     }
220                     aCommand.Append( aDisplayName );
221                     aDisplayName.Append( ellipsis );
222                     pPopupMenu->InsertItem( itemId, aDisplayName );
223                     pPopupMenu->SetItemCommand( itemId, aCommand );
224                     itemId++;
225                     break;
226                 }
227             }
228         }
229     }
230 }
231 
232 }
233