1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <uielement/addonstoolbarwrapper.hxx>
22 #include <com/sun/star/util/XURLTransformer.hpp>
23 #include <com/sun/star/frame/ModuleManager.hpp>
24 #include <com/sun/star/frame/XModuleManager2.hpp>
25 #include <com/sun/star/frame/XFrame.hpp>
26 #include <com/sun/star/lang/XInitialization.hpp>
27 #include <com/sun/star/lang/XServiceInfo.hpp>
28 #include <com/sun/star/ui/XModuleUIConfigurationManagerSupplier.hpp>
29 #include <com/sun/star/ui/XUIConfigurationManagerSupplier.hpp>
30 #include <com/sun/star/ui/XUIElementFactory.hpp>
32 #include <cppuhelper/implbase.hxx>
33 #include <cppuhelper/supportsservice.hxx>
34 #include <vcl/svapp.hxx>
35 #include <rtl/ref.hxx>
36 #include <rtl/ustrbuf.hxx>
40 using namespace com::sun::star::uno
;
41 using namespace com::sun::star::lang
;
42 using namespace com::sun::star::frame
;
43 using namespace com::sun::star::beans
;
44 using namespace com::sun::star::util
;
45 using namespace ::com::sun::star::ui
;
46 using namespace framework
;
50 class AddonsToolBarFactory
: public ::cppu::WeakImplHelper
< css::lang::XServiceInfo
,
51 css::ui::XUIElementFactory
>
54 explicit AddonsToolBarFactory( const css::uno::Reference
< css::uno::XComponentContext
>& xContext
);
56 virtual OUString SAL_CALL
getImplementationName() override
58 return "com.sun.star.comp.framework.AddonsToolBarFactory";
61 virtual sal_Bool SAL_CALL
supportsService(OUString
const & ServiceName
) override
63 return cppu::supportsService(this, ServiceName
);
66 virtual css::uno::Sequence
<OUString
> SAL_CALL
getSupportedServiceNames() override
68 return {"com.sun.star.ui.ToolBarFactory"};
72 virtual css::uno::Reference
< css::ui::XUIElement
> SAL_CALL
createUIElement( const OUString
& ResourceURL
, const css::uno::Sequence
< css::beans::PropertyValue
>& Args
) override
;
74 bool hasButtonsInContext( const css::uno::Sequence
< css::uno::Sequence
< css::beans::PropertyValue
> >& rPropSeq
,
75 const css::uno::Reference
< css::frame::XFrame
>& rFrame
);
78 css::uno::Reference
< css::uno::XComponentContext
> m_xContext
;
79 css::uno::Reference
< css::frame::XModuleManager2
> m_xModuleManager
;
82 AddonsToolBarFactory::AddonsToolBarFactory(
83 const css::uno::Reference
< css::uno::XComponentContext
>& xContext
) :
84 m_xContext( xContext
)
85 , m_xModuleManager( ModuleManager::create( xContext
) )
89 bool IsCorrectContext( const OUString
& rModuleIdentifier
, const OUString
& aContextList
)
91 if ( aContextList
.isEmpty() )
94 if ( !rModuleIdentifier
.isEmpty() )
96 sal_Int32 nIndex
= aContextList
.indexOf( rModuleIdentifier
);
97 return ( nIndex
>= 0 );
103 bool AddonsToolBarFactory::hasButtonsInContext(
104 const Sequence
< Sequence
< PropertyValue
> >& rPropSeqSeq
,
105 const Reference
< XFrame
>& rFrame
)
107 OUString aModuleIdentifier
;
110 aModuleIdentifier
= m_xModuleManager
->identify( rFrame
);
112 catch ( const RuntimeException
& )
116 catch ( const Exception
& )
120 // Check before we create a toolbar that we have at least one button in
121 // the current frame context.
122 for ( sal_uInt32 i
= 0; i
< static_cast<sal_uInt32
>(rPropSeqSeq
.getLength()); i
++ )
124 bool bIsButton( true );
125 bool bIsCorrectContext( false );
126 sal_uInt32
nPropChecked( 0 );
128 const Sequence
< PropertyValue
>& rPropSeq
= rPropSeqSeq
[i
];
129 for ( sal_uInt32 j
= 0; j
< static_cast<sal_uInt32
>(rPropSeq
.getLength()); j
++ )
131 if ( rPropSeq
[j
].Name
== "Context" )
133 OUString aContextList
;
134 if ( rPropSeq
[j
].Value
>>= aContextList
)
135 bIsCorrectContext
= IsCorrectContext( aModuleIdentifier
, aContextList
);
138 else if ( rPropSeq
[j
].Name
== "URL" )
141 rPropSeq
[j
].Value
>>= aURL
;
142 bIsButton
= aURL
!= "private:separator";
146 if ( nPropChecked
== 2 )
150 if ( bIsButton
&& bIsCorrectContext
)
158 Reference
< XUIElement
> SAL_CALL
AddonsToolBarFactory::createUIElement(
159 const OUString
& ResourceURL
,
160 const Sequence
< PropertyValue
>& Args
)
164 Sequence
< Sequence
< PropertyValue
> > aConfigData
;
165 Reference
< XFrame
> xFrame
;
166 OUString
aResourceURL( ResourceURL
);
168 for ( sal_Int32 n
= 0; n
< Args
.getLength(); n
++ )
170 if ( Args
[n
].Name
== "ConfigurationData" )
171 Args
[n
].Value
>>= aConfigData
;
172 else if ( Args
[n
].Name
== "Frame" )
173 Args
[n
].Value
>>= xFrame
;
174 else if ( Args
[n
].Name
== "ResourceURL" )
175 Args
[n
].Value
>>= aResourceURL
;
178 if ( !aResourceURL
.startsWith("private:resource/toolbar/addon_") )
179 throw IllegalArgumentException();
181 // Identify frame and determine module identifier to look for context based buttons
182 Reference
< css::ui::XUIElement
> xToolBar
;
184 aConfigData
.hasElements() &&
185 hasButtonsInContext( aConfigData
, xFrame
))
187 PropertyValue aPropValue
;
188 Sequence
< Any
> aPropSeq( 3 );
189 aPropValue
.Name
= "Frame";
190 aPropValue
.Value
<<= xFrame
;
191 aPropSeq
[0] <<= aPropValue
;
192 aPropValue
.Name
= "ConfigurationData";
193 aPropValue
.Value
<<= aConfigData
;
194 aPropSeq
[1] <<= aPropValue
;
195 aPropValue
.Name
= "ResourceURL";
196 aPropValue
.Value
<<= aResourceURL
;
197 aPropSeq
[2] <<= aPropValue
;
199 SolarMutexGuard aGuard
;
200 AddonsToolBarWrapper
* pToolBarWrapper
= new AddonsToolBarWrapper( m_xContext
);
201 xToolBar
.set( static_cast<OWeakObject
*>(pToolBarWrapper
), UNO_QUERY
);
202 Reference
< XInitialization
> xInit( xToolBar
, UNO_QUERY
);
203 xInit
->initialize( aPropSeq
);
211 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
212 com_sun_star_comp_framework_AddonsToolBarFactory_get_implementation(
213 css::uno::XComponentContext
*context
,
214 css::uno::Sequence
<css::uno::Any
> const &)
216 return cppu::acquire(new AddonsToolBarFactory(context
));
219 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */