|
1 |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #include "mozilla/ArrayUtils.h" |
|
7 |
|
8 #include "nsIPlatformCharset.h" |
|
9 #include "nsUConvPropertySearch.h" |
|
10 #include <windows.h> |
|
11 #include "nsWin32Locale.h" |
|
12 #include "nsCOMPtr.h" |
|
13 #include "nsReadableUtils.h" |
|
14 #include "nsServiceManagerUtils.h" |
|
15 #include "nsPlatformCharset.h" |
|
16 #include "nsEncoderDecoderUtils.h" |
|
17 |
|
18 using namespace mozilla; |
|
19 |
|
20 static const char* kWinCharsets[][3] = { |
|
21 #include "wincharset.properties.h" |
|
22 }; |
|
23 |
|
24 NS_IMPL_ISUPPORTS(nsPlatformCharset, nsIPlatformCharset) |
|
25 |
|
26 nsPlatformCharset::nsPlatformCharset() |
|
27 { |
|
28 nsAutoString acpKey(NS_LITERAL_STRING("acp.")); |
|
29 acpKey.AppendInt(int32_t(::GetACP() & 0x00FFFF), 10); |
|
30 MapToCharset(acpKey, mCharset); |
|
31 } |
|
32 |
|
33 nsPlatformCharset::~nsPlatformCharset() |
|
34 { |
|
35 } |
|
36 |
|
37 nsresult |
|
38 nsPlatformCharset::MapToCharset(nsAString& inANSICodePage, nsACString& outCharset) |
|
39 { |
|
40 nsAutoCString key; |
|
41 LossyCopyUTF16toASCII(inANSICodePage, key); |
|
42 |
|
43 nsresult rv = nsUConvPropertySearch::SearchPropertyValue(kWinCharsets, |
|
44 ArrayLength(kWinCharsets), key, outCharset); |
|
45 if (NS_FAILED(rv)) { |
|
46 outCharset.AssignLiteral("windows-1252"); |
|
47 return NS_SUCCESS_USING_FALLBACK_LOCALE; |
|
48 } |
|
49 return rv; |
|
50 } |
|
51 |
|
52 NS_IMETHODIMP |
|
53 nsPlatformCharset::GetCharset(nsPlatformCharsetSel selector, |
|
54 nsACString& oResult) |
|
55 { |
|
56 oResult = mCharset; |
|
57 return NS_OK; |
|
58 } |
|
59 |
|
60 NS_IMETHODIMP |
|
61 nsPlatformCharset::GetDefaultCharsetForLocale(const nsAString& localeName, nsACString& oResult) |
|
62 { |
|
63 LCID localeAsLCID; |
|
64 |
|
65 // |
|
66 // convert locale name to a code page (through the LCID) |
|
67 // |
|
68 nsresult rv; |
|
69 oResult.Truncate(); |
|
70 |
|
71 rv = nsWin32Locale::GetPlatformLocale(localeName, &localeAsLCID); |
|
72 if (NS_FAILED(rv)) { return rv; } |
|
73 |
|
74 wchar_t acp_name[6]; |
|
75 if (GetLocaleInfoW(localeAsLCID, LOCALE_IDEFAULTANSICODEPAGE, acp_name, |
|
76 ArrayLength(acp_name))==0) { |
|
77 return NS_ERROR_FAILURE; |
|
78 } |
|
79 nsAutoString acp_key(NS_LITERAL_STRING("acp.")); |
|
80 acp_key.Append(acp_name); |
|
81 |
|
82 return MapToCharset(acp_key, oResult); |
|
83 } |
|
84 |
|
85 NS_IMETHODIMP |
|
86 nsPlatformCharset::Init() |
|
87 { |
|
88 return NS_OK; |
|
89 } |
|
90 |
|
91 nsresult |
|
92 nsPlatformCharset::InitGetCharset(nsACString &oString) |
|
93 { |
|
94 return NS_OK; |
|
95 } |
|
96 |
|
97 nsresult |
|
98 nsPlatformCharset::VerifyCharset(nsCString &aCharset) |
|
99 { |
|
100 return NS_OK; |
|
101 } |