1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 #include "PropertyForward.hxx"
24 #include <com/sun/star/beans/PropertyValue.hpp>
25 #include <com/sun/star/beans/PropertyAttribute.hpp>
26 #include <comphelper/property.hxx>
27 #include <com/sun/star/sdbcx/XAppend.hpp>
28 #include <tools/debug.hxx>
29 #include <tools/diagnose_ex.h>
30 #include "corestrings.hrc"
31 
32 //........................................................................
33 namespace rptui
34 {
35 //........................................................................
36 	using namespace ::com::sun::star::uno;
37 	using namespace ::com::sun::star::beans;
38 	using namespace reportdesign;
39 
DBG_NAME(rpt_OPropertyMediator)40 DBG_NAME( rpt_OPropertyMediator )
41 OPropertyMediator::OPropertyMediator(const Reference< XPropertySet>& _xSource
42 									 ,const Reference< XPropertySet>& _xDest
43 									 ,const TPropertyNamePair& _aNameMap
44 									 ,sal_Bool _bReverse)
45 								: OPropertyForward_Base(m_aMutex)
46 								,m_aNameMap(_aNameMap)
47 								,m_xSource(_xSource)
48 								,m_xDest(_xDest)
49 								,m_bInChange(sal_False)
50 {
51 	DBG_CTOR( rpt_OPropertyMediator,NULL);
52 	osl_incrementInterlockedCount(&m_refCount);
53 	OSL_ENSURE(m_xDest.is(),"Dest is NULL!");
54 	OSL_ENSURE(m_xSource.is(),"Source is NULL!");
55 	if ( m_xDest.is() && m_xSource.is() )
56 	{
57 		try
58 		{
59 			m_xDestInfo = m_xDest->getPropertySetInfo();
60 			m_xSourceInfo = m_xSource->getPropertySetInfo();
61 			if ( _bReverse )
62 			{
63 				::comphelper::copyProperties(m_xDest,m_xSource);
64 				TPropertyNamePair::iterator aIter = m_aNameMap.begin();
65 				TPropertyNamePair::iterator aEnd = m_aNameMap.end();
66 				for (; aIter != aEnd; ++aIter)
67                 {
68                     Property aProp = m_xSourceInfo->getPropertyByName(aIter->first);
69                     if (0 == (aProp.Attributes & PropertyAttribute::READONLY))
70                     {
71                         Any aValue = _xDest->getPropertyValue(aIter->second.first);
72                         if ( 0 != (aProp.Attributes & PropertyAttribute::MAYBEVOID) || aValue.hasValue() )
73 					        _xSource->setPropertyValue(aIter->first,aIter->second.second->operator()(aIter->second.first,aValue));
74                     }
75                 }
76 			}
77 			else
78 			{
79 				::comphelper::copyProperties(m_xSource,m_xDest);
80 				TPropertyNamePair::iterator aIter = m_aNameMap.begin();
81 				TPropertyNamePair::iterator aEnd = m_aNameMap.end();
82 				for (; aIter != aEnd; ++aIter)
83 					_xDest->setPropertyValue(aIter->second.first,aIter->second.second->operator()(aIter->second.first,_xSource->getPropertyValue(aIter->first)));
84 			}
85             startListening();
86 		}
87 		catch(Exception& e)
88 		{
89             DBG_UNHANDLED_EXCEPTION();
90             (void)e;
91 		}
92 	} // if ( m_xDest.is() && m_xSource.is() )
93 	osl_decrementInterlockedCount(&m_refCount);
94 }
95 // -----------------------------------------------------------------------------
~OPropertyMediator()96 OPropertyMediator::~OPropertyMediator()
97 {
98 	DBG_DTOR( rpt_OPropertyMediator,NULL);
99 }
100 // -----------------------------------------------------------------------------
propertyChange(const PropertyChangeEvent & evt)101 void SAL_CALL OPropertyMediator::propertyChange( const PropertyChangeEvent& evt ) throw(RuntimeException)
102 {
103 	::osl::MutexGuard aGuard(m_aMutex);
104 	if ( !m_bInChange )
105 	{
106 		m_bInChange = sal_True;
107 		try
108 		{
109 			sal_Bool bDest = (evt.Source == m_xDest);
110 			Reference<XPropertySet> xProp =  bDest ? m_xSource : m_xDest;
111 			Reference<XPropertySetInfo> xPropInfo = bDest ? m_xSourceInfo : m_xDestInfo;
112 			if ( xProp.is() )
113 			{
114 				if ( xPropInfo.is() )
115 				{
116 					if ( xPropInfo->hasPropertyByName(evt.PropertyName) )
117 						xProp->setPropertyValue(evt.PropertyName,evt.NewValue);
118 					else
119 					{
120 						TPropertyNamePair::iterator aFind = m_aNameMap.find(evt.PropertyName);
121 						::rtl::OUString sPropName;
122 						if ( aFind != m_aNameMap.end() )
123 							sPropName = aFind->second.first;
124 						else
125 						{
126 							aFind = ::std::find_if(
127 								m_aNameMap.begin(),
128 								m_aNameMap.end(),
129 								::std::compose1(
130 								::std::bind2nd(::std::equal_to< ::rtl::OUString >(), evt.PropertyName),
131 									::std::compose1(::std::select1st<TPropertyConverter>(),::std::select2nd<TPropertyNamePair::value_type>())
132 								)
133 							);
134 							if ( aFind != m_aNameMap.end() )
135 								sPropName = aFind->first;
136 						}
137 						if ( sPropName.getLength() && xPropInfo->hasPropertyByName(sPropName) )
138 							xProp->setPropertyValue(sPropName,aFind->second.second->operator()(sPropName,evt.NewValue));
139                         else if (   evt.PropertyName == PROPERTY_CHARFONTNAME
140                                 ||  evt.PropertyName == PROPERTY_CHARFONTSTYLENAME
141                                 ||  evt.PropertyName == PROPERTY_CHARSTRIKEOUT
142                                 ||  evt.PropertyName == PROPERTY_CHARWORDMODE
143                                 ||  evt.PropertyName == PROPERTY_CHARROTATION
144                                 ||  evt.PropertyName == PROPERTY_CHARSCALEWIDTH
145                                 ||  evt.PropertyName == PROPERTY_CHARFONTFAMILY
146                                 ||  evt.PropertyName == PROPERTY_CHARFONTCHARSET
147                                 ||  evt.PropertyName == PROPERTY_CHARFONTPITCH
148                                 ||  evt.PropertyName == PROPERTY_CHARHEIGHT
149                                 ||  evt.PropertyName == PROPERTY_CHARUNDERLINE
150                                 ||  evt.PropertyName == PROPERTY_CHARWEIGHT
151                                 ||  evt.PropertyName == PROPERTY_CHARPOSTURE)
152                         {
153                             xProp->setPropertyValue(PROPERTY_FONTDESCRIPTOR,m_xSource->getPropertyValue(PROPERTY_FONTDESCRIPTOR));
154                         }
155 					}
156 				}
157 			}
158 		}
159 		catch(Exception&)
160 		{
161 			OSL_ENSURE(0,"Exception catched!");
162 		}
163 		m_bInChange = sal_False;
164 	}
165 }
166 // -----------------------------------------------------------------------------
disposing(const::com::sun::star::lang::EventObject &)167 void SAL_CALL OPropertyMediator::disposing( const ::com::sun::star::lang::EventObject& /*_rSource*/ ) throw (RuntimeException)
168 {
169 	::osl::MutexGuard aGuard(m_aMutex);
170     disposing();
171 }
172 // -----------------------------------------------------------------------------
disposing()173 void SAL_CALL OPropertyMediator::disposing()
174 {
175     stopListening();
176     m_xSource.clear();
177     m_xSourceInfo.clear();
178     m_xDest.clear();
179     m_xDestInfo.clear();
180 }
181 // -----------------------------------------------------------------------------
stopListening()182 void OPropertyMediator::stopListening()
183 {
184     if ( m_xSource.is() )
185 		m_xSource->removePropertyChangeListener(::rtl::OUString(), this);
186     if ( m_xDest.is() )
187         m_xDest->removePropertyChangeListener(::rtl::OUString(), this);
188 }
189 // -----------------------------------------------------------------------------
startListening()190 void OPropertyMediator::startListening()
191 {
192     if ( m_xSource.is() )
193 		m_xSource->addPropertyChangeListener(::rtl::OUString(), this);
194     if ( m_xDest.is() )
195         m_xDest->addPropertyChangeListener(::rtl::OUString(), this);
196 }
197 // -----------------------------------------------------------------------------
198 //........................................................................
199 }	// namespace dbaccess
200 //........................................................................
201 
202