LibreOffice Module vcl (master)  1
configsettings.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 #include <vcl/configsettings.hxx>
21 
22 #include <svdata.hxx>
23 
24 #include <com/sun/star/uno/Any.hxx>
25 #include <com/sun/star/uno/Sequence.hxx>
26 #include <com/sun/star/beans/PropertyValue.hpp>
27 #include <o3tl/any.hxx>
28 #include <sal/log.hxx>
29 
30 using namespace utl;
31 using namespace vcl;
32 using namespace com::sun::star::uno;
33 using namespace com::sun::star::lang;
34 using namespace com::sun::star::beans;
35 using namespace com::sun::star::container;
36 
37 #define SETTINGS_CONFIGNODE "VCL/Settings"
38 
39 SettingsConfigItem* SettingsConfigItem::get()
40 {
41  ImplSVData* pSVData = ImplGetSVData();
42  if( ! pSVData->mpSettingsConfigItem )
43  pSVData->mpSettingsConfigItem.reset( new SettingsConfigItem() );
44  return pSVData->mpSettingsConfigItem.get();
45 }
46 
47 SettingsConfigItem::SettingsConfigItem()
49  m_aSettings( 0 )
50 {
51  getValues();
52 }
53 
55 {
56  assert(!IsModified()); // should have been committed
57 }
58 
60 {
61  for (auto const& setting : m_aSettings)
62  {
63  OUString aKeyName( setting.first );
64  /*bool bAdded =*/ AddNode( OUString(), aKeyName );
65  Sequence< PropertyValue > aValues( setting.second.size() );
66  PropertyValue* pValues = aValues.getArray();
67  int nIndex = 0;
68  for (auto const& elem : setting.second)
69  {
70  pValues[nIndex].Name = aKeyName + "/" + elem.first;
71  pValues[nIndex].Handle = 0;
72  pValues[nIndex].Value <<= elem.second;
73  pValues[nIndex].State = PropertyState_DIRECT_VALUE;
74  nIndex++;
75  }
76  ReplaceSetProperties( aKeyName, aValues );
77  }
78 }
79 
81 {
82  getValues();
83 }
84 
86 {
87  m_aSettings.clear();
88 
89  const Sequence< OUString > aNames( GetNodeNames( OUString() ) );
90 
91  for( const auto& aKeyName : aNames )
92  {
93 #if OSL_DEBUG_LEVEL > 2
94  SAL_INFO( "vcl", "found settings data for " << aKeyName );
95 #endif
96  Sequence< OUString > aKeys( GetNodeNames( aKeyName ) );
97  Sequence< OUString > aSettingsKeys( aKeys.getLength() );
98  std::transform(aKeys.begin(), aKeys.end(), aSettingsKeys.begin(),
99  [&aKeyName](const OUString& rKey) -> OUString { return aKeyName + "/" + rKey; });
100  Sequence< Any > aValues( GetProperties( aSettingsKeys ) );
101  const OUString* pFrom = aKeys.getConstArray();
102  const Any* pValue = aValues.getConstArray();
103  for( int i = 0; i < aValues.getLength(); i++, pValue++ )
104  {
105  if( auto pLine = o3tl::tryAccess<OUString>(*pValue) )
106  {
107  if( !pLine->isEmpty() )
108  m_aSettings[ aKeyName ][ pFrom[i] ] = *pLine;
109 #if OSL_DEBUG_LEVEL > 2
110  SAL_INFO( "vcl", " \"" << aKeys.getConstArray()[i] << "\"=\"" << *pLine << "\"" );
111 #endif
112  }
113  }
114  }
115 }
116 
117 OUString SettingsConfigItem::getValue( const OUString& rGroup, const OUString& rKey ) const
118 {
119  std::unordered_map< OUString, SmallOUStrMap >::const_iterator group = m_aSettings.find( rGroup );
120  if( group == m_aSettings.end() || group->second.find( rKey ) == group->second.end() )
121  {
122  return OUString();
123  }
124  return group->second.find(rKey)->second;
125 }
126 
127 void SettingsConfigItem::setValue( const OUString& rGroup, const OUString& rKey, const OUString& rValue )
128 {
129  bool bModified = m_aSettings[ rGroup ][ rKey ] != rValue;
130  if( bModified )
131  {
132  m_aSettings[ rGroup ][ rKey ] = rValue;
133  SetModified();
134  }
135 }
136 
137 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual ~SettingsConfigItem() override
ConfigItemMode
std::unique_ptr< vcl::SettingsConfigItem > mpSettingsConfigItem
Definition: svdata.hxx:362
virtual void ImplCommit() override
ImplSVData * ImplGetSVData()
Definition: svdata.cxx:67
NONE
bool ReplaceSetProperties(const OUString &rNode, const css::uno::Sequence< css::beans::PropertyValue > &rValues)
void setValue(const OUString &rGroup, const OUString &rKey, const OUString &rValue)
int i
css::uno::Sequence< css::uno::Any > GetProperties(const css::uno::Sequence< OUString > &rNames)
bool IsModified() const
OUString getValue(const OUString &rGroup, const OUString &rKey) const
std::unordered_map< OUString, SmallOUStrMap > m_aSettings
bool AddNode(const OUString &rNode, const OUString &rNewNode)
#define SAL_INFO(area, stream)
css::uno::Sequence< OUString > GetNodeNames(const OUString &rNode)
virtual void Notify(const css::uno::Sequence< OUString > &rPropertyNames) override
#define SETTINGS_CONFIGNODE
tuple group