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 #ifndef INCLUDED_EXTENSIONS_SOURCE_PROPCTRLR_PROPERTYHANDLER_HXX
21 #define INCLUDED_EXTENSIONS_SOURCE_PROPCTRLR_PROPERTYHANDLER_HXX
23 #include "pcrcommon.hxx"
24 #include "modulepcr.hxx"
26 #include <com/sun/star/uno/XComponentContext.hpp>
27 #include <com/sun/star/beans/PropertyState.hpp>
28 #include <com/sun/star/beans/XPropertySet.hpp>
29 #include <com/sun/star/beans/Property.hpp>
30 #include <com/sun/star/script/XTypeConverter.hpp>
31 #include <com/sun/star/frame/XModel.hpp>
32 #include <com/sun/star/uno/Sequence.hxx>
33 #include <com/sun/star/uno/Any.hxx>
34 #include <com/sun/star/util/Date.hpp>
35 #include <com/sun/star/util/Time.hpp>
36 #include <com/sun/star/util/DateTime.hpp>
37 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
38 #include <com/sun/star/inspection/XPropertyHandler.hpp>
39 #include <com/sun/star/lang/XServiceInfo.hpp>
40 #include <osl/interlck.h>
41 #include <cppuhelper/basemutex.hxx>
42 #include <cppuhelper/compbase.hxx>
43 #include <cppuhelper/implbase1.hxx>
44 #include <comphelper/uno3.hxx>
49 namespace com
{ namespace sun
{ namespace star
{
50 namespace inspection
{
51 struct LineDescriptor
;
52 class XPropertyControlFactory
;
56 namespace vcl
{ class Window
; }
57 namespace weld
{ class Window
; }
63 typedef sal_Int32 PropertyId
;
68 class OPropertyInfoService
;
69 typedef ::cppu::WeakComponentImplHelper
< css::inspection::XPropertyHandler
70 > PropertyHandler_Base
;
71 /** the base class for property handlers
73 class PropertyHandler
: public ::cppu::BaseMutex
74 , public PropertyHandler_Base
77 /// cache for getSupportedProperties
78 mutable StlSyntaxSequence
< css::beans::Property
>
79 m_aSupportedProperties
;
80 mutable bool m_bSupportedPropertiesAreKnown
;
83 /// the property listener which has been registered
84 PropertyChangeListeners m_aPropertyListeners
;
87 /// the context in which the instance was created
88 css::uno::Reference
< css::uno::XComponentContext
> m_xContext
;
89 /// the component we're inspecting
90 css::uno::Reference
< css::beans::XPropertySet
> m_xComponent
;
91 /// info about our component's properties
92 css::uno::Reference
< css::beans::XPropertySetInfo
> m_xComponentPropertyInfo
;
93 /// type converter, needed on various occasions
94 css::uno::Reference
< css::script::XTypeConverter
> m_xTypeConverter
;
95 /// access to property meta data
96 std::unique_ptr
< OPropertyInfoService
> m_pInfoService
;
99 explicit PropertyHandler(
100 const css::uno::Reference
< css::uno::XComponentContext
>& _rxContext
102 virtual ~PropertyHandler() override
;
104 // default implementations for XPropertyHandler
105 virtual void SAL_CALL
inspect( const css::uno::Reference
< css::uno::XInterface
>& _rxIntrospectee
) override
;
106 virtual css::uno::Sequence
< css::beans::Property
> SAL_CALL
getSupportedProperties() override
;
107 virtual css::uno::Sequence
< OUString
> SAL_CALL
getSupersededProperties( ) override
;
108 virtual css::uno::Sequence
< OUString
> SAL_CALL
getActuatingProperties( ) override
;
109 virtual css::uno::Any SAL_CALL
convertToPropertyValue( const OUString
& _rPropertyName
, const css::uno::Any
& _rControlValue
) override
;
110 virtual css::uno::Any SAL_CALL
convertToControlValue( const OUString
& _rPropertyName
, const css::uno::Any
& _rPropertyValue
, const css::uno::Type
& _rControlValueType
) override
;
111 virtual css::beans::PropertyState SAL_CALL
getPropertyState( const OUString
& _rPropertyName
) override
;
112 virtual css::inspection::LineDescriptor SAL_CALL
describePropertyLine( const OUString
& _rPropertyName
, const css::uno::Reference
< css::inspection::XPropertyControlFactory
>& _rxControlFactory
) override
;
113 virtual sal_Bool SAL_CALL
isComposable( const OUString
& _rPropertyName
) override
;
114 virtual css::inspection::InteractiveSelectionResult SAL_CALL
onInteractivePropertySelection( const OUString
& _rPropertyName
, sal_Bool _bPrimary
, css::uno::Any
& _rData
, const css::uno::Reference
< css::inspection::XObjectInspectorUI
>& _rxInspectorUI
) override
;
115 virtual void SAL_CALL
actuatingPropertyChanged( const OUString
& _rActuatingPropertyName
, const css::uno::Any
& _rNewValue
, const css::uno::Any
& _rOldValue
, const css::uno::Reference
< css::inspection::XObjectInspectorUI
>& _rxInspectorUI
, sal_Bool _bFirstTimeInit
) override
;
116 virtual void SAL_CALL
addPropertyChangeListener( const css::uno::Reference
< css::beans::XPropertyChangeListener
>& _rxListener
) override
;
117 virtual void SAL_CALL
removePropertyChangeListener( const css::uno::Reference
< css::beans::XPropertyChangeListener
>& _rxListener
) override
;
118 virtual sal_Bool SAL_CALL
suspend( sal_Bool _bSuspend
) override
;
122 virtual void SAL_CALL
disposing() override
;
125 virtual css::uno::Sequence
< css::beans::Property
>
126 doDescribeSupportedProperties() const = 0;
128 /// called when XPropertyHandler::inspect has been called, and we thus have a new component to inspect
129 virtual void onNewComponent();
132 /** fires the change in a property value to our listener (if any)
133 @see addPropertyChangeListener
135 void firePropertyChange( const OUString
& _rPropName
, PropertyId _nPropId
,
136 const css::uno::Any
& _rOldValue
, const css::uno::Any
& _rNewValue
);
138 /** retrieves a window which can be used as parent for dialogs
140 vcl::Window
* impl_getDefaultDialogParent_nothrow() const;
141 weld::Window
* impl_getDefaultDialogFrame_nothrow() const;
143 /** retrieves the property id for a given property name
144 @throw css::beans::UnknownPropertyException
145 if the property name is not known to our ->m_pInfoService
147 PropertyId
impl_getPropertyId_throwUnknownProperty( const OUString
& _rPropertyName
) const;
149 /** retrieves the property id for a given property name
150 @throw css::uno::RuntimeException
151 if the property name is not known to our ->m_pInfoService
153 PropertyId
impl_getPropertyId_throwRuntime( const OUString
& _rPropertyName
) const;
156 /** retrieves the property id for a given property name
158 if the property name is not known to our ->m_pInfoService
160 PropertyId
impl_getPropertyId_nothrow( const OUString
& _rPropertyName
) const;
162 // helper for implementing doDescribeSupportedProperties
163 /** adds a description for the given string property to the given property vector
164 Most probably to be called from within getSupportedProperties
166 inline void addStringPropertyDescription(
167 std::vector
< css::beans::Property
>& _rProperties
,
168 const OUString
& _rPropertyName
171 /** adds a description for the given int32 property to the given property vector
173 inline void addInt32PropertyDescription(
174 std::vector
< css::beans::Property
>& _rProperties
,
175 const OUString
& _rPropertyName
,
176 sal_Int16 _nAttribs
= 0
179 /** adds a description for the given int16 property to the given property vector
181 inline void addInt16PropertyDescription(
182 std::vector
< css::beans::Property
>& _rProperties
,
183 const OUString
& _rPropertyName
,
184 sal_Int16 _nAttribs
= 0
187 /** adds a description for the given double property to the given property vector
189 inline void addDoublePropertyDescription(
190 std::vector
< css::beans::Property
>& _rProperties
,
191 const OUString
& _rPropertyName
,
195 /** adds a description for the given date property to the given property vector
197 inline void addDatePropertyDescription(
198 std::vector
< css::beans::Property
>& _rProperties
,
199 const OUString
& _rPropertyName
,
203 /** adds a description for the given time property to the given property vector
205 inline void addTimePropertyDescription(
206 std::vector
< css::beans::Property
>& _rProperties
,
207 const OUString
& _rPropertyName
,
211 /** adds a description for the given DateTime property to the given property vector
213 inline void addDateTimePropertyDescription(
214 std::vector
< css::beans::Property
>& _rProperties
,
215 const OUString
& _rPropertyName
,
219 /// adds a Property, given by name only, to a given vector of Properties
220 void implAddPropertyDescription(
221 std::vector
< css::beans::Property
>& _rProperties
,
222 const OUString
& _rPropertyName
,
223 const css::uno::Type
& _rType
,
224 sal_Int16 _nAttribs
= 0
228 // helper for accessing and maintaining meta data about our supported properties
230 /** retrieves a property given by handle
233 a pointer to the descriptor for the given properties, if it is one of our
234 supported properties, <NULL/> else.
236 @see doDescribeSupportedProperties
237 @see impl_getPropertyFromId_throw
239 const css::beans::Property
*
240 impl_getPropertyFromId_nothrow( PropertyId _nPropId
) const;
242 /** retrieves a property given by handle
244 @throws UnknownPropertyException
245 if the handler does not support a property with the given handle
247 @seealso doDescribeSupportedProperties
248 @see impl_getPropertyFromId_nothrow
250 const css::beans::Property
&
251 impl_getPropertyFromId_throw( PropertyId _nPropId
) const;
253 /** determines whether a given property id is part of our supported properties
254 @see getSupportedProperties
255 @see doDescribeSupportedProperties
257 bool impl_isSupportedProperty_nothrow( PropertyId _nPropId
) const
259 return impl_getPropertyFromId_nothrow( _nPropId
) != nullptr;
262 /** retrieves a property given by name
264 @throws UnknownPropertyException
265 if the handler does not support a property with the given name
267 @seealso doDescribeSupportedProperties
269 const css::beans::Property
&
270 impl_getPropertyFromName_throw( const OUString
& _rPropertyName
) const;
272 /** get the name of a property given by handle
275 impl_getPropertyNameFromId_nothrow( PropertyId _nPropId
) const;
277 /** returns the value of the ContextDocument property in the ComponentContext which was used to create
280 css::uno::Reference
< css::frame::XModel
>
281 impl_getContextDocument_nothrow() const
283 return css::uno::Reference
< css::frame::XModel
>(
284 m_xContext
->getValueByName( "ContextDocument" ), css::uno::UNO_QUERY
);
287 /** marks the context document as modified
289 @see impl_getContextDocument_nothrow
291 void impl_setContextDocumentModified_nothrow() const;
293 /// determines whether our component has a given property
294 bool impl_componentHasProperty_throw( const OUString
& _rPropName
) const;
296 /** determines the default measure unit for the document in which our component lives
298 sal_Int16
impl_getDocumentMeasurementUnit_throw() const;
301 PropertyHandler( const PropertyHandler
& ) = delete;
302 PropertyHandler
& operator=( const PropertyHandler
& ) = delete;
306 inline void PropertyHandler::addStringPropertyDescription( std::vector
< css::beans::Property
>& _rProperties
, const OUString
& _rPropertyName
) const
308 implAddPropertyDescription( _rProperties
, _rPropertyName
, ::cppu::UnoType
<OUString
>::get() );
311 inline void PropertyHandler::addInt32PropertyDescription( std::vector
< css::beans::Property
>& _rProperties
, const OUString
& _rPropertyName
, sal_Int16 _nAttribs
) const
313 implAddPropertyDescription( _rProperties
, _rPropertyName
, ::cppu::UnoType
<sal_Int32
>::get(), _nAttribs
);
316 inline void PropertyHandler::addInt16PropertyDescription( std::vector
< css::beans::Property
>& _rProperties
, const OUString
& _rPropertyName
, sal_Int16 _nAttribs
) const
318 implAddPropertyDescription( _rProperties
, _rPropertyName
, ::cppu::UnoType
<sal_Int16
>::get(), _nAttribs
);
321 inline void PropertyHandler::addDoublePropertyDescription( std::vector
< css::beans::Property
>& _rProperties
, const OUString
& _rPropertyName
, sal_Int16 _nAttribs
) const
323 implAddPropertyDescription( _rProperties
, _rPropertyName
, ::cppu::UnoType
<double>::get(), _nAttribs
);
326 inline void PropertyHandler::addDatePropertyDescription( std::vector
< css::beans::Property
>& _rProperties
, const OUString
& _rPropertyName
, sal_Int16 _nAttribs
) const
328 implAddPropertyDescription( _rProperties
, _rPropertyName
, ::cppu::UnoType
<css::util::Date
>::get(), _nAttribs
);
331 inline void PropertyHandler::addTimePropertyDescription( std::vector
< css::beans::Property
>& _rProperties
, const OUString
& _rPropertyName
, sal_Int16 _nAttribs
) const
333 implAddPropertyDescription( _rProperties
, _rPropertyName
, ::cppu::UnoType
<css::util::Time
>::get(), _nAttribs
);
336 inline void PropertyHandler::addDateTimePropertyDescription( std::vector
< css::beans::Property
>& _rProperties
, const OUString
& _rPropertyName
, sal_Int16 _nAttribs
) const
338 implAddPropertyDescription( _rProperties
, _rPropertyName
, ::cppu::UnoType
<css::util::DateTime
>::get(), _nAttribs
);
341 inline OUString
PropertyHandler::impl_getPropertyNameFromId_nothrow( PropertyId _nPropId
) const
343 const css::beans::Property
* pProp
= impl_getPropertyFromId_nothrow( _nPropId
);
344 return pProp
? pProp
->Name
: OUString();
348 //= PropertyHandlerComponent
350 typedef ::cppu::ImplHelper1
< css::lang::XServiceInfo
351 > PropertyHandlerComponent_Base
;
352 /** PropertyHandler implementation which additionally supports XServiceInfo
354 class PropertyHandlerComponent
:public PropertyHandler
355 ,public PropertyHandlerComponent_Base
358 explicit PropertyHandlerComponent(
359 const css::uno::Reference
< css::uno::XComponentContext
>& _rxContext
363 DECLARE_XTYPEPROVIDER()
366 virtual OUString SAL_CALL
getImplementationName( ) override
= 0;
367 virtual sal_Bool SAL_CALL
supportsService( const OUString
& ServiceName
) override
;
368 virtual css::uno::Sequence
< OUString
> SAL_CALL
getSupportedServiceNames( ) override
= 0;
372 //= HandlerComponentBase
374 /** a PropertyHandlerComponent implementation which routes XServiceInfo::getImplementationName and
375 XServiceInfo::getSupportedServiceNames to static versions of those methods, which are part of
378 Additionally, a method <member>Create</member> is provided which takes a component context, and returns a new
379 instance of the derived class. This <member>Create</member> is used to register the implementation
380 of the derived class at the <type>PcrModule</type>.
382 Well, every time we're talking about derived class, we in fact mean the template argument of
383 <type>HandlerComponentBase</type>. But usually this equals your derived class:
386 typedef HandlerComponentBase< MyHandler > MyHandler_Base;
387 class MyHandler : MyHandler_Base
391 static OUString SAL_CALL getImplementationName_static( ) throw (css::uno::RuntimeException);
392 static css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames_static( ) throw (css::uno::RuntimeException);
396 template < class HANDLER
>
397 class HandlerComponentBase
: public PropertyHandlerComponent
400 explicit HandlerComponentBase( const css::uno::Reference
< css::uno::XComponentContext
>& _rxContext
)
401 :PropertyHandlerComponent( _rxContext
)
407 virtual OUString SAL_CALL
getImplementationName( ) override
;
408 virtual css::uno::Sequence
< OUString
> SAL_CALL
getSupportedServiceNames( ) override
;
409 static css::uno::Reference
< css::uno::XInterface
> SAL_CALL
Create( const css::uno::Reference
< css::uno::XComponentContext
>& _rxContext
);
412 /** registers the implementation of HANDLER at the <type>PcrModule</type>
414 static void registerImplementation();
418 template < class HANDLER
>
419 OUString SAL_CALL HandlerComponentBase
< HANDLER
>::getImplementationName( )
421 return HANDLER::getImplementationName_static();
425 template < class HANDLER
>
426 css::uno::Sequence
< OUString
> SAL_CALL HandlerComponentBase
< HANDLER
>::getSupportedServiceNames( )
428 return HANDLER::getSupportedServiceNames_static();
432 template < class HANDLER
>
433 void HandlerComponentBase
< HANDLER
>::registerImplementation()
435 PcrModule::getInstance().registerImplementation(
436 HANDLER::getImplementationName_static(),
437 HANDLER::getSupportedServiceNames_static(),
443 template < class HANDLER
>
444 css::uno::Reference
< css::uno::XInterface
> SAL_CALL HandlerComponentBase
< HANDLER
>::Create( const css::uno::Reference
< css::uno::XComponentContext
>& _rxContext
)
446 return *( new HANDLER( _rxContext
) );
453 #endif // INCLUDED_EXTENSIONS_SOURCE_PROPCTRLR_PROPERTYHANDLER_HXX
455 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */