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 #include <uifactory/addonstoolboxfactory.hxx>
31 
32 //_________________________________________________________________________________________________________________
33 //	my own includes
34 //_________________________________________________________________________________________________________________
35 #include <uielement/addonstoolbarwrapper.hxx>
36 #include <threadhelp/resetableguard.hxx>
37 
38 //_________________________________________________________________________________________________________________
39 //	interface includes
40 //_________________________________________________________________________________________________________________
41 #include <com/sun/star/util/XURLTransformer.hpp>
42 #include <com/sun/star/frame/XFrame.hpp>
43 #include <com/sun/star/frame/XModel.hpp>
44 #include <com/sun/star/lang/XInitialization.hpp>
45 #include <com/sun/star/ui/XModuleUIConfigurationManagerSupplier.hpp>
46 
47 #ifndef _COM_SUN_STAR_UI_XUICONFIGURATIONMANAGERSUPLLIER_HPP_
48 #include <com/sun/star/ui/XUIConfigurationManagerSupplier.hpp>
49 #endif
50 
51 //_________________________________________________________________________________________________________________
52 //	includes of other projects
53 //_________________________________________________________________________________________________________________
54 #include <vcl/svapp.hxx>
55 #include <tools/urlobj.hxx>
56 #include <rtl/ustrbuf.hxx>
57 
58 //_________________________________________________________________________________________________________________
59 //	Defines
60 //_________________________________________________________________________________________________________________
61 //
62 
63 using namespace com::sun::star::uno;
64 using namespace com::sun::star::lang;
65 using namespace com::sun::star::frame;
66 using namespace com::sun::star::beans;
67 using namespace com::sun::star::util;
68 using namespace ::com::sun::star::ui;
69 
70 namespace framework
71 {
72 
73 //*****************************************************************************************************************
74 //	XInterface, XTypeProvider, XServiceInfo
75 //*****************************************************************************************************************
76 DEFINE_XSERVICEINFO_ONEINSTANCESERVICE  (   AddonsToolBoxFactory				            ,
77                                             ::cppu::OWeakObject							    ,
78                                             SERVICENAME_TOOLBARFACTORY	                    ,
79 											IMPLEMENTATIONNAME_ADDONSTOOLBARFACTORY
80 										)
81 
82 DEFINE_INIT_SERVICE                     (   AddonsToolBoxFactory, {} )
83 
84 AddonsToolBoxFactory::AddonsToolBoxFactory(
85     const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xServiceManager ) :
86     ThreadHelpBase( &Application::GetSolarMutex() )
87     , m_xServiceManager( xServiceManager )
88     , m_xModuleManager( xServiceManager->createInstance(SERVICENAME_MODULEMANAGER),UNO_QUERY )
89 {
90 }
91 
92 AddonsToolBoxFactory::~AddonsToolBoxFactory()
93 {
94 }
95 
96 static sal_Bool IsCorrectContext( const ::rtl::OUString& rModuleIdentifier, const rtl::OUString& aContextList )
97 {
98     if ( aContextList.getLength() == 0 )
99         return sal_True;
100 
101     if ( rModuleIdentifier.getLength() > 0 )
102     {
103         sal_Int32 nIndex = aContextList.indexOf( rModuleIdentifier );
104         return ( nIndex >= 0 );
105     }
106 
107 	return sal_False;
108 }
109 
110 sal_Bool AddonsToolBoxFactory::hasButtonsInContext(
111     const Sequence< Sequence< PropertyValue > >& rPropSeqSeq,
112     const Reference< XFrame >& rFrame )
113 {
114     ::rtl::OUString aModuleIdentifier;
115     try
116     {
117         aModuleIdentifier = m_xModuleManager->identify( rFrame );
118     }
119     catch ( RuntimeException& )
120     {
121         throw;
122     }
123     catch ( Exception& )
124     {
125     }
126 
127     // Check before we create a toolbar that we have at least one button in
128     // the current frame context.
129     for ( sal_uInt32 i = 0; i < (sal_uInt32)rPropSeqSeq.getLength(); i++ )
130     {
131         sal_Bool    bIsButton( sal_True );
132         sal_Bool    bIsCorrectContext( sal_False );
133         sal_uInt32  nPropChecked( 0 );
134 
135         const Sequence< PropertyValue >& rPropSeq = rPropSeqSeq[i];
136         for ( sal_uInt32 j = 0; j < (sal_uInt32)rPropSeq.getLength(); j++ )
137         {
138             if ( rPropSeq[j].Name.equalsAsciiL( "Context", 7 ))
139             {
140                 ::rtl::OUString aContextList;
141                 if ( rPropSeq[j].Value >>= aContextList )
142                     bIsCorrectContext = IsCorrectContext( aModuleIdentifier, aContextList );
143                 nPropChecked++;
144             }
145             else if ( rPropSeq[j].Name.equalsAsciiL( "URL", 3 ))
146             {
147                 ::rtl::OUString aURL;
148                 rPropSeq[j].Value >>= aURL;
149                 bIsButton = !aURL.equalsAsciiL( "private:separator", 17 );
150                 nPropChecked++;
151             }
152 
153             if ( nPropChecked == 2 )
154                 break;
155         }
156 
157         if ( bIsButton && bIsCorrectContext )
158             return sal_True;
159     }
160 
161     return sal_False;
162 }
163 
164 // XUIElementFactory
165 Reference< XUIElement > SAL_CALL AddonsToolBoxFactory::createUIElement(
166     const ::rtl::OUString& ResourceURL,
167     const Sequence< PropertyValue >& Args )
168 throw ( ::com::sun::star::container::NoSuchElementException,
169         ::com::sun::star::lang::IllegalArgumentException,
170         ::com::sun::star::uno::RuntimeException )
171 {
172     // SAFE
173     ResetableGuard aLock( m_aLock );
174 
175     Sequence< Sequence< PropertyValue > >   aConfigData;
176     Reference< XFrame >                     xFrame;
177     rtl::OUString                           aResourceURL( ResourceURL );
178 
179     for ( sal_Int32 n = 0; n < Args.getLength(); n++ )
180     {
181         if ( Args[n].Name.equalsAscii( "ConfigurationData" ))
182             Args[n].Value >>= aConfigData;
183         else if ( Args[n].Name.equalsAscii( "Frame" ))
184             Args[n].Value >>= xFrame;
185         else if ( Args[n].Name.equalsAscii( "ResourceURL" ))
186             Args[n].Value >>= aResourceURL;
187     }
188 
189     if ( aResourceURL.indexOf( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "private:resource/toolbar/addon_" ))) != 0 )
190         throw IllegalArgumentException();
191 
192     // Identify frame and determine module identifier to look for context based buttons
193     Reference< ::com::sun::star::ui::XUIElement > xToolBar;
194     if ( xFrame.is() &&
195          ( aConfigData.getLength()> 0 ) &&
196          hasButtonsInContext( aConfigData, xFrame ))
197     {
198         PropertyValue aPropValue;
199         Sequence< Any > aPropSeq( 3 );
200         aPropValue.Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Frame" ));
201         aPropValue.Value <<= xFrame;
202         aPropSeq[0] <<= aPropValue;
203         aPropValue.Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ConfigurationData" ));
204         aPropValue.Value <<= aConfigData;
205         aPropSeq[1] <<= aPropValue;
206         aPropValue.Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ResourceURL" ));
207         aPropValue.Value <<= aResourceURL;
208         aPropSeq[2] <<= aPropValue;
209 
210         vos::OGuard	aGuard( Application::GetSolarMutex() );
211         AddonsToolBarWrapper* pToolBarWrapper = new AddonsToolBarWrapper( m_xServiceManager );
212         xToolBar = Reference< ::com::sun::star::ui::XUIElement >( (OWeakObject *)pToolBarWrapper, UNO_QUERY );
213         Reference< XInitialization > xInit( xToolBar, UNO_QUERY );
214         xInit->initialize( aPropSeq );
215     }
216 
217     return xToolBar;
218 }
219 
220 }
221 
222