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 "itemholder2.hxx"
31 #include <svtools/imgdef.hxx>
32 #include <vcl/svapp.hxx>
33 #include <vcl/settings.hxx>
34 #include <officecfg/Office/Common.hxx>
39 using namespace ::utl
;
40 using namespace ::com::sun::star::uno
;
41 using namespace ::com::sun::star
;
43 constexpr OUStringLiteral ROOTNODE_MISC
= u
"Office.Common/Misc";
45 // PROPERTYHANDLE defines must be sequential from zero for Commit/Load
46 constexpr OUString PROPERTYNAME_SYMBOLSET
= u
"SymbolSet"_ustr
;
47 constexpr OUString PROPERTYNAME_ICONTHEME
= u
"SymbolStyle"_ustr
;
48 #define PROPERTYHANDLE_SYMBOLSTYLE 1
49 constexpr OUString PROPERTYNAME_SIDEBARICONSIZE
= u
"SidebarIconSize"_ustr
;
50 constexpr OUString PROPERTYNAME_NOTEBOOKBARICONSIZE
= u
"NotebookbarIconSize"_ustr
;
52 static std::mutex
& GetInitMutex()
54 static std::mutex theSvtMiscOptionsMutex
;
55 return theSvtMiscOptionsMutex
;
59 class SvtMiscOptions_Impl
: public ConfigItem
62 ::std::vector
<Link
<LinkParamNone
*,void>> aList
;
63 bool m_bIsSymbolsStyleRO
;
64 bool m_bIconThemeWasSetAutomatically
;
66 virtual void ImplCommit() override
;
70 SvtMiscOptions_Impl();
71 virtual ~SvtMiscOptions_Impl() override
;
73 /*-****************************************************************************************************
74 @short called for notify of configmanager
75 @descr This method is called from the ConfigManager before the application ends or from the
76 PropertyChangeListener if the sub tree broadcasts changes. You must update your
79 @seealso baseclass ConfigItem
81 @param "seqPropertyNames" is the list of properties which should be updated.
82 *//*-*****************************************************************************************************/
84 virtual void Notify( const Sequence
< OUString
>& seqPropertyNames
) override
;
86 /** loads required data from the configuration. It's called in the constructor to
87 read all entries and form ::Notify to re-read changed settings
90 void Load( const Sequence
< OUString
>& rPropertyNames
);
94 static OUString
GetIconTheme();
96 enum class SetModifiedFlag
{ SET
, DONT_SET
};
98 /** Set the icon theme
101 * The name of the icon theme to use.
104 * Whether to call SetModified() and CallListeners().
107 * The @p setModified flag was introduced because the unittests fail if we call SetModified()
108 * during initialization in the constructor.
111 SetIconTheme(const OUString
&theme
, SetModifiedFlag setModified
);
113 bool IconThemeWasSetAutomatically() const
114 {return m_bIconThemeWasSetAutomatically
;}
116 void AddListenerLink( const Link
<LinkParamNone
*,void>& rLink
);
117 void RemoveListenerLink( const Link
<LinkParamNone
*,void>& rLink
);
118 void CallListeners();
126 /*-****************************************************************************************************
127 @short return list of key names of our configuration management which represent our module tree
128 @descr These methods return a static const list of key names. We need it to get needed values from our
129 configuration management.
130 @return A list of needed configuration keys is returned.
131 *//*-*****************************************************************************************************/
133 static Sequence
< OUString
> GetPropertyNames();
139 SvtMiscOptions_Impl::SvtMiscOptions_Impl()
140 // Init baseclasses first
141 : ConfigItem( ROOTNODE_MISC
)
143 , m_bIsSymbolsStyleRO( false )
144 , m_bIconThemeWasSetAutomatically( false )
146 // Use our static list of configuration keys to get his values.
147 Sequence
< OUString
> seqNames
= GetPropertyNames ( );
149 Sequence
< Any
> seqValues
= GetProperties ( seqNames
);
150 Sequence
< sal_Bool
> seqRO
= GetReadOnlyStates ( seqNames
);
152 // Safe impossible cases.
153 // We need values from ALL configuration keys.
154 // Follow assignment use order of values in relation to our list of key names!
155 DBG_ASSERT( !(seqNames
.getLength()!=seqValues
.getLength()), "SvtMiscOptions_Impl::SvtMiscOptions_Impl()\nI miss some values of configuration keys!\n" );
157 // Copy values from list in right order to our internal member.
158 sal_Int32 nPropertyCount
= seqValues
.getLength();
159 for( sal_Int32 nProperty
=0; nProperty
<nPropertyCount
; ++nProperty
)
161 if (!seqValues
[nProperty
].hasValue())
165 case PROPERTYHANDLE_SYMBOLSTYLE
:
168 if (seqValues
[nProperty
] >>= aIconTheme
)
169 SetIconTheme(aIconTheme
, SetModifiedFlag::DONT_SET
);
171 OSL_FAIL("Wrong type of \"Misc\\SymbolStyle\"!" );
173 m_bIsSymbolsStyleRO
= seqRO
[nProperty
];
180 // Enable notification mechanism of our baseclass.
181 // We need it to get information about changes outside these class on our used configuration keys!
182 EnableNotification( seqNames
);
188 SvtMiscOptions_Impl::~SvtMiscOptions_Impl()
190 assert(!IsModified()); // should have been committed
193 void SvtMiscOptions_Impl::Load( const Sequence
< OUString
>& rPropertyNames
)
195 const uno::Sequence
< OUString
> aInternalPropertyNames( GetPropertyNames());
196 Sequence
< Any
> seqValues
= GetProperties( rPropertyNames
);
198 // Safe impossible cases.
199 // We need values from ALL configuration keys.
200 // Follow assignment use order of values in relation to our list of key names!
201 DBG_ASSERT( !(rPropertyNames
.getLength()!=seqValues
.getLength()), "SvtSecurityOptions_Impl::SvtSecurityOptions_Impl()\nI miss some values of configuration keys!\n" );
203 // Copy values from list in right order to our internal member.
204 sal_Int32 nPropertyCount
= seqValues
.getLength();
205 for( sal_Int32 nProperty
=0; nProperty
<nPropertyCount
; ++nProperty
)
207 if (!seqValues
[nProperty
].hasValue())
209 switch( comphelper::findValue(aInternalPropertyNames
, rPropertyNames
[nProperty
]) )
211 case PROPERTYHANDLE_SYMBOLSTYLE
: {
213 if (seqValues
[nProperty
] >>= aIconTheme
)
214 SetIconTheme(aIconTheme
, SetModifiedFlag::DONT_SET
);
216 OSL_FAIL("Wrong type of \"Misc\\SymbolStyle\"!" );
223 void SvtMiscOptions_Impl::AddListenerLink( const Link
<LinkParamNone
*,void>& rLink
)
225 aList
.push_back( rLink
);
228 void SvtMiscOptions_Impl::RemoveListenerLink( const Link
<LinkParamNone
*,void>& rLink
)
230 std::erase(aList
, rLink
);
233 void SvtMiscOptions_Impl::CallListeners()
235 for (auto const& elem
: aList
)
236 elem
.Call( nullptr );
239 OUString
SvtMiscOptions_Impl::GetIconTheme()
241 return Application::GetSettings().GetStyleSettings().DetermineIconTheme();
245 SvtMiscOptions_Impl::SetIconTheme(const OUString
&rName
, SetModifiedFlag setModified
)
247 OUString
aTheme(rName
);
248 if (aTheme
.isEmpty() || aTheme
== "auto")
250 aTheme
= Application::GetSettings().GetStyleSettings().GetAutomaticallyChosenIconTheme();
251 m_bIconThemeWasSetAutomatically
= true;
254 m_bIconThemeWasSetAutomatically
= false;
256 AllSettings aAllSettings
= Application::GetSettings();
257 StyleSettings aStyleSettings
= aAllSettings
.GetStyleSettings();
258 aStyleSettings
.SetIconTheme(aTheme
);
260 aAllSettings
.SetStyleSettings(aStyleSettings
);
261 Application::MergeSystemSettings( aAllSettings
);
262 Application::SetSettings(aAllSettings
);
264 if (setModified
== SetModifiedFlag::SET
) {
273 void SvtMiscOptions_Impl::Notify( const Sequence
< OUString
>& rPropertyNames
)
275 Load( rPropertyNames
);
282 void SvtMiscOptions_Impl::ImplCommit()
284 if ( !m_bIsSymbolsStyleRO
)
286 // Get names of supported properties, create a list for values and copy current values to it.
287 Sequence
< OUString
> seqNames
{ PROPERTYNAME_ICONTHEME
};
288 sal_Int32 nCount
= seqNames
.getLength();
289 Sequence
< Any
> seqValues ( nCount
);
290 auto seqValuesRange
= asNonConstRange(seqValues
);
292 if (m_bIconThemeWasSetAutomatically
) {
296 value
= GetIconTheme();
298 seqValuesRange
[0] <<= value
;
299 // Set properties in configuration.
300 PutProperties( seqNames
, seqValues
);
307 Sequence
< OUString
> SvtMiscOptions_Impl::GetPropertyNames()
309 return Sequence
<OUString
>
311 PROPERTYNAME_SYMBOLSET
,
312 PROPERTYNAME_ICONTHEME
,
313 // SidebarIconSize and NotebookbarIconSize so
314 // notifications for their changes are also broadcast
315 // from SvtMiscOptions
316 PROPERTYNAME_SIDEBARICONSIZE
,
317 PROPERTYNAME_NOTEBOOKBARICONSIZE
323 std::weak_ptr
<SvtMiscOptions_Impl
> g_pMiscOptions
;
327 SvtMiscOptions::SvtMiscOptions()
329 // Global access, must be guarded (multithreading!).
330 std::unique_lock
aGuard( GetInitMutex() );
332 m_pImpl
= g_pMiscOptions
.lock();
335 m_pImpl
= std::make_shared
<SvtMiscOptions_Impl
>();
336 g_pMiscOptions
= m_pImpl
;
337 aGuard
.unlock(); // because holdConfigItem will call this constructor
338 svtools::ItemHolder2::holdConfigItem(EItem::MiscOptions
);
342 SvtMiscOptions::~SvtMiscOptions()
344 // Global access, must be guarded (multithreading!)
345 std::unique_lock
aGuard( GetInitMutex() );
351 sal_Int16
SvtMiscOptions::GetSymbolsSize()
353 return officecfg::Office::Common::Misc::SymbolSet::get();
356 void SvtMiscOptions::SetSymbolsSize( sal_Int16 nSet
)
358 if (!officecfg::Office::Common::Misc::SymbolSet::isReadOnly())
360 std::shared_ptr
<comphelper::ConfigurationChanges
> batch(comphelper::ConfigurationChanges::create());
361 officecfg::Office::Common::Misc::SymbolSet::set(nSet
, batch
);
363 m_pImpl
->CallListeners();
367 sal_Int16
SvtMiscOptions::GetCurrentSymbolsSize()
369 sal_Int16 eOptSymbolsSize
= GetSymbolsSize();
371 if ( eOptSymbolsSize
== SFX_SYMBOLS_SIZE_AUTO
)
373 // Use system settings, we have to retrieve the toolbar icon size from the
375 ToolbarIconSize nStyleIconSize
= Application::GetSettings().GetStyleSettings().GetToolbarIconSize();
376 if (nStyleIconSize
== ToolbarIconSize::Size32
)
377 eOptSymbolsSize
= SFX_SYMBOLS_SIZE_32
;
378 else if (nStyleIconSize
== ToolbarIconSize::Large
)
379 eOptSymbolsSize
= SFX_SYMBOLS_SIZE_LARGE
;
381 eOptSymbolsSize
= SFX_SYMBOLS_SIZE_SMALL
;
384 return eOptSymbolsSize
;
387 bool SvtMiscOptions::AreCurrentSymbolsLarge()
389 return ( GetCurrentSymbolsSize() == SFX_SYMBOLS_SIZE_LARGE
|| GetCurrentSymbolsSize() == SFX_SYMBOLS_SIZE_32
);
392 OUString
SvtMiscOptions::GetIconTheme()
394 return SvtMiscOptions_Impl::GetIconTheme();
397 void SvtMiscOptions::SetIconTheme(const OUString
& iconTheme
)
399 m_pImpl
->SetIconTheme(iconTheme
, SvtMiscOptions_Impl::SetModifiedFlag::SET
);
402 void SvtMiscOptions::AddListenerLink( const Link
<LinkParamNone
*,void>& rLink
)
404 m_pImpl
->AddListenerLink( rLink
);
407 void SvtMiscOptions::RemoveListenerLink( const Link
<LinkParamNone
*,void>& rLink
)
409 m_pImpl
->RemoveListenerLink( rLink
);
413 SvtMiscOptions::IconThemeWasSetAutomatically() const
415 return m_pImpl
->IconThemeWasSetAutomatically();
418 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */