Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 :
21 : #include <svl/ctloptions.hxx>
22 :
23 : #include <svl/languageoptions.hxx>
24 : #include <i18nlangtag/mslangid.hxx>
25 : #include <unotools/configitem.hxx>
26 : #include <tools/debug.hxx>
27 : #include <com/sun/star/uno/Any.h>
28 : #include <com/sun/star/uno/Sequence.hxx>
29 : #include <osl/mutex.hxx>
30 : #include <svl/smplhint.hxx>
31 : #include <rtl/instance.hxx>
32 : #include <unotools/syslocale.hxx>
33 : #include <itemholder2.hxx>
34 :
35 : using namespace ::com::sun::star;
36 : using namespace ::com::sun::star::uno;
37 :
38 : #define CFG_READONLY_DEFAULT false
39 :
40 : // SvtCJKOptions_Impl ----------------------------------------------------------
41 :
42 : class SvtCTLOptions_Impl : public utl::ConfigItem
43 : {
44 : private:
45 : bool m_bIsLoaded;
46 : bool m_bCTLFontEnabled;
47 : bool m_bCTLSequenceChecking;
48 : bool m_bCTLRestricted;
49 : bool m_bCTLTypeAndReplace;
50 : SvtCTLOptions::CursorMovement m_eCTLCursorMovement;
51 : SvtCTLOptions::TextNumerals m_eCTLTextNumerals;
52 :
53 : bool m_bROCTLFontEnabled;
54 : bool m_bROCTLSequenceChecking;
55 : bool m_bROCTLRestricted;
56 : bool m_bROCTLTypeAndReplace;
57 : bool m_bROCTLCursorMovement;
58 : bool m_bROCTLTextNumerals;
59 :
60 : public:
61 : SvtCTLOptions_Impl();
62 : virtual ~SvtCTLOptions_Impl();
63 :
64 : virtual void Notify( const Sequence< OUString >& _aPropertyNames ) SAL_OVERRIDE;
65 : virtual void Commit() SAL_OVERRIDE;
66 : void Load();
67 :
68 0 : bool IsLoaded() { return m_bIsLoaded; }
69 : void SetCTLFontEnabled( bool _bEnabled );
70 0 : bool IsCTLFontEnabled() const { return m_bCTLFontEnabled; }
71 :
72 : void SetCTLSequenceChecking( bool _bEnabled );
73 0 : bool IsCTLSequenceChecking() const { return m_bCTLSequenceChecking;}
74 :
75 : void SetCTLSequenceCheckingRestricted( bool _bEnable );
76 0 : bool IsCTLSequenceCheckingRestricted( void ) const { return m_bCTLRestricted; }
77 :
78 : void SetCTLSequenceCheckingTypeAndReplace( bool _bEnable );
79 0 : bool IsCTLSequenceCheckingTypeAndReplace() const { return m_bCTLTypeAndReplace; }
80 :
81 : void SetCTLCursorMovement( SvtCTLOptions::CursorMovement _eMovement );
82 : SvtCTLOptions::CursorMovement
83 0 : GetCTLCursorMovement() const { return m_eCTLCursorMovement; }
84 :
85 : void SetCTLTextNumerals( SvtCTLOptions::TextNumerals _eNumerals );
86 : SvtCTLOptions::TextNumerals
87 0 : GetCTLTextNumerals() const { return m_eCTLTextNumerals; }
88 :
89 : bool IsReadOnly(SvtCTLOptions::EOption eOption) const;
90 : };
91 : namespace
92 : {
93 : struct PropertyNames
94 : : public rtl::Static< Sequence< OUString >, PropertyNames > {};
95 : }
96 0 : bool SvtCTLOptions_Impl::IsReadOnly(SvtCTLOptions::EOption eOption) const
97 : {
98 0 : bool bReadOnly = CFG_READONLY_DEFAULT;
99 0 : switch(eOption)
100 : {
101 0 : case SvtCTLOptions::E_CTLFONT : bReadOnly = m_bROCTLFontEnabled ; break;
102 0 : case SvtCTLOptions::E_CTLSEQUENCECHECKING : bReadOnly = m_bROCTLSequenceChecking ; break;
103 0 : case SvtCTLOptions::E_CTLCURSORMOVEMENT : bReadOnly = m_bROCTLCursorMovement ; break;
104 0 : case SvtCTLOptions::E_CTLTEXTNUMERALS : bReadOnly = m_bROCTLTextNumerals ; break;
105 0 : case SvtCTLOptions::E_CTLSEQUENCECHECKINGRESTRICTED: bReadOnly = m_bROCTLRestricted ; break;
106 0 : case SvtCTLOptions::E_CTLSEQUENCECHECKINGTYPEANDREPLACE: bReadOnly = m_bROCTLTypeAndReplace; break;
107 : default: OSL_FAIL( "SvtCTLOptions_Impl::IsReadOnly() - invalid option" );
108 : }
109 0 : return bReadOnly;
110 : }
111 0 : SvtCTLOptions_Impl::SvtCTLOptions_Impl() :
112 :
113 : utl::ConfigItem("Office.Common/I18N/CTL"),
114 :
115 : m_bIsLoaded ( false ),
116 : m_bCTLFontEnabled ( false ),
117 : m_bCTLSequenceChecking ( false ),
118 : m_bCTLRestricted ( false ),
119 : m_bCTLTypeAndReplace ( false ),
120 : m_eCTLCursorMovement ( SvtCTLOptions::MOVEMENT_LOGICAL ),
121 : m_eCTLTextNumerals ( SvtCTLOptions::NUMERALS_ARABIC ),
122 :
123 : m_bROCTLFontEnabled ( CFG_READONLY_DEFAULT ),
124 : m_bROCTLSequenceChecking( CFG_READONLY_DEFAULT ),
125 : m_bROCTLRestricted ( CFG_READONLY_DEFAULT ),
126 : m_bROCTLTypeAndReplace ( CFG_READONLY_DEFAULT ),
127 : m_bROCTLCursorMovement ( CFG_READONLY_DEFAULT ),
128 0 : m_bROCTLTextNumerals ( CFG_READONLY_DEFAULT )
129 : {
130 0 : }
131 0 : SvtCTLOptions_Impl::~SvtCTLOptions_Impl()
132 : {
133 0 : if ( IsModified() )
134 0 : Commit();
135 0 : }
136 :
137 0 : void SvtCTLOptions_Impl::Notify( const Sequence< OUString >& )
138 : {
139 0 : Load();
140 0 : NotifyListeners(SFX_HINT_CTL_SETTINGS_CHANGED);
141 0 : }
142 :
143 0 : void SvtCTLOptions_Impl::Commit()
144 : {
145 0 : Sequence< OUString > &rPropertyNames = PropertyNames::get();
146 0 : OUString* pOrgNames = rPropertyNames.getArray();
147 0 : sal_Int32 nOrgCount = rPropertyNames.getLength();
148 :
149 0 : Sequence< OUString > aNames( nOrgCount );
150 0 : Sequence< Any > aValues( nOrgCount );
151 :
152 0 : OUString* pNames = aNames.getArray();
153 0 : Any* pValues = aValues.getArray();
154 0 : sal_Int32 nRealCount = 0;
155 :
156 0 : const uno::Type& rType = ::getBooleanCppuType();
157 :
158 0 : for ( int nProp = 0; nProp < nOrgCount; nProp++ )
159 : {
160 0 : switch ( nProp )
161 : {
162 : case 0:
163 : {
164 0 : if (!m_bROCTLFontEnabled)
165 : {
166 0 : pNames[nRealCount] = pOrgNames[nProp];
167 0 : pValues[nRealCount].setValue( &m_bCTLFontEnabled, rType );
168 0 : ++nRealCount;
169 : }
170 : }
171 0 : break;
172 :
173 : case 1:
174 : {
175 0 : if (!m_bROCTLSequenceChecking)
176 : {
177 0 : pNames[nRealCount] = pOrgNames[nProp];
178 0 : pValues[nRealCount].setValue( &m_bCTLSequenceChecking, rType );
179 0 : ++nRealCount;
180 : }
181 : }
182 0 : break;
183 :
184 : case 2:
185 : {
186 0 : if (!m_bROCTLCursorMovement)
187 : {
188 0 : pNames[nRealCount] = pOrgNames[nProp];
189 0 : pValues[nRealCount] <<= (sal_Int32)m_eCTLCursorMovement;
190 0 : ++nRealCount;
191 : }
192 : }
193 0 : break;
194 :
195 : case 3:
196 : {
197 0 : if (!m_bROCTLTextNumerals)
198 : {
199 0 : pNames[nRealCount] = pOrgNames[nProp];
200 0 : pValues[nRealCount] <<= (sal_Int32)m_eCTLTextNumerals;
201 0 : ++nRealCount;
202 : }
203 : }
204 0 : break;
205 :
206 : case 4:
207 : {
208 0 : if (!m_bROCTLRestricted)
209 : {
210 0 : pNames[nRealCount] = pOrgNames[nProp];
211 0 : pValues[nRealCount].setValue( &m_bCTLRestricted, rType );
212 0 : ++nRealCount;
213 : }
214 : }
215 0 : break;
216 : case 5:
217 : {
218 0 : if(!m_bROCTLTypeAndReplace)
219 : {
220 0 : pNames[nRealCount] = pOrgNames[nProp];
221 0 : pValues[nRealCount].setValue( &m_bCTLTypeAndReplace, rType );
222 0 : ++nRealCount;
223 : }
224 : }
225 0 : break;
226 : }
227 : }
228 0 : aNames.realloc(nRealCount);
229 0 : aValues.realloc(nRealCount);
230 0 : PutProperties( aNames, aValues );
231 : //broadcast changes
232 0 : NotifyListeners(SFX_HINT_CTL_SETTINGS_CHANGED);
233 0 : }
234 :
235 0 : void SvtCTLOptions_Impl::Load()
236 : {
237 0 : Sequence< OUString >& rPropertyNames = PropertyNames::get();
238 0 : if ( !rPropertyNames.getLength() )
239 : {
240 0 : rPropertyNames.realloc(6);
241 0 : OUString* pNames = rPropertyNames.getArray();
242 0 : pNames[0] = "CTLFont";
243 0 : pNames[1] = "CTLSequenceChecking";
244 0 : pNames[2] = "CTLCursorMovement";
245 0 : pNames[3] = "CTLTextNumerals";
246 0 : pNames[4] = "CTLSequenceCheckingRestricted";
247 0 : pNames[5] = "CTLSequenceCheckingTypeAndReplace";
248 0 : EnableNotification( rPropertyNames );
249 : }
250 0 : Sequence< Any > aValues = GetProperties( rPropertyNames );
251 0 : Sequence< sal_Bool > aROStates = GetReadOnlyStates( rPropertyNames );
252 0 : const Any* pValues = aValues.getConstArray();
253 0 : const sal_Bool* pROStates = aROStates.getConstArray();
254 : DBG_ASSERT( aValues.getLength() == rPropertyNames.getLength(), "GetProperties failed" );
255 : DBG_ASSERT( aROStates.getLength() == rPropertyNames.getLength(), "GetReadOnlyStates failed" );
256 0 : if ( aValues.getLength() == rPropertyNames.getLength() && aROStates.getLength() == rPropertyNames.getLength() )
257 : {
258 0 : bool bValue = false;
259 0 : sal_Int32 nValue = 0;
260 :
261 0 : for ( int nProp = 0; nProp < rPropertyNames.getLength(); nProp++ )
262 : {
263 0 : if ( pValues[nProp].hasValue() )
264 : {
265 0 : if ( pValues[nProp] >>= bValue )
266 : {
267 0 : switch ( nProp )
268 : {
269 0 : case 0: { m_bCTLFontEnabled = bValue; m_bROCTLFontEnabled = pROStates[nProp]; } break;
270 0 : case 1: { m_bCTLSequenceChecking = bValue; m_bROCTLSequenceChecking = pROStates[nProp]; } break;
271 0 : case 4: { m_bCTLRestricted = bValue; m_bROCTLRestricted = pROStates[nProp]; } break;
272 0 : case 5: { m_bCTLTypeAndReplace = bValue; m_bROCTLTypeAndReplace = pROStates[nProp]; } break;
273 : }
274 : }
275 0 : else if ( pValues[nProp] >>= nValue )
276 : {
277 0 : switch ( nProp )
278 : {
279 0 : case 2: { m_eCTLCursorMovement = (SvtCTLOptions::CursorMovement)nValue; m_bROCTLCursorMovement = pROStates[nProp]; } break;
280 0 : case 3: { m_eCTLTextNumerals = (SvtCTLOptions::TextNumerals)nValue; m_bROCTLTextNumerals = pROStates[nProp]; } break;
281 : }
282 : }
283 : }
284 : }
285 : }
286 :
287 0 : if (!m_bCTLFontEnabled)
288 : {
289 0 : sal_uInt16 nScriptType = SvtLanguageOptions::GetScriptTypeOfLanguage(LANGUAGE_SYSTEM);
290 : //system locale is CTL
291 0 : bool bAutoEnableCTL = (nScriptType & SCRIPTTYPE_COMPLEX);
292 :
293 0 : LanguageType eSystemLanguage = LANGUAGE_SYSTEM;
294 :
295 0 : if (!bAutoEnableCTL)
296 : {
297 0 : SvtSystemLanguageOptions aSystemLocaleSettings;
298 :
299 : //windows secondary system locale is CTL
300 0 : eSystemLanguage = aSystemLocaleSettings.GetWin16SystemLanguage();
301 0 : if (eSystemLanguage != LANGUAGE_SYSTEM)
302 : {
303 0 : sal_uInt16 nWinScript = SvtLanguageOptions::GetScriptTypeOfLanguage( eSystemLanguage );
304 0 : bAutoEnableCTL = (nWinScript & SCRIPTTYPE_COMPLEX);
305 : }
306 :
307 : //CTL keyboard is installed
308 0 : if (!bAutoEnableCTL)
309 0 : bAutoEnableCTL = aSystemLocaleSettings.isCTLKeyboardLayoutInstalled();
310 : }
311 :
312 0 : if (bAutoEnableCTL)
313 : {
314 0 : m_bCTLFontEnabled = true;
315 0 : sal_uInt16 nLanguage = SvtSysLocale().GetLanguageTag().getLanguageType();
316 : //enable sequence checking for the appropriate languages
317 : m_bCTLSequenceChecking = m_bCTLRestricted = m_bCTLTypeAndReplace =
318 0 : (MsLangId::needsSequenceChecking( nLanguage) ||
319 0 : MsLangId::needsSequenceChecking( eSystemLanguage));
320 0 : Commit();
321 : }
322 : }
323 :
324 0 : m_bIsLoaded = true;
325 0 : }
326 0 : void SvtCTLOptions_Impl::SetCTLFontEnabled( bool _bEnabled )
327 : {
328 0 : if(!m_bROCTLFontEnabled && m_bCTLFontEnabled != _bEnabled)
329 : {
330 0 : m_bCTLFontEnabled = _bEnabled;
331 0 : SetModified();
332 0 : NotifyListeners(0);
333 : }
334 0 : }
335 0 : void SvtCTLOptions_Impl::SetCTLSequenceChecking( bool _bEnabled )
336 : {
337 0 : if(!m_bROCTLSequenceChecking && m_bCTLSequenceChecking != _bEnabled)
338 : {
339 0 : SetModified();
340 0 : m_bCTLSequenceChecking = _bEnabled;
341 0 : NotifyListeners(0);
342 : }
343 0 : }
344 0 : void SvtCTLOptions_Impl::SetCTLSequenceCheckingRestricted( bool _bEnabled )
345 : {
346 0 : if(!m_bROCTLRestricted && m_bCTLRestricted != _bEnabled)
347 : {
348 0 : SetModified();
349 0 : m_bCTLRestricted = _bEnabled;
350 0 : NotifyListeners(0);
351 : }
352 0 : }
353 0 : void SvtCTLOptions_Impl::SetCTLSequenceCheckingTypeAndReplace( bool _bEnabled )
354 : {
355 0 : if(!m_bROCTLTypeAndReplace && m_bCTLTypeAndReplace != _bEnabled)
356 : {
357 0 : SetModified();
358 0 : m_bCTLTypeAndReplace = _bEnabled;
359 0 : NotifyListeners(0);
360 : }
361 0 : }
362 0 : void SvtCTLOptions_Impl::SetCTLCursorMovement( SvtCTLOptions::CursorMovement _eMovement )
363 : {
364 0 : if (!m_bROCTLCursorMovement && m_eCTLCursorMovement != _eMovement )
365 : {
366 0 : SetModified();
367 0 : m_eCTLCursorMovement = _eMovement;
368 0 : NotifyListeners(0);
369 : }
370 0 : }
371 0 : void SvtCTLOptions_Impl::SetCTLTextNumerals( SvtCTLOptions::TextNumerals _eNumerals )
372 : {
373 0 : if (!m_bROCTLTextNumerals && m_eCTLTextNumerals != _eNumerals )
374 : {
375 0 : SetModified();
376 0 : m_eCTLTextNumerals = _eNumerals;
377 0 : NotifyListeners(0);
378 : }
379 0 : }
380 : // global ----------------------------------------------------------------
381 :
382 : static SvtCTLOptions_Impl* pCTLOptions = NULL;
383 : static sal_Int32 nCTLRefCount = 0;
384 : namespace { struct CTLMutex : public rtl::Static< osl::Mutex, CTLMutex > {}; }
385 :
386 : // class SvtCTLOptions --------------------------------------------------
387 :
388 0 : SvtCTLOptions::SvtCTLOptions( bool bDontLoad )
389 : {
390 : // Global access, must be guarded (multithreading)
391 0 : ::osl::MutexGuard aGuard( CTLMutex::get() );
392 0 : if ( !pCTLOptions )
393 : {
394 0 : pCTLOptions = new SvtCTLOptions_Impl;
395 0 : ItemHolder2::holdConfigItem(E_CTLOPTIONS);
396 : }
397 0 : if( !bDontLoad && !pCTLOptions->IsLoaded() )
398 0 : pCTLOptions->Load();
399 :
400 0 : ++nCTLRefCount;
401 0 : m_pImp = pCTLOptions;
402 0 : m_pImp->AddListener(this);
403 0 : }
404 :
405 :
406 :
407 0 : SvtCTLOptions::~SvtCTLOptions()
408 : {
409 : // Global access, must be guarded (multithreading)
410 0 : ::osl::MutexGuard aGuard( CTLMutex::get() );
411 :
412 0 : m_pImp->RemoveListener(this);
413 0 : if ( !--nCTLRefCount )
414 0 : DELETEZ( pCTLOptions );
415 0 : }
416 :
417 0 : void SvtCTLOptions::SetCTLFontEnabled( bool _bEnabled )
418 : {
419 : DBG_ASSERT( pCTLOptions->IsLoaded(), "CTL options not loaded" );
420 0 : pCTLOptions->SetCTLFontEnabled( _bEnabled );
421 0 : }
422 :
423 0 : bool SvtCTLOptions::IsCTLFontEnabled() const
424 : {
425 : DBG_ASSERT( pCTLOptions->IsLoaded(), "CTL options not loaded" );
426 0 : return pCTLOptions->IsCTLFontEnabled();
427 : }
428 :
429 0 : void SvtCTLOptions::SetCTLSequenceChecking( bool _bEnabled )
430 : {
431 : DBG_ASSERT( pCTLOptions->IsLoaded(), "CTL options not loaded" );
432 0 : pCTLOptions->SetCTLSequenceChecking(_bEnabled);
433 0 : }
434 :
435 0 : bool SvtCTLOptions::IsCTLSequenceChecking() const
436 : {
437 : DBG_ASSERT( pCTLOptions->IsLoaded(), "CTL options not loaded" );
438 0 : return pCTLOptions->IsCTLSequenceChecking();
439 : }
440 :
441 0 : void SvtCTLOptions::SetCTLSequenceCheckingRestricted( bool _bEnable )
442 : {
443 : DBG_ASSERT( pCTLOptions->IsLoaded(), "CTL options not loaded" );
444 0 : pCTLOptions->SetCTLSequenceCheckingRestricted(_bEnable);
445 0 : }
446 :
447 0 : bool SvtCTLOptions::IsCTLSequenceCheckingRestricted( void ) const
448 : {
449 : DBG_ASSERT( pCTLOptions->IsLoaded(), "CTL options not loaded" );
450 0 : return pCTLOptions->IsCTLSequenceCheckingRestricted();
451 : }
452 :
453 0 : void SvtCTLOptions::SetCTLSequenceCheckingTypeAndReplace( bool _bEnable )
454 : {
455 : DBG_ASSERT( pCTLOptions->IsLoaded(), "CTL options not loaded" );
456 0 : pCTLOptions->SetCTLSequenceCheckingTypeAndReplace(_bEnable);
457 0 : }
458 :
459 0 : bool SvtCTLOptions::IsCTLSequenceCheckingTypeAndReplace() const
460 : {
461 : DBG_ASSERT( pCTLOptions->IsLoaded(), "CTL options not loaded" );
462 0 : return pCTLOptions->IsCTLSequenceCheckingTypeAndReplace();
463 : }
464 :
465 0 : void SvtCTLOptions::SetCTLCursorMovement( SvtCTLOptions::CursorMovement _eMovement )
466 : {
467 : DBG_ASSERT( pCTLOptions->IsLoaded(), "CTL options not loaded" );
468 0 : pCTLOptions->SetCTLCursorMovement( _eMovement );
469 0 : }
470 :
471 0 : SvtCTLOptions::CursorMovement SvtCTLOptions::GetCTLCursorMovement() const
472 : {
473 : DBG_ASSERT( pCTLOptions->IsLoaded(), "CTL options not loaded" );
474 0 : return pCTLOptions->GetCTLCursorMovement();
475 : }
476 :
477 0 : void SvtCTLOptions::SetCTLTextNumerals( SvtCTLOptions::TextNumerals _eNumerals )
478 : {
479 : DBG_ASSERT( pCTLOptions->IsLoaded(), "CTL options not loaded" );
480 0 : pCTLOptions->SetCTLTextNumerals( _eNumerals );
481 0 : }
482 :
483 0 : SvtCTLOptions::TextNumerals SvtCTLOptions::GetCTLTextNumerals() const
484 : {
485 : DBG_ASSERT( pCTLOptions->IsLoaded(), "CTL options not loaded" );
486 0 : return pCTLOptions->GetCTLTextNumerals();
487 : }
488 :
489 0 : bool SvtCTLOptions::IsReadOnly(EOption eOption) const
490 : {
491 : DBG_ASSERT( pCTLOptions->IsLoaded(), "CTL options not loaded" );
492 0 : return pCTLOptions->IsReadOnly(eOption);
493 : }
494 :
495 :
496 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|