update dev300-m58
[ooovba.git] / sal / osl / unx / osxlocale.cxx
blobc51fc6fa6e61d09795f85866dd848bf53674f47a
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: osxlocale.cxx,v $
10 * $Revision: 1.4 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sal.hxx"
34 #include <sal/types.h>
35 #include <assert.h>
37 #include <premac.h>
38 #include <CoreServices/CoreServices.h>
39 #include <CoreFoundation/CoreFoundation.h>
40 #include <postmac.h>
42 namespace /* private */
44 template <typename T>
45 class CFGuard
47 public:
48 explicit CFGuard(T& rT) : rT_(rT) {}
49 ~CFGuard() { if (rT_) CFRelease(rT_); }
50 private:
51 T& rT_;
54 typedef CFGuard<CFArrayRef> CFArrayGuard;
55 typedef CFGuard<CFStringRef> CFStringGuard;
56 typedef CFGuard<CFPropertyListRef> CFPropertyListGuard;
58 /** Get the current process locale from system
60 CFStringRef getProcessLocale()
62 CFPropertyListRef pref = CFPreferencesCopyAppValue(CFSTR("AppleLocale"), kCFPreferencesCurrentApplication);
63 CFPropertyListGuard proplGuard(pref);
65 if (pref == NULL) // return fallback value 'en_US'
66 return CFStringCreateWithCString(kCFAllocatorDefault, "en_US", kCFStringEncodingASCII);
68 CFStringRef sref = (CFGetTypeID(pref) == CFArrayGetTypeID()) ? (CFStringRef)CFArrayGetValueAtIndex((CFArrayRef)pref, 0) : (CFStringRef)pref;
70 // NOTE: this API is only available with Mac OS X >=10.3. We need to use it because
71 // Apple used non-ISO values on systems <10.2 like "German" for instance but didn't
72 // upgrade those values during upgrade to newer Mac OS X versions. See also #i54337#
73 return CFLocaleCreateCanonicalLocaleIdentifierFromString(kCFAllocatorDefault, sref);
75 } // namespace private
77 /** Grab current locale from system.
79 extern "C" {
80 int macosx_getLocale(char *locale, sal_uInt32 bufferLen)
82 CFStringRef sref = getProcessLocale();
83 CFStringGuard sGuard(sref);
85 assert(sref != NULL && "osxlocale.cxx: getProcessLocale must return a non-NULL value");
87 // split the string into substrings; the first two (if there are two) substrings
88 // are language and country
89 CFArrayRef subs = CFStringCreateArrayBySeparatingStrings(NULL, sref, CFSTR("_"));
90 CFArrayGuard arrGuard(subs);
92 CFStringRef lang = (CFStringRef)CFArrayGetValueAtIndex(subs, 0);
93 CFStringGetCString(lang, locale, bufferLen, kCFStringEncodingASCII);
95 // country also available? Assumption: if the array contains more than one
96 // value the second value is always the country!
97 if (CFArrayGetCount(subs) > 1)
99 strlcat(locale, "_", bufferLen - strlen(locale));
101 CFStringRef country = (CFStringRef)CFArrayGetValueAtIndex(subs, 1);
102 CFStringGetCString(country, locale + strlen(locale), bufferLen - strlen(locale), kCFStringEncodingASCII);
104 // Append 'UTF-8' to the locale because the Mac OS X file
105 // system interface is UTF-8 based and sal tries to determine
106 // the file system locale from the locale information
107 strlcat(locale, ".UTF-8", bufferLen - strlen(locale));
109 return noErr;
116 * macxp_OSXConvertCFEncodingToIANACharSetName
118 * Convert a CoreFoundation text encoding to an IANA charset name.
120 extern "C" int macxp_OSXConvertCFEncodingToIANACharSetName( char *buffer, unsigned int bufferLen, CFStringEncoding cfEncoding )
122 CFStringRef sCFEncodingName;
124 sCFEncodingName = CFStringConvertEncodingToIANACharSetName( cfEncoding );
125 CFStringGetCString( sCFEncodingName, buffer, bufferLen, cfEncoding );
127 if ( sCFEncodingName )
128 CFRelease( sCFEncodingName );
130 return( noErr );