Back to index

lightning-sunbird  0.9+nobinonly
nsWinCharset.cpp
Go to the documentation of this file.
00001 
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Mozilla Communicator client code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "nsIPlatformCharset.h"
00039 #include "nsGREResProperties.h"
00040 #include "pratom.h"
00041 #include <windows.h>
00042 #include "nsUConvDll.h"
00043 #include "nsIWin32Locale.h"
00044 #include "nsCOMPtr.h"
00045 #include "nsReadableUtils.h"
00046 #include "nsLocaleCID.h"
00047 #include "nsServiceManagerUtils.h"
00048 #include "nsITimelineService.h"
00049 #include "nsPlatformCharset.h"
00050 
00051 static nsGREResProperties *gInfo = nsnull;
00052 static PRInt32 gCnt= 0;
00053 
00054 NS_IMPL_ISUPPORTS1(nsPlatformCharset, nsIPlatformCharset)
00055 
00056 nsPlatformCharset::nsPlatformCharset()
00057 {
00058   NS_TIMELINE_START_TIMER("nsPlatformCharset()");
00059 
00060   nsAutoString acpKey(NS_LITERAL_STRING("acp."));
00061   acpKey.AppendInt(PRInt32(::GetACP() & 0x00FFFF), 10);
00062   MapToCharset(acpKey, mCharset);
00063 
00064   NS_TIMELINE_STOP_TIMER("nsPlatformCharset()");
00065   NS_TIMELINE_MARK_TIMER("nsPlatformCharset()");
00066 }
00067 
00068 nsPlatformCharset::~nsPlatformCharset()
00069 {
00070   PR_AtomicDecrement(&gCnt);
00071   if ((0 == gCnt) && (nsnull != gInfo)) {
00072     delete gInfo;
00073     gInfo = nsnull;
00074   }
00075 }
00076 
00077 nsresult 
00078 nsPlatformCharset::InitInfo()
00079 {  
00080   PR_AtomicIncrement(&gCnt); // count for gInfo
00081 
00082   if (gInfo == nsnull) {
00083     nsGREResProperties *info = new nsGREResProperties(NS_LITERAL_CSTRING("wincharset.properties"));
00084 
00085     NS_ASSERTION(info , "cannot open properties file");
00086     NS_ENSURE_TRUE(info, NS_ERROR_FAILURE);
00087     gInfo = info;
00088   }
00089   return NS_OK;
00090 }
00091 
00092 nsresult
00093 nsPlatformCharset::MapToCharset(nsAString& inANSICodePage, nsACString& outCharset)
00094 {
00095   //delay loading wincharset.properties bundle if possible
00096   if (inANSICodePage.EqualsLiteral("acp.1252")) {
00097     outCharset.AssignLiteral("windows-1252");
00098     return NS_OK;
00099   } 
00100 
00101   if (inANSICodePage.EqualsLiteral("acp.932")) {
00102     outCharset.AssignLiteral("Shift_JIS");
00103     return NS_OK;
00104   } 
00105 
00106   // ensure the .property file is loaded
00107   nsresult rv = InitInfo();
00108   if (NS_FAILED(rv)) {
00109     outCharset.AssignLiteral("windows-1252");
00110     return rv;
00111   }
00112 
00113   nsAutoString charset;
00114   rv = gInfo->Get(inANSICodePage, charset);
00115   if (NS_FAILED(rv)) {
00116     outCharset.AssignLiteral("windows-1252");
00117     return rv;
00118   }
00119 
00120   CopyUCS2toASCII(charset, outCharset);
00121   return NS_OK;
00122 }
00123 
00124 NS_IMETHODIMP 
00125 nsPlatformCharset::GetCharset(nsPlatformCharsetSel selector,
00126                               nsACString& oResult)
00127 {
00128   oResult = mCharset;
00129   return NS_OK;
00130 }
00131 
00132 NS_IMETHODIMP
00133 nsPlatformCharset::GetDefaultCharsetForLocale(const nsAString& localeName, nsACString& oResult)
00134 {
00135   nsCOMPtr<nsIWin32Locale>  winLocale;
00136   LCID                                    localeAsLCID;
00137   char                                    acp_name[6];
00138 
00139   //
00140   // convert locale name to a code page (through the LCID)
00141   //
00142   nsresult rv;
00143   oResult.Truncate();
00144 
00145   winLocale = do_GetService(NS_WIN32LOCALE_CONTRACTID, &rv);
00146   if (NS_FAILED(rv)) { return rv; }
00147 
00148   rv = winLocale->GetPlatformLocale(localeName, &localeAsLCID);
00149   if (NS_FAILED(rv)) { return rv; }
00150 
00151   if (GetLocaleInfo(localeAsLCID, LOCALE_IDEFAULTANSICODEPAGE, acp_name, sizeof(acp_name))==0) { 
00152     return NS_ERROR_FAILURE; 
00153   }
00154   nsAutoString acp_key(NS_LITERAL_STRING("acp."));
00155   acp_key.AppendWithConversion(acp_name);
00156 
00157   return MapToCharset(acp_key, oResult);
00158 }
00159 
00160 NS_IMETHODIMP 
00161 nsPlatformCharset::Init()
00162 {
00163   return NS_OK;
00164 }
00165 
00166 nsresult 
00167 nsPlatformCharset::MapToCharset(short script, short region, nsACString& outCharset)
00168 {
00169   return NS_OK;
00170 }
00171 
00172 nsresult
00173 nsPlatformCharset::InitGetCharset(nsACString &oString)
00174 {
00175   return NS_OK;
00176 }
00177 
00178 nsresult
00179 nsPlatformCharset::ConvertLocaleToCharsetUsingDeprecatedConfig(nsAString& locale, nsACString& oResult)
00180 {
00181   return NS_OK;
00182 }
00183 
00184 nsresult
00185 nsPlatformCharset::VerifyCharset(nsCString &aCharset)
00186 {
00187   return NS_OK;
00188 }