1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: FilterConfigItem.cxx,v $
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;
59 xub_StrLen nTokenCount
= rTree
.GetTokenCount( (sal_Unicode
)'/' );
62 if ( rTree
.GetChar( 0 ) == (sal_Unicode
)'/' )
64 if ( rTree
.GetChar( rTree
.Len() - 1 ) == (sal_Unicode
)'/' )
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
;
81 xReadAccess
= rXCfgProv
->createInstanceWithArguments(
82 OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.configuration.ConfigurationAccess" ) ),
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
;
100 String
aNode( rTree
.GetToken( i
, (sal_Unicode
)'/' ) );
101 if ( !xHierarchicalNameAccess
->hasByHierarchicalName( aNode
) )
102 bAvailable
= sal_False
;
105 Any
a( xHierarchicalNameAccess
->getByHierarchicalName( aNode
) );
110 catch ( ::com::sun::star::uno::Exception
& )
112 bAvailable
= sal_False
;
122 void FilterConfigItem::ImpInitTree( const String
& rSubTree
)
124 bModified
= sal_False
;
126 OUString
sTree( ConfigManager::GetConfigBaseURL() );
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" ) ) ),
136 if ( ImpIsTreeAvailable( xCfgProv
, String( sTree
) ) )
139 // creation arguments: nodepath
140 PropertyValue aPathArgument
;
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
;
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" ) ),
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
)
180 aFilterData
= *pFilterData
;
183 FilterConfigItem::FilterConfigItem( const OUString
& rSubTree
,
184 ::com::sun::star::uno::Sequence
< ::com::sun::star::beans::PropertyValue
>* pFilterData
)
186 ImpInitTree( rSubTree
);
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
& )
239 rAny
= rXPropSet
->getPropertyValue( rString
);
240 if ( !rAny
.hasValue() )
241 bRetValue
= sal_False
;
243 catch( ::com::sun::star::uno::Exception
& )
245 bRetValue
= sal_False
;
250 bRetValue
= sal_False
;
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
;
262 for ( i
= 0, nCount
= rPropSeq
.getLength(); i
< nCount
; i
++ )
264 if ( rPropSeq
[ i
].Name
== rName
)
266 pPropValue
= &rPropSeq
[ i
];
273 /* if PropertySequence already includes a PropertyValue using the same name, the
274 corresponding PropertyValue is replaced, otherwise the given PropertyValue
277 sal_Bool
FilterConfigItem::WritePropertyValue( Sequence
< PropertyValue
>& rPropSeq
, const PropertyValue
& rPropValue
)
279 sal_Bool bRet
= sal_False
;
280 if ( rPropValue
.Name
.getLength() )
283 for ( i
= 0, nCount
= rPropSeq
.getLength(); i
< nCount
; i
++ )
285 if ( rPropSeq
[ i
].Name
== rPropValue
.Name
)
289 rPropSeq
.realloc( ++nCount
);
291 rPropSeq
[ i
] = rPropValue
;
298 sal_Bool
FilterConfigItem::ReadBool( const OUString
& rKey
, sal_Bool bDefault
)
301 sal_Bool bRetValue
= bDefault
;
302 PropertyValue
* pPropVal
= GetPropertyValue( aFilterData
, rKey
);
305 pPropVal
->Value
>>= bRetValue
;
307 else if ( ImplGetPropertyValue( aAny
, xPropSet
, rKey
, sal_True
) )
313 aBool
.Value
<<= bRetValue
;
314 WritePropertyValue( aFilterData
, aBool
);
318 sal_Int32
FilterConfigItem::ReadInt32( const OUString
& rKey
, sal_Int32 nDefault
)
321 sal_Int32 nRetValue
= nDefault
;
322 PropertyValue
* pPropVal
= GetPropertyValue( aFilterData
, rKey
);
325 pPropVal
->Value
>>= nRetValue
;
327 else if ( ImplGetPropertyValue( aAny
, xPropSet
, rKey
, sal_True
) )
331 PropertyValue aInt32
;
333 aInt32
.Value
<<= nRetValue
;
334 WritePropertyValue( aFilterData
, aInt32
);
339 Size
FilterConfigItem::ReadSize( const OUString
& rKey
, const Size
& rDefault
)
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
);
383 OUString
FilterConfigItem::ReadString( const OUString
& rKey
, const OUString
& rDefault
)
386 OUString
aRetValue( rDefault
);
387 PropertyValue
* pPropVal
= GetPropertyValue( aFilterData
, rKey
);
390 pPropVal
->Value
>>= aRetValue
;
392 else if ( ImplGetPropertyValue( aAny
, xPropSet
, rKey
, sal_True
) )
396 PropertyValue aString
;
398 aString
.Value
<<= aRetValue
;
399 WritePropertyValue( aFilterData
, aString
);
403 Any
FilterConfigItem::ReadAny( const ::rtl::OUString
& rKey
, const Any
& rDefault
)
405 Any aAny
, aRetValue( rDefault
);
406 PropertyValue
* pPropVal
= GetPropertyValue( aFilterData
, rKey
);
409 aRetValue
= pPropVal
->Value
;
411 else if ( ImplGetPropertyValue( aAny
, xPropSet
, rKey
, sal_True
) )
415 PropertyValue aPropValue
;
416 aPropValue
.Name
= rKey
;
417 aPropValue
.Value
= aRetValue
;
418 WritePropertyValue( aFilterData
, aPropValue
);
422 void FilterConfigItem::WriteBool( const OUString
& rKey
, sal_Bool bNewValue
)
426 aBool
.Value
<<= bNewValue
;
427 WritePropertyValue( aFilterData
, aBool
);
432 if ( ImplGetPropertyValue( aAny
, xPropSet
, rKey
, sal_True
) )
435 if ( aAny
>>= bOldValue
)
437 if ( bOldValue
!= 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
;
459 aInt32
.Value
<<= nNewValue
;
460 WritePropertyValue( aFilterData
, aInt32
);
466 if ( ImplGetPropertyValue( aAny
, xPropSet
, rKey
, sal_True
) )
469 if ( aAny
>>= nOldValue
)
471 if ( nOldValue
!= 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
);
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
) )
519 if ( ImplGetPropertyValue( aAny
, aXPropSet
, sHeight
, sal_True
) )
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
;
543 aString
.Value
<<= rNewValue
;
544 WritePropertyValue( aFilterData
, aString
);
550 if ( ImplGetPropertyValue( aAny
, xPropSet
, rKey
, sal_True
) )
553 if ( aAny
>>= aOldValue
)
555 if ( aOldValue
!= 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
);
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
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
;
624 return xStatusIndicator
;