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