tdf#130857 qt weld: Support mail merge "Server Auth" dialog
[LibreOffice.git] / svtools / source / config / miscopt.cxx
blob31257615c2b1679bda64d8f23318c30902cb86d6
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 .
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>
36 #include <mutex>
37 #include <vector>
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
61 private:
62 ::std::vector<Link<LinkParamNone*,void>> aList;
63 bool m_bIsSymbolsStyleRO;
64 bool m_bIconThemeWasSetAutomatically;
66 virtual void ImplCommit() override;
68 public:
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
77 internal values.
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 );
92 // public interface
94 static OUString GetIconTheme();
96 enum class SetModifiedFlag { SET, DONT_SET };
98 /** Set the icon theme
100 * @param theme
101 * The name of the icon theme to use.
103 * @param setModified
104 * Whether to call SetModified() and CallListeners().
106 * @internal
107 * The @p setModified flag was introduced because the unittests fail if we call SetModified()
108 * during initialization in the constructor.
110 void
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();
121 // private methods
124 private:
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();
137 // constructor
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 ( );
148 Load( seqNames );
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())
162 continue;
163 switch( nProperty )
165 case PROPERTYHANDLE_SYMBOLSTYLE :
167 OUString aIconTheme;
168 if (seqValues[nProperty] >>= aIconTheme)
169 SetIconTheme(aIconTheme, SetModifiedFlag::DONT_SET);
170 else
171 OSL_FAIL("Wrong type of \"Misc\\SymbolStyle\"!" );
173 m_bIsSymbolsStyleRO = seqRO[nProperty];
174 break;
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 );
186 // destructor
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())
208 continue;
209 switch( comphelper::findValue(aInternalPropertyNames, rPropertyNames[nProperty]) )
211 case PROPERTYHANDLE_SYMBOLSTYLE : {
212 OUString aIconTheme;
213 if (seqValues[nProperty] >>= aIconTheme)
214 SetIconTheme(aIconTheme, SetModifiedFlag::DONT_SET);
215 else
216 OSL_FAIL("Wrong type of \"Misc\\SymbolStyle\"!" );
218 break;
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();
244 void
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;
253 else
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) {
265 SetModified();
267 CallListeners();
271 // public method
273 void SvtMiscOptions_Impl::Notify( const Sequence< OUString >& rPropertyNames )
275 Load( rPropertyNames );
276 CallListeners();
280 // public method
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);
291 OUString value;
292 if (m_bIconThemeWasSetAutomatically) {
293 value = "auto";
295 else {
296 value = GetIconTheme();
298 seqValuesRange[0] <<= value;
299 // Set properties in configuration.
300 PutProperties( seqNames, seqValues );
305 // private method
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
321 namespace {
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();
333 if( !m_pImpl )
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() );
347 m_pImpl.reset();
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);
362 batch->commit();
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
374 // Application class
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;
380 else
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 );
412 bool
413 SvtMiscOptions::IconThemeWasSetAutomatically() const
415 return m_pImpl->IconThemeWasSetAutomatically();
418 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */