1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
21 #include "configurationaccess.hxx"
22 #include <com/sun/star/frame/XComponentLoader.hpp>
23 #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
24 #include <com/sun/star/configuration/theDefaultProvider.hpp>
25 #include <com/sun/star/util/XChangesBatch.hpp>
26 #include <com/sun/star/container/XNameContainer.hpp>
27 #include <com/sun/star/util/theMacroExpander.hpp>
28 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
29 #include <sal/macros.h>
31 using namespace ::rtl
;
32 using namespace ::com::sun::star
;
33 using namespace ::com::sun::star::uno
;
34 using namespace ::com::sun::star::beans
;
35 using namespace ::com::sun::star::container
;
37 static const OUString
& GetPathToConfigurationRoot (void)
39 static const OUString
sPathToConfigurationRoot ("org.openoffice.Office.extension.SunPresentationMinimizer");
40 return sPathToConfigurationRoot
;
43 void OptimizerSettings::LoadSettingsFromConfiguration( const Reference
< XNameAccess
>& rSettings
)
47 const Sequence
< OUString
> aElements( rSettings
->getElementNames() );
48 for ( int i
= 0; i
< aElements
.getLength(); i
++ )
52 const OUString
aPropertyName( aElements
[ i
] );
53 Any
aValue( rSettings
->getByName( aPropertyName
) );
54 switch( TKGet( aPropertyName
) )
56 case TK_Name
: aValue
>>= maName
; break;
57 case TK_JPEGCompression
: aValue
>>= mbJPEGCompression
; break;
58 case TK_JPEGQuality
: aValue
>>= mnJPEGQuality
; break;
59 case TK_RemoveCropArea
: aValue
>>= mbRemoveCropArea
; break;
60 case TK_ImageResolution
: aValue
>>= mnImageResolution
; break;
61 case TK_EmbedLinkedGraphics
: aValue
>>= mbEmbedLinkedGraphics
; break;
62 case TK_OLEOptimization
: aValue
>>= mbOLEOptimization
; break;
63 case TK_OLEOptimizationType
: aValue
>>= mnOLEOptimizationType
; break;
64 case TK_DeleteUnusedMasterPages
: aValue
>>= mbDeleteUnusedMasterPages
; break;
65 case TK_DeleteHiddenSlides
: aValue
>>= mbDeleteHiddenSlides
; break;
66 case TK_DeleteNotesPages
: aValue
>>= mbDeleteNotesPages
;break;
67 case TK_SaveAs
: aValue
>>= mbSaveAs
; break;
68 // case TK_SaveAsURL : aValue >>= maSaveAsURL; break; // URL is not saved to configuration
69 // case TK_FilterName : aValue >>= maFilterName; break; // URL is not saved to configuration
70 case TK_OpenNewDocument
: aValue
>>= mbOpenNewDocument
; break;
74 catch (const Exception
&)
81 void OptimizerSettings::SaveSettingsToConfiguration( const Reference
< XNameReplace
>& rSettings
)
87 TKGet( TK_JPEGCompression
),
88 TKGet( TK_JPEGQuality
),
89 TKGet( TK_RemoveCropArea
),
90 TKGet( TK_ImageResolution
),
91 TKGet( TK_EmbedLinkedGraphics
),
92 TKGet( TK_OLEOptimization
),
93 TKGet( TK_OLEOptimizationType
),
94 TKGet( TK_DeleteUnusedMasterPages
),
95 TKGet( TK_DeleteHiddenSlides
),
96 TKGet( TK_DeleteNotesPages
),
98 // TKGet( TK_SaveAsURL ),
99 // TKGet( TK_FilterName ),
100 TKGet( TK_OpenNewDocument
) };
104 Any( mbJPEGCompression
),
105 Any( mnJPEGQuality
),
106 Any( mbRemoveCropArea
),
107 Any( mnImageResolution
),
108 Any( mbEmbedLinkedGraphics
),
109 Any( mbOLEOptimization
),
110 Any( mnOLEOptimizationType
),
111 Any( mbDeleteUnusedMasterPages
),
112 Any( mbDeleteHiddenSlides
),
113 Any( mbDeleteNotesPages
),
115 // Any( maSaveAsURL ),
116 // Any( maFilterName ),
117 Any( mbOpenNewDocument
) };
119 sal_Int32 i
, nCount
= SAL_N_ELEMENTS( pNames
);
121 for ( i
= 0; i
< nCount
; i
++ )
125 rSettings
->replaceByName( pNames
[ i
], pValues
[ i
] );
127 catch (const Exception
&)
134 sal_Bool
OptimizerSettings::operator==( const OptimizerSettings
& rOptimizerSettings
) const
136 return ( rOptimizerSettings
.mbJPEGCompression
== mbJPEGCompression
)
137 && ( rOptimizerSettings
.mnJPEGQuality
== mnJPEGQuality
)
138 && ( rOptimizerSettings
.mbRemoveCropArea
== mbRemoveCropArea
)
139 && ( rOptimizerSettings
.mnImageResolution
== mnImageResolution
)
140 && ( rOptimizerSettings
.mbEmbedLinkedGraphics
== mbEmbedLinkedGraphics
)
141 && ( rOptimizerSettings
.mbOLEOptimization
== mbOLEOptimization
)
142 && ( rOptimizerSettings
.mnOLEOptimizationType
== mnOLEOptimizationType
)
143 && ( rOptimizerSettings
.mbDeleteUnusedMasterPages
== mbDeleteUnusedMasterPages
)
144 && ( rOptimizerSettings
.mbDeleteHiddenSlides
== mbDeleteHiddenSlides
)
145 && ( rOptimizerSettings
.mbDeleteNotesPages
== mbDeleteNotesPages
);
146 // && ( rOptimizerSettings.mbOpenNewDocument == mbOpenNewDocument );
150 ConfigurationAccess::ConfigurationAccess( const Reference
< uno::XComponentContext
>& rxMSF
, OptimizerSettings
* pDefaultSettings
) :
154 maSettings
.push_back( pDefaultSettings
?
155 *pDefaultSettings
: OptimizerSettings() );
156 maSettings
.back().maName
= TKGet( TK_LastUsedSettings
);
158 maInitialSettings
= maSettings
;
161 ConfigurationAccess::~ConfigurationAccess()
165 OUString
ConfigurationAccess::getPath( const PPPOptimizerTokenEnum eToken
)
170 static const OUString
sProtocol( "vnd.sun.star.expand:" );
171 Reference
< container::XNameAccess
> xSet( OpenConfiguration( true ), UNO_QUERY_THROW
);
172 if ( xSet
->hasByName( TKGet( eToken
) ) )
173 xSet
->getByName( TKGet( eToken
) ) >>= aPath
;
174 if ( aPath
.match( sProtocol
, 0 ) )
176 OUString
aTmp( aPath
.copy( 20 ) );
177 Reference
< util::XMacroExpander
> xExpander
= util::theMacroExpander::get(mxMSF
);
178 aPath
= xExpander
->expandMacros( aTmp
);
181 catch (const Exception
&)
187 OUString
ConfigurationAccess::getString( const PPPOptimizerTokenEnum eToken
) const
189 std::map
< PPPOptimizerTokenEnum
, OUString
, Compare
>::const_iterator
aIter( maStrings
.find( eToken
) );
190 return aIter
!= maStrings
.end() ? ((*aIter
).second
) : OUString();
193 void ConfigurationAccess::LoadStrings()
199 Reference
< XInterface
> xRoot( OpenConfiguration( true ) );
202 Reference
< container::XNameAccess
> xSet( GetConfigurationNode( xRoot
, TKGet( TK_Strings
) ), UNO_QUERY
);
205 const Sequence
< OUString
> aElements( xSet
->getElementNames() );
206 for ( int i
= 0; i
< aElements
.getLength(); i
++ )
210 OUString aString
, aPropertyName( aElements
[ i
] );
211 if ( xSet
->getByName( aPropertyName
) >>= aString
)
212 maStrings
[ TKGet( aPropertyName
) ] = aString
;
214 catch (const Exception
&)
222 catch (const Exception
&)
227 void ConfigurationAccess::LoadConfiguration()
233 Reference
< XInterface
> xRoot( OpenConfiguration( true ) );
236 Reference
< container::XNameAccess
> xSet( GetConfigurationNode( xRoot
, TKGet( TK_LastUsedSettings
) ), UNO_QUERY
);
239 OptimizerSettings
& rCurrent( maSettings
.front() );
240 rCurrent
.LoadSettingsFromConfiguration( xSet
);
242 xSet
= Reference
< container::XNameAccess
>( GetConfigurationNode( xRoot
, TKGet( TK_Settings_Templates
) ), UNO_QUERY
);
245 const Sequence
< OUString
> aElements( xSet
->getElementNames() );
246 for ( int i
= 0; i
< aElements
.getLength(); i
++ )
250 OUString
aPath( TKGet( TK_Settings_Templates_
).concat( aElements
[ i
] ) );
251 Reference
< container::XNameAccess
> xTemplates( GetConfigurationNode( xRoot
, aPath
), UNO_QUERY
);
252 if ( xTemplates
.is() )
254 maSettings
.push_back( OptimizerSettings() );
255 maSettings
.back().LoadSettingsFromConfiguration( xTemplates
);
258 catch (const Exception
&)
266 catch (const Exception
&)
271 void ConfigurationAccess::SaveConfiguration()
279 Reference
<util::XChangesBatch
> xRoot( OpenConfiguration( false ), UNO_QUERY_THROW
);
281 // storing the last used settings
282 Reference
< container::XNameReplace
> xSet( GetConfigurationNode( xRoot
, TKGet( TK_LastUsedSettings
) ), UNO_QUERY_THROW
);
283 OptimizerSettings
& rCurrent( maSettings
.front() );
284 rCurrent
.SaveSettingsToConfiguration( xSet
);
286 // updating template elements
287 xSet
= Reference
< container::XNameReplace
>( GetConfigurationNode( xRoot
, TKGet( TK_Settings_Templates
) ), UNO_QUERY_THROW
);
288 Reference
< container::XNameContainer
> xNameContainer( xSet
, UNO_QUERY_THROW
);
290 const Sequence
< OUString
> aElements( xSet
->getElementNames() );
291 for( i
= 0; i
< aElements
.getLength(); i
++ )
292 xNameContainer
->removeByName( aElements
[ i
] );
294 for( k
= 1; k
< maSettings
.size(); k
++ )
296 OptimizerSettings
& rSettings( maSettings
[ k
] );
297 OUString
aElementName( TKGet( TK_Template
).concat( OUString::number( k
) ) );
298 Reference
< lang::XSingleServiceFactory
> xChildFactory ( xSet
, UNO_QUERY_THROW
);
299 Reference
< container::XNameReplace
> xChild( xChildFactory
->createInstance(), UNO_QUERY_THROW
);
300 xNameContainer
->insertByName( aElementName
, Any( xChild
) );
302 OUString
aPath( TKGet( TK_Settings_Templates_
).concat( aElementName
) );
303 Reference
< container::XNameReplace
> xTemplates( GetConfigurationNode( xRoot
, aPath
), UNO_QUERY
);
304 rSettings
.SaveSettingsToConfiguration( xTemplates
);
306 xRoot
->commitChanges();
310 catch (const Exception
&)
315 Reference
< XInterface
> ConfigurationAccess::OpenConfiguration( bool bReadOnly
)
317 Reference
< XInterface
> xRoot
;
320 Reference
< lang::XMultiServiceFactory
> xProvider
= configuration::theDefaultProvider::get( mxMSF
);
321 Sequence
< Any
> aCreationArguments( 2 );
322 aCreationArguments
[0] = makeAny( PropertyValue(
323 OUString( "nodepath" ), 0,
324 makeAny( GetPathToConfigurationRoot() ),
325 PropertyState_DIRECT_VALUE
) );
326 aCreationArguments
[1] = makeAny(beans::PropertyValue(
327 OUString( "lazywrite" ), 0, makeAny( true ),
328 PropertyState_DIRECT_VALUE
) );
329 OUString sAccessService
;
331 sAccessService
= OUString(
332 "com.sun.star.configuration.ConfigurationAccess" );
334 sAccessService
= OUString(
335 "com.sun.star.configuration.ConfigurationUpdateAccess" );
337 xRoot
= xProvider
->createInstanceWithArguments(
338 sAccessService
, aCreationArguments
);
340 catch (const Exception
&)
346 Reference
< XInterface
> ConfigurationAccess::GetConfigurationNode(
347 const Reference
< XInterface
>& xRoot
,
348 const OUString
& sPathToNode
)
350 Reference
< XInterface
> xNode
;
353 if ( sPathToNode
.isEmpty() )
357 Reference
< XHierarchicalNameAccess
> xHierarchy( xRoot
, UNO_QUERY
);
358 if ( xHierarchy
.is() )
360 xHierarchy
->getByHierarchicalName( sPathToNode
) >>= xNode
;
364 catch (const Exception
& rException
)
366 OSL_TRACE ("caught exception while getting configuration node %s: %s",
367 OUStringToOString(sPathToNode
,
368 RTL_TEXTENCODING_UTF8
).getStr(),
369 OUStringToOString(rException
.Message
,
370 RTL_TEXTENCODING_UTF8
).getStr());
376 com::sun::star::uno::Any
ConfigurationAccess::GetConfigProperty( const PPPOptimizerTokenEnum ePropertyToken
) const
379 const OptimizerSettings
& rSettings( maSettings
.front() );
382 switch( ePropertyToken
)
384 case TK_Name
: aRetValue
<<= rSettings
.maName
; break;
385 case TK_JPEGCompression
: aRetValue
<<= rSettings
.mbJPEGCompression
; break;
386 case TK_JPEGQuality
: aRetValue
<<= rSettings
.mnJPEGQuality
; break;
387 case TK_RemoveCropArea
: aRetValue
<<= rSettings
.mbRemoveCropArea
; break;
388 case TK_ImageResolution
: aRetValue
<<= rSettings
.mnImageResolution
; break;
389 case TK_EmbedLinkedGraphics
: aRetValue
<<= rSettings
.mbEmbedLinkedGraphics
; break;
390 case TK_OLEOptimization
: aRetValue
<<= rSettings
.mbOLEOptimization
; break;
391 case TK_OLEOptimizationType
: aRetValue
<<= rSettings
.mnOLEOptimizationType
; break;
392 case TK_DeleteUnusedMasterPages
: aRetValue
<<= rSettings
.mbDeleteUnusedMasterPages
; break;
393 case TK_DeleteHiddenSlides
: aRetValue
<<= rSettings
.mbDeleteHiddenSlides
; break;
394 case TK_DeleteNotesPages
: aRetValue
<<= rSettings
.mbDeleteNotesPages
; break;
395 case TK_SaveAs
: aRetValue
<<= rSettings
.mbSaveAs
; break;
396 case TK_SaveAsURL
: aRetValue
<<= rSettings
.maSaveAsURL
; break;
397 case TK_FilterName
: aRetValue
<<= rSettings
.maFilterName
; break;
398 case TK_OpenNewDocument
: aRetValue
<<= rSettings
.mbOpenNewDocument
; break;
399 case TK_EstimatedFileSize
: aRetValue
<<= rSettings
.mnEstimatedFileSize
; break;
404 catch (const Exception
&)
410 void ConfigurationAccess::SetConfigProperty( const PPPOptimizerTokenEnum ePropertyToken
, const com::sun::star::uno::Any
& rValue
)
412 OptimizerSettings
& rSettings( maSettings
.front() );
415 switch( ePropertyToken
)
417 case TK_Name
: rValue
>>= rSettings
.maName
; break;
418 case TK_JPEGCompression
: rValue
>>= rSettings
.mbJPEGCompression
; break;
419 case TK_JPEGQuality
: rValue
>>= rSettings
.mnJPEGQuality
; break;
420 case TK_RemoveCropArea
: rValue
>>= rSettings
.mbRemoveCropArea
; break;
421 case TK_ImageResolution
: rValue
>>= rSettings
.mnImageResolution
; break;
422 case TK_EmbedLinkedGraphics
: rValue
>>= rSettings
.mbEmbedLinkedGraphics
; break;
423 case TK_OLEOptimization
: rValue
>>= rSettings
.mbOLEOptimization
; break;
424 case TK_OLEOptimizationType
: rValue
>>= rSettings
.mnOLEOptimizationType
; break;
425 case TK_DeleteUnusedMasterPages
: rValue
>>= rSettings
.mbDeleteUnusedMasterPages
; break;
426 case TK_DeleteHiddenSlides
: rValue
>>= rSettings
.mbDeleteHiddenSlides
; break;
427 case TK_DeleteNotesPages
: rValue
>>= rSettings
.mbDeleteNotesPages
; break;
428 case TK_CustomShowName
: rValue
>>= rSettings
.maCustomShowName
; break;
429 case TK_SaveAs
: rValue
>>= rSettings
.mbSaveAs
; break;
430 case TK_SaveAsURL
: rValue
>>= rSettings
.maSaveAsURL
; break;
431 case TK_FilterName
: rValue
>>= rSettings
.maFilterName
; break;
432 case TK_OpenNewDocument
: rValue
>>= rSettings
.mbOpenNewDocument
; break;
433 case TK_EstimatedFileSize
: rValue
>>= rSettings
.mnEstimatedFileSize
; break;
438 catch (const Exception
&)
443 sal_Bool
ConfigurationAccess::GetConfigProperty( const PPPOptimizerTokenEnum ePropertyToken
, const sal_Bool bDefault
) const
445 sal_Bool bRetValue
= bDefault
;
446 if ( ! ( GetConfigProperty( ePropertyToken
) >>= bRetValue
) )
447 bRetValue
= bDefault
;
451 sal_Int16
ConfigurationAccess::GetConfigProperty( const PPPOptimizerTokenEnum ePropertyToken
, const sal_Int16 nDefault
) const
453 sal_Int16 nRetValue
= nDefault
;
454 if ( ! ( GetConfigProperty( ePropertyToken
) >>= nRetValue
) )
455 nRetValue
= nDefault
;
459 sal_Int32
ConfigurationAccess::GetConfigProperty( const PPPOptimizerTokenEnum ePropertyToken
, const sal_Int32 nDefault
) const
461 sal_Int32 nRetValue
= nDefault
;
462 if ( ! ( GetConfigProperty( ePropertyToken
) >>= nRetValue
) )
463 nRetValue
= nDefault
;
467 Sequence
< PropertyValue
> ConfigurationAccess::GetConfigurationSequence()
469 Sequence
< PropertyValue
> aRet( 15 );
470 OptimizerSettings
& rSettings( maSettings
.front() );
471 aRet
[ 0 ].Name
= TKGet( TK_JPEGCompression
);
472 aRet
[ 0 ].Value
= Any( rSettings
.mbJPEGCompression
);
473 aRet
[ 1 ].Name
= TKGet( TK_JPEGQuality
);
474 aRet
[ 1 ].Value
= Any( rSettings
.mnJPEGQuality
);
475 aRet
[ 2 ].Name
= TKGet( TK_RemoveCropArea
);
476 aRet
[ 2 ].Value
= Any( rSettings
.mbRemoveCropArea
);
477 aRet
[ 3 ].Name
= TKGet( TK_ImageResolution
);
478 aRet
[ 3 ].Value
= Any( rSettings
.mnImageResolution
);
479 aRet
[ 4 ].Name
= TKGet( TK_EmbedLinkedGraphics
);
480 aRet
[ 4 ].Value
= Any( rSettings
.mbEmbedLinkedGraphics
);
481 aRet
[ 5 ].Name
= TKGet( TK_OLEOptimization
);
482 aRet
[ 5 ].Value
= Any( rSettings
.mbOLEOptimization
);
483 aRet
[ 6 ].Name
= TKGet( TK_OLEOptimizationType
);
484 aRet
[ 6 ].Value
= Any( rSettings
.mnOLEOptimizationType
);
485 aRet
[ 7 ].Name
= TKGet( TK_DeleteUnusedMasterPages
);
486 aRet
[ 7 ].Value
= Any( rSettings
.mbDeleteUnusedMasterPages
);
487 aRet
[ 8 ].Name
= TKGet( TK_DeleteHiddenSlides
);
488 aRet
[ 8 ].Value
= Any( rSettings
.mbDeleteHiddenSlides
);
489 aRet
[ 9 ].Name
= TKGet( TK_DeleteNotesPages
);
490 aRet
[ 9 ].Value
= Any( rSettings
.mbDeleteNotesPages
);
491 aRet
[ 10].Name
= TKGet( TK_CustomShowName
);
492 aRet
[ 10].Value
= Any( rSettings
.maCustomShowName
);
493 aRet
[ 11].Name
= TKGet( TK_SaveAsURL
);
494 aRet
[ 11].Value
= Any( rSettings
.maSaveAsURL
);
495 aRet
[ 12].Name
= TKGet( TK_FilterName
);
496 aRet
[ 12].Value
= Any( rSettings
.maFilterName
);
497 aRet
[ 13].Name
= TKGet( TK_OpenNewDocument
);
498 aRet
[ 13].Value
= Any( rSettings
.mbOpenNewDocument
);
499 aRet
[ 14].Name
= TKGet( TK_EstimatedFileSize
);
500 aRet
[ 14].Value
= Any( rSettings
.mnEstimatedFileSize
);
504 std::vector
< OptimizerSettings
>::iterator
ConfigurationAccess::GetOptimizerSettingsByName( const OUString
& rName
)
506 std::vector
< OptimizerSettings
>::iterator
aIter( maSettings
.begin() + 1 );
507 const std::vector
< OptimizerSettings
>::const_iterator
aEnd( maSettings
.end() );
508 for ( ; aIter
!= aEnd
; ++aIter
)
510 if ( aIter
->maName
== rName
)
516 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */