xref: /trunk/main/vcl/source/gdi/configsettings.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_vcl.hxx"
30 
31 #include <vcl/configsettings.hxx>
32 
33 #include <svdata.hxx>
34 
35 #include <com/sun/star/uno/Any.hxx>
36 #include <com/sun/star/uno/Sequence.hxx>
37 #include <com/sun/star/beans/PropertyValue.hpp>
38 
39 using namespace rtl;
40 using namespace utl;
41 using namespace vcl;
42 using namespace com::sun::star::uno;
43 using namespace com::sun::star::lang;
44 using namespace com::sun::star::beans;
45 using namespace com::sun::star::container;
46 
47 #define SETTINGS_CONFIGNODE "VCL/Settings"
48 
49 /*
50  *	SettingsConfigItem::get
51  */
52 
53 SettingsConfigItem* SettingsConfigItem::get()
54 {
55     ImplSVData* pSVData = ImplGetSVData();
56     if( ! pSVData->mpSettingsConfigItem )
57         pSVData->mpSettingsConfigItem = new SettingsConfigItem();
58     return pSVData->mpSettingsConfigItem;
59 }
60 
61 /*
62  *  SettignsConfigItem constructor
63  */
64 
65 SettingsConfigItem::SettingsConfigItem()
66         :
67         ConfigItem( OUString( RTL_CONSTASCII_USTRINGPARAM( SETTINGS_CONFIGNODE ) ),
68                     CONFIG_MODE_DELAYED_UPDATE ),
69 	m_aSettings( 0 )
70 {
71     getValues();
72 }
73 
74 /*
75  *  SettingsConfigItem destructor
76  */
77 
78 SettingsConfigItem::~SettingsConfigItem()
79 {
80     if( IsModified() )
81         Commit();
82 }
83 
84 /*
85  *  SettingsConfigItem::Commit
86  */
87 
88 void SettingsConfigItem::Commit()
89 {
90     if( ! IsValidConfigMgr() )
91         return;
92 
93     std::hash_map< OUString, SmallOUStrMap, rtl::OUStringHash >::const_iterator group;
94 
95     for( group = m_aSettings.begin(); group != m_aSettings.end(); ++group )
96     {
97         String aKeyName( group->first );
98         /*sal_Bool bAdded =*/ AddNode( OUString(), aKeyName );
99         Sequence< PropertyValue > aValues( group->second.size() );
100         PropertyValue* pValues = aValues.getArray();
101         int nIndex = 0;
102         SmallOUStrMap::const_iterator it;
103         for( it = group->second.begin(); it != group->second.end(); ++it )
104         {
105             String aName( aKeyName );
106             aName.Append( '/' );
107             aName.Append( String( it->first ) );
108             pValues[nIndex].Name	= aName;
109             pValues[nIndex].Handle	= 0;
110             pValues[nIndex].Value <<= it->second;
111             pValues[nIndex].State	= PropertyState_DIRECT_VALUE;
112             nIndex++;
113         }
114         ReplaceSetProperties( aKeyName, aValues );
115     }
116 }
117 
118 /*
119  *  SettingsConfigItem::Notify
120  */
121 
122 void SettingsConfigItem::Notify( const Sequence< OUString >& )
123 {
124     getValues();
125 }
126 
127 /*
128  *  SettingsConfigItem::getValues
129  */
130 void SettingsConfigItem::getValues()
131 {
132     if( ! IsValidConfigMgr() )
133         return;
134 
135     m_aSettings.clear();
136 
137     Sequence< OUString > aNames( GetNodeNames( OUString() ) );
138     m_aSettings.resize( aNames.getLength() );
139 
140     for( int j = 0; j < aNames.getLength(); j++ )
141     {
142 #if OSL_DEBUG_LEVEL > 2
143         fprintf( stderr, "found settings data for \"%s\"\n",
144                  OUStringToOString( aNames.getConstArray()[j], RTL_TEXTENCODING_ASCII_US ).getStr()
145                  );
146 #endif
147         String aKeyName( aNames.getConstArray()[j] );
148         Sequence< OUString > aKeys( GetNodeNames( aKeyName ) );
149         Sequence< OUString > aSettingsKeys( aKeys.getLength() );
150         const OUString* pFrom = aKeys.getConstArray();
151         OUString* pTo = aSettingsKeys.getArray();
152         for( int m = 0; m < aKeys.getLength(); m++ )
153         {
154             String aName( aKeyName );
155             aName.Append( '/' );
156             aName.Append( String( pFrom[m] ) );
157             pTo[m] = aName;
158         }
159         Sequence< Any > aValues( GetProperties( aSettingsKeys ) );
160         const Any* pValue = aValues.getConstArray();
161         for( int i = 0; i < aValues.getLength(); i++, pValue++ )
162         {
163             if( pValue->getValueTypeClass() == TypeClass_STRING )
164             {
165                 const OUString* pLine = (const OUString*)pValue->getValue();
166                 if( pLine->getLength() )
167                     m_aSettings[ aKeyName ][ pFrom[i] ] = *pLine;
168 #if OSL_DEBUG_LEVEL > 2
169                 fprintf( stderr, "   \"%s\"=\"%.30s\"\n",
170                          OUStringToOString( aKeys.getConstArray()[i], RTL_TEXTENCODING_ASCII_US ).getStr(),
171                          OUStringToOString( *pLine, RTL_TEXTENCODING_ASCII_US ).getStr()
172                          );
173 #endif
174             }
175         }
176     }
177 }
178 
179 /*
180  *  SettingsConfigItem::getDefaultFont
181  */
182 
183 const OUString& SettingsConfigItem::getValue( const OUString& rGroup, const OUString& rKey ) const
184 {
185     ::std::hash_map< OUString, SmallOUStrMap, rtl::OUStringHash >::const_iterator group = m_aSettings.find( rGroup );
186     if( group == m_aSettings.end() || group->second.find( rKey ) == group->second.end() )
187     {
188         static OUString aEmpty;
189         return aEmpty;
190     }
191     return group->second.find(rKey)->second;
192 }
193 
194 /*
195  *  SettingsConfigItem::setDefaultFont
196  */
197 
198 void SettingsConfigItem::setValue( const OUString& rGroup, const OUString& rKey, const OUString& rValue )
199 {
200     bool bModified = m_aSettings[ rGroup ][ rKey ] != rValue;
201     if( bModified )
202     {
203         m_aSettings[ rGroup ][ rKey ] = rValue;
204         SetModified();
205     }
206 }
207 
208