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/XModuleManager2.hpp>
28 #include <com/sun/star/frame/XFrame.hpp>
29 #include <com/sun/star/lang/XServiceInfo.hpp>
30 #include <com/sun/star/lang/XSingleComponentFactory.hpp>
31 #include <com/sun/star/uno/XComponentContext.hpp>
33 #include <cppuhelper/basemutex.hxx>
34 #include <cppuhelper/compbase2.hxx>
35 #include <cppuhelper/supportsservice.hxx>
36 #include <tools/diagnose_ex.h>
38 using namespace ::com::sun::star
;
39 using namespace framework
;
43 typedef ::cppu::WeakComponentImplHelper2
<
44 com::sun::star::lang::XServiceInfo
,
45 com::sun::star::lang::XSingleComponentFactory
> WindowContentFactoryManager_BASE
;
47 class WindowContentFactoryManager
: private cppu::BaseMutex
,
48 public WindowContentFactoryManager_BASE
51 WindowContentFactoryManager( const css::uno::Reference
< css::uno::XComponentContext
>& rxContext
);
52 virtual ~WindowContentFactoryManager();
54 virtual OUString SAL_CALL
getImplementationName()
55 throw (css::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
57 return OUString("com.sun.star.comp.framework.WindowContentFactoryManager");
60 virtual sal_Bool SAL_CALL
supportsService(OUString
const & ServiceName
)
61 throw (css::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
63 return cppu::supportsService(this, ServiceName
);
66 virtual css::uno::Sequence
<OUString
> SAL_CALL
getSupportedServiceNames()
67 throw (css::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
69 css::uno::Sequence
< OUString
> aSeq(1);
70 aSeq
[0] = "com.sun.star.ui.WindowContentFactoryManager";
74 // XSingleComponentFactory
75 virtual css::uno::Reference
< css::uno::XInterface
> SAL_CALL
createInstanceWithContext( const css::uno::Reference
< css::uno::XComponentContext
>& Context
) throw (css::uno::Exception
, css::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
76 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
) throw (css::uno::Exception
, css::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
79 virtual void SAL_CALL
disposing() SAL_OVERRIDE
;
81 css::uno::Reference
< css::uno::XComponentContext
> m_xContext
;
83 ConfigurationAccess_FactoryManager
* m_pConfigAccess
;
86 WindowContentFactoryManager::WindowContentFactoryManager( const uno::Reference
< uno::XComponentContext
>& rxContext
) :
87 WindowContentFactoryManager_BASE(m_aMutex
),
88 m_xContext( rxContext
),
89 m_bConfigRead( false )
91 m_pConfigAccess
= new ConfigurationAccess_FactoryManager( m_xContext
,
92 "/org.openoffice.Office.UI.WindowContentFactories/Registered/ContentFactories" );
93 m_pConfigAccess
->acquire();
96 WindowContentFactoryManager::~WindowContentFactoryManager()
101 void SAL_CALL
WindowContentFactoryManager::disposing()
103 osl::MutexGuard
g(rBHelper
.rMutex
);
107 // reduce reference count
108 m_pConfigAccess
->release();
113 // XSingleComponentFactory
114 uno::Reference
< uno::XInterface
> SAL_CALL
WindowContentFactoryManager::createInstanceWithContext(
115 const uno::Reference
< uno::XComponentContext
>& /*xContext*/ )
116 throw (uno::Exception
, uno::RuntimeException
, std::exception
)
118 uno::Reference
< uno::XInterface
> xWindow
;
122 uno::Reference
< uno::XInterface
> SAL_CALL
WindowContentFactoryManager::createInstanceWithArgumentsAndContext(
123 const uno::Sequence
< uno::Any
>& Arguments
, const uno::Reference
< uno::XComponentContext
>& Context
)
124 throw (uno::Exception
, uno::RuntimeException
, std::exception
)
126 uno::Reference
< uno::XInterface
> xWindow
;
127 uno::Reference
< frame::XFrame
> xFrame
;
128 OUString aResourceURL
;
130 for (sal_Int32 i
=0; i
< Arguments
.getLength(); i
++ )
132 beans::PropertyValue aPropValue
;
133 if ( Arguments
[i
] >>= aPropValue
)
135 if ( aPropValue
.Name
== "Frame" )
136 aPropValue
.Value
>>= xFrame
;
137 else if ( aPropValue
.Name
== "ResourceURL" )
138 aPropValue
.Value
>>= aResourceURL
;
142 // Determine the module identifier
146 uno::Reference
< frame::XModuleManager2
> xModuleManager
=
147 frame::ModuleManager::create( m_xContext
);
150 if ( xFrame
.is() && xModuleManager
.is() )
151 aModuleId
= xModuleManager
->identify( uno::Reference
< uno::XInterface
>( xFrame
, uno::UNO_QUERY
) );
153 catch ( const frame::UnknownModuleException
& )
157 RetrieveTypeNameFromResourceURL( aResourceURL
, aType
, aName
);
158 if ( !aType
.isEmpty() &&
160 !aModuleId
.isEmpty() )
162 OUString aImplementationName
;
163 uno::Reference
< uno::XInterface
> xHolder( static_cast<cppu::OWeakObject
*>(this), uno::UNO_QUERY
);
165 // Detetmine the implementation name of the window content factory dependent on the
166 // module identifier, user interface element type and name
168 osl::MutexGuard
g(rBHelper
.rMutex
);
169 if ( !m_bConfigRead
)
171 m_bConfigRead
= true;
172 m_pConfigAccess
->readConfigurationData();
174 aImplementationName
= m_pConfigAccess
->getFactorySpecifierFromTypeNameModule( aType
, aName
, aModuleId
);
177 if ( !aImplementationName
.isEmpty() )
179 uno::Reference
< lang::XMultiServiceFactory
> xServiceManager( Context
->getServiceManager(), uno::UNO_QUERY
);
180 if ( xServiceManager
.is() )
182 uno::Reference
< lang::XSingleComponentFactory
> xFactory(
183 xServiceManager
->createInstance( aImplementationName
), uno::UNO_QUERY
);
186 // Be careful: We call external code. Therefore here we have to catch all exceptions
189 xWindow
= xFactory
->createInstanceWithArgumentsAndContext( Arguments
, Context
);
191 catch ( uno::Exception
& )
193 DBG_UNHANDLED_EXCEPTION();
203 // Fallback: Use internal factory code to create a toolkit dialog as a content window
204 xWindow
= createInstanceWithContext(Context
);
212 css::uno::Reference
<css::uno::XComponentContext
> const & context
):
213 instance(static_cast<cppu::OWeakObject
*>(
214 new WindowContentFactoryManager(context
)))
218 css::uno::Reference
<css::uno::XInterface
> instance
;
222 public rtl::StaticWithArg
<
223 Instance
, css::uno::Reference
<css::uno::XComponentContext
>, Singleton
>
228 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
* SAL_CALL
229 com_sun_star_comp_framework_WindowContentFactoryManager_get_implementation(
230 css::uno::XComponentContext
*context
,
231 css::uno::Sequence
<css::uno::Any
> const &)
233 return cppu::acquire(static_cast<cppu::OWeakObject
*>(
234 Singleton::get(context
).instance
.get()));
237 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */