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