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