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 ::rtl::OUString
;
33 using namespace ::utl
; // getProcessServiceFactory
34 using namespace ::com::sun::star::lang
; // XMultiServiceFactory
35 using namespace ::com::sun::star::beans
; // PropertyValue
36 using namespace ::com::sun::star::uno
; // Reference
37 using namespace ::com::sun::star::util
; // XChangesBatch
38 using namespace ::com::sun::star::awt
; // Size
39 using namespace ::com::sun::star::container
; //
40 using namespace ::com::sun::star::configuration
; //
41 using namespace ::com::sun::star::task
; // XStatusIndicator
43 static sal_Bool
ImpIsTreeAvailable( Reference
< XMultiServiceFactory
>& rXCfgProv
, const rtl::OUString
& rTree
)
45 sal_Bool bAvailable
= !rTree
.isEmpty();
48 using comphelper::string::getTokenCount
;
49 using comphelper::string::getToken
;
51 sal_Int32 nTokenCount
= getTokenCount(rTree
, '/');
54 if ( rTree
[0] == '/' )
56 if ( rTree
[rTree
.getLength() - 1] == '/' )
60 aAny
<<= getToken(rTree
, i
++, '/');
62 // creation arguments: nodepath
63 PropertyValue aPathArgument
;
64 aPathArgument
.Name
= OUString( RTL_CONSTASCII_USTRINGPARAM( "nodepath" ) );
65 aPathArgument
.Value
= aAny
;
67 Sequence
< Any
> aArguments( 1 );
68 aArguments
[ 0 ] <<= aPathArgument
;
70 Reference
< XInterface
> xReadAccess
;
73 xReadAccess
= rXCfgProv
->createInstanceWithArguments(
74 OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.configuration.ConfigurationAccess" ) ),
77 catch (const ::com::sun::star::uno::Exception
&)
79 bAvailable
= sal_False
;
81 if ( xReadAccess
.is() )
83 for ( ; bAvailable
&& ( i
< nTokenCount
); i
++ )
85 Reference
< XHierarchicalNameAccess
> xHierarchicalNameAccess
86 ( xReadAccess
, UNO_QUERY
);
88 if ( !xHierarchicalNameAccess
.is() )
89 bAvailable
= sal_False
;
92 rtl::OUString
aNode( getToken(rTree
, i
, '/') );
93 if ( !xHierarchicalNameAccess
->hasByHierarchicalName( aNode
) )
94 bAvailable
= sal_False
;
97 Any
a( xHierarchicalNameAccess
->getByHierarchicalName( aNode
) );
102 catch ( ::com::sun::star::uno::Exception
& )
104 bAvailable
= sal_False
;
114 void FilterConfigItem::ImpInitTree( const String
& rSubTree
)
116 bModified
= sal_False
;
118 Reference
< XComponentContext
> xContext( comphelper::getProcessComponentContext() );
120 Reference
< XMultiServiceFactory
> xCfgProv
= theDefaultProvider::get( xContext
);
123 OUString(RTL_CONSTASCII_USTRINGPARAM("/org.openoffice.")) +
125 if ( ImpIsTreeAvailable(xCfgProv
, sTree
) )
128 // creation arguments: nodepath
129 PropertyValue aPathArgument
;
131 aPathArgument
.Name
= OUString( RTL_CONSTASCII_USTRINGPARAM( "nodepath" ) );
132 aPathArgument
.Value
= aAny
;
134 // creation arguments: commit mode
135 PropertyValue aModeArgument
;
136 sal_Bool bAsyncron
= sal_True
;
138 aModeArgument
.Name
= OUString( RTL_CONSTASCII_USTRINGPARAM( "lazywrite" ) );
139 aModeArgument
.Value
= aAny
;
141 Sequence
< Any
> aArguments( 2 );
142 aArguments
[ 0 ] <<= aPathArgument
;
143 aArguments
[ 1 ] <<= aModeArgument
;
147 xUpdatableView
= xCfgProv
->createInstanceWithArguments(
148 OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.configuration.ConfigurationUpdateAccess" ) ),
150 if ( xUpdatableView
.is() )
151 xPropSet
= Reference
< XPropertySet
>( xUpdatableView
, UNO_QUERY
);
153 catch ( ::com::sun::star::uno::Exception
& )
155 OSL_FAIL( "FilterConfigItem::FilterConfigItem - Could not access configuration Key" );
160 FilterConfigItem::FilterConfigItem( const OUString
& rSubTree
)
162 ImpInitTree( rSubTree
);
165 FilterConfigItem::FilterConfigItem( ::com::sun::star::uno::Sequence
< ::com::sun::star::beans::PropertyValue
>* pFilterData
)
168 aFilterData
= *pFilterData
;
171 FilterConfigItem::FilterConfigItem( const OUString
& rSubTree
,
172 ::com::sun::star::uno::Sequence
< ::com::sun::star::beans::PropertyValue
>* pFilterData
)
174 ImpInitTree( rSubTree
);
177 aFilterData
= *pFilterData
;
180 FilterConfigItem::~FilterConfigItem()
182 if ( xUpdatableView
.is() )
184 if ( xPropSet
.is() && bModified
)
186 Reference
< XChangesBatch
> xUpdateControl( xUpdatableView
, UNO_QUERY
);
187 if ( xUpdateControl
.is() )
191 xUpdateControl
->commitChanges();
193 catch ( ::com::sun::star::uno::Exception
& )
195 OSL_FAIL( "FilterConfigItem::FilterConfigItem - Could not update configuration data" );
202 sal_Bool
FilterConfigItem::ImplGetPropertyValue( Any
& rAny
, const Reference
< XPropertySet
>& rXPropSet
, const OUString
& rString
, sal_Bool bTestPropertyAvailability
)
204 sal_Bool bRetValue
= sal_True
;
206 if ( rXPropSet
.is() )
208 if ( bTestPropertyAvailability
)
210 bRetValue
= sal_False
;
213 Reference
< XPropertySetInfo
>
214 aXPropSetInfo( rXPropSet
->getPropertySetInfo() );
215 if ( aXPropSetInfo
.is() )
216 bRetValue
= aXPropSetInfo
->hasPropertyByName( rString
);
218 catch( ::com::sun::star::uno::Exception
& )
227 rAny
= rXPropSet
->getPropertyValue( rString
);
228 if ( !rAny
.hasValue() )
229 bRetValue
= sal_False
;
231 catch( ::com::sun::star::uno::Exception
& )
233 bRetValue
= sal_False
;
238 bRetValue
= sal_False
;
243 // if property is available it returns a pointer,
244 // otherwise the result is null
245 PropertyValue
* FilterConfigItem::GetPropertyValue( Sequence
< PropertyValue
>& rPropSeq
, const OUString
& rName
)
247 PropertyValue
* pPropValue
= NULL
;
250 for ( i
= 0, nCount
= rPropSeq
.getLength(); i
< nCount
; i
++ )
252 if ( rPropSeq
[ i
].Name
== rName
)
254 pPropValue
= &rPropSeq
[ i
];
261 /* if PropertySequence already includes a PropertyValue using the same name, the
262 corresponding PropertyValue is replaced, otherwise the given PropertyValue
265 sal_Bool
FilterConfigItem::WritePropertyValue( Sequence
< PropertyValue
>& rPropSeq
, const PropertyValue
& rPropValue
)
267 sal_Bool bRet
= sal_False
;
268 if ( !rPropValue
.Name
.isEmpty() )
271 for ( i
= 0, nCount
= rPropSeq
.getLength(); i
< nCount
; i
++ )
273 if ( rPropSeq
[ i
].Name
== rPropValue
.Name
)
277 rPropSeq
.realloc( ++nCount
);
279 rPropSeq
[ i
] = rPropValue
;
286 sal_Bool
FilterConfigItem::ReadBool( const OUString
& rKey
, sal_Bool bDefault
)
289 sal_Bool bRetValue
= bDefault
;
290 PropertyValue
* pPropVal
= GetPropertyValue( aFilterData
, rKey
);
293 pPropVal
->Value
>>= bRetValue
;
295 else if ( ImplGetPropertyValue( aAny
, xPropSet
, rKey
, sal_True
) )
301 aBool
.Value
<<= bRetValue
;
302 WritePropertyValue( aFilterData
, aBool
);
306 sal_Int32
FilterConfigItem::ReadInt32( const OUString
& rKey
, sal_Int32 nDefault
)
309 sal_Int32 nRetValue
= nDefault
;
310 PropertyValue
* pPropVal
= GetPropertyValue( aFilterData
, rKey
);
313 pPropVal
->Value
>>= nRetValue
;
315 else if ( ImplGetPropertyValue( aAny
, xPropSet
, rKey
, sal_True
) )
319 PropertyValue aInt32
;
321 aInt32
.Value
<<= nRetValue
;
322 WritePropertyValue( aFilterData
, aInt32
);
326 OUString
FilterConfigItem::ReadString( const OUString
& rKey
, const OUString
& rDefault
)
329 OUString
aRetValue( rDefault
);
330 PropertyValue
* pPropVal
= GetPropertyValue( aFilterData
, rKey
);
333 pPropVal
->Value
>>= aRetValue
;
335 else if ( ImplGetPropertyValue( aAny
, xPropSet
, rKey
, sal_True
) )
339 PropertyValue aString
;
341 aString
.Value
<<= aRetValue
;
342 WritePropertyValue( aFilterData
, aString
);
346 void FilterConfigItem::WriteBool( const OUString
& rKey
, sal_Bool bNewValue
)
350 aBool
.Value
<<= bNewValue
;
351 WritePropertyValue( aFilterData
, aBool
);
356 if ( ImplGetPropertyValue( aAny
, xPropSet
, rKey
, sal_True
) )
358 sal_Bool
bOldValue(sal_True
);
359 if ( aAny
>>= bOldValue
)
361 if ( bOldValue
!= bNewValue
)
366 xPropSet
->setPropertyValue( rKey
, aAny
);
367 bModified
= sal_True
;
369 catch ( ::com::sun::star::uno::Exception
& )
371 OSL_FAIL( "FilterConfigItem::WriteBool - could not set PropertyValue" );
379 void FilterConfigItem::WriteInt32( const OUString
& rKey
, sal_Int32 nNewValue
)
381 PropertyValue aInt32
;
383 aInt32
.Value
<<= nNewValue
;
384 WritePropertyValue( aFilterData
, aInt32
);
390 if ( ImplGetPropertyValue( aAny
, xPropSet
, rKey
, sal_True
) )
392 sal_Int32 nOldValue
= 0;
393 if ( aAny
>>= nOldValue
)
395 if ( nOldValue
!= nNewValue
)
400 xPropSet
->setPropertyValue( rKey
, aAny
);
401 bModified
= sal_True
;
403 catch ( ::com::sun::star::uno::Exception
& )
405 OSL_FAIL( "FilterConfigItem::WriteInt32 - could not set PropertyValue" );
413 // ------------------------------------------------------------------------
415 Sequence
< PropertyValue
> FilterConfigItem::GetFilterData() const
420 // ------------------------------------------------------------------------
422 Reference
< XStatusIndicator
> FilterConfigItem::GetStatusIndicator() const
424 Reference
< XStatusIndicator
> xStatusIndicator
;
425 const rtl::OUString
sStatusIndicator( RTL_CONSTASCII_USTRINGPARAM( "StatusIndicator" ) );
427 sal_Int32 i
, nCount
= aFilterData
.getLength();
428 for ( i
= 0; i
< nCount
; i
++ )
430 if ( aFilterData
[ i
].Name
== sStatusIndicator
)
432 aFilterData
[ i
].Value
>>= xStatusIndicator
;
436 return xStatusIndicator
;
439 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */