LibreOffice Module extensions (master) 1
propertyhandler.cxx
Go to the documentation of this file.
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#include <memory>
21#include "propertyhandler.hxx"
22#include "formmetadata.hxx"
23#include "formbrowsertools.hxx"
24#include "handlerhelper.hxx"
25#include "formstrings.hxx"
26
27#include <com/sun/star/lang/NullPointerException.hpp>
28#include <com/sun/star/util/XModifiable.hpp>
29#include <com/sun/star/script/Converter.hpp>
30
32#include <tools/debug.hxx>
37#include <rtl/ref.hxx>
38
39#include <algorithm>
40
41namespace pcr
42{
43
44 using namespace ::com::sun::star::uno;
45 using namespace ::com::sun::star::awt;
46 using namespace ::com::sun::star::beans;
47 using namespace ::com::sun::star::script;
48 using namespace ::com::sun::star::lang;
49 using namespace ::com::sun::star::util;
50 using namespace ::com::sun::star::frame;
51 using namespace ::com::sun::star::inspection;
52 using namespace ::comphelper;
53
54
55 PropertyHandler::PropertyHandler( const Reference< XComponentContext >& _rxContext )
57 ,m_bSupportedPropertiesAreKnown( false )
58 ,m_aPropertyListeners( m_aMutex )
59 ,m_xContext( _rxContext )
60 ,m_pInfoService ( new OPropertyInfoService )
61 {
62
63 m_xTypeConverter = Converter::create(_rxContext);
64 }
65
66 PropertyHandler::~PropertyHandler()
67 {
68 }
69
70 void SAL_CALL PropertyHandler::inspect( const Reference< XInterface >& _rxIntrospectee )
71 {
72 if ( !_rxIntrospectee.is() )
73 throw NullPointerException();
74
75 ::osl::MutexGuard aGuard( m_aMutex );
76
77 Reference< XPropertySet > xNewComponent( _rxIntrospectee, UNO_QUERY );
78 if ( xNewComponent == m_xComponent )
79 return;
80
81 // remove all old property change listeners
83 ::comphelper::OInterfaceIteratorHelper3 readdListener(m_aPropertyListeners); // will copy the container as needed
84 while ( removeListener.hasMoreElements() )
85 removePropertyChangeListener( removeListener.next() );
86 OSL_ENSURE( m_aPropertyListeners.getLength() == 0, "PropertyHandler::inspect: derived classes are expected to forward the removePropertyChangeListener call to their base class (me)!" );
87
88 // remember the new component, and give derived classes the chance to react on it
89 m_xComponent = xNewComponent;
90 onNewComponent();
91
92 // add the listeners, again
93 while ( readdListener.hasMoreElements() )
94 addPropertyChangeListener( readdListener.next() );
95 }
96
97 void PropertyHandler::onNewComponent()
98 {
99 if ( m_xComponent.is() )
100 m_xComponentPropertyInfo = m_xComponent->getPropertySetInfo();
101 else
102 m_xComponentPropertyInfo.clear();
103
104 m_bSupportedPropertiesAreKnown = false;
105 m_aSupportedProperties.realloc( 0 );
106 }
107
108 Sequence< Property > SAL_CALL PropertyHandler::getSupportedProperties()
109 {
110 ::osl::MutexGuard aGuard( m_aMutex );
111 if ( !m_bSupportedPropertiesAreKnown )
112 {
113 m_aSupportedProperties = StlSyntaxSequence<css::beans::Property>(doDescribeSupportedProperties());
114 m_bSupportedPropertiesAreKnown = true;
115 }
116 return m_aSupportedProperties;
117 }
118
119 Sequence< OUString > SAL_CALL PropertyHandler::getSupersededProperties( )
120 {
121 return Sequence< OUString >();
122 }
123
124 Sequence< OUString > SAL_CALL PropertyHandler::getActuatingProperties( )
125 {
126 return Sequence< OUString >();
127 }
128
129 Any SAL_CALL PropertyHandler::convertToPropertyValue( const OUString& _rPropertyName, const Any& _rControlValue )
130 {
131 ::osl::MutexGuard aGuard( m_aMutex );
132 PropertyId nPropId = m_pInfoService->getPropertyId( _rPropertyName );
133 Property aProperty( impl_getPropertyFromName_throw( _rPropertyName ) );
134
135 Any aPropertyValue;
136 if ( !_rControlValue.hasValue() )
137 // NULL is converted to NULL
138 return aPropertyValue;
139
140 if ( ( m_pInfoService->getPropertyUIFlags( nPropId ) & PROP_FLAG_ENUM ) != 0 )
141 {
142 OUString sControlValue;
143 OSL_VERIFY( _rControlValue >>= sControlValue );
145 new DefaultEnumRepresentation( *m_pInfoService, aProperty.Type, nPropId ) );
146 // TODO/UNOize: cache those converters?
147 aEnumConversion->getValueFromDescription( sControlValue, aPropertyValue );
148 }
149 else
150 aPropertyValue = PropertyHandlerHelper::convertToPropertyValue(
151 m_xContext, m_xTypeConverter, aProperty, _rControlValue );
152 return aPropertyValue;
153 }
154
155 Any SAL_CALL PropertyHandler::convertToControlValue( const OUString& _rPropertyName, const Any& _rPropertyValue, const Type& _rControlValueType )
156 {
157 ::osl::MutexGuard aGuard( m_aMutex );
158 PropertyId nPropId = m_pInfoService->getPropertyId( _rPropertyName );
159
160 if ( ( m_pInfoService->getPropertyUIFlags( nPropId ) & PROP_FLAG_ENUM ) != 0 )
161 {
162 DBG_ASSERT( _rControlValueType.getTypeClass() == TypeClass_STRING, "PropertyHandler::convertToControlValue: ENUM, but not STRING?" );
163
165 new DefaultEnumRepresentation( *m_pInfoService, _rPropertyValue.getValueType(), nPropId ) );
166 // TODO/UNOize: cache those converters?
167 return Any( aEnumConversion->getDescriptionForValue( _rPropertyValue ) );
168 }
169
170 return PropertyHandlerHelper::convertToControlValue(
171 m_xContext, m_xTypeConverter, _rPropertyValue, _rControlValueType );
172 }
173
174 PropertyState SAL_CALL PropertyHandler::getPropertyState( const OUString& /*_rPropertyName*/ )
175 {
176 return PropertyState_DIRECT_VALUE;
177 }
178
179 LineDescriptor SAL_CALL PropertyHandler::describePropertyLine( const OUString& _rPropertyName,
180 const Reference< XPropertyControlFactory >& _rxControlFactory )
181 {
182 if ( !_rxControlFactory.is() )
183 throw NullPointerException();
184
185 ::osl::MutexGuard aGuard( m_aMutex );
186 PropertyId nPropId( impl_getPropertyId_throwUnknownProperty( _rPropertyName ) );
187 const Property& rProperty( impl_getPropertyFromId_throw( nPropId ) );
188
189 LineDescriptor aDescriptor;
190 if ( ( m_pInfoService->getPropertyUIFlags( nPropId ) & PROP_FLAG_ENUM ) != 0 )
191 {
192 aDescriptor.Control = PropertyHandlerHelper::createListBoxControl(
193 _rxControlFactory, m_pInfoService->getPropertyEnumRepresentations( nPropId ),
194 PropertyHandlerHelper::requiresReadOnlyControl( rProperty.Attributes ), false );
195 }
196 else
197 PropertyHandlerHelper::describePropertyLine( rProperty, aDescriptor, _rxControlFactory );
198
199 aDescriptor.HelpURL = HelpIdUrl::getHelpURL( m_pInfoService->getPropertyHelpId( nPropId ) );
200 aDescriptor.DisplayName = m_pInfoService->getPropertyTranslation( nPropId );
201
202 if ( ( m_pInfoService->getPropertyUIFlags( nPropId ) & PROP_FLAG_DATA_PROPERTY ) != 0 )
203 aDescriptor.Category = "Data";
204 else
205 aDescriptor.Category = "General";
206 return aDescriptor;
207 }
208
209 sal_Bool SAL_CALL PropertyHandler::isComposable( const OUString& _rPropertyName )
210 {
211 ::osl::MutexGuard aGuard( m_aMutex );
212 return m_pInfoService->isComposeable( _rPropertyName );
213 }
214
215 InteractiveSelectionResult SAL_CALL PropertyHandler::onInteractivePropertySelection( const OUString& /*_rPropertyName*/, sal_Bool /*_bPrimary*/, Any& /*_rData*/, const Reference< XObjectInspectorUI >& /*_rxInspectorUI*/ )
216 {
217 OSL_FAIL( "PropertyHandler::onInteractivePropertySelection: not implemented!" );
218 return InteractiveSelectionResult_Cancelled;
219 }
220
221 void SAL_CALL PropertyHandler::actuatingPropertyChanged( const OUString& /*_rActuatingPropertyName*/, const Any& /*_rNewValue*/, const Any& /*_rOldValue*/, const Reference< XObjectInspectorUI >& /*_rxInspectorUI*/, sal_Bool /*_bFirstTimeInit*/ )
222 {
223 OSL_FAIL( "PropertyHandler::actuatingPropertyChanged: not implemented!" );
224 }
225
226 void SAL_CALL PropertyHandler::addPropertyChangeListener( const Reference< XPropertyChangeListener >& _rxListener )
227 {
228 ::osl::MutexGuard aGuard( m_aMutex );
229 if ( !_rxListener.is() )
230 throw NullPointerException();
231 m_aPropertyListeners.addInterface( _rxListener );
232 }
233
234 void SAL_CALL PropertyHandler::removePropertyChangeListener( const Reference< XPropertyChangeListener >& _rxListener )
235 {
236 ::osl::MutexGuard aGuard( m_aMutex );
237 m_aPropertyListeners.removeInterface( _rxListener );
238 }
239
240 sal_Bool SAL_CALL PropertyHandler::suspend( sal_Bool /*_bSuspend*/ )
241 {
242 return true;
243 }
244
246
247 void SAL_CALL PropertyHandler::disposing()
248 {
249 m_xComponent.clear();
250 m_aPropertyListeners.clear();
251 m_xTypeConverter.clear();
252 m_aSupportedProperties.realloc( 0 );
253 }
254
255 void PropertyHandler::firePropertyChange( const OUString& _rPropName, PropertyId _nPropId, const Any& _rOldValue, const Any& _rNewValue )
256 {
257 PropertyChangeEvent aEvent;
258 aEvent.Source = m_xComponent;
259 aEvent.PropertyHandle = _nPropId;
260 aEvent.PropertyName = _rPropName;
261 aEvent.OldValue = _rOldValue;
262 aEvent.NewValue = _rNewValue;
263 m_aPropertyListeners.notifyEach( &XPropertyChangeListener::propertyChange, aEvent );
264 }
265
266 const Property* PropertyHandler::impl_getPropertyFromId_nothrow( PropertyId _nPropId ) const
267 {
268 const_cast< PropertyHandler* >( this )->getSupportedProperties();
269 const Property* pFound = std::find_if( m_aSupportedProperties.begin(), m_aSupportedProperties.end(),
270 FindPropertyByHandle( _nPropId )
271 );
272 if ( pFound != m_aSupportedProperties.end() )
273 return pFound;
274 return nullptr;
275 }
276
277 const Property& PropertyHandler::impl_getPropertyFromId_throw( PropertyId _nPropId ) const
278 {
279 const Property* pProperty = impl_getPropertyFromId_nothrow( _nPropId );
280 if ( !pProperty )
281 throw UnknownPropertyException();
282
283 return *pProperty;
284 }
285
286 const Property& PropertyHandler::impl_getPropertyFromName_throw( const OUString& _rPropertyName ) const
287 {
288 const_cast< PropertyHandler* >( this )->getSupportedProperties();
289 StlSyntaxSequence< Property >::const_iterator pFound = std::find_if( m_aSupportedProperties.begin(), m_aSupportedProperties.end(),
290 FindPropertyByName( _rPropertyName )
291 );
292 if ( pFound == m_aSupportedProperties.end() )
293 throw UnknownPropertyException(_rPropertyName);
294
295 return *pFound;
296 }
297
298 void PropertyHandler::implAddPropertyDescription( std::vector< Property >& _rProperties, const OUString& _rPropertyName, const Type& _rType, sal_Int16 _nAttribs ) const
299 {
300 _rProperties.push_back( Property(
301 _rPropertyName,
302 m_pInfoService->getPropertyId( _rPropertyName ),
303 _rType,
304 _nAttribs
305 ) );
306 }
307
308 weld::Window* PropertyHandler::impl_getDefaultDialogFrame_nothrow() const
309 {
310 return PropertyHandlerHelper::getDialogParentFrame(m_xContext);
311 }
312
313 PropertyId PropertyHandler::impl_getPropertyId_throwUnknownProperty( const OUString& _rPropertyName ) const
314 {
315 PropertyId nPropId = m_pInfoService->getPropertyId( _rPropertyName );
316 if ( nPropId == -1 )
317 throw UnknownPropertyException(_rPropertyName);
318 return nPropId;
319 }
320
321 PropertyId PropertyHandler::impl_getPropertyId_throwRuntime( const OUString& _rPropertyName ) const
322 {
323 PropertyId nPropId = m_pInfoService->getPropertyId( _rPropertyName );
324 if ( nPropId == -1 )
325 throw RuntimeException();
326 return nPropId;
327 }
328
329 PropertyId PropertyHandler::impl_getPropertyId_nothrow( const OUString& _rPropertyName ) const
330 {
331 return m_pInfoService->getPropertyId( _rPropertyName );
332 }
333
334 void PropertyHandler::impl_setContextDocumentModified_nothrow() const
335 {
336 Reference< XModifiable > xModifiable( impl_getContextDocument_nothrow(), UNO_QUERY );
337 if ( xModifiable.is() )
338 xModifiable->setModified( true );
339 }
340
341 bool PropertyHandler::impl_componentHasProperty_throw( const OUString& _rPropName ) const
342 {
343 return m_xComponentPropertyInfo.is() && m_xComponentPropertyInfo->hasPropertyByName( _rPropName );
344 }
345
346 sal_Int16 PropertyHandler::impl_getDocumentMeasurementUnit_throw() const
347 {
348 FieldUnit eUnit = FieldUnit::NONE;
349
350 Reference< XServiceInfo > xDocumentSI( impl_getContextDocument_nothrow(), UNO_QUERY );
351 OSL_ENSURE( xDocumentSI.is(), "PropertyHandlerHelper::impl_getDocumentMeasurementUnit_throw: No context document - where do I live?" );
352 if ( xDocumentSI.is() )
353 {
354 // determine the application type we live in
355 OUString sConfigurationLocation;
356 OUString sConfigurationProperty;
357 if ( xDocumentSI->supportsService( SERVICE_WEB_DOCUMENT ) )
358 { // writer
359 sConfigurationLocation = "/org.openoffice.Office.WriterWeb/Layout/Other";
360 sConfigurationProperty = "MeasureUnit";
361 }
362 else if ( xDocumentSI->supportsService( SERVICE_TEXT_DOCUMENT ) )
363 { // writer
364 sConfigurationLocation = "/org.openoffice.Office.Writer/Layout/Other";
365 sConfigurationProperty = "MeasureUnit";
366 }
367 else if ( xDocumentSI->supportsService( SERVICE_SPREADSHEET_DOCUMENT ) )
368 { // calc
369 sConfigurationLocation = "/org.openoffice.Office.Calc/Layout/Other/MeasureUnit";
370 sConfigurationProperty = "Metric";
371 }
372 else if ( xDocumentSI->supportsService( SERVICE_DRAWING_DOCUMENT ) )
373 {
374 sConfigurationLocation = "/org.openoffice.Office.Draw/Layout/Other/MeasureUnit";
375 sConfigurationProperty = "Metric";
376 }
377 else if ( xDocumentSI->supportsService( SERVICE_PRESENTATION_DOCUMENT ) )
378 {
379 sConfigurationLocation = "/org.openoffice.Office.Impress/Layout/Other/MeasureUnit";
380 sConfigurationProperty = "Metric";
381 }
382
383 // read the measurement unit from the configuration
384 if ( !(sConfigurationLocation.isEmpty() || sConfigurationProperty.isEmpty()) )
385 {
387 m_xContext, sConfigurationLocation, -1, ::utl::OConfigurationTreeRoot::CM_READONLY ) );
388 sal_Int32 nUnitAsInt = sal_Int32(FieldUnit::NONE);
389 aConfigTree.getNodeValue( sConfigurationProperty ) >>= nUnitAsInt;
390
391 // if this denotes a valid (and accepted) unit, then use it
392 if ( ( nUnitAsInt > sal_Int32(FieldUnit::NONE) ) && ( nUnitAsInt <= sal_Int32(FieldUnit::MM_100TH) ) )
393 eUnit = static_cast< FieldUnit >( nUnitAsInt );
394 }
395 }
396
397 if ( FieldUnit::NONE == eUnit )
398 {
400 eUnit = MeasurementSystem::Metric == eSystem ? FieldUnit::CM : FieldUnit::INCH;
401 }
402
404 }
405
406 PropertyHandlerComponent::PropertyHandlerComponent( const Reference< XComponentContext >& _rxContext )
407 :PropertyHandler( _rxContext )
408 {
409 }
410
413
414 sal_Bool SAL_CALL PropertyHandlerComponent::supportsService( const OUString& ServiceName )
415 {
417 }
418
419} // namespace pcr
420
421/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
AnyEventRef aEvent
MeasurementSystem getMeasurementSystemEnum() const
const LocaleDataWrapper & GetLocaleData() const
static sal_Int16 ConvertToMeasurementUnit(FieldUnit _nFieldUnit, sal_Int16 _rFieldToUNOValueFactor)
css::uno::Reference< ListenerT > const & next()
an implementation of the IPropertyEnumRepresentation
PropertyHandler implementation which additionally supports XServiceInfo.
the base class for property handlers
PropertyHandler(const css::uno::Reference< css::uno::XComponentContext > &_rxContext)
const_iterator end() const
Definition: pcrcommon.hxx:88
const ELEMENT * const_iterator
Definition: pcrcommon.hxx:84
css::uno::Any getNodeValue(const OUString &_rPath) const noexcept
static OConfigurationTreeRoot createWithComponentContext(const css::uno::Reference< css::uno::XComponentContext > &_rxContext, const OUString &_rPath, sal_Int32 _nDepth=-1, CREATION_MODE _eMode=CM_UPDATABLE)
#define DBG_ASSERT(sCon, aError)
Reference< XComponentContext > m_xContext
Definition: filehandler.cxx:78
FieldUnit
#define PROP_FLAG_DATA_PROPERTY
#define PROP_FLAG_ENUM
constexpr OUStringLiteral SERVICE_TEXT_DOCUMENT
constexpr OUStringLiteral SERVICE_PRESENTATION_DOCUMENT
constexpr OUStringLiteral SERVICE_DRAWING_DOCUMENT
constexpr OUStringLiteral SERVICE_WEB_DOCUMENT
constexpr OUStringLiteral SERVICE_SPREADSHEET_DOCUMENT
MeasurementSystem
::osl::Mutex m_aMutex
Definition: logger.cxx:98
void removeListener(const InterfaceRef &xObject, const css::uno::Reference< css::lang::XEventListener > &xListener)
Type
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
a property handler for any virtual string properties
Definition: browserline.cxx:39
sal_Int32 PropertyId
::cppu::WeakComponentImplHelper< css::inspection::XPropertyHandler > PropertyHandler_Base
IMPLEMENT_FORWARD_XTYPEPROVIDER2(ChildWindowPane, ChildWindowPaneInterfaceBase, Pane)
IMPLEMENT_FORWARD_XINTERFACE2(ChildWindowPane, ChildWindowPaneInterfaceBase, Pane)
#define IMPLEMENT_FORWARD_XCOMPONENT(classname, baseclass)
Definition: pcrcommon.hxx:105
unsigned char sal_Bool