update dev300-m58
[ooovba.git] / framework / source / uifactory / addonstoolboxfactory.cxx
blobc41213d6d207f4ffe57713bd3503950501706a85
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: addonstoolboxfactory.cxx,v $
10 * $Revision: 1.10 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_framework.hxx"
33 #include <uifactory/addonstoolboxfactory.hxx>
35 //_________________________________________________________________________________________________________________
36 // my own includes
37 //_________________________________________________________________________________________________________________
38 #include <uielement/addonstoolbarwrapper.hxx>
39 #include <threadhelp/resetableguard.hxx>
41 //_________________________________________________________________________________________________________________
42 // interface includes
43 //_________________________________________________________________________________________________________________
44 #include <com/sun/star/util/XURLTransformer.hpp>
45 #include <com/sun/star/frame/XFrame.hpp>
46 #include <com/sun/star/frame/XModel.hpp>
47 #include <com/sun/star/lang/XInitialization.hpp>
48 #include <com/sun/star/ui/XModuleUIConfigurationManagerSupplier.hpp>
50 #ifndef _COM_SUN_STAR_UI_XUICONFIGURATIONMANAGERSUPLLIER_HPP_
51 #include <com/sun/star/ui/XUIConfigurationManagerSupplier.hpp>
52 #endif
54 //_________________________________________________________________________________________________________________
55 // includes of other projects
56 //_________________________________________________________________________________________________________________
57 #include <vcl/svapp.hxx>
58 #include <tools/urlobj.hxx>
59 #include <rtl/ustrbuf.hxx>
61 //_________________________________________________________________________________________________________________
62 // Defines
63 //_________________________________________________________________________________________________________________
66 using namespace com::sun::star::uno;
67 using namespace com::sun::star::lang;
68 using namespace com::sun::star::frame;
69 using namespace com::sun::star::beans;
70 using namespace com::sun::star::util;
71 using namespace ::com::sun::star::ui;
73 namespace framework
76 //*****************************************************************************************************************
77 // XInterface, XTypeProvider, XServiceInfo
78 //*****************************************************************************************************************
79 DEFINE_XSERVICEINFO_ONEINSTANCESERVICE ( AddonsToolBoxFactory ,
80 ::cppu::OWeakObject ,
81 SERVICENAME_TOOLBARFACTORY ,
82 IMPLEMENTATIONNAME_ADDONSTOOLBARFACTORY
85 DEFINE_INIT_SERVICE ( AddonsToolBoxFactory, {} )
87 AddonsToolBoxFactory::AddonsToolBoxFactory(
88 const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xServiceManager ) :
89 ThreadHelpBase( &Application::GetSolarMutex() )
90 , m_xServiceManager( xServiceManager )
91 , m_xModuleManager( xServiceManager->createInstance(SERVICENAME_MODULEMANAGER),UNO_QUERY )
95 AddonsToolBoxFactory::~AddonsToolBoxFactory()
99 static sal_Bool IsCorrectContext( const ::rtl::OUString& rModuleIdentifier, const rtl::OUString& aContextList )
101 if ( aContextList.getLength() == 0 )
102 return sal_True;
104 if ( rModuleIdentifier.getLength() > 0 )
106 sal_Int32 nIndex = aContextList.indexOf( rModuleIdentifier );
107 return ( nIndex >= 0 );
110 return sal_False;
113 sal_Bool AddonsToolBoxFactory::hasButtonsInContext(
114 const Sequence< Sequence< PropertyValue > >& rPropSeqSeq,
115 const Reference< XFrame >& rFrame )
117 ::rtl::OUString aModuleIdentifier;
120 aModuleIdentifier = m_xModuleManager->identify( rFrame );
122 catch ( RuntimeException& )
124 throw;
126 catch ( Exception& )
130 // Check before we create a toolbar that we have at least one button in
131 // the current frame context.
132 for ( sal_uInt32 i = 0; i < (sal_uInt32)rPropSeqSeq.getLength(); i++ )
134 sal_Bool bIsButton( sal_True );
135 sal_Bool bIsCorrectContext( sal_False );
136 sal_uInt32 nPropChecked( 0 );
138 const Sequence< PropertyValue >& rPropSeq = rPropSeqSeq[i];
139 for ( sal_uInt32 j = 0; j < (sal_uInt32)rPropSeq.getLength(); j++ )
141 if ( rPropSeq[j].Name.equalsAsciiL( "Context", 7 ))
143 ::rtl::OUString aContextList;
144 if ( rPropSeq[j].Value >>= aContextList )
145 bIsCorrectContext = IsCorrectContext( aModuleIdentifier, aContextList );
146 nPropChecked++;
148 else if ( rPropSeq[j].Name.equalsAsciiL( "URL", 3 ))
150 ::rtl::OUString aURL;
151 rPropSeq[j].Value >>= aURL;
152 bIsButton = !aURL.equalsAsciiL( "private:separator", 17 );
153 nPropChecked++;
156 if ( nPropChecked == 2 )
157 break;
160 if ( bIsButton && bIsCorrectContext )
161 return sal_True;
164 return sal_False;
167 // XUIElementFactory
168 Reference< XUIElement > SAL_CALL AddonsToolBoxFactory::createUIElement(
169 const ::rtl::OUString& ResourceURL,
170 const Sequence< PropertyValue >& Args )
171 throw ( ::com::sun::star::container::NoSuchElementException,
172 ::com::sun::star::lang::IllegalArgumentException,
173 ::com::sun::star::uno::RuntimeException )
175 // SAFE
176 ResetableGuard aLock( m_aLock );
178 Sequence< Sequence< PropertyValue > > aConfigData;
179 Reference< XFrame > xFrame;
180 rtl::OUString aResourceURL( ResourceURL );
182 for ( sal_Int32 n = 0; n < Args.getLength(); n++ )
184 if ( Args[n].Name.equalsAscii( "ConfigurationData" ))
185 Args[n].Value >>= aConfigData;
186 else if ( Args[n].Name.equalsAscii( "Frame" ))
187 Args[n].Value >>= xFrame;
188 else if ( Args[n].Name.equalsAscii( "ResourceURL" ))
189 Args[n].Value >>= aResourceURL;
192 if ( aResourceURL.indexOf( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "private:resource/toolbar/addon_" ))) != 0 )
193 throw IllegalArgumentException();
195 // Identify frame and determine module identifier to look for context based buttons
196 Reference< ::com::sun::star::ui::XUIElement > xToolBar;
197 if ( xFrame.is() &&
198 ( aConfigData.getLength()> 0 ) &&
199 hasButtonsInContext( aConfigData, xFrame ))
201 PropertyValue aPropValue;
202 Sequence< Any > aPropSeq( 3 );
203 aPropValue.Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Frame" ));
204 aPropValue.Value <<= xFrame;
205 aPropSeq[0] <<= aPropValue;
206 aPropValue.Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ConfigurationData" ));
207 aPropValue.Value <<= aConfigData;
208 aPropSeq[1] <<= aPropValue;
209 aPropValue.Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ResourceURL" ));
210 aPropValue.Value <<= aResourceURL;
211 aPropSeq[2] <<= aPropValue;
213 vos::OGuard aGuard( Application::GetSolarMutex() );
214 AddonsToolBarWrapper* pToolBarWrapper = new AddonsToolBarWrapper( m_xServiceManager );
215 xToolBar = Reference< ::com::sun::star::ui::XUIElement >( (OWeakObject *)pToolBarWrapper, UNO_QUERY );
216 Reference< XInitialization > xInit( xToolBar, UNO_QUERY );
217 xInit->initialize( aPropSeq );
220 return xToolBar;