build fix: no comphelper/profilezone.hxx in this branch
[LibreOffice.git] / vcl / source / filter / FilterConfigItem.cxx
blob0d1f6b87e1ce2641e1fec91b15cbeacbf0d58fd5
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 <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();
45 if ( bAvailable )
47 using comphelper::string::getTokenCount;
49 sal_Int32 nTokenCount = getTokenCount(rTree, '/');
50 sal_Int32 i = 0;
52 if ( rTree[0] == '/' )
53 ++i;
54 if ( rTree.endsWith("/") )
55 --nTokenCount;
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;
66 try
68 xReadAccess = rXCfgProv->createInstanceWithArguments(
69 "com.sun.star.configuration.ConfigurationAccess",
70 aArguments );
72 catch (const css::uno::Exception&)
74 bAvailable = false;
76 if ( xReadAccess.is() )
78 for ( ; bAvailable && ( i < nTokenCount ); i++ )
80 Reference< XHierarchicalNameAccess > xHierarchicalNameAccess
81 ( xReadAccess, UNO_QUERY );
83 if ( !xHierarchicalNameAccess.is() )
84 bAvailable = false;
85 else
87 OUString aNode( rTree.getToken(i, '/') );
88 if ( !xHierarchicalNameAccess->hasByHierarchicalName( aNode ) )
89 bAvailable = false;
90 else
92 Any a( xHierarchicalNameAccess->getByHierarchicalName( aNode ) );
93 bAvailable = (a >>= xReadAccess);
99 return bAvailable;
102 void FilterConfigItem::ImpInitTree( const OUString& rSubTree )
104 bModified = false;
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",
132 aArguments );
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 )
149 : bModified(false)
151 if ( pFilterData )
152 aFilterData = *pFilterData;
155 FilterConfigItem::FilterConfigItem( const OUString& rSubTree,
156 css::uno::Sequence< css::beans::PropertyValue >* pFilterData )
158 ImpInitTree( rSubTree );
160 if ( pFilterData )
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();
181 bModified = false;
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 )
200 bRetValue = false;
203 Reference< XPropertySetInfo >
204 aXPropSetInfo( rXPropSet->getPropertySetInfo() );
205 if ( aXPropSetInfo.is() )
206 bRetValue = aXPropSetInfo->hasPropertyByName( rString );
208 catch( css::uno::Exception& )
213 if ( bRetValue )
217 rAny = rXPropSet->getPropertyValue( rString );
218 if ( !rAny.hasValue() )
219 bRetValue = false;
221 catch( css::uno::Exception& )
223 bRetValue = false;
227 else
228 bRetValue = false;
229 return bRetValue;
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;
238 sal_Int32 i, nCount;
239 for ( i = 0, nCount = rPropSeq.getLength(); i < nCount; i++ )
241 if ( rPropSeq[ i ].Name == rName )
243 pPropValue = &rPropSeq[ i ];
244 break;
247 return pPropValue;
250 /* if PropertySequence already includes a PropertyValue using the same name, the
251 corresponding PropertyValue is replaced, otherwise the given PropertyValue
252 will be appended */
254 bool FilterConfigItem::WritePropertyValue( Sequence< PropertyValue >& rPropSeq, const PropertyValue& rPropValue )
256 bool bRet = false;
257 if ( !rPropValue.Name.isEmpty() )
259 sal_Int32 i, nCount;
260 for ( i = 0, nCount = rPropSeq.getLength(); i < nCount; i++ )
262 if ( rPropSeq[ i ].Name == rPropValue.Name )
263 break;
265 if ( i == nCount )
266 rPropSeq.realloc( ++nCount );
268 rPropSeq[ i ] = rPropValue;
270 bRet = true;
272 return bRet;
275 bool FilterConfigItem::ReadBool( const OUString& rKey, bool bDefault )
277 Any aAny;
278 bool bRetValue = bDefault;
279 PropertyValue* pPropVal = GetPropertyValue( aFilterData, rKey );
280 if ( pPropVal )
282 pPropVal->Value >>= bRetValue;
284 else if ( ImplGetPropertyValue( aAny, xPropSet, rKey, true ) )
286 aAny >>= bRetValue;
288 PropertyValue aBool;
289 aBool.Name = rKey;
290 aBool.Value <<= bRetValue;
291 WritePropertyValue( aFilterData, aBool );
292 return bRetValue;
295 sal_Int32 FilterConfigItem::ReadInt32( const OUString& rKey, sal_Int32 nDefault )
297 Any aAny;
298 sal_Int32 nRetValue = nDefault;
299 PropertyValue* pPropVal = GetPropertyValue( aFilterData, rKey );
300 if ( pPropVal )
302 pPropVal->Value >>= nRetValue;
304 else if ( ImplGetPropertyValue( aAny, xPropSet, rKey, true ) )
306 aAny >>= nRetValue;
308 PropertyValue aInt32;
309 aInt32.Name = rKey;
310 aInt32.Value <<= nRetValue;
311 WritePropertyValue( aFilterData, aInt32 );
312 return nRetValue;
315 OUString FilterConfigItem::ReadString( const OUString& rKey, const OUString& rDefault )
317 Any aAny;
318 OUString aRetValue( rDefault );
319 PropertyValue* pPropVal = GetPropertyValue( aFilterData, rKey );
320 if ( pPropVal )
322 pPropVal->Value >>= aRetValue;
324 else if ( ImplGetPropertyValue( aAny, xPropSet, rKey, true ) )
326 aAny >>= aRetValue;
328 PropertyValue aString;
329 aString.Name = rKey;
330 aString.Value <<= aRetValue;
331 WritePropertyValue( aFilterData, aString );
332 return aRetValue;
335 void FilterConfigItem::WriteBool( const OUString& rKey, bool bNewValue )
337 PropertyValue aBool;
338 aBool.Name = rKey;
339 aBool.Value <<= bNewValue;
340 WritePropertyValue( aFilterData, aBool );
342 if ( xPropSet.is() )
344 Any aAny;
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) );
355 bModified = true;
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;
370 aInt32.Name = rKey;
371 aInt32.Value <<= nNewValue;
372 WritePropertyValue( aFilterData, aInt32 );
374 if ( xPropSet.is() )
376 Any aAny;
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) );
388 bModified = true;
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;
412 break;
415 return xStatusIndicator;
418 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */