Back to index

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