cid#1607171 Data race condition
[LibreOffice.git] / reportdesign / source / core / sdr / PropertyForward.cxx
blob7685b11dc191a92884eb87361176db9a2e9c2eb1
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 .
19 #include <PropertyForward.hxx>
20 #include <com/sun/star/beans/PropertyAttribute.hpp>
21 #include <comphelper/property.hxx>
22 #include <comphelper/diagnose_ex.hxx>
23 #include <strings.hxx>
25 namespace rptui
28 using namespace ::com::sun::star::uno;
29 using namespace ::com::sun::star::beans;
31 OPropertyMediator::OPropertyMediator(const Reference< XPropertySet>& _xSource
32 ,const Reference< XPropertySet>& _xDest
33 ,TPropertyNamePair&& _aNameMap
34 ,bool _bReverse)
35 : OPropertyForward_Base(m_aMutex)
36 ,m_aNameMap(std::move(_aNameMap))
37 ,m_xSource(_xSource)
38 ,m_xDest(_xDest)
39 ,m_bInChange(false)
41 osl_atomic_increment(&m_refCount);
42 OSL_ENSURE(m_xDest.is(),"Dest is NULL!");
43 OSL_ENSURE(m_xSource.is(),"Source is NULL!");
44 if ( m_xDest.is() && m_xSource.is() )
46 try
48 m_xDestInfo = m_xDest->getPropertySetInfo();
49 m_xSourceInfo = m_xSource->getPropertySetInfo();
50 if ( _bReverse )
52 ::comphelper::copyProperties(m_xDest,m_xSource);
53 for (const auto& [rName, rPropConv] : m_aNameMap)
55 Property aProp = m_xSourceInfo->getPropertyByName(rName);
56 if (0 == (aProp.Attributes & PropertyAttribute::READONLY))
58 Any aValue = _xDest->getPropertyValue(rPropConv.first);
59 if ( 0 != (aProp.Attributes & PropertyAttribute::MAYBEVOID) || aValue.hasValue() )
60 _xSource->setPropertyValue(rName, rPropConv.second->operator()(rPropConv.first, aValue));
64 else
66 ::comphelper::copyProperties(m_xSource,m_xDest);
67 for (const auto& [rName, rPropConv] : m_aNameMap)
68 _xDest->setPropertyValue(rPropConv.first, rPropConv.second->operator()(rPropConv.first, _xSource->getPropertyValue(rName)));
70 startListening();
72 catch(Exception&)
74 DBG_UNHANDLED_EXCEPTION("reportdesign");
77 osl_atomic_decrement(&m_refCount);
80 OPropertyMediator::~OPropertyMediator()
84 void SAL_CALL OPropertyMediator::propertyChange( const PropertyChangeEvent& evt )
86 ::osl::MutexGuard aGuard(m_aMutex);
87 if ( m_bInChange )
88 return;
90 m_bInChange = true;
91 try
93 bool bDest = (evt.Source == m_xDest);
94 Reference<XPropertySet> xProp = bDest ? m_xSource : m_xDest;
95 Reference<XPropertySetInfo> xPropInfo = bDest ? m_xSourceInfo : m_xDestInfo;
96 if ( xProp.is() && xPropInfo.is() )
98 if ( xPropInfo->hasPropertyByName(evt.PropertyName) )
99 xProp->setPropertyValue(evt.PropertyName,evt.NewValue);
100 else
102 TPropertyNamePair::const_iterator aFind = m_aNameMap.find(evt.PropertyName);
103 OUString sPropName;
104 if ( aFind != m_aNameMap.end() )
105 sPropName = aFind->second.first;
106 else
108 aFind = ::std::find_if(
109 m_aNameMap.begin(),
110 m_aNameMap.end(),
111 [&evt] (const TPropertyNamePair::value_type& namePair) {
112 return namePair.second.first == evt.PropertyName;
114 if ( aFind != m_aNameMap.end() )
115 sPropName = aFind->first;
117 if (aFind != m_aNameMap.end() && !sPropName.isEmpty() && xPropInfo->hasPropertyByName(sPropName))
118 xProp->setPropertyValue(sPropName,aFind->second.second->operator()(sPropName,evt.NewValue));
119 else if ( evt.PropertyName == PROPERTY_CHARFONTNAME
120 || evt.PropertyName == PROPERTY_CHARFONTSTYLENAME
121 || evt.PropertyName == PROPERTY_CHARSTRIKEOUT
122 || evt.PropertyName == PROPERTY_CHARWORDMODE
123 || evt.PropertyName == PROPERTY_CHARROTATION
124 || evt.PropertyName == PROPERTY_CHARSCALEWIDTH
125 || evt.PropertyName == PROPERTY_CHARFONTFAMILY
126 || evt.PropertyName == PROPERTY_CHARFONTCHARSET
127 || evt.PropertyName == PROPERTY_CHARFONTPITCH
128 || evt.PropertyName == PROPERTY_CHARHEIGHT
129 || evt.PropertyName == PROPERTY_CHARUNDERLINE
130 || evt.PropertyName == PROPERTY_CHARWEIGHT
131 || evt.PropertyName == PROPERTY_CHARPOSTURE)
133 xProp->setPropertyValue(PROPERTY_FONTDESCRIPTOR,m_xSource->getPropertyValue(PROPERTY_FONTDESCRIPTOR));
138 catch(Exception&)
140 TOOLS_WARN_EXCEPTION( "reportdesign", "");
142 m_bInChange = false;
145 void SAL_CALL OPropertyMediator::disposing( const css::lang::EventObject& /*_rSource*/ )
147 ::osl::MutexGuard aGuard(m_aMutex);
148 disposing();
151 void SAL_CALL OPropertyMediator::disposing()
153 stopListening();
154 m_xSource.clear();
155 m_xSourceInfo.clear();
156 m_xDest.clear();
157 m_xDestInfo.clear();
160 void OPropertyMediator::stopListening()
162 if ( m_xSource.is() )
163 m_xSource->removePropertyChangeListener(OUString(), this);
164 if ( m_xDest.is() )
165 m_xDest->removePropertyChangeListener(OUString(), this);
168 void OPropertyMediator::startListening()
170 if ( m_xSource.is() )
171 m_xSource->addPropertyChangeListener(OUString(), this);
172 if ( m_xDest.is() )
173 m_xDest->addPropertyChangeListener(OUString(), this);
177 } // namespace dbaccess
180 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */