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/container/XHierarchicalNameAccess.hpp>
23 #include <com/sun/star/configuration/theDefaultProvider.hpp>
24 #include <com/sun/star/util/XChangesBatch.hpp>
25 #include <com/sun/star/container/XNameContainer.hpp>
26 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
27 #include <comphelper/propertysequence.hxx>
28 #include <comphelper/propertyvalue.hxx>
29 #include <sal/macros.h>
30 #include <sal/log.hxx>
31 #include <comphelper/diagnose_ex.hxx>
33 using namespace ::com::sun::star
;
34 using namespace ::com::sun::star::uno
;
35 using namespace ::com::sun::star::beans
;
36 using namespace ::com::sun::star::container
;
38 static OUString
GetPathToConfigurationRoot()
40 return u
"org.openoffice.Office.PresentationMinimizer"_ustr
;
43 void OptimizerSettings::LoadSettingsFromConfiguration( const Reference
< XNameAccess
>& rSettings
)
45 if ( !rSettings
.is() )
48 const Sequence
< OUString
> aElements( rSettings
->getElementNames() );
49 for ( const OUString
& aPropertyName
: aElements
)
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
&)
80 void OptimizerSettings::SaveSettingsToConfiguration( const Reference
< XNameReplace
>& rSettings
)
82 if ( !rSettings
.is() )
87 u
"JPEGCompression"_ustr
,
89 u
"RemoveCropArea"_ustr
,
90 u
"ImageResolution"_ustr
,
91 u
"EmbedLinkedGraphics"_ustr
,
92 u
"OLEOptimization"_ustr
,
93 u
"OLEOptimizationType"_ustr
,
94 u
"DeleteUnusedMasterPages"_ustr
,
95 u
"DeleteHiddenSlides"_ustr
,
96 u
"DeleteNotesPages"_ustr
,
98 // OUString("SaveAsURL"),
99 // OUString("FilterName"),
100 u
"OpenNewDocument"_ustr
};
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 for ( int i
= 0; i
< int(SAL_N_ELEMENTS( pNames
)); i
++ )
123 rSettings
->replaceByName( pNames
[ i
], pValues
[ i
] );
125 catch (const Exception
&)
131 bool OptimizerSettings::operator==( const OptimizerSettings
& rOptimizerSettings
) const
133 return ( rOptimizerSettings
.mbJPEGCompression
== mbJPEGCompression
)
134 && ( rOptimizerSettings
.mnJPEGQuality
== mnJPEGQuality
)
135 && ( rOptimizerSettings
.mbRemoveCropArea
== mbRemoveCropArea
)
136 && ( rOptimizerSettings
.mnImageResolution
== mnImageResolution
)
137 && ( rOptimizerSettings
.mbEmbedLinkedGraphics
== mbEmbedLinkedGraphics
)
138 && ( rOptimizerSettings
.mbOLEOptimization
== mbOLEOptimization
)
139 && ( rOptimizerSettings
.mnOLEOptimizationType
== mnOLEOptimizationType
)
140 && ( rOptimizerSettings
.mbDeleteUnusedMasterPages
== mbDeleteUnusedMasterPages
)
141 && ( rOptimizerSettings
.mbDeleteHiddenSlides
== mbDeleteHiddenSlides
)
142 && ( rOptimizerSettings
.mbDeleteNotesPages
== mbDeleteNotesPages
);
145 ConfigurationAccess::ConfigurationAccess( const Reference
< uno::XComponentContext
>& rxContext
) :
146 mxContext( rxContext
)
149 maSettings
.emplace_back( );
150 maSettings
.back().maName
= "LastUsedSettings";
154 ConfigurationAccess::~ConfigurationAccess()
158 OUString
ConfigurationAccess::getString( const PPPOptimizerTokenEnum eToken
) const
160 std::map
< PPPOptimizerTokenEnum
, OUString
>::const_iterator
aIter( maStrings
.find( eToken
) );
161 return aIter
!= maStrings
.end() ? ((*aIter
).second
) : OUString();
164 void ConfigurationAccess::LoadStrings()
170 Reference
< XInterface
> xRoot( OpenConfiguration( true ) );
173 Reference
< container::XNameAccess
> xSet( GetConfigurationNode( xRoot
, u
"Strings"_ustr
), UNO_QUERY
);
176 const Sequence
< OUString
> aElements( xSet
->getElementNames() );
177 for ( const auto& rElement
: aElements
)
181 OUString aString
, aPropertyName( rElement
);
182 if ( xSet
->getByName( aPropertyName
) >>= aString
)
183 maStrings
[ TKGet( aPropertyName
) ] = aString
;
185 catch (const Exception
&)
193 catch (const Exception
&)
198 void ConfigurationAccess::LoadConfiguration()
204 Reference
< XInterface
> xRoot( OpenConfiguration( true ) );
207 Reference
< container::XNameAccess
> xSet( GetConfigurationNode( xRoot
, u
"LastUsedSettings"_ustr
), UNO_QUERY
);
210 OptimizerSettings
& rCurrent( maSettings
.front() );
211 rCurrent
.LoadSettingsFromConfiguration( xSet
);
213 xSet
.set( GetConfigurationNode( xRoot
, u
"Settings/Templates"_ustr
), UNO_QUERY
);
216 const Sequence
< OUString
> aElements( xSet
->getElementNames() );
217 for ( const auto& rElement
: aElements
)
221 OUString
aPath( "Settings/Templates/" + rElement
);
222 Reference
< container::XNameAccess
> xTemplates( GetConfigurationNode( xRoot
, aPath
), UNO_QUERY
);
223 if ( xTemplates
.is() )
225 maSettings
.emplace_back( );
226 maSettings
.back().LoadSettingsFromConfiguration( xTemplates
);
229 catch (const Exception
&)
237 catch (const Exception
&)
242 void ConfigurationAccess::SaveConfiguration()
248 Reference
<util::XChangesBatch
> xRoot( OpenConfiguration( false ), UNO_QUERY_THROW
);
250 // storing the last used settings
251 Reference
< container::XNameReplace
> xSet( GetConfigurationNode( xRoot
, u
"LastUsedSettings"_ustr
), UNO_QUERY_THROW
);
252 OptimizerSettings
& rCurrent( maSettings
.front() );
253 rCurrent
.SaveSettingsToConfiguration( xSet
);
255 // updating template elements
256 xSet
.set( GetConfigurationNode( xRoot
, u
"Settings/Templates"_ustr
), UNO_QUERY_THROW
);
257 Reference
< container::XNameContainer
> xNameContainer( xSet
, UNO_QUERY_THROW
);
259 const Sequence
< OUString
> aElements( xSet
->getElementNames() );
260 for( const auto& rElement
: aElements
)
261 xNameContainer
->removeByName( rElement
);
263 for( std::vector
<OptimizerSettings
>::size_type k
= 1; k
< maSettings
.size(); k
++ )
265 OptimizerSettings
& rSettings( maSettings
[ k
] );
266 OUString
aElementName( "Template" + OUString::number( k
) );
267 Reference
< lang::XSingleServiceFactory
> xChildFactory ( xSet
, UNO_QUERY_THROW
);
268 Reference
< container::XNameReplace
> xChild( xChildFactory
->createInstance(), UNO_QUERY_THROW
);
269 xNameContainer
->insertByName( aElementName
, Any( xChild
) );
271 OUString
aPath( "Settings/Templates/" + aElementName
);
272 Reference
< container::XNameReplace
> xTemplates( GetConfigurationNode( xRoot
, aPath
), UNO_QUERY
);
273 rSettings
.SaveSettingsToConfiguration( xTemplates
);
275 xRoot
->commitChanges();
279 catch (const Exception
&)
284 Reference
< XInterface
> ConfigurationAccess::OpenConfiguration( bool bReadOnly
)
286 Reference
< XInterface
> xRoot
;
289 Reference
< lang::XMultiServiceFactory
> xProvider
= configuration::theDefaultProvider::get( mxContext
);
290 uno::Sequence
<uno::Any
> aCreationArguments(comphelper::InitAnyPropertySequence(
292 {"nodepath", uno::Any(GetPathToConfigurationRoot())}
294 OUString sAccessService
;
296 sAccessService
= "com.sun.star.configuration.ConfigurationAccess";
299 "com.sun.star.configuration.ConfigurationUpdateAccess";
301 xRoot
= xProvider
->createInstanceWithArguments(
302 sAccessService
, aCreationArguments
);
304 catch (const Exception
&)
310 Reference
< XInterface
> ConfigurationAccess::GetConfigurationNode(
311 const Reference
< XInterface
>& xRoot
,
312 const OUString
& sPathToNode
)
314 Reference
< XInterface
> xNode
;
317 if ( sPathToNode
.isEmpty() )
321 Reference
< XHierarchicalNameAccess
> xHierarchy( xRoot
, UNO_QUERY
);
322 if ( xHierarchy
.is() )
324 xHierarchy
->getByHierarchicalName( sPathToNode
) >>= xNode
;
328 catch (const Exception
&)
330 TOOLS_WARN_EXCEPTION("sdext.minimizer", "caught exception while getting configuration node "
336 css::uno::Any
ConfigurationAccess::GetConfigProperty( const PPPOptimizerTokenEnum ePropertyToken
) const
339 const OptimizerSettings
& rSettings( maSettings
.front() );
342 switch( ePropertyToken
)
344 case TK_Name
: aRetValue
<<= rSettings
.maName
; break;
345 case TK_JPEGCompression
: aRetValue
<<= rSettings
.mbJPEGCompression
; break;
346 case TK_JPEGQuality
: aRetValue
<<= rSettings
.mnJPEGQuality
; break;
347 case TK_RemoveCropArea
: aRetValue
<<= rSettings
.mbRemoveCropArea
; break;
348 case TK_ImageResolution
: aRetValue
<<= rSettings
.mnImageResolution
; break;
349 case TK_EmbedLinkedGraphics
: aRetValue
<<= rSettings
.mbEmbedLinkedGraphics
; break;
350 case TK_OLEOptimization
: aRetValue
<<= rSettings
.mbOLEOptimization
; break;
351 case TK_OLEOptimizationType
: aRetValue
<<= rSettings
.mnOLEOptimizationType
; break;
352 case TK_DeleteUnusedMasterPages
: aRetValue
<<= rSettings
.mbDeleteUnusedMasterPages
; break;
353 case TK_DeleteHiddenSlides
: aRetValue
<<= rSettings
.mbDeleteHiddenSlides
; break;
354 case TK_DeleteNotesPages
: aRetValue
<<= rSettings
.mbDeleteNotesPages
; break;
355 case TK_SaveAs
: aRetValue
<<= rSettings
.mbSaveAs
; break;
356 case TK_SaveAsURL
: aRetValue
<<= rSettings
.maSaveAsURL
; break;
357 case TK_FilterName
: aRetValue
<<= rSettings
.maFilterName
; break;
358 case TK_OpenNewDocument
: aRetValue
<<= rSettings
.mbOpenNewDocument
; break;
359 case TK_EstimatedFileSize
: aRetValue
<<= rSettings
.mnEstimatedFileSize
; break;
364 catch (const Exception
&)
370 void ConfigurationAccess::SetConfigProperty( const PPPOptimizerTokenEnum ePropertyToken
, const css::uno::Any
& rValue
)
372 OptimizerSettings
& rSettings( maSettings
.front() );
375 switch( ePropertyToken
)
377 case TK_Name
: rValue
>>= rSettings
.maName
; break;
378 case TK_JPEGCompression
: rValue
>>= rSettings
.mbJPEGCompression
; break;
379 case TK_JPEGQuality
: rValue
>>= rSettings
.mnJPEGQuality
; break;
380 case TK_RemoveCropArea
: rValue
>>= rSettings
.mbRemoveCropArea
; break;
381 case TK_ImageResolution
: rValue
>>= rSettings
.mnImageResolution
; break;
382 case TK_EmbedLinkedGraphics
: rValue
>>= rSettings
.mbEmbedLinkedGraphics
; break;
383 case TK_OLEOptimization
: rValue
>>= rSettings
.mbOLEOptimization
; break;
384 case TK_OLEOptimizationType
: rValue
>>= rSettings
.mnOLEOptimizationType
; break;
385 case TK_DeleteUnusedMasterPages
: rValue
>>= rSettings
.mbDeleteUnusedMasterPages
; break;
386 case TK_DeleteHiddenSlides
: rValue
>>= rSettings
.mbDeleteHiddenSlides
; break;
387 case TK_DeleteNotesPages
: rValue
>>= rSettings
.mbDeleteNotesPages
; break;
388 case TK_CustomShowName
: rValue
>>= rSettings
.maCustomShowName
; break;
389 case TK_SaveAs
: rValue
>>= rSettings
.mbSaveAs
; break;
390 case TK_SaveAsURL
: rValue
>>= rSettings
.maSaveAsURL
; break;
391 case TK_FilterName
: rValue
>>= rSettings
.maFilterName
; break;
392 case TK_OpenNewDocument
: rValue
>>= rSettings
.mbOpenNewDocument
; break;
393 case TK_EstimatedFileSize
: rValue
>>= rSettings
.mnEstimatedFileSize
; break;
398 catch (const Exception
&)
403 bool ConfigurationAccess::GetConfigProperty( const PPPOptimizerTokenEnum ePropertyToken
, const bool bDefault
) const
405 bool bRetValue
= bDefault
;
406 if ( ! ( GetConfigProperty( ePropertyToken
) >>= bRetValue
) )
407 bRetValue
= bDefault
;
411 sal_Int16
ConfigurationAccess::GetConfigProperty( const PPPOptimizerTokenEnum ePropertyToken
, const sal_Int16 nDefault
) const
413 sal_Int16 nRetValue
= nDefault
;
414 if ( ! ( GetConfigProperty( ePropertyToken
) >>= nRetValue
) )
415 nRetValue
= nDefault
;
419 sal_Int32
ConfigurationAccess::GetConfigProperty( const PPPOptimizerTokenEnum ePropertyToken
, const sal_Int32 nDefault
) const
421 sal_Int32 nRetValue
= nDefault
;
422 if ( ! ( GetConfigProperty( ePropertyToken
) >>= nRetValue
) )
423 nRetValue
= nDefault
;
427 Sequence
< PropertyValue
> ConfigurationAccess::GetConfigurationSequence()
429 OptimizerSettings
& rSettings( maSettings
.front() );
430 Sequence
< PropertyValue
> aRet
{
431 comphelper::makePropertyValue(u
"JPEGCompression"_ustr
, rSettings
.mbJPEGCompression
),
432 comphelper::makePropertyValue(u
"JPEGQuality"_ustr
, rSettings
.mnJPEGQuality
),
433 comphelper::makePropertyValue(u
"RemoveCropArea"_ustr
, rSettings
.mbRemoveCropArea
),
434 comphelper::makePropertyValue(u
"ImageResolution"_ustr
, rSettings
.mnImageResolution
),
435 comphelper::makePropertyValue(u
"EmbedLinkedGraphics"_ustr
, rSettings
.mbEmbedLinkedGraphics
),
436 comphelper::makePropertyValue(u
"OLEOptimization"_ustr
, rSettings
.mbOLEOptimization
),
437 comphelper::makePropertyValue(u
"OLEOptimizationType"_ustr
, rSettings
.mnOLEOptimizationType
),
438 comphelper::makePropertyValue(u
"DeleteUnusedMasterPages"_ustr
, rSettings
.mbDeleteUnusedMasterPages
),
439 comphelper::makePropertyValue(u
"DeleteHiddenSlides"_ustr
, rSettings
.mbDeleteHiddenSlides
),
440 comphelper::makePropertyValue(u
"DeleteNotesPages"_ustr
, rSettings
.mbDeleteNotesPages
),
441 comphelper::makePropertyValue(u
"CustomShowName"_ustr
, rSettings
.maCustomShowName
),
442 comphelper::makePropertyValue(u
"SaveAsURL"_ustr
, rSettings
.maSaveAsURL
),
443 comphelper::makePropertyValue(u
"FilterName"_ustr
, rSettings
.maFilterName
),
444 comphelper::makePropertyValue(u
"OpenNewDocument"_ustr
, rSettings
.mbOpenNewDocument
),
445 comphelper::makePropertyValue(u
"EstimatedFileSize"_ustr
, rSettings
.mnEstimatedFileSize
)
450 std::vector
< OptimizerSettings
>::iterator
ConfigurationAccess::GetOptimizerSettingsByName( const OUString
& rName
)
452 return std::find_if(maSettings
.begin() + 1, maSettings
.end(),
453 [&rName
](const OptimizerSettings
& rSettings
) { return rSettings
.maName
== rName
; });
456 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */