Back to index

lightning-sunbird  0.9+nobinonly
nsLanguageAtomService.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.org 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) 2000
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 "nsIComponentManager.h"
00039 #include "nsLanguageAtomService.h"
00040 #include "nsICharsetConverterManager.h"
00041 #include "nsILocaleService.h"
00042 #include "nsXPIDLString.h"
00043 #include "nsUnicharUtils.h"
00044 #include "nsIServiceManager.h"
00045 
00046 NS_IMPL_ISUPPORTS1(nsLanguageAtomService, nsILanguageAtomService)
00047 
00048 nsLanguageAtomService::nsLanguageAtomService()
00049 {
00050   mLangs.Init();
00051 }
00052 
00053 nsresult
00054 nsLanguageAtomService::InitLangGroupTable()
00055 {
00056   if (mLangGroups) return NS_OK;
00057   nsresult rv;
00058   
00059   nsCOMPtr<nsIStringBundleService> bundleService =
00060     do_GetService(NS_STRINGBUNDLE_CONTRACTID, &rv);
00061   if (NS_FAILED(rv)) return rv;
00062 
00063 
00064   rv = bundleService->CreateBundle("resource://gre/res/langGroups.properties",
00065                                    getter_AddRefs(mLangGroups));
00066   return rv;
00067 }
00068 
00069 nsIAtom*
00070 nsLanguageAtomService::LookupLanguage(const nsAString &aLanguage,
00071                                       nsresult *aError)
00072 {
00073   nsresult res = NS_OK;
00074 
00075   nsAutoString lowered(aLanguage);
00076   ToLowerCase(lowered);
00077 
00078   nsIAtom *lang = mLangs.GetWeak(lowered);
00079 
00080   if (!lang) {
00081     nsXPIDLString langGroupStr;
00082 
00083     if (lowered.EqualsLiteral("en-us")) {
00084       langGroupStr.AssignLiteral("x-western");
00085     } else if (lowered.EqualsLiteral("de-de")) {
00086       langGroupStr.AssignLiteral("x-western");
00087     } else if (lowered.EqualsLiteral("ja-jp")) {
00088       langGroupStr.AssignLiteral("ja");
00089     } else {
00090       if (!mLangGroups) {
00091         if (NS_FAILED(InitLangGroupTable())) {
00092           if (aError)
00093             *aError = NS_ERROR_FAILURE;
00094 
00095           return nsnull;
00096         }
00097       }
00098       res = mLangGroups->GetStringFromName(lowered.get(), getter_Copies(langGroupStr));
00099       if (NS_FAILED(res)) {
00100         PRInt32 hyphen = lowered.FindChar('-');
00101         if (hyphen >= 0) {
00102           nsAutoString truncated(lowered);
00103           truncated.Truncate(hyphen);
00104           res = mLangGroups->GetStringFromName(truncated.get(), getter_Copies(langGroupStr));
00105           if (NS_FAILED(res)) {
00106             langGroupStr.AssignLiteral("x-unicode");
00107           }
00108         } else {
00109           langGroupStr.AssignLiteral("x-unicode");
00110         }
00111       }
00112     }
00113     nsCOMPtr<nsIAtom> langGroup = do_GetAtom(langGroupStr);
00114 
00115     // The hashtable will keep an owning reference to the atom
00116     mLangs.Put(lowered, langGroup);
00117     lang = langGroup;
00118   }
00119 
00120   if (aError)
00121     *aError = res;
00122 
00123   return lang;
00124 }
00125 
00126 already_AddRefed<nsIAtom>
00127 nsLanguageAtomService::LookupCharSet(const char *aCharSet, nsresult *aError)
00128 {
00129   if (!mCharSets) {
00130     mCharSets = do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID);
00131     if (!mCharSets) {
00132       if (aError)
00133         *aError = NS_ERROR_FAILURE;
00134 
00135       return nsnull;
00136     }
00137   }
00138 
00139   nsCOMPtr<nsIAtom> langGroup;
00140   mCharSets->GetCharsetLangGroup(aCharSet, getter_AddRefs(langGroup));
00141   if (!langGroup) {
00142     if (aError)
00143       *aError = NS_ERROR_FAILURE;
00144 
00145     return nsnull;
00146   }
00147 
00148   // transfer reference to raw pointer
00149   nsIAtom *raw = nsnull;
00150   langGroup.swap(raw);
00151 
00152   if (aError)
00153     *aError = NS_OK;
00154 
00155   return raw;
00156 }
00157 
00158 nsIAtom*
00159 nsLanguageAtomService::GetLocaleLanguageGroup(nsresult *aError)
00160 {
00161   nsresult res = NS_OK;
00162 
00163   do {
00164     if (!mLocaleLangGroup) {
00165       nsCOMPtr<nsILocaleService> localeService;
00166       localeService = do_GetService(NS_LOCALESERVICE_CONTRACTID);
00167       if (!localeService) {
00168         res = NS_ERROR_FAILURE;
00169         break;
00170       }
00171 
00172       nsCOMPtr<nsILocale> locale;
00173       res = localeService->GetApplicationLocale(getter_AddRefs(locale));
00174       if (NS_FAILED(res))
00175         break;
00176 
00177       nsAutoString category;
00178       category.AssignWithConversion(NSILOCALE_MESSAGE);
00179       nsAutoString loc;
00180       res = locale->GetCategory(category, loc);
00181       if (NS_FAILED(res))
00182         break;
00183 
00184       mLocaleLangGroup = LookupLanguage(loc, &res);
00185     }
00186   } while (0);
00187 
00188   if (aError)
00189     *aError = res;
00190 
00191   return mLocaleLangGroup;
00192 }