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 <svtools/miscopt.hxx>
21 #include <unotools/configitem.hxx>
22 #include <tools/debug.hxx>
23 #include <com/sun/star/uno/Any.hxx>
24 #include <com/sun/star/uno/Sequence.hxx>
25 #include <comphelper/sequence.hxx>
26 #include <tools/link.hxx>
27 #include <osl/diagnose.h>
29 #include <rtl/instance.hxx>
30 #include "itemholder2.hxx"
32 #include <svtools/imgdef.hxx>
33 #include <vcl/svapp.hxx>
34 #include <vcl/settings.hxx>
35 #include <vcl/toolbox.hxx>
39 using namespace ::utl
;
40 using namespace ::osl
;
41 using namespace ::com::sun::star::uno
;
42 using namespace ::com::sun::star
;
44 #define ROOTNODE_MISC "Office.Common/Misc"
46 // PROPERTYHANDLE defines must be sequential from zero for Commit/Load
47 #define PROPERTYNAME_SYMBOLSET "SymbolSet"
48 #define PROPERTYHANDLE_SYMBOLSET 0
49 #define PROPERTYNAME_ICONTHEME "SymbolStyle"
50 #define PROPERTYHANDLE_SYMBOLSTYLE 1
51 #define PROPERTYNAME_DISABLEUICUSTOMIZATION "DisableUICustomization"
52 #define PROPERTYHANDLE_DISABLEUICUSTOMIZATION 2
53 #define PROPERTYNAME_SIDEBARICONSIZE "SidebarIconSize"
54 #define PROPERTYHANDLE_SIDEBARICONSIZE 3
55 #define PROPERTYNAME_NOTEBOOKBARICONSIZE "NotebookbarIconSize"
56 #define PROPERTYHANDLE_NOTEBOOKBARICONSIZE 4
58 class SvtMiscOptions_Impl
: public ConfigItem
61 ::std::vector
<Link
<LinkParamNone
*,void>> aList
;
62 sal_Int16 m_nSymbolsSize
;
63 bool m_bIsSymbolsSizeRO
;
64 ToolBoxButtonSize m_nSidebarIconSize
;
65 bool m_bIsSidebarIconSizeRO
;
66 ToolBoxButtonSize m_nNotebookbarIconSize
;
67 bool m_bIsNotebookbarIconSizeRO
;
68 bool m_bIsSymbolsStyleRO
;
69 bool m_bDisableUICustomization
;
70 bool m_bIconThemeWasSetAutomatically
;
72 virtual void ImplCommit() override
;
76 SvtMiscOptions_Impl();
77 virtual ~SvtMiscOptions_Impl() override
;
79 /*-****************************************************************************************************
80 @short called for notify of configmanager
81 @descr This method is called from the ConfigManager before the application ends or from the
82 PropertyChangeListener if the sub tree broadcasts changes. You must update your
85 @seealso baseclass ConfigItem
87 @param "seqPropertyNames" is the list of properties which should be updated.
88 *//*-*****************************************************************************************************/
90 virtual void Notify( const Sequence
< OUString
>& seqPropertyNames
) override
;
92 /** loads required data from the configuration. It's called in the constructor to
93 read all entries and form ::Notify to re-read changed settings
96 void Load( const Sequence
< OUString
>& rPropertyNames
);
100 bool DisableUICustomization() const
101 { return m_bDisableUICustomization
; }
103 sal_Int16
GetSymbolsSize() const
104 { return m_nSymbolsSize
; }
106 ToolBoxButtonSize
GetSidebarIconSize() const
107 { return m_nSidebarIconSize
; }
109 ToolBoxButtonSize
GetNotebookbarIconSize() const
110 { return m_nNotebookbarIconSize
; }
112 void SetSymbolsSize( sal_Int16 nSet
);
114 void SetSidebarIconSize( ToolBoxButtonSize nSet
);
116 void SetNotebookbarIconSize( ToolBoxButtonSize nSet
);
118 static OUString
GetIconTheme();
120 enum class SetModifiedFlag
{ SET
, DONT_SET
};
122 /** Set the icon theme
125 * The name of the icon theme to use.
128 * Whether to call SetModified() and CallListeners().
131 * The @p setModified flag was introduced because the unittests fail if we call SetModified()
132 * during initialization in the constructor.
135 SetIconTheme(const OUString
&theme
, SetModifiedFlag setModified
);
137 bool IconThemeWasSetAutomatically()
138 {return m_bIconThemeWasSetAutomatically
;}
140 void AddListenerLink( const Link
<LinkParamNone
*,void>& rLink
);
141 void RemoveListenerLink( const Link
<LinkParamNone
*,void>& rLink
);
142 void CallListeners();
150 /*-****************************************************************************************************
151 @short return list of key names of our configuration management which represent our module tree
152 @descr These methods return a static const list of key names. We need it to get needed values from our
153 configuration management.
154 @return A list of needed configuration keys is returned.
155 *//*-*****************************************************************************************************/
157 static Sequence
< OUString
> GetPropertyNames();
163 SvtMiscOptions_Impl::SvtMiscOptions_Impl()
164 // Init baseclasses first
165 : ConfigItem( ROOTNODE_MISC
)
167 , m_nSymbolsSize( 0 )
168 , m_bIsSymbolsSizeRO( false )
169 , m_nSidebarIconSize( ToolBoxButtonSize::DontCare
)
170 , m_bIsSidebarIconSizeRO( false )
171 , m_nNotebookbarIconSize( ToolBoxButtonSize::DontCare
)
172 , m_bIsNotebookbarIconSizeRO( false )
173 , m_bIsSymbolsStyleRO( false )
174 , m_bIconThemeWasSetAutomatically( false )
176 // Use our static list of configuration keys to get his values.
177 Sequence
< OUString
> seqNames
= GetPropertyNames ( );
179 Sequence
< Any
> seqValues
= GetProperties ( seqNames
);
180 Sequence
< sal_Bool
> seqRO
= GetReadOnlyStates ( seqNames
);
182 // Safe impossible cases.
183 // We need values from ALL configuration keys.
184 // Follow assignment use order of values in relation to our list of key names!
185 DBG_ASSERT( !(seqNames
.getLength()!=seqValues
.getLength()), "SvtMiscOptions_Impl::SvtMiscOptions_Impl()\nI miss some values of configuration keys!\n" );
187 // Copy values from list in right order to our internal member.
188 sal_Int32 nPropertyCount
= seqValues
.getLength();
189 for( sal_Int32 nProperty
=0; nProperty
<nPropertyCount
; ++nProperty
)
191 if (!seqValues
[nProperty
].hasValue())
195 case PROPERTYHANDLE_SYMBOLSET
:
197 if( !(seqValues
[nProperty
] >>= m_nSymbolsSize
) )
199 OSL_FAIL("Wrong type of \"Misc\\SymbolSet\"!" );
201 m_bIsSymbolsSizeRO
= seqRO
[nProperty
];
205 case PROPERTYHANDLE_SIDEBARICONSIZE
:
208 if( !(seqValues
[nProperty
] >>= nTmp
) )
210 OSL_FAIL("Wrong type of \"Misc\\SidebarIconSize\"!" );
212 m_nSidebarIconSize
= static_cast<ToolBoxButtonSize
>(nTmp
);
213 m_bIsSidebarIconSizeRO
= seqRO
[nProperty
];
217 case PROPERTYHANDLE_NOTEBOOKBARICONSIZE
:
220 if( !(seqValues
[nProperty
] >>= nTmp
) )
222 OSL_FAIL("Wrong type of \"Misc\\NotebookbarIconSize\"!" );
224 m_nNotebookbarIconSize
= static_cast<ToolBoxButtonSize
>(nTmp
);
225 m_bIsNotebookbarIconSizeRO
= seqRO
[nProperty
];
229 case PROPERTYHANDLE_SYMBOLSTYLE
:
232 if (seqValues
[nProperty
] >>= aIconTheme
)
233 SetIconTheme(aIconTheme
, SetModifiedFlag::DONT_SET
);
235 OSL_FAIL("Wrong type of \"Misc\\SymbolStyle\"!" );
237 m_bIsSymbolsStyleRO
= seqRO
[nProperty
];
241 case PROPERTYHANDLE_DISABLEUICUSTOMIZATION
:
243 if( !(seqValues
[nProperty
] >>= m_bDisableUICustomization
) )
244 OSL_FAIL("Wrong type of \"Misc\\DisableUICustomization\"!" );
250 // Enable notification mechanism of our baseclass.
251 // We need it to get information about changes outside these class on our used configuration keys!
252 EnableNotification( seqNames
);
258 SvtMiscOptions_Impl::~SvtMiscOptions_Impl()
260 assert(!IsModified()); // should have been committed
263 void SvtMiscOptions_Impl::Load( const Sequence
< OUString
>& rPropertyNames
)
265 const uno::Sequence
< OUString
> aInternalPropertyNames( GetPropertyNames());
266 Sequence
< Any
> seqValues
= GetProperties( rPropertyNames
);
268 // Safe impossible cases.
269 // We need values from ALL configuration keys.
270 // Follow assignment use order of values in relation to our list of key names!
271 DBG_ASSERT( !(rPropertyNames
.getLength()!=seqValues
.getLength()), "SvtSecurityOptions_Impl::SvtSecurityOptions_Impl()\nI miss some values of configuration keys!\n" );
273 // Copy values from list in right order to our internal member.
274 sal_Int32 nPropertyCount
= seqValues
.getLength();
275 for( sal_Int32 nProperty
=0; nProperty
<nPropertyCount
; ++nProperty
)
277 if (!seqValues
[nProperty
].hasValue())
279 switch( comphelper::findValue(aInternalPropertyNames
, rPropertyNames
[nProperty
]) )
281 case PROPERTYHANDLE_SYMBOLSET
: {
282 if( !(seqValues
[nProperty
] >>= m_nSymbolsSize
) )
284 OSL_FAIL("Wrong type of \"Misc\\SymbolSet\"!" );
288 case PROPERTYHANDLE_SIDEBARICONSIZE
: {
290 if( !(seqValues
[nProperty
] >>= nTmp
) )
292 OSL_FAIL("Wrong type of \"Misc\\SidebarIconSize\"!" );
294 m_nSidebarIconSize
= static_cast<ToolBoxButtonSize
>(nTmp
);
297 case PROPERTYHANDLE_NOTEBOOKBARICONSIZE
: {
299 if( !(seqValues
[nProperty
] >>= nTmp
) )
301 OSL_FAIL("Wrong type of \"Misc\\NotebookbarIconSize\"!" );
303 m_nNotebookbarIconSize
= static_cast<ToolBoxButtonSize
>(nTmp
);
306 case PROPERTYHANDLE_SYMBOLSTYLE
: {
308 if (seqValues
[nProperty
] >>= aIconTheme
)
309 SetIconTheme(aIconTheme
, SetModifiedFlag::DONT_SET
);
311 OSL_FAIL("Wrong type of \"Misc\\SymbolStyle\"!" );
314 case PROPERTYHANDLE_DISABLEUICUSTOMIZATION
: {
315 if( !(seqValues
[nProperty
] >>= m_bDisableUICustomization
) )
316 OSL_FAIL("Wrong type of \"Misc\\DisableUICustomization\"!" );
323 void SvtMiscOptions_Impl::AddListenerLink( const Link
<LinkParamNone
*,void>& rLink
)
325 aList
.push_back( rLink
);
328 void SvtMiscOptions_Impl::RemoveListenerLink( const Link
<LinkParamNone
*,void>& rLink
)
330 aList
.erase(std::remove(aList
.begin(), aList
.end(), rLink
), aList
.end());
333 void SvtMiscOptions_Impl::CallListeners()
335 for (auto const& elem
: aList
)
336 elem
.Call( nullptr );
339 void SvtMiscOptions_Impl::SetSymbolsSize( sal_Int16 nSet
)
341 m_nSymbolsSize
= nSet
;
346 void SvtMiscOptions_Impl::SetSidebarIconSize( ToolBoxButtonSize nSet
)
348 m_nSidebarIconSize
= nSet
;
353 void SvtMiscOptions_Impl::SetNotebookbarIconSize( ToolBoxButtonSize nSet
)
355 m_nNotebookbarIconSize
= nSet
;
360 OUString
SvtMiscOptions_Impl::GetIconTheme()
362 return Application::GetSettings().GetStyleSettings().DetermineIconTheme();
366 SvtMiscOptions_Impl::SetIconTheme(const OUString
&rName
, SetModifiedFlag setModified
)
368 OUString
aTheme(rName
);
369 if (aTheme
.isEmpty() || aTheme
== "auto")
371 aTheme
= Application::GetSettings().GetStyleSettings().GetAutomaticallyChosenIconTheme();
372 m_bIconThemeWasSetAutomatically
= true;
375 m_bIconThemeWasSetAutomatically
= false;
377 AllSettings aAllSettings
= Application::GetSettings();
378 StyleSettings aStyleSettings
= aAllSettings
.GetStyleSettings();
379 aStyleSettings
.SetIconTheme(aTheme
);
381 aAllSettings
.SetStyleSettings(aStyleSettings
);
382 Application::MergeSystemSettings( aAllSettings
);
383 Application::SetSettings(aAllSettings
);
385 if (setModified
== SetModifiedFlag::SET
) {
394 void SvtMiscOptions_Impl::Notify( const Sequence
< OUString
>& rPropertyNames
)
396 Load( rPropertyNames
);
403 void SvtMiscOptions_Impl::ImplCommit()
405 // Get names of supported properties, create a list for values and copy current values to it.
406 Sequence
< OUString
> seqNames
= GetPropertyNames ();
407 sal_Int32 nCount
= seqNames
.getLength();
408 Sequence
< Any
> seqValues ( nCount
);
409 for( sal_Int32 nProperty
=0; nProperty
<nCount
; ++nProperty
)
413 case PROPERTYHANDLE_SYMBOLSET
:
415 if ( !m_bIsSymbolsSizeRO
)
416 seqValues
[nProperty
] <<= m_nSymbolsSize
;
420 case PROPERTYHANDLE_SIDEBARICONSIZE
:
422 if ( !m_bIsSidebarIconSizeRO
)
423 seqValues
[nProperty
] <<= static_cast<sal_uInt16
>(m_nSidebarIconSize
);
427 case PROPERTYHANDLE_NOTEBOOKBARICONSIZE
:
429 if ( !m_bIsNotebookbarIconSizeRO
)
430 seqValues
[nProperty
] <<= static_cast<sal_uInt16
>(m_nNotebookbarIconSize
);
434 case PROPERTYHANDLE_SYMBOLSTYLE
:
436 if ( !m_bIsSymbolsStyleRO
) {
438 if (m_bIconThemeWasSetAutomatically
) {
442 value
= GetIconTheme();
444 seqValues
[nProperty
] <<= value
;
449 case PROPERTYHANDLE_DISABLEUICUSTOMIZATION
:
451 seqValues
[nProperty
] <<= m_bDisableUICustomization
;
456 // Set properties in configuration.
457 PutProperties( seqNames
, seqValues
);
463 Sequence
< OUString
> SvtMiscOptions_Impl::GetPropertyNames()
465 return Sequence
<OUString
>
467 PROPERTYNAME_SYMBOLSET
,
468 PROPERTYNAME_ICONTHEME
,
469 PROPERTYNAME_DISABLEUICUSTOMIZATION
,
470 PROPERTYNAME_SIDEBARICONSIZE
,
471 PROPERTYNAME_NOTEBOOKBARICONSIZE
477 std::weak_ptr
<SvtMiscOptions_Impl
> g_pMiscOptions
;
481 SvtMiscOptions::SvtMiscOptions()
483 // Global access, must be guarded (multithreading!).
484 MutexGuard
aGuard( GetInitMutex() );
486 m_pImpl
= g_pMiscOptions
.lock();
489 m_pImpl
= std::make_shared
<SvtMiscOptions_Impl
>();
490 g_pMiscOptions
= m_pImpl
;
491 svtools::ItemHolder2::holdConfigItem(EItem::MiscOptions
);
495 SvtMiscOptions::~SvtMiscOptions()
497 // Global access, must be guarded (multithreading!)
498 MutexGuard
aGuard( GetInitMutex() );
504 sal_Int16
SvtMiscOptions::GetSymbolsSize() const
506 return m_pImpl
->GetSymbolsSize();
509 void SvtMiscOptions::SetSymbolsSize( sal_Int16 nSet
)
511 m_pImpl
->SetSymbolsSize( nSet
);
514 ToolBoxButtonSize
SvtMiscOptions::GetSidebarIconSize() const
516 return m_pImpl
->GetSidebarIconSize();
519 ToolBoxButtonSize
SvtMiscOptions::GetNotebookbarIconSize() const
521 return m_pImpl
->GetNotebookbarIconSize();
524 void SvtMiscOptions::SetSidebarIconSize( ToolBoxButtonSize nSet
)
526 m_pImpl
->SetSidebarIconSize( nSet
);
529 void SvtMiscOptions::SetNotebookbarIconSize( ToolBoxButtonSize nSet
)
531 m_pImpl
->SetNotebookbarIconSize( nSet
);
534 sal_Int16
SvtMiscOptions::GetCurrentSymbolsSize() const
536 sal_Int16 eOptSymbolsSize
= m_pImpl
->GetSymbolsSize();
538 if ( eOptSymbolsSize
== SFX_SYMBOLS_SIZE_AUTO
)
540 // Use system settings, we have to retrieve the toolbar icon size from the
542 ToolbarIconSize nStyleIconSize
= Application::GetSettings().GetStyleSettings().GetToolbarIconSize();
543 if (nStyleIconSize
== ToolbarIconSize::Size32
)
544 eOptSymbolsSize
= SFX_SYMBOLS_SIZE_32
;
545 else if (nStyleIconSize
== ToolbarIconSize::Large
)
546 eOptSymbolsSize
= SFX_SYMBOLS_SIZE_LARGE
;
548 eOptSymbolsSize
= SFX_SYMBOLS_SIZE_SMALL
;
551 return eOptSymbolsSize
;
554 bool SvtMiscOptions::AreCurrentSymbolsLarge() const
556 return ( GetCurrentSymbolsSize() == SFX_SYMBOLS_SIZE_LARGE
|| GetCurrentSymbolsSize() == SFX_SYMBOLS_SIZE_32
);
559 OUString
SvtMiscOptions::GetIconTheme() const
561 return SvtMiscOptions_Impl::GetIconTheme();
564 void SvtMiscOptions::SetIconTheme(const OUString
& iconTheme
)
566 m_pImpl
->SetIconTheme(iconTheme
, SvtMiscOptions_Impl::SetModifiedFlag::SET
);
569 bool SvtMiscOptions::DisableUICustomization() const
571 return m_pImpl
->DisableUICustomization();
576 class theSvtMiscOptionsMutex
:
577 public rtl::Static
< osl::Mutex
, theSvtMiscOptionsMutex
> {};
580 Mutex
& SvtMiscOptions::GetInitMutex()
582 return theSvtMiscOptionsMutex::get();
585 void SvtMiscOptions::AddListenerLink( const Link
<LinkParamNone
*,void>& rLink
)
587 m_pImpl
->AddListenerLink( rLink
);
590 void SvtMiscOptions::RemoveListenerLink( const Link
<LinkParamNone
*,void>& rLink
)
592 m_pImpl
->RemoveListenerLink( rLink
);
596 SvtMiscOptions::IconThemeWasSetAutomatically()
598 return m_pImpl
->IconThemeWasSetAutomatically();
601 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */