1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_chart2.hxx"
30 #include "ItemConverter.hxx"
31 #include "macros.hxx"
32 #include <com/sun/star/lang/XComponent.hpp>
33 #include <svl/itemprop.hxx>
34 #include <svl/itemiter.hxx>
35 // header for class SfxWhichIter
36 #include <svl/whiter.hxx>
37 #include <svx/svxids.hrc>
38 
39 using namespace ::com::sun::star;
40 
41 namespace comphelper
42 {
43 
44 ItemConverter::ItemConverter(
45     const uno::Reference< beans::XPropertySet > & rPropertySet,
46     SfxItemPool& rItemPool ) :
47         m_xPropertySet( rPropertySet ),
48         m_xPropertySetInfo( NULL ),
49         m_rItemPool( rItemPool ),
50         m_bIsValid( true )
51 {
52     resetPropertySet( m_xPropertySet );
53 }
54 
55 ItemConverter::~ItemConverter()
56 {
57     stopAllComponentListening();
58 }
59 
60 void ItemConverter::resetPropertySet(
61     const uno::Reference< beans::XPropertySet > & xPropSet )
62 {
63     if( xPropSet.is())
64     {
65         stopAllComponentListening();
66         m_xPropertySet = xPropSet;
67         m_xPropertySetInfo = m_xPropertySet->getPropertySetInfo();
68 
69         uno::Reference< lang::XComponent > xComp( m_xPropertySet, uno::UNO_QUERY );
70         if( xComp.is())
71         {
72             // method of base class ::utl::OEventListenerAdapter
73             startComponentListening( xComp );
74         }
75     }
76 }
77 
78 SfxItemPool & ItemConverter::GetItemPool() const
79 {
80     return m_rItemPool;
81 }
82 
83 SfxItemSet ItemConverter::CreateEmptyItemSet() const
84 {
85     return SfxItemSet( GetItemPool(), GetWhichPairs() );
86 }
87 
88 uno::Reference< beans::XPropertySet > ItemConverter::GetPropertySet() const
89 {
90     return m_xPropertySet;
91 }
92 
93 void ItemConverter::_disposing( const lang::EventObject& rSource )
94 {
95     if( rSource.Source == m_xPropertySet )
96     {
97         m_bIsValid = false;
98     }
99 }
100 
101 void ItemConverter::FillItemSet( SfxItemSet & rOutItemSet ) const
102 {
103     const sal_uInt16 * pRanges = rOutItemSet.GetRanges();
104     tPropertyNameWithMemberId aProperty;
105     SfxItemPool & rPool = GetItemPool();
106 
107     OSL_ASSERT( pRanges != NULL );
108     OSL_ASSERT( m_xPropertySetInfo.is());
109     OSL_ASSERT( m_xPropertySet.is());
110 
111     while( (*pRanges) != 0)
112     {
113         sal_uInt16 nBeg = (*pRanges);
114         ++pRanges;
115         sal_uInt16 nEnd = (*pRanges);
116         ++pRanges;
117 
118         OSL_ASSERT( nBeg <= nEnd );
119         for( sal_uInt16 nWhich = nBeg; nWhich <= nEnd; ++nWhich )
120         {
121             if( GetItemProperty( nWhich, aProperty ))
122             {
123                 // put the Property into the itemset
124                 SfxPoolItem * pItem = rPool.GetDefaultItem( nWhich ).Clone();
125 
126                 if( pItem )
127                 {
128                     try
129                     {
130                         if( ! pItem->PutValue( m_xPropertySet->getPropertyValue( aProperty.first ),
131                                                aProperty.second // nMemberId
132                                 ))
133                         {
134                             delete pItem;
135                         }
136                         else
137                         {
138                             rOutItemSet.Put( *pItem, nWhich );
139                             delete pItem;
140                         }
141                     }
142                     catch( beans::UnknownPropertyException ex )
143                     {
144                         delete pItem;
145                         OSL_ENSURE( false,
146                                     ::rtl::OUStringToOString(
147                                         ex.Message +
148                                         ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
149                                                              " - unknown Property: " )) + aProperty.first,
150                                         RTL_TEXTENCODING_ASCII_US ).getStr());
151                     }
152                     catch( uno::Exception ex )
153                     {
154                         ASSERT_EXCEPTION( ex );
155                     }
156                 }
157             }
158             else
159             {
160                 try
161                 {
162                     FillSpecialItem( nWhich, rOutItemSet );
163                 }
164                 catch( uno::Exception ex )
165                 {
166                     ASSERT_EXCEPTION( ex );
167                 }
168             }
169         }
170     }
171 }
172 
173 void ItemConverter::FillSpecialItem(
174     sal_uInt16 /*nWhichId*/, SfxItemSet & /*rOutItemSet*/ ) const
175     throw( uno::Exception )
176 {
177     OSL_ENSURE( false, "ItemConverter: Unhandled special item found!" );
178 }
179 
180 bool ItemConverter::ApplySpecialItem(
181     sal_uInt16 /*nWhichId*/, const SfxItemSet & /*rItemSet*/ )
182     throw( uno::Exception )
183 {
184     OSL_ENSURE( false, "ItemConverter: Unhandled special item found!" );
185     return false;
186 }
187 
188 bool ItemConverter::ApplyItemSet( const SfxItemSet & rItemSet )
189 {
190     OSL_ASSERT( m_xPropertySet.is());
191 
192     bool bItemsChanged = false;
193     SfxItemIter aIter( rItemSet );
194     const SfxPoolItem * pItem = aIter.FirstItem();
195     tPropertyNameWithMemberId aProperty;
196     uno::Any aValue;
197 
198     while( pItem )
199     {
200         if( rItemSet.GetItemState( pItem->Which(), sal_False ) == SFX_ITEM_SET )
201         {
202             if( GetItemProperty( pItem->Which(), aProperty ))
203             {
204                 pItem->QueryValue( aValue, aProperty.second /* nMemberId */ );
205 
206                 try
207                 {
208                     if( aValue != m_xPropertySet->getPropertyValue( aProperty.first ))
209                     {
210                         m_xPropertySet->setPropertyValue( aProperty.first, aValue );
211                         bItemsChanged = true;
212                     }
213                 }
214                 catch( beans::UnknownPropertyException ex )
215                 {
216                     OSL_ENSURE( false,
217                                 ::rtl::OUStringToOString(
218                                     ex.Message +
219                                     ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
220                                                          " - unknown Property: " )) + aProperty.first,
221                                     RTL_TEXTENCODING_ASCII_US).getStr());
222                 }
223                 catch( uno::Exception ex )
224                 {
225                     OSL_ENSURE( false, ::rtl::OUStringToOString(
226                                     ex.Message, RTL_TEXTENCODING_ASCII_US ).getStr());
227                 }
228             }
229             else
230             {
231                 bItemsChanged = ApplySpecialItem( pItem->Which(), rItemSet ) || bItemsChanged;
232             }
233         }
234         pItem = aIter.NextItem();
235     }
236 
237     return bItemsChanged;
238 }
239 
240 // --------------------------------------------------------------------------------
241 
242 void ItemConverter::InvalidateUnequalItems( SfxItemSet  &rDestSet, const SfxItemSet &rSourceSet )
243 {
244 	SfxWhichIter      aIter (rSourceSet);
245 	sal_uInt16            nWhich     = aIter.FirstWhich ();
246 	const SfxPoolItem *pPoolItem = NULL;
247 
248 	while (nWhich)
249 	{
250 		if ((rSourceSet.GetItemState(nWhich, sal_True, &pPoolItem) == SFX_ITEM_SET) &&
251 			(rDestSet.GetItemState(nWhich, sal_True, &pPoolItem) == SFX_ITEM_SET))
252         {
253 			if (rSourceSet.Get(nWhich) != rDestSet.Get(nWhich))
254             {
255                 if( SID_CHAR_DLG_PREVIEW_STRING != nWhich )
256                 {
257                     rDestSet.InvalidateItem(nWhich);
258                 }
259             }
260         }
261         else if( rSourceSet.GetItemState(nWhich, sal_True, &pPoolItem) == SFX_ITEM_DONTCARE )
262             rDestSet.InvalidateItem(nWhich);
263 
264 		nWhich = aIter.NextWhich ();
265 	}
266 }
267 
268 } //  namespace comphelper
269