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