Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / unotools / source / misc / syslocale.cxx
blob31b448e6583a792115da1741a264d42af0a23bdb
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 <sal/config.h>
22 #include <sal/log.hxx>
23 #include <unotools/localedatawrapper.hxx>
24 #include <unotools/charclass.hxx>
25 #include <unotools/syslocale.hxx>
26 #include <unotools/syslocaleoptions.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>
33 #include <memory>
34 #include <mutex>
35 #include <optional>
36 #include <vector>
38 using namespace osl;
39 using namespace com::sun::star;
41 namespace {
43 std::weak_ptr<SvtSysLocale_Impl> g_pSysLocale;
45 // static
46 std::mutex& GetMutex()
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);
53 return *persistentMutex;
59 class SvtSysLocale_Impl : public utl::ConfigurationListener
61 public:
62 SvtSysLocaleOptions aSysLocaleOptions;
63 std::optional<LocaleDataWrapper> moLocaleData;
64 std::optional<CharClass> moCharClass;
66 SvtSysLocale_Impl();
67 virtual ~SvtSysLocale_Impl() override;
69 CharClass& GetCharClass();
70 virtual void ConfigurationChanged( utl::ConfigurationBroadcaster*, ConfigurationHints ) override;
72 private:
73 std::vector<OUString> getDateAcceptancePatternsConfig() const;
76 SvtSysLocale_Impl::SvtSysLocale_Impl()
78 moLocaleData.emplace(
79 aSysLocaleOptions.GetRealLanguageTag(),
80 getDateAcceptancePatternsConfig() );
82 // listen for further changes
83 aSysLocaleOptions.AddListener( this );
86 SvtSysLocale_Impl::~SvtSysLocale_Impl()
88 aSysLocaleOptions.RemoveListener( this );
91 CharClass& SvtSysLocale_Impl::GetCharClass()
93 if ( !moCharClass )
94 moCharClass.emplace( aSysLocaleOptions.GetRealLanguageTag() );
95 return *moCharClass;
98 void SvtSysLocale_Impl::ConfigurationChanged( utl::ConfigurationBroadcaster*, ConfigurationHints nHint )
100 if ( !(nHint & ConfigurationHints::Locale) &&
101 !(nHint & ConfigurationHints::DatePatterns) )
102 return;
104 std::unique_lock aGuard( GetMutex() );
106 const LanguageTag& rLanguageTag = aSysLocaleOptions.GetRealLanguageTag();
107 if ( nHint & ConfigurationHints::Locale )
109 moCharClass.emplace( rLanguageTag );
111 moLocaleData.emplace(rLanguageTag, getDateAcceptancePatternsConfig());
114 std::vector<OUString> SvtSysLocale_Impl::getDateAcceptancePatternsConfig() const
116 OUString aStr( aSysLocaleOptions.GetDatePatternsConfigString());
117 if (aStr.isEmpty())
118 return {}; // reset
119 ::std::vector< OUString > aVec;
120 for (sal_Int32 nIndex = 0; nIndex >= 0; /*nop*/)
122 OUString aTok( aStr.getToken( 0, ';', nIndex));
123 if (!aTok.isEmpty())
124 aVec.push_back( aTok);
126 return aVec;
129 SvtSysLocale::SvtSysLocale()
131 std::unique_lock aGuard( GetMutex() );
132 pImpl = g_pSysLocale.lock();
133 if ( !pImpl )
135 pImpl = std::make_shared<SvtSysLocale_Impl>();
136 g_pSysLocale = pImpl;
140 SvtSysLocale::~SvtSysLocale()
142 std::unique_lock aGuard( GetMutex() );
143 pImpl.reset();
146 const LocaleDataWrapper& SvtSysLocale::GetLocaleData() const
148 return *(pImpl->moLocaleData);
151 const CharClass& SvtSysLocale::GetCharClass() const
153 return pImpl->GetCharClass();
156 SvtSysLocaleOptions& SvtSysLocale::GetOptions() const
158 return pImpl->aSysLocaleOptions;
161 const LanguageTag& SvtSysLocale::GetLanguageTag() const
163 if (comphelper::LibreOfficeKit::isActive())
164 return comphelper::LibreOfficeKit::getLocale();
166 return pImpl->aSysLocaleOptions.GetRealLanguageTag();
169 const LanguageTag& SvtSysLocale::GetUILanguageTag() const
171 if (comphelper::LibreOfficeKit::isActive())
172 return comphelper::LibreOfficeKit::getLanguageTag();
174 return pImpl->aSysLocaleOptions.GetRealUILanguageTag();
177 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */