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 <osl/diagnose.h>
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/beans/XPropertySet.hpp>
31 #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
32 #include <com/sun/star/awt/Size.hpp>
33 #include <com/sun/star/task/XStatusIndicator.hpp>
35 using namespace ::com::sun::star::lang
; // XMultiServiceFactory
36 using namespace ::com::sun::star::beans
; // PropertyValue
37 using namespace ::com::sun::star::uno
; // Reference
38 using namespace ::com::sun::star::util
; // XChangesBatch
39 using namespace ::com::sun::star::awt
; // Size
40 using namespace ::com::sun::star::container
;
41 using namespace ::com::sun::star::configuration
;
42 using namespace ::com::sun::star::task
; // XStatusIndicator
44 static bool ImpIsTreeAvailable( Reference
< XMultiServiceFactory
> const & rXCfgProv
, const OUString
& rTree
)
46 bool bAvailable
= !rTree
.isEmpty();
50 if ( rTree
[0] == '/' )
53 // creation arguments: nodepath
54 PropertyValue aPathArgument
;
55 aPathArgument
.Name
= "nodepath";
56 aPathArgument
.Value
<<= rTree
.getToken(0, '/', nIdx
);
58 Sequence
< Any
> aArguments( 1 );
59 aArguments
[ 0 ] <<= aPathArgument
;
61 Reference
< XInterface
> xReadAccess
;
64 xReadAccess
= rXCfgProv
->createInstanceWithArguments(
65 "com.sun.star.configuration.ConfigurationAccess",
68 catch (const css::uno::Exception
&)
72 if ( xReadAccess
.is() )
74 const sal_Int32 nEnd
{rTree
.getLength()};
75 while (bAvailable
&& nIdx
>=0 && nIdx
<nEnd
)
77 Reference
< XHierarchicalNameAccess
> xHierarchicalNameAccess
78 ( xReadAccess
, UNO_QUERY
);
80 if ( !xHierarchicalNameAccess
.is() )
84 const OUString
aNode( rTree
.getToken(0, '/', nIdx
) );
85 if ( !xHierarchicalNameAccess
->hasByHierarchicalName( aNode
) )
89 Any
a( xHierarchicalNameAccess
->getByHierarchicalName( aNode
) );
90 bAvailable
= (a
>>= xReadAccess
);
99 void FilterConfigItem::ImpInitTree( const OUString
& rSubTree
)
103 Reference
< XComponentContext
> xContext( comphelper::getProcessComponentContext() );
105 Reference
< XMultiServiceFactory
> xCfgProv
= theDefaultProvider::get( xContext
);
107 OUString sTree
= "/org.openoffice." + rSubTree
;
108 if ( ImpIsTreeAvailable(xCfgProv
, sTree
) )
110 // creation arguments: nodepath
111 PropertyValue aPathArgument
;
112 aPathArgument
.Name
= "nodepath";
113 aPathArgument
.Value
<<= sTree
;
115 Sequence
< Any
> aArguments( 1 );
116 aArguments
[ 0 ] <<= aPathArgument
;
120 xUpdatableView
= xCfgProv
->createInstanceWithArguments(
121 "com.sun.star.configuration.ConfigurationUpdateAccess",
123 if ( xUpdatableView
.is() )
124 xPropSet
.set( xUpdatableView
, UNO_QUERY
);
126 catch ( css::uno::Exception
& )
128 OSL_FAIL( "FilterConfigItem::FilterConfigItem - Could not access configuration Key" );
133 FilterConfigItem::FilterConfigItem( const OUString
& rSubTree
)
135 ImpInitTree( rSubTree
);
138 FilterConfigItem::FilterConfigItem( css::uno::Sequence
< css::beans::PropertyValue
> const * pFilterData
)
142 aFilterData
= *pFilterData
;
145 FilterConfigItem::FilterConfigItem( const OUString
& rSubTree
,
146 css::uno::Sequence
< css::beans::PropertyValue
> const * pFilterData
)
148 ImpInitTree( rSubTree
);
151 aFilterData
= *pFilterData
;
154 FilterConfigItem::~FilterConfigItem()
156 WriteModifiedConfig();
159 void FilterConfigItem::WriteModifiedConfig()
161 if ( xUpdatableView
.is() )
163 if ( xPropSet
.is() && bModified
)
165 Reference
< XChangesBatch
> xUpdateControl( xUpdatableView
, UNO_QUERY
);
166 if ( xUpdateControl
.is() )
170 xUpdateControl
->commitChanges();
173 catch ( css::uno::Exception
& )
175 OSL_FAIL( "FilterConfigItem::FilterConfigItem - Could not update configuration data" );
182 bool FilterConfigItem::ImplGetPropertyValue( Any
& rAny
, const Reference
< XPropertySet
>& rXPropSet
, const OUString
& rString
)
184 bool bRetValue
= true;
186 if ( rXPropSet
.is() )
191 Reference
< XPropertySetInfo
>
192 aXPropSetInfo( rXPropSet
->getPropertySetInfo() );
193 if ( aXPropSetInfo
.is() )
194 bRetValue
= aXPropSetInfo
->hasPropertyByName( rString
);
196 catch( css::uno::Exception
& )
203 rAny
= rXPropSet
->getPropertyValue( rString
);
204 if ( !rAny
.hasValue() )
207 catch( css::uno::Exception
& )
218 // if property is available it returns a pointer,
219 // otherwise the result is null
220 PropertyValue
* FilterConfigItem::GetPropertyValue( Sequence
< PropertyValue
>& rPropSeq
, const OUString
& rName
)
222 auto pProp
= std::find_if(rPropSeq
.begin(), rPropSeq
.end(),
223 [&rName
](const PropertyValue
& rProp
) { return rProp
.Name
== rName
; });
224 if (pProp
!= rPropSeq
.end())
229 /* if PropertySequence already includes a PropertyValue using the same name, the
230 corresponding PropertyValue is replaced, otherwise the given PropertyValue
233 bool FilterConfigItem::WritePropertyValue( Sequence
< PropertyValue
>& rPropSeq
, const PropertyValue
& rPropValue
)
236 if ( !rPropValue
.Name
.isEmpty() )
238 auto pProp
= std::find_if(rPropSeq
.begin(), rPropSeq
.end(),
239 [&rPropValue
](const PropertyValue
& rProp
) { return rProp
.Name
== rPropValue
.Name
; });
240 sal_Int32 i
= std::distance(rPropSeq
.begin(), pProp
);
241 sal_Int32 nCount
= rPropSeq
.getLength();
243 rPropSeq
.realloc( ++nCount
);
245 rPropSeq
[ i
] = rPropValue
;
252 bool FilterConfigItem::ReadBool( const OUString
& rKey
, bool bDefault
)
255 bool bRetValue
= bDefault
;
256 PropertyValue
* pPropVal
= GetPropertyValue( aFilterData
, rKey
);
259 pPropVal
->Value
>>= bRetValue
;
261 else if ( ImplGetPropertyValue( aAny
, xPropSet
, rKey
) )
267 aBool
.Value
<<= bRetValue
;
268 WritePropertyValue( aFilterData
, aBool
);
272 sal_Int32
FilterConfigItem::ReadInt32( const OUString
& rKey
, sal_Int32 nDefault
)
275 sal_Int32 nRetValue
= nDefault
;
276 PropertyValue
* pPropVal
= GetPropertyValue( aFilterData
, rKey
);
279 pPropVal
->Value
>>= nRetValue
;
281 else if ( ImplGetPropertyValue( aAny
, xPropSet
, rKey
) )
285 PropertyValue aInt32
;
287 aInt32
.Value
<<= nRetValue
;
288 WritePropertyValue( aFilterData
, aInt32
);
292 OUString
FilterConfigItem::ReadString( const OUString
& rKey
, const OUString
& rDefault
)
295 OUString
aRetValue( rDefault
);
296 PropertyValue
* pPropVal
= GetPropertyValue( aFilterData
, rKey
);
299 pPropVal
->Value
>>= aRetValue
;
301 else if ( ImplGetPropertyValue( aAny
, xPropSet
, rKey
) )
305 PropertyValue aString
;
307 aString
.Value
<<= aRetValue
;
308 WritePropertyValue( aFilterData
, aString
);
312 void FilterConfigItem::WriteBool( const OUString
& rKey
, bool bNewValue
)
316 aBool
.Value
<<= bNewValue
;
317 WritePropertyValue( aFilterData
, aBool
);
322 if ( ImplGetPropertyValue( aAny
, xPropSet
, rKey
) )
324 bool bOldValue(true);
325 if ( aAny
>>= bOldValue
)
327 if ( bOldValue
!= bNewValue
)
331 xPropSet
->setPropertyValue( rKey
, Any(bNewValue
) );
334 catch ( css::uno::Exception
& )
336 OSL_FAIL( "FilterConfigItem::WriteBool - could not set PropertyValue" );
344 void FilterConfigItem::WriteInt32( const OUString
& rKey
, sal_Int32 nNewValue
)
346 PropertyValue aInt32
;
348 aInt32
.Value
<<= nNewValue
;
349 WritePropertyValue( aFilterData
, aInt32
);
355 if ( ImplGetPropertyValue( aAny
, xPropSet
, rKey
) )
357 sal_Int32 nOldValue
= 0;
358 if ( aAny
>>= nOldValue
)
360 if ( nOldValue
!= nNewValue
)
364 xPropSet
->setPropertyValue( rKey
, Any(nNewValue
) );
367 catch ( css::uno::Exception
& )
369 OSL_FAIL( "FilterConfigItem::WriteInt32 - could not set PropertyValue" );
378 Reference
< XStatusIndicator
> FilterConfigItem::GetStatusIndicator() const
380 Reference
< XStatusIndicator
> xStatusIndicator
;
381 const OUString
sStatusIndicator( "StatusIndicator" );
383 auto pPropVal
= std::find_if(aFilterData
.begin(), aFilterData
.end(),
384 [&sStatusIndicator
](const css::beans::PropertyValue
& rPropVal
) {
385 return rPropVal
.Name
== sStatusIndicator
; });
386 if (pPropVal
!= aFilterData
.end())
388 pPropVal
->Value
>>= xStatusIndicator
;
390 return xStatusIndicator
;
393 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */