1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <vcl/FilterConfigItem.hxx>
22 #include <unotools/configmgr.hxx>
23 #include <comphelper/processfactory.hxx>
24 #include <comphelper/string.hxx>
25 #include <com/sun/star/beans/PropertyValue.hpp>
26 #include <com/sun/star/configuration/theDefaultProvider.hpp>
27 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
28 #include <com/sun/star/util/XChangesBatch.hpp>
29 #include <com/sun/star/beans/XPropertySetInfo.hpp>
30 #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
32 using namespace ::com::sun::star::lang
; // XMultiServiceFactory
33 using namespace ::com::sun::star::beans
; // PropertyValue
34 using namespace ::com::sun::star::uno
; // Reference
35 using namespace ::com::sun::star::util
; // XChangesBatch
36 using namespace ::com::sun::star::awt
; // Size
37 using namespace ::com::sun::star::container
; //
38 using namespace ::com::sun::star::configuration
; //
39 using namespace ::com::sun::star::task
; // XStatusIndicator
41 static sal_Bool
ImpIsTreeAvailable( Reference
< XMultiServiceFactory
>& rXCfgProv
, const OUString
& rTree
)
43 sal_Bool bAvailable
= !rTree
.isEmpty();
46 using comphelper::string::getTokenCount
;
47 using comphelper::string::getToken
;
49 sal_Int32 nTokenCount
= getTokenCount(rTree
, '/');
52 if ( rTree
[0] == '/' )
54 if ( rTree
[rTree
.getLength() - 1] == '/' )
58 aAny
<<= getToken(rTree
, i
++, '/');
60 // creation arguments: nodepath
61 PropertyValue aPathArgument
;
62 aPathArgument
.Name
= OUString( "nodepath" );
63 aPathArgument
.Value
= aAny
;
65 Sequence
< Any
> aArguments( 1 );
66 aArguments
[ 0 ] <<= aPathArgument
;
68 Reference
< XInterface
> xReadAccess
;
71 xReadAccess
= rXCfgProv
->createInstanceWithArguments(
72 OUString( "com.sun.star.configuration.ConfigurationAccess" ),
75 catch (const ::com::sun::star::uno::Exception
&)
77 bAvailable
= sal_False
;
79 if ( xReadAccess
.is() )
81 for ( ; bAvailable
&& ( i
< nTokenCount
); i
++ )
83 Reference
< XHierarchicalNameAccess
> xHierarchicalNameAccess
84 ( xReadAccess
, UNO_QUERY
);
86 if ( !xHierarchicalNameAccess
.is() )
87 bAvailable
= sal_False
;
90 OUString
aNode( getToken(rTree
, i
, '/') );
91 if ( !xHierarchicalNameAccess
->hasByHierarchicalName( aNode
) )
92 bAvailable
= sal_False
;
95 Any
a( xHierarchicalNameAccess
->getByHierarchicalName( aNode
) );
100 catch ( ::com::sun::star::uno::Exception
& )
102 bAvailable
= sal_False
;
112 void FilterConfigItem::ImpInitTree( const String
& rSubTree
)
114 bModified
= sal_False
;
116 Reference
< XComponentContext
> xContext( comphelper::getProcessComponentContext() );
118 Reference
< XMultiServiceFactory
> xCfgProv
= theDefaultProvider::get( xContext
);
120 OUString
sTree(OUString("/org.openoffice.") + rSubTree
);
121 if ( ImpIsTreeAvailable(xCfgProv
, sTree
) )
124 // creation arguments: nodepath
125 PropertyValue aPathArgument
;
127 aPathArgument
.Name
= OUString( "nodepath" );
128 aPathArgument
.Value
= aAny
;
130 // creation arguments: commit mode
131 PropertyValue aModeArgument
;
132 sal_Bool bAsyncron
= sal_True
;
134 aModeArgument
.Name
= OUString( "lazywrite" );
135 aModeArgument
.Value
= aAny
;
137 Sequence
< Any
> aArguments( 2 );
138 aArguments
[ 0 ] <<= aPathArgument
;
139 aArguments
[ 1 ] <<= aModeArgument
;
143 xUpdatableView
= xCfgProv
->createInstanceWithArguments(
144 OUString( "com.sun.star.configuration.ConfigurationUpdateAccess" ),
146 if ( xUpdatableView
.is() )
147 xPropSet
= Reference
< XPropertySet
>( xUpdatableView
, UNO_QUERY
);
149 catch ( ::com::sun::star::uno::Exception
& )
151 OSL_FAIL( "FilterConfigItem::FilterConfigItem - Could not access configuration Key" );
156 FilterConfigItem::FilterConfigItem( const OUString
& rSubTree
)
158 ImpInitTree( rSubTree
);
161 FilterConfigItem::FilterConfigItem( ::com::sun::star::uno::Sequence
< ::com::sun::star::beans::PropertyValue
>* pFilterData
)
164 aFilterData
= *pFilterData
;
167 FilterConfigItem::FilterConfigItem( const OUString
& rSubTree
,
168 ::com::sun::star::uno::Sequence
< ::com::sun::star::beans::PropertyValue
>* pFilterData
)
170 ImpInitTree( rSubTree
);
173 aFilterData
= *pFilterData
;
176 FilterConfigItem::~FilterConfigItem()
178 if ( xUpdatableView
.is() )
180 if ( xPropSet
.is() && bModified
)
182 Reference
< XChangesBatch
> xUpdateControl( xUpdatableView
, UNO_QUERY
);
183 if ( xUpdateControl
.is() )
187 xUpdateControl
->commitChanges();
189 catch ( ::com::sun::star::uno::Exception
& )
191 OSL_FAIL( "FilterConfigItem::FilterConfigItem - Could not update configuration data" );
198 sal_Bool
FilterConfigItem::ImplGetPropertyValue( Any
& rAny
, const Reference
< XPropertySet
>& rXPropSet
, const OUString
& rString
, sal_Bool bTestPropertyAvailability
)
200 sal_Bool bRetValue
= sal_True
;
202 if ( rXPropSet
.is() )
204 if ( bTestPropertyAvailability
)
206 bRetValue
= sal_False
;
209 Reference
< XPropertySetInfo
>
210 aXPropSetInfo( rXPropSet
->getPropertySetInfo() );
211 if ( aXPropSetInfo
.is() )
212 bRetValue
= aXPropSetInfo
->hasPropertyByName( rString
);
214 catch( ::com::sun::star::uno::Exception
& )
223 rAny
= rXPropSet
->getPropertyValue( rString
);
224 if ( !rAny
.hasValue() )
225 bRetValue
= sal_False
;
227 catch( ::com::sun::star::uno::Exception
& )
229 bRetValue
= sal_False
;
234 bRetValue
= sal_False
;
239 // if property is available it returns a pointer,
240 // otherwise the result is null
241 PropertyValue
* FilterConfigItem::GetPropertyValue( Sequence
< PropertyValue
>& rPropSeq
, const OUString
& rName
)
243 PropertyValue
* pPropValue
= NULL
;
246 for ( i
= 0, nCount
= rPropSeq
.getLength(); i
< nCount
; i
++ )
248 if ( rPropSeq
[ i
].Name
== rName
)
250 pPropValue
= &rPropSeq
[ i
];
257 /* if PropertySequence already includes a PropertyValue using the same name, the
258 corresponding PropertyValue is replaced, otherwise the given PropertyValue
261 sal_Bool
FilterConfigItem::WritePropertyValue( Sequence
< PropertyValue
>& rPropSeq
, const PropertyValue
& rPropValue
)
263 sal_Bool bRet
= sal_False
;
264 if ( !rPropValue
.Name
.isEmpty() )
267 for ( i
= 0, nCount
= rPropSeq
.getLength(); i
< nCount
; i
++ )
269 if ( rPropSeq
[ i
].Name
== rPropValue
.Name
)
273 rPropSeq
.realloc( ++nCount
);
275 rPropSeq
[ i
] = rPropValue
;
282 sal_Bool
FilterConfigItem::ReadBool( const OUString
& rKey
, sal_Bool bDefault
)
285 sal_Bool bRetValue
= bDefault
;
286 PropertyValue
* pPropVal
= GetPropertyValue( aFilterData
, rKey
);
289 pPropVal
->Value
>>= bRetValue
;
291 else if ( ImplGetPropertyValue( aAny
, xPropSet
, rKey
, sal_True
) )
297 aBool
.Value
<<= bRetValue
;
298 WritePropertyValue( aFilterData
, aBool
);
302 sal_Int32
FilterConfigItem::ReadInt32( const OUString
& rKey
, sal_Int32 nDefault
)
305 sal_Int32 nRetValue
= nDefault
;
306 PropertyValue
* pPropVal
= GetPropertyValue( aFilterData
, rKey
);
309 pPropVal
->Value
>>= nRetValue
;
311 else if ( ImplGetPropertyValue( aAny
, xPropSet
, rKey
, sal_True
) )
315 PropertyValue aInt32
;
317 aInt32
.Value
<<= nRetValue
;
318 WritePropertyValue( aFilterData
, aInt32
);
322 OUString
FilterConfigItem::ReadString( const OUString
& rKey
, const OUString
& rDefault
)
325 OUString
aRetValue( rDefault
);
326 PropertyValue
* pPropVal
= GetPropertyValue( aFilterData
, rKey
);
329 pPropVal
->Value
>>= aRetValue
;
331 else if ( ImplGetPropertyValue( aAny
, xPropSet
, rKey
, sal_True
) )
335 PropertyValue aString
;
337 aString
.Value
<<= aRetValue
;
338 WritePropertyValue( aFilterData
, aString
);
342 void FilterConfigItem::WriteBool( const OUString
& rKey
, sal_Bool bNewValue
)
346 aBool
.Value
<<= bNewValue
;
347 WritePropertyValue( aFilterData
, aBool
);
352 if ( ImplGetPropertyValue( aAny
, xPropSet
, rKey
, sal_True
) )
354 sal_Bool
bOldValue(sal_True
);
355 if ( aAny
>>= bOldValue
)
357 if ( bOldValue
!= bNewValue
)
362 xPropSet
->setPropertyValue( rKey
, aAny
);
363 bModified
= sal_True
;
365 catch ( ::com::sun::star::uno::Exception
& )
367 OSL_FAIL( "FilterConfigItem::WriteBool - could not set PropertyValue" );
375 void FilterConfigItem::WriteInt32( const OUString
& rKey
, sal_Int32 nNewValue
)
377 PropertyValue aInt32
;
379 aInt32
.Value
<<= nNewValue
;
380 WritePropertyValue( aFilterData
, aInt32
);
386 if ( ImplGetPropertyValue( aAny
, xPropSet
, rKey
, sal_True
) )
388 sal_Int32 nOldValue
= 0;
389 if ( aAny
>>= nOldValue
)
391 if ( nOldValue
!= nNewValue
)
396 xPropSet
->setPropertyValue( rKey
, aAny
);
397 bModified
= sal_True
;
399 catch ( ::com::sun::star::uno::Exception
& )
401 OSL_FAIL( "FilterConfigItem::WriteInt32 - could not set PropertyValue" );
409 // ------------------------------------------------------------------------
411 Sequence
< PropertyValue
> FilterConfigItem::GetFilterData() const
416 // ------------------------------------------------------------------------
418 Reference
< XStatusIndicator
> FilterConfigItem::GetStatusIndicator() const
420 Reference
< XStatusIndicator
> xStatusIndicator
;
421 const OUString
sStatusIndicator( "StatusIndicator" );
423 sal_Int32 i
, nCount
= aFilterData
.getLength();
424 for ( i
= 0; i
< nCount
; i
++ )
426 if ( aFilterData
[ i
].Name
== sStatusIndicator
)
428 aFilterData
[ i
].Value
>>= xStatusIndicator
;
432 return xStatusIndicator
;
435 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */