Back to index

lightning-sunbird  0.9+nobinonly
nsCharsetAliasImp.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 
00039 #include "nsICharsetAlias.h"
00040 #include "pratom.h"
00041 
00042 // for NS_IMPL_IDS only
00043 #include "nsIPlatformCharset.h"
00044 
00045 #include "nsUConvDll.h"
00046 #include "nsReadableUtils.h"
00047 #include "nsUnicharUtils.h"
00048 #include "nsGREResProperties.h"
00049 #include "nsITimelineService.h"
00050 #include "nsCharsetAlias.h"
00051 
00052 //--------------------------------------------------------------
00053 NS_IMPL_ISUPPORTS1(nsCharsetAlias2, nsICharsetAlias)
00054 
00055 //--------------------------------------------------------------
00056 nsCharsetAlias2::nsCharsetAlias2()
00057 {
00058   mDelegate = nsnull; // delay the load of mDelegate untill we need it.
00059 }
00060 //--------------------------------------------------------------
00061 nsCharsetAlias2::~nsCharsetAlias2()
00062 {
00063   if(mDelegate)
00064      delete mDelegate;
00065 }
00066 //--------------------------------------------------------------
00067 NS_IMETHODIMP nsCharsetAlias2::GetPreferred(const nsACString& aAlias,
00068                                             nsACString& oResult)
00069 {
00070    if (aAlias.IsEmpty()) return NS_ERROR_NULL_POINTER;
00071    NS_TIMELINE_START_TIMER("nsCharsetAlias2:GetPreferred");
00072 
00073    nsCAutoString aKey(aAlias);
00074    ToLowerCase(aKey);
00075    oResult.Truncate();
00076 
00077    // Delay loading charsetalias.properties by hardcoding the most
00078    // frequent aliases.  Note that it's possible to recur in to this
00079    // function *while loading* charsetalias.properties (see bug 190951),
00080    // so we might have an |mDelegate| already that isn't valid yet, but
00081    // the load is guaranteed to be "UTF-8" so things will be OK.
00082    if(aKey.EqualsLiteral("utf-8")) {
00083      oResult.AssignLiteral("UTF-8");
00084      NS_TIMELINE_STOP_TIMER("nsCharsetAlias2:GetPreferred");
00085      return NS_OK;
00086    } 
00087    if(aKey.EqualsLiteral("iso-8859-1")) {
00088      oResult.AssignLiteral("ISO-8859-1");
00089      NS_TIMELINE_STOP_TIMER("nsCharsetAlias2:GetPreferred");
00090      return NS_OK;
00091    } 
00092    if(aKey.EqualsLiteral("x-sjis") ||
00093       aKey.EqualsLiteral("shift_jis")) {
00094      oResult.AssignLiteral("Shift_JIS");
00095      NS_TIMELINE_STOP_TIMER("nsCharsetAlias2:GetPreferred");
00096      return NS_OK;
00097    } 
00098 
00099    if(!mDelegate) {
00100      //load charsetalias.properties string bundle with all remaining aliases
00101      // we may need to protect the following section with a lock so we won't call the 
00102      // 'new nsGREResProperties' from two different threads
00103      mDelegate = new nsGREResProperties( NS_LITERAL_CSTRING("charsetalias.properties") );
00104      NS_ASSERTION(mDelegate, "cannot create nsGREResProperties");
00105      if(nsnull == mDelegate)
00106        return NS_ERROR_OUT_OF_MEMORY;
00107    }
00108 
00109    NS_TIMELINE_STOP_TIMER("nsCharsetAlias2:GetPreferred");
00110    NS_TIMELINE_MARK_TIMER("nsCharsetAlias2:GetPreferred");
00111 
00112    // hack for now, have to fix nsGREResProperties, but we can't until
00113    // string bundles use UTF8 keys
00114    nsAutoString result;
00115    nsresult rv = mDelegate->Get(NS_ConvertASCIItoUCS2(aKey), result);
00116    LossyAppendUTF16toASCII(result, oResult);
00117    return rv;
00118 }
00119 
00120 //--------------------------------------------------------------
00121 NS_IMETHODIMP
00122 nsCharsetAlias2::Equals(const nsACString& aCharset1,
00123                         const nsACString& aCharset2, PRBool* oResult)
00124 {
00125    nsresult res = NS_OK;
00126 
00127    if(aCharset1.Equals(aCharset2, nsCaseInsensitiveCStringComparator())) {
00128       *oResult = PR_TRUE;
00129       return res;
00130    }
00131 
00132    if(aCharset1.IsEmpty() || aCharset2.IsEmpty()) {
00133       *oResult = PR_FALSE;
00134       return res;
00135    }
00136 
00137    *oResult = PR_FALSE;
00138    nsCAutoString name1;
00139    nsCAutoString name2;
00140    res = this->GetPreferred(aCharset1, name1);
00141    if(NS_SUCCEEDED(res)) {
00142       res = this->GetPreferred(aCharset2, name2);
00143       if(NS_SUCCEEDED(res)) {
00144         *oResult = name1.Equals(name2, nsCaseInsensitiveCStringComparator());
00145       }
00146    }
00147    
00148    return res;
00149 }
00150