Line data Source code
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 : #ifndef EXTENSIONS_SOURCE_PROPCTRLR_PCROMPONENTCONTEXT_HXX
21 : #define EXTENSIONS_SOURCE_PROPCTRLR_PCROMPONENTCONTEXT_HXX
22 :
23 : #include <com/sun/star/uno/XComponentContext.hpp>
24 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
25 :
26 : //........................................................................
27 : namespace pcr
28 : {
29 : //........................................................................
30 :
31 : //====================================================================
32 : //= ComponentContext
33 : //====================================================================
34 : /** a helper class for working with a component context
35 : */
36 0 : class ComponentContext
37 : {
38 : private:
39 : ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext > m_xContext;
40 : ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiComponentFactory > m_xORB;
41 :
42 : public:
43 : /** constructs an instance
44 : @param _rxContext
45 : the component context to manage
46 : @throws ::com::sun::star::lang::NullPointerException
47 : if the given context, or its component factory, are <NULL/>
48 : */
49 : ComponentContext( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& _rxContext );
50 :
51 : /** returns the ->XComponentContext interface
52 : */
53 : inline ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >
54 0 : getUNOContext() const { return m_xContext; }
55 :
56 : /** determines whether the context is not <NULL/>
57 : */
58 : inline sal_Bool is() const
59 : {
60 : return m_xContext.is();
61 : }
62 :
63 : /** creates a component using our component factory/context
64 : @throws ::com::sun::star::uno::Exception
65 : @return
66 : <TRUE/> if and only if the component could be successfully created
67 : */
68 : template < class INTERFACE >
69 0 : bool createComponent( const ::rtl::OUString& _rServiceName, ::com::sun::star::uno::Reference< INTERFACE >& _out_rxComponent ) const
70 : {
71 0 : _out_rxComponent.clear();
72 0 : _out_rxComponent = _out_rxComponent.query(
73 : m_xORB->createInstanceWithContext( _rServiceName, m_xContext )
74 : );
75 0 : return _out_rxComponent.is();
76 : }
77 :
78 : /** creates a component using our component factory/context
79 : @throws ::com::sun::star::uno::Exception
80 : @return
81 : <TRUE/> if and only if the component could be successfully created
82 : */
83 : template < class INTERFACE >
84 0 : bool createComponent( const sal_Char* _pAsciiServiceName, ::com::sun::star::uno::Reference< INTERFACE >& _out_rxComponent ) const
85 : {
86 0 : return createComponent( ::rtl::OUString::createFromAscii( _pAsciiServiceName ), _out_rxComponent );
87 : }
88 :
89 : /** creates a component using our component factory/context
90 :
91 : @throws ::com::sun::star::lang::ServiceNotRegisteredException
92 : if the given service is not registered
93 : @throws Exception
94 : if an exception occurred during creating the component
95 : @return
96 : the newly created component. Is never <NULL/>.
97 : */
98 : ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > createComponent( const ::rtl::OUString& _rServiceName ) const;
99 :
100 : /** creates a component using our component factory/context
101 :
102 : @throws ::com::sun::star::lang::ServiceNotRegisteredException
103 : if the given service is not registered
104 : @throws Exception
105 : if an exception occurred during creating the component
106 : @return
107 : the newly created component. Is never <NULL/>.
108 : */
109 0 : ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > createComponent( const sal_Char* _pAsciiServiceName ) const
110 : {
111 0 : return createComponent( ::rtl::OUString::createFromAscii( _pAsciiServiceName ) );
112 : }
113 :
114 : /** returns the ->XMultiServiceFactory interface of ->m_xORB, for passing to
115 : older code which does not yet support ->XMultiComponentFactory
116 : @throws ::com::sun::star::uno::RuntimeException
117 : if our our component factory does not support this interface
118 : */
119 : ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >
120 : getLegacyServiceFactory() const;
121 :
122 : /** retrieves a value from our component context
123 : @param _rName
124 : the name of the value to retrieve
125 : @return
126 : the context value with the given name
127 : @seealso XComponentContext::getValueByName
128 : @seealso getContextValueByAsciiName
129 : */
130 : ::com::sun::star::uno::Any
131 : getContextValueByName( const ::rtl::OUString& _rName ) const;
132 :
133 : /** retrieves a value from our component context, specified by 8-bit ASCII string
134 : @param _rName
135 : the name of the value to retrieve, as ASCII character string
136 : @return
137 : the context value with the given name
138 : @seealso XComponentContext::getValueByName
139 : @seealso getContextValueByName
140 : */
141 : inline ::com::sun::star::uno::Any
142 0 : getContextValueByAsciiName( const sal_Char* _pAsciiName ) const
143 : {
144 0 : return getContextValueByName( ::rtl::OUString::createFromAscii( _pAsciiName ) );
145 : }
146 :
147 : /** retrieve context to create interfaces by the ctors
148 : */
149 0 : ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext > getContext() const { return m_xContext;}
150 :
151 : };
152 :
153 : //........................................................................
154 : } // namespace pcr
155 : //........................................................................
156 :
157 : #endif // EXTENSIONS_SOURCE_PROPCTRLR_PCROMPONENTCONTEXT_HXX
158 :
159 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|