LibreOffice Module sdext (master)  1
configurationaccess.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 
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>
28 #include <sal/macros.h>
29 #include <sal/log.hxx>
30 #include <tools/diagnose_ex.h>
31 
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;
36 
37 static OUString GetPathToConfigurationRoot()
38 {
39  return "org.openoffice.Office.PresentationMinimizer";
40 }
41 
42 void OptimizerSettings::LoadSettingsFromConfiguration( const Reference< XNameAccess >& rSettings )
43 {
44  if ( !rSettings.is() )
45  return;
46 
47  const Sequence< OUString > aElements( rSettings->getElementNames() );
48  for ( const OUString& aPropertyName : aElements )
49  {
50  try
51  {
52  Any aValue( rSettings->getByName( aPropertyName ) );
53  switch( TKGet( aPropertyName ) )
54  {
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;
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;
71  }
72  }
73  catch (const Exception&)
74  {
75  }
76  }
77 }
78 
79 void OptimizerSettings::SaveSettingsToConfiguration( const Reference< XNameReplace >& rSettings )
80 {
81  if ( !rSettings.is() )
82  return;
83 
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") };
100 
101  Any pValues[] = {
102  Any( maName ),
104  Any( mnJPEGQuality ),
113  Any( mbSaveAs ),
114 // Any( maSaveAsURL ),
115 // Any( maFilterName ),
116  Any( mbOpenNewDocument ) };
117 
118  for ( int i = 0; i < int(SAL_N_ELEMENTS( pNames )); i++ )
119  {
120  try
121  {
122  rSettings->replaceByName( pNames[ i ], pValues[ i ] );
123  }
124  catch (const Exception&)
125  {
126  }
127  }
128 }
129 
130 bool OptimizerSettings::operator==( const OptimizerSettings& rOptimizerSettings ) const
131 {
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 );
143 }
144 
145 
147  mxContext( rxContext )
148 {
149  LoadStrings();
150  maSettings.emplace_back( );
151  maSettings.back().maName = "LastUsedSettings";
152  LoadConfiguration();
153 };
154 
156 {
157 }
158 
160 {
161  std::map< PPPOptimizerTokenEnum, OUString >::const_iterator aIter( maStrings.find( eToken ) );
162  return aIter != maStrings.end() ? ((*aIter).second) : OUString();
163 }
164 
166 {
167  try
168  {
169  do
170  {
172  if ( !xRoot.is() )
173  break;
174  Reference< container::XNameAccess > xSet( GetConfigurationNode( xRoot, "Strings" ), UNO_QUERY );
175  if ( xSet.is() )
176  {
177  const Sequence< OUString > aElements( xSet->getElementNames() );
178  for ( const auto& rElement : aElements )
179  {
180  try
181  {
182  OUString aString, aPropertyName( rElement );
183  if ( xSet->getByName( aPropertyName ) >>= aString )
184  maStrings[ TKGet( aPropertyName ) ] = aString;
185  }
186  catch (const Exception&)
187  {
188  }
189  }
190  }
191  }
192  while( false );
193  }
194  catch (const Exception&)
195  {
196  }
197 }
198 
200 {
201  try
202  {
203  do
204  {
206  if ( !xRoot.is() )
207  break;
208  Reference< container::XNameAccess > xSet( GetConfigurationNode( xRoot, "LastUsedSettings" ), UNO_QUERY );
209  if ( xSet.is() )
210  {
211  OptimizerSettings& rCurrent( maSettings.front() );
212  rCurrent.LoadSettingsFromConfiguration( xSet );
213  }
214  xSet.set( GetConfigurationNode( xRoot, "Settings/Templates" ), UNO_QUERY );
215  if ( xSet.is() )
216  {
217  const Sequence< OUString > aElements( xSet->getElementNames() );
218  for ( const auto& rElement : aElements )
219  {
220  try
221  {
222  OUString aPath( "Settings/Templates/" + rElement );
223  Reference< container::XNameAccess > xTemplates( GetConfigurationNode( xRoot, aPath ), UNO_QUERY );
224  if ( xTemplates.is() )
225  {
226  maSettings.emplace_back( );
227  maSettings.back().LoadSettingsFromConfiguration( xTemplates );
228  }
229  }
230  catch (const Exception&)
231  {
232  }
233  }
234  }
235  }
236  while( false );
237  }
238  catch (const Exception&)
239  {
240  }
241 }
242 
244 {
245  try
246  {
247  do
248  {
249  Reference<util::XChangesBatch> xRoot( OpenConfiguration( false ), UNO_QUERY_THROW );
250 
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 );
255 
256  // updating template elements
257  xSet.set( GetConfigurationNode( xRoot, "Settings/Templates" ), UNO_QUERY_THROW );
258  Reference< container::XNameContainer > xNameContainer( xSet, UNO_QUERY_THROW );
259 
260  const Sequence< OUString > aElements( xSet->getElementNames() );
261  for( const auto& rElement : aElements )
262  xNameContainer->removeByName( rElement );
263 
264  for( std::vector<OptimizerSettings>::size_type k = 1; k < maSettings.size(); k++ )
265  {
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 ) );
271 
272  OUString aPath( "Settings/Templates/" + aElementName );
273  Reference< container::XNameReplace > xTemplates( GetConfigurationNode( xRoot, aPath ), UNO_QUERY );
274  rSettings.SaveSettingsToConfiguration( xTemplates );
275  }
276  xRoot->commitChanges();
277  }
278  while( false );
279  }
280  catch (const Exception&)
281  {
282  }
283 }
284 
286 {
288  try
289  {
290  Reference< lang::XMultiServiceFactory > xProvider = configuration::theDefaultProvider::get( mxContext );
291  uno::Sequence<uno::Any> aCreationArguments(comphelper::InitAnyPropertySequence(
292  {
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";
301 
302  xRoot = xProvider->createInstanceWithArguments(
303  sAccessService, aCreationArguments );
304  }
305  catch (const Exception&)
306  {
307  }
308  return xRoot;
309 }
310 
312  const Reference< XInterface >& xRoot,
313  const OUString& sPathToNode )
314 {
316  try
317  {
318  if ( sPathToNode.isEmpty() )
319  xNode = xRoot;
320  else
321  {
322  Reference< XHierarchicalNameAccess > xHierarchy( xRoot, UNO_QUERY );
323  if ( xHierarchy.is() )
324  {
325  xHierarchy->getByHierarchicalName( sPathToNode ) >>= xNode;
326  }
327  }
328  }
329  catch (const Exception&)
330  {
331  TOOLS_WARN_EXCEPTION("sdext.minimizer", "caught exception while getting configuration node "
332  << sPathToNode);
333  }
334  return xNode;
335 }
336 
337 css::uno::Any ConfigurationAccess::GetConfigProperty( const PPPOptimizerTokenEnum ePropertyToken ) const
338 {
339  Any aRetValue;
340  const OptimizerSettings& rSettings( maSettings.front() );
341  try
342  {
343  switch( ePropertyToken )
344  {
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;
363  }
364  }
365  catch (const Exception&)
366  {
367  }
368  return aRetValue;
369 }
370 
371 void ConfigurationAccess::SetConfigProperty( const PPPOptimizerTokenEnum ePropertyToken, const css::uno::Any& rValue )
372 {
373  OptimizerSettings& rSettings( maSettings.front() );
374  try
375  {
376  switch( ePropertyToken )
377  {
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;
397  }
398  }
399  catch (const Exception&)
400  {
401  }
402 }
403 
404 bool ConfigurationAccess::GetConfigProperty( const PPPOptimizerTokenEnum ePropertyToken, const bool bDefault ) const
405 {
406  bool bRetValue = bDefault;
407  if ( ! ( GetConfigProperty( ePropertyToken ) >>= bRetValue ) )
408  bRetValue = bDefault;
409  return bRetValue;
410 }
411 
412 sal_Int16 ConfigurationAccess::GetConfigProperty( const PPPOptimizerTokenEnum ePropertyToken, const sal_Int16 nDefault ) const
413 {
414  sal_Int16 nRetValue = nDefault;
415  if ( ! ( GetConfigProperty( ePropertyToken ) >>= nRetValue ) )
416  nRetValue = nDefault;
417  return nRetValue;
418 }
419 
420 sal_Int32 ConfigurationAccess::GetConfigProperty( const PPPOptimizerTokenEnum ePropertyToken, const sal_Int32 nDefault ) const
421 {
422  sal_Int32 nRetValue = nDefault;
423  if ( ! ( GetConfigProperty( ePropertyToken ) >>= nRetValue ) )
424  nRetValue = nDefault;
425  return nRetValue;
426 }
427 
429 {
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;
463 }
464 
465 std::vector< OptimizerSettings >::iterator ConfigurationAccess::GetOptimizerSettingsByName( const OUString& rName )
466 {
467  return std::find_if(maSettings.begin() + 1, maSettings.end(),
468  [&rName](const OptimizerSettings& rSettings) { return rSettings.maName == rName; });
469 }
470 
471 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::uno::Reference< css::uno::XInterface > OpenConfiguration(bool bReadOnly)
bool operator==(const OptimizerSettings &rOptimizerSettings) const
const wchar_t *typedef int(__stdcall *DllNativeUnregProc)(int
css::uno::Sequence< css::beans::PropertyValue > GetConfigurationSequence()
ConfigurationAccess(const css::uno::Reference< css::uno::XComponentContext > &rXFactory)
std::vector< OptimizerSettings >::iterator GetOptimizerSettingsByName(const OUString &rName)
static OUString GetPathToConfigurationRoot()
static css::uno::Reference< css::uno::XInterface > GetConfigurationNode(const css::uno::Reference< css::uno::XInterface > &xRoot, const OUString &sPathToNode)
std::map< PPPOptimizerTokenEnum, OUString > maStrings
PPPOptimizerTokenEnum
#define SAL_N_ELEMENTS(arr)
#define TOOLS_WARN_EXCEPTION(area, stream)
OUString getString(const PPPOptimizerTokenEnum) const
void SetConfigProperty(const PPPOptimizerTokenEnum, const css::uno::Any &aValue)
css::uno::Sequence< css::uno::Any > InitAnyPropertySequence(::std::initializer_list< ::std::pair< OUString, css::uno::Any > > vInit)
std::vector< OptimizerSettings > maSettings
int i
void LoadSettingsFromConfiguration(const css::uno::Reference< css::container::XNameAccess > &rSettings)
css::uno::Reference< css::uno::XComponentContext > mxContext
const PropertyValue * pValues
PPPOptimizerTokenEnum TKGet(const OUString &rToken)
void SaveSettingsToConfiguration(const css::uno::Reference< css::container::XNameReplace > &rSettings)
css::uno::Any GetConfigProperty(const PPPOptimizerTokenEnum) const
Reference< XComponentContext > mxContext