/src/libreoffice/shell/source/backends/localebe/localebackend.cxx
Line | Count | Source |
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 | | #include <sal/log.hxx> |
22 | | |
23 | | #include <cassert> |
24 | | #include <limits> |
25 | | |
26 | | #include "localebackend.hxx" |
27 | | #include <com/sun/star/beans/Optional.hpp> |
28 | | #include <com/sun/star/uno/XComponentContext.hpp> |
29 | | #include <cppuhelper/supportsservice.hxx> |
30 | | #include <rtl/character.hxx> |
31 | | #include <o3tl/char16_t2wchar_t.hxx> |
32 | | #include <i18nlangtag/languagetag.hxx> |
33 | | #include <i18nlangtag/mslangid.hxx> |
34 | | |
35 | | #ifdef _WIN32 |
36 | | #if !defined WIN32_LEAN_AND_MEAN |
37 | | # define WIN32_LEAN_AND_MEAN |
38 | | #endif |
39 | | #include <windows.h> |
40 | | |
41 | | static css::beans::Optional<css::uno::Any> ImplGetLocale(LCID lcid) |
42 | | { |
43 | | WCHAR buffer[8]; |
44 | | PWSTR cp = buffer; |
45 | | |
46 | | cp += GetLocaleInfoW( lcid, LOCALE_SISO639LANGNAME, buffer, 4 ); |
47 | | if( cp > buffer ) |
48 | | { |
49 | | if( 0 < GetLocaleInfoW( lcid, LOCALE_SISO3166CTRYNAME, cp, buffer + 8 - cp) ) |
50 | | // #i50822# minus character must be written before cp |
51 | | *(cp - 1) = '-'; |
52 | | |
53 | | return {true, css::uno::Any(OUString(o3tl::toU(buffer)))}; |
54 | | } |
55 | | |
56 | | return {false, {}}; |
57 | | } |
58 | | |
59 | | #elif defined(MACOSX) |
60 | | |
61 | | #include <rtl/ustrbuf.hxx> |
62 | | #include <locale.h> |
63 | | #include <string.h> |
64 | | |
65 | | #include <premac.h> |
66 | | #include <CoreServices/CoreServices.h> |
67 | | #include <CoreFoundation/CoreFoundation.h> |
68 | | #include <postmac.h> |
69 | | |
70 | | namespace /* private */ |
71 | | { |
72 | | |
73 | | void OUStringBufferAppendCFString(OUStringBuffer& buffer, const CFStringRef s) |
74 | | { |
75 | | CFIndex lstr = CFStringGetLength(s); |
76 | | for (CFIndex i = 0; i < lstr; i++) |
77 | | buffer.append(sal_Unicode(CFStringGetCharacterAtIndex(s, i))); |
78 | | } |
79 | | |
80 | | template <typename T> |
81 | | class CFGuard |
82 | | { |
83 | | public: |
84 | | explicit CFGuard(T& rT) : rT_(rT) {} |
85 | | ~CFGuard() { if (rT_) CFRelease(rT_); } |
86 | | private: |
87 | | T& rT_; |
88 | | }; |
89 | | |
90 | | typedef CFGuard<CFArrayRef> CFArrayGuard; |
91 | | typedef CFGuard<CFStringRef> CFStringGuard; |
92 | | typedef CFGuard<CFTypeRef> CFTypeRefGuard; |
93 | | |
94 | | /* For more information on the Apple locale concept please refer to |
95 | | http://developer.apple.com/documentation/CoreFoundation/Conceptual/CFLocales/Articles/CFLocaleConcepts.html |
96 | | According to this documentation a locale identifier has the format: language[_country][_variant]* |
97 | | e.g. es_ES_PREEURO -> spain prior Euro support |
98 | | Note: The calling code should be able to handle locales with only language information e.g. 'en' for certain |
99 | | UI languages just the language code will be returned. |
100 | | */ |
101 | | |
102 | | CFStringRef ImplGetAppPreference(const char* pref) |
103 | | { |
104 | | CFStringRef csPref = CFStringCreateWithCString(nullptr, pref, kCFStringEncodingASCII); |
105 | | CFStringGuard csRefGuard(csPref); |
106 | | |
107 | | CFTypeRef ref = CFPreferencesCopyAppValue(csPref, kCFPreferencesCurrentApplication); |
108 | | CFTypeRefGuard refGuard(ref); |
109 | | |
110 | | if (ref == nullptr) |
111 | | return nullptr; |
112 | | |
113 | | CFStringRef sref = (CFGetTypeID(ref) == CFArrayGetTypeID()) ? static_cast<CFStringRef>(CFArrayGetValueAtIndex(static_cast<CFArrayRef>(ref), 0)) : static_cast<CFStringRef>(ref); |
114 | | |
115 | | // NOTE: this API is only available with macOS >=10.3. We need to use it because |
116 | | // Apple used non-ISO values on systems <10.2 like "German" for instance but didn't |
117 | | // upgrade those values during upgrade to newer macOS versions. See also #i54337# |
118 | | return CFLocaleCreateCanonicalLocaleIdentifierFromString(kCFAllocatorDefault, sref); |
119 | | } |
120 | | |
121 | | css::beans::Optional<css::uno::Any> ImplGetLocale(const char* pref) |
122 | | { |
123 | | CFStringRef sref = ImplGetAppPreference(pref); |
124 | | CFStringGuard srefGuard(sref); |
125 | | |
126 | | OUStringBuffer aLocaleBuffer("en-US"); // initialize with fallback value |
127 | | |
128 | | if (sref != nullptr) |
129 | | { |
130 | | // split the string into substrings; the first two (if there are two) substrings |
131 | | // are language and country |
132 | | CFArrayRef subs = CFStringCreateArrayBySeparatingStrings(nullptr, sref, CFSTR("_")); |
133 | | CFArrayGuard subsGuard(subs); |
134 | | |
135 | | if (subs != nullptr) |
136 | | { |
137 | | aLocaleBuffer.setLength(0); // clear buffer which still contains fallback value |
138 | | |
139 | | CFStringRef lang = static_cast<CFStringRef>(CFArrayGetValueAtIndex(subs, 0)); |
140 | | OUStringBufferAppendCFString(aLocaleBuffer, lang); |
141 | | |
142 | | // country also available? Assumption: if the array contains more than one |
143 | | // value the second value is always the country! |
144 | | if (CFArrayGetCount(subs) > 1) |
145 | | { |
146 | | aLocaleBuffer.append("-"); |
147 | | CFStringRef country = static_cast<CFStringRef>(CFArrayGetValueAtIndex(subs, 1)); |
148 | | OUStringBufferAppendCFString(aLocaleBuffer, country); |
149 | | } |
150 | | } |
151 | | } |
152 | | return {true, css::uno::Any(aLocaleBuffer.makeStringAndClear())}; |
153 | | } |
154 | | |
155 | | } // namespace /* private */ |
156 | | |
157 | | #else |
158 | | |
159 | | #include <rtl/ustrbuf.hxx> |
160 | | #include <cstdlib> |
161 | | #include <cstring> |
162 | | |
163 | | static css::beans::Optional<css::uno::Any> ImplGetLocale(char const * category) |
164 | 0 | { |
165 | 0 | const char *locale = std::getenv("LC_ALL"); |
166 | 0 | if (locale == nullptr || *locale == '\0') { |
167 | 0 | locale = std::getenv(category); |
168 | 0 | if (locale == nullptr || *locale == '\0') { |
169 | 0 | locale = std::getenv("LANG"); |
170 | 0 | } |
171 | 0 | } |
172 | | |
173 | | // Return "en-US" for C locales |
174 | 0 | if( (locale == nullptr) || *locale == '\0' || std::strcmp(locale, "C") == 0 |
175 | 0 | || std::strcmp(locale, "POSIX") == 0 ) |
176 | 0 | return {true, css::uno::Any(u"en-US"_ustr)}; |
177 | | |
178 | | |
179 | 0 | const char *cp; |
180 | 0 | const char *uscore = nullptr; |
181 | 0 | const char *end = nullptr; |
182 | | |
183 | | // locale string have the format lang[_ctry][.encoding][@modifier] |
184 | | // Let LanguageTag handle all conversion, but do a sanity and length check |
185 | | // first. |
186 | | // For the fallback we are only interested in the first two items, so we |
187 | | // handle '.' and '@' as string end for that. |
188 | 0 | for (cp = locale; *cp; cp++) |
189 | 0 | { |
190 | 0 | if (*cp == '_' && !uscore) |
191 | 0 | uscore = cp; |
192 | 0 | if ((*cp == '.' || *cp == '@') && !end) |
193 | 0 | end = cp; |
194 | 0 | if (!rtl::isAscii(static_cast<unsigned char>(*cp))) { |
195 | 0 | SAL_INFO("shell", "locale env var with non-ASCII content"); |
196 | 0 | return {false, {}}; |
197 | 0 | } |
198 | 0 | } |
199 | 0 | assert(cp >= locale); |
200 | 0 | if (cp - locale > std::numeric_limits<sal_Int32>::max()) { |
201 | 0 | SAL_INFO("shell", "locale env var content too long"); |
202 | 0 | return {false, {}}; |
203 | 0 | } |
204 | | |
205 | | // This is a tad awkward... but the easiest way to obtain what we're |
206 | | // actually interested in. For example this also converts |
207 | | // "ca_ES.UTF-8@valencia" to "ca-ES-valencia". |
208 | 0 | const OString aLocaleStr(locale); |
209 | 0 | const LanguageType nLang = MsLangId::convertUnxByteStringToLanguage( aLocaleStr); |
210 | 0 | if (nLang != LANGUAGE_DONTKNOW) |
211 | 0 | { |
212 | 0 | const OUString aLangTagStr( LanguageTag::convertToBcp47( nLang)); |
213 | 0 | return {true, css::uno::Any(aLangTagStr)}; |
214 | 0 | } |
215 | | |
216 | | // As a fallback, strip encoding and modifier and return just a |
217 | | // language-country combination and let the caller handle unknowns. |
218 | 0 | OUStringBuffer aLocaleBuffer; |
219 | 0 | if (!end) |
220 | 0 | end = cp; |
221 | 0 | if( uscore != nullptr ) |
222 | 0 | { |
223 | 0 | aLocaleBuffer.appendAscii(locale, uscore++ - locale); |
224 | 0 | aLocaleBuffer.append("-"); |
225 | 0 | aLocaleBuffer.appendAscii(uscore, end - uscore); |
226 | 0 | } |
227 | 0 | else |
228 | 0 | { |
229 | 0 | aLocaleBuffer.appendAscii(locale, end - locale); |
230 | 0 | } |
231 | |
|
232 | 0 | return {true, css::uno::Any(aLocaleBuffer.makeStringAndClear())}; |
233 | 0 | } |
234 | | |
235 | | #endif |
236 | | |
237 | | |
238 | | LocaleBackend::LocaleBackend() |
239 | 0 | { |
240 | 0 | } |
241 | | |
242 | | |
243 | | LocaleBackend::~LocaleBackend() |
244 | 0 | { |
245 | 0 | } |
246 | | |
247 | | |
248 | | |
249 | | css::beans::Optional<css::uno::Any> LocaleBackend::getLocale() |
250 | 0 | { |
251 | | #if defined(_WIN32) |
252 | | return ImplGetLocale( GetUserDefaultLCID() ); |
253 | | #elif defined (MACOSX) |
254 | | return ImplGetLocale("AppleLocale"); |
255 | | #else |
256 | 0 | return ImplGetLocale("LC_CTYPE"); |
257 | 0 | #endif |
258 | 0 | } |
259 | | |
260 | | |
261 | | css::beans::Optional<css::uno::Any> LocaleBackend::getUILocale() |
262 | 0 | { |
263 | | #if defined(_WIN32) |
264 | | return ImplGetLocale( MAKELCID(GetUserDefaultUILanguage(), SORT_DEFAULT) ); |
265 | | #elif defined(MACOSX) |
266 | | return ImplGetLocale("AppleLanguages"); |
267 | | #else |
268 | 0 | return ImplGetLocale("LC_MESSAGES"); |
269 | 0 | #endif |
270 | 0 | } |
271 | | |
272 | | |
273 | | css::beans::Optional<css::uno::Any> LocaleBackend::getSystemLocale() |
274 | 0 | { |
275 | | // note: the implementation differs from getLocale() only on Windows |
276 | | #if defined(_WIN32) |
277 | | return ImplGetLocale( GetSystemDefaultLCID() ); |
278 | | #else |
279 | 0 | return getLocale(); |
280 | 0 | #endif |
281 | 0 | } |
282 | | |
283 | | |
284 | | void LocaleBackend::setPropertyValue( |
285 | | OUString const &, css::uno::Any const &) |
286 | 0 | { |
287 | 0 | throw css::lang::IllegalArgumentException( |
288 | 0 | u"setPropertyValue not supported"_ustr, |
289 | 0 | getXWeak(), -1); |
290 | 0 | } |
291 | | |
292 | | css::uno::Any LocaleBackend::getPropertyValue( |
293 | | OUString const & PropertyName) |
294 | 0 | { |
295 | 0 | if ( PropertyName == "Locale" ) { |
296 | 0 | return css::uno::Any(getLocale()); |
297 | 0 | } else if (PropertyName == "SystemLocale") |
298 | 0 | { |
299 | 0 | return css::uno::Any(getSystemLocale()); |
300 | 0 | } else if (PropertyName == "UILocale") |
301 | 0 | { |
302 | 0 | return css::uno::Any(getUILocale()); |
303 | 0 | } else { |
304 | 0 | throw css::beans::UnknownPropertyException( |
305 | 0 | PropertyName, getXWeak()); |
306 | 0 | } |
307 | 0 | } |
308 | | |
309 | | |
310 | | OUString SAL_CALL LocaleBackend::getImplementationName() |
311 | 0 | { |
312 | 0 | return u"com.sun.star.comp.configuration.backend.LocaleBackend"_ustr ; |
313 | 0 | } |
314 | | |
315 | | sal_Bool SAL_CALL LocaleBackend::supportsService(const OUString& aServiceName) |
316 | 0 | { |
317 | 0 | return cppu::supportsService(this, aServiceName); |
318 | 0 | } |
319 | | |
320 | | uno::Sequence<OUString> SAL_CALL LocaleBackend::getSupportedServiceNames() |
321 | 0 | { |
322 | 0 | return { u"com.sun.star.configuration.backend.LocaleBackend"_ustr }; |
323 | 0 | } |
324 | | |
325 | | extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface* |
326 | | shell_LocaleBackend_get_implementation( |
327 | | css::uno::XComponentContext* , css::uno::Sequence<css::uno::Any> const&) |
328 | 0 | { |
329 | 0 | return cppu::acquire(new LocaleBackend()); |
330 | 0 | } |
331 | | |
332 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |