Line data Source code
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 :
21 : #include "localebackend.hxx"
22 : #include <com/sun/star/beans/Optional.hpp>
23 : #include <osl/time.h>
24 :
25 : #include <stdio.h>
26 :
27 : #ifdef WNT
28 :
29 : #ifdef WINVER
30 : #undef WINVER
31 : #endif
32 : #define WINVER 0x0501
33 :
34 : #if defined _MSC_VER
35 : #pragma warning(push, 1)
36 : #endif
37 : #include <windows.h>
38 : #if defined _MSC_VER
39 : #pragma warning(pop)
40 : #endif
41 :
42 : OUString ImplGetLocale(LCID lcid)
43 : {
44 : TCHAR buffer[8];
45 : LPTSTR cp = buffer;
46 :
47 : cp += GetLocaleInfo( lcid, LOCALE_SISO639LANGNAME , buffer, 4 );
48 : if( cp > buffer )
49 : {
50 : if( 0 < GetLocaleInfo( lcid, LOCALE_SISO3166CTRYNAME, cp, buffer + 8 - cp) )
51 : // #i50822# minus character must be written before cp
52 : *(cp - 1) = '-';
53 :
54 : return OUString::createFromAscii(buffer);
55 : }
56 :
57 : return OUString();
58 : }
59 :
60 : #elif defined(MACOSX)
61 :
62 : #include <rtl/ustrbuf.hxx>
63 : #include <locale.h>
64 : #include <string.h>
65 :
66 : #include <premac.h>
67 : #include <CoreServices/CoreServices.h>
68 : #include <CoreFoundation/CoreFoundation.h>
69 : #include <postmac.h>
70 :
71 : namespace /* private */
72 : {
73 :
74 : void OUStringBufferAppendCFString(OUStringBuffer& buffer, const CFStringRef s)
75 : {
76 : CFIndex lstr = CFStringGetLength(s);
77 : for (CFIndex i = 0; i < lstr; i++)
78 : buffer.append(CFStringGetCharacterAtIndex(s, i));
79 : }
80 :
81 : template <typename T>
82 : class CFGuard
83 : {
84 : public:
85 : explicit CFGuard(T& rT) : rT_(rT) {}
86 : ~CFGuard() { if (rT_) CFRelease(rT_); }
87 : private:
88 : T& rT_;
89 : };
90 :
91 : typedef CFGuard<CFArrayRef> CFArrayGuard;
92 : typedef CFGuard<CFStringRef> CFStringGuard;
93 : typedef CFGuard<CFTypeRef> CFTypeRefGuard;
94 :
95 : /* For more information on the Apple locale concept please refer to
96 : http://developer.apple.com/documentation/CoreFoundation/Conceptual/CFLocales/Articles/CFLocaleConcepts.html
97 : According to this documentation a locale identifier has the format: language[_country][_variant]*
98 : e.g. es_ES_PREEURO -> spain prior Euro support
99 : Note: The calling code should be able to handle locales with only language information e.g. 'en' for certain
100 : UI languages just the language code will be returned.
101 : */
102 :
103 : CFStringRef ImplGetAppPreference(const char* pref)
104 : {
105 : CFStringRef csPref = CFStringCreateWithCString(NULL, pref, kCFStringEncodingASCII);
106 : CFStringGuard csRefGuard(csPref);
107 :
108 : CFTypeRef ref = CFPreferencesCopyAppValue(csPref, kCFPreferencesCurrentApplication);
109 : CFTypeRefGuard refGuard(ref);
110 :
111 : if (ref == NULL)
112 : return NULL;
113 :
114 : CFStringRef sref = (CFGetTypeID(ref) == CFArrayGetTypeID()) ? (CFStringRef)CFArrayGetValueAtIndex((CFArrayRef)ref, 0) : (CFStringRef)ref;
115 :
116 : // NOTE: this API is only available with Mac OS X >=10.3. We need to use it because
117 : // Apple used non-ISO values on systems <10.2 like "German" for instance but didn't
118 : // upgrade those values during upgrade to newer Mac OS X versions. See also #i54337#
119 : return CFLocaleCreateCanonicalLocaleIdentifierFromString(kCFAllocatorDefault, sref);
120 : }
121 :
122 : OUString ImplGetLocale(const char* pref)
123 : {
124 : CFStringRef sref = ImplGetAppPreference(pref);
125 : CFStringGuard srefGuard(sref);
126 :
127 : OUStringBuffer aLocaleBuffer;
128 : aLocaleBuffer.appendAscii("en-US"); // initialize with fallback value
129 :
130 : if (sref != NULL)
131 : {
132 : // split the string into substrings; the first two (if there are two) substrings
133 : // are language and country
134 : CFArrayRef subs = CFStringCreateArrayBySeparatingStrings(NULL, sref, CFSTR("_"));
135 : CFArrayGuard subsGuard(subs);
136 :
137 : if (subs != NULL)
138 : {
139 : aLocaleBuffer.setLength(0); // clear buffer which still contains fallback value
140 :
141 : CFStringRef lang = (CFStringRef)CFArrayGetValueAtIndex(subs, 0);
142 : OUStringBufferAppendCFString(aLocaleBuffer, lang);
143 :
144 : // country also available? Assumption: if the array contains more than one
145 : // value the second value is always the country!
146 : if (CFArrayGetCount(subs) > 1)
147 : {
148 : aLocaleBuffer.appendAscii("-");
149 : CFStringRef country = (CFStringRef)CFArrayGetValueAtIndex(subs, 1);
150 : OUStringBufferAppendCFString(aLocaleBuffer, country);
151 : }
152 : }
153 : }
154 : return aLocaleBuffer.makeStringAndClear();
155 : }
156 :
157 : } // namespace /* private */
158 :
159 : #else
160 :
161 : #include <rtl/ustrbuf.hxx>
162 : #include <locale.h>
163 : #include <string.h>
164 :
165 : /*
166 : * Note: setlocale is not at all thread safe, so is this code. It could
167 : * especially interfere with the stuff VCL is doing, so make sure this
168 : * is called from the main thread only.
169 : */
170 :
171 250 : static OUString ImplGetLocale(int category)
172 : {
173 250 : const char *locale = setlocale(category, "");
174 :
175 : // Return "en-US" for C locales
176 250 : if( (locale == NULL) || ( locale[0] == 'C' && locale[1] == '\0' ) )
177 0 : return OUString( "en-US" );
178 :
179 :
180 : const char *cp;
181 250 : const char *uscore = NULL;
182 :
183 : // locale string have the format lang[_ctry][.encoding][@modifier]
184 : // we are only interested in the first two items, so we handle
185 : // '.' and '@' as string end.
186 1500 : for (cp = locale; *cp; cp++)
187 : {
188 1500 : if (*cp == '_')
189 250 : uscore = cp;
190 1500 : if (*cp == '.' || *cp == '@')
191 : break;
192 : }
193 :
194 250 : OUStringBuffer aLocaleBuffer;
195 250 : if( uscore != NULL )
196 : {
197 250 : aLocaleBuffer.appendAscii(locale, uscore++ - locale);
198 250 : aLocaleBuffer.appendAscii("-");
199 250 : aLocaleBuffer.appendAscii(uscore, cp - uscore);
200 : }
201 : else
202 : {
203 0 : aLocaleBuffer.appendAscii(locale, cp - locale);
204 : }
205 :
206 250 : return aLocaleBuffer.makeStringAndClear();
207 : }
208 :
209 : #endif
210 :
211 : // -------------------------------------------------------------------------------
212 :
213 84 : LocaleBackend::LocaleBackend()
214 : {
215 84 : }
216 :
217 : //------------------------------------------------------------------------------
218 :
219 0 : LocaleBackend::~LocaleBackend(void)
220 : {
221 0 : }
222 :
223 : //------------------------------------------------------------------------------
224 :
225 84 : LocaleBackend* LocaleBackend::createInstance()
226 : {
227 84 : return new LocaleBackend;
228 : }
229 :
230 : // ---------------------------------------------------------------------------------------
231 :
232 167 : OUString LocaleBackend::getLocale(void)
233 : {
234 : #if defined WNT
235 : return ImplGetLocale( GetUserDefaultLCID() );
236 : #elif defined (MACOSX)
237 : return ImplGetLocale("AppleLocale");
238 : #else
239 167 : return ImplGetLocale(LC_CTYPE);
240 : #endif
241 : }
242 :
243 : //------------------------------------------------------------------------------
244 :
245 83 : OUString LocaleBackend::getUILocale(void)
246 : {
247 : #if defined WNT
248 : return ImplGetLocale( MAKELCID(GetUserDefaultUILanguage(), SORT_DEFAULT) );
249 : #elif defined(MACOSX)
250 : return ImplGetLocale("AppleLanguages");
251 : #else
252 83 : return ImplGetLocale(LC_MESSAGES);
253 : #endif
254 : }
255 :
256 : // ---------------------------------------------------------------------------------------
257 :
258 84 : OUString LocaleBackend::getSystemLocale(void)
259 : {
260 : // note: the implementation differs from getLocale() only on Windows
261 : #if defined WNT
262 : return ImplGetLocale( GetSystemDefaultLCID() );
263 : #else
264 84 : return getLocale();
265 : #endif
266 : }
267 : //------------------------------------------------------------------------------
268 :
269 0 : void LocaleBackend::setPropertyValue(
270 : OUString const &, css::uno::Any const &)
271 : throw (
272 : css::beans::UnknownPropertyException, css::beans::PropertyVetoException,
273 : css::lang::IllegalArgumentException, css::lang::WrappedTargetException,
274 : css::uno::RuntimeException)
275 : {
276 : throw css::lang::IllegalArgumentException(
277 : OUString(
278 : "setPropertyValue not supported"),
279 0 : static_cast< cppu::OWeakObject * >(this), -1);
280 : }
281 :
282 250 : css::uno::Any LocaleBackend::getPropertyValue(
283 : OUString const & PropertyName)
284 : throw (
285 : css::beans::UnknownPropertyException, css::lang::WrappedTargetException,
286 : css::uno::RuntimeException)
287 : {
288 250 : if ( PropertyName == "Locale" ) {
289 : return css::uno::makeAny(
290 : css::beans::Optional< css::uno::Any >(
291 83 : true, css::uno::makeAny(getLocale())));
292 167 : } else if (PropertyName.equals("SystemLocale"))
293 : {
294 : return css::uno::makeAny(
295 : css::beans::Optional< css::uno::Any >(
296 84 : true, css::uno::makeAny(getSystemLocale())));
297 83 : } else if (PropertyName.equals("UILocale"))
298 : {
299 : return css::uno::makeAny(
300 : css::beans::Optional< css::uno::Any >(
301 83 : true, css::uno::makeAny(getUILocale())));
302 : } else {
303 : throw css::beans::UnknownPropertyException(
304 0 : PropertyName, static_cast< cppu::OWeakObject * >(this));
305 : }
306 : }
307 :
308 : //------------------------------------------------------------------------------
309 :
310 84 : OUString SAL_CALL LocaleBackend::getBackendName(void) {
311 84 : return OUString("com.sun.star.comp.configuration.backend.LocaleBackend") ;
312 : }
313 :
314 : //------------------------------------------------------------------------------
315 :
316 0 : OUString SAL_CALL LocaleBackend::getImplementationName(void)
317 : throw (uno::RuntimeException)
318 : {
319 0 : return getBackendName() ;
320 : }
321 :
322 : //------------------------------------------------------------------------------
323 :
324 84 : uno::Sequence<OUString> SAL_CALL LocaleBackend::getBackendServiceNames(void)
325 : {
326 84 : uno::Sequence<OUString> aServiceNameList(1);
327 84 : aServiceNameList[0] = OUString( "com.sun.star.configuration.backend.LocaleBackend") ;
328 84 : return aServiceNameList ;
329 : }
330 :
331 : //------------------------------------------------------------------------------
332 :
333 0 : sal_Bool SAL_CALL LocaleBackend::supportsService(const OUString& aServiceName)
334 : throw (uno::RuntimeException)
335 : {
336 0 : uno::Sequence< OUString > const svc = getBackendServiceNames();
337 :
338 0 : for(sal_Int32 i = 0; i < svc.getLength(); ++i )
339 0 : if(svc[i] == aServiceName)
340 0 : return true;
341 :
342 0 : return false;
343 : }
344 :
345 : //------------------------------------------------------------------------------
346 :
347 0 : uno::Sequence<OUString> SAL_CALL LocaleBackend::getSupportedServiceNames(void)
348 : throw (uno::RuntimeException)
349 : {
350 0 : return getBackendServiceNames() ;
351 : }
352 :
353 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|