1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 #ifndef EXTENSIONS_SOURCE_PROPCTRLR_PCROMPONENTCONTEXT_HXX 29 #define EXTENSIONS_SOURCE_PROPCTRLR_PCROMPONENTCONTEXT_HXX 30 31 /** === begin UNO includes === **/ 32 #include <com/sun/star/uno/XComponentContext.hpp> 33 #include <com/sun/star/lang/XMultiServiceFactory.hpp> 34 /** === end UNO includes === **/ 35 36 //........................................................................ 37 namespace pcr 38 { 39 //........................................................................ 40 41 //==================================================================== 42 //= ComponentContext 43 //==================================================================== 44 /** a helper class for working with a component context 45 */ 46 class ComponentContext 47 { 48 private: 49 ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext > m_xContext; 50 ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiComponentFactory > m_xORB; 51 52 public: 53 /** constructs an instance 54 @param _rxContext 55 the component context to manage 56 @throws ::com::sun::star::lang::NullPointerException 57 if the given context, or its component factory, are <NULL/> 58 */ 59 ComponentContext( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& _rxContext ); 60 61 /** returns the ->XComponentContext interface 62 */ 63 inline ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext > 64 getUNOContext() const { return m_xContext; } 65 66 /** determines whether the context is not <NULL/> 67 */ 68 inline sal_Bool is() const 69 { 70 return m_xContext.is(); 71 } 72 73 /** creates a component using our component factory/context 74 @throws ::com::sun::star::uno::Exception 75 @return 76 <TRUE/> if and only if the component could be successfully created 77 */ 78 template < class INTERFACE > 79 bool createComponent( const ::rtl::OUString& _rServiceName, ::com::sun::star::uno::Reference< INTERFACE >& _out_rxComponent ) const 80 { 81 _out_rxComponent.clear(); 82 _out_rxComponent = _out_rxComponent.query( 83 m_xORB->createInstanceWithContext( _rServiceName, m_xContext ) 84 ); 85 return _out_rxComponent.is(); 86 } 87 88 /** creates a component using our component factory/context 89 @throws ::com::sun::star::uno::Exception 90 @return 91 <TRUE/> if and only if the component could be successfully created 92 */ 93 template < class INTERFACE > 94 bool createComponent( const sal_Char* _pAsciiServiceName, ::com::sun::star::uno::Reference< INTERFACE >& _out_rxComponent ) const 95 { 96 return createComponent( ::rtl::OUString::createFromAscii( _pAsciiServiceName ), _out_rxComponent ); 97 } 98 99 /** creates a component using our component factory/context 100 101 @throws ::com::sun::star::lang::ServiceNotRegisteredException 102 if the given service is not registered 103 @throws Exception 104 if an exception occured during creating the component 105 @return 106 the newly created component. Is never <NULL/>. 107 */ 108 ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > createComponent( const ::rtl::OUString& _rServiceName ) const; 109 110 /** creates a component using our component factory/context 111 112 @throws ::com::sun::star::lang::ServiceNotRegisteredException 113 if the given service is not registered 114 @throws Exception 115 if an exception occured during creating the component 116 @return 117 the newly created component. Is never <NULL/>. 118 */ 119 ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > createComponent( const sal_Char* _pAsciiServiceName ) const 120 { 121 return createComponent( ::rtl::OUString::createFromAscii( _pAsciiServiceName ) ); 122 } 123 124 /** returns the ->XMultiServiceFactory interface of ->m_xORB, for passing to 125 older code which does not yet support ->XMultiComponentFactory 126 @throws ::com::sun::star::uno::RuntimeException 127 if our our component factory does not support this interface 128 */ 129 ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > 130 getLegacyServiceFactory() const; 131 132 /** retrieves a value from our component context 133 @param _rName 134 the name of the value to retrieve 135 @return 136 the context value with the given name 137 @seealso XComponentContext::getValueByName 138 @seealso getContextValueByAsciiName 139 */ 140 ::com::sun::star::uno::Any 141 getContextValueByName( const ::rtl::OUString& _rName ) const; 142 143 /** retrieves a value from our component context, specified by 8-bit ASCII string 144 @param _rName 145 the name of the value to retrieve, as ASCII character string 146 @return 147 the context value with the given name 148 @seealso XComponentContext::getValueByName 149 @seealso getContextValueByName 150 */ 151 inline ::com::sun::star::uno::Any 152 getContextValueByAsciiName( const sal_Char* _pAsciiName ) const 153 { 154 return getContextValueByName( ::rtl::OUString::createFromAscii( _pAsciiName ) ); 155 } 156 157 /** retrieve context to create interfaces by the ctors 158 */ 159 ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext > getContext() const { return m_xContext;} 160 161 }; 162 163 //........................................................................ 164 } // namespace pcr 165 //........................................................................ 166 167 #endif // EXTENSIONS_SOURCE_PROPCTRLR_PCROMPONENTCONTEXT_HXX 168 169