1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef EXTENSIONS_SOURCE_PROPCTRLR_PCROMPONENTCONTEXT_HXX
25 #define EXTENSIONS_SOURCE_PROPCTRLR_PCROMPONENTCONTEXT_HXX
26 
27 /** === begin UNO includes === **/
28 #include <com/sun/star/uno/XComponentContext.hpp>
29 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
30 /** === end UNO includes === **/
31 
32 //........................................................................
33 namespace pcr
34 {
35 //........................................................................
36 
37 	//====================================================================
38 	//= ComponentContext
39 	//====================================================================
40     /** a helper class for working with a component context
41     */
42 	class ComponentContext
43 	{
44     private:
45         ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >        m_xContext;
46         ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiComponentFactory >  m_xORB;
47 
48     public:
49         /** constructs an instance
50             @param _rxContext
51                 the component context to manage
52             @throws ::com::sun::star::lang::NullPointerException
53                 if the given context, or its component factory, are <NULL/>
54         */
55         ComponentContext( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& _rxContext );
56 
57         /** returns the ->XComponentContext interface
58         */
59         inline ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >
getUNOContext() const60             getUNOContext() const { return m_xContext; }
61 
62         /** determines whether the context is not <NULL/>
63         */
is() const64         inline sal_Bool is() const
65         {
66             return m_xContext.is();
67         }
68 
69         /** creates a component using our component factory/context
70             @throws ::com::sun::star::uno::Exception
71             @return
72                 <TRUE/> if and only if the component could be successfully created
73         */
74         template < class INTERFACE >
createComponent(const::rtl::OUString & _rServiceName,::com::sun::star::uno::Reference<INTERFACE> & _out_rxComponent) const75         bool createComponent( const ::rtl::OUString& _rServiceName, ::com::sun::star::uno::Reference< INTERFACE >& _out_rxComponent ) const
76         {
77             _out_rxComponent.clear();
78             _out_rxComponent = _out_rxComponent.query(
79                 m_xORB->createInstanceWithContext( _rServiceName, m_xContext )
80             );
81             return _out_rxComponent.is();
82         }
83 
84         /** creates a component using our component factory/context
85             @throws ::com::sun::star::uno::Exception
86             @return
87                 <TRUE/> if and only if the component could be successfully created
88         */
89         template < class INTERFACE >
createComponent(const sal_Char * _pAsciiServiceName,::com::sun::star::uno::Reference<INTERFACE> & _out_rxComponent) const90         bool createComponent( const sal_Char* _pAsciiServiceName, ::com::sun::star::uno::Reference< INTERFACE >& _out_rxComponent ) const
91         {
92             return createComponent( ::rtl::OUString::createFromAscii( _pAsciiServiceName ), _out_rxComponent );
93         }
94 
95         /** creates a component using our component factory/context
96 
97             @throws ::com::sun::star::lang::ServiceNotRegisteredException
98                 if the given service is not registered
99             @throws Exception
100                 if an exception occurred during creating the component
101             @return
102                 the newly created component. Is never <NULL/>.
103         */
104         ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > createComponent( const ::rtl::OUString& _rServiceName ) const;
105 
106         /** creates a component using our component factory/context
107 
108             @throws ::com::sun::star::lang::ServiceNotRegisteredException
109                 if the given service is not registered
110             @throws Exception
111                 if an exception occurred during creating the component
112             @return
113                 the newly created component. Is never <NULL/>.
114         */
createComponent(const sal_Char * _pAsciiServiceName) const115         ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > createComponent( const sal_Char* _pAsciiServiceName ) const
116         {
117             return createComponent( ::rtl::OUString::createFromAscii( _pAsciiServiceName ) );
118         }
119 
120         /** returns the ->XMultiServiceFactory interface of ->m_xORB, for passing to
121             older code which does not yet support ->XMultiComponentFactory
122             @throws ::com::sun::star::uno::RuntimeException
123                 if our our component factory does not support this interface
124         */
125         ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >
126             getLegacyServiceFactory() const;
127 
128         /** retrieves a value from our component context
129             @param _rName
130                 the name of the value to retrieve
131             @return
132                 the context value with the given name
133             @seealso XComponentContext::getValueByName
134             @seealso getContextValueByAsciiName
135         */
136         ::com::sun::star::uno::Any
137                 getContextValueByName( const ::rtl::OUString& _rName ) const;
138 
139         /** retrieves a value from our component context, specified by 8-bit ASCII string
140             @param _rName
141                 the name of the value to retrieve, as ASCII character string
142             @return
143                 the context value with the given name
144             @seealso XComponentContext::getValueByName
145             @seealso getContextValueByName
146         */
147         inline ::com::sun::star::uno::Any
getContextValueByAsciiName(const sal_Char * _pAsciiName) const148                 getContextValueByAsciiName( const sal_Char* _pAsciiName ) const
149         {
150             return getContextValueByName( ::rtl::OUString::createFromAscii( _pAsciiName ) );
151         }
152 
153 		/** retrieve context to create interfaces by the ctors
154 		*/
getContext() const155         ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext > getContext() const { return        m_xContext;}
156 
157 	};
158 
159 //........................................................................
160 } // namespace pcr
161 //........................................................................
162 
163 #endif // EXTENSIONS_SOURCE_PROPCTRLR_PCROMPONENTCONTEXT_HXX
164 
165