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>
30 #include <osl/diagnose.h>
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 OUString
GetPathToConfigurationRoot()
39 return OUString("org.openoffice.Office.PresentationMinimizer");
42 void OptimizerSettings::LoadSettingsFromConfiguration( const Reference
< XNameAccess
>& rSettings
)
46 const Sequence
< OUString
> aElements( rSettings
->getElementNames() );
47 for ( int i
= 0; i
< aElements
.getLength(); i
++ )
51 const OUString
aPropertyName( aElements
[ i
] );
52 Any
aValue( rSettings
->getByName( aPropertyName
) );
53 switch( TKGet( aPropertyName
) )
55 case TK_Name
: aValue
>>= maName
; break;
56 case TK_JPEGCompression
: aValue
>>= mbJPEGCompression
; break;
57 case TK_JPEGQuality
: aValue
>>= mnJPEGQuality
; break;
58 case TK_RemoveCropArea
: aValue
>>= mbRemoveCropArea
; break;
59 case TK_ImageResolution
: aValue
>>= mnImageResolution
; break;
60 case TK_EmbedLinkedGraphics
: aValue
>>= mbEmbedLinkedGraphics
; break;
61 case TK_OLEOptimization
: aValue
>>= mbOLEOptimization
; break;
62 case TK_OLEOptimizationType
: aValue
>>= mnOLEOptimizationType
; break;
63 case TK_DeleteUnusedMasterPages
: aValue
>>= mbDeleteUnusedMasterPages
; break;
64 case TK_DeleteHiddenSlides
: aValue
>>= mbDeleteHiddenSlides
; break;
65 case TK_DeleteNotesPages
: aValue
>>= mbDeleteNotesPages
;break;
66 case TK_SaveAs
: aValue
>>= mbSaveAs
; break;
67 // case TK_SaveAsURL : aValue >>= maSaveAsURL; break; // URL is not saved to configuration
68 // case TK_FilterName : aValue >>= maFilterName; break; // URL is not saved to configuration
69 case TK_OpenNewDocument
: aValue
>>= mbOpenNewDocument
; break;
73 catch (const Exception
&)
80 void OptimizerSettings::SaveSettingsToConfiguration( const Reference
< XNameReplace
>& rSettings
)
86 OUString("JPEGCompression"),
87 OUString("JPEGQuality"),
88 OUString("RemoveCropArea"),
89 OUString("ImageResolution"),
90 OUString("EmbedLinkedGraphics"),
91 OUString("OLEOptimization"),
92 OUString("OLEOptimizationType"),
93 OUString("DeleteUnusedMasterPages"),
94 OUString("DeleteHiddenSlides"),
95 OUString("DeleteNotesPages"),
97 // OUString("SaveAsURL"),
98 // OUString("FilterName"),
99 OUString("OpenNewDocument") };
103 Any( mbJPEGCompression
),
104 Any( mnJPEGQuality
),
105 Any( mbRemoveCropArea
),
106 Any( mnImageResolution
),
107 Any( mbEmbedLinkedGraphics
),
108 Any( mbOLEOptimization
),
109 Any( mnOLEOptimizationType
),
110 Any( mbDeleteUnusedMasterPages
),
111 Any( mbDeleteHiddenSlides
),
112 Any( mbDeleteNotesPages
),
114 // Any( maSaveAsURL ),
115 // Any( maFilterName ),
116 Any( mbOpenNewDocument
) };
118 sal_Int32 i
, nCount
= SAL_N_ELEMENTS( pNames
);
120 for ( i
= 0; i
< nCount
; i
++ )
124 rSettings
->replaceByName( pNames
[ i
], pValues
[ i
] );
126 catch (const Exception
&)
133 bool OptimizerSettings::operator==( const OptimizerSettings
& rOptimizerSettings
) const
135 return ( rOptimizerSettings
.mbJPEGCompression
== mbJPEGCompression
)
136 && ( rOptimizerSettings
.mnJPEGQuality
== mnJPEGQuality
)
137 && ( rOptimizerSettings
.mbRemoveCropArea
== mbRemoveCropArea
)
138 && ( rOptimizerSettings
.mnImageResolution
== mnImageResolution
)
139 && ( rOptimizerSettings
.mbEmbedLinkedGraphics
== mbEmbedLinkedGraphics
)
140 && ( rOptimizerSettings
.mbOLEOptimization
== mbOLEOptimization
)
141 && ( rOptimizerSettings
.mnOLEOptimizationType
== mnOLEOptimizationType
)
142 && ( rOptimizerSettings
.mbDeleteUnusedMasterPages
== mbDeleteUnusedMasterPages
)
143 && ( rOptimizerSettings
.mbDeleteHiddenSlides
== mbDeleteHiddenSlides
)
144 && ( rOptimizerSettings
.mbDeleteNotesPages
== mbDeleteNotesPages
);
145 // && ( rOptimizerSettings.mbOpenNewDocument == mbOpenNewDocument );
149 ConfigurationAccess::ConfigurationAccess( const Reference
< uno::XComponentContext
>& rxContext
, OptimizerSettings
* pDefaultSettings
) :
150 mxContext( rxContext
)
153 maSettings
.push_back( pDefaultSettings
?
154 *pDefaultSettings
: OptimizerSettings() );
155 maSettings
.back().maName
= "LastUsedSettings";
157 maInitialSettings
= maSettings
;
160 ConfigurationAccess::~ConfigurationAccess()
164 OUString
ConfigurationAccess::getString( const PPPOptimizerTokenEnum eToken
) const
166 std::map
< PPPOptimizerTokenEnum
, OUString
, Compare
>::const_iterator
aIter( maStrings
.find( eToken
) );
167 return aIter
!= maStrings
.end() ? ((*aIter
).second
) : OUString();
170 void ConfigurationAccess::LoadStrings()
176 Reference
< XInterface
> xRoot( OpenConfiguration( true ) );
179 Reference
< container::XNameAccess
> xSet( GetConfigurationNode( xRoot
, "Strings" ), UNO_QUERY
);
182 const Sequence
< OUString
> aElements( xSet
->getElementNames() );
183 for ( int i
= 0; i
< aElements
.getLength(); i
++ )
187 OUString aString
, aPropertyName( aElements
[ i
] );
188 if ( xSet
->getByName( aPropertyName
) >>= aString
)
189 maStrings
[ TKGet( aPropertyName
) ] = aString
;
191 catch (const Exception
&)
199 catch (const Exception
&)
204 void ConfigurationAccess::LoadConfiguration()
210 Reference
< XInterface
> xRoot( OpenConfiguration( true ) );
213 Reference
< container::XNameAccess
> xSet( GetConfigurationNode( xRoot
, "LastUsedSettings" ), UNO_QUERY
);
216 OptimizerSettings
& rCurrent( maSettings
.front() );
217 rCurrent
.LoadSettingsFromConfiguration( xSet
);
219 xSet
= Reference
< container::XNameAccess
>( GetConfigurationNode( xRoot
, "Settings/Templates" ), UNO_QUERY
);
222 const Sequence
< OUString
> aElements( xSet
->getElementNames() );
223 for ( int i
= 0; i
< aElements
.getLength(); i
++ )
227 OUString
aPath( "Settings/Templates/" + aElements
[ i
] );
228 Reference
< container::XNameAccess
> xTemplates( GetConfigurationNode( xRoot
, aPath
), UNO_QUERY
);
229 if ( xTemplates
.is() )
231 maSettings
.push_back( OptimizerSettings() );
232 maSettings
.back().LoadSettingsFromConfiguration( xTemplates
);
235 catch (const Exception
&)
243 catch (const Exception
&)
248 void ConfigurationAccess::SaveConfiguration()
256 Reference
<util::XChangesBatch
> xRoot( OpenConfiguration( false ), UNO_QUERY_THROW
);
258 // storing the last used settings
259 Reference
< container::XNameReplace
> xSet( GetConfigurationNode( xRoot
, "LastUsedSettings" ), UNO_QUERY_THROW
);
260 OptimizerSettings
& rCurrent( maSettings
.front() );
261 rCurrent
.SaveSettingsToConfiguration( xSet
);
263 // updating template elements
264 xSet
= Reference
< container::XNameReplace
>( GetConfigurationNode( xRoot
, "Settings/Templates" ), UNO_QUERY_THROW
);
265 Reference
< container::XNameContainer
> xNameContainer( xSet
, UNO_QUERY_THROW
);
267 const Sequence
< OUString
> aElements( xSet
->getElementNames() );
268 for( i
= 0; i
< aElements
.getLength(); i
++ )
269 xNameContainer
->removeByName( aElements
[ i
] );
271 for( k
= 1; k
< maSettings
.size(); k
++ )
273 OptimizerSettings
& rSettings( maSettings
[ k
] );
274 OUString
aElementName( "Template" + OUString::number( k
) );
275 Reference
< lang::XSingleServiceFactory
> xChildFactory ( xSet
, UNO_QUERY_THROW
);
276 Reference
< container::XNameReplace
> xChild( xChildFactory
->createInstance(), UNO_QUERY_THROW
);
277 xNameContainer
->insertByName( aElementName
, Any( xChild
) );
279 OUString
aPath( "Settings/Templates/" + aElementName
);
280 Reference
< container::XNameReplace
> xTemplates( GetConfigurationNode( xRoot
, aPath
), UNO_QUERY
);
281 rSettings
.SaveSettingsToConfiguration( xTemplates
);
283 xRoot
->commitChanges();
287 catch (const Exception
&)
292 Reference
< XInterface
> ConfigurationAccess::OpenConfiguration( bool bReadOnly
)
294 Reference
< XInterface
> xRoot
;
297 Reference
< lang::XMultiServiceFactory
> xProvider
= configuration::theDefaultProvider::get( mxContext
);
298 Sequence
< Any
> aCreationArguments( 2 );
299 aCreationArguments
[0] = makeAny( PropertyValue(
300 OUString( "nodepath" ), 0,
301 makeAny( GetPathToConfigurationRoot() ),
302 PropertyState_DIRECT_VALUE
) );
303 aCreationArguments
[1] = makeAny(beans::PropertyValue(
304 OUString( "lazywrite" ), 0, makeAny( true ),
305 PropertyState_DIRECT_VALUE
) );
306 OUString sAccessService
;
308 sAccessService
= "com.sun.star.configuration.ConfigurationAccess";
311 "com.sun.star.configuration.ConfigurationUpdateAccess";
313 xRoot
= xProvider
->createInstanceWithArguments(
314 sAccessService
, aCreationArguments
);
316 catch (const Exception
&)
322 Reference
< XInterface
> ConfigurationAccess::GetConfigurationNode(
323 const Reference
< XInterface
>& xRoot
,
324 const OUString
& sPathToNode
)
326 Reference
< XInterface
> xNode
;
329 if ( sPathToNode
.isEmpty() )
333 Reference
< XHierarchicalNameAccess
> xHierarchy( xRoot
, UNO_QUERY
);
334 if ( xHierarchy
.is() )
336 xHierarchy
->getByHierarchicalName( sPathToNode
) >>= xNode
;
340 catch (const Exception
& rException
)
342 OSL_TRACE ("caught exception while getting configuration node %s: %s",
343 OUStringToOString(sPathToNode
,
344 RTL_TEXTENCODING_UTF8
).getStr(),
345 OUStringToOString(rException
.Message
,
346 RTL_TEXTENCODING_UTF8
).getStr());
352 com::sun::star::uno::Any
ConfigurationAccess::GetConfigProperty( const PPPOptimizerTokenEnum ePropertyToken
) const
355 const OptimizerSettings
& rSettings( maSettings
.front() );
358 switch( ePropertyToken
)
360 case TK_Name
: aRetValue
<<= rSettings
.maName
; break;
361 case TK_JPEGCompression
: aRetValue
<<= rSettings
.mbJPEGCompression
; break;
362 case TK_JPEGQuality
: aRetValue
<<= rSettings
.mnJPEGQuality
; break;
363 case TK_RemoveCropArea
: aRetValue
<<= rSettings
.mbRemoveCropArea
; break;
364 case TK_ImageResolution
: aRetValue
<<= rSettings
.mnImageResolution
; break;
365 case TK_EmbedLinkedGraphics
: aRetValue
<<= rSettings
.mbEmbedLinkedGraphics
; break;
366 case TK_OLEOptimization
: aRetValue
<<= rSettings
.mbOLEOptimization
; break;
367 case TK_OLEOptimizationType
: aRetValue
<<= rSettings
.mnOLEOptimizationType
; break;
368 case TK_DeleteUnusedMasterPages
: aRetValue
<<= rSettings
.mbDeleteUnusedMasterPages
; break;
369 case TK_DeleteHiddenSlides
: aRetValue
<<= rSettings
.mbDeleteHiddenSlides
; break;
370 case TK_DeleteNotesPages
: aRetValue
<<= rSettings
.mbDeleteNotesPages
; break;
371 case TK_SaveAs
: aRetValue
<<= rSettings
.mbSaveAs
; break;
372 case TK_SaveAsURL
: aRetValue
<<= rSettings
.maSaveAsURL
; break;
373 case TK_FilterName
: aRetValue
<<= rSettings
.maFilterName
; break;
374 case TK_OpenNewDocument
: aRetValue
<<= rSettings
.mbOpenNewDocument
; break;
375 case TK_EstimatedFileSize
: aRetValue
<<= rSettings
.mnEstimatedFileSize
; break;
380 catch (const Exception
&)
386 void ConfigurationAccess::SetConfigProperty( const PPPOptimizerTokenEnum ePropertyToken
, const com::sun::star::uno::Any
& rValue
)
388 OptimizerSettings
& rSettings( maSettings
.front() );
391 switch( ePropertyToken
)
393 case TK_Name
: rValue
>>= rSettings
.maName
; break;
394 case TK_JPEGCompression
: rValue
>>= rSettings
.mbJPEGCompression
; break;
395 case TK_JPEGQuality
: rValue
>>= rSettings
.mnJPEGQuality
; break;
396 case TK_RemoveCropArea
: rValue
>>= rSettings
.mbRemoveCropArea
; break;
397 case TK_ImageResolution
: rValue
>>= rSettings
.mnImageResolution
; break;
398 case TK_EmbedLinkedGraphics
: rValue
>>= rSettings
.mbEmbedLinkedGraphics
; break;
399 case TK_OLEOptimization
: rValue
>>= rSettings
.mbOLEOptimization
; break;
400 case TK_OLEOptimizationType
: rValue
>>= rSettings
.mnOLEOptimizationType
; break;
401 case TK_DeleteUnusedMasterPages
: rValue
>>= rSettings
.mbDeleteUnusedMasterPages
; break;
402 case TK_DeleteHiddenSlides
: rValue
>>= rSettings
.mbDeleteHiddenSlides
; break;
403 case TK_DeleteNotesPages
: rValue
>>= rSettings
.mbDeleteNotesPages
; break;
404 case TK_CustomShowName
: rValue
>>= rSettings
.maCustomShowName
; break;
405 case TK_SaveAs
: rValue
>>= rSettings
.mbSaveAs
; break;
406 case TK_SaveAsURL
: rValue
>>= rSettings
.maSaveAsURL
; break;
407 case TK_FilterName
: rValue
>>= rSettings
.maFilterName
; break;
408 case TK_OpenNewDocument
: rValue
>>= rSettings
.mbOpenNewDocument
; break;
409 case TK_EstimatedFileSize
: rValue
>>= rSettings
.mnEstimatedFileSize
; break;
414 catch (const Exception
&)
419 bool ConfigurationAccess::GetConfigProperty( const PPPOptimizerTokenEnum ePropertyToken
, const bool bDefault
) const
421 bool bRetValue
= bDefault
;
422 if ( ! ( GetConfigProperty( ePropertyToken
) >>= bRetValue
) )
423 bRetValue
= bDefault
;
427 sal_Int16
ConfigurationAccess::GetConfigProperty( const PPPOptimizerTokenEnum ePropertyToken
, const sal_Int16 nDefault
) const
429 sal_Int16 nRetValue
= nDefault
;
430 if ( ! ( GetConfigProperty( ePropertyToken
) >>= nRetValue
) )
431 nRetValue
= nDefault
;
435 sal_Int32
ConfigurationAccess::GetConfigProperty( const PPPOptimizerTokenEnum ePropertyToken
, const sal_Int32 nDefault
) const
437 sal_Int32 nRetValue
= nDefault
;
438 if ( ! ( GetConfigProperty( ePropertyToken
) >>= nRetValue
) )
439 nRetValue
= nDefault
;
443 Sequence
< PropertyValue
> ConfigurationAccess::GetConfigurationSequence()
445 Sequence
< PropertyValue
> aRet( 15 );
446 OptimizerSettings
& rSettings( maSettings
.front() );
447 aRet
[ 0 ].Name
= "JPEGCompression";
448 aRet
[ 0 ].Value
= Any( rSettings
.mbJPEGCompression
);
449 aRet
[ 1 ].Name
= "JPEGQuality";
450 aRet
[ 1 ].Value
= Any( rSettings
.mnJPEGQuality
);
451 aRet
[ 2 ].Name
= "RemoveCropArea";
452 aRet
[ 2 ].Value
= Any( rSettings
.mbRemoveCropArea
);
453 aRet
[ 3 ].Name
= "ImageResolution";
454 aRet
[ 3 ].Value
= Any( rSettings
.mnImageResolution
);
455 aRet
[ 4 ].Name
= "EmbedLinkedGraphics";
456 aRet
[ 4 ].Value
= Any( rSettings
.mbEmbedLinkedGraphics
);
457 aRet
[ 5 ].Name
= "OLEOptimization";
458 aRet
[ 5 ].Value
= Any( rSettings
.mbOLEOptimization
);
459 aRet
[ 6 ].Name
= "OLEOptimizationType";
460 aRet
[ 6 ].Value
= Any( rSettings
.mnOLEOptimizationType
);
461 aRet
[ 7 ].Name
= "DeleteUnusedMasterPages";
462 aRet
[ 7 ].Value
= Any( rSettings
.mbDeleteUnusedMasterPages
);
463 aRet
[ 8 ].Name
= "DeleteHiddenSlides";
464 aRet
[ 8 ].Value
= Any( rSettings
.mbDeleteHiddenSlides
);
465 aRet
[ 9 ].Name
= "DeleteNotesPages";
466 aRet
[ 9 ].Value
= Any( rSettings
.mbDeleteNotesPages
);
467 aRet
[ 10].Name
= "CustomShowName";
468 aRet
[ 10].Value
= Any( rSettings
.maCustomShowName
);
469 aRet
[ 11].Name
= "SaveAsURL";
470 aRet
[ 11].Value
= Any( rSettings
.maSaveAsURL
);
471 aRet
[ 12].Name
= "FilterName";
472 aRet
[ 12].Value
= Any( rSettings
.maFilterName
);
473 aRet
[ 13].Name
= "OpenNewDocument";
474 aRet
[ 13].Value
= Any( rSettings
.mbOpenNewDocument
);
475 aRet
[ 14].Name
= "EstimatedFileSize";
476 aRet
[ 14].Value
= Any( rSettings
.mnEstimatedFileSize
);
480 std::vector
< OptimizerSettings
>::iterator
ConfigurationAccess::GetOptimizerSettingsByName( const OUString
& rName
)
482 std::vector
< OptimizerSettings
>::iterator
aIter( maSettings
.begin() + 1 );
483 const std::vector
< OptimizerSettings
>::const_iterator
aEnd( maSettings
.end() );
484 for ( ; aIter
!= aEnd
; ++aIter
)
486 if ( aIter
->maName
== rName
)
492 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */