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 <osl/diagnose.h>
26 #include <com/sun/star/beans/PropertyValue.hpp>
27 #include <com/sun/star/configuration/theDefaultProvider.hpp>
28 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
29 #include <com/sun/star/util/XChangesBatch.hpp>
30 #include <com/sun/star/beans/XPropertySetInfo.hpp>
31 #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
33 using namespace ::com::sun::star::lang
; // XMultiServiceFactory
34 using namespace ::com::sun::star::beans
; // PropertyValue
35 using namespace ::com::sun::star::uno
; // Reference
36 using namespace ::com::sun::star::util
; // XChangesBatch
37 using namespace ::com::sun::star::awt
; // Size
38 using namespace ::com::sun::star::container
;
39 using namespace ::com::sun::star::configuration
;
40 using namespace ::com::sun::star::task
; // XStatusIndicator
42 static bool ImpIsTreeAvailable( Reference
< XMultiServiceFactory
>& rXCfgProv
, const OUString
& rTree
)
44 bool bAvailable
= !rTree
.isEmpty();
47 using comphelper::string::getTokenCount
;
49 sal_Int32 nTokenCount
= getTokenCount(rTree
, '/');
52 if ( rTree
[0] == '/' )
54 if ( rTree
.endsWith("/") )
58 aAny
<<= rTree
.getToken(i
++, '/');
60 // creation arguments: nodepath
61 PropertyValue aPathArgument
;
62 aPathArgument
.Name
= "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
&)
79 if ( xReadAccess
.is() )
81 for ( ; bAvailable
&& ( i
< nTokenCount
); i
++ )
83 Reference
< XHierarchicalNameAccess
> xHierarchicalNameAccess
84 ( xReadAccess
, UNO_QUERY
);
86 if ( !xHierarchicalNameAccess
.is() )
90 OUString
aNode( rTree
.getToken(i
, '/') );
91 if ( !xHierarchicalNameAccess
->hasByHierarchicalName( aNode
) )
95 Any
a( xHierarchicalNameAccess
->getByHierarchicalName( aNode
) );
96 bAvailable
= (a
>>= xReadAccess
);
105 void FilterConfigItem::ImpInitTree( const OUString
& rSubTree
)
109 Reference
< XComponentContext
> xContext( comphelper::getProcessComponentContext() );
111 Reference
< XMultiServiceFactory
> xCfgProv
= theDefaultProvider::get( xContext
);
113 OUString sTree
= "/org.openoffice." + rSubTree
;
114 if ( ImpIsTreeAvailable(xCfgProv
, sTree
) )
117 // creation arguments: nodepath
118 PropertyValue aPathArgument
;
120 aPathArgument
.Name
= "nodepath";
121 aPathArgument
.Value
= aAny
;
123 // creation arguments: commit mode
124 PropertyValue aModeArgument
;
125 bool bAsynchron
= true;
127 aModeArgument
.Name
= "lazywrite";
128 aModeArgument
.Value
= aAny
;
130 Sequence
< Any
> aArguments( 2 );
131 aArguments
[ 0 ] <<= aPathArgument
;
132 aArguments
[ 1 ] <<= aModeArgument
;
136 xUpdatableView
= xCfgProv
->createInstanceWithArguments(
137 OUString( "com.sun.star.configuration.ConfigurationUpdateAccess" ),
139 if ( xUpdatableView
.is() )
140 xPropSet
= Reference
< XPropertySet
>( xUpdatableView
, UNO_QUERY
);
142 catch ( ::com::sun::star::uno::Exception
& )
144 OSL_FAIL( "FilterConfigItem::FilterConfigItem - Could not access configuration Key" );
149 FilterConfigItem::FilterConfigItem( const OUString
& rSubTree
)
151 ImpInitTree( rSubTree
);
154 FilterConfigItem::FilterConfigItem( ::com::sun::star::uno::Sequence
< ::com::sun::star::beans::PropertyValue
>* pFilterData
)
158 aFilterData
= *pFilterData
;
161 FilterConfigItem::FilterConfigItem( const OUString
& rSubTree
,
162 ::com::sun::star::uno::Sequence
< ::com::sun::star::beans::PropertyValue
>* pFilterData
)
164 ImpInitTree( rSubTree
);
167 aFilterData
= *pFilterData
;
170 FilterConfigItem::~FilterConfigItem()
172 WriteModifiedConfig();
175 void FilterConfigItem::WriteModifiedConfig()
177 if ( xUpdatableView
.is() )
179 if ( xPropSet
.is() && bModified
)
181 Reference
< XChangesBatch
> xUpdateControl( xUpdatableView
, UNO_QUERY
);
182 if ( xUpdateControl
.is() )
186 xUpdateControl
->commitChanges();
189 catch ( ::com::sun::star::uno::Exception
& )
191 OSL_FAIL( "FilterConfigItem::FilterConfigItem - Could not update configuration data" );
198 bool FilterConfigItem::ImplGetPropertyValue( Any
& rAny
, const Reference
< XPropertySet
>& rXPropSet
, const OUString
& rString
, bool bTestPropertyAvailability
)
200 bool bRetValue
= true;
202 if ( rXPropSet
.is() )
204 if ( bTestPropertyAvailability
)
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() )
227 catch( ::com::sun::star::uno::Exception
& )
238 // if property is available it returns a pointer,
239 // otherwise the result is null
240 PropertyValue
* FilterConfigItem::GetPropertyValue( Sequence
< PropertyValue
>& rPropSeq
, const OUString
& rName
)
242 PropertyValue
* pPropValue
= NULL
;
245 for ( i
= 0, nCount
= rPropSeq
.getLength(); i
< nCount
; i
++ )
247 if ( rPropSeq
[ i
].Name
== rName
)
249 pPropValue
= &rPropSeq
[ i
];
256 /* if PropertySequence already includes a PropertyValue using the same name, the
257 corresponding PropertyValue is replaced, otherwise the given PropertyValue
260 bool FilterConfigItem::WritePropertyValue( Sequence
< PropertyValue
>& rPropSeq
, const PropertyValue
& rPropValue
)
263 if ( !rPropValue
.Name
.isEmpty() )
266 for ( i
= 0, nCount
= rPropSeq
.getLength(); i
< nCount
; i
++ )
268 if ( rPropSeq
[ i
].Name
== rPropValue
.Name
)
272 rPropSeq
.realloc( ++nCount
);
274 rPropSeq
[ i
] = rPropValue
;
281 bool FilterConfigItem::ReadBool( const OUString
& rKey
, bool bDefault
)
284 bool bRetValue
= bDefault
;
285 PropertyValue
* pPropVal
= GetPropertyValue( aFilterData
, rKey
);
288 pPropVal
->Value
>>= bRetValue
;
290 else if ( ImplGetPropertyValue( aAny
, xPropSet
, rKey
, true ) )
296 aBool
.Value
<<= bRetValue
;
297 WritePropertyValue( aFilterData
, aBool
);
301 sal_Int32
FilterConfigItem::ReadInt32( const OUString
& rKey
, sal_Int32 nDefault
)
304 sal_Int32 nRetValue
= nDefault
;
305 PropertyValue
* pPropVal
= GetPropertyValue( aFilterData
, rKey
);
308 pPropVal
->Value
>>= nRetValue
;
310 else if ( ImplGetPropertyValue( aAny
, xPropSet
, rKey
, true ) )
314 PropertyValue aInt32
;
316 aInt32
.Value
<<= nRetValue
;
317 WritePropertyValue( aFilterData
, aInt32
);
321 OUString
FilterConfigItem::ReadString( const OUString
& rKey
, const OUString
& rDefault
)
324 OUString
aRetValue( rDefault
);
325 PropertyValue
* pPropVal
= GetPropertyValue( aFilterData
, rKey
);
328 pPropVal
->Value
>>= aRetValue
;
330 else if ( ImplGetPropertyValue( aAny
, xPropSet
, rKey
, true ) )
334 PropertyValue aString
;
336 aString
.Value
<<= aRetValue
;
337 WritePropertyValue( aFilterData
, aString
);
341 void FilterConfigItem::WriteBool( const OUString
& rKey
, bool bNewValue
)
345 aBool
.Value
<<= bNewValue
;
346 WritePropertyValue( aFilterData
, aBool
);
351 if ( ImplGetPropertyValue( aAny
, xPropSet
, rKey
, true ) )
353 bool bOldValue(true);
354 if ( aAny
>>= bOldValue
)
356 if ( bOldValue
!= bNewValue
)
361 xPropSet
->setPropertyValue( rKey
, aAny
);
364 catch ( ::com::sun::star::uno::Exception
& )
366 OSL_FAIL( "FilterConfigItem::WriteBool - could not set PropertyValue" );
374 void FilterConfigItem::WriteInt32( const OUString
& rKey
, sal_Int32 nNewValue
)
376 PropertyValue aInt32
;
378 aInt32
.Value
<<= nNewValue
;
379 WritePropertyValue( aFilterData
, aInt32
);
385 if ( ImplGetPropertyValue( aAny
, xPropSet
, rKey
, true ) )
387 sal_Int32 nOldValue
= 0;
388 if ( aAny
>>= nOldValue
)
390 if ( nOldValue
!= nNewValue
)
395 xPropSet
->setPropertyValue( rKey
, aAny
);
398 catch ( ::com::sun::star::uno::Exception
& )
400 OSL_FAIL( "FilterConfigItem::WriteInt32 - could not set PropertyValue" );
409 Reference
< XStatusIndicator
> FilterConfigItem::GetStatusIndicator() const
411 Reference
< XStatusIndicator
> xStatusIndicator
;
412 const OUString
sStatusIndicator( "StatusIndicator" );
414 sal_Int32 i
, nCount
= aFilterData
.getLength();
415 for ( i
= 0; i
< nCount
; i
++ )
417 if ( aFilterData
[ i
].Name
== sStatusIndicator
)
419 aFilterData
[ i
].Value
>>= xStatusIndicator
;
423 return xStatusIndicator
;
426 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */