1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
20 #include <unotools/optionsdlg.hxx>
21 #include <unotools/configitem.hxx>
22 #include <com/sun/star/uno/Any.hxx>
23 #include <com/sun/star/uno/Sequence.hxx>
24 #include <osl/mutex.hxx>
26 #include "itemholder1.hxx"
28 #include <unordered_map>
31 using namespace com::sun::star::beans
;
32 using namespace com::sun::star::uno
;
34 #define CFG_FILENAME "Office.OptionsDialog"
35 #define ROOT_NODE "OptionsDialogGroups"
36 #define PAGES_NODE "Pages"
37 #define OPTIONS_NODE "Options"
39 static SvtOptionsDlgOptions_Impl
* pOptions
= nullptr;
40 static sal_Int32 nRefCount
= 0;
42 class SvtOptionsDlgOptions_Impl
: public utl::ConfigItem
45 typedef std::unordered_map
< OUString
, bool > OptionNodeList
;
47 static constexpr OUStringLiteral g_sPathDelimiter
= "/";
48 OptionNodeList m_aOptionNodeList
;
50 enum NodeType
{ NT_Group
, NT_Page
, NT_Option
};
51 void ReadNode( const OUString
& _rNode
, NodeType _eType
);
52 bool IsHidden( const OUString
& _rPath
) const;
54 virtual void ImplCommit() override
;
57 SvtOptionsDlgOptions_Impl();
59 virtual void Notify( const css::uno::Sequence
< OUString
>& aPropertyNames
) override
;
61 static ::osl::Mutex
& getInitMutex();
63 bool IsGroupHidden ( const OUString
& _rGroup
) const;
64 bool IsPageHidden ( const OUString
& _rPage
,
65 const OUString
& _rGroup
) const;
66 bool IsOptionHidden ( const OUString
& _rOption
,
67 const OUString
& _rPage
,
68 const OUString
& _rGroup
) const;
73 class theOptionsDlgOptions_ImplMutex
: public rtl::Static
<osl::Mutex
, theOptionsDlgOptions_ImplMutex
>{};
76 ::osl::Mutex
& SvtOptionsDlgOptions_Impl::getInitMutex()
78 return theOptionsDlgOptions_ImplMutex::get();
81 SvtOptionsDlgOptions_Impl::SvtOptionsDlgOptions_Impl()
82 : ConfigItem( CFG_FILENAME
),
83 m_aOptionNodeList( OptionNodeList() )
85 OUString
sRootNode( ROOT_NODE
);
86 const Sequence
< OUString
> aNodeSeq
= GetNodeNames( sRootNode
);
87 OUString
sNode( sRootNode
+ g_sPathDelimiter
);
88 for ( const auto& rNode
: aNodeSeq
)
90 OUString
sSubNode( sNode
+ rNode
);
91 ReadNode( sSubNode
, NT_Group
);
95 void SvtOptionsDlgOptions_Impl::ImplCommit()
100 void SvtOptionsDlgOptions_Impl::Notify( const Sequence
< OUString
>& )
105 void SvtOptionsDlgOptions_Impl::ReadNode( const OUString
& _rNode
, NodeType _eType
)
107 OUString
sNode( _rNode
+ g_sPathDelimiter
);
133 Sequence
< OUString
> lResult( nLen
);
134 lResult
[0] = sNode
+ "Hide";
135 if ( _eType
!= NT_Option
)
136 lResult
[1] = sNode
+ sSet
;
138 Sequence
< Any
> aValues
= GetProperties( lResult
);
140 if ( aValues
[0] >>= bHide
)
141 m_aOptionNodeList
.emplace( sNode
, bHide
);
143 if ( _eType
!= NT_Option
)
145 OUString
sNodes( sNode
+ sSet
);
146 const Sequence
< OUString
> aNodes
= GetNodeNames( sNodes
);
147 for ( const auto& rNode
: aNodes
)
149 OUString
sSubNodeName( sNodes
+ g_sPathDelimiter
+ rNode
);
150 ReadNode( sSubNodeName
, _eType
== NT_Group
? NT_Page
: NT_Option
);
155 static OUString
getGroupPath( const OUString
& _rGroup
)
157 return OUString( ROOT_NODE
"/" + _rGroup
+ "/" );
159 static OUString
getPagePath( const OUString
& _rPage
)
161 return OUString( PAGES_NODE
"/" + _rPage
+ "/" );
163 static OUString
getOptionPath( const OUString
& _rOption
)
165 return OUString( OPTIONS_NODE
"/" + _rOption
+ "/" );
168 bool SvtOptionsDlgOptions_Impl::IsHidden( const OUString
& _rPath
) const
171 OptionNodeList::const_iterator pIter
= m_aOptionNodeList
.find( _rPath
);
172 if ( pIter
!= m_aOptionNodeList
.end() )
173 bRet
= pIter
->second
;
177 bool SvtOptionsDlgOptions_Impl::IsGroupHidden( const OUString
& _rGroup
) const
179 return IsHidden( getGroupPath( _rGroup
) );
182 bool SvtOptionsDlgOptions_Impl::IsPageHidden( const OUString
& _rPage
, const OUString
& _rGroup
) const
184 return IsHidden( getGroupPath( _rGroup
) + getPagePath( _rPage
) );
187 bool SvtOptionsDlgOptions_Impl::IsOptionHidden(
188 const OUString
& _rOption
, const OUString
& _rPage
, const OUString
& _rGroup
) const
190 return IsHidden( getGroupPath( _rGroup
) + getPagePath( _rPage
) + getOptionPath( _rOption
) );
193 SvtOptionsDialogOptions::SvtOptionsDialogOptions()
195 // Global access, must be guarded (multithreading)
196 ::osl::MutexGuard
aGuard( SvtOptionsDlgOptions_Impl::getInitMutex() );
200 pOptions
= new SvtOptionsDlgOptions_Impl
;
202 ItemHolder1::holdConfigItem( EItem::OptionsDialogOptions
);
207 SvtOptionsDialogOptions::~SvtOptionsDialogOptions()
209 // Global access, must be guarded (multithreading)
210 ::osl::MutexGuard
aGuard( SvtOptionsDlgOptions_Impl::getInitMutex() );
213 if ( pOptions
->IsModified() )
220 bool SvtOptionsDialogOptions::IsGroupHidden( const OUString
& _rGroup
) const
222 return m_pImp
->IsGroupHidden( _rGroup
);
225 bool SvtOptionsDialogOptions::IsPageHidden( const OUString
& _rPage
, const OUString
& _rGroup
) const
227 return m_pImp
->IsPageHidden( _rPage
, _rGroup
);
230 bool SvtOptionsDialogOptions::IsOptionHidden(
231 const OUString
& _rOption
, const OUString
& _rPage
, const OUString
& _rGroup
) const
233 return m_pImp
->IsOptionHidden( _rOption
, _rPage
, _rGroup
);
236 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */