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 <uifactory/configurationaccessfactorymanager.hxx>
23 #include <helper/mischelper.hxx>
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>
34 #include <cppuhelper/basemutex.hxx>
35 #include <cppuhelper/compbase.hxx>
36 #include <cppuhelper/supportsservice.hxx>
37 #include <rtl/ref.hxx>
38 #include <tools/diagnose_ex.h>
40 using namespace ::com::sun::star
;
41 using namespace framework
;
45 typedef ::cppu::WeakComponentImplHelper
<
46 css::lang::XServiceInfo
,
47 css::lang::XSingleComponentFactory
> WindowContentFactoryManager_BASE
;
49 class WindowContentFactoryManager
: private cppu::BaseMutex
,
50 public WindowContentFactoryManager_BASE
53 explicit WindowContentFactoryManager( const css::uno::Reference
< css::uno::XComponentContext
>& rxContext
);
55 virtual OUString SAL_CALL
getImplementationName() override
57 return "com.sun.star.comp.framework.WindowContentFactoryManager";
60 virtual sal_Bool SAL_CALL
supportsService(OUString
const & ServiceName
) override
62 return cppu::supportsService(this, ServiceName
);
65 virtual css::uno::Sequence
<OUString
> SAL_CALL
getSupportedServiceNames() override
67 return {"com.sun.star.ui.WindowContentFactoryManager"};
70 // XSingleComponentFactory
71 virtual css::uno::Reference
< css::uno::XInterface
> SAL_CALL
createInstanceWithContext( const css::uno::Reference
< css::uno::XComponentContext
>& Context
) override
;
72 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
;
75 virtual void SAL_CALL
disposing() override
;
77 css::uno::Reference
< css::uno::XComponentContext
> m_xContext
;
79 rtl::Reference
<ConfigurationAccess_FactoryManager
> m_pConfigAccess
;
82 WindowContentFactoryManager::WindowContentFactoryManager( const uno::Reference
< uno::XComponentContext
>& rxContext
) :
83 WindowContentFactoryManager_BASE(m_aMutex
),
84 m_xContext( rxContext
),
85 m_bConfigRead( false ),
87 new ConfigurationAccess_FactoryManager(
89 "/org.openoffice.Office.UI.WindowContentFactories/Registered/ContentFactories"))
92 void SAL_CALL
WindowContentFactoryManager::disposing()
94 m_pConfigAccess
.clear();
97 // XSingleComponentFactory
98 uno::Reference
< uno::XInterface
> SAL_CALL
WindowContentFactoryManager::createInstanceWithContext(
99 const uno::Reference
< uno::XComponentContext
>& /*xContext*/ )
101 uno::Reference
< uno::XInterface
> xWindow
;
105 uno::Reference
< uno::XInterface
> SAL_CALL
WindowContentFactoryManager::createInstanceWithArgumentsAndContext(
106 const uno::Sequence
< uno::Any
>& Arguments
, const uno::Reference
< uno::XComponentContext
>& Context
)
108 uno::Reference
< uno::XInterface
> xWindow
;
109 uno::Reference
< frame::XFrame
> xFrame
;
110 OUString aResourceURL
;
112 for (sal_Int32 i
=0; i
< Arguments
.getLength(); i
++ )
114 beans::PropertyValue aPropValue
;
115 if ( Arguments
[i
] >>= aPropValue
)
117 if ( aPropValue
.Name
== "Frame" )
118 aPropValue
.Value
>>= xFrame
;
119 else if ( aPropValue
.Name
== "ResourceURL" )
120 aPropValue
.Value
>>= aResourceURL
;
124 // Determine the module identifier
128 uno::Reference
< frame::XModuleManager2
> xModuleManager
=
129 frame::ModuleManager::create( m_xContext
);
132 if ( xFrame
.is() && xModuleManager
.is() )
133 aModuleId
= xModuleManager
->identify( uno::Reference
< uno::XInterface
>( xFrame
, uno::UNO_QUERY
) );
135 catch ( const frame::UnknownModuleException
& )
139 RetrieveTypeNameFromResourceURL( aResourceURL
, aType
, aName
);
140 if ( !aType
.isEmpty() &&
142 !aModuleId
.isEmpty() )
144 OUString aImplementationName
;
145 uno::Reference
< uno::XInterface
> xHolder( static_cast<cppu::OWeakObject
*>(this), uno::UNO_QUERY
);
147 // Determine the implementation name of the window content factory dependent on the
148 // module identifier, user interface element type and name
150 osl::MutexGuard
g(rBHelper
.rMutex
);
151 if (rBHelper
.bDisposed
) {
152 throw css::lang::DisposedException(
153 "disposed", static_cast<OWeakObject
*>(this));
155 if ( !m_bConfigRead
)
157 m_bConfigRead
= true;
158 m_pConfigAccess
->readConfigurationData();
160 aImplementationName
= m_pConfigAccess
->getFactorySpecifierFromTypeNameModule( aType
, aName
, aModuleId
);
163 if ( !aImplementationName
.isEmpty() )
165 uno::Reference
< lang::XMultiServiceFactory
> xServiceManager( Context
->getServiceManager(), uno::UNO_QUERY
);
166 if ( xServiceManager
.is() )
168 uno::Reference
< lang::XSingleComponentFactory
> xFactory(
169 xServiceManager
->createInstance( aImplementationName
), uno::UNO_QUERY
);
172 // Be careful: We call external code. Therefore here we have to catch all exceptions
175 xWindow
= xFactory
->createInstanceWithArgumentsAndContext( Arguments
, Context
);
177 catch ( uno::Exception
& )
179 DBG_UNHANDLED_EXCEPTION("fwk");
189 // Fallback: Use internal factory code to create a toolkit dialog as a content window
190 xWindow
= createInstanceWithContext(Context
);
198 css::uno::Reference
<css::uno::XComponentContext
> const & context
):
199 instance(static_cast<cppu::OWeakObject
*>(
200 new WindowContentFactoryManager(context
)))
204 css::uno::Reference
<css::uno::XInterface
> instance
;
208 public rtl::StaticWithArg
<
209 Instance
, css::uno::Reference
<css::uno::XComponentContext
>, Singleton
>
214 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
215 com_sun_star_comp_framework_WindowContentFactoryManager_get_implementation(
216 css::uno::XComponentContext
*context
,
217 css::uno::Sequence
<css::uno::Any
> const &)
219 return cppu::acquire(static_cast<cppu::OWeakObject
*>(
220 Singleton::get(context
).instance
.get()));
223 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */