Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #include <svtools/FilterConfigItem.hxx>
30 : :
31 : : #include <unotools/configmgr.hxx>
32 : : #include <comphelper/processfactory.hxx>
33 : : #include <comphelper/string.hxx>
34 : : #include <com/sun/star/beans/PropertyValue.hpp>
35 : : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
36 : : #include <com/sun/star/util/XChangesBatch.hpp>
37 : : #include <com/sun/star/beans/XPropertySetInfo.hpp>
38 : : #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
39 : :
40 : : using ::rtl::OUString;
41 : : using namespace ::utl ; // getProcessServiceFactory
42 : : using namespace ::com::sun::star::lang ; // XMultiServiceFactory
43 : : using namespace ::com::sun::star::beans ; // PropertyValue
44 : : using namespace ::com::sun::star::uno ; // Reference
45 : : using namespace ::com::sun::star::util ; // XChangesBatch
46 : : using namespace ::com::sun::star::awt ; // Size
47 : : using namespace ::com::sun::star::container ; //
48 : : using namespace ::com::sun::star::task ; // XStatusIndicator
49 : :
50 : 0 : static sal_Bool ImpIsTreeAvailable( Reference< XMultiServiceFactory >& rXCfgProv, const rtl::OUString& rTree )
51 : : {
52 : 0 : sal_Bool bAvailable = !rTree.isEmpty();
53 [ # # ]: 0 : if ( bAvailable )
54 : : {
55 : : using comphelper::string::getTokenCount;
56 : : using comphelper::string::getToken;
57 : :
58 [ # # ]: 0 : sal_Int32 nTokenCount = getTokenCount(rTree, '/');
59 : 0 : sal_Int32 i = 0;
60 : :
61 [ # # ]: 0 : if ( rTree[0] == '/' )
62 : 0 : ++i;
63 [ # # ]: 0 : if ( rTree[rTree.getLength() - 1] == '/' )
64 : 0 : --nTokenCount;
65 : :
66 : 0 : Any aAny;
67 [ # # ]: 0 : aAny <<= getToken(rTree, i++, '/');
68 : :
69 : : // creation arguments: nodepath
70 : 0 : PropertyValue aPathArgument;
71 [ # # ]: 0 : aPathArgument.Name = OUString( RTL_CONSTASCII_USTRINGPARAM( "nodepath" ) );
72 : 0 : aPathArgument.Value = aAny;
73 : :
74 [ # # ]: 0 : Sequence< Any > aArguments( 1 );
75 [ # # ][ # # ]: 0 : aArguments[ 0 ] <<= aPathArgument;
76 : :
77 : 0 : Reference< XInterface > xReadAccess;
78 : : try
79 : : {
80 [ # # ]: 0 : xReadAccess = rXCfgProv->createInstanceWithArguments(
81 : : OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.configuration.ConfigurationAccess" ) ),
82 [ # # ][ # # ]: 0 : aArguments );
[ # # ][ # # ]
83 : : }
84 [ # # # # ]: 0 : catch (const ::com::sun::star::uno::Exception&)
85 : : {
86 : 0 : bAvailable = sal_False;
87 : : }
88 [ # # ]: 0 : if ( xReadAccess.is() )
89 : : {
90 [ # # ][ # # ]: 0 : for ( ; bAvailable && ( i < nTokenCount ); i++ )
[ # # ]
91 : : {
92 : : Reference< XHierarchicalNameAccess > xHierarchicalNameAccess
93 [ # # ]: 0 : ( xReadAccess, UNO_QUERY );
94 : :
95 [ # # ]: 0 : if ( !xHierarchicalNameAccess.is() )
96 : 0 : bAvailable = sal_False;
97 : : else
98 : : {
99 : 0 : rtl::OUString aNode( getToken(rTree, i, '/') );
100 [ # # ][ # # ]: 0 : if ( !xHierarchicalNameAccess->hasByHierarchicalName( aNode ) )
[ # # ]
101 : 0 : bAvailable = sal_False;
102 : : else
103 : : {
104 [ # # ][ # # ]: 0 : Any a( xHierarchicalNameAccess->getByHierarchicalName( aNode ) );
105 : : try
106 : : {
107 [ # # ]: 0 : a >>= xReadAccess;
108 : : }
109 [ # # ]: 0 : catch ( ::com::sun::star::uno::Exception& )
110 : : {
111 : 0 : bAvailable = sal_False;
112 : 0 : }
113 : 0 : }
114 : : }
115 : 0 : }
116 [ # # ]: 0 : }
117 : : }
118 : 0 : return bAvailable;
119 : : }
120 : :
121 : 0 : void FilterConfigItem::ImpInitTree( const String& rSubTree )
122 : : {
123 : 0 : bModified = sal_False;
124 : :
125 : : Reference< XMultiServiceFactory > xSMGR(
126 [ # # ]: 0 : comphelper::getProcessServiceFactory() );
127 : :
128 : : Reference< XMultiServiceFactory > xCfgProv(
129 [ # # ]: 0 : xSMGR->createInstance( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.configuration.ConfigurationProvider" ) ) ),
130 [ # # ][ # # ]: 0 : UNO_QUERY );
[ # # ]
131 : :
132 [ # # ]: 0 : if ( xCfgProv.is() )
133 : : {
134 : : OUString sTree(
135 : : OUString(RTL_CONSTASCII_USTRINGPARAM("/org.openoffice.")) +
136 [ # # ][ # # ]: 0 : rSubTree);
137 [ # # ][ # # ]: 0 : if ( ImpIsTreeAvailable(xCfgProv, sTree) )
138 : : {
139 : 0 : Any aAny;
140 : : // creation arguments: nodepath
141 : 0 : PropertyValue aPathArgument;
142 [ # # ]: 0 : aAny <<= sTree;
143 [ # # ]: 0 : aPathArgument.Name = OUString( RTL_CONSTASCII_USTRINGPARAM( "nodepath" ) );
144 : 0 : aPathArgument.Value = aAny;
145 : :
146 : : // creation arguments: commit mode
147 : 0 : PropertyValue aModeArgument;
148 : 0 : sal_Bool bAsyncron = sal_True;
149 [ # # ]: 0 : aAny <<= bAsyncron;
150 [ # # ]: 0 : aModeArgument.Name = OUString( RTL_CONSTASCII_USTRINGPARAM( "lazywrite" ) );
151 : 0 : aModeArgument.Value = aAny;
152 : :
153 [ # # ]: 0 : Sequence< Any > aArguments( 2 );
154 [ # # ][ # # ]: 0 : aArguments[ 0 ] <<= aPathArgument;
155 [ # # ][ # # ]: 0 : aArguments[ 1 ] <<= aModeArgument;
156 : :
157 : : try
158 : : {
159 [ # # ]: 0 : xUpdatableView = xCfgProv->createInstanceWithArguments(
160 : : OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.configuration.ConfigurationUpdateAccess" ) ),
161 [ # # ][ # # ]: 0 : aArguments );
[ # # ]
162 [ # # ]: 0 : if ( xUpdatableView.is() )
163 [ # # ][ # # ]: 0 : xPropSet = Reference< XPropertySet >( xUpdatableView, UNO_QUERY );
[ # # ]
164 : : }
165 [ # # ]: 0 : catch ( ::com::sun::star::uno::Exception& )
166 : : {
167 : : OSL_FAIL( "FilterConfigItem::FilterConfigItem - Could not access configuration Key" );
168 [ # # ]: 0 : }
169 : 0 : }
170 : 0 : }
171 : 0 : }
172 : :
173 [ # # ]: 0 : FilterConfigItem::FilterConfigItem( const OUString& rSubTree )
174 : : {
175 [ # # ][ # # ]: 0 : ImpInitTree( rSubTree );
[ # # ]
176 : 0 : }
177 : :
178 [ + - ]: 298 : FilterConfigItem::FilterConfigItem( ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >* pFilterData )
179 : : {
180 [ + + ]: 298 : if ( pFilterData )
181 [ + - ]: 90 : aFilterData = *pFilterData;
182 : 298 : }
183 : :
184 : 0 : FilterConfigItem::FilterConfigItem( const OUString& rSubTree,
185 [ # # ]: 0 : ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >* pFilterData )
186 : : {
187 [ # # ][ # # ]: 0 : ImpInitTree( rSubTree );
[ # # ]
188 : :
189 [ # # ]: 0 : if ( pFilterData )
190 [ # # ]: 0 : aFilterData = *pFilterData;
191 : 0 : };
192 : :
193 [ + - ]: 298 : FilterConfigItem::~FilterConfigItem()
194 : : {
195 [ - + ]: 298 : if ( xUpdatableView.is() )
196 : : {
197 [ # # ][ # # ]: 0 : if ( xPropSet.is() && bModified )
[ # # ]
198 : : {
199 [ # # ]: 0 : Reference< XChangesBatch > xUpdateControl( xUpdatableView, UNO_QUERY );
200 [ # # ]: 0 : if ( xUpdateControl.is() )
201 : : {
202 : : try
203 : : {
204 [ # # ][ # # ]: 0 : xUpdateControl->commitChanges();
205 : : }
206 [ # # ]: 0 : catch ( ::com::sun::star::uno::Exception& )
207 : : {
208 : : OSL_FAIL( "FilterConfigItem::FilterConfigItem - Could not update configuration data" );
209 : : }
210 : 0 : }
211 : : }
212 : : }
213 [ # # ]: 298 : }
214 : :
215 : 925 : sal_Bool FilterConfigItem::ImplGetPropertyValue( Any& rAny, const Reference< XPropertySet >& rXPropSet, const OUString& rString, sal_Bool bTestPropertyAvailability )
216 : : {
217 : 925 : sal_Bool bRetValue = sal_True;
218 : :
219 [ - + ]: 925 : if ( rXPropSet.is() )
220 : : {
221 [ # # ]: 0 : if ( bTestPropertyAvailability )
222 : : {
223 : 0 : bRetValue = sal_False;
224 : : try
225 : : {
226 : : Reference< XPropertySetInfo >
227 [ # # ][ # # ]: 0 : aXPropSetInfo( rXPropSet->getPropertySetInfo() );
228 [ # # ]: 0 : if ( aXPropSetInfo.is() )
229 [ # # ][ # # ]: 0 : bRetValue = aXPropSetInfo->hasPropertyByName( rString );
[ # # ]
230 : : }
231 [ # # ]: 0 : catch( ::com::sun::star::uno::Exception& )
232 : : {
233 : : //
234 : : }
235 : : }
236 [ # # ]: 0 : if ( bRetValue )
237 : : {
238 : : try
239 : : {
240 [ # # ][ # # ]: 0 : rAny = rXPropSet->getPropertyValue( rString );
241 [ # # ]: 0 : if ( !rAny.hasValue() )
242 : 0 : bRetValue = sal_False;
243 : : }
244 : 0 : catch( ::com::sun::star::uno::Exception& )
245 : : {
246 : 0 : bRetValue = sal_False;
247 : : }
248 : : }
249 : : }
250 : : else
251 : 925 : bRetValue = sal_False;
252 : 925 : return bRetValue;
253 : : }
254 : :
255 : :
256 : : // if property is available it returns a pointer,
257 : : // otherwise the result is null
258 : 1015 : PropertyValue* FilterConfigItem::GetPropertyValue( Sequence< PropertyValue >& rPropSeq, const OUString& rName )
259 : : {
260 : 1015 : PropertyValue* pPropValue = NULL;
261 : :
262 : : sal_Int32 i, nCount;
263 [ + + ]: 4342 : for ( i = 0, nCount = rPropSeq.getLength(); i < nCount; i++ )
264 : : {
265 [ + + ]: 3417 : if ( rPropSeq[ i ].Name == rName )
266 : : {
267 : 90 : pPropValue = &rPropSeq[ i ];
268 : 90 : break;
269 : : }
270 : : }
271 : 1015 : return pPropValue;
272 : : }
273 : :
274 : : /* if PropertySequence already includes a PropertyValue using the same name, the
275 : : corresponding PropertyValue is replaced, otherwise the given PropertyValue
276 : : will be appended */
277 : :
278 : 1015 : sal_Bool FilterConfigItem::WritePropertyValue( Sequence< PropertyValue >& rPropSeq, const PropertyValue& rPropValue )
279 : : {
280 : 1015 : sal_Bool bRet = sal_False;
281 [ + - ]: 1015 : if ( !rPropValue.Name.isEmpty() )
282 : : {
283 : : sal_Int32 i, nCount;
284 [ + + ]: 4342 : for ( i = 0, nCount = rPropSeq.getLength(); i < nCount; i++ )
285 : : {
286 [ + + ]: 3417 : if ( rPropSeq[ i ].Name == rPropValue.Name )
287 : 90 : break;
288 : : }
289 [ + + ]: 1015 : if ( i == nCount )
290 : 925 : rPropSeq.realloc( ++nCount );
291 : :
292 : 1015 : rPropSeq[ i ] = rPropValue;
293 : :
294 : 1015 : bRet = sal_True;
295 : : }
296 : 1015 : return bRet;
297 : : }
298 : :
299 : 0 : sal_Bool FilterConfigItem::ReadBool( const OUString& rKey, sal_Bool bDefault )
300 : : {
301 : 0 : Any aAny;
302 : 0 : sal_Bool bRetValue = bDefault;
303 [ # # ]: 0 : PropertyValue* pPropVal = GetPropertyValue( aFilterData, rKey );
304 [ # # ]: 0 : if ( pPropVal )
305 : : {
306 : 0 : pPropVal->Value >>= bRetValue;
307 : : }
308 [ # # ][ # # ]: 0 : else if ( ImplGetPropertyValue( aAny, xPropSet, rKey, sal_True ) )
309 : : {
310 : 0 : aAny >>= bRetValue;
311 : : }
312 : 0 : PropertyValue aBool;
313 : 0 : aBool.Name = rKey;
314 [ # # ]: 0 : aBool.Value <<= bRetValue;
315 [ # # ]: 0 : WritePropertyValue( aFilterData, aBool );
316 : 0 : return bRetValue;
317 : : }
318 : :
319 : 1015 : sal_Int32 FilterConfigItem::ReadInt32( const OUString& rKey, sal_Int32 nDefault )
320 : : {
321 : 1015 : Any aAny;
322 : 1015 : sal_Int32 nRetValue = nDefault;
323 [ + - ]: 1015 : PropertyValue* pPropVal = GetPropertyValue( aFilterData, rKey );
324 [ + + ]: 1015 : if ( pPropVal )
325 : : {
326 : 90 : pPropVal->Value >>= nRetValue;
327 : : }
328 [ + - ][ - + ]: 925 : else if ( ImplGetPropertyValue( aAny, xPropSet, rKey, sal_True ) )
329 : : {
330 : 0 : aAny >>= nRetValue;
331 : : }
332 : 1015 : PropertyValue aInt32;
333 : 1015 : aInt32.Name = rKey;
334 [ + - ]: 1015 : aInt32.Value <<= nRetValue;
335 [ + - ]: 1015 : WritePropertyValue( aFilterData, aInt32 );
336 : 1015 : return nRetValue;
337 : : }
338 : :
339 : 0 : OUString FilterConfigItem::ReadString( const OUString& rKey, const OUString& rDefault )
340 : : {
341 : 0 : Any aAny;
342 : 0 : OUString aRetValue( rDefault );
343 [ # # ]: 0 : PropertyValue* pPropVal = GetPropertyValue( aFilterData, rKey );
344 [ # # ]: 0 : if ( pPropVal )
345 : : {
346 : 0 : pPropVal->Value >>= aRetValue;
347 : : }
348 [ # # ][ # # ]: 0 : else if ( ImplGetPropertyValue( aAny, xPropSet, rKey, sal_True ) )
349 : : {
350 : 0 : aAny >>= aRetValue;
351 : : }
352 : 0 : PropertyValue aString;
353 : 0 : aString.Name = rKey;
354 [ # # ]: 0 : aString.Value <<= aRetValue;
355 [ # # ]: 0 : WritePropertyValue( aFilterData, aString );
356 : 0 : return aRetValue;
357 : : }
358 : :
359 : 0 : void FilterConfigItem::WriteBool( const OUString& rKey, sal_Bool bNewValue )
360 : : {
361 : 0 : PropertyValue aBool;
362 : 0 : aBool.Name = rKey;
363 [ # # ]: 0 : aBool.Value <<= bNewValue;
364 [ # # ]: 0 : WritePropertyValue( aFilterData, aBool );
365 : :
366 [ # # ]: 0 : if ( xPropSet.is() )
367 : : {
368 : 0 : Any aAny;
369 [ # # ][ # # ]: 0 : if ( ImplGetPropertyValue( aAny, xPropSet, rKey, sal_True ) )
370 : : {
371 : 0 : sal_Bool bOldValue(sal_True);
372 [ # # ]: 0 : if ( aAny >>= bOldValue )
373 : : {
374 [ # # ]: 0 : if ( bOldValue != bNewValue )
375 : : {
376 [ # # ]: 0 : aAny <<= bNewValue;
377 : : try
378 : : {
379 [ # # ][ # # ]: 0 : xPropSet->setPropertyValue( rKey, aAny );
380 : 0 : bModified = sal_True;
381 : : }
382 [ # # ]: 0 : catch ( ::com::sun::star::uno::Exception& )
383 : : {
384 : : OSL_FAIL( "FilterConfigItem::WriteBool - could not set PropertyValue" );
385 : : }
386 : : }
387 : : }
388 : 0 : }
389 : 0 : }
390 [ # # ]: 0 : }
391 : :
392 : 0 : void FilterConfigItem::WriteInt32( const OUString& rKey, sal_Int32 nNewValue )
393 : : {
394 : 0 : PropertyValue aInt32;
395 : 0 : aInt32.Name = rKey;
396 [ # # ]: 0 : aInt32.Value <<= nNewValue;
397 [ # # ]: 0 : WritePropertyValue( aFilterData, aInt32 );
398 : :
399 [ # # ]: 0 : if ( xPropSet.is() )
400 : : {
401 : 0 : Any aAny;
402 : :
403 [ # # ][ # # ]: 0 : if ( ImplGetPropertyValue( aAny, xPropSet, rKey, sal_True ) )
404 : : {
405 : 0 : sal_Int32 nOldValue = 0;
406 [ # # ]: 0 : if ( aAny >>= nOldValue )
407 : : {
408 [ # # ]: 0 : if ( nOldValue != nNewValue )
409 : : {
410 [ # # ]: 0 : aAny <<= nNewValue;
411 : : try
412 : : {
413 [ # # ][ # # ]: 0 : xPropSet->setPropertyValue( rKey, aAny );
414 : 0 : bModified = sal_True;
415 : : }
416 [ # # ]: 0 : catch ( ::com::sun::star::uno::Exception& )
417 : : {
418 : : OSL_FAIL( "FilterConfigItem::WriteInt32 - could not set PropertyValue" );
419 : : }
420 : : }
421 : : }
422 : 0 : }
423 : 0 : }
424 [ # # ]: 0 : }
425 : :
426 : : // ------------------------------------------------------------------------
427 : :
428 : 0 : Sequence< PropertyValue > FilterConfigItem::GetFilterData() const
429 : : {
430 : 0 : return aFilterData;
431 : : }
432 : :
433 : : // ------------------------------------------------------------------------
434 : :
435 : 87 : Reference< XStatusIndicator > FilterConfigItem::GetStatusIndicator() const
436 : : {
437 : 87 : Reference< XStatusIndicator > xStatusIndicator;
438 [ + - ]: 87 : const rtl::OUString sStatusIndicator( RTL_CONSTASCII_USTRINGPARAM( "StatusIndicator" ) );
439 : :
440 : 87 : sal_Int32 i, nCount = aFilterData.getLength();
441 [ + + ]: 348 : for ( i = 0; i < nCount; i++ )
442 : : {
443 [ - + ]: 261 : if ( aFilterData[ i ].Name == sStatusIndicator )
444 : : {
445 [ # # ]: 0 : aFilterData[ i ].Value >>= xStatusIndicator;
446 : 0 : break;
447 : : }
448 : : }
449 : 87 : return xStatusIndicator;
450 : : }
451 : :
452 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|