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>
29 #include <sal/macros.h>
30 #include <sal/log.hxx>
31 #include <tools/diagnose_ex.h>
32 
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;
37 
38 static OUString GetPathToConfigurationRoot()
39 {
40  return "org.openoffice.Office.PresentationMinimizer";
41 }
42 
44 {
45  if ( !rSettings.is() )
46  return;
47 
48  const Sequence< OUString > aElements( rSettings->getElementNames() );
49  for ( const OUString& aPropertyName : aElements )
50  {
51  try
52  {
53  Any aValue( rSettings->getByName( aPropertyName ) );
54  switch( TKGet( aPropertyName ) )
55  {
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;
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;
71  default: break;
72  }
73  }
74  catch (const Exception&)
75  {
76  }
77  }
78 }
79 
80 void OptimizerSettings::SaveSettingsToConfiguration( const Reference< XNameReplace >& rSettings )
81 {
82  if ( !rSettings.is() )
83  return;
84 
85  OUString pNames[] = {
86  OUString("Name"),
87  OUString("JPEGCompression"),
88  OUString("JPEGQuality"),
89  OUString("RemoveCropArea"),
90  OUString("ImageResolution"),
91  OUString("EmbedLinkedGraphics"),
92  OUString("OLEOptimization"),
93  OUString("OLEOptimizationType"),
94  OUString("DeleteUnusedMasterPages"),
95  OUString("DeleteHiddenSlides"),
96  OUString("DeleteNotesPages"),
97  OUString("SaveAs"),
98 // OUString("SaveAsURL"),
99 // OUString("FilterName"),
100  OUString("OpenNewDocument") };
101 
102  Any pValues[] = {
103  Any( maName ),
105  Any( mnJPEGQuality ),
114  Any( mbSaveAs ),
115 // Any( maSaveAsURL ),
116 // Any( maFilterName ),
117  Any( mbOpenNewDocument ) };
118 
119  for ( int i = 0; i < int(SAL_N_ELEMENTS( pNames )); i++ )
120  {
121  try
122  {
123  rSettings->replaceByName( pNames[ i ], pValues[ i ] );
124  }
125  catch (const Exception&)
126  {
127  }
128  }
129 }
130 
131 bool OptimizerSettings::operator==( const OptimizerSettings& rOptimizerSettings ) const
132 {
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 );
143 // && ( rOptimizerSettings.mbOpenNewDocument == mbOpenNewDocument );
144 }
145 
146 
148  mxContext( rxContext )
149 {
150  LoadStrings();
151  maSettings.emplace_back( );
152  maSettings.back().maName = "LastUsedSettings";
153  LoadConfiguration();
154 };
155 
157 {
158 }
159 
161 {
162  std::map< PPPOptimizerTokenEnum, OUString >::const_iterator aIter( maStrings.find( eToken ) );
163  return aIter != maStrings.end() ? ((*aIter).second) : OUString();
164 }
165 
167 {
168  try
169  {
170  do
171  {
172  Reference< XInterface > xRoot( OpenConfiguration( true ) );
173  if ( !xRoot.is() )
174  break;
175  Reference< container::XNameAccess > xSet( GetConfigurationNode( xRoot, "Strings" ), UNO_QUERY );
176  if ( xSet.is() )
177  {
178  const Sequence< OUString > aElements( xSet->getElementNames() );
179  for ( const auto& rElement : aElements )
180  {
181  try
182  {
183  OUString aString, aPropertyName( rElement );
184  if ( xSet->getByName( aPropertyName ) >>= aString )
185  maStrings[ TKGet( aPropertyName ) ] = aString;
186  }
187  catch (const Exception&)
188  {
189  }
190  }
191  }
192  }
193  while( false );
194  }
195  catch (const Exception&)
196  {
197  }
198 }
199 
201 {
202  try
203  {
204  do
205  {
206  Reference< XInterface > xRoot( OpenConfiguration( true ) );
207  if ( !xRoot.is() )
208  break;
209  Reference< container::XNameAccess > xSet( GetConfigurationNode( xRoot, "LastUsedSettings" ), UNO_QUERY );
210  if ( xSet.is() )
211  {
212  OptimizerSettings& rCurrent( maSettings.front() );
213  rCurrent.LoadSettingsFromConfiguration( xSet );
214  }
215  xSet.set( GetConfigurationNode( xRoot, "Settings/Templates" ), UNO_QUERY );
216  if ( xSet.is() )
217  {
218  const Sequence< OUString > aElements( xSet->getElementNames() );
219  for ( const auto& rElement : aElements )
220  {
221  try
222  {
223  OUString aPath( "Settings/Templates/" + rElement );
224  Reference< container::XNameAccess > xTemplates( GetConfigurationNode( xRoot, aPath ), UNO_QUERY );
225  if ( xTemplates.is() )
226  {
227  maSettings.emplace_back( );
228  maSettings.back().LoadSettingsFromConfiguration( xTemplates );
229  }
230  }
231  catch (const Exception&)
232  {
233  }
234  }
235  }
236  }
237  while( false );
238  }
239  catch (const Exception&)
240  {
241  }
242 }
243 
245 {
246  try
247  {
248  do
249  {
250  Reference<util::XChangesBatch> xRoot( OpenConfiguration( false ), UNO_QUERY_THROW );
251 
252  // storing the last used settings
253  Reference< container::XNameReplace > xSet( GetConfigurationNode( xRoot, "LastUsedSettings" ), UNO_QUERY_THROW );
254  OptimizerSettings& rCurrent( maSettings.front() );
255  rCurrent.SaveSettingsToConfiguration( xSet );
256 
257  // updating template elements
258  xSet.set( GetConfigurationNode( xRoot, "Settings/Templates" ), UNO_QUERY_THROW );
259  Reference< container::XNameContainer > xNameContainer( xSet, UNO_QUERY_THROW );
260 
261  const Sequence< OUString > aElements( xSet->getElementNames() );
262  for( const auto& rElement : aElements )
263  xNameContainer->removeByName( rElement );
264 
265  for( std::vector<OptimizerSettings>::size_type k = 1; k < maSettings.size(); k++ )
266  {
267  OptimizerSettings& rSettings( maSettings[ k ] );
268  OUString aElementName( "Template" + OUString::number( k ) );
269  Reference< lang::XSingleServiceFactory > xChildFactory ( xSet, UNO_QUERY_THROW );
270  Reference< container::XNameReplace > xChild( xChildFactory->createInstance(), UNO_QUERY_THROW );
271  xNameContainer->insertByName( aElementName, Any( xChild ) );
272 
273  OUString aPath( "Settings/Templates/" + aElementName );
274  Reference< container::XNameReplace > xTemplates( GetConfigurationNode( xRoot, aPath ), UNO_QUERY );
275  rSettings.SaveSettingsToConfiguration( xTemplates );
276  }
277  xRoot->commitChanges();
278  }
279  while( false );
280  }
281  catch (const Exception&)
282  {
283  }
284 }
285 
286 Reference< XInterface > ConfigurationAccess::OpenConfiguration( bool bReadOnly )
287 {
288  Reference< XInterface > xRoot;
289  try
290  {
291  Reference< lang::XMultiServiceFactory > xProvider = configuration::theDefaultProvider::get( mxContext );
292  uno::Sequence<uno::Any> aCreationArguments(comphelper::InitAnyPropertySequence(
293  {
294  {"nodepath", uno::Any(GetPathToConfigurationRoot())}
295  }));
296  OUString sAccessService;
297  if ( bReadOnly )
298  sAccessService = "com.sun.star.configuration.ConfigurationAccess";
299  else
300  sAccessService =
301  "com.sun.star.configuration.ConfigurationUpdateAccess";
302 
303  xRoot = xProvider->createInstanceWithArguments(
304  sAccessService, aCreationArguments );
305  }
306  catch (const Exception&)
307  {
308  }
309  return xRoot;
310 }
311 
312 Reference< XInterface > ConfigurationAccess::GetConfigurationNode(
313  const Reference< XInterface >& xRoot,
314  const OUString& sPathToNode )
315 {
316  Reference< XInterface > xNode;
317  try
318  {
319  if ( sPathToNode.isEmpty() )
320  xNode = xRoot;
321  else
322  {
323  Reference< XHierarchicalNameAccess > xHierarchy( xRoot, UNO_QUERY );
324  if ( xHierarchy.is() )
325  {
326  xHierarchy->getByHierarchicalName( sPathToNode ) >>= xNode;
327  }
328  }
329  }
330  catch (const Exception&)
331  {
332  TOOLS_WARN_EXCEPTION("sdext.minimizer", "caught exception while getting configuration node "
333  << sPathToNode);
334  }
335  return xNode;
336 }
337 
338 css::uno::Any ConfigurationAccess::GetConfigProperty( const PPPOptimizerTokenEnum ePropertyToken ) const
339 {
340  Any aRetValue;
341  const OptimizerSettings& rSettings( maSettings.front() );
342  try
343  {
344  switch( ePropertyToken )
345  {
346  case TK_Name : aRetValue <<= rSettings.maName; break;
347  case TK_JPEGCompression : aRetValue <<= rSettings.mbJPEGCompression; break;
348  case TK_JPEGQuality : aRetValue <<= rSettings.mnJPEGQuality; break;
349  case TK_RemoveCropArea : aRetValue <<= rSettings.mbRemoveCropArea; break;
350  case TK_ImageResolution : aRetValue <<= rSettings.mnImageResolution; break;
351  case TK_EmbedLinkedGraphics : aRetValue <<= rSettings.mbEmbedLinkedGraphics; break;
352  case TK_OLEOptimization : aRetValue <<= rSettings.mbOLEOptimization; break;
353  case TK_OLEOptimizationType : aRetValue <<= rSettings.mnOLEOptimizationType; break;
354  case TK_DeleteUnusedMasterPages : aRetValue <<= rSettings.mbDeleteUnusedMasterPages; break;
355  case TK_DeleteHiddenSlides : aRetValue <<= rSettings.mbDeleteHiddenSlides; break;
356  case TK_DeleteNotesPages : aRetValue <<= rSettings.mbDeleteNotesPages; break;
357  case TK_SaveAs : aRetValue <<= rSettings.mbSaveAs; break;
358  case TK_SaveAsURL : aRetValue <<= rSettings.maSaveAsURL; break;
359  case TK_FilterName : aRetValue <<= rSettings.maFilterName; break;
360  case TK_OpenNewDocument : aRetValue <<= rSettings.mbOpenNewDocument; break;
361  case TK_EstimatedFileSize : aRetValue <<= rSettings.mnEstimatedFileSize; break;
362  default:
363  break;
364  }
365  }
366  catch (const Exception&)
367  {
368  }
369  return aRetValue;
370 }
371 
372 void ConfigurationAccess::SetConfigProperty( const PPPOptimizerTokenEnum ePropertyToken, const css::uno::Any& rValue )
373 {
374  OptimizerSettings& rSettings( maSettings.front() );
375  try
376  {
377  switch( ePropertyToken )
378  {
379  case TK_Name : rValue >>= rSettings.maName; break;
380  case TK_JPEGCompression : rValue >>= rSettings.mbJPEGCompression; break;
381  case TK_JPEGQuality : rValue >>= rSettings.mnJPEGQuality; break;
382  case TK_RemoveCropArea : rValue >>= rSettings.mbRemoveCropArea; break;
383  case TK_ImageResolution : rValue >>= rSettings.mnImageResolution; break;
384  case TK_EmbedLinkedGraphics : rValue >>= rSettings.mbEmbedLinkedGraphics; break;
385  case TK_OLEOptimization : rValue >>= rSettings.mbOLEOptimization; break;
386  case TK_OLEOptimizationType : rValue >>= rSettings.mnOLEOptimizationType; break;
387  case TK_DeleteUnusedMasterPages : rValue >>= rSettings.mbDeleteUnusedMasterPages; break;
388  case TK_DeleteHiddenSlides : rValue >>= rSettings.mbDeleteHiddenSlides; break;
389  case TK_DeleteNotesPages : rValue >>= rSettings.mbDeleteNotesPages; break;
390  case TK_CustomShowName : rValue >>= rSettings.maCustomShowName; break;
391  case TK_SaveAs : rValue >>= rSettings.mbSaveAs; break;
392  case TK_SaveAsURL : rValue >>= rSettings.maSaveAsURL; break;
393  case TK_FilterName : rValue >>= rSettings.maFilterName; break;
394  case TK_OpenNewDocument : rValue >>= rSettings.mbOpenNewDocument; break;
395  case TK_EstimatedFileSize : rValue >>= rSettings.mnEstimatedFileSize; break;
396  default:
397  break;
398  }
399  }
400  catch (const Exception&)
401  {
402  }
403 }
404 
405 bool ConfigurationAccess::GetConfigProperty( const PPPOptimizerTokenEnum ePropertyToken, const bool bDefault ) const
406 {
407  bool bRetValue = bDefault;
408  if ( ! ( GetConfigProperty( ePropertyToken ) >>= bRetValue ) )
409  bRetValue = bDefault;
410  return bRetValue;
411 }
412 
413 sal_Int16 ConfigurationAccess::GetConfigProperty( const PPPOptimizerTokenEnum ePropertyToken, const sal_Int16 nDefault ) const
414 {
415  sal_Int16 nRetValue = nDefault;
416  if ( ! ( GetConfigProperty( ePropertyToken ) >>= nRetValue ) )
417  nRetValue = nDefault;
418  return nRetValue;
419 }
420 
421 sal_Int32 ConfigurationAccess::GetConfigProperty( const PPPOptimizerTokenEnum ePropertyToken, const sal_Int32 nDefault ) const
422 {
423  sal_Int32 nRetValue = nDefault;
424  if ( ! ( GetConfigProperty( ePropertyToken ) >>= nRetValue ) )
425  nRetValue = nDefault;
426  return nRetValue;
427 }
428 
430 {
431  OptimizerSettings& rSettings( maSettings.front() );
432  Sequence< PropertyValue > aRet{
433  comphelper::makePropertyValue("JPEGCompression", rSettings.mbJPEGCompression),
434  comphelper::makePropertyValue("JPEGQuality", rSettings.mnJPEGQuality),
435  comphelper::makePropertyValue("RemoveCropArea", rSettings.mbRemoveCropArea),
436  comphelper::makePropertyValue("ImageResolution", rSettings.mnImageResolution),
437  comphelper::makePropertyValue("EmbedLinkedGraphics", rSettings.mbEmbedLinkedGraphics),
438  comphelper::makePropertyValue("OLEOptimization", rSettings.mbOLEOptimization),
439  comphelper::makePropertyValue("OLEOptimizationType", rSettings.mnOLEOptimizationType),
440  comphelper::makePropertyValue("DeleteUnusedMasterPages", rSettings.mbDeleteUnusedMasterPages),
441  comphelper::makePropertyValue("DeleteHiddenSlides", rSettings.mbDeleteHiddenSlides),
442  comphelper::makePropertyValue("DeleteNotesPages", rSettings.mbDeleteNotesPages),
443  comphelper::makePropertyValue("CustomShowName", rSettings.maCustomShowName),
444  comphelper::makePropertyValue("SaveAsURL", rSettings.maSaveAsURL),
445  comphelper::makePropertyValue("FilterName", rSettings.maFilterName),
446  comphelper::makePropertyValue("OpenNewDocument", rSettings.mbOpenNewDocument),
447  comphelper::makePropertyValue("EstimatedFileSize", rSettings.mnEstimatedFileSize)
448  };
449  return aRet;
450 }
451 
452 std::vector< OptimizerSettings >::iterator ConfigurationAccess::GetOptimizerSettingsByName( const OUString& rName )
453 {
454  return std::find_if(maSettings.begin() + 1, maSettings.end(),
455  [&rName](const OptimizerSettings& rSettings) { return rSettings.maName == rName; });
456 }
457 
458 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::uno::Reference< css::uno::XInterface > OpenConfiguration(bool bReadOnly)
uno::Reference< uno::XComponentContext > mxContext
css::beans::PropertyValue makePropertyValue(const OUString &rName, T &&rValue)
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
int i
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
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