cid#1640468 Dereference after null check
[LibreOffice.git] / unotools / source / misc / syslocale.cxx
blob63b98f9f1614f5576c51d1dae5174d92dc76cc3d
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 com::sun::star;
40 namespace {
42 std::weak_ptr<SvtSysLocale_Impl> g_pSysLocale;
44 // static
45 std::mutex& GetMutex()
47 // #i77768# Due to a static reference in the toolkit lib
48 // we need a mutex that lives longer than the svl library.
49 // Otherwise the dtor would use a destructed mutex!!
50 static std::mutex* persistentMutex(new std::mutex);
52 return *persistentMutex;
58 class SvtSysLocale_Impl : public utl::ConfigurationListener
60 public:
61 SvtSysLocaleOptions aSysLocaleOptions;
62 std::optional<LocaleDataWrapper> moLocaleData;
63 std::optional<CharClass> moCharClass;
65 SvtSysLocale_Impl();
66 virtual ~SvtSysLocale_Impl() override;
68 CharClass& GetCharClass();
69 virtual void ConfigurationChanged( utl::ConfigurationBroadcaster*, ConfigurationHints ) override;
71 private:
72 std::vector<OUString> getDateAcceptancePatternsConfig() const;
75 SvtSysLocale_Impl::SvtSysLocale_Impl()
77 moLocaleData.emplace(
78 aSysLocaleOptions.GetRealLanguageTag(),
79 getDateAcceptancePatternsConfig() );
81 // listen for further changes
82 aSysLocaleOptions.AddListener( this );
85 SvtSysLocale_Impl::~SvtSysLocale_Impl()
87 aSysLocaleOptions.RemoveListener( this );
90 CharClass& SvtSysLocale_Impl::GetCharClass()
92 if ( !moCharClass )
93 moCharClass.emplace( aSysLocaleOptions.GetRealLanguageTag() );
94 return *moCharClass;
97 void SvtSysLocale_Impl::ConfigurationChanged( utl::ConfigurationBroadcaster*, ConfigurationHints nHint )
99 if ( !(nHint & ConfigurationHints::Locale) &&
100 !(nHint & ConfigurationHints::DatePatterns) )
101 return;
103 std::unique_lock aGuard( GetMutex() );
105 const LanguageTag& rLanguageTag = aSysLocaleOptions.GetRealLanguageTag();
106 if ( nHint & ConfigurationHints::Locale )
108 moCharClass.emplace( rLanguageTag );
110 moLocaleData.emplace(rLanguageTag, getDateAcceptancePatternsConfig());
113 std::vector<OUString> SvtSysLocale_Impl::getDateAcceptancePatternsConfig() const
115 OUString aStr( aSysLocaleOptions.GetDatePatternsConfigString());
116 if (aStr.isEmpty())
117 return {}; // reset
118 ::std::vector< OUString > aVec;
119 for (sal_Int32 nIndex = 0; nIndex >= 0; /*nop*/)
121 OUString aTok( aStr.getToken( 0, ';', nIndex));
122 if (!aTok.isEmpty())
123 aVec.push_back( aTok);
125 return aVec;
128 SvtSysLocale::SvtSysLocale()
130 std::unique_lock aGuard( GetMutex() );
131 pImpl = g_pSysLocale.lock();
132 if ( !pImpl )
134 pImpl = std::make_shared<SvtSysLocale_Impl>();
135 g_pSysLocale = pImpl;
139 SvtSysLocale::~SvtSysLocale()
141 std::unique_lock aGuard( GetMutex() );
142 pImpl.reset();
145 const LocaleDataWrapper& SvtSysLocale::GetLocaleData() const
147 return *(pImpl->moLocaleData);
150 const CharClass& SvtSysLocale::GetCharClass() const
152 return pImpl->GetCharClass();
155 SvtSysLocaleOptions& SvtSysLocale::GetOptions() const
157 return pImpl->aSysLocaleOptions;
160 const LanguageTag& SvtSysLocale::GetLanguageTag() const
162 if (comphelper::LibreOfficeKit::isActive())
163 return comphelper::LibreOfficeKit::getLocale();
165 return pImpl->aSysLocaleOptions.GetRealLanguageTag();
168 const LanguageTag& SvtSysLocale::GetUILanguageTag() const
170 if (comphelper::LibreOfficeKit::isActive())
171 return comphelper::LibreOfficeKit::getLanguageTag();
173 return pImpl->aSysLocaleOptions.GetRealUILanguageTag();
176 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */