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("/") )
57 // creation arguments: nodepath
58 PropertyValue aPathArgument
;
59 aPathArgument
.Name
= "nodepath";
60 aPathArgument
.Value
= Any(rTree
.getToken(i
++, '/'));
62 Sequence
< Any
> aArguments( 1 );
63 aArguments
[ 0 ] <<= aPathArgument
;
65 Reference
< XInterface
> xReadAccess
;
68 xReadAccess
= rXCfgProv
->createInstanceWithArguments(
69 "com.sun.star.configuration.ConfigurationAccess",
72 catch (const css::uno::Exception
&)
76 if ( xReadAccess
.is() )
78 for ( ; bAvailable
&& ( i
< nTokenCount
); i
++ )
80 Reference
< XHierarchicalNameAccess
> xHierarchicalNameAccess
81 ( xReadAccess
, UNO_QUERY
);
83 if ( !xHierarchicalNameAccess
.is() )
87 OUString
aNode( rTree
.getToken(i
, '/') );
88 if ( !xHierarchicalNameAccess
->hasByHierarchicalName( aNode
) )
92 Any
a( xHierarchicalNameAccess
->getByHierarchicalName( aNode
) );
93 bAvailable
= (a
>>= xReadAccess
);
102 void FilterConfigItem::ImpInitTree( const OUString
& rSubTree
)
106 Reference
< XComponentContext
> xContext( comphelper::getProcessComponentContext() );
108 Reference
< XMultiServiceFactory
> xCfgProv
= theDefaultProvider::get( xContext
);
110 OUString sTree
= "/org.openoffice." + rSubTree
;
111 if ( ImpIsTreeAvailable(xCfgProv
, sTree
) )
113 // creation arguments: nodepath
114 PropertyValue aPathArgument
;
115 aPathArgument
.Name
= "nodepath";
116 aPathArgument
.Value
= Any(sTree
);
118 // creation arguments: commit mode
119 PropertyValue aModeArgument
;
120 bool bAsynchron
= true;
121 aModeArgument
.Name
= "lazywrite";
122 aModeArgument
.Value
= Any(bAsynchron
);
124 Sequence
< Any
> aArguments( 2 );
125 aArguments
[ 0 ] <<= aPathArgument
;
126 aArguments
[ 1 ] <<= aModeArgument
;
130 xUpdatableView
= xCfgProv
->createInstanceWithArguments(
131 "com.sun.star.configuration.ConfigurationUpdateAccess",
133 if ( xUpdatableView
.is() )
134 xPropSet
.set( xUpdatableView
, UNO_QUERY
);
136 catch ( css::uno::Exception
& )
138 OSL_FAIL( "FilterConfigItem::FilterConfigItem - Could not access configuration Key" );
143 FilterConfigItem::FilterConfigItem( const OUString
& rSubTree
)
145 ImpInitTree( rSubTree
);
148 FilterConfigItem::FilterConfigItem( css::uno::Sequence
< css::beans::PropertyValue
>* pFilterData
)
152 aFilterData
= *pFilterData
;
155 FilterConfigItem::FilterConfigItem( const OUString
& rSubTree
,
156 css::uno::Sequence
< css::beans::PropertyValue
>* pFilterData
)
158 ImpInitTree( rSubTree
);
161 aFilterData
= *pFilterData
;
164 FilterConfigItem::~FilterConfigItem()
166 WriteModifiedConfig();
169 void FilterConfigItem::WriteModifiedConfig()
171 if ( xUpdatableView
.is() )
173 if ( xPropSet
.is() && bModified
)
175 Reference
< XChangesBatch
> xUpdateControl( xUpdatableView
, UNO_QUERY
);
176 if ( xUpdateControl
.is() )
180 xUpdateControl
->commitChanges();
183 catch ( css::uno::Exception
& )
185 OSL_FAIL( "FilterConfigItem::FilterConfigItem - Could not update configuration data" );
192 bool FilterConfigItem::ImplGetPropertyValue( Any
& rAny
, const Reference
< XPropertySet
>& rXPropSet
, const OUString
& rString
, bool bTestPropertyAvailability
)
194 bool bRetValue
= true;
196 if ( rXPropSet
.is() )
198 if ( bTestPropertyAvailability
)
203 Reference
< XPropertySetInfo
>
204 aXPropSetInfo( rXPropSet
->getPropertySetInfo() );
205 if ( aXPropSetInfo
.is() )
206 bRetValue
= aXPropSetInfo
->hasPropertyByName( rString
);
208 catch( css::uno::Exception
& )
217 rAny
= rXPropSet
->getPropertyValue( rString
);
218 if ( !rAny
.hasValue() )
221 catch( css::uno::Exception
& )
232 // if property is available it returns a pointer,
233 // otherwise the result is null
234 PropertyValue
* FilterConfigItem::GetPropertyValue( Sequence
< PropertyValue
>& rPropSeq
, const OUString
& rName
)
236 PropertyValue
* pPropValue
= nullptr;
239 for ( i
= 0, nCount
= rPropSeq
.getLength(); i
< nCount
; i
++ )
241 if ( rPropSeq
[ i
].Name
== rName
)
243 pPropValue
= &rPropSeq
[ i
];
250 /* if PropertySequence already includes a PropertyValue using the same name, the
251 corresponding PropertyValue is replaced, otherwise the given PropertyValue
254 bool FilterConfigItem::WritePropertyValue( Sequence
< PropertyValue
>& rPropSeq
, const PropertyValue
& rPropValue
)
257 if ( !rPropValue
.Name
.isEmpty() )
260 for ( i
= 0, nCount
= rPropSeq
.getLength(); i
< nCount
; i
++ )
262 if ( rPropSeq
[ i
].Name
== rPropValue
.Name
)
266 rPropSeq
.realloc( ++nCount
);
268 rPropSeq
[ i
] = rPropValue
;
275 bool FilterConfigItem::ReadBool( const OUString
& rKey
, bool bDefault
)
278 bool bRetValue
= bDefault
;
279 PropertyValue
* pPropVal
= GetPropertyValue( aFilterData
, rKey
);
282 pPropVal
->Value
>>= bRetValue
;
284 else if ( ImplGetPropertyValue( aAny
, xPropSet
, rKey
, true ) )
290 aBool
.Value
<<= bRetValue
;
291 WritePropertyValue( aFilterData
, aBool
);
295 sal_Int32
FilterConfigItem::ReadInt32( const OUString
& rKey
, sal_Int32 nDefault
)
298 sal_Int32 nRetValue
= nDefault
;
299 PropertyValue
* pPropVal
= GetPropertyValue( aFilterData
, rKey
);
302 pPropVal
->Value
>>= nRetValue
;
304 else if ( ImplGetPropertyValue( aAny
, xPropSet
, rKey
, true ) )
308 PropertyValue aInt32
;
310 aInt32
.Value
<<= nRetValue
;
311 WritePropertyValue( aFilterData
, aInt32
);
315 OUString
FilterConfigItem::ReadString( const OUString
& rKey
, const OUString
& rDefault
)
318 OUString
aRetValue( rDefault
);
319 PropertyValue
* pPropVal
= GetPropertyValue( aFilterData
, rKey
);
322 pPropVal
->Value
>>= aRetValue
;
324 else if ( ImplGetPropertyValue( aAny
, xPropSet
, rKey
, true ) )
328 PropertyValue aString
;
330 aString
.Value
<<= aRetValue
;
331 WritePropertyValue( aFilterData
, aString
);
335 void FilterConfigItem::WriteBool( const OUString
& rKey
, bool bNewValue
)
339 aBool
.Value
<<= bNewValue
;
340 WritePropertyValue( aFilterData
, aBool
);
345 if ( ImplGetPropertyValue( aAny
, xPropSet
, rKey
, true ) )
347 bool bOldValue(true);
348 if ( aAny
>>= bOldValue
)
350 if ( bOldValue
!= bNewValue
)
354 xPropSet
->setPropertyValue( rKey
, Any(bNewValue
) );
357 catch ( css::uno::Exception
& )
359 OSL_FAIL( "FilterConfigItem::WriteBool - could not set PropertyValue" );
367 void FilterConfigItem::WriteInt32( const OUString
& rKey
, sal_Int32 nNewValue
)
369 PropertyValue aInt32
;
371 aInt32
.Value
<<= nNewValue
;
372 WritePropertyValue( aFilterData
, aInt32
);
378 if ( ImplGetPropertyValue( aAny
, xPropSet
, rKey
, true ) )
380 sal_Int32 nOldValue
= 0;
381 if ( aAny
>>= nOldValue
)
383 if ( nOldValue
!= nNewValue
)
387 xPropSet
->setPropertyValue( rKey
, Any(nNewValue
) );
390 catch ( css::uno::Exception
& )
392 OSL_FAIL( "FilterConfigItem::WriteInt32 - could not set PropertyValue" );
401 Reference
< XStatusIndicator
> FilterConfigItem::GetStatusIndicator() const
403 Reference
< XStatusIndicator
> xStatusIndicator
;
404 const OUString
sStatusIndicator( "StatusIndicator" );
406 sal_Int32 i
, nCount
= aFilterData
.getLength();
407 for ( i
= 0; i
< nCount
; i
++ )
409 if ( aFilterData
[ i
].Name
== sStatusIndicator
)
411 aFilterData
[ i
].Value
>>= xStatusIndicator
;
415 return xStatusIndicator
;
418 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */