Branch libreoffice-5-0-4
[LibreOffice.git] / vcl / source / filter / FilterConfigItem.cxx
blob11948597144538082d99b64d1c7591d334f4a622
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 Any aAny;
58 aAny <<= rTree.getToken(i++, '/');
60 // creation arguments: nodepath
61 PropertyValue aPathArgument;
62 aPathArgument.Name = "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 = 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 = false;
88 else
90 OUString aNode( rTree.getToken(i, '/') );
91 if ( !xHierarchicalNameAccess->hasByHierarchicalName( aNode ) )
92 bAvailable = false;
93 else
95 Any a( xHierarchicalNameAccess->getByHierarchicalName( aNode ) );
96 bAvailable = (a >>= xReadAccess);
102 return bAvailable;
105 void FilterConfigItem::ImpInitTree( const OUString& rSubTree )
107 bModified = false;
109 Reference< XComponentContext > xContext( comphelper::getProcessComponentContext() );
111 Reference< XMultiServiceFactory > xCfgProv = theDefaultProvider::get( xContext );
113 OUString sTree = "/org.openoffice." + rSubTree;
114 if ( ImpIsTreeAvailable(xCfgProv, sTree) )
116 Any aAny;
117 // creation arguments: nodepath
118 PropertyValue aPathArgument;
119 aAny <<= sTree;
120 aPathArgument.Name = "nodepath";
121 aPathArgument.Value = aAny;
123 // creation arguments: commit mode
124 PropertyValue aModeArgument;
125 bool bAsynchron = true;
126 aAny <<= bAsynchron;
127 aModeArgument.Name = "lazywrite";
128 aModeArgument.Value = aAny;
130 Sequence< Any > aArguments( 2 );
131 aArguments[ 0 ] <<= aPathArgument;
132 aArguments[ 1 ] <<= aModeArgument;
136 xUpdatableView = xCfgProv->createInstanceWithArguments(
137 OUString( "com.sun.star.configuration.ConfigurationUpdateAccess" ),
138 aArguments );
139 if ( xUpdatableView.is() )
140 xPropSet = Reference< XPropertySet >( xUpdatableView, UNO_QUERY );
142 catch ( ::com::sun::star::uno::Exception& )
144 OSL_FAIL( "FilterConfigItem::FilterConfigItem - Could not access configuration Key" );
149 FilterConfigItem::FilterConfigItem( const OUString& rSubTree )
151 ImpInitTree( rSubTree );
154 FilterConfigItem::FilterConfigItem( ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >* pFilterData )
155 : bModified(false)
157 if ( pFilterData )
158 aFilterData = *pFilterData;
161 FilterConfigItem::FilterConfigItem( const OUString& rSubTree,
162 ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >* pFilterData )
164 ImpInitTree( rSubTree );
166 if ( pFilterData )
167 aFilterData = *pFilterData;
170 FilterConfigItem::~FilterConfigItem()
172 WriteModifiedConfig();
175 void FilterConfigItem::WriteModifiedConfig()
177 if ( xUpdatableView.is() )
179 if ( xPropSet.is() && bModified )
181 Reference< XChangesBatch > xUpdateControl( xUpdatableView, UNO_QUERY );
182 if ( xUpdateControl.is() )
186 xUpdateControl->commitChanges();
187 bModified = false;
189 catch ( ::com::sun::star::uno::Exception& )
191 OSL_FAIL( "FilterConfigItem::FilterConfigItem - Could not update configuration data" );
198 bool FilterConfigItem::ImplGetPropertyValue( Any& rAny, const Reference< XPropertySet >& rXPropSet, const OUString& rString, bool bTestPropertyAvailability )
200 bool bRetValue = true;
202 if ( rXPropSet.is() )
204 if ( bTestPropertyAvailability )
206 bRetValue = 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 = false;
227 catch( ::com::sun::star::uno::Exception& )
229 bRetValue = false;
233 else
234 bRetValue = false;
235 return bRetValue;
238 // if property is available it returns a pointer,
239 // otherwise the result is null
240 PropertyValue* FilterConfigItem::GetPropertyValue( Sequence< PropertyValue >& rPropSeq, const OUString& rName )
242 PropertyValue* pPropValue = NULL;
244 sal_Int32 i, nCount;
245 for ( i = 0, nCount = rPropSeq.getLength(); i < nCount; i++ )
247 if ( rPropSeq[ i ].Name == rName )
249 pPropValue = &rPropSeq[ i ];
250 break;
253 return pPropValue;
256 /* if PropertySequence already includes a PropertyValue using the same name, the
257 corresponding PropertyValue is replaced, otherwise the given PropertyValue
258 will be appended */
260 bool FilterConfigItem::WritePropertyValue( Sequence< PropertyValue >& rPropSeq, const PropertyValue& rPropValue )
262 bool bRet = false;
263 if ( !rPropValue.Name.isEmpty() )
265 sal_Int32 i, nCount;
266 for ( i = 0, nCount = rPropSeq.getLength(); i < nCount; i++ )
268 if ( rPropSeq[ i ].Name == rPropValue.Name )
269 break;
271 if ( i == nCount )
272 rPropSeq.realloc( ++nCount );
274 rPropSeq[ i ] = rPropValue;
276 bRet = true;
278 return bRet;
281 bool FilterConfigItem::ReadBool( const OUString& rKey, bool bDefault )
283 Any aAny;
284 bool bRetValue = bDefault;
285 PropertyValue* pPropVal = GetPropertyValue( aFilterData, rKey );
286 if ( pPropVal )
288 pPropVal->Value >>= bRetValue;
290 else if ( ImplGetPropertyValue( aAny, xPropSet, rKey, true ) )
292 aAny >>= bRetValue;
294 PropertyValue aBool;
295 aBool.Name = rKey;
296 aBool.Value <<= bRetValue;
297 WritePropertyValue( aFilterData, aBool );
298 return bRetValue;
301 sal_Int32 FilterConfigItem::ReadInt32( const OUString& rKey, sal_Int32 nDefault )
303 Any aAny;
304 sal_Int32 nRetValue = nDefault;
305 PropertyValue* pPropVal = GetPropertyValue( aFilterData, rKey );
306 if ( pPropVal )
308 pPropVal->Value >>= nRetValue;
310 else if ( ImplGetPropertyValue( aAny, xPropSet, rKey, true ) )
312 aAny >>= nRetValue;
314 PropertyValue aInt32;
315 aInt32.Name = rKey;
316 aInt32.Value <<= nRetValue;
317 WritePropertyValue( aFilterData, aInt32 );
318 return nRetValue;
321 OUString FilterConfigItem::ReadString( const OUString& rKey, const OUString& rDefault )
323 Any aAny;
324 OUString aRetValue( rDefault );
325 PropertyValue* pPropVal = GetPropertyValue( aFilterData, rKey );
326 if ( pPropVal )
328 pPropVal->Value >>= aRetValue;
330 else if ( ImplGetPropertyValue( aAny, xPropSet, rKey, true ) )
332 aAny >>= aRetValue;
334 PropertyValue aString;
335 aString.Name = rKey;
336 aString.Value <<= aRetValue;
337 WritePropertyValue( aFilterData, aString );
338 return aRetValue;
341 void FilterConfigItem::WriteBool( const OUString& rKey, bool bNewValue )
343 PropertyValue aBool;
344 aBool.Name = rKey;
345 aBool.Value <<= bNewValue;
346 WritePropertyValue( aFilterData, aBool );
348 if ( xPropSet.is() )
350 Any aAny;
351 if ( ImplGetPropertyValue( aAny, xPropSet, rKey, true ) )
353 bool bOldValue(true);
354 if ( aAny >>= bOldValue )
356 if ( bOldValue != bNewValue )
358 aAny <<= bNewValue;
361 xPropSet->setPropertyValue( rKey, aAny );
362 bModified = true;
364 catch ( ::com::sun::star::uno::Exception& )
366 OSL_FAIL( "FilterConfigItem::WriteBool - could not set PropertyValue" );
374 void FilterConfigItem::WriteInt32( const OUString& rKey, sal_Int32 nNewValue )
376 PropertyValue aInt32;
377 aInt32.Name = rKey;
378 aInt32.Value <<= nNewValue;
379 WritePropertyValue( aFilterData, aInt32 );
381 if ( xPropSet.is() )
383 Any aAny;
385 if ( ImplGetPropertyValue( aAny, xPropSet, rKey, true ) )
387 sal_Int32 nOldValue = 0;
388 if ( aAny >>= nOldValue )
390 if ( nOldValue != nNewValue )
392 aAny <<= nNewValue;
395 xPropSet->setPropertyValue( rKey, aAny );
396 bModified = true;
398 catch ( ::com::sun::star::uno::Exception& )
400 OSL_FAIL( "FilterConfigItem::WriteInt32 - could not set PropertyValue" );
409 Reference< XStatusIndicator > FilterConfigItem::GetStatusIndicator() const
411 Reference< XStatusIndicator > xStatusIndicator;
412 const OUString sStatusIndicator( "StatusIndicator" );
414 sal_Int32 i, nCount = aFilterData.getLength();
415 for ( i = 0; i < nCount; i++ )
417 if ( aFilterData[ i ].Name == sStatusIndicator )
419 aFilterData[ i ].Value >>= xStatusIndicator;
420 break;
423 return xStatusIndicator;
426 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */