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 .
21 #include <svl/ctloptions.hxx>
23 #include <unotools/configitem.hxx>
24 #include <unotools/configmgr.hxx>
25 #include <com/sun/star/uno/Any.h>
26 #include <com/sun/star/uno/Sequence.hxx>
27 #include <osl/mutex.hxx>
28 #include "itemholder2.hxx"
29 #include <officecfg/Office/Common.hxx>
31 using namespace ::com::sun::star
;
32 using namespace ::com::sun::star::uno
;
34 #define CFG_READONLY_DEFAULT false
36 class SvtCTLOptions_Impl
: public utl::ConfigItem
40 bool m_bCTLFontEnabled
;
41 bool m_bCTLSequenceChecking
;
42 bool m_bCTLRestricted
;
43 bool m_bCTLTypeAndReplace
;
44 SvtCTLOptions::CursorMovement m_eCTLCursorMovement
;
45 SvtCTLOptions::TextNumerals m_eCTLTextNumerals
;
47 bool m_bROCTLFontEnabled
;
48 bool m_bROCTLSequenceChecking
;
49 bool m_bROCTLRestricted
;
50 bool m_bROCTLTypeAndReplace
;
51 bool m_bROCTLCursorMovement
;
52 bool m_bROCTLTextNumerals
;
54 virtual void ImplCommit() override
;
58 virtual ~SvtCTLOptions_Impl() override
;
60 virtual void Notify( const Sequence
< OUString
>& _aPropertyNames
) override
;
63 bool IsLoaded() const { return m_bIsLoaded
; }
64 void SetCTLFontEnabled( bool _bEnabled
);
66 void SetCTLSequenceChecking( bool _bEnabled
);
68 void SetCTLSequenceCheckingRestricted( bool _bEnable
);
70 void SetCTLSequenceCheckingTypeAndReplace( bool _bEnable
);
72 void SetCTLCursorMovement( SvtCTLOptions::CursorMovement _eMovement
);
74 void SetCTLTextNumerals( SvtCTLOptions::TextNumerals _eNumerals
);
76 bool IsReadOnly(SvtCTLOptions::EOption eOption
) const;
80 Sequence
<OUString
> & PropertyNames()
82 static Sequence
<OUString
> SINGLETON
;
86 bool SvtCTLOptions_Impl::IsReadOnly(SvtCTLOptions::EOption eOption
) const
88 bool bReadOnly
= CFG_READONLY_DEFAULT
;
91 case SvtCTLOptions::E_CTLFONT
: bReadOnly
= m_bROCTLFontEnabled
; break;
92 case SvtCTLOptions::E_CTLSEQUENCECHECKING
: bReadOnly
= m_bROCTLSequenceChecking
; break;
93 case SvtCTLOptions::E_CTLCURSORMOVEMENT
: bReadOnly
= m_bROCTLCursorMovement
; break;
94 case SvtCTLOptions::E_CTLTEXTNUMERALS
: bReadOnly
= m_bROCTLTextNumerals
; break;
95 case SvtCTLOptions::E_CTLSEQUENCECHECKINGRESTRICTED
: bReadOnly
= m_bROCTLRestricted
; break;
96 case SvtCTLOptions::E_CTLSEQUENCECHECKINGTYPEANDREPLACE
: bReadOnly
= m_bROCTLTypeAndReplace
; break;
97 default: assert(false);
101 SvtCTLOptions_Impl::SvtCTLOptions_Impl() :
103 utl::ConfigItem(u
"Office.Common/I18N/CTL"_ustr
),
105 m_bIsLoaded ( false ),
106 m_bCTLFontEnabled ( true ),
107 m_bCTLSequenceChecking ( false ),
108 m_bCTLRestricted ( false ),
109 m_bCTLTypeAndReplace ( false ),
110 m_eCTLCursorMovement ( SvtCTLOptions::MOVEMENT_LOGICAL
),
111 m_eCTLTextNumerals ( SvtCTLOptions::NUMERALS_ARABIC
),
113 m_bROCTLFontEnabled ( CFG_READONLY_DEFAULT
),
114 m_bROCTLSequenceChecking( CFG_READONLY_DEFAULT
),
115 m_bROCTLRestricted ( CFG_READONLY_DEFAULT
),
116 m_bROCTLTypeAndReplace ( CFG_READONLY_DEFAULT
),
117 m_bROCTLCursorMovement ( CFG_READONLY_DEFAULT
),
118 m_bROCTLTextNumerals ( CFG_READONLY_DEFAULT
)
121 SvtCTLOptions_Impl::~SvtCTLOptions_Impl()
123 assert(!IsModified()); // should have been committed
126 void SvtCTLOptions_Impl::Notify( const Sequence
< OUString
>& )
129 NotifyListeners(ConfigurationHints::CtlSettingsChanged
);
132 void SvtCTLOptions_Impl::ImplCommit()
134 Sequence
< OUString
> &rPropertyNames
= PropertyNames();
135 OUString
* pOrgNames
= rPropertyNames
.getArray();
136 sal_Int32 nOrgCount
= rPropertyNames
.getLength();
138 Sequence
< OUString
> aNames( nOrgCount
);
139 Sequence
< Any
> aValues( nOrgCount
);
141 OUString
* pNames
= aNames
.getArray();
142 Any
* pValues
= aValues
.getArray();
143 sal_Int32 nRealCount
= 0;
145 for ( int nProp
= 0; nProp
< nOrgCount
; nProp
++ )
151 if (!m_bROCTLFontEnabled
)
153 pNames
[nRealCount
] = pOrgNames
[nProp
];
154 pValues
[nRealCount
] <<= m_bCTLFontEnabled
;
162 if (!m_bROCTLSequenceChecking
)
164 pNames
[nRealCount
] = pOrgNames
[nProp
];
165 pValues
[nRealCount
] <<= m_bCTLSequenceChecking
;
173 if (!m_bROCTLCursorMovement
)
175 pNames
[nRealCount
] = pOrgNames
[nProp
];
176 pValues
[nRealCount
] <<= static_cast<sal_Int32
>(m_eCTLCursorMovement
);
184 if (!m_bROCTLTextNumerals
)
186 pNames
[nRealCount
] = pOrgNames
[nProp
];
187 pValues
[nRealCount
] <<= static_cast<sal_Int32
>(m_eCTLTextNumerals
);
195 if (!m_bROCTLRestricted
)
197 pNames
[nRealCount
] = pOrgNames
[nProp
];
198 pValues
[nRealCount
] <<= m_bCTLRestricted
;
205 if(!m_bROCTLTypeAndReplace
)
207 pNames
[nRealCount
] = pOrgNames
[nProp
];
208 pValues
[nRealCount
] <<= m_bCTLTypeAndReplace
;
215 aNames
.realloc(nRealCount
);
216 aValues
.realloc(nRealCount
);
217 PutProperties( aNames
, aValues
);
219 NotifyListeners(ConfigurationHints::CtlSettingsChanged
);
222 void SvtCTLOptions_Impl::Load()
224 Sequence
< OUString
>& rPropertyNames
= PropertyNames();
225 if ( !rPropertyNames
.hasElements() )
229 u
"CTLSequenceChecking"_ustr
,
230 u
"CTLCursorMovement"_ustr
,
231 u
"CTLTextNumerals"_ustr
,
232 u
"CTLSequenceCheckingRestricted"_ustr
,
233 u
"CTLSequenceCheckingTypeAndReplace"_ustr
};
234 EnableNotification( rPropertyNames
);
236 Sequence
< Any
> aValues
= GetProperties( rPropertyNames
);
237 Sequence
< sal_Bool
> aROStates
= GetReadOnlyStates( rPropertyNames
);
238 const Any
* pValues
= aValues
.getConstArray();
239 const sal_Bool
* pROStates
= aROStates
.getConstArray();
240 assert(aValues
.getLength() == rPropertyNames
.getLength() && "GetProperties failed");
241 assert(aROStates
.getLength() == rPropertyNames
.getLength() && "GetReadOnlyStates failed");
242 if ( aValues
.getLength() == rPropertyNames
.getLength() && aROStates
.getLength() == rPropertyNames
.getLength() )
245 sal_Int32 nValue
= 0;
247 for ( int nProp
= 0; nProp
< rPropertyNames
.getLength(); nProp
++ )
249 if ( pValues
[nProp
].hasValue() )
251 if ( pValues
[nProp
] >>= bValue
)
255 case 0: { m_bCTLFontEnabled
= bValue
; m_bROCTLFontEnabled
= pROStates
[nProp
]; } break;
256 case 1: { m_bCTLSequenceChecking
= bValue
; m_bROCTLSequenceChecking
= pROStates
[nProp
]; } break;
257 case 4: { m_bCTLRestricted
= bValue
; m_bROCTLRestricted
= pROStates
[nProp
]; } break;
258 case 5: { m_bCTLTypeAndReplace
= bValue
; m_bROCTLTypeAndReplace
= pROStates
[nProp
]; } break;
261 else if ( pValues
[nProp
] >>= nValue
)
265 case 2: { m_eCTLCursorMovement
= static_cast<SvtCTLOptions::CursorMovement
>(nValue
); m_bROCTLCursorMovement
= pROStates
[nProp
]; } break;
266 case 3: { m_eCTLTextNumerals
= static_cast<SvtCTLOptions::TextNumerals
>(nValue
); m_bROCTLTextNumerals
= pROStates
[nProp
]; } break;
275 void SvtCTLOptions_Impl::SetCTLFontEnabled( bool _bEnabled
)
277 if(!m_bROCTLFontEnabled
&& m_bCTLFontEnabled
!= _bEnabled
)
279 m_bCTLFontEnabled
= _bEnabled
;
281 NotifyListeners(ConfigurationHints::NONE
);
284 void SvtCTLOptions_Impl::SetCTLSequenceChecking( bool _bEnabled
)
286 if(!m_bROCTLSequenceChecking
&& m_bCTLSequenceChecking
!= _bEnabled
)
289 m_bCTLSequenceChecking
= _bEnabled
;
290 NotifyListeners(ConfigurationHints::NONE
);
293 void SvtCTLOptions_Impl::SetCTLSequenceCheckingRestricted( bool _bEnabled
)
295 if(!m_bROCTLRestricted
&& m_bCTLRestricted
!= _bEnabled
)
298 m_bCTLRestricted
= _bEnabled
;
299 NotifyListeners(ConfigurationHints::NONE
);
302 void SvtCTLOptions_Impl::SetCTLSequenceCheckingTypeAndReplace( bool _bEnabled
)
304 if(!m_bROCTLTypeAndReplace
&& m_bCTLTypeAndReplace
!= _bEnabled
)
307 m_bCTLTypeAndReplace
= _bEnabled
;
308 NotifyListeners(ConfigurationHints::NONE
);
311 void SvtCTLOptions_Impl::SetCTLCursorMovement( SvtCTLOptions::CursorMovement _eMovement
)
313 if (!m_bROCTLCursorMovement
&& m_eCTLCursorMovement
!= _eMovement
)
316 m_eCTLCursorMovement
= _eMovement
;
317 NotifyListeners(ConfigurationHints::NONE
);
320 void SvtCTLOptions_Impl::SetCTLTextNumerals( SvtCTLOptions::TextNumerals _eNumerals
)
322 if (!m_bROCTLTextNumerals
&& m_eCTLTextNumerals
!= _eNumerals
)
325 m_eCTLTextNumerals
= _eNumerals
;
326 NotifyListeners(ConfigurationHints::NONE
);
333 std::weak_ptr
<SvtCTLOptions_Impl
> g_pCTLOptions
;
335 osl::Mutex
& CTLMutex()
337 static osl::Mutex aMutex
;
342 SvtCTLOptions::SvtCTLOptions( bool bDontLoad
)
344 // Global access, must be guarded (multithreading)
345 ::osl::MutexGuard
aGuard( CTLMutex() );
347 m_pImpl
= g_pCTLOptions
.lock();
350 m_pImpl
= std::make_shared
<SvtCTLOptions_Impl
>();
351 g_pCTLOptions
= m_pImpl
;
352 ItemHolder2::holdConfigItem(EItem::CTLOptions
);
355 if( !bDontLoad
&& !m_pImpl
->IsLoaded() )
358 m_pImpl
->AddListener(this);
362 SvtCTLOptions::~SvtCTLOptions()
364 // Global access, must be guarded (multithreading)
365 ::osl::MutexGuard
aGuard( CTLMutex() );
367 m_pImpl
->RemoveListener(this);
371 void SvtCTLOptions::SetCTLFontEnabled( bool _bEnabled
)
373 assert(m_pImpl
->IsLoaded());
374 m_pImpl
->SetCTLFontEnabled( _bEnabled
);
377 bool SvtCTLOptions::IsCTLFontEnabled()
379 return officecfg::Office::Common::I18N::CTL::CTLFont::get();
382 void SvtCTLOptions::SetCTLSequenceChecking( bool _bEnabled
)
384 assert(m_pImpl
->IsLoaded());
385 m_pImpl
->SetCTLSequenceChecking(_bEnabled
);
388 bool SvtCTLOptions::IsCTLSequenceChecking()
390 return officecfg::Office::Common::I18N::CTL::CTLSequenceChecking::get();
393 void SvtCTLOptions::SetCTLSequenceCheckingRestricted( bool _bEnable
)
395 assert(m_pImpl
->IsLoaded());
396 m_pImpl
->SetCTLSequenceCheckingRestricted(_bEnable
);
399 bool SvtCTLOptions::IsCTLSequenceCheckingRestricted()
401 return officecfg::Office::Common::I18N::CTL::CTLSequenceCheckingRestricted::get();
404 void SvtCTLOptions::SetCTLSequenceCheckingTypeAndReplace( bool _bEnable
)
406 assert(m_pImpl
->IsLoaded());
407 m_pImpl
->SetCTLSequenceCheckingTypeAndReplace(_bEnable
);
410 bool SvtCTLOptions::IsCTLSequenceCheckingTypeAndReplace()
412 return officecfg::Office::Common::I18N::CTL::CTLSequenceCheckingTypeAndReplace::get();
415 void SvtCTLOptions::SetCTLCursorMovement( SvtCTLOptions::CursorMovement _eMovement
)
417 assert(m_pImpl
->IsLoaded());
418 m_pImpl
->SetCTLCursorMovement( _eMovement
);
421 SvtCTLOptions::CursorMovement
SvtCTLOptions::GetCTLCursorMovement()
423 return static_cast<SvtCTLOptions::CursorMovement
>(officecfg::Office::Common::I18N::CTL::CTLCursorMovement::get());
426 void SvtCTLOptions::SetCTLTextNumerals( SvtCTLOptions::TextNumerals _eNumerals
)
428 assert(m_pImpl
->IsLoaded());
429 m_pImpl
->SetCTLTextNumerals( _eNumerals
);
432 SvtCTLOptions::TextNumerals
SvtCTLOptions::GetCTLTextNumerals()
434 if (comphelper::IsFuzzing())
435 return SvtCTLOptions::NUMERALS_ARABIC
;
436 return static_cast<SvtCTLOptions::TextNumerals
>(officecfg::Office::Common::I18N::CTL::CTLTextNumerals::get());
439 bool SvtCTLOptions::IsReadOnly(EOption eOption
) const
441 assert(m_pImpl
->IsLoaded());
442 return m_pImpl
->IsReadOnly(eOption
);
446 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */