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