Version 4.0.0.1, tag libreoffice-4.0.0.1
[LibreOffice.git] / unotools / source / config / optionsdlg.cxx
blob69928e52d0d9859e0fd45aca7cfefb53c957e5d2
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <unotools/optionsdlg.hxx>
22 #include <unotools/configmgr.hxx>
23 #include <unotools/configitem.hxx>
24 #include <com/sun/star/uno/Any.hxx>
25 #include <com/sun/star/uno/Sequence.hxx>
26 #include <osl/mutex.hxx>
27 #include <comphelper/stl_types.hxx>
29 #include <boost/unordered_map.hpp>
30 #include "itemholder1.hxx"
32 using namespace utl;
33 using namespace com::sun::star::beans ;
34 using namespace com::sun::star::uno;
36 using ::rtl::OUString;
38 #define CFG_FILENAME OUString( RTL_CONSTASCII_USTRINGPARAM( "Office.OptionsDialog" ) )
39 #define ROOT_NODE OUString( RTL_CONSTASCII_USTRINGPARAM( "OptionsDialogGroups" ) )
40 #define PAGES_NODE OUString( RTL_CONSTASCII_USTRINGPARAM( "Pages" ) )
41 #define OPTIONS_NODE OUString( RTL_CONSTASCII_USTRINGPARAM( "Options" ) )
42 #define PROPERTY_HIDE OUString( RTL_CONSTASCII_USTRINGPARAM( "Hide" ) )
44 static SvtOptionsDlgOptions_Impl* pOptions = NULL;
45 static sal_Int32 nRefCount = 0;
47 class SvtOptionsDlgOptions_Impl : public utl::ConfigItem
49 private:
50 struct OUStringHashCode
52 size_t operator()( const ::rtl::OUString& sString ) const
54 return sString.hashCode();
58 typedef boost::unordered_map< OUString, sal_Bool, OUStringHashCode, ::std::equal_to< OUString > > OptionNodeList;
60 OUString m_sPathDelimiter;
61 OptionNodeList m_aOptionNodeList;
63 enum NodeType{ NT_Group, NT_Page, NT_Option };
64 void ReadNode( const OUString& _rNode, NodeType _eType );
65 sal_Bool IsHidden( const OUString& _rPath ) const;
67 public:
68 SvtOptionsDlgOptions_Impl();
70 virtual void Notify( const com::sun::star::uno::Sequence< rtl::OUString >& aPropertyNames );
71 virtual void Commit();
73 static ::osl::Mutex & getInitMutex();
75 sal_Bool IsGroupHidden ( const OUString& _rGroup ) const;
76 sal_Bool IsPageHidden ( const OUString& _rPage,
77 const OUString& _rGroup ) const;
78 sal_Bool IsOptionHidden ( const OUString& _rOption,
79 const OUString& _rPage,
80 const OUString& _rGroup ) const;
83 namespace
85 class theOptionsDlgOptions_ImplMutex : public rtl::Static<osl::Mutex, theOptionsDlgOptions_ImplMutex>{};
88 ::osl::Mutex & SvtOptionsDlgOptions_Impl::getInitMutex()
90 return theOptionsDlgOptions_ImplMutex::get();
93 // -----------------------------------------------------------------------
95 SvtOptionsDlgOptions_Impl::SvtOptionsDlgOptions_Impl()
96 : ConfigItem( OUString( CFG_FILENAME ) ),
98 m_sPathDelimiter( RTL_CONSTASCII_USTRINGPARAM( "/" ) ),
99 m_aOptionNodeList( OptionNodeList() )
102 OUString sRootNode( ROOT_NODE );
103 Sequence< OUString > aNodeSeq = GetNodeNames( sRootNode );
104 OUString sNode( sRootNode + m_sPathDelimiter );
105 sal_uInt32 nCount = aNodeSeq.getLength();
106 for ( sal_uInt32 n = 0; n < nCount; n++ )
108 OUString sSubNode( sNode + aNodeSeq[n] );
109 ReadNode( sSubNode, NT_Group );
113 // -----------------------------------------------------------------------
115 void SvtOptionsDlgOptions_Impl::Commit()
117 // nothing to commit
120 // -----------------------------------------------------------------------
122 void SvtOptionsDlgOptions_Impl::Notify( const Sequence< rtl::OUString >& )
124 // nothing to notify
127 void SvtOptionsDlgOptions_Impl::ReadNode( const OUString& _rNode, NodeType _eType )
129 OUString sNode( _rNode + m_sPathDelimiter );
130 OUString sSet;
131 sal_Int32 nLen = 0;
132 switch ( _eType )
134 case NT_Group :
136 sSet = PAGES_NODE;
137 nLen = 2;
138 break;
141 case NT_Page :
143 sSet = OPTIONS_NODE;
144 nLen = 2;
145 break;
148 case NT_Option :
150 nLen = 1;
151 break;
155 Sequence< OUString > lResult( nLen );
156 lResult[0] = OUString( sNode + PROPERTY_HIDE );
157 if ( _eType != NT_Option )
158 lResult[1] = OUString( sNode + sSet );
160 Sequence< Any > aValues;
161 aValues = GetProperties( lResult );
162 sal_Bool bHide = sal_False;
163 if ( aValues[0] >>= bHide )
164 m_aOptionNodeList.insert( OptionNodeList::value_type( sNode, bHide ) );
166 if ( _eType != NT_Option )
168 OUString sNodes( sNode + sSet );
169 Sequence< OUString > aNodes = GetNodeNames( sNodes );
170 if ( aNodes.getLength() > 0 )
172 for ( sal_uInt32 n = 0; n < (sal_uInt32)aNodes.getLength(); ++n )
174 OUString sSubNodeName( sNodes + m_sPathDelimiter + aNodes[n] );
175 ReadNode( sSubNodeName, _eType == NT_Group ? NT_Page : NT_Option );
181 // -----------------------------------------------------------------------
183 OUString getGroupPath( const OUString& _rGroup )
185 return OUString( ROOT_NODE + OUString('/') + _rGroup + OUString('/') );
187 OUString getPagePath( const OUString& _rPage )
189 return OUString( PAGES_NODE + OUString('/') + _rPage + OUString('/') );
191 OUString getOptionPath( const OUString& _rOption )
193 return OUString( OPTIONS_NODE + OUString('/') + _rOption + OUString('/') );
196 // -----------------------------------------------------------------------
198 sal_Bool SvtOptionsDlgOptions_Impl::IsHidden( const OUString& _rPath ) const
200 sal_Bool bRet = sal_False;
201 OptionNodeList::const_iterator pIter = m_aOptionNodeList.find( _rPath );
202 if ( pIter != m_aOptionNodeList.end() )
203 bRet = pIter->second;
204 return bRet;
207 // -----------------------------------------------------------------------
209 sal_Bool SvtOptionsDlgOptions_Impl::IsGroupHidden( const OUString& _rGroup ) const
211 return IsHidden( getGroupPath( _rGroup ) );
214 // -----------------------------------------------------------------------
216 sal_Bool SvtOptionsDlgOptions_Impl::IsPageHidden( const OUString& _rPage, const OUString& _rGroup ) const
218 return IsHidden( getGroupPath( _rGroup ) + getPagePath( _rPage ) );
221 // -----------------------------------------------------------------------
223 sal_Bool SvtOptionsDlgOptions_Impl::IsOptionHidden(
224 const OUString& _rOption, const OUString& _rPage, const OUString& _rGroup ) const
226 return IsHidden( getGroupPath( _rGroup ) + getPagePath( _rPage ) + getOptionPath( _rOption ) );
229 // -----------------------------------------------------------------------
231 SvtOptionsDialogOptions::SvtOptionsDialogOptions()
233 // Global access, must be guarded (multithreading)
234 ::osl::MutexGuard aGuard( SvtOptionsDlgOptions_Impl::getInitMutex() );
235 ++nRefCount;
236 if ( !pOptions )
238 pOptions = new SvtOptionsDlgOptions_Impl;
240 ItemHolder1::holdConfigItem( E_OPTIONSDLGOPTIONS );
242 m_pImp = pOptions;
245 // -----------------------------------------------------------------------
247 SvtOptionsDialogOptions::~SvtOptionsDialogOptions()
249 // Global access, must be guarded (multithreading)
250 ::osl::MutexGuard aGuard( SvtOptionsDlgOptions_Impl::getInitMutex() );
251 if ( !--nRefCount )
253 if ( pOptions->IsModified() )
254 pOptions->Commit();
255 delete pOptions;
256 pOptions = NULL;
260 sal_Bool SvtOptionsDialogOptions::IsGroupHidden( const rtl::OUString& _rGroup ) const
262 return m_pImp->IsGroupHidden( _rGroup );
265 sal_Bool SvtOptionsDialogOptions::IsPageHidden( const rtl::OUString& _rPage, const rtl::OUString& _rGroup ) const
267 return m_pImp->IsPageHidden( _rPage, _rGroup );
270 sal_Bool SvtOptionsDialogOptions::IsOptionHidden(
271 const rtl::OUString& _rOption, const rtl::OUString& _rPage, const rtl::OUString& _rGroup ) const
273 return m_pImp->IsOptionHidden( _rOption, _rPage, _rGroup );
276 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */