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