Back to index

lightning-sunbird  0.9+nobinonly
nsMacCharset.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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 <Script.h>
00039 #include <TextCommon.h>
00040 #include "nsIPlatformCharset.h"
00041 #include "pratom.h"
00042 #include "nsGREResProperties.h"
00043 #include "nsUConvDll.h"
00044 #include "nsCOMPtr.h"
00045 #include "nsIComponentManager.h"
00046 #include "nsIMacLocale.h"
00047 #include "nsLocaleCID.h"
00048 #include "nsReadableUtils.h"
00049 #include "nsPlatformCharset.h"
00050 #include "nsEncoderDecoderUtils.h"
00051 
00052 static nsGREResProperties *gInfo = nsnull;
00053 static PRInt32 gCnt = 0;
00054 
00055 NS_IMPL_ISUPPORTS1(nsPlatformCharset, nsIPlatformCharset)
00056 
00057 nsPlatformCharset::nsPlatformCharset()
00058 {
00059   PR_AtomicIncrement(&gCnt);
00060 }
00061 nsPlatformCharset::~nsPlatformCharset()
00062 {
00063   PR_AtomicDecrement(&gCnt);
00064   if((0 == gCnt) && (nsnull != gInfo)) {
00065        delete gInfo;
00066        gInfo = nsnull;
00067   }
00068 }
00069 
00070 nsresult nsPlatformCharset::InitInfo()
00071 {  
00072   // load the .property file if necessary
00073   if (gInfo == nsnull) {
00074     nsGREResProperties *info =
00075         new nsGREResProperties(NS_LITERAL_CSTRING("maccharset.properties"));
00076     NS_ASSERTION(info , "cannot open properties file");
00077     NS_ENSURE_TRUE(info, NS_ERROR_FAILURE);
00078     gInfo = info;
00079   }
00080 
00081   return NS_OK;
00082 }
00083 
00084 nsresult nsPlatformCharset::MapToCharset(short script, short region, nsACString& outCharset)
00085 {
00086   switch (region) {
00087     case verUS:
00088     case verFrance:
00089     case verGermany:
00090       outCharset.AssignLiteral("x-mac-roman");
00091       return NS_OK;
00092     case verJapan:
00093       outCharset.AssignLiteral("Shift_JIS");
00094       return NS_OK;
00095   }
00096 
00097   // ensure the .property file is loaded
00098   nsresult rv = InitInfo();
00099   NS_ENSURE_SUCCESS(rv, rv);
00100 
00101   // try mapping from region then from script
00102   nsAutoString key(NS_LITERAL_STRING("region."));
00103   key.AppendInt(region, 10);
00104 
00105   nsAutoString uCharset;
00106   rv = gInfo->Get(key, uCharset);
00107   if (NS_SUCCEEDED(rv))
00108     CopyUCS2toASCII(uCharset, outCharset);
00109   else {
00110     key.AssignLiteral("script.");
00111     key.AppendInt(script, 10);
00112     rv = gInfo->Get(key, uCharset);
00113     // not found in the .property file, assign x-mac-roman
00114     if (NS_SUCCEEDED(rv))
00115       CopyUCS2toASCII(uCharset, outCharset);
00116     else {
00117       outCharset.AssignLiteral("x-mac-roman");
00118     }
00119   }
00120   
00121   return NS_OK;
00122 }
00123 
00124 NS_IMETHODIMP 
00125 nsPlatformCharset::GetCharset(nsPlatformCharsetSel selector, nsACString& oResult)
00126 {
00127   nsresult rv;
00128   if (mCharset.IsEmpty()) {
00129     rv = MapToCharset(
00130            (short)(0x0000FFFF & ::GetScriptManagerVariable(smSysScript)),
00131            (short)(0x0000FFFF & ::GetScriptManagerVariable(smRegionCode)),
00132            mCharset);
00133     NS_ENSURE_SUCCESS(rv, rv);
00134   }
00135 
00136   switch (selector) {
00137 #ifdef XP_MACOSX  
00138     case kPlatformCharsetSel_FileName:
00139       oResult.AssignLiteral("UTF-8");
00140       break;
00141 #endif
00142     case  kPlatformCharsetSel_KeyboardInput:
00143       rv = MapToCharset(
00144              (short) (0x0000FFFF & ::GetScriptManagerVariable(smKeyScript)),
00145              kTextRegionDontCare, oResult);
00146       NS_ENSURE_SUCCESS(rv, rv);
00147       break;
00148     default:
00149       oResult = mCharset;
00150       break;
00151   }
00152 
00153    return NS_OK;
00154 }
00155 
00156 NS_IMETHODIMP 
00157 nsPlatformCharset::GetDefaultCharsetForLocale(const nsAString& localeName, nsACString &oResult)
00158 {
00159   nsresult rv;
00160   nsCOMPtr<nsIMacLocale> pMacLocale;
00161   pMacLocale = do_CreateInstance(NS_MACLOCALE_CONTRACTID, &rv);
00162   if (NS_SUCCEEDED(rv)) {
00163     short script, language, region;
00164     rv = pMacLocale->GetPlatformLocale(localeName, &script, &language, &region);
00165     if (NS_SUCCEEDED(rv)) {
00166       if (NS_SUCCEEDED(MapToCharset(script, region, oResult))) {
00167         return NS_OK;
00168       }
00169     }
00170   }
00171 
00172   // fallback 
00173   oResult.AssignLiteral("x-mac-roman");
00174   return NS_SUCCESS_USING_FALLBACK_LOCALE;
00175 }
00176 
00177 NS_IMETHODIMP 
00178 nsPlatformCharset::Init()
00179 {
00180   return NS_OK;
00181 }
00182 
00183 nsresult 
00184 nsPlatformCharset::MapToCharset(nsAString& inANSICodePage, nsACString& outCharset)
00185 {
00186   return NS_OK;
00187 }
00188 
00189 nsresult
00190 nsPlatformCharset::InitGetCharset(nsACString &oString)
00191 {
00192   return NS_OK;
00193 }
00194 
00195 nsresult
00196 nsPlatformCharset::ConvertLocaleToCharsetUsingDeprecatedConfig(nsAString& locale, nsACString& oResult)
00197 {
00198   return NS_OK;
00199 }
00200 
00201 nsresult
00202 nsPlatformCharset::VerifyCharset(nsCString &aCharset)
00203 {
00204   return NS_OK;
00205 }