update credits
[LibreOffice.git] / vcl / source / filter / FilterConfigItem.cxx
blob4f7b26d30dda4ab06e3e2469bccc17b5ed93496e
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 <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/container/XHierarchicalNameAccess.hpp>
32 using namespace ::com::sun::star::lang ; // XMultiServiceFactory
33 using namespace ::com::sun::star::beans ; // PropertyValue
34 using namespace ::com::sun::star::uno ; // Reference
35 using namespace ::com::sun::star::util ; // XChangesBatch
36 using namespace ::com::sun::star::awt ; // Size
37 using namespace ::com::sun::star::container ; //
38 using namespace ::com::sun::star::configuration; //
39 using namespace ::com::sun::star::task ; // XStatusIndicator
41 static sal_Bool ImpIsTreeAvailable( Reference< XMultiServiceFactory >& rXCfgProv, const OUString& rTree )
43 sal_Bool bAvailable = !rTree.isEmpty();
44 if ( bAvailable )
46 using comphelper::string::getTokenCount;
47 using comphelper::string::getToken;
49 sal_Int32 nTokenCount = getTokenCount(rTree, '/');
50 sal_Int32 i = 0;
52 if ( rTree[0] == '/' )
53 ++i;
54 if ( rTree[rTree.getLength() - 1] == '/' )
55 --nTokenCount;
57 Any aAny;
58 aAny <<= getToken(rTree, i++, '/');
60 // creation arguments: nodepath
61 PropertyValue aPathArgument;
62 aPathArgument.Name = OUString( "nodepath" );
63 aPathArgument.Value = aAny;
65 Sequence< Any > aArguments( 1 );
66 aArguments[ 0 ] <<= aPathArgument;
68 Reference< XInterface > xReadAccess;
69 try
71 xReadAccess = rXCfgProv->createInstanceWithArguments(
72 OUString( "com.sun.star.configuration.ConfigurationAccess" ),
73 aArguments );
75 catch (const ::com::sun::star::uno::Exception&)
77 bAvailable = sal_False;
79 if ( xReadAccess.is() )
81 for ( ; bAvailable && ( i < nTokenCount ); i++ )
83 Reference< XHierarchicalNameAccess > xHierarchicalNameAccess
84 ( xReadAccess, UNO_QUERY );
86 if ( !xHierarchicalNameAccess.is() )
87 bAvailable = sal_False;
88 else
90 OUString aNode( getToken(rTree, i, '/') );
91 if ( !xHierarchicalNameAccess->hasByHierarchicalName( aNode ) )
92 bAvailable = sal_False;
93 else
95 Any a( xHierarchicalNameAccess->getByHierarchicalName( aNode ) );
96 try
98 a >>= xReadAccess;
100 catch ( ::com::sun::star::uno::Exception& )
102 bAvailable = sal_False;
109 return bAvailable;
112 void FilterConfigItem::ImpInitTree( const String& rSubTree )
114 bModified = sal_False;
116 Reference< XComponentContext > xContext( comphelper::getProcessComponentContext() );
118 Reference< XMultiServiceFactory > xCfgProv = theDefaultProvider::get( xContext );
120 OUString sTree(OUString("/org.openoffice.") + rSubTree);
121 if ( ImpIsTreeAvailable(xCfgProv, sTree) )
123 Any aAny;
124 // creation arguments: nodepath
125 PropertyValue aPathArgument;
126 aAny <<= sTree;
127 aPathArgument.Name = OUString( "nodepath" );
128 aPathArgument.Value = aAny;
130 // creation arguments: commit mode
131 PropertyValue aModeArgument;
132 sal_Bool bAsyncron = sal_True;
133 aAny <<= bAsyncron;
134 aModeArgument.Name = OUString( "lazywrite" );
135 aModeArgument.Value = aAny;
137 Sequence< Any > aArguments( 2 );
138 aArguments[ 0 ] <<= aPathArgument;
139 aArguments[ 1 ] <<= aModeArgument;
143 xUpdatableView = xCfgProv->createInstanceWithArguments(
144 OUString( "com.sun.star.configuration.ConfigurationUpdateAccess" ),
145 aArguments );
146 if ( xUpdatableView.is() )
147 xPropSet = Reference< XPropertySet >( xUpdatableView, UNO_QUERY );
149 catch ( ::com::sun::star::uno::Exception& )
151 OSL_FAIL( "FilterConfigItem::FilterConfigItem - Could not access configuration Key" );
156 FilterConfigItem::FilterConfigItem( const OUString& rSubTree )
158 ImpInitTree( rSubTree );
161 FilterConfigItem::FilterConfigItem( ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >* pFilterData )
163 if ( pFilterData )
164 aFilterData = *pFilterData;
167 FilterConfigItem::FilterConfigItem( const OUString& rSubTree,
168 ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >* pFilterData )
170 ImpInitTree( rSubTree );
172 if ( pFilterData )
173 aFilterData = *pFilterData;
176 FilterConfigItem::~FilterConfigItem()
178 if ( xUpdatableView.is() )
180 if ( xPropSet.is() && bModified )
182 Reference< XChangesBatch > xUpdateControl( xUpdatableView, UNO_QUERY );
183 if ( xUpdateControl.is() )
187 xUpdateControl->commitChanges();
189 catch ( ::com::sun::star::uno::Exception& )
191 OSL_FAIL( "FilterConfigItem::FilterConfigItem - Could not update configuration data" );
198 sal_Bool FilterConfigItem::ImplGetPropertyValue( Any& rAny, const Reference< XPropertySet >& rXPropSet, const OUString& rString, sal_Bool bTestPropertyAvailability )
200 sal_Bool bRetValue = sal_True;
202 if ( rXPropSet.is() )
204 if ( bTestPropertyAvailability )
206 bRetValue = sal_False;
209 Reference< XPropertySetInfo >
210 aXPropSetInfo( rXPropSet->getPropertySetInfo() );
211 if ( aXPropSetInfo.is() )
212 bRetValue = aXPropSetInfo->hasPropertyByName( rString );
214 catch( ::com::sun::star::uno::Exception& )
219 if ( bRetValue )
223 rAny = rXPropSet->getPropertyValue( rString );
224 if ( !rAny.hasValue() )
225 bRetValue = sal_False;
227 catch( ::com::sun::star::uno::Exception& )
229 bRetValue = sal_False;
233 else
234 bRetValue = sal_False;
235 return bRetValue;
239 // if property is available it returns a pointer,
240 // otherwise the result is null
241 PropertyValue* FilterConfigItem::GetPropertyValue( Sequence< PropertyValue >& rPropSeq, const OUString& rName )
243 PropertyValue* pPropValue = NULL;
245 sal_Int32 i, nCount;
246 for ( i = 0, nCount = rPropSeq.getLength(); i < nCount; i++ )
248 if ( rPropSeq[ i ].Name == rName )
250 pPropValue = &rPropSeq[ i ];
251 break;
254 return pPropValue;
257 /* if PropertySequence already includes a PropertyValue using the same name, the
258 corresponding PropertyValue is replaced, otherwise the given PropertyValue
259 will be appended */
261 sal_Bool FilterConfigItem::WritePropertyValue( Sequence< PropertyValue >& rPropSeq, const PropertyValue& rPropValue )
263 sal_Bool bRet = sal_False;
264 if ( !rPropValue.Name.isEmpty() )
266 sal_Int32 i, nCount;
267 for ( i = 0, nCount = rPropSeq.getLength(); i < nCount; i++ )
269 if ( rPropSeq[ i ].Name == rPropValue.Name )
270 break;
272 if ( i == nCount )
273 rPropSeq.realloc( ++nCount );
275 rPropSeq[ i ] = rPropValue;
277 bRet = sal_True;
279 return bRet;
282 sal_Bool FilterConfigItem::ReadBool( const OUString& rKey, sal_Bool bDefault )
284 Any aAny;
285 sal_Bool bRetValue = bDefault;
286 PropertyValue* pPropVal = GetPropertyValue( aFilterData, rKey );
287 if ( pPropVal )
289 pPropVal->Value >>= bRetValue;
291 else if ( ImplGetPropertyValue( aAny, xPropSet, rKey, sal_True ) )
293 aAny >>= bRetValue;
295 PropertyValue aBool;
296 aBool.Name = rKey;
297 aBool.Value <<= bRetValue;
298 WritePropertyValue( aFilterData, aBool );
299 return bRetValue;
302 sal_Int32 FilterConfigItem::ReadInt32( const OUString& rKey, sal_Int32 nDefault )
304 Any aAny;
305 sal_Int32 nRetValue = nDefault;
306 PropertyValue* pPropVal = GetPropertyValue( aFilterData, rKey );
307 if ( pPropVal )
309 pPropVal->Value >>= nRetValue;
311 else if ( ImplGetPropertyValue( aAny, xPropSet, rKey, sal_True ) )
313 aAny >>= nRetValue;
315 PropertyValue aInt32;
316 aInt32.Name = rKey;
317 aInt32.Value <<= nRetValue;
318 WritePropertyValue( aFilterData, aInt32 );
319 return nRetValue;
322 OUString FilterConfigItem::ReadString( const OUString& rKey, const OUString& rDefault )
324 Any aAny;
325 OUString aRetValue( rDefault );
326 PropertyValue* pPropVal = GetPropertyValue( aFilterData, rKey );
327 if ( pPropVal )
329 pPropVal->Value >>= aRetValue;
331 else if ( ImplGetPropertyValue( aAny, xPropSet, rKey, sal_True ) )
333 aAny >>= aRetValue;
335 PropertyValue aString;
336 aString.Name = rKey;
337 aString.Value <<= aRetValue;
338 WritePropertyValue( aFilterData, aString );
339 return aRetValue;
342 void FilterConfigItem::WriteBool( const OUString& rKey, sal_Bool bNewValue )
344 PropertyValue aBool;
345 aBool.Name = rKey;
346 aBool.Value <<= bNewValue;
347 WritePropertyValue( aFilterData, aBool );
349 if ( xPropSet.is() )
351 Any aAny;
352 if ( ImplGetPropertyValue( aAny, xPropSet, rKey, sal_True ) )
354 sal_Bool bOldValue(sal_True);
355 if ( aAny >>= bOldValue )
357 if ( bOldValue != bNewValue )
359 aAny <<= bNewValue;
362 xPropSet->setPropertyValue( rKey, aAny );
363 bModified = sal_True;
365 catch ( ::com::sun::star::uno::Exception& )
367 OSL_FAIL( "FilterConfigItem::WriteBool - could not set PropertyValue" );
375 void FilterConfigItem::WriteInt32( const OUString& rKey, sal_Int32 nNewValue )
377 PropertyValue aInt32;
378 aInt32.Name = rKey;
379 aInt32.Value <<= nNewValue;
380 WritePropertyValue( aFilterData, aInt32 );
382 if ( xPropSet.is() )
384 Any aAny;
386 if ( ImplGetPropertyValue( aAny, xPropSet, rKey, sal_True ) )
388 sal_Int32 nOldValue = 0;
389 if ( aAny >>= nOldValue )
391 if ( nOldValue != nNewValue )
393 aAny <<= nNewValue;
396 xPropSet->setPropertyValue( rKey, aAny );
397 bModified = sal_True;
399 catch ( ::com::sun::star::uno::Exception& )
401 OSL_FAIL( "FilterConfigItem::WriteInt32 - could not set PropertyValue" );
409 // ------------------------------------------------------------------------
411 Sequence< PropertyValue > FilterConfigItem::GetFilterData() const
413 return aFilterData;
416 // ------------------------------------------------------------------------
418 Reference< XStatusIndicator > FilterConfigItem::GetStatusIndicator() const
420 Reference< XStatusIndicator > xStatusIndicator;
421 const OUString sStatusIndicator( "StatusIndicator" );
423 sal_Int32 i, nCount = aFilterData.getLength();
424 for ( i = 0; i < nCount; i++ )
426 if ( aFilterData[ i ].Name == sStatusIndicator )
428 aFilterData[ i ].Value >>= xStatusIndicator;
429 break;
432 return xStatusIndicator;
435 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */