2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is Mozilla Communicator client code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #include "nsIPlatformCharset.h"
39 #include "nsGREResProperties.h"
42 #include "nsUConvDll.h"
43 #include "nsIWin32Locale.h"
45 #include "nsReadableUtils.h"
46 #include "nsLocaleCID.h"
47 #include "nsServiceManagerUtils.h"
48 #include "nsITimelineService.h"
49 #include "nsPlatformCharset.h"
51 static nsGREResProperties
*gInfo
= nsnull
;
52 static PRInt32 gCnt
= 0;
54 NS_IMPL_ISUPPORTS1(nsPlatformCharset
, nsIPlatformCharset
)
56 nsPlatformCharset::nsPlatformCharset()
58 NS_TIMELINE_START_TIMER("nsPlatformCharset()");
60 PR_AtomicIncrement(&gCnt
);
61 nsAutoString
acpKey(NS_LITERAL_STRING("acp."));
62 acpKey
.AppendInt(PRInt32(::GetACP() & 0x00FFFF), 10);
63 MapToCharset(acpKey
, mCharset
);
65 NS_TIMELINE_STOP_TIMER("nsPlatformCharset()");
66 NS_TIMELINE_MARK_TIMER("nsPlatformCharset()");
69 nsPlatformCharset::~nsPlatformCharset()
71 PR_AtomicDecrement(&gCnt
);
72 if ((0 == gCnt
) && (nsnull
!= gInfo
)) {
79 nsPlatformCharset::InitInfo()
81 if (gInfo
== nsnull
) {
82 nsGREResProperties
*info
= new nsGREResProperties(NS_LITERAL_CSTRING("wincharset.properties"));
84 NS_ASSERTION(info
, "cannot open properties file");
85 NS_ENSURE_TRUE(info
, NS_ERROR_FAILURE
);
92 nsPlatformCharset::MapToCharset(nsAString
& inANSICodePage
, nsACString
& outCharset
)
94 //delay loading wincharset.properties bundle if possible
95 if (inANSICodePage
.EqualsLiteral("acp.1252")) {
96 outCharset
.AssignLiteral("windows-1252");
100 if (inANSICodePage
.EqualsLiteral("acp.932")) {
101 outCharset
.AssignLiteral("Shift_JIS");
105 // ensure the .property file is loaded
106 nsresult rv
= InitInfo();
108 outCharset
.AssignLiteral("windows-1252");
112 nsAutoString charset
;
113 rv
= gInfo
->Get(inANSICodePage
, charset
);
115 outCharset
.AssignLiteral("windows-1252");
119 LossyCopyUTF16toASCII(charset
, outCharset
);
124 nsPlatformCharset::GetCharset(nsPlatformCharsetSel selector
,
132 nsPlatformCharset::GetDefaultCharsetForLocale(const nsAString
& localeName
, nsACString
& oResult
)
134 nsCOMPtr
<nsIWin32Locale
> winLocale
;
138 // convert locale name to a code page (through the LCID)
143 winLocale
= do_GetService(NS_WIN32LOCALE_CONTRACTID
, &rv
);
144 if (NS_FAILED(rv
)) { return rv
; }
146 rv
= winLocale
->GetPlatformLocale(localeName
, &localeAsLCID
);
147 if (NS_FAILED(rv
)) { return rv
; }
149 PRUnichar acp_name
[6];
150 if (GetLocaleInfoW(localeAsLCID
, LOCALE_IDEFAULTANSICODEPAGE
, acp_name
,
151 NS_ARRAY_LENGTH(acp_name
))==0) {
152 return NS_ERROR_FAILURE
;
154 nsAutoString
acp_key(NS_LITERAL_STRING("acp."));
155 acp_key
.Append(acp_name
);
157 return MapToCharset(acp_key
, oResult
);
161 nsPlatformCharset::Init()
167 nsPlatformCharset::MapToCharset(short script
, short region
, nsACString
& outCharset
)
173 nsPlatformCharset::InitGetCharset(nsACString
&oString
)
179 nsPlatformCharset::ConvertLocaleToCharsetUsingDeprecatedConfig(nsAString
& locale
, nsACString
& oResult
)
185 nsPlatformCharset::VerifyCharset(nsCString
&aCharset
)