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::ui
;
42 using namespace framework
;
46 class AddonsToolBarFactory
: public ::cppu::WeakImplHelper
< css::lang::XServiceInfo
,
47 css::ui::XUIElementFactory
>
50 explicit AddonsToolBarFactory( const css::uno::Reference
< css::uno::XComponentContext
>& xContext
);
52 virtual OUString SAL_CALL
getImplementationName() override
54 return u
"com.sun.star.comp.framework.AddonsToolBarFactory"_ustr
;
57 virtual sal_Bool SAL_CALL
supportsService(OUString
const & ServiceName
) override
59 return cppu::supportsService(this, ServiceName
);
62 virtual css::uno::Sequence
<OUString
> SAL_CALL
getSupportedServiceNames() override
64 return {u
"com.sun.star.ui.ToolBarFactory"_ustr
};
68 virtual css::uno::Reference
< css::ui::XUIElement
> SAL_CALL
createUIElement( const OUString
& ResourceURL
, const css::uno::Sequence
< css::beans::PropertyValue
>& Args
) override
;
70 bool hasButtonsInContext( const css::uno::Sequence
< css::uno::Sequence
< css::beans::PropertyValue
> >& rPropSeq
,
71 const css::uno::Reference
< css::frame::XFrame
>& rFrame
);
74 css::uno::Reference
< css::uno::XComponentContext
> m_xContext
;
75 css::uno::Reference
< css::frame::XModuleManager2
> m_xModuleManager
;
78 AddonsToolBarFactory::AddonsToolBarFactory(
79 const css::uno::Reference
< css::uno::XComponentContext
>& xContext
) :
80 m_xContext( xContext
)
81 , m_xModuleManager( ModuleManager::create( xContext
) )
85 bool IsCorrectContext( std::u16string_view rModuleIdentifier
, std::u16string_view aContextList
)
87 if ( aContextList
.empty() )
90 if ( !rModuleIdentifier
.empty() )
92 return aContextList
.find( rModuleIdentifier
) != std::u16string_view::npos
;
98 bool AddonsToolBarFactory::hasButtonsInContext(
99 const Sequence
< Sequence
< PropertyValue
> >& rPropSeqSeq
,
100 const Reference
< XFrame
>& rFrame
)
102 OUString aModuleIdentifier
;
105 aModuleIdentifier
= m_xModuleManager
->identify( rFrame
);
107 catch ( const RuntimeException
& )
111 catch ( const Exception
& )
115 // Check before we create a toolbar that we have at least one button in
116 // the current frame context.
117 for ( Sequence
<PropertyValue
> const & props
: rPropSeqSeq
)
119 bool bIsButton( true );
120 bool bIsCorrectContext( false );
121 sal_uInt32
nPropChecked( 0 );
123 for ( PropertyValue
const & prop
: props
)
125 if ( prop
.Name
== "Context" )
127 OUString aContextList
;
128 if ( prop
.Value
>>= aContextList
)
129 bIsCorrectContext
= IsCorrectContext( aModuleIdentifier
, aContextList
);
132 else if ( prop
.Name
== "URL" )
136 bIsButton
= aURL
!= "private:separator";
140 if ( nPropChecked
== 2 )
144 if ( bIsButton
&& bIsCorrectContext
)
152 Reference
< XUIElement
> SAL_CALL
AddonsToolBarFactory::createUIElement(
153 const OUString
& ResourceURL
,
154 const Sequence
< PropertyValue
>& Args
)
158 Sequence
< Sequence
< PropertyValue
> > aConfigData
;
159 Reference
< XFrame
> xFrame
;
160 OUString
aResourceURL( ResourceURL
);
162 for ( PropertyValue
const & arg
: Args
)
164 if ( arg
.Name
== "ConfigurationData" )
165 arg
.Value
>>= aConfigData
;
166 else if ( arg
.Name
== "Frame" )
167 arg
.Value
>>= xFrame
;
168 else if ( arg
.Name
== "ResourceURL" )
169 arg
.Value
>>= aResourceURL
;
172 if ( !aResourceURL
.startsWith("private:resource/toolbar/addon_") )
173 throw IllegalArgumentException();
175 // Identify frame and determine module identifier to look for context based buttons
176 Reference
< css::ui::XUIElement
> xToolBar
;
178 aConfigData
.hasElements() &&
179 hasButtonsInContext( aConfigData
, xFrame
))
181 Sequence
< Any
> aPropSeq
{ Any(comphelper::makePropertyValue(u
"Frame"_ustr
, xFrame
)),
182 Any(comphelper::makePropertyValue(u
"ConfigurationData"_ustr
,
184 Any(comphelper::makePropertyValue(u
"ResourceURL"_ustr
, aResourceURL
)) };
186 SolarMutexGuard aGuard
;
187 rtl::Reference
<AddonsToolBarWrapper
> pToolBarWrapper
= new AddonsToolBarWrapper( m_xContext
);
188 xToolBar
= pToolBarWrapper
;
189 pToolBarWrapper
->initialize( aPropSeq
);
197 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
198 com_sun_star_comp_framework_AddonsToolBarFactory_get_implementation(
199 css::uno::XComponentContext
*context
,
200 css::uno::Sequence
<css::uno::Any
> const &)
202 return cppu::acquire(new AddonsToolBarFactory(context
));
205 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */