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/implbase2.hxx>
33 #include <cppuhelper/supportsservice.hxx>
34 #include <vcl/svapp.hxx>
35 #include <rtl/ref.hxx>
36 #include <rtl/ustrbuf.hxx>
38 #include <macros/generic.hxx>
39 #include <macros/xinterface.hxx>
40 #include <macros/xtypeprovider.hxx>
41 #include <macros/xserviceinfo.hxx>
44 using namespace com::sun::star::uno
;
45 using namespace com::sun::star::lang
;
46 using namespace com::sun::star::frame
;
47 using namespace com::sun::star::beans
;
48 using namespace com::sun::star::util
;
49 using namespace ::com::sun::star::ui
;
50 using namespace framework
;
54 class AddonsToolBarFactory
: public ::cppu::WeakImplHelper2
< css::lang::XServiceInfo
,
55 css::ui::XUIElementFactory
>
58 AddonsToolBarFactory( const css::uno::Reference
< css::uno::XComponentContext
>& xContext
);
59 virtual ~AddonsToolBarFactory();
61 virtual OUString SAL_CALL
getImplementationName()
62 throw (css::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
64 return OUString("com.sun.star.comp.framework.AddonsToolBarFactory");
67 virtual sal_Bool SAL_CALL
supportsService(OUString
const & ServiceName
)
68 throw (css::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
70 return cppu::supportsService(this, ServiceName
);
73 virtual css::uno::Sequence
<OUString
> SAL_CALL
getSupportedServiceNames()
74 throw (css::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
76 css::uno::Sequence
< OUString
> aSeq(1);
77 aSeq
[0] = "com.sun.star.ui.ToolBarFactory";
82 virtual css::uno::Reference
< css::ui::XUIElement
> SAL_CALL
createUIElement( const OUString
& ResourceURL
, const css::uno::Sequence
< css::beans::PropertyValue
>& Args
) throw ( css::container::NoSuchElementException
, css::lang::IllegalArgumentException
, css::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
84 bool hasButtonsInContext( const css::uno::Sequence
< css::uno::Sequence
< css::beans::PropertyValue
> >& rPropSeq
,
85 const css::uno::Reference
< css::frame::XFrame
>& rFrame
);
88 css::uno::Reference
< css::uno::XComponentContext
> m_xContext
;
89 css::uno::Reference
< css::frame::XModuleManager2
> m_xModuleManager
;
92 AddonsToolBarFactory::AddonsToolBarFactory(
93 const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XComponentContext
>& xContext
) :
94 m_xContext( xContext
)
95 , m_xModuleManager( ModuleManager::create( xContext
) )
99 AddonsToolBarFactory::~AddonsToolBarFactory()
103 static bool IsCorrectContext( const OUString
& rModuleIdentifier
, const OUString
& aContextList
)
105 if ( aContextList
.isEmpty() )
108 if ( !rModuleIdentifier
.isEmpty() )
110 sal_Int32 nIndex
= aContextList
.indexOf( rModuleIdentifier
);
111 return ( nIndex
>= 0 );
117 bool AddonsToolBarFactory::hasButtonsInContext(
118 const Sequence
< Sequence
< PropertyValue
> >& rPropSeqSeq
,
119 const Reference
< XFrame
>& rFrame
)
121 OUString aModuleIdentifier
;
124 aModuleIdentifier
= m_xModuleManager
->identify( rFrame
);
126 catch ( const RuntimeException
& )
130 catch ( const Exception
& )
134 // Check before we create a toolbar that we have at least one button in
135 // the current frame context.
136 for ( sal_uInt32 i
= 0; i
< (sal_uInt32
)rPropSeqSeq
.getLength(); i
++ )
138 bool bIsButton( true );
139 bool bIsCorrectContext( false );
140 sal_uInt32
nPropChecked( 0 );
142 const Sequence
< PropertyValue
>& rPropSeq
= rPropSeqSeq
[i
];
143 for ( sal_uInt32 j
= 0; j
< (sal_uInt32
)rPropSeq
.getLength(); j
++ )
145 if ( rPropSeq
[j
].Name
== "Context" )
147 OUString aContextList
;
148 if ( rPropSeq
[j
].Value
>>= aContextList
)
149 bIsCorrectContext
= IsCorrectContext( aModuleIdentifier
, aContextList
);
152 else if ( rPropSeq
[j
].Name
== "URL" )
155 rPropSeq
[j
].Value
>>= aURL
;
156 bIsButton
= aURL
!= "private:separator";
160 if ( nPropChecked
== 2 )
164 if ( bIsButton
&& bIsCorrectContext
)
172 Reference
< XUIElement
> SAL_CALL
AddonsToolBarFactory::createUIElement(
173 const OUString
& ResourceURL
,
174 const Sequence
< PropertyValue
>& Args
)
175 throw ( ::com::sun::star::container::NoSuchElementException
,
176 ::com::sun::star::lang::IllegalArgumentException
,
177 ::com::sun::star::uno::RuntimeException
, std::exception
)
181 Sequence
< Sequence
< PropertyValue
> > aConfigData
;
182 Reference
< XFrame
> xFrame
;
183 OUString
aResourceURL( ResourceURL
);
185 for ( sal_Int32 n
= 0; n
< Args
.getLength(); n
++ )
187 if ( Args
[n
].Name
== "ConfigurationData" )
188 Args
[n
].Value
>>= aConfigData
;
189 else if ( Args
[n
].Name
== "Frame" )
190 Args
[n
].Value
>>= xFrame
;
191 else if ( Args
[n
].Name
== "ResourceURL" )
192 Args
[n
].Value
>>= aResourceURL
;
195 if ( !aResourceURL
.startsWith("private:resource/toolbar/addon_") )
196 throw IllegalArgumentException();
198 // Identify frame and determine module identifier to look for context based buttons
199 Reference
< ::com::sun::star::ui::XUIElement
> xToolBar
;
201 ( aConfigData
.getLength()> 0 ) &&
202 hasButtonsInContext( aConfigData
, xFrame
))
204 PropertyValue aPropValue
;
205 Sequence
< Any
> aPropSeq( 3 );
206 aPropValue
.Name
= "Frame";
207 aPropValue
.Value
<<= xFrame
;
208 aPropSeq
[0] <<= aPropValue
;
209 aPropValue
.Name
= "ConfigurationData";
210 aPropValue
.Value
<<= aConfigData
;
211 aPropSeq
[1] <<= aPropValue
;
212 aPropValue
.Name
= "ResourceURL";
213 aPropValue
.Value
<<= aResourceURL
;
214 aPropSeq
[2] <<= aPropValue
;
216 SolarMutexGuard aGuard
;
217 AddonsToolBarWrapper
* pToolBarWrapper
= new AddonsToolBarWrapper( m_xContext
);
218 xToolBar
= Reference
< ::com::sun::star::ui::XUIElement
>( (OWeakObject
*)pToolBarWrapper
, UNO_QUERY
);
219 Reference
< XInitialization
> xInit( xToolBar
, UNO_QUERY
);
220 xInit
->initialize( aPropSeq
);
228 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
* SAL_CALL
229 com_sun_star_comp_framework_AddonsToolBarFactory_get_implementation(
230 css::uno::XComponentContext
*context
,
231 css::uno::Sequence
<css::uno::Any
> const &)
233 return cppu::acquire(new AddonsToolBarFactory(context
));
236 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */