vcl: allow for overriding the default PDF rendering resolution
[LibreOffice.git] / sdext / source / minimizer / configurationaccess.cxx
blob0ed089e341a25e9e9785f9e5b72b656abd06f96e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <sal/macros.h>
29 #include <sal/log.hxx>
30 #include <tools/diagnose_ex.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 "org.openoffice.Office.PresentationMinimizer";
42 void OptimizerSettings::LoadSettingsFromConfiguration( const Reference< XNameAccess >& rSettings )
44 if ( !rSettings.is() )
45 return;
47 const Sequence< OUString > aElements( rSettings->getElementNames() );
48 for ( const OUString& aPropertyName : aElements )
50 try
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;
70 default: break;
73 catch (const Exception&)
79 void OptimizerSettings::SaveSettingsToConfiguration( const Reference< XNameReplace >& rSettings )
81 if ( !rSettings.is() )
82 return;
84 OUString pNames[] = {
85 OUString("Name"),
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"),
96 OUString("SaveAs"),
97 // OUString("SaveAsURL"),
98 // OUString("FilterName"),
99 OUString("OpenNewDocument") };
101 Any pValues[] = {
102 Any( maName ),
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 ),
113 Any( mbSaveAs ),
114 // Any( maSaveAsURL ),
115 // Any( maFilterName ),
116 Any( mbOpenNewDocument ) };
118 for ( int i = 0; i < int(SAL_N_ELEMENTS( pNames )); i++ )
122 rSettings->replaceByName( pNames[ i ], pValues[ i ] );
124 catch (const Exception&)
130 bool OptimizerSettings::operator==( const OptimizerSettings& rOptimizerSettings ) const
132 return ( rOptimizerSettings.mbJPEGCompression == mbJPEGCompression )
133 && ( rOptimizerSettings.mnJPEGQuality == mnJPEGQuality )
134 && ( rOptimizerSettings.mbRemoveCropArea == mbRemoveCropArea )
135 && ( rOptimizerSettings.mnImageResolution == mnImageResolution )
136 && ( rOptimizerSettings.mbEmbedLinkedGraphics == mbEmbedLinkedGraphics )
137 && ( rOptimizerSettings.mbOLEOptimization == mbOLEOptimization )
138 && ( rOptimizerSettings.mnOLEOptimizationType == mnOLEOptimizationType )
139 && ( rOptimizerSettings.mbDeleteUnusedMasterPages == mbDeleteUnusedMasterPages )
140 && ( rOptimizerSettings.mbDeleteHiddenSlides == mbDeleteHiddenSlides )
141 && ( rOptimizerSettings.mbDeleteNotesPages == mbDeleteNotesPages );
142 // && ( rOptimizerSettings.mbOpenNewDocument == mbOpenNewDocument );
146 ConfigurationAccess::ConfigurationAccess( const Reference< uno::XComponentContext >& rxContext ) :
147 mxContext( rxContext )
149 LoadStrings();
150 maSettings.emplace_back( );
151 maSettings.back().maName = "LastUsedSettings";
152 LoadConfiguration();
155 ConfigurationAccess::~ConfigurationAccess()
159 OUString ConfigurationAccess::getString( const PPPOptimizerTokenEnum eToken ) const
161 std::map< PPPOptimizerTokenEnum, OUString >::const_iterator aIter( maStrings.find( eToken ) );
162 return aIter != maStrings.end() ? ((*aIter).second) : OUString();
165 void ConfigurationAccess::LoadStrings()
171 Reference< XInterface > xRoot( OpenConfiguration( true ) );
172 if ( !xRoot.is() )
173 break;
174 Reference< container::XNameAccess > xSet( GetConfigurationNode( xRoot, "Strings" ), UNO_QUERY );
175 if ( xSet.is() )
177 const Sequence< OUString > aElements( xSet->getElementNames() );
178 for ( const auto& rElement : aElements )
182 OUString aString, aPropertyName( rElement );
183 if ( xSet->getByName( aPropertyName ) >>= aString )
184 maStrings[ TKGet( aPropertyName ) ] = aString;
186 catch (const Exception&)
192 while( false );
194 catch (const Exception&)
199 void ConfigurationAccess::LoadConfiguration()
205 Reference< XInterface > xRoot( OpenConfiguration( true ) );
206 if ( !xRoot.is() )
207 break;
208 Reference< container::XNameAccess > xSet( GetConfigurationNode( xRoot, "LastUsedSettings" ), UNO_QUERY );
209 if ( xSet.is() )
211 OptimizerSettings& rCurrent( maSettings.front() );
212 rCurrent.LoadSettingsFromConfiguration( xSet );
214 xSet.set( GetConfigurationNode( xRoot, "Settings/Templates" ), UNO_QUERY );
215 if ( xSet.is() )
217 const Sequence< OUString > aElements( xSet->getElementNames() );
218 for ( const auto& rElement : aElements )
222 OUString aPath( "Settings/Templates/" + rElement );
223 Reference< container::XNameAccess > xTemplates( GetConfigurationNode( xRoot, aPath ), UNO_QUERY );
224 if ( xTemplates.is() )
226 maSettings.emplace_back( );
227 maSettings.back().LoadSettingsFromConfiguration( xTemplates );
230 catch (const Exception&)
236 while( false );
238 catch (const Exception&)
243 void ConfigurationAccess::SaveConfiguration()
249 Reference<util::XChangesBatch> xRoot( OpenConfiguration( false ), UNO_QUERY_THROW );
251 // storing the last used settings
252 Reference< container::XNameReplace > xSet( GetConfigurationNode( xRoot, "LastUsedSettings" ), UNO_QUERY_THROW );
253 OptimizerSettings& rCurrent( maSettings.front() );
254 rCurrent.SaveSettingsToConfiguration( xSet );
256 // updating template elements
257 xSet.set( GetConfigurationNode( xRoot, "Settings/Templates" ), UNO_QUERY_THROW );
258 Reference< container::XNameContainer > xNameContainer( xSet, UNO_QUERY_THROW );
260 const Sequence< OUString > aElements( xSet->getElementNames() );
261 for( const auto& rElement : aElements )
262 xNameContainer->removeByName( rElement );
264 for( std::vector<OptimizerSettings>::size_type k = 1; k < maSettings.size(); k++ )
266 OptimizerSettings& rSettings( maSettings[ k ] );
267 OUString aElementName( "Template" + OUString::number( k ) );
268 Reference< lang::XSingleServiceFactory > xChildFactory ( xSet, UNO_QUERY_THROW );
269 Reference< container::XNameReplace > xChild( xChildFactory->createInstance(), UNO_QUERY_THROW );
270 xNameContainer->insertByName( aElementName, Any( xChild ) );
272 OUString aPath( "Settings/Templates/" + aElementName );
273 Reference< container::XNameReplace > xTemplates( GetConfigurationNode( xRoot, aPath ), UNO_QUERY );
274 rSettings.SaveSettingsToConfiguration( xTemplates );
276 xRoot->commitChanges();
278 while( false );
280 catch (const Exception&)
285 Reference< XInterface > ConfigurationAccess::OpenConfiguration( bool bReadOnly )
287 Reference< XInterface > xRoot;
290 Reference< lang::XMultiServiceFactory > xProvider = configuration::theDefaultProvider::get( mxContext );
291 uno::Sequence<uno::Any> aCreationArguments(comphelper::InitAnyPropertySequence(
293 {"nodepath", uno::Any(GetPathToConfigurationRoot())}
294 }));
295 OUString sAccessService;
296 if ( bReadOnly )
297 sAccessService = "com.sun.star.configuration.ConfigurationAccess";
298 else
299 sAccessService =
300 "com.sun.star.configuration.ConfigurationUpdateAccess";
302 xRoot = xProvider->createInstanceWithArguments(
303 sAccessService, aCreationArguments );
305 catch (const Exception&)
308 return xRoot;
311 Reference< XInterface > ConfigurationAccess::GetConfigurationNode(
312 const Reference< XInterface >& xRoot,
313 const OUString& sPathToNode )
315 Reference< XInterface > xNode;
318 if ( sPathToNode.isEmpty() )
319 xNode = xRoot;
320 else
322 Reference< XHierarchicalNameAccess > xHierarchy( xRoot, UNO_QUERY );
323 if ( xHierarchy.is() )
325 xHierarchy->getByHierarchicalName( sPathToNode ) >>= xNode;
329 catch (const Exception&)
331 TOOLS_WARN_EXCEPTION("sdext.minimizer", "caught exception while getting configuration node "
332 << sPathToNode);
334 return xNode;
337 css::uno::Any ConfigurationAccess::GetConfigProperty( const PPPOptimizerTokenEnum ePropertyToken ) const
339 Any aRetValue;
340 const OptimizerSettings& rSettings( maSettings.front() );
343 switch( ePropertyToken )
345 case TK_Name : aRetValue <<= rSettings.maName; break;
346 case TK_JPEGCompression : aRetValue <<= rSettings.mbJPEGCompression; break;
347 case TK_JPEGQuality : aRetValue <<= rSettings.mnJPEGQuality; break;
348 case TK_RemoveCropArea : aRetValue <<= rSettings.mbRemoveCropArea; break;
349 case TK_ImageResolution : aRetValue <<= rSettings.mnImageResolution; break;
350 case TK_EmbedLinkedGraphics : aRetValue <<= rSettings.mbEmbedLinkedGraphics; break;
351 case TK_OLEOptimization : aRetValue <<= rSettings.mbOLEOptimization; break;
352 case TK_OLEOptimizationType : aRetValue <<= rSettings.mnOLEOptimizationType; break;
353 case TK_DeleteUnusedMasterPages : aRetValue <<= rSettings.mbDeleteUnusedMasterPages; break;
354 case TK_DeleteHiddenSlides : aRetValue <<= rSettings.mbDeleteHiddenSlides; break;
355 case TK_DeleteNotesPages : aRetValue <<= rSettings.mbDeleteNotesPages; break;
356 case TK_SaveAs : aRetValue <<= rSettings.mbSaveAs; break;
357 case TK_SaveAsURL : aRetValue <<= rSettings.maSaveAsURL; break;
358 case TK_FilterName : aRetValue <<= rSettings.maFilterName; break;
359 case TK_OpenNewDocument : aRetValue <<= rSettings.mbOpenNewDocument; break;
360 case TK_EstimatedFileSize : aRetValue <<= rSettings.mnEstimatedFileSize; break;
361 default:
362 break;
365 catch (const Exception&)
368 return aRetValue;
371 void ConfigurationAccess::SetConfigProperty( const PPPOptimizerTokenEnum ePropertyToken, const css::uno::Any& rValue )
373 OptimizerSettings& rSettings( maSettings.front() );
376 switch( ePropertyToken )
378 case TK_Name : rValue >>= rSettings.maName; break;
379 case TK_JPEGCompression : rValue >>= rSettings.mbJPEGCompression; break;
380 case TK_JPEGQuality : rValue >>= rSettings.mnJPEGQuality; break;
381 case TK_RemoveCropArea : rValue >>= rSettings.mbRemoveCropArea; break;
382 case TK_ImageResolution : rValue >>= rSettings.mnImageResolution; break;
383 case TK_EmbedLinkedGraphics : rValue >>= rSettings.mbEmbedLinkedGraphics; break;
384 case TK_OLEOptimization : rValue >>= rSettings.mbOLEOptimization; break;
385 case TK_OLEOptimizationType : rValue >>= rSettings.mnOLEOptimizationType; break;
386 case TK_DeleteUnusedMasterPages : rValue >>= rSettings.mbDeleteUnusedMasterPages; break;
387 case TK_DeleteHiddenSlides : rValue >>= rSettings.mbDeleteHiddenSlides; break;
388 case TK_DeleteNotesPages : rValue >>= rSettings.mbDeleteNotesPages; break;
389 case TK_CustomShowName : rValue >>= rSettings.maCustomShowName; break;
390 case TK_SaveAs : rValue >>= rSettings.mbSaveAs; break;
391 case TK_SaveAsURL : rValue >>= rSettings.maSaveAsURL; break;
392 case TK_FilterName : rValue >>= rSettings.maFilterName; break;
393 case TK_OpenNewDocument : rValue >>= rSettings.mbOpenNewDocument; break;
394 case TK_EstimatedFileSize : rValue >>= rSettings.mnEstimatedFileSize; break;
395 default:
396 break;
399 catch (const Exception&)
404 bool ConfigurationAccess::GetConfigProperty( const PPPOptimizerTokenEnum ePropertyToken, const bool bDefault ) const
406 bool bRetValue = bDefault;
407 if ( ! ( GetConfigProperty( ePropertyToken ) >>= bRetValue ) )
408 bRetValue = bDefault;
409 return bRetValue;
412 sal_Int16 ConfigurationAccess::GetConfigProperty( const PPPOptimizerTokenEnum ePropertyToken, const sal_Int16 nDefault ) const
414 sal_Int16 nRetValue = nDefault;
415 if ( ! ( GetConfigProperty( ePropertyToken ) >>= nRetValue ) )
416 nRetValue = nDefault;
417 return nRetValue;
420 sal_Int32 ConfigurationAccess::GetConfigProperty( const PPPOptimizerTokenEnum ePropertyToken, const sal_Int32 nDefault ) const
422 sal_Int32 nRetValue = nDefault;
423 if ( ! ( GetConfigProperty( ePropertyToken ) >>= nRetValue ) )
424 nRetValue = nDefault;
425 return nRetValue;
428 Sequence< PropertyValue > ConfigurationAccess::GetConfigurationSequence()
430 Sequence< PropertyValue > aRet( 15 );
431 OptimizerSettings& rSettings( maSettings.front() );
432 aRet[ 0 ].Name = "JPEGCompression";
433 aRet[ 0 ].Value <<= rSettings.mbJPEGCompression;
434 aRet[ 1 ].Name = "JPEGQuality";
435 aRet[ 1 ].Value <<= rSettings.mnJPEGQuality;
436 aRet[ 2 ].Name = "RemoveCropArea";
437 aRet[ 2 ].Value <<= rSettings.mbRemoveCropArea;
438 aRet[ 3 ].Name = "ImageResolution";
439 aRet[ 3 ].Value <<= rSettings.mnImageResolution;
440 aRet[ 4 ].Name = "EmbedLinkedGraphics";
441 aRet[ 4 ].Value <<= rSettings.mbEmbedLinkedGraphics;
442 aRet[ 5 ].Name = "OLEOptimization";
443 aRet[ 5 ].Value <<= rSettings.mbOLEOptimization;
444 aRet[ 6 ].Name = "OLEOptimizationType";
445 aRet[ 6 ].Value <<= rSettings.mnOLEOptimizationType;
446 aRet[ 7 ].Name = "DeleteUnusedMasterPages";
447 aRet[ 7 ].Value <<= rSettings.mbDeleteUnusedMasterPages;
448 aRet[ 8 ].Name = "DeleteHiddenSlides";
449 aRet[ 8 ].Value <<= rSettings.mbDeleteHiddenSlides;
450 aRet[ 9 ].Name = "DeleteNotesPages";
451 aRet[ 9 ].Value <<= rSettings.mbDeleteNotesPages;
452 aRet[ 10].Name = "CustomShowName";
453 aRet[ 10].Value <<= rSettings.maCustomShowName;
454 aRet[ 11].Name = "SaveAsURL";
455 aRet[ 11].Value <<= rSettings.maSaveAsURL;
456 aRet[ 12].Name = "FilterName";
457 aRet[ 12].Value <<= rSettings.maFilterName;
458 aRet[ 13].Name = "OpenNewDocument";
459 aRet[ 13].Value <<= rSettings.mbOpenNewDocument;
460 aRet[ 14].Name = "EstimatedFileSize";
461 aRet[ 14].Value <<= rSettings.mnEstimatedFileSize;
462 return aRet;
465 std::vector< OptimizerSettings >::iterator ConfigurationAccess::GetOptimizerSettingsByName( const OUString& rName )
467 return std::find_if(maSettings.begin() + 1, maSettings.end(),
468 [&rName](const OptimizerSettings& rSettings) { return rSettings.maName == rName; });
471 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */