Update ooo320-m1
[ooovba.git] / svtools / source / config / optionsdlg.cxx
blobff0cbc0a949fb54d0020cefd64cc28ec42ae6823
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: optionsdlg.cxx,v $
10 * $Revision: 1.6 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svtools.hxx"
34 #include <svtools/optionsdlg.hxx>
35 #include <unotools/configmgr.hxx>
36 #include <unotools/configitem.hxx>
37 #include <tools/debug.hxx>
38 #include <com/sun/star/uno/Any.hxx>
39 #include <com/sun/star/uno/Sequence.hxx>
40 #include <osl/mutex.hxx>
41 #include <comphelper/stl_types.hxx>
43 #include <hash_map>
44 #include "itemholder1.hxx"
46 using namespace utl;
47 using namespace rtl;
48 using namespace com::sun::star::beans ;
49 using namespace com::sun::star::uno;
51 #define CFG_FILENAME OUString( RTL_CONSTASCII_USTRINGPARAM( "Office.OptionsDialog" ) )
52 #define ROOT_NODE OUString( RTL_CONSTASCII_USTRINGPARAM( "OptionsDialogGroups" ) )
53 #define PAGES_NODE OUString( RTL_CONSTASCII_USTRINGPARAM( "Pages" ) )
54 #define OPTIONS_NODE OUString( RTL_CONSTASCII_USTRINGPARAM( "Options" ) )
55 #define PROPERTY_HIDE OUString( RTL_CONSTASCII_USTRINGPARAM( "Hide" ) )
57 static SvtOptionsDlgOptions_Impl* pOptions = NULL;
58 static sal_Int32 nRefCount = 0;
60 class SvtOptionsDlgOptions_Impl : public utl::ConfigItem
62 private:
63 struct OUStringHashCode
65 size_t operator()( const ::rtl::OUString& sString ) const
67 return sString.hashCode();
71 typedef std::hash_map< OUString, sal_Bool, OUStringHashCode, ::std::equal_to< OUString > > OptionNodeList;
73 OUString m_sPathDelimiter;
74 OptionNodeList m_aOptionNodeList;
76 enum NodeType{ NT_Group, NT_Page, NT_Option };
77 void ReadNode( const OUString& _rNode, NodeType _eType );
78 sal_Bool IsHidden( const OUString& _rPath ) const;
80 public:
81 SvtOptionsDlgOptions_Impl();
83 virtual void Notify( const com::sun::star::uno::Sequence< rtl::OUString >& aPropertyNames );
84 virtual void Commit();
86 static ::osl::Mutex & getInitMutex();
88 sal_Bool IsGroupHidden ( const OUString& _rGroup ) const;
89 sal_Bool IsPageHidden ( const OUString& _rPage,
90 const OUString& _rGroup ) const;
91 sal_Bool IsOptionHidden ( const OUString& _rOption,
92 const OUString& _rPage,
93 const OUString& _rGroup ) const;
96 ::osl::Mutex & SvtOptionsDlgOptions_Impl::getInitMutex()
98 static ::osl::Mutex *pMutex = 0;
100 if( ! pMutex )
102 ::osl::MutexGuard guard( ::osl::Mutex::getGlobalMutex() );
103 if( ! pMutex )
105 static ::osl::Mutex mutex;
106 pMutex = &mutex;
109 return *pMutex;
112 // -----------------------------------------------------------------------
114 SvtOptionsDlgOptions_Impl::SvtOptionsDlgOptions_Impl()
115 : ConfigItem( OUString( CFG_FILENAME ) ),
117 m_sPathDelimiter( RTL_CONSTASCII_USTRINGPARAM( "/" ) ),
118 m_aOptionNodeList( OptionNodeList() )
121 OUString sRootNode( ROOT_NODE );
122 Sequence< OUString > aNodeSeq = GetNodeNames( sRootNode );
123 OUString sNode( sRootNode + m_sPathDelimiter );
124 sal_uInt32 nCount = aNodeSeq.getLength();
125 for ( sal_uInt32 n = 0; n < nCount; n++ )
127 OUString sSubNode( sNode + aNodeSeq[n] );
128 ReadNode( sSubNode, NT_Group );
132 // -----------------------------------------------------------------------
134 void SvtOptionsDlgOptions_Impl::Commit()
136 // nothing to commit
139 // -----------------------------------------------------------------------
141 void SvtOptionsDlgOptions_Impl::Notify( const Sequence< rtl::OUString >& )
143 // nothing to notify
146 void SvtOptionsDlgOptions_Impl::ReadNode( const OUString& _rNode, NodeType _eType )
148 OUString sNode( _rNode + m_sPathDelimiter );
149 OUString sSet;
150 sal_Int32 nLen = 0;
151 switch ( _eType )
153 case NT_Group :
155 sSet = PAGES_NODE;
156 nLen = 2;
157 break;
160 case NT_Page :
162 sSet = OPTIONS_NODE;
163 nLen = 2;
164 break;
167 case NT_Option :
169 nLen = 1;
170 break;
174 Sequence< OUString > lResult( nLen );
175 lResult[0] = OUString( sNode + PROPERTY_HIDE );
176 if ( _eType != NT_Option )
177 lResult[1] = OUString( sNode + sSet );
179 Sequence< Any > aValues;
180 aValues = GetProperties( lResult );
181 sal_Bool bHide = sal_False;
182 if ( aValues[0] >>= bHide )
183 m_aOptionNodeList.insert( OptionNodeList::value_type( sNode, bHide ) );
185 if ( _eType != NT_Option )
187 OUString sNodes( sNode + sSet );
188 Sequence< OUString > aNodes = GetNodeNames( sNodes );
189 if ( aNodes.getLength() > 0 )
191 for ( sal_uInt32 n = 0; n < (sal_uInt32)aNodes.getLength(); ++n )
193 OUString sSubNodeName( sNodes + m_sPathDelimiter + aNodes[n] );
194 ReadNode( sSubNodeName, _eType == NT_Group ? NT_Page : NT_Option );
200 // -----------------------------------------------------------------------
202 OUString getGroupPath( const OUString& _rGroup )
204 return OUString( ROOT_NODE + OUString('/') + _rGroup + OUString('/') );
206 OUString getPagePath( const OUString& _rPage )
208 return OUString( PAGES_NODE + OUString('/') + _rPage + OUString('/') );
210 OUString getOptionPath( const OUString& _rOption )
212 return OUString( OPTIONS_NODE + OUString('/') + _rOption + OUString('/') );
215 // -----------------------------------------------------------------------
217 sal_Bool SvtOptionsDlgOptions_Impl::IsHidden( const OUString& _rPath ) const
219 sal_Bool bRet = sal_False;
220 OptionNodeList::const_iterator pIter = m_aOptionNodeList.find( _rPath );
221 if ( pIter != m_aOptionNodeList.end() )
222 bRet = pIter->second;
223 return bRet;
226 // -----------------------------------------------------------------------
228 sal_Bool SvtOptionsDlgOptions_Impl::IsGroupHidden( const OUString& _rGroup ) const
230 return IsHidden( getGroupPath( _rGroup ) );
233 // -----------------------------------------------------------------------
235 sal_Bool SvtOptionsDlgOptions_Impl::IsPageHidden( const OUString& _rPage, const OUString& _rGroup ) const
237 return IsHidden( getGroupPath( _rGroup ) + getPagePath( _rPage ) );
240 // -----------------------------------------------------------------------
242 sal_Bool SvtOptionsDlgOptions_Impl::IsOptionHidden(
243 const OUString& _rOption, const OUString& _rPage, const OUString& _rGroup ) const
245 return IsHidden( getGroupPath( _rGroup ) + getPagePath( _rPage ) + getOptionPath( _rOption ) );
248 // -----------------------------------------------------------------------
250 SvtOptionsDialogOptions::SvtOptionsDialogOptions()
252 // Global access, must be guarded (multithreading)
253 ::osl::MutexGuard aGuard( SvtOptionsDlgOptions_Impl::getInitMutex() );
254 ++nRefCount;
255 if ( !pOptions )
257 pOptions = new SvtOptionsDlgOptions_Impl;
259 ItemHolder1::holdConfigItem( E_OPTIONSDLGOPTIONS );
261 m_pImp = pOptions;
264 // -----------------------------------------------------------------------
266 SvtOptionsDialogOptions::~SvtOptionsDialogOptions()
268 // Global access, must be guarded (multithreading)
269 ::osl::MutexGuard aGuard( SvtOptionsDlgOptions_Impl::getInitMutex() );
270 if ( !--nRefCount )
272 if ( pOptions->IsModified() )
273 pOptions->Commit();
274 DELETEZ( pOptions );
278 sal_Bool SvtOptionsDialogOptions::IsGroupHidden( const String& _rGroup ) const
280 return m_pImp->IsGroupHidden( _rGroup );
283 sal_Bool SvtOptionsDialogOptions::IsPageHidden( const String& _rPage, const String& _rGroup ) const
285 return m_pImp->IsPageHidden( _rPage, _rGroup );
288 sal_Bool SvtOptionsDialogOptions::IsOptionHidden(
289 const String& _rOption, const String& _rPage, const String& _rGroup ) const
291 return m_pImp->IsOptionHidden( _rOption, _rPage, _rGroup );