Coverage Report

Created: 2025-07-07 10:01

/src/libreoffice/unotools/source/misc/syslocale.cxx
Line
Count
Source (jump to first uncovered line)
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>
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>
32
33
#include <memory>
34
#include <mutex>
35
#include <optional>
36
#include <vector>
37
38
using namespace com::sun::star;
39
40
namespace {
41
42
std::weak_ptr<SvtSysLocale_Impl> g_pSysLocale;
43
44
// static
45
std::mutex& GetMutex()
46
82.0M
{
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
82.0M
    static std::mutex* persistentMutex(new std::mutex);
51
52
82.0M
    return *persistentMutex;
53
82.0M
}
54
55
56
}
57
58
class SvtSysLocale_Impl : public utl::ConfigurationListener
59
{
60
public:
61
        SvtSysLocaleOptions                    aSysLocaleOptions;
62
        std::optional<LocaleDataWrapper>       moLocaleData;
63
        std::optional<CharClass>               moCharClass;
64
65
                                SvtSysLocale_Impl();
66
    virtual                     ~SvtSysLocale_Impl() override;
67
68
    CharClass&                  GetCharClass();
69
    virtual void                ConfigurationChanged( utl::ConfigurationBroadcaster*, ConfigurationHints ) override;
70
71
private:
72
    std::vector<OUString>       getDateAcceptancePatternsConfig() const;
73
};
74
75
SvtSysLocale_Impl::SvtSysLocale_Impl()
76
1.34k
{
77
1.34k
    moLocaleData.emplace(
78
1.34k
        aSysLocaleOptions.GetRealLanguageTag(),
79
1.34k
        getDateAcceptancePatternsConfig() );
80
81
    // listen for further changes
82
1.34k
    aSysLocaleOptions.AddListener( this );
83
1.34k
}
84
85
SvtSysLocale_Impl::~SvtSysLocale_Impl()
86
120
{
87
120
    aSysLocaleOptions.RemoveListener( this );
88
120
}
89
90
CharClass& SvtSysLocale_Impl::GetCharClass()
91
10.1M
{
92
10.1M
    if ( !moCharClass )
93
12
        moCharClass.emplace( aSysLocaleOptions.GetRealLanguageTag() );
94
10.1M
    return *moCharClass;
95
10.1M
}
96
97
void SvtSysLocale_Impl::ConfigurationChanged( utl::ConfigurationBroadcaster*, ConfigurationHints nHint )
98
0
{
99
0
    if ( !(nHint & ConfigurationHints::Locale) &&
100
0
         !(nHint & ConfigurationHints::DatePatterns) )
101
0
        return;
102
103
0
    std::unique_lock aGuard( GetMutex() );
104
105
0
    const LanguageTag& rLanguageTag = aSysLocaleOptions.GetRealLanguageTag();
106
0
    if ( nHint & ConfigurationHints::Locale )
107
0
    {
108
0
        moCharClass.emplace( rLanguageTag );
109
0
    }
110
0
    moLocaleData.emplace(rLanguageTag, getDateAcceptancePatternsConfig());
111
0
}
112
113
std::vector<OUString> SvtSysLocale_Impl::getDateAcceptancePatternsConfig() const
114
1.34k
{
115
1.34k
    OUString aStr( aSysLocaleOptions.GetDatePatternsConfigString());
116
1.34k
    if (aStr.isEmpty())
117
1.34k
        return {};  // reset
118
0
    ::std::vector< OUString > aVec;
119
0
    for (sal_Int32 nIndex = 0; nIndex >= 0; /*nop*/)
120
0
    {
121
0
        OUString aTok( aStr.getToken( 0, ';', nIndex));
122
0
        if (!aTok.isEmpty())
123
0
            aVec.push_back( aTok);
124
0
    }
125
0
    return aVec;
126
1.34k
}
127
128
SvtSysLocale::SvtSysLocale()
129
41.0M
{
130
41.0M
    std::unique_lock aGuard( GetMutex() );
131
41.0M
    pImpl = g_pSysLocale.lock();
132
41.0M
    if ( !pImpl )
133
1.34k
    {
134
1.34k
        pImpl = std::make_shared<SvtSysLocale_Impl>();
135
1.34k
        g_pSysLocale = pImpl;
136
1.34k
    }
137
41.0M
}
138
139
SvtSysLocale::~SvtSysLocale()
140
41.0M
{
141
41.0M
    std::unique_lock aGuard( GetMutex() );
142
41.0M
    pImpl.reset();
143
41.0M
}
144
145
const LocaleDataWrapper& SvtSysLocale::GetLocaleData() const
146
325M
{
147
325M
    return *(pImpl->moLocaleData);
148
325M
}
149
150
const CharClass& SvtSysLocale::GetCharClass() const
151
10.1M
{
152
10.1M
    return pImpl->GetCharClass();
153
10.1M
}
154
155
SvtSysLocaleOptions& SvtSysLocale::GetOptions() const
156
0
{
157
0
    return pImpl->aSysLocaleOptions;
158
0
}
159
160
const LanguageTag& SvtSysLocale::GetLanguageTag() const
161
30.0k
{
162
30.0k
    if (comphelper::LibreOfficeKit::isActive())
163
0
        return comphelper::LibreOfficeKit::getLocale();
164
165
30.0k
    return pImpl->aSysLocaleOptions.GetRealLanguageTag();
166
30.0k
}
167
168
const LanguageTag& SvtSysLocale::GetUILanguageTag() const
169
35.1M
{
170
35.1M
    if (comphelper::LibreOfficeKit::isActive())
171
0
        return comphelper::LibreOfficeKit::getLanguageTag();
172
173
35.1M
    return pImpl->aSysLocaleOptions.GetRealUILanguageTag();
174
35.1M
}
175
176
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */