bump product version to 6.4.0.3
[LibreOffice.git] / vcl / source / filter / FilterConfigItem.cxx
blob41c9ec4010641b78519fa659fe6b54f5b6738290
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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();
47 if ( bAvailable )
49 sal_Int32 nIdx{0};
50 if ( rTree[0] == '/' )
51 ++nIdx;
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;
62 try
64 xReadAccess = rXCfgProv->createInstanceWithArguments(
65 "com.sun.star.configuration.ConfigurationAccess",
66 aArguments );
68 catch (const css::uno::Exception&)
70 bAvailable = false;
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() )
81 bAvailable = false;
82 else
84 const OUString aNode( rTree.getToken(0, '/', nIdx) );
85 if ( !xHierarchicalNameAccess->hasByHierarchicalName( aNode ) )
86 bAvailable = false;
87 else
89 Any a( xHierarchicalNameAccess->getByHierarchicalName( aNode ) );
90 bAvailable = (a >>= xReadAccess);
96 return bAvailable;
99 void FilterConfigItem::ImpInitTree( const OUString& rSubTree )
101 bModified = false;
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",
122 aArguments );
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 )
139 : bModified(false)
141 if ( pFilterData )
142 aFilterData = *pFilterData;
145 FilterConfigItem::FilterConfigItem( const OUString& rSubTree,
146 css::uno::Sequence< css::beans::PropertyValue > const * pFilterData )
148 ImpInitTree( rSubTree );
150 if ( pFilterData )
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();
171 bModified = false;
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() )
188 bRetValue = false;
191 Reference< XPropertySetInfo >
192 aXPropSetInfo( rXPropSet->getPropertySetInfo() );
193 if ( aXPropSetInfo.is() )
194 bRetValue = aXPropSetInfo->hasPropertyByName( rString );
196 catch( css::uno::Exception& )
199 if ( bRetValue )
203 rAny = rXPropSet->getPropertyValue( rString );
204 if ( !rAny.hasValue() )
205 bRetValue = false;
207 catch( css::uno::Exception& )
209 bRetValue = false;
213 else
214 bRetValue = false;
215 return bRetValue;
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())
225 return pProp;
226 return nullptr;
229 /* if PropertySequence already includes a PropertyValue using the same name, the
230 corresponding PropertyValue is replaced, otherwise the given PropertyValue
231 will be appended */
233 bool FilterConfigItem::WritePropertyValue( Sequence< PropertyValue >& rPropSeq, const PropertyValue& rPropValue )
235 bool bRet = false;
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();
242 if ( i == nCount )
243 rPropSeq.realloc( ++nCount );
245 rPropSeq[ i ] = rPropValue;
247 bRet = true;
249 return bRet;
252 bool FilterConfigItem::ReadBool( const OUString& rKey, bool bDefault )
254 Any aAny;
255 bool bRetValue = bDefault;
256 PropertyValue* pPropVal = GetPropertyValue( aFilterData, rKey );
257 if ( pPropVal )
259 pPropVal->Value >>= bRetValue;
261 else if ( ImplGetPropertyValue( aAny, xPropSet, rKey ) )
263 aAny >>= bRetValue;
265 PropertyValue aBool;
266 aBool.Name = rKey;
267 aBool.Value <<= bRetValue;
268 WritePropertyValue( aFilterData, aBool );
269 return bRetValue;
272 sal_Int32 FilterConfigItem::ReadInt32( const OUString& rKey, sal_Int32 nDefault )
274 Any aAny;
275 sal_Int32 nRetValue = nDefault;
276 PropertyValue* pPropVal = GetPropertyValue( aFilterData, rKey );
277 if ( pPropVal )
279 pPropVal->Value >>= nRetValue;
281 else if ( ImplGetPropertyValue( aAny, xPropSet, rKey ) )
283 aAny >>= nRetValue;
285 PropertyValue aInt32;
286 aInt32.Name = rKey;
287 aInt32.Value <<= nRetValue;
288 WritePropertyValue( aFilterData, aInt32 );
289 return nRetValue;
292 OUString FilterConfigItem::ReadString( const OUString& rKey, const OUString& rDefault )
294 Any aAny;
295 OUString aRetValue( rDefault );
296 PropertyValue* pPropVal = GetPropertyValue( aFilterData, rKey );
297 if ( pPropVal )
299 pPropVal->Value >>= aRetValue;
301 else if ( ImplGetPropertyValue( aAny, xPropSet, rKey ) )
303 aAny >>= aRetValue;
305 PropertyValue aString;
306 aString.Name = rKey;
307 aString.Value <<= aRetValue;
308 WritePropertyValue( aFilterData, aString );
309 return aRetValue;
312 void FilterConfigItem::WriteBool( const OUString& rKey, bool bNewValue )
314 PropertyValue aBool;
315 aBool.Name = rKey;
316 aBool.Value <<= bNewValue;
317 WritePropertyValue( aFilterData, aBool );
319 if ( xPropSet.is() )
321 Any aAny;
322 if ( ImplGetPropertyValue( aAny, xPropSet, rKey ) )
324 bool bOldValue(true);
325 if ( aAny >>= bOldValue )
327 if ( bOldValue != bNewValue )
331 xPropSet->setPropertyValue( rKey, Any(bNewValue) );
332 bModified = true;
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;
347 aInt32.Name = rKey;
348 aInt32.Value <<= nNewValue;
349 WritePropertyValue( aFilterData, aInt32 );
351 if ( xPropSet.is() )
353 Any aAny;
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) );
365 bModified = true;
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: */