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