xref: /trunk/main/vcl/source/gdi/configsettings.cxx (revision 71fb1a33)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_vcl.hxx"
26 
27 #include <vcl/configsettings.hxx>
28 
29 #include <svdata.hxx>
30 
31 #include <com/sun/star/uno/Any.hxx>
32 #include <com/sun/star/uno/Sequence.hxx>
33 #include <com/sun/star/beans/PropertyValue.hpp>
34 
35 using namespace rtl;
36 using namespace utl;
37 using namespace vcl;
38 using namespace com::sun::star::uno;
39 using namespace com::sun::star::lang;
40 using namespace com::sun::star::beans;
41 using namespace com::sun::star::container;
42 
43 #define SETTINGS_CONFIGNODE "VCL/Settings"
44 
45 /*
46  *	SettingsConfigItem::get
47  */
48 
get()49 SettingsConfigItem* SettingsConfigItem::get()
50 {
51     ImplSVData* pSVData = ImplGetSVData();
52     if( ! pSVData->mpSettingsConfigItem )
53         pSVData->mpSettingsConfigItem = new SettingsConfigItem();
54     return pSVData->mpSettingsConfigItem;
55 }
56 
57 /*
58  *  SettignsConfigItem constructor
59  */
60 
SettingsConfigItem()61 SettingsConfigItem::SettingsConfigItem()
62         :
63         ConfigItem( OUString( RTL_CONSTASCII_USTRINGPARAM( SETTINGS_CONFIGNODE ) ),
64                     CONFIG_MODE_DELAYED_UPDATE ),
65 	m_aSettings( 0 )
66 {
67     getValues();
68 }
69 
70 /*
71  *  SettingsConfigItem destructor
72  */
73 
~SettingsConfigItem()74 SettingsConfigItem::~SettingsConfigItem()
75 {
76     if( IsModified() )
77         Commit();
78 }
79 
80 /*
81  *  SettingsConfigItem::Commit
82  */
83 
Commit()84 void SettingsConfigItem::Commit()
85 {
86     if( ! IsValidConfigMgr() )
87         return;
88 
89     std::hash_map< OUString, SmallOUStrMap, rtl::OUStringHash >::const_iterator group;
90 
91     for( group = m_aSettings.begin(); group != m_aSettings.end(); ++group )
92     {
93         String aKeyName( group->first );
94         /*sal_Bool bAdded =*/ AddNode( OUString(), aKeyName );
95         Sequence< PropertyValue > aValues( group->second.size() );
96         PropertyValue* pValues = aValues.getArray();
97         int nIndex = 0;
98         SmallOUStrMap::const_iterator it;
99         for( it = group->second.begin(); it != group->second.end(); ++it )
100         {
101             String aName( aKeyName );
102             aName.Append( '/' );
103             aName.Append( String( it->first ) );
104             pValues[nIndex].Name	= aName;
105             pValues[nIndex].Handle	= 0;
106             pValues[nIndex].Value <<= it->second;
107             pValues[nIndex].State	= PropertyState_DIRECT_VALUE;
108             nIndex++;
109         }
110         ReplaceSetProperties( aKeyName, aValues );
111     }
112 }
113 
114 /*
115  *  SettingsConfigItem::Notify
116  */
117 
Notify(const Sequence<OUString> &)118 void SettingsConfigItem::Notify( const Sequence< OUString >& )
119 {
120     getValues();
121 }
122 
123 /*
124  *  SettingsConfigItem::getValues
125  */
getValues()126 void SettingsConfigItem::getValues()
127 {
128     if( ! IsValidConfigMgr() )
129         return;
130 
131     m_aSettings.clear();
132 
133     Sequence< OUString > aNames( GetNodeNames( OUString() ) );
134     m_aSettings.rehash( aNames.getLength() );
135 
136     for( int j = 0; j < aNames.getLength(); j++ )
137     {
138 #if OSL_DEBUG_LEVEL > 2
139         fprintf( stderr, "found settings data for \"%s\"\n",
140                  OUStringToOString( aNames.getConstArray()[j], RTL_TEXTENCODING_ASCII_US ).getStr()
141                  );
142 #endif
143         String aKeyName( aNames.getConstArray()[j] );
144         Sequence< OUString > aKeys( GetNodeNames( aKeyName ) );
145         Sequence< OUString > aSettingsKeys( aKeys.getLength() );
146         const OUString* pFrom = aKeys.getConstArray();
147         OUString* pTo = aSettingsKeys.getArray();
148         for( int m = 0; m < aKeys.getLength(); m++ )
149         {
150             String aName( aKeyName );
151             aName.Append( '/' );
152             aName.Append( String( pFrom[m] ) );
153             pTo[m] = aName;
154         }
155         Sequence< Any > aValues( GetProperties( aSettingsKeys ) );
156         const Any* pValue = aValues.getConstArray();
157         for( int i = 0; i < aValues.getLength(); i++, pValue++ )
158         {
159             if( pValue->getValueTypeClass() == TypeClass_STRING )
160             {
161                 const OUString* pLine = (const OUString*)pValue->getValue();
162                 if( pLine->getLength() )
163                     m_aSettings[ aKeyName ][ pFrom[i] ] = *pLine;
164 #if OSL_DEBUG_LEVEL > 2
165                 fprintf( stderr, "   \"%s\"=\"%.30s\"\n",
166                          OUStringToOString( aKeys.getConstArray()[i], RTL_TEXTENCODING_ASCII_US ).getStr(),
167                          OUStringToOString( *pLine, RTL_TEXTENCODING_ASCII_US ).getStr()
168                          );
169 #endif
170             }
171         }
172     }
173 }
174 
175 /*
176  *  SettingsConfigItem::getDefaultFont
177  */
178 
getValue(const OUString & rGroup,const OUString & rKey) const179 const OUString& SettingsConfigItem::getValue( const OUString& rGroup, const OUString& rKey ) const
180 {
181     ::std::hash_map< OUString, SmallOUStrMap, rtl::OUStringHash >::const_iterator group = m_aSettings.find( rGroup );
182     if( group == m_aSettings.end() || group->second.find( rKey ) == group->second.end() )
183     {
184         static OUString aEmpty;
185         return aEmpty;
186     }
187     return group->second.find(rKey)->second;
188 }
189 
190 /*
191  *  SettingsConfigItem::setDefaultFont
192  */
193 
setValue(const OUString & rGroup,const OUString & rKey,const OUString & rValue)194 void SettingsConfigItem::setValue( const OUString& rGroup, const OUString& rKey, const OUString& rValue )
195 {
196     bool bModified = m_aSettings[ rGroup ][ rKey ] != rValue;
197     if( bModified )
198     {
199         m_aSettings[ rGroup ][ rKey ] = rValue;
200         SetModified();
201     }
202 }
203 
204