cid#1607171 Data race condition
[LibreOffice.git] / reportdesign / source / ui / inspection / DataProviderHandler.cxx
blob18d4c286339dc36e7551eb7ba4bcf6a62a908dc5
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 #include <sal/config.h>
22 #include <string_view>
24 #include <DataProviderHandler.hxx>
25 #include <comphelper/namedvaluecollection.hxx>
26 #include <comphelper/sequence.hxx>
27 #include <comphelper/types.hxx>
28 #include <comphelper/propertysequence.hxx>
29 #include <cppuhelper/supportsservice.hxx>
30 #include <strings.hxx>
31 #include <com/sun/star/form/inspection/FormComponentPropertyHandler.hpp>
32 #include <com/sun/star/inspection/PropertyControlType.hpp>
33 #include <com/sun/star/inspection/PropertyLineElement.hpp>
34 #include <com/sun/star/lang/NullPointerException.hpp>
35 #include <com/sun/star/chart/ChartDataRowSource.hpp>
36 #include <com/sun/star/chart2/FormattedString.hpp>
37 #include <com/sun/star/chart2/XTitled.hpp>
38 #include <com/sun/star/chart2/XTitle.hpp>
39 #include <com/sun/star/chart2/data/XDataReceiver.hpp>
40 #include <com/sun/star/ui/dialogs/XExecutableDialog.hpp>
41 #include <com/sun/star/report/XReportDefinition.hpp>
42 #include <com/sun/star/script/Converter.hpp>
43 #include <com/sun/star/container/XNameContainer.hpp>
44 #include <metadata.hxx>
45 #include <osl/mutex.hxx>
46 #include <comphelper/diagnose_ex.hxx>
47 #include <core_resource.hxx>
48 #include <helpids.h>
49 #include <strings.hrc>
50 #include <PropertyForward.hxx>
51 #include <utility>
53 namespace rptui
56 using namespace ::com::sun::star;
58 DataProviderHandler::DataProviderHandler(uno::Reference< uno::XComponentContext > context)
59 :DataProviderHandler_Base(m_aMutex)
60 ,m_xContext(std::move(context))
62 try
64 m_xFormComponentHandler = form::inspection::FormComponentPropertyHandler::create(m_xContext);
65 m_xTypeConverter = script::Converter::create(m_xContext);
67 }catch(const uno::Exception &)
72 OUString SAL_CALL DataProviderHandler::getImplementationName( )
74 return u"com.sun.star.comp.report.DataProviderHandler"_ustr;
77 sal_Bool SAL_CALL DataProviderHandler::supportsService( const OUString& ServiceName )
79 return cppu::supportsService(this, ServiceName);
82 uno::Sequence< OUString > SAL_CALL DataProviderHandler::getSupportedServiceNames( )
84 return { u"com.sun.star.report.inspection.DataProviderHandler"_ustr };
87 // override WeakComponentImplHelperBase::disposing()
88 // This function is called upon disposing the component,
89 // if your component needs special work when it becomes
90 // disposed, do it here.
91 void SAL_CALL DataProviderHandler::disposing()
93 ::comphelper::disposeComponent(m_xFormComponentHandler);
94 ::comphelper::disposeComponent( m_xMasterDetails );
95 ::comphelper::disposeComponent(m_xTypeConverter);
97 void SAL_CALL DataProviderHandler::addEventListener(const uno::Reference< lang::XEventListener > & xListener)
99 m_xFormComponentHandler->addEventListener(xListener);
102 void SAL_CALL DataProviderHandler::removeEventListener(const uno::Reference< lang::XEventListener > & aListener)
104 m_xFormComponentHandler->removeEventListener(aListener);
107 // inspection::XPropertyHandler:
109 /********************************************************************************/
110 void SAL_CALL DataProviderHandler::inspect(const uno::Reference< uno::XInterface > & Component)
114 uno::Reference< container::XNameContainer > xNameCont(Component,uno::UNO_QUERY);
115 static constexpr OUString sFormComponent(u"FormComponent"_ustr);
116 if ( xNameCont->hasByName(sFormComponent) )
118 uno::Reference<beans::XPropertySet> xProp(xNameCont->getByName(sFormComponent),uno::UNO_QUERY);
119 static constexpr OUString sModel(u"Model"_ustr);
120 if ( xProp.is() && xProp->getPropertySetInfo()->hasPropertyByName(sModel) )
122 m_xChartModel.set(xProp->getPropertyValue(sModel),uno::UNO_QUERY);
123 if ( m_xChartModel.is() )
124 m_xFormComponent = m_xChartModel->getDataProvider();
127 m_xDataProvider.set(m_xFormComponent,uno::UNO_QUERY);
128 m_xReportComponent.set( xNameCont->getByName(u"ReportComponent"_ustr), uno::UNO_QUERY );
129 if ( m_xDataProvider.is() )
131 auto aNoConverter = std::make_shared<AnyConverter>();
132 TPropertyNamePair aPropertyMediation;
133 aPropertyMediation.emplace( PROPERTY_MASTERFIELDS, TPropertyConverter(PROPERTY_MASTERFIELDS,aNoConverter) );
134 aPropertyMediation.emplace( PROPERTY_DETAILFIELDS, TPropertyConverter(PROPERTY_DETAILFIELDS,aNoConverter) );
136 m_xMasterDetails = new OPropertyMediator( m_xDataProvider, m_xReportComponent, std::move(aPropertyMediation), true );
139 catch(const uno::Exception &)
141 throw lang::NullPointerException();
143 if ( m_xFormComponent.is() )
145 m_xFormComponentHandler->inspect(m_xFormComponent);
149 uno::Any SAL_CALL DataProviderHandler::getPropertyValue(const OUString & PropertyName)
151 ::osl::MutexGuard aGuard( m_aMutex );
152 uno::Any aPropertyValue;
153 const sal_Int32 nId = OPropertyInfoService::getPropertyId(PropertyName);
154 switch(nId)
156 case PROPERTY_ID_CHARTTYPE:
157 // TODO: We need a possibility to get the UI of the selected chart type
158 // LEM: this business of ignoring ChartType seems very fishy!
159 //if( m_xChartModel.is() )
161 // uno::Reference< chart2::XDiagram > xDiagram( m_xChartModel->getFirstDiagram() );
162 // if( xDiagram.is() )
163 // {
164 // OUString sChartTypes;
165 // uno::Reference< chart2::XCoordinateSystemContainer > xCooSysCnt( xDiagram, uno::UNO_QUERY_THROW );
166 // const uno::Sequence< uno::Reference< chart2::XCoordinateSystem > > aCooSysSeq( xCooSysCnt->getCoordinateSystems());
167 // const uno::Reference< chart2::XCoordinateSystem >* pIter = aCooSysSeq.getConstArray();
168 // const uno::Reference< chart2::XCoordinateSystem >* pEnd = pIter + aCooSysSeq.getLength();
169 // for(;pIter != pEnd;++pIter)
170 // {
171 // const uno::Reference< chart2::XChartTypeContainer > xCTCnt( *pIter, uno::UNO_QUERY_THROW );
172 // const uno::Sequence< uno::Reference< chart2::XChartType > > aCTSeq( xCTCnt->getChartTypes());
173 // const uno::Reference< chart2::XChartType >* pChartTypeIter = aCTSeq.getConstArray();
174 // const uno::Reference< chart2::XChartType >* pChartTypeEnd = pChartTypeIter + aCTSeq.getLength();
175 // for(;pChartTypeIter != pChartTypeEnd;++pChartTypeIter)
176 // {
177 // sChartTypes += (*pChartTypeIter)->getChartType();
178 // sChartTypes += ";";
179 // }
180 // }
181 // aPropertyValue;// <<= sChartTypes;
182 // }
184 break;
185 case PROPERTY_ID_PREVIEW_COUNT:
186 if (m_xDataProvider) // tdf#117159 crash with chart in database report otherwise
187 aPropertyValue <<= m_xDataProvider->getRowLimit();
188 break;
189 default:
190 aPropertyValue = m_xFormComponentHandler->getPropertyValue( PropertyName );
191 break;
193 return aPropertyValue;
196 void SAL_CALL DataProviderHandler::setPropertyValue(const OUString & PropertyName, const uno::Any & Value)
198 ::osl::MutexGuard aGuard( m_aMutex );
199 const sal_Int32 nId = OPropertyInfoService::getPropertyId(PropertyName);
200 switch(nId)
202 case PROPERTY_ID_CHARTTYPE:
203 break;
204 case PROPERTY_ID_PREVIEW_COUNT:
205 m_xDataProvider->setPropertyValue(PropertyName,Value);
206 break;
207 default:
208 m_xFormComponentHandler->setPropertyValue(PropertyName, Value);
209 break;
213 void DataProviderHandler::impl_updateChartTitle_throw(const uno::Any& _aValue)
215 uno::Reference<chart2::XTitled> xTitled(m_xChartModel,uno::UNO_QUERY);
216 if ( !xTitled.is() )
217 return;
219 uno::Reference<chart2::XTitle> xTitle = xTitled->getTitleObject();
220 if ( !xTitle.is() )
222 xTitle.set(m_xContext->getServiceManager()->createInstanceWithContext(u"com.sun.star.chart2.Title"_ustr,m_xContext),uno::UNO_QUERY);
223 xTitled->setTitleObject(xTitle);
225 if ( xTitle.is() )
227 uno::Reference< chart2::XFormattedString2> xFormatted = chart2::FormattedString::create(m_xContext);
228 OUString sStr;
229 _aValue >>= sStr;
230 xFormatted->setString(sStr);
231 xTitle->setText({ xFormatted });
235 beans::PropertyState SAL_CALL DataProviderHandler::getPropertyState(const OUString & PropertyName)
237 return m_xFormComponentHandler->getPropertyState(PropertyName);
240 inspection::LineDescriptor SAL_CALL DataProviderHandler::describePropertyLine(const OUString & PropertyName, const uno::Reference< inspection::XPropertyControlFactory > & _xControlFactory)
242 inspection::LineDescriptor aOut;
243 const sal_Int32 nId = OPropertyInfoService::getPropertyId(PropertyName);
244 switch(nId)
246 case PROPERTY_ID_CHARTTYPE:
247 aOut.PrimaryButtonId = UID_RPT_PROP_CHARTTYPE_DLG;
248 aOut.Control = _xControlFactory->createPropertyControl(inspection::PropertyControlType::TextField , true);
249 aOut.HasPrimaryButton = true;
250 break;
251 case PROPERTY_ID_PREVIEW_COUNT:
252 aOut.Control = _xControlFactory->createPropertyControl(inspection::PropertyControlType::NumericField , false);
253 break;
254 case PROPERTY_ID_MASTERFIELDS:
255 case PROPERTY_ID_DETAILFIELDS:
256 aOut.Control = _xControlFactory->createPropertyControl(inspection::PropertyControlType::StringListField , false);
257 aOut.PrimaryButtonId = UID_RPT_PROP_DLG_LINKFIELDS;
258 aOut.HasPrimaryButton = true;
259 break;
260 default:
261 aOut = m_xFormComponentHandler->describePropertyLine(PropertyName, _xControlFactory);
263 if ( nId != -1 )
265 aOut.Category = (OPropertyInfoService::getPropertyUIFlags(nId ) & PropUIFlags::DataProperty) ?
266 std::u16string_view(u"Data")
268 std::u16string_view(u"General");
269 aOut.HelpURL = HelpIdUrl::getHelpURL( OPropertyInfoService::getPropertyHelpId( nId ) );
270 aOut.DisplayName = OPropertyInfoService::getPropertyTranslation(nId);
272 return aOut;
275 uno::Any SAL_CALL DataProviderHandler::convertToPropertyValue(const OUString & _rPropertyValue, const uno::Any & _rControlValue)
277 ::osl::MutexGuard aGuard( m_aMutex );
278 uno::Any aPropertyValue( _rControlValue );
279 const sal_Int32 nId = OPropertyInfoService::getPropertyId(_rPropertyValue);
280 switch(nId)
282 case PROPERTY_ID_CHARTTYPE:
283 break;
284 case PROPERTY_ID_PREVIEW_COUNT:
287 aPropertyValue = m_xTypeConverter->convertTo( _rControlValue, ::cppu::UnoType<sal_Int32>::get());
289 catch( const uno::Exception& )
291 TOOLS_WARN_EXCEPTION( "reportdesign", "DataProviderHandler::convertToPropertyValue: caught an exception while converting via TypeConverter!" );
293 break;
294 case PROPERTY_ID_MASTERFIELDS:
295 case PROPERTY_ID_DETAILFIELDS:
296 break;
297 default:
298 aPropertyValue = m_xFormComponentHandler->convertToPropertyValue(_rPropertyValue, _rControlValue);
300 return aPropertyValue;
303 uno::Any SAL_CALL DataProviderHandler::convertToControlValue(const OUString & _rPropertyName, const uno::Any & _rPropertyValue, const uno::Type & ControlValueType)
305 uno::Any aControlValue( _rPropertyValue );
306 if ( !aControlValue.hasValue() )
307 // NULL is converted to NULL
308 return aControlValue;
310 ::osl::MutexGuard aGuard( m_aMutex );
311 const sal_Int32 nId = OPropertyInfoService::getPropertyId(_rPropertyName);
312 switch(nId)
314 case PROPERTY_ID_CHARTTYPE:
315 break;
316 case PROPERTY_ID_MASTERFIELDS:
317 case PROPERTY_ID_DETAILFIELDS:
318 case PROPERTY_ID_PREVIEW_COUNT:
321 aControlValue = m_xTypeConverter->convertTo( _rPropertyValue, ControlValueType);
323 catch( const uno::Exception& )
325 TOOLS_WARN_EXCEPTION( "reportdesign", "GeometryHandler::convertToPropertyValue: caught an exception while converting via TypeConverter!" );
327 break;
328 default:
329 aControlValue = m_xFormComponentHandler->convertToControlValue(_rPropertyName, _rPropertyValue, ControlValueType);
331 return aControlValue;
334 void SAL_CALL DataProviderHandler::addPropertyChangeListener(const uno::Reference< beans::XPropertyChangeListener > & Listener)
336 m_xFormComponentHandler->addPropertyChangeListener(Listener);
339 void SAL_CALL DataProviderHandler::removePropertyChangeListener(const uno::Reference< beans::XPropertyChangeListener > & _rxListener)
341 m_xFormComponentHandler->removePropertyChangeListener(_rxListener);
344 uno::Sequence< beans::Property > SAL_CALL DataProviderHandler::getSupportedProperties()
346 ::std::vector< beans::Property > aNewProps;
347 if( m_xChartModel.is() )
349 rptui::OPropertyInfoService::getExcludeProperties( aNewProps, m_xFormComponentHandler );
350 beans::Property aValue;
351 static constexpr OUString s_pProperties[] =
353 PROPERTY_CHARTTYPE
354 ,PROPERTY_MASTERFIELDS
355 ,PROPERTY_DETAILFIELDS
356 ,PROPERTY_PREVIEW_COUNT
359 for (const auto & rName : s_pProperties)
361 aValue.Name = rName;
362 aNewProps.push_back(aValue);
365 return uno::Sequence< beans::Property >(aNewProps.data(), aNewProps.size());
368 uno::Sequence< OUString > SAL_CALL DataProviderHandler::getSupersededProperties()
370 uno::Sequence< OUString > aRet { PROPERTY_TITLE }; // have a look at OPropertyInfoService::getExcludeProperties
371 return aRet;
374 uno::Sequence< OUString > SAL_CALL DataProviderHandler::getActuatingProperties()
376 ::osl::MutexGuard aGuard( m_aMutex );
378 uno::Sequence< OUString > aSeq { PROPERTY_TITLE };
379 return ::comphelper::concatSequences(m_xFormComponentHandler->getActuatingProperties(),aSeq);
382 sal_Bool SAL_CALL DataProviderHandler::isComposable( const OUString& _rPropertyName )
384 return OPropertyInfoService::isComposable( _rPropertyName, m_xFormComponentHandler );
387 inspection::InteractiveSelectionResult SAL_CALL DataProviderHandler::onInteractivePropertySelection(const OUString & PropertyName, sal_Bool Primary, uno::Any & out_Data, const uno::Reference< inspection::XObjectInspectorUI > & _rxInspectorUI)
389 if ( !_rxInspectorUI.is() )
390 throw lang::NullPointerException();
392 inspection::InteractiveSelectionResult eResult = inspection::InteractiveSelectionResult_Cancelled;
393 ::osl::ClearableMutexGuard aGuard( m_aMutex );
395 const sal_Int32 nId = OPropertyInfoService::getPropertyId(PropertyName);
396 switch(nId)
398 case PROPERTY_ID_CHARTTYPE:
399 if ( impl_dialogChartType_nothrow(aGuard) )
400 eResult = inspection::InteractiveSelectionResult_ObtainedValue;
401 break;
402 case PROPERTY_ID_MASTERFIELDS:
403 case PROPERTY_ID_DETAILFIELDS:
404 if ( impl_dialogLinkedFields_nothrow( aGuard ) )
405 eResult = inspection::InteractiveSelectionResult_Success;
406 break;
407 default:
408 eResult = m_xFormComponentHandler->onInteractivePropertySelection(PropertyName, Primary, out_Data, _rxInspectorUI);
411 return eResult;
414 void SAL_CALL DataProviderHandler::actuatingPropertyChanged(const OUString & ActuatingPropertyName, const uno::Any & NewValue, const uno::Any & OldValue, const uno::Reference< inspection::XObjectInspectorUI > & InspectorUI, sal_Bool FirstTimeInit)
416 osl::MutexGuard aGuard( m_aMutex );
418 if ( ActuatingPropertyName == PROPERTY_COMMAND )
420 if ( NewValue != OldValue )
422 uno::Reference< report::XReportDefinition> xReport = m_xReportComponent->getSection()->getReportDefinition();
423 bool bDoEnableMasterDetailFields = xReport.is() && !xReport->getCommand().isEmpty() && !m_xDataProvider->getCommand().isEmpty();
424 InspectorUI->enablePropertyUIElements( PROPERTY_DETAILFIELDS, inspection::PropertyLineElement::PrimaryButton, bDoEnableMasterDetailFields );
425 InspectorUI->enablePropertyUIElements( PROPERTY_MASTERFIELDS, inspection::PropertyLineElement::PrimaryButton, bDoEnableMasterDetailFields );
427 bool bModified = xReport->isModified();
428 // this fills the chart again
429 ::comphelper::NamedValueCollection aArgs;
430 aArgs.put( u"CellRangeRepresentation"_ustr, uno::Any( u"all"_ustr ) );
431 aArgs.put( u"HasCategories"_ustr, uno::Any( true ) );
432 aArgs.put( u"FirstCellAsLabel"_ustr, uno::Any( true ) );
433 aArgs.put( u"DataRowSource"_ustr, uno::Any( chart::ChartDataRowSource_COLUMNS ) );
434 uno::Reference< chart2::data::XDataReceiver > xReceiver(m_xChartModel,uno::UNO_QUERY_THROW);
435 xReceiver->setArguments( aArgs.getPropertyValues() );
436 if ( !bModified )
437 xReport->setModified(false);
439 m_xFormComponentHandler->actuatingPropertyChanged(ActuatingPropertyName, NewValue, OldValue, InspectorUI, FirstTimeInit);
441 else if ( ActuatingPropertyName == PROPERTY_TITLE )
443 if ( NewValue != OldValue )
444 impl_updateChartTitle_throw(NewValue);
446 else
448 const sal_Int32 nId = OPropertyInfoService::getPropertyId(ActuatingPropertyName);
449 switch(nId)
452 case PROPERTY_ID_MASTERFIELDS:
453 break;
454 case PROPERTY_ID_DETAILFIELDS:
455 break;
456 default:
457 m_xFormComponentHandler->actuatingPropertyChanged(ActuatingPropertyName, NewValue, OldValue, InspectorUI, FirstTimeInit);
462 sal_Bool SAL_CALL DataProviderHandler::suspend(sal_Bool Suspend)
464 return m_xFormComponentHandler->suspend(Suspend);
466 bool DataProviderHandler::impl_dialogLinkedFields_nothrow( ::osl::ClearableMutexGuard& _rClearBeforeDialog ) const
468 uno::Sequence<uno::Any> aSeq(comphelper::InitAnyPropertySequence(
470 {"ParentWindow", m_xContext->getValueByName(u"DialogParentWindow"_ustr)},
471 {"Detail", uno::Any(m_xDataProvider)},
472 {"Master", uno::Any(m_xReportComponent->getSection()->getReportDefinition())},
473 {"Explanation", uno::Any(RptResId(RID_STR_EXPLANATION))},
474 {"DetailLabel", uno::Any(RptResId(RID_STR_DETAILLABEL))},
475 {"MasterLabel", uno::Any(RptResId(RID_STR_MASTERLABEL))},
476 }));
478 uno::Reference< ui::dialogs::XExecutableDialog > xDialog(
479 m_xContext->getServiceManager()->createInstanceWithArgumentsAndContext(
480 u"org.openoffice.comp.form.ui.MasterDetailLinkDialog"_ustr, aSeq, m_xContext),
481 uno::UNO_QUERY);
483 _rClearBeforeDialog.clear();
484 return ( xDialog->execute() != 0 );
487 bool DataProviderHandler::impl_dialogChartType_nothrow( ::osl::ClearableMutexGuard& _rClearBeforeDialog ) const
489 uno::Sequence<uno::Any> aSeq(comphelper::InitAnyPropertySequence(
491 {"ParentWindow", m_xContext->getValueByName(u"DialogParentWindow"_ustr)},
492 {"ChartModel", uno::Any(m_xChartModel)}
493 }));
495 uno::Reference< ui::dialogs::XExecutableDialog > xDialog(
496 m_xContext->getServiceManager()->createInstanceWithArgumentsAndContext(
497 u"com.sun.star.comp.chart2.ChartTypeDialog"_ustr, aSeq, m_xContext),
498 uno::UNO_QUERY);
500 _rClearBeforeDialog.clear();
501 return ( xDialog->execute() != 0 );
504 } // namespace rptui
507 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
508 reportdesign_DataProviderHandler_get_implementation(
509 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&)
511 return cppu::acquire(new rptui::DataProviderHandler(context));
514 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */