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 <sal/config.h>
22 #include <string_view>
24 #include <uielement/addonstoolbarwrapper.hxx>
26 #include <com/sun/star/frame/ModuleManager.hpp>
27 #include <com/sun/star/frame/XModuleManager2.hpp>
28 #include <com/sun/star/frame/XFrame.hpp>
29 #include <com/sun/star/lang/XServiceInfo.hpp>
30 #include <com/sun/star/ui/XUIElementFactory.hpp>
32 #include <comphelper/propertyvalue.hxx>
33 #include <cppuhelper/implbase.hxx>
34 #include <cppuhelper/supportsservice.hxx>
35 #include <vcl/svapp.hxx>
37 using namespace com::sun::star::uno
;
38 using namespace com::sun::star::lang
;
39 using namespace com::sun::star::frame
;
40 using namespace com::sun::star::beans
;
41 using namespace com::sun::star::util
;
42 using namespace ::com::sun::star::ui
;
43 using namespace framework
;
47 class AddonsToolBarFactory
: public ::cppu::WeakImplHelper
< css::lang::XServiceInfo
,
48 css::ui::XUIElementFactory
>
51 explicit AddonsToolBarFactory( const css::uno::Reference
< css::uno::XComponentContext
>& xContext
);
53 virtual OUString SAL_CALL
getImplementationName() override
55 return "com.sun.star.comp.framework.AddonsToolBarFactory";
58 virtual sal_Bool SAL_CALL
supportsService(OUString
const & ServiceName
) override
60 return cppu::supportsService(this, ServiceName
);
63 virtual css::uno::Sequence
<OUString
> SAL_CALL
getSupportedServiceNames() override
65 return {"com.sun.star.ui.ToolBarFactory"};
69 virtual css::uno::Reference
< css::ui::XUIElement
> SAL_CALL
createUIElement( const OUString
& ResourceURL
, const css::uno::Sequence
< css::beans::PropertyValue
>& Args
) override
;
71 bool hasButtonsInContext( const css::uno::Sequence
< css::uno::Sequence
< css::beans::PropertyValue
> >& rPropSeq
,
72 const css::uno::Reference
< css::frame::XFrame
>& rFrame
);
75 css::uno::Reference
< css::uno::XComponentContext
> m_xContext
;
76 css::uno::Reference
< css::frame::XModuleManager2
> m_xModuleManager
;
79 AddonsToolBarFactory::AddonsToolBarFactory(
80 const css::uno::Reference
< css::uno::XComponentContext
>& xContext
) :
81 m_xContext( xContext
)
82 , m_xModuleManager( ModuleManager::create( xContext
) )
86 bool IsCorrectContext( std::u16string_view rModuleIdentifier
, std::u16string_view aContextList
)
88 if ( aContextList
.empty() )
91 if ( !rModuleIdentifier
.empty() )
93 return aContextList
.find( rModuleIdentifier
) != std::u16string_view::npos
;
99 bool AddonsToolBarFactory::hasButtonsInContext(
100 const Sequence
< Sequence
< PropertyValue
> >& rPropSeqSeq
,
101 const Reference
< XFrame
>& rFrame
)
103 OUString aModuleIdentifier
;
106 aModuleIdentifier
= m_xModuleManager
->identify( rFrame
);
108 catch ( const RuntimeException
& )
112 catch ( const Exception
& )
116 // Check before we create a toolbar that we have at least one button in
117 // the current frame context.
118 for ( Sequence
<PropertyValue
> const & props
: rPropSeqSeq
)
120 bool bIsButton( true );
121 bool bIsCorrectContext( false );
122 sal_uInt32
nPropChecked( 0 );
124 for ( PropertyValue
const & prop
: props
)
126 if ( prop
.Name
== "Context" )
128 OUString aContextList
;
129 if ( prop
.Value
>>= aContextList
)
130 bIsCorrectContext
= IsCorrectContext( aModuleIdentifier
, aContextList
);
133 else if ( prop
.Name
== "URL" )
137 bIsButton
= aURL
!= "private:separator";
141 if ( nPropChecked
== 2 )
145 if ( bIsButton
&& bIsCorrectContext
)
153 Reference
< XUIElement
> SAL_CALL
AddonsToolBarFactory::createUIElement(
154 const OUString
& ResourceURL
,
155 const Sequence
< PropertyValue
>& Args
)
159 Sequence
< Sequence
< PropertyValue
> > aConfigData
;
160 Reference
< XFrame
> xFrame
;
161 OUString
aResourceURL( ResourceURL
);
163 for ( PropertyValue
const & arg
: Args
)
165 if ( arg
.Name
== "ConfigurationData" )
166 arg
.Value
>>= aConfigData
;
167 else if ( arg
.Name
== "Frame" )
168 arg
.Value
>>= xFrame
;
169 else if ( arg
.Name
== "ResourceURL" )
170 arg
.Value
>>= aResourceURL
;
173 if ( !aResourceURL
.startsWith("private:resource/toolbar/addon_") )
174 throw IllegalArgumentException();
176 // Identify frame and determine module identifier to look for context based buttons
177 Reference
< css::ui::XUIElement
> xToolBar
;
179 aConfigData
.hasElements() &&
180 hasButtonsInContext( aConfigData
, xFrame
))
182 Sequence
< Any
> aPropSeq
{ Any(comphelper::makePropertyValue("Frame", xFrame
)),
183 Any(comphelper::makePropertyValue("ConfigurationData",
185 Any(comphelper::makePropertyValue("ResourceURL", aResourceURL
)) };
187 SolarMutexGuard aGuard
;
188 rtl::Reference
<AddonsToolBarWrapper
> pToolBarWrapper
= new AddonsToolBarWrapper( m_xContext
);
189 xToolBar
= pToolBarWrapper
;
190 pToolBarWrapper
->initialize( aPropSeq
);
198 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
199 com_sun_star_comp_framework_AddonsToolBarFactory_get_implementation(
200 css::uno::XComponentContext
*context
,
201 css::uno::Sequence
<css::uno::Any
> const &)
203 return cppu::acquire(new AddonsToolBarFactory(context
));
206 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */