Line data Source code
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 :
28 : using namespace utl;
29 : using namespace vcl;
30 : using namespace com::sun::star::uno;
31 : using namespace com::sun::star::lang;
32 : using namespace com::sun::star::beans;
33 : using namespace com::sun::star::container;
34 :
35 : #define SETTINGS_CONFIGNODE "VCL/Settings"
36 :
37 220 : SettingsConfigItem* SettingsConfigItem::get()
38 : {
39 220 : ImplSVData* pSVData = ImplGetSVData();
40 220 : if( ! pSVData->mpSettingsConfigItem )
41 179 : pSVData->mpSettingsConfigItem = new SettingsConfigItem();
42 220 : return pSVData->mpSettingsConfigItem;
43 : }
44 :
45 179 : SettingsConfigItem::SettingsConfigItem()
46 : :
47 : ConfigItem( OUString( SETTINGS_CONFIGNODE ),
48 : ConfigItemMode::DelayedUpdate ),
49 179 : m_aSettings( 0 )
50 : {
51 179 : getValues();
52 179 : }
53 :
54 356 : SettingsConfigItem::~SettingsConfigItem()
55 : {
56 : assert(!IsModified()); // should have been committed
57 356 : }
58 :
59 0 : void SettingsConfigItem::ImplCommit()
60 : {
61 0 : std::unordered_map< OUString, SmallOUStrMap, OUStringHash >::const_iterator group;
62 :
63 0 : for( group = m_aSettings.begin(); group != m_aSettings.end(); ++group )
64 : {
65 0 : OUString aKeyName( group->first );
66 0 : /*bool bAdded =*/ AddNode( OUString(), aKeyName );
67 0 : Sequence< PropertyValue > aValues( group->second.size() );
68 0 : PropertyValue* pValues = aValues.getArray();
69 0 : int nIndex = 0;
70 0 : SmallOUStrMap::const_iterator it;
71 0 : for( it = group->second.begin(); it != group->second.end(); ++it )
72 : {
73 0 : OUString aName( aKeyName );
74 0 : aName += OUString('/');
75 0 : aName += it->first;
76 0 : pValues[nIndex].Name = aName;
77 0 : pValues[nIndex].Handle = 0;
78 0 : pValues[nIndex].Value <<= it->second;
79 0 : pValues[nIndex].State = PropertyState_DIRECT_VALUE;
80 0 : nIndex++;
81 0 : }
82 0 : ReplaceSetProperties( aKeyName, aValues );
83 0 : }
84 0 : }
85 :
86 0 : void SettingsConfigItem::Notify( const Sequence< OUString >& )
87 : {
88 0 : getValues();
89 0 : }
90 :
91 179 : void SettingsConfigItem::getValues()
92 : {
93 179 : m_aSettings.clear();
94 :
95 179 : Sequence< OUString > aNames( GetNodeNames( OUString() ) );
96 :
97 1253 : for( int j = 0; j < aNames.getLength(); j++ )
98 : {
99 : #if OSL_DEBUG_LEVEL > 2
100 : OSL_TRACE( "found settings data for \"%s\"\n",
101 : OUStringToOString( aNames.getConstArray()[j], RTL_TEXTENCODING_ASCII_US ).getStr()
102 : );
103 : #endif
104 1074 : OUString aKeyName( aNames.getConstArray()[j] );
105 2148 : Sequence< OUString > aKeys( GetNodeNames( aKeyName ) );
106 2148 : Sequence< OUString > aSettingsKeys( aKeys.getLength() );
107 1074 : const OUString* pFrom = aKeys.getConstArray();
108 1074 : OUString* pTo = aSettingsKeys.getArray();
109 2327 : for( int m = 0; m < aKeys.getLength(); m++ )
110 : {
111 1253 : OUString aName( aKeyName );
112 1253 : aName += OUString('/');
113 1253 : aName += pFrom[m];
114 1253 : pTo[m] = aName;
115 1253 : }
116 2148 : Sequence< Any > aValues( GetProperties( aSettingsKeys ) );
117 1074 : const Any* pValue = aValues.getConstArray();
118 2327 : for( int i = 0; i < aValues.getLength(); i++, pValue++ )
119 : {
120 1253 : if( pValue->getValueTypeClass() == TypeClass_STRING )
121 : {
122 1253 : const OUString* pLine = static_cast<const OUString*>(pValue->getValue());
123 1253 : if( !pLine->isEmpty() )
124 1253 : m_aSettings[ aKeyName ][ pFrom[i] ] = *pLine;
125 : #if OSL_DEBUG_LEVEL > 2
126 : OSL_TRACE( " \"%s\"=\"%.30s\"\n",
127 : OUStringToOString( aKeys.getConstArray()[i], RTL_TEXTENCODING_ASCII_US ).getStr(),
128 : OUStringToOString( *pLine, RTL_TEXTENCODING_ASCII_US ).getStr()
129 : );
130 : #endif
131 : }
132 : }
133 1253 : }
134 179 : }
135 :
136 220 : const OUString& SettingsConfigItem::getValue( const OUString& rGroup, const OUString& rKey ) const
137 : {
138 220 : std::unordered_map< OUString, SmallOUStrMap, OUStringHash >::const_iterator group = m_aSettings.find( rGroup );
139 220 : if( group == m_aSettings.end() || group->second.find( rKey ) == group->second.end() )
140 : {
141 0 : static OUString aEmpty;
142 0 : return aEmpty;
143 : }
144 220 : return group->second.find(rKey)->second;
145 : }
146 :
147 0 : void SettingsConfigItem::setValue( const OUString& rGroup, const OUString& rKey, const OUString& rValue )
148 : {
149 0 : bool bModified = m_aSettings[ rGroup ][ rKey ] != rValue;
150 0 : if( bModified )
151 : {
152 0 : m_aSettings[ rGroup ][ rKey ] = rValue;
153 0 : SetModified();
154 : }
155 0 : }
156 :
157 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|