1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_framework.hxx"
26 #include <uifactory/menubarfactory.hxx>
27 
28 //_________________________________________________________________________________________________________________
29 //	my own includes
30 //_________________________________________________________________________________________________________________
31 #include <threadhelp/resetableguard.hxx>
32 #include "services.h"
33 #include <uielement/menubarwrapper.hxx>
34 
35 //_________________________________________________________________________________________________________________
36 //	interface includes
37 //_________________________________________________________________________________________________________________
38 #include <com/sun/star/util/XURLTransformer.hpp>
39 #include <com/sun/star/frame/XFrame.hpp>
40 #include <com/sun/star/frame/XModel.hpp>
41 #include <com/sun/star/lang/XInitialization.hpp>
42 #include <com/sun/star/ui/XModuleUIConfigurationManagerSupplier.hpp>
43 
44 #ifndef _COM_SUN_STAR_UI_XUICONFIGURATIONMANAGERSUPLLIER_HPP_
45 #include <com/sun/star/ui/XUIConfigurationManagerSupplier.hpp>
46 #endif
47 
48 //_________________________________________________________________________________________________________________
49 //	includes of other projects
50 //_________________________________________________________________________________________________________________
51 
52 #ifndef _VCL_MENU_HXX_
53 #include <vcl/menu.hxx>
54 #endif
55 #include <vcl/svapp.hxx>
56 #include <tools/urlobj.hxx>
57 #include <rtl/ustrbuf.hxx>
58 #include <rtl/logfile.hxx>
59 
60 //_________________________________________________________________________________________________________________
61 //	Defines
62 //_________________________________________________________________________________________________________________
63 //
64 
65 using namespace com::sun::star::uno;
66 using namespace com::sun::star::lang;
67 using namespace com::sun::star::frame;
68 using namespace com::sun::star::beans;
69 using namespace com::sun::star::util;
70 using namespace ::com::sun::star::ui;
71 
72 namespace framework
73 {
74 
75 //*****************************************************************************************************************
76 //	XInterface, XTypeProvider, XServiceInfo
77 //*****************************************************************************************************************
DEFINE_XSERVICEINFO_ONEINSTANCESERVICE(MenuBarFactory,::cppu::OWeakObject,SERVICENAME_MENUBARFACTORY,IMPLEMENTATIONNAME_MENUBARFACTORY)78 DEFINE_XSERVICEINFO_ONEINSTANCESERVICE  (   MenuBarFactory				                    ,
79                                             ::cppu::OWeakObject							    ,
80                                             SERVICENAME_MENUBARFACTORY	                    ,
81 											IMPLEMENTATIONNAME_MENUBARFACTORY
82 										)
83 
84 DEFINE_INIT_SERVICE                     (   MenuBarFactory, {} )
85 
86 MenuBarFactory::MenuBarFactory( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xServiceManager ) :
87     ThreadHelpBase()
88     , m_xServiceManager( xServiceManager )
89     , m_xModuleManager( xServiceManager->createInstance( SERVICENAME_MODULEMANAGER ), UNO_QUERY )
90 {
91 }
MenuBarFactory(const::com::sun::star::uno::Reference<::com::sun::star::lang::XMultiServiceFactory> & xServiceManager,bool)92 MenuBarFactory::MenuBarFactory( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xServiceManager,bool ) :
93     ThreadHelpBase(&Application::GetSolarMutex())
94     , m_xServiceManager( xServiceManager )
95     , m_xModuleManager( xServiceManager->createInstance( SERVICENAME_MODULEMANAGER ), UNO_QUERY )
96 {
97 }
98 
~MenuBarFactory()99 MenuBarFactory::~MenuBarFactory()
100 {
101 }
102 
103 // XUIElementFactory
createUIElement(const::rtl::OUString & ResourceURL,const Sequence<PropertyValue> & Args)104 Reference< XUIElement > SAL_CALL MenuBarFactory::createUIElement(
105     const ::rtl::OUString& ResourceURL,
106     const Sequence< PropertyValue >& Args )
107 throw ( ::com::sun::star::container::NoSuchElementException, ::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException )
108 {
109     // SAFE
110     ResetableGuard aLock( m_aLock );
111     MenuBarWrapper* pMenuBarWrapper = new MenuBarWrapper( m_xServiceManager );
112     Reference< ::com::sun::star::ui::XUIElement > xMenuBar( (OWeakObject *)pMenuBarWrapper, UNO_QUERY );
113     Reference< ::com::sun::star::frame::XModuleManager > xModuleManager = m_xModuleManager;
114     aLock.unlock();
115     CreateUIElement(ResourceURL,Args,"MenuOnly","private:resource/menubar/",xMenuBar,xModuleManager,m_xServiceManager);
116     return xMenuBar;
117 }
CreateUIElement(const::rtl::OUString & ResourceURL,const Sequence<PropertyValue> & Args,const char * _pExtraMode,const char * _pAsciiName,const Reference<::com::sun::star::ui::XUIElement> & _xMenuBar,const::com::sun::star::uno::Reference<::com::sun::star::frame::XModuleManager> & _xModuleManager,const::com::sun::star::uno::Reference<::com::sun::star::lang::XMultiServiceFactory> & _xServiceManager)118 void MenuBarFactory::CreateUIElement(const ::rtl::OUString& ResourceURL
119                                      , const Sequence< PropertyValue >& Args
120                                      ,const char* _pExtraMode
121                                      ,const char* _pAsciiName
122                                      ,const Reference< ::com::sun::star::ui::XUIElement >& _xMenuBar
123                                      ,const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XModuleManager >& _xModuleManager
124                                      ,const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _xServiceManager)
125 {
126     Reference< XUIConfigurationManager > xCfgMgr;
127     Reference< XUIConfigurationManager > xConfigSource;
128     Reference< XFrame >                  xFrame;
129     rtl::OUString                        aResourceURL( ResourceURL );
130     sal_Bool                             bPersistent( sal_True );
131     sal_Bool                             bExtraMode( sal_False );
132 
133     for ( sal_Int32 n = 0; n < Args.getLength(); n++ )
134     {
135         if ( Args[n].Name.equalsAscii( "ConfigurationSource" ))
136             Args[n].Value >>= xConfigSource;
137         else if ( Args[n].Name.equalsAscii( "Frame" ))
138             Args[n].Value >>= xFrame;
139         else if ( Args[n].Name.equalsAscii( "ResourceURL" ))
140             Args[n].Value >>= aResourceURL;
141         else if ( Args[n].Name.equalsAscii( "Persistent" ))
142             Args[n].Value >>= bPersistent;
143         else if ( _pExtraMode && Args[n].Name.equalsAscii( _pExtraMode ))
144             Args[n].Value >>= bExtraMode;
145     } // for ( sal_Int32 n = 0; n < Args.getLength(); n++ )
146     if ( aResourceURL.indexOf( rtl::OUString::createFromAscii(_pAsciiName)) != 0 )
147         throw IllegalArgumentException();
148 
149     // Identify frame and determine document based ui configuration manager/module ui configuration manager
150     if ( xFrame.is() && !xConfigSource.is() )
151     {
152         bool bHasSettings( false );
153         Reference< XModel > xModel;
154 
155         Reference< XController > xController = xFrame->getController();
156         if ( xController.is() )
157             xModel = xController->getModel();
158 
159         if ( xModel.is() )
160         {
161             Reference< XUIConfigurationManagerSupplier > xUIConfigurationManagerSupplier( xModel, UNO_QUERY );
162             if ( xUIConfigurationManagerSupplier.is() )
163             {
164                 xCfgMgr = xUIConfigurationManagerSupplier->getUIConfigurationManager();
165                 bHasSettings = xCfgMgr->hasSettings( aResourceURL );
166             }
167         }
168 
169         if ( !bHasSettings )
170         {
171             rtl::OUString aModuleIdentifier = _xModuleManager->identify( Reference< XInterface >( xFrame, UNO_QUERY ));
172             if ( aModuleIdentifier.getLength() )
173             {
174                 Reference< ::com::sun::star::ui::XModuleUIConfigurationManagerSupplier > xModuleCfgSupplier(
175                     _xServiceManager->createInstance( SERVICENAME_MODULEUICONFIGURATIONMANAGERSUPPLIER ), UNO_QUERY );
176                 xCfgMgr = xModuleCfgSupplier->getUIConfigurationManager( aModuleIdentifier );
177                 bHasSettings = xCfgMgr->hasSettings( aResourceURL );
178             }
179         }
180     }
181 
182     PropertyValue aPropValue;
183     Sequence< Any > aPropSeq( _pExtraMode ? 5 : 4);
184     aPropValue.Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Frame" ));
185     aPropValue.Value <<= xFrame;
186     aPropSeq[0] <<= aPropValue;
187     aPropValue.Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ConfigurationSource" ));
188     aPropValue.Value <<= xCfgMgr;
189     aPropSeq[1] <<= aPropValue;
190     aPropValue.Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ResourceURL" ));
191     aPropValue.Value <<= aResourceURL;
192     aPropSeq[2] <<= aPropValue;
193     aPropValue.Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Persistent" ));
194     aPropValue.Value <<= bPersistent;
195     aPropSeq[3] <<= aPropValue;
196     if ( _pExtraMode )
197     {
198         aPropValue.Name = rtl::OUString::createFromAscii(_pExtraMode);
199         aPropValue.Value <<= bExtraMode;
200         aPropSeq[4] <<= aPropValue;
201     }
202 
203     vos::OGuard	aGuard( Application::GetSolarMutex() );
204     Reference< XInitialization > xInit( _xMenuBar, UNO_QUERY );
205     xInit->initialize( aPropSeq );
206 }
207 
208 } // namespace framework
209