LibreOffice Module framework (master) 1
windowcontentfactorymanager.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
23#include <helper/mischelper.hxx>
24
25#include <com/sun/star/beans/PropertyValue.hpp>
26#include <com/sun/star/frame/ModuleManager.hpp>
27#include <com/sun/star/frame/UnknownModuleException.hpp>
28#include <com/sun/star/frame/XModuleManager2.hpp>
29#include <com/sun/star/frame/XFrame.hpp>
30#include <com/sun/star/lang/XServiceInfo.hpp>
31#include <com/sun/star/lang/XSingleComponentFactory.hpp>
32#include <com/sun/star/uno/XComponentContext.hpp>
33
36#include <rtl/ref.hxx>
37#include <utility>
39
40using namespace ::com::sun::star;
41using namespace framework;
42
43namespace {
44
46 css::lang::XServiceInfo,
47 css::lang::XSingleComponentFactory > WindowContentFactoryManager_BASE;
48
49class WindowContentFactoryManager : public WindowContentFactoryManager_BASE
50{
51public:
52 explicit WindowContentFactoryManager( css::uno::Reference< css::uno::XComponentContext> xContext );
53
54 virtual OUString SAL_CALL getImplementationName() override
55 {
56 return "com.sun.star.comp.framework.WindowContentFactoryManager";
57 }
58
59 virtual sal_Bool SAL_CALL supportsService(OUString const & ServiceName) override
60 {
61 return cppu::supportsService(this, ServiceName);
62 }
63
64 virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override
65 {
66 return {"com.sun.star.ui.WindowContentFactoryManager"};
67 }
68
69 // XSingleComponentFactory
70 virtual css::uno::Reference< css::uno::XInterface > SAL_CALL createInstanceWithContext( const css::uno::Reference< css::uno::XComponentContext >& Context ) override;
71 virtual css::uno::Reference< css::uno::XInterface > SAL_CALL createInstanceWithArgumentsAndContext( const css::uno::Sequence< css::uno::Any >& Arguments, const css::uno::Reference< css::uno::XComponentContext >& Context ) override;
72
73private:
74 virtual void disposing(std::unique_lock<std::mutex>&) override;
75
76 css::uno::Reference< css::uno::XComponentContext > m_xContext;
77 bool m_bConfigRead;
79};
80
81WindowContentFactoryManager::WindowContentFactoryManager( uno::Reference< uno::XComponentContext > xContext ) :
82 m_xContext(std::move( xContext )),
83 m_bConfigRead( false ),
84 m_pConfigAccess(
87 "/org.openoffice.Office.UI.WindowContentFactories/Registered/ContentFactories"))
88{}
89
90void WindowContentFactoryManager::disposing(std::unique_lock<std::mutex>&)
91{
92 m_pConfigAccess.clear();
93}
94
95// XSingleComponentFactory
96uno::Reference< uno::XInterface > SAL_CALL WindowContentFactoryManager::createInstanceWithContext(
97 const uno::Reference< uno::XComponentContext >& /*xContext*/ )
98{
99 uno::Reference< uno::XInterface > xWindow;
100 return xWindow;
101}
102
103uno::Reference< uno::XInterface > SAL_CALL WindowContentFactoryManager::createInstanceWithArgumentsAndContext(
104 const uno::Sequence< uno::Any >& Arguments, const uno::Reference< uno::XComponentContext >& Context )
105{
106 uno::Reference< uno::XInterface > xWindow;
107 uno::Reference< frame::XFrame > xFrame;
108 OUString aResourceURL;
109
110 for (auto const & arg : Arguments )
111 {
112 beans::PropertyValue aPropValue;
113 if ( arg >>= aPropValue )
114 {
115 if ( aPropValue.Name == "Frame" )
116 aPropValue.Value >>= xFrame;
117 else if ( aPropValue.Name == "ResourceURL" )
118 aPropValue.Value >>= aResourceURL;
119 }
120 }
121
122 // Determine the module identifier
123 OUString aType;
124 OUString aName;
125 OUString aModuleId;
126 uno::Reference< frame::XModuleManager2 > xModuleManager =
127 frame::ModuleManager::create( m_xContext );
128 try
129 {
130 if ( xFrame.is() && xModuleManager.is() )
131 aModuleId = xModuleManager->identify( uno::Reference< uno::XInterface >( xFrame, uno::UNO_QUERY ) );
132 }
133 catch ( const frame::UnknownModuleException& )
134 {
135 }
136
137 RetrieveTypeNameFromResourceURL( aResourceURL, aType, aName );
138 if ( !aType.isEmpty() &&
139 !aName.isEmpty() &&
140 !aModuleId.isEmpty() )
141 {
142 OUString aImplementationName;
143 uno::Reference< uno::XInterface > xHolder( static_cast<cppu::OWeakObject*>(this), uno::UNO_QUERY );
144
145 // Determine the implementation name of the window content factory dependent on the
146 // module identifier, user interface element type and name
147 { // SAFE
148 std::unique_lock g(m_aMutex);
149 if (m_bDisposed) {
150 throw css::lang::DisposedException(
151 "disposed", static_cast<OWeakObject *>(this));
152 }
153 if ( !m_bConfigRead )
154 {
155 m_bConfigRead = true;
156 m_pConfigAccess->readConfigurationData();
157 }
158 aImplementationName = m_pConfigAccess->getFactorySpecifierFromTypeNameModule( aType, aName, aModuleId );
159 } // SAFE
160
161 if ( !aImplementationName.isEmpty() )
162 {
163 uno::Reference< lang::XMultiServiceFactory > xServiceManager( Context->getServiceManager(), uno::UNO_QUERY );
164 if ( xServiceManager.is() )
165 {
166 uno::Reference< lang::XSingleComponentFactory > xFactory(
167 xServiceManager->createInstance( aImplementationName ), uno::UNO_QUERY );
168 if ( xFactory.is() )
169 {
170 // Be careful: We call external code. Therefore here we have to catch all exceptions
171 try
172 {
173 xWindow = xFactory->createInstanceWithArgumentsAndContext( Arguments, Context );
174 }
175 catch ( uno::Exception& )
176 {
178 }
179 }
180 }
181 }
182 }
183
184 // UNSAFE
185 if ( !xWindow.is())
186 {
187 // Fallback: Use internal factory code to create a toolkit dialog as a content window
188 xWindow = createInstanceWithContext(Context);
189 }
190
191 return xWindow;
192}
193
194}
195
196extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
198 css::uno::XComponentContext *context,
199 css::uno::Sequence<css::uno::Any> const &)
200{
201 return cppu::acquire(new WindowContentFactoryManager(context));
202}
203
204/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define DBG_UNHANDLED_EXCEPTION(...)
Reference< XSingleServiceFactory > xFactory
OUString aImplementationName
bool m_bConfigRead
css::uno::Reference< css::uno::XComponentContext > m_xContext
bool m_bDisposed
OUString aName
css::uno::Sequence< OUString > getSupportedServiceNames()
OUString getImplementationName()
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
void RetrieveTypeNameFromResourceURL(std::u16string_view aResourceURL, OUString &aType, OUString &aName)
Definition: mischelper.hxx:86
Reference< XFrame > xFrame
unsigned char sal_Bool
std::mutex m_aMutex
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_comp_framework_WindowContentFactoryManager_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)