1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Netscape Communications Corporation.
19 : * Portions created by the Initial Developer are Copyright (C) 2000
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either of the GNU General Public License Version 2 or later (the "GPL"),
26 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #include "nsIComponentManager.h"
39 : #include "nsLanguageAtomService.h"
40 : #include "nsICharsetConverterManager.h"
41 : #include "nsILocaleService.h"
42 : #include "nsXPIDLString.h"
43 : #include "nsUnicharUtils.h"
44 : #include "nsIServiceManager.h"
45 : #include "nsIAtom.h"
46 : #include "mozilla/Services.h"
47 :
48 0 : NS_IMPL_ISUPPORTS1(nsLanguageAtomService, nsILanguageAtomService)
49 :
50 0 : nsLanguageAtomService::nsLanguageAtomService()
51 : {
52 0 : mLangToGroup.Init();
53 0 : }
54 :
55 : nsresult
56 0 : nsLanguageAtomService::InitLangGroupTable()
57 : {
58 0 : if (mLangGroups)
59 0 : return NS_OK;
60 :
61 : nsCOMPtr<nsIStringBundleService> bundleService =
62 0 : mozilla::services::GetStringBundleService();
63 0 : if (!bundleService)
64 0 : return NS_ERROR_FAILURE;
65 :
66 0 : return bundleService->CreateBundle("resource://gre/res/langGroups.properties",
67 0 : getter_AddRefs(mLangGroups));
68 : }
69 :
70 : nsIAtom*
71 0 : nsLanguageAtomService::LookupLanguage(const nsACString &aLanguage,
72 : nsresult *aError)
73 : {
74 0 : nsCAutoString lowered(aLanguage);
75 0 : ToLowerCase(lowered);
76 :
77 0 : nsCOMPtr<nsIAtom> lang = do_GetAtom(lowered);
78 0 : return GetLanguageGroup(lang, aError);
79 : }
80 :
81 : already_AddRefed<nsIAtom>
82 0 : nsLanguageAtomService::LookupCharSet(const char *aCharSet, nsresult *aError)
83 : {
84 0 : if (!mCharSets) {
85 0 : mCharSets = do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID);
86 0 : if (!mCharSets) {
87 0 : if (aError)
88 0 : *aError = NS_ERROR_FAILURE;
89 :
90 0 : return nsnull;
91 : }
92 : }
93 :
94 0 : nsCOMPtr<nsIAtom> langGroup;
95 0 : mCharSets->GetCharsetLangGroup(aCharSet, getter_AddRefs(langGroup));
96 0 : if (!langGroup) {
97 0 : if (aError)
98 0 : *aError = NS_ERROR_FAILURE;
99 :
100 0 : return nsnull;
101 : }
102 :
103 : // transfer reference to raw pointer
104 0 : nsIAtom *raw = nsnull;
105 0 : langGroup.swap(raw);
106 :
107 0 : if (aError)
108 0 : *aError = NS_OK;
109 :
110 0 : return raw;
111 : }
112 :
113 : nsIAtom*
114 0 : nsLanguageAtomService::GetLocaleLanguage(nsresult *aError)
115 : {
116 0 : nsresult res = NS_OK;
117 :
118 : do {
119 0 : if (!mLocaleLanguage) {
120 0 : nsCOMPtr<nsILocaleService> localeService;
121 0 : localeService = do_GetService(NS_LOCALESERVICE_CONTRACTID);
122 0 : if (!localeService) {
123 0 : res = NS_ERROR_FAILURE;
124 : break;
125 : }
126 :
127 0 : nsCOMPtr<nsILocale> locale;
128 0 : res = localeService->GetApplicationLocale(getter_AddRefs(locale));
129 0 : if (NS_FAILED(res))
130 : break;
131 :
132 0 : nsAutoString loc;
133 0 : res = locale->GetCategory(NS_LITERAL_STRING(NSILOCALE_MESSAGE), loc);
134 0 : if (NS_FAILED(res))
135 : break;
136 :
137 0 : ToLowerCase(loc); // use lowercase for all language atoms
138 0 : mLocaleLanguage = do_GetAtom(loc);
139 : }
140 : } while (0);
141 :
142 0 : if (aError)
143 0 : *aError = res;
144 :
145 0 : return mLocaleLanguage;
146 : }
147 :
148 : nsIAtom*
149 0 : nsLanguageAtomService::GetLanguageGroup(nsIAtom *aLanguage,
150 : nsresult *aError)
151 : {
152 : nsIAtom *retVal;
153 0 : nsresult res = NS_OK;
154 :
155 0 : retVal = mLangToGroup.GetWeak(aLanguage);
156 :
157 0 : if (!retVal) {
158 0 : if (!mLangGroups) {
159 0 : if (NS_FAILED(InitLangGroupTable())) {
160 0 : if (aError) {
161 0 : *aError = NS_ERROR_FAILURE;
162 : }
163 0 : return nsnull;
164 : }
165 : }
166 :
167 0 : nsString langStr;
168 0 : aLanguage->ToString(langStr);
169 :
170 0 : nsXPIDLString langGroupStr;
171 0 : res = mLangGroups->GetStringFromName(langStr.get(),
172 0 : getter_Copies(langGroupStr));
173 0 : if (NS_FAILED(res)) {
174 0 : PRInt32 hyphen = langStr.FindChar('-');
175 0 : if (hyphen >= 0) {
176 0 : nsAutoString truncated(langStr);
177 0 : truncated.Truncate(hyphen);
178 0 : res = mLangGroups->GetStringFromName(truncated.get(),
179 0 : getter_Copies(langGroupStr));
180 0 : if (NS_FAILED(res)) {
181 0 : langGroupStr.AssignLiteral("x-unicode");
182 : }
183 : } else {
184 0 : langGroupStr.AssignLiteral("x-unicode");
185 : }
186 : }
187 :
188 0 : nsCOMPtr<nsIAtom> langGroup = do_GetAtom(langGroupStr);
189 :
190 : // The hashtable will keep an owning reference to the atom
191 0 : mLangToGroup.Put(aLanguage, langGroup);
192 0 : retVal = langGroup.get();
193 : }
194 :
195 0 : if (aError) {
196 0 : *aError = res;
197 : }
198 :
199 0 : return retVal;
200 : }
|