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/propertyvalue.hxx>
25 #include <o3tl/string_view.hxx>
26 #include <osl/diagnose.h>
27 #include <com/sun/star/beans/PropertyAttribute.hpp>
28 #include <com/sun/star/beans/PropertyValue.hpp>
29 #include <com/sun/star/configuration/theDefaultProvider.hpp>
30 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
31 #include <com/sun/star/util/XChangesBatch.hpp>
32 #include <com/sun/star/beans/XPropertySetInfo.hpp>
33 #include <com/sun/star/beans/XPropertySet.hpp>
34 #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
35 #include <com/sun/star/awt/Size.hpp>
36 #include <com/sun/star/task/XStatusIndicator.hpp>
38 using namespace ::com::sun::star::lang
; // XMultiServiceFactory
39 using namespace ::com::sun::star::beans
; // PropertyValue
40 using namespace ::com::sun::star::uno
; // Reference
41 using namespace ::com::sun::star::util
; // XChangesBatch
42 using namespace ::com::sun::star::container
;
43 using namespace ::com::sun::star::configuration
;
44 using namespace ::com::sun::star::task
; // XStatusIndicator
46 static bool ImpIsTreeAvailable( Reference
< XMultiServiceFactory
> const & rXCfgProv
, std::u16string_view rTree
)
48 bool bAvailable
= !rTree
.empty();
52 if ( rTree
[0] == '/' )
55 // creation arguments: nodepath
56 PropertyValue aPathArgument
= comphelper::makePropertyValue(u
"nodepath"_ustr
,
57 OUString(o3tl::getToken(rTree
, 0, '/', nIdx
)));
58 Sequence
< Any
> aArguments
{ Any(aPathArgument
) };
60 Reference
< XInterface
> xReadAccess
;
63 xReadAccess
= rXCfgProv
->createInstanceWithArguments(
64 u
"com.sun.star.configuration.ConfigurationAccess"_ustr
,
67 catch (const css::uno::Exception
&)
71 if ( xReadAccess
.is() )
73 const sal_Int32 nEnd
= rTree
.size();
74 while (bAvailable
&& nIdx
>=0 && nIdx
<nEnd
)
76 Reference
< XHierarchicalNameAccess
> xHierarchicalNameAccess
77 ( xReadAccess
, UNO_QUERY
);
79 if ( !xHierarchicalNameAccess
.is() )
83 const OUString
aNode( o3tl::getToken(rTree
, 0, '/', nIdx
) );
84 if ( !xHierarchicalNameAccess
->hasByHierarchicalName( aNode
) )
88 Any
a( xHierarchicalNameAccess
->getByHierarchicalName( aNode
) );
89 bAvailable
= (a
>>= xReadAccess
);
98 void FilterConfigItem::ImpInitTree( std::u16string_view rSubTree
)
102 const Reference
< XComponentContext
>& xContext( comphelper::getProcessComponentContext() );
104 Reference
< XMultiServiceFactory
> xCfgProv
= theDefaultProvider::get( xContext
);
106 OUString sTree
= OUString::Concat("/org.openoffice.") + rSubTree
;
107 if ( !ImpIsTreeAvailable(xCfgProv
, sTree
) )
110 // creation arguments: nodepath
111 PropertyValue aPathArgument
;
112 aPathArgument
.Name
= "nodepath";
113 aPathArgument
.Value
<<= sTree
;
115 Sequence
< Any
> aArguments
{ Any(aPathArgument
) };
119 xUpdatableView
= xCfgProv
->createInstanceWithArguments(
120 u
"com.sun.star.configuration.ConfigurationUpdateAccess"_ustr
,
122 if ( xUpdatableView
.is() )
123 xPropSet
.set( xUpdatableView
, UNO_QUERY
);
125 catch ( css::uno::Exception
& )
127 OSL_FAIL( "FilterConfigItem::FilterConfigItem - Could not access configuration Key" );
131 FilterConfigItem::FilterConfigItem( std::u16string_view rSubTree
)
133 ImpInitTree( rSubTree
);
136 FilterConfigItem::FilterConfigItem( css::uno::Sequence
< css::beans::PropertyValue
> const * pFilterData
)
140 aFilterData
= *pFilterData
;
143 FilterConfigItem::FilterConfigItem( std::u16string_view rSubTree
,
144 css::uno::Sequence
< css::beans::PropertyValue
> const * pFilterData
)
146 ImpInitTree( rSubTree
);
149 aFilterData
= *pFilterData
;
152 FilterConfigItem::~FilterConfigItem()
154 WriteModifiedConfig();
157 void FilterConfigItem::WriteModifiedConfig()
159 if ( !xUpdatableView
.is() )
162 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" );
180 bool FilterConfigItem::ImplGetPropertyValue( Any
& rAny
, const Reference
< XPropertySet
>& rXPropSet
, const OUString
& rString
)
182 bool bRetValue
= true;
184 if ( rXPropSet
.is() )
189 Reference
< XPropertySetInfo
>
190 aXPropSetInfo( rXPropSet
->getPropertySetInfo() );
191 if ( aXPropSetInfo
.is() )
192 bRetValue
= aXPropSetInfo
->hasPropertyByName( rString
);
194 catch( css::uno::Exception
& )
201 rAny
= rXPropSet
->getPropertyValue( rString
);
202 if ( !rAny
.hasValue() )
205 catch( css::uno::Exception
& )
216 // if property is available it returns a pointer,
217 // otherwise the result is null
218 const PropertyValue
* FilterConfigItem::GetPropertyValue( const Sequence
< PropertyValue
>& rPropSeq
, const OUString
& rName
)
220 auto pProp
= std::find_if(rPropSeq
.begin(), rPropSeq
.end(),
221 [&rName
](const PropertyValue
& rProp
) { return rProp
.Name
== rName
; });
222 if (pProp
!= rPropSeq
.end())
227 /* if PropertySequence already includes a PropertyValue using the same name, the
228 corresponding PropertyValue is replaced, otherwise the given PropertyValue
231 bool FilterConfigItem::WritePropertyValue( Sequence
< PropertyValue
>& rPropSeq
, const PropertyValue
& rPropValue
)
234 if ( !rPropValue
.Name
.isEmpty() )
236 auto pProp
= std::find_if(std::cbegin(rPropSeq
), std::cend(rPropSeq
),
237 [&rPropValue
](const PropertyValue
& rProp
) { return rProp
.Name
== rPropValue
.Name
; });
238 sal_Int32 i
= std::distance(std::cbegin(rPropSeq
), pProp
);
239 sal_Int32 nCount
= rPropSeq
.getLength();
241 rPropSeq
.realloc( ++nCount
);
243 rPropSeq
.getArray()[ i
] = rPropValue
;
250 bool FilterConfigItem::IsReadOnly(const OUString
& rName
)
255 const Reference
<XPropertySetInfo
> xInfo(xPropSet
->getPropertySetInfo());
256 if (!xInfo
.is() || !xInfo
->hasPropertyByName(rName
))
259 const css::beans::Property
aProp(xInfo
->getPropertyByName(rName
));
260 return (aProp
.Attributes
& PropertyAttribute::READONLY
);
263 bool FilterConfigItem::ReadBool( const OUString
& rKey
, bool bDefault
)
266 bool bRetValue
= bDefault
;
267 const PropertyValue
* pPropVal
= GetPropertyValue( aFilterData
, rKey
);
270 pPropVal
->Value
>>= bRetValue
;
272 else if ( ImplGetPropertyValue( aAny
, xPropSet
, rKey
) )
278 aBool
.Value
<<= bRetValue
;
279 WritePropertyValue( aFilterData
, aBool
);
283 sal_Int32
FilterConfigItem::ReadInt32( const OUString
& rKey
, sal_Int32 nDefault
)
286 sal_Int32 nRetValue
= nDefault
;
287 const PropertyValue
* pPropVal
= GetPropertyValue( aFilterData
, rKey
);
290 pPropVal
->Value
>>= nRetValue
;
292 else if ( ImplGetPropertyValue( aAny
, xPropSet
, rKey
) )
296 PropertyValue aInt32
;
298 aInt32
.Value
<<= nRetValue
;
299 WritePropertyValue( aFilterData
, aInt32
);
303 OUString
FilterConfigItem::ReadString( const OUString
& rKey
, const OUString
& rDefault
)
306 OUString
aRetValue( rDefault
);
307 const PropertyValue
* pPropVal
= GetPropertyValue( aFilterData
, rKey
);
310 pPropVal
->Value
>>= aRetValue
;
312 else if ( ImplGetPropertyValue( aAny
, xPropSet
, rKey
) )
316 PropertyValue aString
;
318 aString
.Value
<<= aRetValue
;
319 WritePropertyValue( aFilterData
, aString
);
323 void FilterConfigItem::WriteBool( const OUString
& rKey
, bool bNewValue
)
327 aBool
.Value
<<= bNewValue
;
328 WritePropertyValue( aFilterData
, aBool
);
330 if ( !xPropSet
.is() )
334 if ( !ImplGetPropertyValue( aAny
, xPropSet
, rKey
) )
337 bool bOldValue(true);
338 if ( !(aAny
>>= bOldValue
) )
341 if ( bOldValue
!= bNewValue
)
345 xPropSet
->setPropertyValue( rKey
, Any(bNewValue
) );
348 catch ( css::uno::Exception
& )
350 OSL_FAIL( "FilterConfigItem::WriteBool - could not set PropertyValue" );
355 void FilterConfigItem::WriteInt32( const OUString
& rKey
, sal_Int32 nNewValue
)
357 PropertyValue aInt32
;
359 aInt32
.Value
<<= nNewValue
;
360 WritePropertyValue( aFilterData
, aInt32
);
362 if ( !xPropSet
.is() )
367 if ( !ImplGetPropertyValue( aAny
, xPropSet
, rKey
) )
370 sal_Int32 nOldValue
= 0;
371 if ( !(aAny
>>= nOldValue
) )
374 if ( nOldValue
!= nNewValue
)
378 xPropSet
->setPropertyValue( rKey
, Any(nNewValue
) );
381 catch ( css::uno::Exception
& )
383 OSL_FAIL( "FilterConfigItem::WriteInt32 - could not set PropertyValue" );
389 Reference
< XStatusIndicator
> FilterConfigItem::GetStatusIndicator() const
391 Reference
< XStatusIndicator
> xStatusIndicator
;
393 auto pPropVal
= std::find_if(aFilterData
.begin(), aFilterData
.end(),
394 [](const css::beans::PropertyValue
& rPropVal
) {
395 return rPropVal
.Name
== "StatusIndicator"; });
396 if (pPropVal
!= aFilterData
.end())
398 pPropVal
->Value
>>= xStatusIndicator
;
400 return xStatusIndicator
;
403 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */