LibreOffice Module framework (master) 1
addonstoolbarfactory.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <sal/config.h>
21
22#include <string_view>
23
25
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>
31
35#include <vcl/svapp.hxx>
36
37using namespace com::sun::star::uno;
38using namespace com::sun::star::lang;
39using namespace com::sun::star::frame;
40using namespace com::sun::star::beans;
41using namespace com::sun::star::util;
42using namespace ::com::sun::star::ui;
43using namespace framework;
44
45namespace {
46
47class AddonsToolBarFactory : public ::cppu::WeakImplHelper< css::lang::XServiceInfo ,
48 css::ui::XUIElementFactory >
49{
50public:
51 explicit AddonsToolBarFactory( const css::uno::Reference< css::uno::XComponentContext >& xContext );
52
53 virtual OUString SAL_CALL getImplementationName() override
54 {
55 return "com.sun.star.comp.framework.AddonsToolBarFactory";
56 }
57
58 virtual sal_Bool SAL_CALL supportsService(OUString const & ServiceName) override
59 {
60 return cppu::supportsService(this, ServiceName);
61 }
62
63 virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override
64 {
65 return {"com.sun.star.ui.ToolBarFactory"};
66 }
67
68 // XUIElementFactory
69 virtual css::uno::Reference< css::ui::XUIElement > SAL_CALL createUIElement( const OUString& ResourceURL, const css::uno::Sequence< css::beans::PropertyValue >& Args ) override;
70
71 bool hasButtonsInContext( const css::uno::Sequence< css::uno::Sequence< css::beans::PropertyValue > >& rPropSeq,
72 const css::uno::Reference< css::frame::XFrame >& rFrame );
73
74private:
75 css::uno::Reference< css::uno::XComponentContext > m_xContext;
76 css::uno::Reference< css::frame::XModuleManager2 > m_xModuleManager;
77};
78
79AddonsToolBarFactory::AddonsToolBarFactory(
80 const css::uno::Reference< css::uno::XComponentContext >& xContext ) :
81 m_xContext( xContext )
82 , m_xModuleManager( ModuleManager::create( xContext ) )
83{
84}
85
86bool IsCorrectContext( std::u16string_view rModuleIdentifier, std::u16string_view aContextList )
87{
88 if ( aContextList.empty() )
89 return true;
90
91 if ( !rModuleIdentifier.empty() )
92 {
93 return aContextList.find( rModuleIdentifier ) != std::u16string_view::npos;
94 }
95
96 return false;
97}
98
99bool AddonsToolBarFactory::hasButtonsInContext(
100 const Sequence< Sequence< PropertyValue > >& rPropSeqSeq,
101 const Reference< XFrame >& rFrame )
102{
103 OUString aModuleIdentifier;
104 try
105 {
106 aModuleIdentifier = m_xModuleManager->identify( rFrame );
107 }
108 catch ( const RuntimeException& )
109 {
110 throw;
111 }
112 catch ( const Exception& )
113 {
114 }
115
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 )
119 {
120 bool bIsButton( true );
121 bool bIsCorrectContext( false );
122 sal_uInt32 nPropChecked( 0 );
123
124 for ( PropertyValue const & prop : props )
125 {
126 if ( prop.Name == "Context" )
127 {
128 OUString aContextList;
129 if ( prop.Value >>= aContextList )
130 bIsCorrectContext = IsCorrectContext( aModuleIdentifier, aContextList );
131 nPropChecked++;
132 }
133 else if ( prop.Name == "URL" )
134 {
135 OUString aURL;
136 prop.Value >>= aURL;
137 bIsButton = aURL != "private:separator";
138 nPropChecked++;
139 }
140
141 if ( nPropChecked == 2 )
142 break;
143 }
144
145 if ( bIsButton && bIsCorrectContext )
146 return true;
147 }
148
149 return false;
150}
151
152// XUIElementFactory
153Reference< XUIElement > SAL_CALL AddonsToolBarFactory::createUIElement(
154 const OUString& ResourceURL,
155 const Sequence< PropertyValue >& Args )
156{
158
161 OUString aResourceURL( ResourceURL );
162
163 for ( PropertyValue const & arg : Args )
164 {
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;
171 }
172
173 if ( !aResourceURL.startsWith("private:resource/toolbar/addon_") )
174 throw IllegalArgumentException();
175
176 // Identify frame and determine module identifier to look for context based buttons
178 if ( xFrame.is() &&
179 aConfigData.hasElements() &&
180 hasButtonsInContext( aConfigData, xFrame ))
181 {
182 Sequence< Any > aPropSeq{ Any(comphelper::makePropertyValue("Frame", xFrame)),
183 Any(comphelper::makePropertyValue("ConfigurationData",
184 aConfigData)),
185 Any(comphelper::makePropertyValue("ResourceURL", aResourceURL)) };
186
187 SolarMutexGuard aGuard;
189 xToolBar = pToolBarWrapper;
190 pToolBarWrapper->initialize( aPropSeq );
191 }
192
193 return xToolBar;
194}
195
196}
197
198extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
200 css::uno::XComponentContext *context,
201 css::uno::Sequence<css::uno::Any> const &)
202{
203 return cppu::acquire(new AddonsToolBarFactory(context));
204}
205
206/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_comp_framework_AddonsToolBarFactory_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
URL aURL
css::uno::Reference< css::uno::XComponentContext > m_xContext
css::beans::PropertyValue makePropertyValue(const OUString &rName, T &&rValue)
css::uno::Sequence< OUString > getSupportedServiceNames()
OUString getImplementationName()
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
css::uno::Reference< css::deployment::XPackageRegistry > create(css::uno::Reference< css::deployment::XPackageRegistry > const &xRootRegistry, OUString const &context, OUString const &cachePath, css::uno::Reference< css::uno::XComponentContext > const &xComponentContext)
bool IsCorrectContext(std::u16string_view aContext, std::u16string_view aModuleIdentifier)
Check whether a module identifier is part of a context defined by a colon separated list of module id...
Reference< XFrame > xFrame
unsigned char sal_Bool