LibreOffice Module framework (master) 1
moduleuicfgsupplier.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 <stdtypes.h>
21
22#include <com/sun/star/beans/XPropertySet.hpp>
23#include <com/sun/star/container/XNameAccess.hpp>
24#include <com/sun/star/embed/ElementModes.hpp>
25#include <com/sun/star/frame/ModuleManager.hpp>
26#include <com/sun/star/io/XOutputStream.hpp>
27#include <com/sun/star/ui/ModuleUIConfigurationManager.hpp>
28#include <com/sun/star/lang/XServiceInfo.hpp>
29#include <com/sun/star/ui/XModuleUIConfigurationManagerSupplier.hpp>
30#include <com/sun/star/ui/XUIConfigurationManager.hpp>
31#include <com/sun/star/ui/XModuleUIConfigurationManager2.hpp>
32#include <com/sun/star/frame/XModuleManager2.hpp>
33
36
37#include <unordered_map>
38
39using namespace com::sun::star::uno;
40using namespace com::sun::star::io;
41using namespace com::sun::star::lang;
42using namespace com::sun::star::container;
43using namespace com::sun::star::beans;
44using namespace com::sun::star::embed;
45using namespace ::com::sun::star::ui;
46using namespace ::com::sun::star::frame;
47using namespace framework;
48
49namespace {
50
52 css::lang::XServiceInfo,
53 css::ui::XModuleUIConfigurationManagerSupplier >
54 ModuleUIConfigurationManagerSupplier_BASE;
55
56class ModuleUIConfigurationManagerSupplier : public ModuleUIConfigurationManagerSupplier_BASE
57{
58public:
59 explicit ModuleUIConfigurationManagerSupplier( const css::uno::Reference< css::uno::XComponentContext >& rxContext );
60 virtual ~ModuleUIConfigurationManagerSupplier() override;
61
62 virtual OUString SAL_CALL getImplementationName() override
63 {
64 return "com.sun.star.comp.framework.ModuleUIConfigurationManagerSupplier";
65 }
66
67 virtual sal_Bool SAL_CALL supportsService(OUString const & ServiceName) override
68 {
69 return cppu::supportsService(this, ServiceName);
70 }
71
72 virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override
73 {
74 return {"com.sun.star.ui.ModuleUIConfigurationManagerSupplier"};
75 }
76
77 // XModuleUIConfigurationManagerSupplier
78 virtual css::uno::Reference< css::ui::XUIConfigurationManager > SAL_CALL getUIConfigurationManager( const OUString& ModuleIdentifier ) override;
79
80private:
81 virtual void disposing(std::unique_lock<std::mutex>&) final override;
82
83 typedef std::unordered_map< OUString, css::uno::Reference< css::ui::XModuleUIConfigurationManager2 > > ModuleToModuleCfgMgr;
84
85//TODO_AS void impl_initStorages();
86
87 ModuleToModuleCfgMgr m_aModuleToModuleUICfgMgrMap;
88 css::uno::Reference< css::frame::XModuleManager2 > m_xModuleMgr;
89 css::uno::Reference< css::uno::XComponentContext > m_xContext;
90};
91
92ModuleUIConfigurationManagerSupplier::ModuleUIConfigurationManagerSupplier( const Reference< XComponentContext >& xContext ) :
93 m_xModuleMgr( ModuleManager::create( xContext ) )
94 , m_xContext( xContext )
95{
96 try
97 {
98 // Retrieve known modules and insert them into our unordered_map to speed-up access time.
99 Reference< XNameAccess > xNameAccess( m_xModuleMgr, UNO_QUERY_THROW );
100 const Sequence< OUString > aNameSeq = xNameAccess->getElementNames();
101 for ( const OUString& rName : aNameSeq )
102 m_aModuleToModuleUICfgMgrMap.emplace( rName, Reference< XModuleUIConfigurationManager2 >() );
103 }
104 catch(...)
105 {
106 }
107}
108
109ModuleUIConfigurationManagerSupplier::~ModuleUIConfigurationManagerSupplier()
110{
111 std::unique_lock g(m_aMutex);
112 disposing(g);
113}
114
115void ModuleUIConfigurationManagerSupplier::disposing(std::unique_lock<std::mutex>&)
116{
117 // dispose all our module user interface configuration managers
118 for (auto const& elem : m_aModuleToModuleUICfgMgrMap)
119 {
120 Reference< XComponent > xComponent( elem.second, UNO_QUERY );
121 if ( xComponent.is() )
122 xComponent->dispose();
123 }
124 m_aModuleToModuleUICfgMgrMap.clear();
125 m_xModuleMgr.clear();
126}
127
128// XModuleUIConfigurationManagerSupplier
129Reference< XUIConfigurationManager > SAL_CALL ModuleUIConfigurationManagerSupplier::getUIConfigurationManager( const OUString& sModuleIdentifier )
130{
131 std::unique_lock g(m_aMutex);
132 /* SAFE AREA ----------------------------------------------------------------------------------------------- */
133
134 ModuleToModuleCfgMgr::iterator pIter = m_aModuleToModuleUICfgMgrMap.find( sModuleIdentifier );
135 if ( pIter == m_aModuleToModuleUICfgMgrMap.end() )
136 throw NoSuchElementException();
137//TODO_AS impl_initStorages();
138
139 // Create instance on demand
140 if ( !pIter->second.is() )
141 {
142 OUString sShort;
143 try
144 {
146 m_xModuleMgr->getByName(sModuleIdentifier) >>= lProps;
147 for (PropertyValue const & rProp : std::as_const(lProps))
148 {
149 if ( rProp.Name == "ooSetupFactoryShortName" )
150 {
151 rProp.Value >>= sShort;
152 break;
153 }
154 }
155 }
156 catch( const Exception& )
157 {
158 sShort.clear();
159 }
160
161 if (sShort.isEmpty())
162 throw NoSuchElementException();
163
164 pIter->second = css::ui::ModuleUIConfigurationManager::createDefault(m_xContext, sShort, sModuleIdentifier);
165 }
166
167 return pIter->second;
168}
169
170}
171
172extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
174 css::uno::XComponentContext *context,
175 css::uno::Sequence<css::uno::Any> const &)
176{
177 return cppu::acquire(new ModuleUIConfigurationManagerSupplier(context));
178}
179
180/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::uno::Reference< css::uno::XComponentContext > m_xContext
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_comp_framework_ModuleUIConfigurationManagerSupplier_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
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)
unsigned char sal_Bool
std::mutex m_aMutex