Version 4.0.0.1, tag libreoffice-4.0.0.1
[LibreOffice.git] / vcl / source / filter / FilterConfigItem.cxx
blobbd7d7c5985c05cc5872d83532ed37fa6f78188ad
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 ::rtl::OUString;
33 using namespace ::utl ; // getProcessServiceFactory
34 using namespace ::com::sun::star::lang ; // XMultiServiceFactory
35 using namespace ::com::sun::star::beans ; // PropertyValue
36 using namespace ::com::sun::star::uno ; // Reference
37 using namespace ::com::sun::star::util ; // XChangesBatch
38 using namespace ::com::sun::star::awt ; // Size
39 using namespace ::com::sun::star::container ; //
40 using namespace ::com::sun::star::configuration; //
41 using namespace ::com::sun::star::task ; // XStatusIndicator
43 static sal_Bool ImpIsTreeAvailable( Reference< XMultiServiceFactory >& rXCfgProv, const rtl::OUString& rTree )
45 sal_Bool bAvailable = !rTree.isEmpty();
46 if ( bAvailable )
48 using comphelper::string::getTokenCount;
49 using comphelper::string::getToken;
51 sal_Int32 nTokenCount = getTokenCount(rTree, '/');
52 sal_Int32 i = 0;
54 if ( rTree[0] == '/' )
55 ++i;
56 if ( rTree[rTree.getLength() - 1] == '/' )
57 --nTokenCount;
59 Any aAny;
60 aAny <<= getToken(rTree, i++, '/');
62 // creation arguments: nodepath
63 PropertyValue aPathArgument;
64 aPathArgument.Name = OUString( RTL_CONSTASCII_USTRINGPARAM( "nodepath" ) );
65 aPathArgument.Value = aAny;
67 Sequence< Any > aArguments( 1 );
68 aArguments[ 0 ] <<= aPathArgument;
70 Reference< XInterface > xReadAccess;
71 try
73 xReadAccess = rXCfgProv->createInstanceWithArguments(
74 OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.configuration.ConfigurationAccess" ) ),
75 aArguments );
77 catch (const ::com::sun::star::uno::Exception&)
79 bAvailable = sal_False;
81 if ( xReadAccess.is() )
83 for ( ; bAvailable && ( i < nTokenCount ); i++ )
85 Reference< XHierarchicalNameAccess > xHierarchicalNameAccess
86 ( xReadAccess, UNO_QUERY );
88 if ( !xHierarchicalNameAccess.is() )
89 bAvailable = sal_False;
90 else
92 rtl::OUString aNode( getToken(rTree, i, '/') );
93 if ( !xHierarchicalNameAccess->hasByHierarchicalName( aNode ) )
94 bAvailable = sal_False;
95 else
97 Any a( xHierarchicalNameAccess->getByHierarchicalName( aNode ) );
98 try
100 a >>= xReadAccess;
102 catch ( ::com::sun::star::uno::Exception& )
104 bAvailable = sal_False;
111 return bAvailable;
114 void FilterConfigItem::ImpInitTree( const String& rSubTree )
116 bModified = sal_False;
118 Reference< XComponentContext > xContext( comphelper::getProcessComponentContext() );
120 Reference< XMultiServiceFactory > xCfgProv = theDefaultProvider::get( xContext );
122 OUString sTree(
123 OUString(RTL_CONSTASCII_USTRINGPARAM("/org.openoffice.")) +
124 rSubTree);
125 if ( ImpIsTreeAvailable(xCfgProv, sTree) )
127 Any aAny;
128 // creation arguments: nodepath
129 PropertyValue aPathArgument;
130 aAny <<= sTree;
131 aPathArgument.Name = OUString( RTL_CONSTASCII_USTRINGPARAM( "nodepath" ) );
132 aPathArgument.Value = aAny;
134 // creation arguments: commit mode
135 PropertyValue aModeArgument;
136 sal_Bool bAsyncron = sal_True;
137 aAny <<= bAsyncron;
138 aModeArgument.Name = OUString( RTL_CONSTASCII_USTRINGPARAM( "lazywrite" ) );
139 aModeArgument.Value = aAny;
141 Sequence< Any > aArguments( 2 );
142 aArguments[ 0 ] <<= aPathArgument;
143 aArguments[ 1 ] <<= aModeArgument;
147 xUpdatableView = xCfgProv->createInstanceWithArguments(
148 OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.configuration.ConfigurationUpdateAccess" ) ),
149 aArguments );
150 if ( xUpdatableView.is() )
151 xPropSet = Reference< XPropertySet >( xUpdatableView, UNO_QUERY );
153 catch ( ::com::sun::star::uno::Exception& )
155 OSL_FAIL( "FilterConfigItem::FilterConfigItem - Could not access configuration Key" );
160 FilterConfigItem::FilterConfigItem( const OUString& rSubTree )
162 ImpInitTree( rSubTree );
165 FilterConfigItem::FilterConfigItem( ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >* pFilterData )
167 if ( pFilterData )
168 aFilterData = *pFilterData;
171 FilterConfigItem::FilterConfigItem( const OUString& rSubTree,
172 ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >* pFilterData )
174 ImpInitTree( rSubTree );
176 if ( pFilterData )
177 aFilterData = *pFilterData;
180 FilterConfigItem::~FilterConfigItem()
182 if ( xUpdatableView.is() )
184 if ( xPropSet.is() && bModified )
186 Reference< XChangesBatch > xUpdateControl( xUpdatableView, UNO_QUERY );
187 if ( xUpdateControl.is() )
191 xUpdateControl->commitChanges();
193 catch ( ::com::sun::star::uno::Exception& )
195 OSL_FAIL( "FilterConfigItem::FilterConfigItem - Could not update configuration data" );
202 sal_Bool FilterConfigItem::ImplGetPropertyValue( Any& rAny, const Reference< XPropertySet >& rXPropSet, const OUString& rString, sal_Bool bTestPropertyAvailability )
204 sal_Bool bRetValue = sal_True;
206 if ( rXPropSet.is() )
208 if ( bTestPropertyAvailability )
210 bRetValue = sal_False;
213 Reference< XPropertySetInfo >
214 aXPropSetInfo( rXPropSet->getPropertySetInfo() );
215 if ( aXPropSetInfo.is() )
216 bRetValue = aXPropSetInfo->hasPropertyByName( rString );
218 catch( ::com::sun::star::uno::Exception& )
223 if ( bRetValue )
227 rAny = rXPropSet->getPropertyValue( rString );
228 if ( !rAny.hasValue() )
229 bRetValue = sal_False;
231 catch( ::com::sun::star::uno::Exception& )
233 bRetValue = sal_False;
237 else
238 bRetValue = sal_False;
239 return bRetValue;
243 // if property is available it returns a pointer,
244 // otherwise the result is null
245 PropertyValue* FilterConfigItem::GetPropertyValue( Sequence< PropertyValue >& rPropSeq, const OUString& rName )
247 PropertyValue* pPropValue = NULL;
249 sal_Int32 i, nCount;
250 for ( i = 0, nCount = rPropSeq.getLength(); i < nCount; i++ )
252 if ( rPropSeq[ i ].Name == rName )
254 pPropValue = &rPropSeq[ i ];
255 break;
258 return pPropValue;
261 /* if PropertySequence already includes a PropertyValue using the same name, the
262 corresponding PropertyValue is replaced, otherwise the given PropertyValue
263 will be appended */
265 sal_Bool FilterConfigItem::WritePropertyValue( Sequence< PropertyValue >& rPropSeq, const PropertyValue& rPropValue )
267 sal_Bool bRet = sal_False;
268 if ( !rPropValue.Name.isEmpty() )
270 sal_Int32 i, nCount;
271 for ( i = 0, nCount = rPropSeq.getLength(); i < nCount; i++ )
273 if ( rPropSeq[ i ].Name == rPropValue.Name )
274 break;
276 if ( i == nCount )
277 rPropSeq.realloc( ++nCount );
279 rPropSeq[ i ] = rPropValue;
281 bRet = sal_True;
283 return bRet;
286 sal_Bool FilterConfigItem::ReadBool( const OUString& rKey, sal_Bool bDefault )
288 Any aAny;
289 sal_Bool bRetValue = bDefault;
290 PropertyValue* pPropVal = GetPropertyValue( aFilterData, rKey );
291 if ( pPropVal )
293 pPropVal->Value >>= bRetValue;
295 else if ( ImplGetPropertyValue( aAny, xPropSet, rKey, sal_True ) )
297 aAny >>= bRetValue;
299 PropertyValue aBool;
300 aBool.Name = rKey;
301 aBool.Value <<= bRetValue;
302 WritePropertyValue( aFilterData, aBool );
303 return bRetValue;
306 sal_Int32 FilterConfigItem::ReadInt32( const OUString& rKey, sal_Int32 nDefault )
308 Any aAny;
309 sal_Int32 nRetValue = nDefault;
310 PropertyValue* pPropVal = GetPropertyValue( aFilterData, rKey );
311 if ( pPropVal )
313 pPropVal->Value >>= nRetValue;
315 else if ( ImplGetPropertyValue( aAny, xPropSet, rKey, sal_True ) )
317 aAny >>= nRetValue;
319 PropertyValue aInt32;
320 aInt32.Name = rKey;
321 aInt32.Value <<= nRetValue;
322 WritePropertyValue( aFilterData, aInt32 );
323 return nRetValue;
326 OUString FilterConfigItem::ReadString( const OUString& rKey, const OUString& rDefault )
328 Any aAny;
329 OUString aRetValue( rDefault );
330 PropertyValue* pPropVal = GetPropertyValue( aFilterData, rKey );
331 if ( pPropVal )
333 pPropVal->Value >>= aRetValue;
335 else if ( ImplGetPropertyValue( aAny, xPropSet, rKey, sal_True ) )
337 aAny >>= aRetValue;
339 PropertyValue aString;
340 aString.Name = rKey;
341 aString.Value <<= aRetValue;
342 WritePropertyValue( aFilterData, aString );
343 return aRetValue;
346 void FilterConfigItem::WriteBool( const OUString& rKey, sal_Bool bNewValue )
348 PropertyValue aBool;
349 aBool.Name = rKey;
350 aBool.Value <<= bNewValue;
351 WritePropertyValue( aFilterData, aBool );
353 if ( xPropSet.is() )
355 Any aAny;
356 if ( ImplGetPropertyValue( aAny, xPropSet, rKey, sal_True ) )
358 sal_Bool bOldValue(sal_True);
359 if ( aAny >>= bOldValue )
361 if ( bOldValue != bNewValue )
363 aAny <<= bNewValue;
366 xPropSet->setPropertyValue( rKey, aAny );
367 bModified = sal_True;
369 catch ( ::com::sun::star::uno::Exception& )
371 OSL_FAIL( "FilterConfigItem::WriteBool - could not set PropertyValue" );
379 void FilterConfigItem::WriteInt32( const OUString& rKey, sal_Int32 nNewValue )
381 PropertyValue aInt32;
382 aInt32.Name = rKey;
383 aInt32.Value <<= nNewValue;
384 WritePropertyValue( aFilterData, aInt32 );
386 if ( xPropSet.is() )
388 Any aAny;
390 if ( ImplGetPropertyValue( aAny, xPropSet, rKey, sal_True ) )
392 sal_Int32 nOldValue = 0;
393 if ( aAny >>= nOldValue )
395 if ( nOldValue != nNewValue )
397 aAny <<= nNewValue;
400 xPropSet->setPropertyValue( rKey, aAny );
401 bModified = sal_True;
403 catch ( ::com::sun::star::uno::Exception& )
405 OSL_FAIL( "FilterConfigItem::WriteInt32 - could not set PropertyValue" );
413 // ------------------------------------------------------------------------
415 Sequence< PropertyValue > FilterConfigItem::GetFilterData() const
417 return aFilterData;
420 // ------------------------------------------------------------------------
422 Reference< XStatusIndicator > FilterConfigItem::GetStatusIndicator() const
424 Reference< XStatusIndicator > xStatusIndicator;
425 const rtl::OUString sStatusIndicator( RTL_CONSTASCII_USTRINGPARAM( "StatusIndicator" ) );
427 sal_Int32 i, nCount = aFilterData.getLength();
428 for ( i = 0; i < nCount; i++ )
430 if ( aFilterData[ i ].Name == sStatusIndicator )
432 aFilterData[ i ].Value >>= xStatusIndicator;
433 break;
436 return xStatusIndicator;
439 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */