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_chart2.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 
ItemConverter(const uno::Reference<beans::XPropertySet> & rPropertySet,SfxItemPool & rItemPool)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 
~ItemConverter()51 ItemConverter::~ItemConverter()
52 {
53     stopAllComponentListening();
54 }
55 
resetPropertySet(const uno::Reference<beans::XPropertySet> & xPropSet)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 
GetItemPool() const74 SfxItemPool & ItemConverter::GetItemPool() const
75 {
76     return m_rItemPool;
77 }
78 
CreateEmptyItemSet() const79 SfxItemSet ItemConverter::CreateEmptyItemSet() const
80 {
81     return SfxItemSet( GetItemPool(), GetWhichPairs() );
82 }
83 
GetPropertySet() const84 uno::Reference< beans::XPropertySet > ItemConverter::GetPropertySet() const
85 {
86     return m_xPropertySet;
87 }
88 
_disposing(const lang::EventObject & rSource)89 void ItemConverter::_disposing( const lang::EventObject& rSource )
90 {
91     if( rSource.Source == m_xPropertySet )
92     {
93         m_bIsValid = false;
94     }
95 }
96 
FillItemSet(SfxItemSet & rOutItemSet) const97 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 
FillSpecialItem(sal_uInt16,SfxItemSet &) const169 void ItemConverter::FillSpecialItem(
170     sal_uInt16 /*nWhichId*/, SfxItemSet & /*rOutItemSet*/ ) const
171     throw( uno::Exception )
172 {
173     OSL_ENSURE( false, "ItemConverter: Unhandled special item found!" );
174 }
175 
ApplySpecialItem(sal_uInt16,const SfxItemSet &)176 bool ItemConverter::ApplySpecialItem(
177     sal_uInt16 /*nWhichId*/, const SfxItemSet & /*rItemSet*/ )
178     throw( uno::Exception )
179 {
180     OSL_ENSURE( false, "ItemConverter: Unhandled special item found!" );
181     return false;
182 }
183 
ApplyItemSet(const SfxItemSet & rItemSet)184 bool ItemConverter::ApplyItemSet( const SfxItemSet & rItemSet )
185 {
186     OSL_ASSERT( m_xPropertySet.is());
187 
188     bool bItemsChanged = false;
189     SfxItemIter aIter( rItemSet );
190     const SfxPoolItem * pItem = aIter.FirstItem();
191     tPropertyNameWithMemberId aProperty;
192     uno::Any aValue;
193 
194     while( pItem )
195     {
196         if( rItemSet.GetItemState( pItem->Which(), sal_False ) == SFX_ITEM_SET )
197         {
198             if( GetItemProperty( pItem->Which(), aProperty ))
199             {
200                 pItem->QueryValue( aValue, aProperty.second /* nMemberId */ );
201 
202                 try
203                 {
204                     if( aValue != m_xPropertySet->getPropertyValue( aProperty.first ))
205                     {
206                         m_xPropertySet->setPropertyValue( aProperty.first, aValue );
207                         bItemsChanged = true;
208                     }
209                 }
210                 catch( beans::UnknownPropertyException ex )
211                 {
212                     OSL_ENSURE( false,
213                                 ::rtl::OUStringToOString(
214                                     ex.Message +
215                                     ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
216                                                          " - unknown Property: " )) + aProperty.first,
217                                     RTL_TEXTENCODING_ASCII_US).getStr());
218                 }
219                 catch( uno::Exception ex )
220                 {
221                     OSL_ENSURE( false, ::rtl::OUStringToOString(
222                                     ex.Message, RTL_TEXTENCODING_ASCII_US ).getStr());
223                 }
224             }
225             else
226             {
227                 bItemsChanged = ApplySpecialItem( pItem->Which(), rItemSet ) || bItemsChanged;
228             }
229         }
230         pItem = aIter.NextItem();
231     }
232 
233     return bItemsChanged;
234 }
235 
236 // --------------------------------------------------------------------------------
237 
InvalidateUnequalItems(SfxItemSet & rDestSet,const SfxItemSet & rSourceSet)238 void ItemConverter::InvalidateUnequalItems( SfxItemSet  &rDestSet, const SfxItemSet &rSourceSet )
239 {
240 	SfxWhichIter      aIter (rSourceSet);
241 	sal_uInt16            nWhich     = aIter.FirstWhich ();
242 	const SfxPoolItem *pPoolItem = NULL;
243 
244 	while (nWhich)
245 	{
246 		if ((rSourceSet.GetItemState(nWhich, sal_True, &pPoolItem) == SFX_ITEM_SET) &&
247 			(rDestSet.GetItemState(nWhich, sal_True, &pPoolItem) == SFX_ITEM_SET))
248         {
249 			if (rSourceSet.Get(nWhich) != rDestSet.Get(nWhich))
250             {
251                 if( SID_CHAR_DLG_PREVIEW_STRING != nWhich )
252                 {
253                     rDestSet.InvalidateItem(nWhich);
254                 }
255             }
256         }
257         else if( rSourceSet.GetItemState(nWhich, sal_True, &pPoolItem) == SFX_ITEM_DONTCARE )
258             rDestSet.InvalidateItem(nWhich);
259 
260 		nWhich = aIter.NextWhich ();
261 	}
262 }
263 
264 } //  namespace comphelper
265