1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: configurationaccess.cxx,v $
13 * This file is part of OpenOffice.org.
15 * OpenOffice.org is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU Lesser General Public License version 3
17 * only, as published by the Free Software Foundation.
19 * OpenOffice.org is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Lesser General Public License version 3 for more details
23 * (a copy is included in the LICENSE file that accompanied this code).
25 * You should have received a copy of the GNU Lesser General Public License
26 * version 3 along with OpenOffice.org. If not, see
27 * <http://www.openoffice.org/license.html>
28 * for a copy of the LGPLv3 License.
30 ************************************************************************/
32 // MARKER(update_precomp.py): autogen include statement, do not remove
33 #include "precompiled_sdext.hxx"
35 #include "configurationaccess.hxx"
36 #include <comphelper/processfactory.hxx>
37 #include <com/sun/star/frame/XComponentLoader.hpp>
38 #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
39 #include <com/sun/star/util/XChangesBatch.hpp>
40 #include <com/sun/star/container/XNameContainer.hpp>
41 #include <com/sun/star/util/XMacroExpander.hpp>
42 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
44 using namespace ::rtl
;
45 using namespace ::com::sun::star
;
46 using namespace ::com::sun::star::uno
;
47 using namespace ::com::sun::star::beans
;
48 using namespace ::com::sun::star::container
;
50 static const OUString
& GetConfigurationProviderServiceName (void)
52 static const OUString
sConfigurationProviderServiceName (
53 RTL_CONSTASCII_USTRINGPARAM(
54 "com.sun.star.configuration.ConfigurationProvider"));
55 return sConfigurationProviderServiceName
;
57 static const OUString
& GetPathToConfigurationRoot (void)
59 static const OUString
sPathToConfigurationRoot (
60 RTL_CONSTASCII_USTRINGPARAM("org.openoffice.Office.extension.SunPresentationMinimizer"));
61 return sPathToConfigurationRoot
;
64 void OptimizerSettings::LoadSettingsFromConfiguration( const Reference
< XNameAccess
>& rSettings
)
68 const Sequence
< OUString
> aElements( rSettings
->getElementNames() );
69 for ( int i
= 0; i
< aElements
.getLength(); i
++ )
73 const OUString
aPropertyName( aElements
[ i
] );
74 Any
aValue( rSettings
->getByName( aPropertyName
) );
75 switch( TKGet( aPropertyName
) )
77 case TK_Name
: aValue
>>= maName
; break;
78 case TK_JPEGCompression
: aValue
>>= mbJPEGCompression
; break;
79 case TK_JPEGQuality
: aValue
>>= mnJPEGQuality
; break;
80 case TK_RemoveCropArea
: aValue
>>= mbRemoveCropArea
; break;
81 case TK_ImageResolution
: aValue
>>= mnImageResolution
; break;
82 case TK_EmbedLinkedGraphics
: aValue
>>= mbEmbedLinkedGraphics
; break;
83 case TK_OLEOptimization
: aValue
>>= mbOLEOptimization
; break;
84 case TK_OLEOptimizationType
: aValue
>>= mnOLEOptimizationType
; break;
85 case TK_DeleteUnusedMasterPages
: aValue
>>= mbDeleteUnusedMasterPages
; break;
86 case TK_DeleteHiddenSlides
: aValue
>>= mbDeleteHiddenSlides
; break;
87 case TK_DeleteNotesPages
: aValue
>>= mbDeleteNotesPages
;break;
88 case TK_SaveAs
: aValue
>>= mbSaveAs
; break;
89 // case TK_SaveAsURL : aValue >>= maSaveAsURL; break; // URL is not saved to configuration
90 // case TK_FilterName : aValue >>= maFilterName; break; // URL is not saved to configuration
91 case TK_OpenNewDocument
: aValue
>>= mbOpenNewDocument
; break;
102 void OptimizerSettings::SaveSettingsToConfiguration( const Reference
< XNameReplace
>& rSettings
)
104 if ( rSettings
.is() )
106 OUString pNames
[] = {
108 TKGet( TK_JPEGCompression
),
109 TKGet( TK_JPEGQuality
),
110 TKGet( TK_RemoveCropArea
),
111 TKGet( TK_ImageResolution
),
112 TKGet( TK_EmbedLinkedGraphics
),
113 TKGet( TK_OLEOptimization
),
114 TKGet( TK_OLEOptimizationType
),
115 TKGet( TK_DeleteUnusedMasterPages
),
116 TKGet( TK_DeleteHiddenSlides
),
117 TKGet( TK_DeleteNotesPages
),
119 // TKGet( TK_SaveAsURL ),
120 // TKGet( TK_FilterName ),
121 TKGet( TK_OpenNewDocument
) };
125 Any( mbJPEGCompression
),
126 Any( mnJPEGQuality
),
127 Any( mbRemoveCropArea
),
128 Any( mnImageResolution
),
129 Any( mbEmbedLinkedGraphics
),
130 Any( mbOLEOptimization
),
131 Any( mnOLEOptimizationType
),
132 Any( mbDeleteUnusedMasterPages
),
133 Any( mbDeleteHiddenSlides
),
134 Any( mbDeleteNotesPages
),
136 // Any( maSaveAsURL ),
137 // Any( maFilterName ),
138 Any( mbOpenNewDocument
) };
140 sal_Int32 i
, nCount
= sizeof( pNames
) / sizeof( OUString
);
142 for ( i
= 0; i
< nCount
; i
++ )
146 rSettings
->replaceByName( pNames
[ i
], pValues
[ i
] );
148 catch( Exception
& /* rException */ )
155 sal_Bool
OptimizerSettings::operator==( const OptimizerSettings
& rOptimizerSettings
) const
157 return ( rOptimizerSettings
.mbJPEGCompression
== mbJPEGCompression
)
158 && ( rOptimizerSettings
.mnJPEGQuality
== mnJPEGQuality
)
159 && ( rOptimizerSettings
.mbRemoveCropArea
== mbRemoveCropArea
)
160 && ( rOptimizerSettings
.mnImageResolution
== mnImageResolution
)
161 && ( rOptimizerSettings
.mbEmbedLinkedGraphics
== mbEmbedLinkedGraphics
)
162 && ( rOptimizerSettings
.mbOLEOptimization
== mbOLEOptimization
)
163 && ( rOptimizerSettings
.mnOLEOptimizationType
== mnOLEOptimizationType
)
164 && ( rOptimizerSettings
.mbDeleteUnusedMasterPages
== mbDeleteUnusedMasterPages
)
165 && ( rOptimizerSettings
.mbDeleteHiddenSlides
== mbDeleteHiddenSlides
)
166 && ( rOptimizerSettings
.mbDeleteNotesPages
== mbDeleteNotesPages
);
167 // && ( rOptimizerSettings.mbOpenNewDocument == mbOpenNewDocument );
171 ConfigurationAccess::ConfigurationAccess( const Reference
< uno::XComponentContext
>& rxMSF
, OptimizerSettings
* pDefaultSettings
) :
175 maSettings
.push_back( pDefaultSettings
?
176 *pDefaultSettings
: OptimizerSettings() );
177 maSettings
.back().maName
= TKGet( TK_LastUsedSettings
);
179 maInitialSettings
= maSettings
;
182 ConfigurationAccess::~ConfigurationAccess()
186 rtl::OUString
ConfigurationAccess::getPath( const PPPOptimizerTokenEnum eToken
)
191 static const OUString
sProtocol( RTL_CONSTASCII_USTRINGPARAM( "vnd.sun.star.expand:" ) );
192 static const OUString
stheMacroExpander( RTL_CONSTASCII_USTRINGPARAM( "/singletons/com.sun.star.util.theMacroExpander" ) );
193 Reference
< container::XNameAccess
> xSet( OpenConfiguration( true ), UNO_QUERY_THROW
);
194 if ( xSet
->hasByName( TKGet( eToken
) ) )
195 xSet
->getByName( TKGet( eToken
) ) >>= aPath
;
196 if ( aPath
.match( sProtocol
, 0 ) )
198 rtl::OUString
aTmp( aPath
.copy( 20 ) );
199 Reference
< util::XMacroExpander
> xExpander
;
200 if ( mxMSF
->getValueByName( stheMacroExpander
) >>= xExpander
)
202 aPath
= xExpander
->expandMacros( aTmp
);
212 rtl::OUString
ConfigurationAccess::getString( const PPPOptimizerTokenEnum eToken
) const
214 std::map
< PPPOptimizerTokenEnum
, rtl::OUString
, Compare
>::const_iterator
aIter( maStrings
.find( eToken
) );
215 return aIter
!= maStrings
.end() ? ((*aIter
).second
) : rtl::OUString();
218 void ConfigurationAccess::LoadStrings()
224 Reference
< XInterface
> xRoot( OpenConfiguration( true ) );
227 Reference
< container::XNameAccess
> xSet( GetConfigurationNode( xRoot
, TKGet( TK_Strings
) ), UNO_QUERY
);
230 const Sequence
< OUString
> aElements( xSet
->getElementNames() );
231 for ( int i
= 0; i
< aElements
.getLength(); i
++ )
235 OUString aString
, aPropertyName( aElements
[ i
] );
236 if ( xSet
->getByName( aPropertyName
) >>= aString
)
237 maStrings
[ TKGet( aPropertyName
) ] = aString
;
252 void ConfigurationAccess::LoadConfiguration()
258 Reference
< XInterface
> xRoot( OpenConfiguration( true ) );
261 Reference
< container::XNameAccess
> xSet( GetConfigurationNode( xRoot
, TKGet( TK_LastUsedSettings
) ), UNO_QUERY
);
264 OptimizerSettings
& rCurrent( maSettings
.front() );
265 rCurrent
.LoadSettingsFromConfiguration( xSet
);
267 xSet
= Reference
< container::XNameAccess
>( GetConfigurationNode( xRoot
, TKGet( TK_Settings_Templates
) ), UNO_QUERY
);
270 const Sequence
< OUString
> aElements( xSet
->getElementNames() );
271 for ( int i
= 0; i
< aElements
.getLength(); i
++ )
275 OUString
aPath( TKGet( TK_Settings_Templates_
).concat( aElements
[ i
] ) );
276 Reference
< container::XNameAccess
> xTemplates( GetConfigurationNode( xRoot
, aPath
), UNO_QUERY
);
277 if ( xTemplates
.is() )
279 maSettings
.push_back( OptimizerSettings() );
280 maSettings
.back().LoadSettingsFromConfiguration( xTemplates
);
283 catch( Exception
& /* rException */ )
296 void ConfigurationAccess::SaveConfiguration()
304 Reference
<util::XChangesBatch
> xRoot( OpenConfiguration( false ), UNO_QUERY_THROW
);
306 // storing the last used settings
307 Reference
< container::XNameReplace
> xSet( GetConfigurationNode( xRoot
, TKGet( TK_LastUsedSettings
) ), UNO_QUERY_THROW
);
308 OptimizerSettings
& rCurrent( maSettings
.front() );
309 rCurrent
.SaveSettingsToConfiguration( xSet
);
311 // updating template elements
312 xSet
= Reference
< container::XNameReplace
>( GetConfigurationNode( xRoot
, TKGet( TK_Settings_Templates
) ), UNO_QUERY_THROW
);
313 Reference
< container::XNameContainer
> xNameContainer( xSet
, UNO_QUERY_THROW
);
315 const Sequence
< OUString
> aElements( xSet
->getElementNames() );
316 for( i
= 0; i
< aElements
.getLength(); i
++ )
317 xNameContainer
->removeByName( aElements
[ i
] );
319 for( k
= 1; k
< maSettings
.size(); k
++ )
321 OptimizerSettings
& rSettings( maSettings
[ k
] );
322 OUString
aElementName( TKGet( TK_Template
).concat( OUString::valueOf( static_cast< sal_Int32
>( k
) ) ) );
323 Reference
< lang::XSingleServiceFactory
> xChildFactory ( xSet
, UNO_QUERY_THROW
);
324 Reference
< container::XNameReplace
> xChild( xChildFactory
->createInstance(), UNO_QUERY_THROW
);
325 xNameContainer
->insertByName( aElementName
, Any( xChild
) );
327 OUString
aPath( TKGet( TK_Settings_Templates_
).concat( aElementName
) );
328 Reference
< container::XNameReplace
> xTemplates( GetConfigurationNode( xRoot
, aPath
), UNO_QUERY
);
329 rSettings
.SaveSettingsToConfiguration( xTemplates
);
331 xRoot
->commitChanges();
335 catch( Exception
& /* rException */ )
341 Reference
< XInterface
> ConfigurationAccess::OpenConfiguration( bool bReadOnly
)
343 Reference
< XInterface
> xRoot
;
346 Reference
< lang::XMultiServiceFactory
> xProvider( mxMSF
->getServiceManager()->createInstanceWithContext( GetConfigurationProviderServiceName(), mxMSF
), UNO_QUERY
);
347 if ( xProvider
.is() )
349 Sequence
< Any
> aCreationArguments( 2 );
350 aCreationArguments
[0] = makeAny( PropertyValue(
351 OUString( RTL_CONSTASCII_USTRINGPARAM( "nodepath" ) ), 0,
352 makeAny( GetPathToConfigurationRoot() ),
353 PropertyState_DIRECT_VALUE
) );
354 aCreationArguments
[1] = makeAny(beans::PropertyValue(
355 OUString( RTL_CONSTASCII_USTRINGPARAM( "lazywrite" ) ), 0, makeAny( true ),
356 PropertyState_DIRECT_VALUE
) );
357 OUString sAccessService
;
359 sAccessService
= OUString( RTL_CONSTASCII_USTRINGPARAM(
360 "com.sun.star.configuration.ConfigurationAccess" ) );
362 sAccessService
= OUString( RTL_CONSTASCII_USTRINGPARAM(
363 "com.sun.star.configuration.ConfigurationUpdateAccess" ) );
365 xRoot
= xProvider
->createInstanceWithArguments(
366 sAccessService
, aCreationArguments
);
369 catch ( Exception
& /* rException */ )
375 Reference
< XInterface
> ConfigurationAccess::GetConfigurationNode(
376 const Reference
< XInterface
>& xRoot
,
377 const OUString
& sPathToNode
)
379 Reference
< XInterface
> xNode
;
382 if ( !sPathToNode
.getLength() )
386 Reference
< XHierarchicalNameAccess
> xHierarchy( xRoot
, UNO_QUERY
);
387 if ( xHierarchy
.is() )
389 xHierarchy
->getByHierarchicalName( sPathToNode
) >>= xNode
;
393 catch ( Exception
& rException
)
395 OSL_TRACE ("caught exception while getting configuration node %s: %s",
396 ::rtl::OUStringToOString(sPathToNode
,
397 RTL_TEXTENCODING_UTF8
).getStr(),
398 ::rtl::OUStringToOString(rException
.Message
,
399 RTL_TEXTENCODING_UTF8
).getStr());
404 com::sun::star::uno::Any
ConfigurationAccess::GetConfigProperty( const PPPOptimizerTokenEnum ePropertyToken
) const
407 const OptimizerSettings
& rSettings( maSettings
.front() );
410 switch( ePropertyToken
)
412 case TK_Name
: aRetValue
<<= rSettings
.maName
; break;
413 case TK_JPEGCompression
: aRetValue
<<= rSettings
.mbJPEGCompression
; break;
414 case TK_JPEGQuality
: aRetValue
<<= rSettings
.mnJPEGQuality
; break;
415 case TK_RemoveCropArea
: aRetValue
<<= rSettings
.mbRemoveCropArea
; break;
416 case TK_ImageResolution
: aRetValue
<<= rSettings
.mnImageResolution
; break;
417 case TK_EmbedLinkedGraphics
: aRetValue
<<= rSettings
.mbEmbedLinkedGraphics
; break;
418 case TK_OLEOptimization
: aRetValue
<<= rSettings
.mbOLEOptimization
; break;
419 case TK_OLEOptimizationType
: aRetValue
<<= rSettings
.mnOLEOptimizationType
; break;
420 case TK_DeleteUnusedMasterPages
: aRetValue
<<= rSettings
.mbDeleteUnusedMasterPages
; break;
421 case TK_DeleteHiddenSlides
: aRetValue
<<= rSettings
.mbDeleteHiddenSlides
; break;
422 case TK_DeleteNotesPages
: aRetValue
<<= rSettings
.mbDeleteNotesPages
; break;
423 case TK_SaveAs
: aRetValue
<<= rSettings
.mbSaveAs
; break;
424 case TK_SaveAsURL
: aRetValue
<<= rSettings
.maSaveAsURL
; break;
425 case TK_FilterName
: aRetValue
<<= rSettings
.maFilterName
; break;
426 case TK_OpenNewDocument
: aRetValue
<<= rSettings
.mbOpenNewDocument
; break;
427 case TK_EstimatedFileSize
: aRetValue
<<= rSettings
.mnEstimatedFileSize
; break;
432 catch( Exception
& /* rException */ )
438 void ConfigurationAccess::SetConfigProperty( const PPPOptimizerTokenEnum ePropertyToken
, const com::sun::star::uno::Any
& rValue
)
440 OptimizerSettings
& rSettings( maSettings
.front() );
443 switch( ePropertyToken
)
445 case TK_Name
: rValue
>>= rSettings
.maName
; break;
446 case TK_JPEGCompression
: rValue
>>= rSettings
.mbJPEGCompression
; break;
447 case TK_JPEGQuality
: rValue
>>= rSettings
.mnJPEGQuality
; break;
448 case TK_RemoveCropArea
: rValue
>>= rSettings
.mbRemoveCropArea
; break;
449 case TK_ImageResolution
: rValue
>>= rSettings
.mnImageResolution
; break;
450 case TK_EmbedLinkedGraphics
: rValue
>>= rSettings
.mbEmbedLinkedGraphics
; break;
451 case TK_OLEOptimization
: rValue
>>= rSettings
.mbOLEOptimization
; break;
452 case TK_OLEOptimizationType
: rValue
>>= rSettings
.mnOLEOptimizationType
; break;
453 case TK_DeleteUnusedMasterPages
: rValue
>>= rSettings
.mbDeleteUnusedMasterPages
; break;
454 case TK_DeleteHiddenSlides
: rValue
>>= rSettings
.mbDeleteHiddenSlides
; break;
455 case TK_DeleteNotesPages
: rValue
>>= rSettings
.mbDeleteNotesPages
; break;
456 case TK_CustomShowName
: rValue
>>= rSettings
.maCustomShowName
; break;
457 case TK_SaveAs
: rValue
>>= rSettings
.mbSaveAs
; break;
458 case TK_SaveAsURL
: rValue
>>= rSettings
.maSaveAsURL
; break;
459 case TK_FilterName
: rValue
>>= rSettings
.maFilterName
; break;
460 case TK_OpenNewDocument
: rValue
>>= rSettings
.mbOpenNewDocument
; break;
461 case TK_EstimatedFileSize
: rValue
>>= rSettings
.mnEstimatedFileSize
; break;
466 catch( Exception
& /* rException */ )
471 sal_Bool
ConfigurationAccess::GetConfigProperty( const PPPOptimizerTokenEnum ePropertyToken
, const sal_Bool bDefault
) const
473 sal_Bool bRetValue
= bDefault
;
474 if ( ! ( GetConfigProperty( ePropertyToken
) >>= bRetValue
) )
475 bRetValue
= bDefault
;
479 sal_Int16
ConfigurationAccess::GetConfigProperty( const PPPOptimizerTokenEnum ePropertyToken
, const sal_Int16 nDefault
) const
481 sal_Int16 nRetValue
= nDefault
;
482 if ( ! ( GetConfigProperty( ePropertyToken
) >>= nRetValue
) )
483 nRetValue
= nDefault
;
487 sal_Int32
ConfigurationAccess::GetConfigProperty( const PPPOptimizerTokenEnum ePropertyToken
, const sal_Int32 nDefault
) const
489 sal_Int32 nRetValue
= nDefault
;
490 if ( ! ( GetConfigProperty( ePropertyToken
) >>= nRetValue
) )
491 nRetValue
= nDefault
;
495 Sequence
< PropertyValue
> ConfigurationAccess::GetConfigurationSequence()
497 Sequence
< PropertyValue
> aRet( 15 );
498 OptimizerSettings
& rSettings( maSettings
.front() );
499 aRet
[ 0 ].Name
= TKGet( TK_JPEGCompression
);
500 aRet
[ 0 ].Value
= Any( rSettings
.mbJPEGCompression
);
501 aRet
[ 1 ].Name
= TKGet( TK_JPEGQuality
);
502 aRet
[ 1 ].Value
= Any( rSettings
.mnJPEGQuality
);
503 aRet
[ 2 ].Name
= TKGet( TK_RemoveCropArea
);
504 aRet
[ 2 ].Value
= Any( rSettings
.mbRemoveCropArea
);
505 aRet
[ 3 ].Name
= TKGet( TK_ImageResolution
);
506 aRet
[ 3 ].Value
= Any( rSettings
.mnImageResolution
);
507 aRet
[ 4 ].Name
= TKGet( TK_EmbedLinkedGraphics
);
508 aRet
[ 4 ].Value
= Any( rSettings
.mbEmbedLinkedGraphics
);
509 aRet
[ 5 ].Name
= TKGet( TK_OLEOptimization
);
510 aRet
[ 5 ].Value
= Any( rSettings
.mbOLEOptimization
);
511 aRet
[ 6 ].Name
= TKGet( TK_OLEOptimizationType
);
512 aRet
[ 6 ].Value
= Any( rSettings
.mnOLEOptimizationType
);
513 aRet
[ 7 ].Name
= TKGet( TK_DeleteUnusedMasterPages
);
514 aRet
[ 7 ].Value
= Any( rSettings
.mbDeleteUnusedMasterPages
);
515 aRet
[ 8 ].Name
= TKGet( TK_DeleteHiddenSlides
);
516 aRet
[ 8 ].Value
= Any( rSettings
.mbDeleteHiddenSlides
);
517 aRet
[ 9 ].Name
= TKGet( TK_DeleteNotesPages
);
518 aRet
[ 9 ].Value
= Any( rSettings
.mbDeleteNotesPages
);
519 aRet
[ 10].Name
= TKGet( TK_CustomShowName
);
520 aRet
[ 10].Value
= Any( rSettings
.maCustomShowName
);
521 aRet
[ 11].Name
= TKGet( TK_SaveAsURL
);
522 aRet
[ 11].Value
= Any( rSettings
.maSaveAsURL
);
523 aRet
[ 12].Name
= TKGet( TK_FilterName
);
524 aRet
[ 12].Value
= Any( rSettings
.maFilterName
);
525 aRet
[ 13].Name
= TKGet( TK_OpenNewDocument
);
526 aRet
[ 13].Value
= Any( rSettings
.mbOpenNewDocument
);
527 aRet
[ 14].Name
= TKGet( TK_EstimatedFileSize
);
528 aRet
[ 14].Value
= Any( rSettings
.mnEstimatedFileSize
);
532 std::vector
< OptimizerSettings
>::iterator
ConfigurationAccess::GetOptimizerSettingsByName( const rtl::OUString
& rName
)
534 std::vector
< OptimizerSettings
>::iterator
aIter( maSettings
.begin() + 1 );
535 while ( aIter
!= maSettings
.end() )
537 if ( aIter
->maName
== rName
)