merge the formfield patch from ooo-build
[ooovba.git] / svtools / source / filter.vcl / filter / FilterConfigItem.cxx
blob9c630919a22e2321fb52d17d09a5cfa0fbc13de7
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: FilterConfigItem.cxx,v $
10 * $Revision: 1.17 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svtools.hxx"
33 #include <svtools/FilterConfigItem.hxx>
35 #include <tools/debug.hxx>
36 #include <unotools/configmgr.hxx>
37 #include <unotools/processfactory.hxx>
38 #include <com/sun/star/beans/PropertyValue.hpp>
39 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
40 #include <com/sun/star/util/XChangesBatch.hpp>
41 #include <com/sun/star/beans/XPropertySetInfo.hpp>
42 #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
44 using namespace ::rtl;
45 using namespace ::utl ; // getProcessServiceFactory
46 using namespace ::com::sun::star::lang ; // XMultiServiceFactory
47 using namespace ::com::sun::star::beans ; // PropertyValue
48 using namespace ::com::sun::star::uno ; // Reference
49 using namespace ::com::sun::star::util ; // XChangesBatch
50 using namespace ::com::sun::star::awt ; // Size
51 using namespace ::com::sun::star::container ; //
52 using namespace ::com::sun::star::task ; // XStatusIndicator
54 static sal_Bool ImpIsTreeAvailable( Reference< XMultiServiceFactory >& rXCfgProv, const String& rTree )
56 sal_Bool bAvailable = rTree.Len() != 0;
57 if ( bAvailable )
59 xub_StrLen nTokenCount = rTree.GetTokenCount( (sal_Unicode)'/' );
60 xub_StrLen i = 0;
62 if ( rTree.GetChar( 0 ) == (sal_Unicode)'/' )
63 i++;
64 if ( rTree.GetChar( rTree.Len() - 1 ) == (sal_Unicode)'/' )
65 nTokenCount--;
67 Any aAny;
68 aAny <<= (OUString)rTree.GetToken( i++, (sal_Unicode)'/' );
70 // creation arguments: nodepath
71 PropertyValue aPathArgument;
72 aPathArgument.Name = OUString( RTL_CONSTASCII_USTRINGPARAM( "nodepath" ) );
73 aPathArgument.Value = aAny;
75 Sequence< Any > aArguments( 1 );
76 aArguments[ 0 ] <<= aPathArgument;
78 Reference< XInterface > xReadAccess;
79 try
81 xReadAccess = rXCfgProv->createInstanceWithArguments(
82 OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.configuration.ConfigurationAccess" ) ),
83 aArguments );
85 catch ( ::com::sun::star::uno::Exception& )
87 bAvailable = sal_False;
89 if ( xReadAccess.is() )
91 for ( ; bAvailable && ( i < nTokenCount ); i++ )
93 Reference< XHierarchicalNameAccess > xHierarchicalNameAccess
94 ( xReadAccess, UNO_QUERY );
96 if ( !xHierarchicalNameAccess.is() )
97 bAvailable = sal_False;
98 else
100 String aNode( rTree.GetToken( i, (sal_Unicode)'/' ) );
101 if ( !xHierarchicalNameAccess->hasByHierarchicalName( aNode ) )
102 bAvailable = sal_False;
103 else
105 Any a( xHierarchicalNameAccess->getByHierarchicalName( aNode ) );
108 a >>= xReadAccess;
110 catch ( ::com::sun::star::uno::Exception& )
112 bAvailable = sal_False;
119 return bAvailable;
122 void FilterConfigItem::ImpInitTree( const String& rSubTree )
124 bModified = sal_False;
126 OUString sTree( ConfigManager::GetConfigBaseURL() );
127 sTree += rSubTree;
128 Reference< XMultiServiceFactory > xSMGR = getProcessServiceFactory(); // get global uno service manager
130 Reference< XMultiServiceFactory > xCfgProv(
131 xSMGR->createInstance( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.configuration.ConfigurationProvider" ) ) ),
132 UNO_QUERY );
134 if ( xCfgProv.is() )
136 if ( ImpIsTreeAvailable( xCfgProv, String( sTree ) ) )
138 Any aAny;
139 // creation arguments: nodepath
140 PropertyValue aPathArgument;
141 aAny <<= sTree;
142 aPathArgument.Name = OUString( RTL_CONSTASCII_USTRINGPARAM( "nodepath" ) );
143 aPathArgument.Value = aAny;
145 // creation arguments: commit mode
146 PropertyValue aModeArgument;
147 sal_Bool bAsyncron = sal_True;
148 aAny <<= bAsyncron;
149 aModeArgument.Name = OUString( RTL_CONSTASCII_USTRINGPARAM( "lazywrite" ) );
150 aModeArgument.Value = aAny;
152 Sequence< Any > aArguments( 2 );
153 aArguments[ 0 ] <<= aPathArgument;
154 aArguments[ 1 ] <<= aModeArgument;
158 xUpdatableView = xCfgProv->createInstanceWithArguments(
159 OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.configuration.ConfigurationUpdateAccess" ) ),
160 aArguments );
161 if ( xUpdatableView.is() )
162 xPropSet = Reference< XPropertySet >( xUpdatableView, UNO_QUERY );
164 catch ( ::com::sun::star::uno::Exception& )
166 DBG_ERROR( "FilterConfigItem::FilterConfigItem - Could not access configuration Key" );
172 FilterConfigItem::FilterConfigItem( const OUString& rSubTree )
174 ImpInitTree( rSubTree );
177 FilterConfigItem::FilterConfigItem( ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >* pFilterData )
179 if ( pFilterData )
180 aFilterData = *pFilterData;
183 FilterConfigItem::FilterConfigItem( const OUString& rSubTree,
184 ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >* pFilterData )
186 ImpInitTree( rSubTree );
188 if ( pFilterData )
189 aFilterData = *pFilterData;
192 FilterConfigItem::~FilterConfigItem()
194 if ( xUpdatableView.is() )
196 if ( xPropSet.is() && bModified )
198 Reference< XChangesBatch > xUpdateControl( xUpdatableView, UNO_QUERY );
199 if ( xUpdateControl.is() )
203 xUpdateControl->commitChanges();
205 catch ( ::com::sun::star::uno::Exception& )
207 DBG_ERROR( "FilterConfigItem::FilterConfigItem - Could not update configuration data" );
214 sal_Bool FilterConfigItem::ImplGetPropertyValue( Any& rAny, const Reference< XPropertySet >& rXPropSet, const OUString& rString, sal_Bool bTestPropertyAvailability )
216 sal_Bool bRetValue = sal_True;
218 if ( rXPropSet.is() )
220 if ( bTestPropertyAvailability )
222 bRetValue = sal_False;
225 Reference< XPropertySetInfo >
226 aXPropSetInfo( rXPropSet->getPropertySetInfo() );
227 if ( aXPropSetInfo.is() )
228 bRetValue = aXPropSetInfo->hasPropertyByName( rString );
230 catch( ::com::sun::star::uno::Exception& )
235 if ( bRetValue )
239 rAny = rXPropSet->getPropertyValue( rString );
240 if ( !rAny.hasValue() )
241 bRetValue = sal_False;
243 catch( ::com::sun::star::uno::Exception& )
245 bRetValue = sal_False;
249 else
250 bRetValue = sal_False;
251 return bRetValue;
255 // if property is available it returns a pointer,
256 // otherwise the result is null
257 PropertyValue* FilterConfigItem::GetPropertyValue( Sequence< PropertyValue >& rPropSeq, const OUString& rName )
259 PropertyValue* pPropValue = NULL;
261 sal_Int32 i, nCount;
262 for ( i = 0, nCount = rPropSeq.getLength(); i < nCount; i++ )
264 if ( rPropSeq[ i ].Name == rName )
266 pPropValue = &rPropSeq[ i ];
267 break;
270 return pPropValue;
273 /* if PropertySequence already includes a PropertyValue using the same name, the
274 corresponding PropertyValue is replaced, otherwise the given PropertyValue
275 will be appended */
277 sal_Bool FilterConfigItem::WritePropertyValue( Sequence< PropertyValue >& rPropSeq, const PropertyValue& rPropValue )
279 sal_Bool bRet = sal_False;
280 if ( rPropValue.Name.getLength() )
282 sal_Int32 i, nCount;
283 for ( i = 0, nCount = rPropSeq.getLength(); i < nCount; i++ )
285 if ( rPropSeq[ i ].Name == rPropValue.Name )
286 break;
288 if ( i == nCount )
289 rPropSeq.realloc( ++nCount );
291 rPropSeq[ i ] = rPropValue;
293 bRet = sal_True;
295 return bRet;
298 sal_Bool FilterConfigItem::ReadBool( const OUString& rKey, sal_Bool bDefault )
300 Any aAny;
301 sal_Bool bRetValue = bDefault;
302 PropertyValue* pPropVal = GetPropertyValue( aFilterData, rKey );
303 if ( pPropVal )
305 pPropVal->Value >>= bRetValue;
307 else if ( ImplGetPropertyValue( aAny, xPropSet, rKey, sal_True ) )
309 aAny >>= bRetValue;
311 PropertyValue aBool;
312 aBool.Name = rKey;
313 aBool.Value <<= bRetValue;
314 WritePropertyValue( aFilterData, aBool );
315 return bRetValue;
318 sal_Int32 FilterConfigItem::ReadInt32( const OUString& rKey, sal_Int32 nDefault )
320 Any aAny;
321 sal_Int32 nRetValue = nDefault;
322 PropertyValue* pPropVal = GetPropertyValue( aFilterData, rKey );
323 if ( pPropVal )
325 pPropVal->Value >>= nRetValue;
327 else if ( ImplGetPropertyValue( aAny, xPropSet, rKey, sal_True ) )
329 aAny >>= nRetValue;
331 PropertyValue aInt32;
332 aInt32.Name = rKey;
333 aInt32.Value <<= nRetValue;
334 WritePropertyValue( aFilterData, aInt32 );
335 return nRetValue;
339 Size FilterConfigItem::ReadSize( const OUString& rKey, const Size& rDefault )
341 Any aAny;
342 Size aRetValue( rDefault );
344 const OUString sWidth( RTL_CONSTASCII_USTRINGPARAM( "LogicalWidth" ) );
345 const OUString sHeight( RTL_CONSTASCII_USTRINGPARAM( "LogicalHeight" ) );
347 Reference< XPropertySet > aXPropSet;
350 PropertyValue* pPropWidth = GetPropertyValue( aFilterData, sWidth );
351 PropertyValue* pPropHeight= GetPropertyValue( aFilterData, sHeight );
352 if ( pPropWidth && pPropHeight )
354 pPropWidth->Value >>= aRetValue.Width;
355 pPropHeight->Value >>= aRetValue.Height;
357 else if ( ImplGetPropertyValue( aAny, xPropSet, rKey, sal_True ) )
359 if ( aAny >>= aXPropSet )
361 if ( ImplGetPropertyValue( aAny, aXPropSet, sWidth, sal_True ) )
362 aAny >>= aRetValue.Width;
363 if ( ImplGetPropertyValue( aAny, aXPropSet, sHeight, sal_True ) )
364 aAny >>= aRetValue.Height;
368 catch ( ::com::sun::star::uno::Exception& )
370 DBG_ERROR( "FilterConfigItem::ReadSize - could not read PropertyValue" );
372 PropertyValue aWidth;
373 aWidth.Name = sWidth;
374 aWidth.Value <<= aRetValue.Width;
375 WritePropertyValue( aFilterData, aWidth );
376 PropertyValue aHeight;
377 aHeight.Name = sHeight;
378 aHeight.Value <<= aRetValue.Height;
379 WritePropertyValue( aFilterData, aHeight );
380 return aRetValue;
383 OUString FilterConfigItem::ReadString( const OUString& rKey, const OUString& rDefault )
385 Any aAny;
386 OUString aRetValue( rDefault );
387 PropertyValue* pPropVal = GetPropertyValue( aFilterData, rKey );
388 if ( pPropVal )
390 pPropVal->Value >>= aRetValue;
392 else if ( ImplGetPropertyValue( aAny, xPropSet, rKey, sal_True ) )
394 aAny >>= aRetValue;
396 PropertyValue aString;
397 aString.Name = rKey;
398 aString.Value <<= aRetValue;
399 WritePropertyValue( aFilterData, aString );
400 return aRetValue;
403 Any FilterConfigItem::ReadAny( const ::rtl::OUString& rKey, const Any& rDefault )
405 Any aAny, aRetValue( rDefault );
406 PropertyValue* pPropVal = GetPropertyValue( aFilterData, rKey );
407 if ( pPropVal )
409 aRetValue = pPropVal->Value;
411 else if ( ImplGetPropertyValue( aAny, xPropSet, rKey, sal_True ) )
413 aRetValue = aAny;
415 PropertyValue aPropValue;
416 aPropValue.Name = rKey;
417 aPropValue.Value = aRetValue;
418 WritePropertyValue( aFilterData, aPropValue );
419 return aRetValue;
422 void FilterConfigItem::WriteBool( const OUString& rKey, sal_Bool bNewValue )
424 PropertyValue aBool;
425 aBool.Name = rKey;
426 aBool.Value <<= bNewValue;
427 WritePropertyValue( aFilterData, aBool );
429 if ( xPropSet.is() )
431 Any aAny;
432 if ( ImplGetPropertyValue( aAny, xPropSet, rKey, sal_True ) )
434 sal_Bool bOldValue;
435 if ( aAny >>= bOldValue )
437 if ( bOldValue != bNewValue )
439 aAny <<= bNewValue;
442 xPropSet->setPropertyValue( rKey, aAny );
443 bModified = sal_True;
445 catch ( ::com::sun::star::uno::Exception& )
447 DBG_ERROR( "FilterConfigItem::WriteBool - could not set PropertyValue" );
455 void FilterConfigItem::WriteInt32( const OUString& rKey, sal_Int32 nNewValue )
457 PropertyValue aInt32;
458 aInt32.Name = rKey;
459 aInt32.Value <<= nNewValue;
460 WritePropertyValue( aFilterData, aInt32 );
462 if ( xPropSet.is() )
464 Any aAny;
466 if ( ImplGetPropertyValue( aAny, xPropSet, rKey, sal_True ) )
468 sal_Int32 nOldValue;
469 if ( aAny >>= nOldValue )
471 if ( nOldValue != nNewValue )
473 aAny <<= nNewValue;
476 xPropSet->setPropertyValue( rKey, aAny );
477 bModified = sal_True;
479 catch ( ::com::sun::star::uno::Exception& )
481 DBG_ERROR( "FilterConfigItem::WriteInt32 - could not set PropertyValue" );
489 void FilterConfigItem::WriteSize( const OUString& rKey, const Size& rNewValue )
491 const OUString sWidth( RTL_CONSTASCII_USTRINGPARAM( "LogicalWidth" ) );
492 const OUString sHeight( RTL_CONSTASCII_USTRINGPARAM( "LogicalHeight" ) );
494 PropertyValue aWidth;
495 aWidth.Name = sWidth;
496 aWidth.Value <<= rNewValue.Width;
497 WritePropertyValue( aFilterData, aWidth );
499 PropertyValue aHeight;
500 aHeight.Name = sHeight;
501 aHeight.Value <<= rNewValue.Height;
502 WritePropertyValue( aFilterData, aHeight );
504 if ( xPropSet.is() )
506 Any aAny;
507 sal_Int32 nOldWidth = rNewValue.Width;
508 sal_Int32 nOldHeight = rNewValue.Height;
510 if ( ImplGetPropertyValue( aAny, xPropSet, rKey, sal_True ) )
514 Reference< XPropertySet > aXPropSet;
515 if ( aAny >>= aXPropSet )
517 if ( ImplGetPropertyValue( aAny, aXPropSet, sWidth, sal_True ) )
518 aAny >>= nOldWidth;
519 if ( ImplGetPropertyValue( aAny, aXPropSet, sHeight, sal_True ) )
520 aAny >>= nOldHeight;
522 if ( ( nOldWidth != rNewValue.Width ) || ( nOldHeight != rNewValue.Height ) )
524 aAny <<= rNewValue.Width;
525 aXPropSet->setPropertyValue( sWidth, aAny );
526 aAny <<= rNewValue.Height;
527 aXPropSet->setPropertyValue( sHeight, aAny );
528 bModified = sal_True;
531 catch ( ::com::sun::star::uno::Exception& )
533 DBG_ERROR( "FilterConfigItem::WriteSize - could not read PropertyValue" );
539 void FilterConfigItem::WriteString( const OUString& rKey, const OUString& rNewValue )
541 PropertyValue aString;
542 aString.Name = rKey;
543 aString.Value <<= rNewValue;
544 WritePropertyValue( aFilterData, aString );
546 if ( xPropSet.is() )
548 Any aAny;
550 if ( ImplGetPropertyValue( aAny, xPropSet, rKey, sal_True ) )
552 OUString aOldValue;
553 if ( aAny >>= aOldValue )
555 if ( aOldValue != rNewValue )
557 aAny <<= rNewValue;
560 xPropSet->setPropertyValue( rKey, aAny );
561 bModified = sal_True;
563 catch ( ::com::sun::star::uno::Exception& )
565 DBG_ERROR( "FilterConfigItem::WriteInt32 - could not set PropertyValue" );
573 void FilterConfigItem::WriteAny( const OUString& rKey, const Any& rNewAny )
575 PropertyValue aPropValue;
576 aPropValue.Name = rKey;
577 aPropValue.Value = rNewAny;
578 WritePropertyValue( aFilterData, aPropValue );
579 if ( xPropSet.is() )
581 Any aAny;
582 if ( ImplGetPropertyValue( aAny, xPropSet, rKey, sal_True ) )
584 if ( aAny != rNewAny )
588 xPropSet->setPropertyValue( rKey, rNewAny );
589 bModified = sal_True;
591 catch ( com::sun::star::uno::Exception& )
593 DBG_ERROR( "FilterConfigItem::WriteAny - could not set PropertyValue" );
601 // ------------------------------------------------------------------------
603 Sequence< PropertyValue > FilterConfigItem::GetFilterData() const
605 return aFilterData;
608 // ------------------------------------------------------------------------
610 Reference< XStatusIndicator > FilterConfigItem::GetStatusIndicator() const
612 Reference< XStatusIndicator > xStatusIndicator;
613 const rtl::OUString sStatusIndicator( RTL_CONSTASCII_USTRINGPARAM( "StatusIndicator" ) );
615 sal_Int32 i, nCount = aFilterData.getLength();
616 for ( i = 0; i < nCount; i++ )
618 if ( aFilterData[ i ].Name == sStatusIndicator )
620 aFilterData[ i ].Value >>= xStatusIndicator;
621 break;
624 return xStatusIndicator;