Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <svtools/FilterConfigItem.hxx>
21 :
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>
31 :
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
42 :
43 0 : static sal_Bool ImpIsTreeAvailable( Reference< XMultiServiceFactory >& rXCfgProv, const rtl::OUString& rTree )
44 : {
45 0 : sal_Bool bAvailable = !rTree.isEmpty();
46 0 : if ( bAvailable )
47 : {
48 : using comphelper::string::getTokenCount;
49 : using comphelper::string::getToken;
50 :
51 0 : sal_Int32 nTokenCount = getTokenCount(rTree, '/');
52 0 : sal_Int32 i = 0;
53 :
54 0 : if ( rTree[0] == '/' )
55 0 : ++i;
56 0 : if ( rTree[rTree.getLength() - 1] == '/' )
57 0 : --nTokenCount;
58 :
59 0 : Any aAny;
60 0 : aAny <<= getToken(rTree, i++, '/');
61 :
62 : // creation arguments: nodepath
63 0 : PropertyValue aPathArgument;
64 0 : aPathArgument.Name = OUString( RTL_CONSTASCII_USTRINGPARAM( "nodepath" ) );
65 0 : aPathArgument.Value = aAny;
66 :
67 0 : Sequence< Any > aArguments( 1 );
68 0 : aArguments[ 0 ] <<= aPathArgument;
69 :
70 0 : Reference< XInterface > xReadAccess;
71 : try
72 : {
73 0 : xReadAccess = rXCfgProv->createInstanceWithArguments(
74 : OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.configuration.ConfigurationAccess" ) ),
75 0 : aArguments );
76 : }
77 0 : catch (const ::com::sun::star::uno::Exception&)
78 : {
79 0 : bAvailable = sal_False;
80 : }
81 0 : if ( xReadAccess.is() )
82 : {
83 0 : for ( ; bAvailable && ( i < nTokenCount ); i++ )
84 : {
85 : Reference< XHierarchicalNameAccess > xHierarchicalNameAccess
86 0 : ( xReadAccess, UNO_QUERY );
87 :
88 0 : if ( !xHierarchicalNameAccess.is() )
89 0 : bAvailable = sal_False;
90 : else
91 : {
92 0 : rtl::OUString aNode( getToken(rTree, i, '/') );
93 0 : if ( !xHierarchicalNameAccess->hasByHierarchicalName( aNode ) )
94 0 : bAvailable = sal_False;
95 : else
96 : {
97 0 : Any a( xHierarchicalNameAccess->getByHierarchicalName( aNode ) );
98 : try
99 : {
100 0 : a >>= xReadAccess;
101 : }
102 0 : catch ( ::com::sun::star::uno::Exception& )
103 : {
104 0 : bAvailable = sal_False;
105 0 : }
106 0 : }
107 : }
108 0 : }
109 0 : }
110 : }
111 0 : return bAvailable;
112 : }
113 :
114 0 : void FilterConfigItem::ImpInitTree( const String& rSubTree )
115 : {
116 0 : bModified = sal_False;
117 :
118 0 : Reference< XComponentContext > xContext( comphelper::getProcessComponentContext() );
119 :
120 0 : Reference< XMultiServiceFactory > xCfgProv = theDefaultProvider::get( xContext );
121 :
122 : OUString sTree(
123 : OUString(RTL_CONSTASCII_USTRINGPARAM("/org.openoffice.")) +
124 0 : rSubTree);
125 0 : if ( ImpIsTreeAvailable(xCfgProv, sTree) )
126 : {
127 0 : Any aAny;
128 : // creation arguments: nodepath
129 0 : PropertyValue aPathArgument;
130 0 : aAny <<= sTree;
131 0 : aPathArgument.Name = OUString( RTL_CONSTASCII_USTRINGPARAM( "nodepath" ) );
132 0 : aPathArgument.Value = aAny;
133 :
134 : // creation arguments: commit mode
135 0 : PropertyValue aModeArgument;
136 0 : sal_Bool bAsyncron = sal_True;
137 0 : aAny <<= bAsyncron;
138 0 : aModeArgument.Name = OUString( RTL_CONSTASCII_USTRINGPARAM( "lazywrite" ) );
139 0 : aModeArgument.Value = aAny;
140 :
141 0 : Sequence< Any > aArguments( 2 );
142 0 : aArguments[ 0 ] <<= aPathArgument;
143 0 : aArguments[ 1 ] <<= aModeArgument;
144 :
145 : try
146 : {
147 0 : xUpdatableView = xCfgProv->createInstanceWithArguments(
148 : OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.configuration.ConfigurationUpdateAccess" ) ),
149 0 : aArguments );
150 0 : if ( xUpdatableView.is() )
151 0 : xPropSet = Reference< XPropertySet >( xUpdatableView, UNO_QUERY );
152 : }
153 0 : catch ( ::com::sun::star::uno::Exception& )
154 : {
155 : OSL_FAIL( "FilterConfigItem::FilterConfigItem - Could not access configuration Key" );
156 0 : }
157 0 : }
158 0 : }
159 :
160 0 : FilterConfigItem::FilterConfigItem( const OUString& rSubTree )
161 : {
162 0 : ImpInitTree( rSubTree );
163 0 : }
164 :
165 62 : FilterConfigItem::FilterConfigItem( ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >* pFilterData )
166 : {
167 62 : if ( pFilterData )
168 0 : aFilterData = *pFilterData;
169 62 : }
170 :
171 0 : FilterConfigItem::FilterConfigItem( const OUString& rSubTree,
172 0 : ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >* pFilterData )
173 : {
174 0 : ImpInitTree( rSubTree );
175 :
176 0 : if ( pFilterData )
177 0 : aFilterData = *pFilterData;
178 0 : };
179 :
180 124 : FilterConfigItem::~FilterConfigItem()
181 : {
182 62 : if ( xUpdatableView.is() )
183 : {
184 0 : if ( xPropSet.is() && bModified )
185 : {
186 0 : Reference< XChangesBatch > xUpdateControl( xUpdatableView, UNO_QUERY );
187 0 : if ( xUpdateControl.is() )
188 : {
189 : try
190 : {
191 0 : xUpdateControl->commitChanges();
192 : }
193 0 : catch ( ::com::sun::star::uno::Exception& )
194 : {
195 : OSL_FAIL( "FilterConfigItem::FilterConfigItem - Could not update configuration data" );
196 : }
197 0 : }
198 : }
199 : }
200 62 : }
201 :
202 189 : sal_Bool FilterConfigItem::ImplGetPropertyValue( Any& rAny, const Reference< XPropertySet >& rXPropSet, const OUString& rString, sal_Bool bTestPropertyAvailability )
203 : {
204 189 : sal_Bool bRetValue = sal_True;
205 :
206 189 : if ( rXPropSet.is() )
207 : {
208 0 : if ( bTestPropertyAvailability )
209 : {
210 0 : bRetValue = sal_False;
211 : try
212 : {
213 : Reference< XPropertySetInfo >
214 0 : aXPropSetInfo( rXPropSet->getPropertySetInfo() );
215 0 : if ( aXPropSetInfo.is() )
216 0 : bRetValue = aXPropSetInfo->hasPropertyByName( rString );
217 : }
218 0 : catch( ::com::sun::star::uno::Exception& )
219 : {
220 : //
221 : }
222 : }
223 0 : if ( bRetValue )
224 : {
225 : try
226 : {
227 0 : rAny = rXPropSet->getPropertyValue( rString );
228 0 : if ( !rAny.hasValue() )
229 0 : bRetValue = sal_False;
230 : }
231 0 : catch( ::com::sun::star::uno::Exception& )
232 : {
233 0 : bRetValue = sal_False;
234 : }
235 : }
236 : }
237 : else
238 189 : bRetValue = sal_False;
239 189 : return bRetValue;
240 : }
241 :
242 :
243 : // if property is available it returns a pointer,
244 : // otherwise the result is null
245 189 : PropertyValue* FilterConfigItem::GetPropertyValue( Sequence< PropertyValue >& rPropSeq, const OUString& rName )
246 : {
247 189 : PropertyValue* pPropValue = NULL;
248 :
249 : sal_Int32 i, nCount;
250 384 : for ( i = 0, nCount = rPropSeq.getLength(); i < nCount; i++ )
251 : {
252 195 : if ( rPropSeq[ i ].Name == rName )
253 : {
254 0 : pPropValue = &rPropSeq[ i ];
255 0 : break;
256 : }
257 : }
258 189 : return pPropValue;
259 : }
260 :
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 */
264 :
265 189 : sal_Bool FilterConfigItem::WritePropertyValue( Sequence< PropertyValue >& rPropSeq, const PropertyValue& rPropValue )
266 : {
267 189 : sal_Bool bRet = sal_False;
268 189 : if ( !rPropValue.Name.isEmpty() )
269 : {
270 : sal_Int32 i, nCount;
271 384 : for ( i = 0, nCount = rPropSeq.getLength(); i < nCount; i++ )
272 : {
273 195 : if ( rPropSeq[ i ].Name == rPropValue.Name )
274 0 : break;
275 : }
276 189 : if ( i == nCount )
277 189 : rPropSeq.realloc( ++nCount );
278 :
279 189 : rPropSeq[ i ] = rPropValue;
280 :
281 189 : bRet = sal_True;
282 : }
283 189 : return bRet;
284 : }
285 :
286 0 : sal_Bool FilterConfigItem::ReadBool( const OUString& rKey, sal_Bool bDefault )
287 : {
288 0 : Any aAny;
289 0 : sal_Bool bRetValue = bDefault;
290 0 : PropertyValue* pPropVal = GetPropertyValue( aFilterData, rKey );
291 0 : if ( pPropVal )
292 : {
293 0 : pPropVal->Value >>= bRetValue;
294 : }
295 0 : else if ( ImplGetPropertyValue( aAny, xPropSet, rKey, sal_True ) )
296 : {
297 0 : aAny >>= bRetValue;
298 : }
299 0 : PropertyValue aBool;
300 0 : aBool.Name = rKey;
301 0 : aBool.Value <<= bRetValue;
302 0 : WritePropertyValue( aFilterData, aBool );
303 0 : return bRetValue;
304 : }
305 :
306 189 : sal_Int32 FilterConfigItem::ReadInt32( const OUString& rKey, sal_Int32 nDefault )
307 : {
308 189 : Any aAny;
309 189 : sal_Int32 nRetValue = nDefault;
310 189 : PropertyValue* pPropVal = GetPropertyValue( aFilterData, rKey );
311 189 : if ( pPropVal )
312 : {
313 0 : pPropVal->Value >>= nRetValue;
314 : }
315 189 : else if ( ImplGetPropertyValue( aAny, xPropSet, rKey, sal_True ) )
316 : {
317 0 : aAny >>= nRetValue;
318 : }
319 189 : PropertyValue aInt32;
320 189 : aInt32.Name = rKey;
321 189 : aInt32.Value <<= nRetValue;
322 189 : WritePropertyValue( aFilterData, aInt32 );
323 189 : return nRetValue;
324 : }
325 :
326 0 : OUString FilterConfigItem::ReadString( const OUString& rKey, const OUString& rDefault )
327 : {
328 0 : Any aAny;
329 0 : OUString aRetValue( rDefault );
330 0 : PropertyValue* pPropVal = GetPropertyValue( aFilterData, rKey );
331 0 : if ( pPropVal )
332 : {
333 0 : pPropVal->Value >>= aRetValue;
334 : }
335 0 : else if ( ImplGetPropertyValue( aAny, xPropSet, rKey, sal_True ) )
336 : {
337 0 : aAny >>= aRetValue;
338 : }
339 0 : PropertyValue aString;
340 0 : aString.Name = rKey;
341 0 : aString.Value <<= aRetValue;
342 0 : WritePropertyValue( aFilterData, aString );
343 0 : return aRetValue;
344 : }
345 :
346 0 : void FilterConfigItem::WriteBool( const OUString& rKey, sal_Bool bNewValue )
347 : {
348 0 : PropertyValue aBool;
349 0 : aBool.Name = rKey;
350 0 : aBool.Value <<= bNewValue;
351 0 : WritePropertyValue( aFilterData, aBool );
352 :
353 0 : if ( xPropSet.is() )
354 : {
355 0 : Any aAny;
356 0 : if ( ImplGetPropertyValue( aAny, xPropSet, rKey, sal_True ) )
357 : {
358 0 : sal_Bool bOldValue(sal_True);
359 0 : if ( aAny >>= bOldValue )
360 : {
361 0 : if ( bOldValue != bNewValue )
362 : {
363 0 : aAny <<= bNewValue;
364 : try
365 : {
366 0 : xPropSet->setPropertyValue( rKey, aAny );
367 0 : bModified = sal_True;
368 : }
369 0 : catch ( ::com::sun::star::uno::Exception& )
370 : {
371 : OSL_FAIL( "FilterConfigItem::WriteBool - could not set PropertyValue" );
372 : }
373 : }
374 : }
375 0 : }
376 0 : }
377 0 : }
378 :
379 0 : void FilterConfigItem::WriteInt32( const OUString& rKey, sal_Int32 nNewValue )
380 : {
381 0 : PropertyValue aInt32;
382 0 : aInt32.Name = rKey;
383 0 : aInt32.Value <<= nNewValue;
384 0 : WritePropertyValue( aFilterData, aInt32 );
385 :
386 0 : if ( xPropSet.is() )
387 : {
388 0 : Any aAny;
389 :
390 0 : if ( ImplGetPropertyValue( aAny, xPropSet, rKey, sal_True ) )
391 : {
392 0 : sal_Int32 nOldValue = 0;
393 0 : if ( aAny >>= nOldValue )
394 : {
395 0 : if ( nOldValue != nNewValue )
396 : {
397 0 : aAny <<= nNewValue;
398 : try
399 : {
400 0 : xPropSet->setPropertyValue( rKey, aAny );
401 0 : bModified = sal_True;
402 : }
403 0 : catch ( ::com::sun::star::uno::Exception& )
404 : {
405 : OSL_FAIL( "FilterConfigItem::WriteInt32 - could not set PropertyValue" );
406 : }
407 : }
408 : }
409 0 : }
410 0 : }
411 0 : }
412 :
413 : // ------------------------------------------------------------------------
414 :
415 0 : Sequence< PropertyValue > FilterConfigItem::GetFilterData() const
416 : {
417 0 : return aFilterData;
418 : }
419 :
420 : // ------------------------------------------------------------------------
421 :
422 29 : Reference< XStatusIndicator > FilterConfigItem::GetStatusIndicator() const
423 : {
424 29 : Reference< XStatusIndicator > xStatusIndicator;
425 29 : const rtl::OUString sStatusIndicator( RTL_CONSTASCII_USTRINGPARAM( "StatusIndicator" ) );
426 :
427 29 : sal_Int32 i, nCount = aFilterData.getLength();
428 116 : for ( i = 0; i < nCount; i++ )
429 : {
430 87 : if ( aFilterData[ i ].Name == sStatusIndicator )
431 : {
432 0 : aFilterData[ i ].Value >>= xStatusIndicator;
433 0 : break;
434 : }
435 : }
436 29 : return xStatusIndicator;
437 : }
438 :
439 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|