1*6d739b60SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*6d739b60SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*6d739b60SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*6d739b60SAndrew Rist  * distributed with this work for additional information
6*6d739b60SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*6d739b60SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*6d739b60SAndrew Rist  * "License"); you may not use this file except in compliance
9*6d739b60SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*6d739b60SAndrew Rist  *
11*6d739b60SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*6d739b60SAndrew Rist  *
13*6d739b60SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*6d739b60SAndrew Rist  * software distributed under the License is distributed on an
15*6d739b60SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*6d739b60SAndrew Rist  * KIND, either express or implied.  See the License for the
17*6d739b60SAndrew Rist  * specific language governing permissions and limitations
18*6d739b60SAndrew Rist  * under the License.
19*6d739b60SAndrew Rist  *
20*6d739b60SAndrew Rist  *************************************************************/
21*6d739b60SAndrew Rist 
22*6d739b60SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_framework.hxx"
26cdf0e10cSrcweir #include <uifactory/addonstoolboxfactory.hxx>
27cdf0e10cSrcweir 
28cdf0e10cSrcweir //_________________________________________________________________________________________________________________
29cdf0e10cSrcweir //	my own includes
30cdf0e10cSrcweir //_________________________________________________________________________________________________________________
31cdf0e10cSrcweir #include <uielement/addonstoolbarwrapper.hxx>
32cdf0e10cSrcweir #include <threadhelp/resetableguard.hxx>
33cdf0e10cSrcweir 
34cdf0e10cSrcweir //_________________________________________________________________________________________________________________
35cdf0e10cSrcweir //	interface includes
36cdf0e10cSrcweir //_________________________________________________________________________________________________________________
37cdf0e10cSrcweir #include <com/sun/star/util/XURLTransformer.hpp>
38cdf0e10cSrcweir #include <com/sun/star/frame/XFrame.hpp>
39cdf0e10cSrcweir #include <com/sun/star/frame/XModel.hpp>
40cdf0e10cSrcweir #include <com/sun/star/lang/XInitialization.hpp>
41cdf0e10cSrcweir #include <com/sun/star/ui/XModuleUIConfigurationManagerSupplier.hpp>
42cdf0e10cSrcweir 
43cdf0e10cSrcweir #ifndef _COM_SUN_STAR_UI_XUICONFIGURATIONMANAGERSUPLLIER_HPP_
44cdf0e10cSrcweir #include <com/sun/star/ui/XUIConfigurationManagerSupplier.hpp>
45cdf0e10cSrcweir #endif
46cdf0e10cSrcweir 
47cdf0e10cSrcweir //_________________________________________________________________________________________________________________
48cdf0e10cSrcweir //	includes of other projects
49cdf0e10cSrcweir //_________________________________________________________________________________________________________________
50cdf0e10cSrcweir #include <vcl/svapp.hxx>
51cdf0e10cSrcweir #include <tools/urlobj.hxx>
52cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
53cdf0e10cSrcweir 
54cdf0e10cSrcweir //_________________________________________________________________________________________________________________
55cdf0e10cSrcweir //	Defines
56cdf0e10cSrcweir //_________________________________________________________________________________________________________________
57cdf0e10cSrcweir //
58cdf0e10cSrcweir 
59cdf0e10cSrcweir using namespace com::sun::star::uno;
60cdf0e10cSrcweir using namespace com::sun::star::lang;
61cdf0e10cSrcweir using namespace com::sun::star::frame;
62cdf0e10cSrcweir using namespace com::sun::star::beans;
63cdf0e10cSrcweir using namespace com::sun::star::util;
64cdf0e10cSrcweir using namespace ::com::sun::star::ui;
65cdf0e10cSrcweir 
66cdf0e10cSrcweir namespace framework
67cdf0e10cSrcweir {
68cdf0e10cSrcweir 
69cdf0e10cSrcweir //*****************************************************************************************************************
70cdf0e10cSrcweir //	XInterface, XTypeProvider, XServiceInfo
71cdf0e10cSrcweir //*****************************************************************************************************************
DEFINE_XSERVICEINFO_ONEINSTANCESERVICE(AddonsToolBoxFactory,::cppu::OWeakObject,SERVICENAME_TOOLBARFACTORY,IMPLEMENTATIONNAME_ADDONSTOOLBARFACTORY)72cdf0e10cSrcweir DEFINE_XSERVICEINFO_ONEINSTANCESERVICE  (   AddonsToolBoxFactory				            ,
73cdf0e10cSrcweir                                             ::cppu::OWeakObject							    ,
74cdf0e10cSrcweir                                             SERVICENAME_TOOLBARFACTORY	                    ,
75cdf0e10cSrcweir 											IMPLEMENTATIONNAME_ADDONSTOOLBARFACTORY
76cdf0e10cSrcweir 										)
77cdf0e10cSrcweir 
78cdf0e10cSrcweir DEFINE_INIT_SERVICE                     (   AddonsToolBoxFactory, {} )
79cdf0e10cSrcweir 
80cdf0e10cSrcweir AddonsToolBoxFactory::AddonsToolBoxFactory(
81cdf0e10cSrcweir     const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xServiceManager ) :
82cdf0e10cSrcweir     ThreadHelpBase( &Application::GetSolarMutex() )
83cdf0e10cSrcweir     , m_xServiceManager( xServiceManager )
84cdf0e10cSrcweir     , m_xModuleManager( xServiceManager->createInstance(SERVICENAME_MODULEMANAGER),UNO_QUERY )
85cdf0e10cSrcweir {
86cdf0e10cSrcweir }
87cdf0e10cSrcweir 
~AddonsToolBoxFactory()88cdf0e10cSrcweir AddonsToolBoxFactory::~AddonsToolBoxFactory()
89cdf0e10cSrcweir {
90cdf0e10cSrcweir }
91cdf0e10cSrcweir 
IsCorrectContext(const::rtl::OUString & rModuleIdentifier,const rtl::OUString & aContextList)92cdf0e10cSrcweir static sal_Bool IsCorrectContext( const ::rtl::OUString& rModuleIdentifier, const rtl::OUString& aContextList )
93cdf0e10cSrcweir {
94cdf0e10cSrcweir     if ( aContextList.getLength() == 0 )
95cdf0e10cSrcweir         return sal_True;
96cdf0e10cSrcweir 
97cdf0e10cSrcweir     if ( rModuleIdentifier.getLength() > 0 )
98cdf0e10cSrcweir     {
99cdf0e10cSrcweir         sal_Int32 nIndex = aContextList.indexOf( rModuleIdentifier );
100cdf0e10cSrcweir         return ( nIndex >= 0 );
101cdf0e10cSrcweir     }
102cdf0e10cSrcweir 
103cdf0e10cSrcweir 	return sal_False;
104cdf0e10cSrcweir }
105cdf0e10cSrcweir 
hasButtonsInContext(const Sequence<Sequence<PropertyValue>> & rPropSeqSeq,const Reference<XFrame> & rFrame)106cdf0e10cSrcweir sal_Bool AddonsToolBoxFactory::hasButtonsInContext(
107cdf0e10cSrcweir     const Sequence< Sequence< PropertyValue > >& rPropSeqSeq,
108cdf0e10cSrcweir     const Reference< XFrame >& rFrame )
109cdf0e10cSrcweir {
110cdf0e10cSrcweir     ::rtl::OUString aModuleIdentifier;
111cdf0e10cSrcweir     try
112cdf0e10cSrcweir     {
113cdf0e10cSrcweir         aModuleIdentifier = m_xModuleManager->identify( rFrame );
114cdf0e10cSrcweir     }
115cdf0e10cSrcweir     catch ( RuntimeException& )
116cdf0e10cSrcweir     {
117cdf0e10cSrcweir         throw;
118cdf0e10cSrcweir     }
119cdf0e10cSrcweir     catch ( Exception& )
120cdf0e10cSrcweir     {
121cdf0e10cSrcweir     }
122cdf0e10cSrcweir 
123cdf0e10cSrcweir     // Check before we create a toolbar that we have at least one button in
124cdf0e10cSrcweir     // the current frame context.
125cdf0e10cSrcweir     for ( sal_uInt32 i = 0; i < (sal_uInt32)rPropSeqSeq.getLength(); i++ )
126cdf0e10cSrcweir     {
127cdf0e10cSrcweir         sal_Bool    bIsButton( sal_True );
128cdf0e10cSrcweir         sal_Bool    bIsCorrectContext( sal_False );
129cdf0e10cSrcweir         sal_uInt32  nPropChecked( 0 );
130cdf0e10cSrcweir 
131cdf0e10cSrcweir         const Sequence< PropertyValue >& rPropSeq = rPropSeqSeq[i];
132cdf0e10cSrcweir         for ( sal_uInt32 j = 0; j < (sal_uInt32)rPropSeq.getLength(); j++ )
133cdf0e10cSrcweir         {
134cdf0e10cSrcweir             if ( rPropSeq[j].Name.equalsAsciiL( "Context", 7 ))
135cdf0e10cSrcweir             {
136cdf0e10cSrcweir                 ::rtl::OUString aContextList;
137cdf0e10cSrcweir                 if ( rPropSeq[j].Value >>= aContextList )
138cdf0e10cSrcweir                     bIsCorrectContext = IsCorrectContext( aModuleIdentifier, aContextList );
139cdf0e10cSrcweir                 nPropChecked++;
140cdf0e10cSrcweir             }
141cdf0e10cSrcweir             else if ( rPropSeq[j].Name.equalsAsciiL( "URL", 3 ))
142cdf0e10cSrcweir             {
143cdf0e10cSrcweir                 ::rtl::OUString aURL;
144cdf0e10cSrcweir                 rPropSeq[j].Value >>= aURL;
145cdf0e10cSrcweir                 bIsButton = !aURL.equalsAsciiL( "private:separator", 17 );
146cdf0e10cSrcweir                 nPropChecked++;
147cdf0e10cSrcweir             }
148cdf0e10cSrcweir 
149cdf0e10cSrcweir             if ( nPropChecked == 2 )
150cdf0e10cSrcweir                 break;
151cdf0e10cSrcweir         }
152cdf0e10cSrcweir 
153cdf0e10cSrcweir         if ( bIsButton && bIsCorrectContext )
154cdf0e10cSrcweir             return sal_True;
155cdf0e10cSrcweir     }
156cdf0e10cSrcweir 
157cdf0e10cSrcweir     return sal_False;
158cdf0e10cSrcweir }
159cdf0e10cSrcweir 
160cdf0e10cSrcweir // XUIElementFactory
createUIElement(const::rtl::OUString & ResourceURL,const Sequence<PropertyValue> & Args)161cdf0e10cSrcweir Reference< XUIElement > SAL_CALL AddonsToolBoxFactory::createUIElement(
162cdf0e10cSrcweir     const ::rtl::OUString& ResourceURL,
163cdf0e10cSrcweir     const Sequence< PropertyValue >& Args )
164cdf0e10cSrcweir throw ( ::com::sun::star::container::NoSuchElementException,
165cdf0e10cSrcweir         ::com::sun::star::lang::IllegalArgumentException,
166cdf0e10cSrcweir         ::com::sun::star::uno::RuntimeException )
167cdf0e10cSrcweir {
168cdf0e10cSrcweir     // SAFE
169cdf0e10cSrcweir     ResetableGuard aLock( m_aLock );
170cdf0e10cSrcweir 
171cdf0e10cSrcweir     Sequence< Sequence< PropertyValue > >   aConfigData;
172cdf0e10cSrcweir     Reference< XFrame >                     xFrame;
173cdf0e10cSrcweir     rtl::OUString                           aResourceURL( ResourceURL );
174cdf0e10cSrcweir 
175cdf0e10cSrcweir     for ( sal_Int32 n = 0; n < Args.getLength(); n++ )
176cdf0e10cSrcweir     {
177cdf0e10cSrcweir         if ( Args[n].Name.equalsAscii( "ConfigurationData" ))
178cdf0e10cSrcweir             Args[n].Value >>= aConfigData;
179cdf0e10cSrcweir         else if ( Args[n].Name.equalsAscii( "Frame" ))
180cdf0e10cSrcweir             Args[n].Value >>= xFrame;
181cdf0e10cSrcweir         else if ( Args[n].Name.equalsAscii( "ResourceURL" ))
182cdf0e10cSrcweir             Args[n].Value >>= aResourceURL;
183cdf0e10cSrcweir     }
184cdf0e10cSrcweir 
185cdf0e10cSrcweir     if ( aResourceURL.indexOf( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "private:resource/toolbar/addon_" ))) != 0 )
186cdf0e10cSrcweir         throw IllegalArgumentException();
187cdf0e10cSrcweir 
188cdf0e10cSrcweir     // Identify frame and determine module identifier to look for context based buttons
189cdf0e10cSrcweir     Reference< ::com::sun::star::ui::XUIElement > xToolBar;
190cdf0e10cSrcweir     if ( xFrame.is() &&
191cdf0e10cSrcweir          ( aConfigData.getLength()> 0 ) &&
192cdf0e10cSrcweir          hasButtonsInContext( aConfigData, xFrame ))
193cdf0e10cSrcweir     {
194cdf0e10cSrcweir         PropertyValue aPropValue;
195cdf0e10cSrcweir         Sequence< Any > aPropSeq( 3 );
196cdf0e10cSrcweir         aPropValue.Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Frame" ));
197cdf0e10cSrcweir         aPropValue.Value <<= xFrame;
198cdf0e10cSrcweir         aPropSeq[0] <<= aPropValue;
199cdf0e10cSrcweir         aPropValue.Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ConfigurationData" ));
200cdf0e10cSrcweir         aPropValue.Value <<= aConfigData;
201cdf0e10cSrcweir         aPropSeq[1] <<= aPropValue;
202cdf0e10cSrcweir         aPropValue.Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ResourceURL" ));
203cdf0e10cSrcweir         aPropValue.Value <<= aResourceURL;
204cdf0e10cSrcweir         aPropSeq[2] <<= aPropValue;
205cdf0e10cSrcweir 
206cdf0e10cSrcweir         vos::OGuard	aGuard( Application::GetSolarMutex() );
207cdf0e10cSrcweir         AddonsToolBarWrapper* pToolBarWrapper = new AddonsToolBarWrapper( m_xServiceManager );
208cdf0e10cSrcweir         xToolBar = Reference< ::com::sun::star::ui::XUIElement >( (OWeakObject *)pToolBarWrapper, UNO_QUERY );
209cdf0e10cSrcweir         Reference< XInitialization > xInit( xToolBar, UNO_QUERY );
210cdf0e10cSrcweir         xInit->initialize( aPropSeq );
211cdf0e10cSrcweir     }
212cdf0e10cSrcweir 
213cdf0e10cSrcweir     return xToolBar;
214cdf0e10cSrcweir }
215cdf0e10cSrcweir 
216cdf0e10cSrcweir }
217cdf0e10cSrcweir 
218