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 24 // MARKER(update_precomp.py): autogen include statement, do not remove 25 #include "precompiled_chartcontroller.hxx" 26 #include "ItemConverter.hxx" 27 #include "macros.hxx" 28 #include <com/sun/star/lang/XComponent.hpp> 29 #include <svl/itemprop.hxx> 30 #include <svl/itemiter.hxx> 31 // header for class SfxWhichIter 32 #include <svl/whiter.hxx> 33 #include <svx/svxids.hrc> 34 35 using namespace ::com::sun::star; 36 37 namespace comphelper 38 { 39 40 ItemConverter::ItemConverter( 41 const uno::Reference< beans::XPropertySet > & rPropertySet, 42 SfxItemPool& rItemPool ) : 43 m_xPropertySet( rPropertySet ), 44 m_xPropertySetInfo( NULL ), 45 m_rItemPool( rItemPool ), 46 m_bIsValid( true ) 47 { 48 resetPropertySet( m_xPropertySet ); 49 } 50 51 ItemConverter::~ItemConverter() 52 { 53 stopAllComponentListening(); 54 } 55 56 void ItemConverter::resetPropertySet( 57 const uno::Reference< beans::XPropertySet > & xPropSet ) 58 { 59 if( xPropSet.is()) 60 { 61 stopAllComponentListening(); 62 m_xPropertySet = xPropSet; 63 m_xPropertySetInfo = m_xPropertySet->getPropertySetInfo(); 64 65 uno::Reference< lang::XComponent > xComp( m_xPropertySet, uno::UNO_QUERY ); 66 if( xComp.is()) 67 { 68 // method of base class ::utl::OEventListenerAdapter 69 startComponentListening( xComp ); 70 } 71 } 72 } 73 74 SfxItemPool & ItemConverter::GetItemPool() const 75 { 76 return m_rItemPool; 77 } 78 79 SfxItemSet ItemConverter::CreateEmptyItemSet() const 80 { 81 return SfxItemSet( GetItemPool(), GetWhichPairs() ); 82 } 83 84 uno::Reference< beans::XPropertySet > ItemConverter::GetPropertySet() const 85 { 86 return m_xPropertySet; 87 } 88 89 void ItemConverter::_disposing( const lang::EventObject& rSource ) 90 { 91 if( rSource.Source == m_xPropertySet ) 92 { 93 m_bIsValid = false; 94 } 95 } 96 97 void ItemConverter::FillItemSet( SfxItemSet & rOutItemSet ) const 98 { 99 const sal_uInt16 * pRanges = rOutItemSet.GetRanges(); 100 tPropertyNameWithMemberId aProperty; 101 SfxItemPool & rPool = GetItemPool(); 102 103 OSL_ASSERT( pRanges != NULL ); 104 OSL_ASSERT( m_xPropertySetInfo.is()); 105 OSL_ASSERT( m_xPropertySet.is()); 106 107 while( (*pRanges) != 0) 108 { 109 sal_uInt16 nBeg = (*pRanges); 110 ++pRanges; 111 sal_uInt16 nEnd = (*pRanges); 112 ++pRanges; 113 114 OSL_ASSERT( nBeg <= nEnd ); 115 for( sal_uInt16 nWhich = nBeg; nWhich <= nEnd; ++nWhich ) 116 { 117 if( GetItemProperty( nWhich, aProperty )) 118 { 119 // put the Property into the itemset 120 SfxPoolItem * pItem = rPool.GetDefaultItem( nWhich ).Clone(); 121 122 if( pItem ) 123 { 124 try 125 { 126 if( ! pItem->PutValue( m_xPropertySet->getPropertyValue( aProperty.first ), 127 aProperty.second // nMemberId 128 )) 129 { 130 delete pItem; 131 } 132 else 133 { 134 rOutItemSet.Put( *pItem, nWhich ); 135 delete pItem; 136 } 137 } 138 catch( beans::UnknownPropertyException ex ) 139 { 140 delete pItem; 141 OSL_ENSURE( false, 142 ::rtl::OUStringToOString( 143 ex.Message + 144 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( 145 " - unknown Property: " )) + aProperty.first, 146 RTL_TEXTENCODING_ASCII_US ).getStr()); 147 } 148 catch( uno::Exception ex ) 149 { 150 ASSERT_EXCEPTION( ex ); 151 } 152 } 153 } 154 else 155 { 156 try 157 { 158 FillSpecialItem( nWhich, rOutItemSet ); 159 } 160 catch( uno::Exception ex ) 161 { 162 ASSERT_EXCEPTION( ex ); 163 } 164 } 165 } 166 } 167 } 168 169 void ItemConverter::FillSpecialItem( 170 sal_uInt16 /*nWhichId*/, SfxItemSet & /*rOutItemSet*/ ) const 171 { 172 OSL_ENSURE( false, "ItemConverter: Unhandled special item found!" ); 173 } 174 175 bool ItemConverter::ApplySpecialItem( 176 sal_uInt16 /*nWhichId*/, const SfxItemSet & /*rItemSet*/ ) 177 { 178 OSL_ENSURE( false, "ItemConverter: Unhandled special item found!" ); 179 return false; 180 } 181 182 bool ItemConverter::ApplyItemSet( const SfxItemSet & rItemSet ) 183 { 184 OSL_ASSERT( m_xPropertySet.is()); 185 186 bool bItemsChanged = false; 187 SfxItemIter aIter( rItemSet ); 188 const SfxPoolItem * pItem = aIter.FirstItem(); 189 tPropertyNameWithMemberId aProperty; 190 uno::Any aValue; 191 192 while( pItem ) 193 { 194 if( rItemSet.GetItemState( pItem->Which(), sal_False ) == SFX_ITEM_SET ) 195 { 196 if( GetItemProperty( pItem->Which(), aProperty )) 197 { 198 pItem->QueryValue( aValue, aProperty.second /* nMemberId */ ); 199 200 try 201 { 202 if( aValue != m_xPropertySet->getPropertyValue( aProperty.first )) 203 { 204 m_xPropertySet->setPropertyValue( aProperty.first, aValue ); 205 bItemsChanged = true; 206 } 207 } 208 catch( beans::UnknownPropertyException ex ) 209 { 210 OSL_ENSURE( false, 211 ::rtl::OUStringToOString( 212 ex.Message + 213 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( 214 " - unknown Property: " )) + aProperty.first, 215 RTL_TEXTENCODING_ASCII_US).getStr()); 216 } 217 catch( uno::Exception ex ) 218 { 219 OSL_ENSURE( false, ::rtl::OUStringToOString( 220 ex.Message, RTL_TEXTENCODING_ASCII_US ).getStr()); 221 } 222 } 223 else 224 { 225 bItemsChanged = ApplySpecialItem( pItem->Which(), rItemSet ) || bItemsChanged; 226 } 227 } 228 pItem = aIter.NextItem(); 229 } 230 231 return bItemsChanged; 232 } 233 234 // -------------------------------------------------------------------------------- 235 236 void ItemConverter::InvalidateUnequalItems( SfxItemSet &rDestSet, const SfxItemSet &rSourceSet ) 237 { 238 SfxWhichIter aIter (rSourceSet); 239 sal_uInt16 nWhich = aIter.FirstWhich (); 240 const SfxPoolItem *pPoolItem = NULL; 241 242 while (nWhich) 243 { 244 if ((rSourceSet.GetItemState(nWhich, sal_True, &pPoolItem) == SFX_ITEM_SET) && 245 (rDestSet.GetItemState(nWhich, sal_True, &pPoolItem) == SFX_ITEM_SET)) 246 { 247 if (rSourceSet.Get(nWhich) != rDestSet.Get(nWhich)) 248 { 249 if( SID_CHAR_DLG_PREVIEW_STRING != nWhich ) 250 { 251 rDestSet.InvalidateItem(nWhich); 252 } 253 } 254 } 255 else if( rSourceSet.GetItemState(nWhich, sal_True, &pPoolItem) == SFX_ITEM_DONTCARE ) 256 rDestSet.InvalidateItem(nWhich); 257 258 nWhich = aIter.NextWhich (); 259 } 260 } 261 262 } // namespace comphelper 263