LibreOffice Module unotools (master) 1
syslocale.cxx
Go to the documentation of this file.
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#include <sal/config.h>
21
22#include <sal/log.hxx>
27#include <comphelper/lok.hxx>
28#include <rtl/tencinfo.h>
29#include <rtl/locale.h>
30#include <osl/thread.h>
31#include <osl/nlsupport.h>
32
33#include <memory>
34#include <mutex>
35#include <optional>
36#include <vector>
37
38using namespace osl;
39using namespace com::sun::star;
40
41namespace {
42
43std::weak_ptr<SvtSysLocale_Impl> g_pSysLocale;
44
45// static
46std::mutex& GetMutex()
47{
48 // #i77768# Due to a static reference in the toolkit lib
49 // we need a mutex that lives longer than the svl library.
50 // Otherwise the dtor would use a destructed mutex!!
51 static std::mutex* persistentMutex(new std::mutex);
52
53 return *persistentMutex;
54}
55
56
57}
58
60{
61public:
63 std::optional<LocaleDataWrapper> moLocaleData;
64 std::optional<CharClass> moCharClass;
65
67 virtual ~SvtSysLocale_Impl() override;
68
71
72private:
73 std::vector<OUString> getDateAcceptancePatternsConfig() const;
74};
75
77{
78 moLocaleData.emplace(
81
82 // listen for further changes
84}
85
87{
89}
90
92{
93 if ( !moCharClass )
95 return *moCharClass;
96}
97
99{
100 if ( !(nHint & ConfigurationHints::Locale) &&
102 return;
103
104 std::unique_lock aGuard( GetMutex() );
105
106 const LanguageTag& rLanguageTag = aSysLocaleOptions.GetRealLanguageTag();
107 if ( nHint & ConfigurationHints::Locale )
108 {
109 moCharClass.emplace( rLanguageTag );
110 }
111 moLocaleData.emplace(rLanguageTag, getDateAcceptancePatternsConfig());
112}
113
115{
117 if (aStr.isEmpty())
118 return {}; // reset
119 ::std::vector< OUString > aVec;
120 for (sal_Int32 nIndex = 0; nIndex >= 0; /*nop*/)
121 {
122 OUString aTok( aStr.getToken( 0, ';', nIndex));
123 if (!aTok.isEmpty())
124 aVec.push_back( aTok);
125 }
126 return aVec;
127}
128
130{
131 std::unique_lock aGuard( GetMutex() );
132 pImpl = g_pSysLocale.lock();
133 if ( !pImpl )
134 {
135 pImpl = std::make_shared<SvtSysLocale_Impl>();
136 g_pSysLocale = pImpl;
137 }
138}
139
141{
142 std::unique_lock aGuard( GetMutex() );
143 pImpl.reset();
144}
145
147{
148 return *(pImpl->moLocaleData);
149}
150
152{
153 return pImpl->GetCharClass();
154}
155
157{
158 return pImpl->aSysLocaleOptions;
159}
160
162{
165
166 return pImpl->aSysLocaleOptions.GetRealLanguageTag();
167}
168
170{
173
174 return pImpl->aSysLocaleOptions.GetRealUILanguageTag();
175}
176
177/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
This class can be accessed without locking because we load all of the data in the constructor.
const OUString & GetDatePatternsConfigString() const
The config string may be empty to denote the default DateAcceptancePatterns of the locale.
const LanguageTag & GetRealLanguageTag() const
Get locale set, always resolved to the real locale.
std::vector< OUString > getDateAcceptancePatternsConfig() const
Definition: syslocale.cxx:114
virtual void ConfigurationChanged(utl::ConfigurationBroadcaster *, ConfigurationHints) override
Definition: syslocale.cxx:98
std::optional< LocaleDataWrapper > moLocaleData
Definition: syslocale.cxx:63
SvtSysLocaleOptions aSysLocaleOptions
Definition: syslocale.cxx:62
std::optional< CharClass > moCharClass
Definition: syslocale.cxx:64
CharClass & GetCharClass()
Definition: syslocale.cxx:91
virtual ~SvtSysLocale_Impl() override
Definition: syslocale.cxx:86
const LanguageTag & GetLanguageTag() const
Definition: syslocale.cxx:161
const LanguageTag & GetUILanguageTag() const
Definition: syslocale.cxx:169
SvtSysLocaleOptions & GetOptions() const
It is safe to store the pointers locally and use them AS LONG AS THE INSTANCE OF SvtSysLocale LIVES!...
Definition: syslocale.cxx:156
std::shared_ptr< SvtSysLocale_Impl > pImpl
Definition: syslocale.hxx:47
const LocaleDataWrapper & GetLocaleData() const
Definition: syslocale.cxx:146
const CharClass & GetCharClass() const
Definition: syslocale.cxx:151
void AddListener(utl::ConfigurationListener *pListener)
Definition: options.cxx:59
void RemoveListener(utl::ConfigurationListener const *pListener)
Definition: options.cxx:66
sal_Int32 nIndex
aStr
const LanguageTag & getLocale()
const LanguageTag & getLanguageTag()
ConfigurationHints
Definition: options.hxx:30
std::mutex mutex
Definition: textsearch.cxx:94