1*b5088357SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*b5088357SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*b5088357SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*b5088357SAndrew Rist  * distributed with this work for additional information
6*b5088357SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*b5088357SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*b5088357SAndrew Rist  * "License"); you may not use this file except in compliance
9*b5088357SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*b5088357SAndrew Rist  *
11*b5088357SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*b5088357SAndrew Rist  *
13*b5088357SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*b5088357SAndrew Rist  * software distributed under the License is distributed on an
15*b5088357SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b5088357SAndrew Rist  * KIND, either express or implied.  See the License for the
17*b5088357SAndrew Rist  * specific language governing permissions and limitations
18*b5088357SAndrew Rist  * under the License.
19*b5088357SAndrew Rist  *
20*b5088357SAndrew Rist  *************************************************************/
21*b5088357SAndrew Rist 
22*b5088357SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_unotools.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <unotools/optionsdlg.hxx>
28cdf0e10cSrcweir #include <unotools/configmgr.hxx>
29cdf0e10cSrcweir #include <unotools/configitem.hxx>
30cdf0e10cSrcweir #include <tools/debug.hxx>
31cdf0e10cSrcweir #include <com/sun/star/uno/Any.hxx>
32cdf0e10cSrcweir #include <com/sun/star/uno/Sequence.hxx>
33cdf0e10cSrcweir #include <osl/mutex.hxx>
34cdf0e10cSrcweir #include <comphelper/stl_types.hxx>
35cdf0e10cSrcweir 
36cdf0e10cSrcweir #include <hash_map>
37cdf0e10cSrcweir #include "itemholder1.hxx"
38cdf0e10cSrcweir 
39cdf0e10cSrcweir using namespace utl;
40cdf0e10cSrcweir using namespace rtl;
41cdf0e10cSrcweir using namespace com::sun::star::beans ;
42cdf0e10cSrcweir using namespace com::sun::star::uno;
43cdf0e10cSrcweir 
44cdf0e10cSrcweir #define CFG_FILENAME            OUString( RTL_CONSTASCII_USTRINGPARAM( "Office.OptionsDialog" ) )
45cdf0e10cSrcweir #define ROOT_NODE               OUString( RTL_CONSTASCII_USTRINGPARAM( "OptionsDialogGroups" ) )
46cdf0e10cSrcweir #define PAGES_NODE              OUString( RTL_CONSTASCII_USTRINGPARAM( "Pages" ) )
47cdf0e10cSrcweir #define OPTIONS_NODE            OUString( RTL_CONSTASCII_USTRINGPARAM( "Options" ) )
48cdf0e10cSrcweir #define PROPERTY_HIDE           OUString( RTL_CONSTASCII_USTRINGPARAM( "Hide" ) )
49cdf0e10cSrcweir 
50cdf0e10cSrcweir static SvtOptionsDlgOptions_Impl*   pOptions = NULL;
51cdf0e10cSrcweir static sal_Int32                    nRefCount = 0;
52cdf0e10cSrcweir 
53cdf0e10cSrcweir class SvtOptionsDlgOptions_Impl : public utl::ConfigItem
54cdf0e10cSrcweir {
55cdf0e10cSrcweir private:
56cdf0e10cSrcweir     struct OUStringHashCode
57cdf0e10cSrcweir     {
operator ()SvtOptionsDlgOptions_Impl::OUStringHashCode58cdf0e10cSrcweir         size_t operator()( const ::rtl::OUString& sString ) const
59cdf0e10cSrcweir         {
60cdf0e10cSrcweir             return sString.hashCode();
61cdf0e10cSrcweir         }
62cdf0e10cSrcweir     };
63cdf0e10cSrcweir 
64cdf0e10cSrcweir     typedef std::hash_map< OUString, sal_Bool, OUStringHashCode, ::std::equal_to< OUString > > OptionNodeList;
65cdf0e10cSrcweir 
66cdf0e10cSrcweir     OUString        m_sPathDelimiter;
67cdf0e10cSrcweir     OptionNodeList  m_aOptionNodeList;
68cdf0e10cSrcweir 
69cdf0e10cSrcweir     enum NodeType{ NT_Group, NT_Page, NT_Option };
70cdf0e10cSrcweir     void            ReadNode( const OUString& _rNode, NodeType _eType );
71cdf0e10cSrcweir     sal_Bool        IsHidden( const OUString& _rPath ) const;
72cdf0e10cSrcweir 
73cdf0e10cSrcweir public:
74cdf0e10cSrcweir                     SvtOptionsDlgOptions_Impl();
75cdf0e10cSrcweir 
76cdf0e10cSrcweir     virtual void    Notify( const com::sun::star::uno::Sequence< rtl::OUString >& aPropertyNames );
77cdf0e10cSrcweir     virtual void    Commit();
78cdf0e10cSrcweir 
79cdf0e10cSrcweir     static ::osl::Mutex & getInitMutex();
80cdf0e10cSrcweir 
81cdf0e10cSrcweir     sal_Bool        IsGroupHidden   (   const OUString& _rGroup ) const;
82cdf0e10cSrcweir     sal_Bool        IsPageHidden    (   const OUString& _rPage,
83cdf0e10cSrcweir                                         const OUString& _rGroup ) const;
84cdf0e10cSrcweir     sal_Bool        IsOptionHidden  (   const OUString& _rOption,
85cdf0e10cSrcweir                                         const OUString& _rPage,
86cdf0e10cSrcweir                                         const OUString& _rGroup ) const;
87cdf0e10cSrcweir };
88cdf0e10cSrcweir 
getInitMutex()89cdf0e10cSrcweir ::osl::Mutex & SvtOptionsDlgOptions_Impl::getInitMutex()
90cdf0e10cSrcweir {
91cdf0e10cSrcweir     static ::osl::Mutex *pMutex = 0;
92cdf0e10cSrcweir 
93cdf0e10cSrcweir     if( ! pMutex )
94cdf0e10cSrcweir     {
95cdf0e10cSrcweir         ::osl::MutexGuard guard( ::osl::Mutex::getGlobalMutex() );
96cdf0e10cSrcweir         if( ! pMutex )
97cdf0e10cSrcweir         {
98cdf0e10cSrcweir             static ::osl::Mutex mutex;
99cdf0e10cSrcweir             pMutex = &mutex;
100cdf0e10cSrcweir         }
101cdf0e10cSrcweir     }
102cdf0e10cSrcweir     return *pMutex;
103cdf0e10cSrcweir }
104cdf0e10cSrcweir 
105cdf0e10cSrcweir // -----------------------------------------------------------------------
106cdf0e10cSrcweir 
SvtOptionsDlgOptions_Impl()107cdf0e10cSrcweir SvtOptionsDlgOptions_Impl::SvtOptionsDlgOptions_Impl()
108cdf0e10cSrcweir     : ConfigItem( OUString( CFG_FILENAME ) ),
109cdf0e10cSrcweir 
110cdf0e10cSrcweir     m_sPathDelimiter( RTL_CONSTASCII_USTRINGPARAM( "/" ) ),
111cdf0e10cSrcweir     m_aOptionNodeList( OptionNodeList() )
112cdf0e10cSrcweir 
113cdf0e10cSrcweir {
114cdf0e10cSrcweir     OUString sRootNode( ROOT_NODE );
115cdf0e10cSrcweir     Sequence< OUString > aNodeSeq = GetNodeNames( sRootNode );
116cdf0e10cSrcweir     OUString sNode( sRootNode + m_sPathDelimiter );
117cdf0e10cSrcweir     sal_uInt32 nCount = aNodeSeq.getLength();
118cdf0e10cSrcweir     for ( sal_uInt32 n = 0; n < nCount; n++ )
119cdf0e10cSrcweir     {
120cdf0e10cSrcweir         OUString sSubNode( sNode + aNodeSeq[n] );
121cdf0e10cSrcweir         ReadNode( sSubNode, NT_Group );
122cdf0e10cSrcweir     }
123cdf0e10cSrcweir }
124cdf0e10cSrcweir 
125cdf0e10cSrcweir // -----------------------------------------------------------------------
126cdf0e10cSrcweir 
Commit()127cdf0e10cSrcweir void SvtOptionsDlgOptions_Impl::Commit()
128cdf0e10cSrcweir {
129cdf0e10cSrcweir     // nothing to commit
130cdf0e10cSrcweir }
131cdf0e10cSrcweir 
132cdf0e10cSrcweir // -----------------------------------------------------------------------
133cdf0e10cSrcweir 
Notify(const Sequence<rtl::OUString> &)134cdf0e10cSrcweir void SvtOptionsDlgOptions_Impl::Notify( const Sequence< rtl::OUString >& )
135cdf0e10cSrcweir {
136cdf0e10cSrcweir     // nothing to notify
137cdf0e10cSrcweir }
138cdf0e10cSrcweir 
ReadNode(const OUString & _rNode,NodeType _eType)139cdf0e10cSrcweir void SvtOptionsDlgOptions_Impl::ReadNode( const OUString& _rNode, NodeType _eType )
140cdf0e10cSrcweir {
141cdf0e10cSrcweir     OUString sNode( _rNode + m_sPathDelimiter );
142cdf0e10cSrcweir     OUString sSet;
143cdf0e10cSrcweir     sal_Int32 nLen = 0;
144cdf0e10cSrcweir     switch ( _eType )
145cdf0e10cSrcweir     {
146cdf0e10cSrcweir         case NT_Group :
147cdf0e10cSrcweir         {
148cdf0e10cSrcweir             sSet = PAGES_NODE;
149cdf0e10cSrcweir             nLen = 2;
150cdf0e10cSrcweir             break;
151cdf0e10cSrcweir         }
152cdf0e10cSrcweir 
153cdf0e10cSrcweir         case NT_Page :
154cdf0e10cSrcweir         {
155cdf0e10cSrcweir             sSet = OPTIONS_NODE;
156cdf0e10cSrcweir             nLen = 2;
157cdf0e10cSrcweir             break;
158cdf0e10cSrcweir         }
159cdf0e10cSrcweir 
160cdf0e10cSrcweir         case NT_Option :
161cdf0e10cSrcweir         {
162cdf0e10cSrcweir             nLen = 1;
163cdf0e10cSrcweir             break;
164cdf0e10cSrcweir         }
165cdf0e10cSrcweir     }
166cdf0e10cSrcweir 
167cdf0e10cSrcweir     Sequence< OUString > lResult( nLen );
168cdf0e10cSrcweir     lResult[0] = OUString( sNode + PROPERTY_HIDE );
169cdf0e10cSrcweir     if ( _eType != NT_Option )
170cdf0e10cSrcweir         lResult[1] = OUString( sNode + sSet );
171cdf0e10cSrcweir 
172cdf0e10cSrcweir     Sequence< Any > aValues;
173cdf0e10cSrcweir     aValues = GetProperties( lResult );
174cdf0e10cSrcweir     sal_Bool bHide = sal_False;
175cdf0e10cSrcweir     if ( aValues[0] >>= bHide )
176cdf0e10cSrcweir         m_aOptionNodeList.insert( OptionNodeList::value_type( sNode, bHide ) );
177cdf0e10cSrcweir 
178cdf0e10cSrcweir     if ( _eType != NT_Option )
179cdf0e10cSrcweir     {
180cdf0e10cSrcweir         OUString sNodes( sNode + sSet );
181cdf0e10cSrcweir         Sequence< OUString > aNodes = GetNodeNames( sNodes );
182cdf0e10cSrcweir         if ( aNodes.getLength() > 0 )
183cdf0e10cSrcweir         {
184cdf0e10cSrcweir             for ( sal_uInt32 n = 0; n < (sal_uInt32)aNodes.getLength(); ++n )
185cdf0e10cSrcweir             {
186cdf0e10cSrcweir                 OUString sSubNodeName( sNodes + m_sPathDelimiter + aNodes[n] );
187cdf0e10cSrcweir                 ReadNode( sSubNodeName, _eType == NT_Group ? NT_Page : NT_Option );
188cdf0e10cSrcweir             }
189cdf0e10cSrcweir         }
190cdf0e10cSrcweir     }
191cdf0e10cSrcweir }
192cdf0e10cSrcweir 
193cdf0e10cSrcweir // -----------------------------------------------------------------------
194cdf0e10cSrcweir 
getGroupPath(const OUString & _rGroup)195cdf0e10cSrcweir OUString getGroupPath( const OUString& _rGroup )
196cdf0e10cSrcweir {
197cdf0e10cSrcweir     return OUString( ROOT_NODE + OUString('/') + _rGroup + OUString('/') );
198cdf0e10cSrcweir }
getPagePath(const OUString & _rPage)199cdf0e10cSrcweir OUString getPagePath( const OUString& _rPage )
200cdf0e10cSrcweir {
201cdf0e10cSrcweir     return OUString( PAGES_NODE + OUString('/') + _rPage + OUString('/') );
202cdf0e10cSrcweir }
getOptionPath(const OUString & _rOption)203cdf0e10cSrcweir OUString getOptionPath( const OUString& _rOption )
204cdf0e10cSrcweir {
205cdf0e10cSrcweir     return OUString( OPTIONS_NODE + OUString('/') + _rOption + OUString('/') );
206cdf0e10cSrcweir }
207cdf0e10cSrcweir 
208cdf0e10cSrcweir // -----------------------------------------------------------------------
209cdf0e10cSrcweir 
IsHidden(const OUString & _rPath) const210cdf0e10cSrcweir sal_Bool SvtOptionsDlgOptions_Impl::IsHidden( const OUString& _rPath ) const
211cdf0e10cSrcweir {
212cdf0e10cSrcweir     sal_Bool bRet = sal_False;
213cdf0e10cSrcweir     OptionNodeList::const_iterator pIter = m_aOptionNodeList.find( _rPath );
214cdf0e10cSrcweir     if ( pIter != m_aOptionNodeList.end() )
215cdf0e10cSrcweir         bRet = pIter->second;
216cdf0e10cSrcweir     return bRet;
217cdf0e10cSrcweir }
218cdf0e10cSrcweir 
219cdf0e10cSrcweir // -----------------------------------------------------------------------
220cdf0e10cSrcweir 
IsGroupHidden(const OUString & _rGroup) const221cdf0e10cSrcweir sal_Bool SvtOptionsDlgOptions_Impl::IsGroupHidden( const OUString& _rGroup ) const
222cdf0e10cSrcweir {
223cdf0e10cSrcweir     return IsHidden( getGroupPath( _rGroup ) );
224cdf0e10cSrcweir }
225cdf0e10cSrcweir 
226cdf0e10cSrcweir // -----------------------------------------------------------------------
227cdf0e10cSrcweir 
IsPageHidden(const OUString & _rPage,const OUString & _rGroup) const228cdf0e10cSrcweir sal_Bool SvtOptionsDlgOptions_Impl::IsPageHidden( const OUString& _rPage, const OUString& _rGroup ) const
229cdf0e10cSrcweir {
230cdf0e10cSrcweir     return IsHidden( getGroupPath( _rGroup  ) + getPagePath( _rPage ) );
231cdf0e10cSrcweir }
232cdf0e10cSrcweir 
233cdf0e10cSrcweir // -----------------------------------------------------------------------
234cdf0e10cSrcweir 
IsOptionHidden(const OUString & _rOption,const OUString & _rPage,const OUString & _rGroup) const235cdf0e10cSrcweir sal_Bool SvtOptionsDlgOptions_Impl::IsOptionHidden(
236cdf0e10cSrcweir     const OUString& _rOption, const OUString& _rPage, const OUString& _rGroup ) const
237cdf0e10cSrcweir {
238cdf0e10cSrcweir     return IsHidden( getGroupPath( _rGroup  ) + getPagePath( _rPage ) + getOptionPath( _rOption ) );
239cdf0e10cSrcweir }
240cdf0e10cSrcweir 
241cdf0e10cSrcweir // -----------------------------------------------------------------------
242cdf0e10cSrcweir 
SvtOptionsDialogOptions()243cdf0e10cSrcweir SvtOptionsDialogOptions::SvtOptionsDialogOptions()
244cdf0e10cSrcweir {
245cdf0e10cSrcweir     // Global access, must be guarded (multithreading)
246cdf0e10cSrcweir     ::osl::MutexGuard aGuard( SvtOptionsDlgOptions_Impl::getInitMutex() );
247cdf0e10cSrcweir     ++nRefCount;
248cdf0e10cSrcweir     if ( !pOptions )
249cdf0e10cSrcweir     {
250cdf0e10cSrcweir         pOptions = new SvtOptionsDlgOptions_Impl;
251cdf0e10cSrcweir 
252cdf0e10cSrcweir         ItemHolder1::holdConfigItem( E_OPTIONSDLGOPTIONS );
253cdf0e10cSrcweir     }
254cdf0e10cSrcweir     m_pImp = pOptions;
255cdf0e10cSrcweir }
256cdf0e10cSrcweir 
257cdf0e10cSrcweir // -----------------------------------------------------------------------
258cdf0e10cSrcweir 
~SvtOptionsDialogOptions()259cdf0e10cSrcweir SvtOptionsDialogOptions::~SvtOptionsDialogOptions()
260cdf0e10cSrcweir {
261cdf0e10cSrcweir     // Global access, must be guarded (multithreading)
262cdf0e10cSrcweir     ::osl::MutexGuard aGuard( SvtOptionsDlgOptions_Impl::getInitMutex() );
263cdf0e10cSrcweir     if ( !--nRefCount )
264cdf0e10cSrcweir 	{
265cdf0e10cSrcweir 		if ( pOptions->IsModified() )
266cdf0e10cSrcweir 			pOptions->Commit();
267cdf0e10cSrcweir         DELETEZ( pOptions );
268cdf0e10cSrcweir 	}
269cdf0e10cSrcweir }
270cdf0e10cSrcweir 
IsGroupHidden(const String & _rGroup) const271cdf0e10cSrcweir sal_Bool SvtOptionsDialogOptions::IsGroupHidden( const String& _rGroup ) const
272cdf0e10cSrcweir {
273cdf0e10cSrcweir     return m_pImp->IsGroupHidden( _rGroup );
274cdf0e10cSrcweir }
275cdf0e10cSrcweir 
IsPageHidden(const String & _rPage,const String & _rGroup) const276cdf0e10cSrcweir sal_Bool SvtOptionsDialogOptions::IsPageHidden( const String& _rPage, const String& _rGroup ) const
277cdf0e10cSrcweir {
278cdf0e10cSrcweir     return m_pImp->IsPageHidden( _rPage, _rGroup );
279cdf0e10cSrcweir }
280cdf0e10cSrcweir 
IsOptionHidden(const String & _rOption,const String & _rPage,const String & _rGroup) const281cdf0e10cSrcweir sal_Bool SvtOptionsDialogOptions::IsOptionHidden(
282cdf0e10cSrcweir     const String& _rOption, const String& _rPage, const String& _rGroup ) const
283cdf0e10cSrcweir {
284cdf0e10cSrcweir     return m_pImp->IsOptionHidden( _rOption, _rPage, _rGroup );
285cdf0e10cSrcweir }
286cdf0e10cSrcweir 
287