1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* ***** BEGIN LICENSE BLOCK *****
3
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
5
* The contents of this file are subject to the Netscape Public License
6
* Version 1.1 (the "License"); you may not use this file except in
7
* compliance with the License. You may obtain a copy of the License at
8
* http://www.mozilla.org/NPL/
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
15
* The Original Code is mozilla.org code.
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.
23
* Frank Yung-Fong Tang <ftang@netscape.com>
26
* Alternatively, the contents of this file may be used under the terms of
27
* either the GNU General Public License Version 2 or later (the "GPL"), or
28
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29
* in which case the provisions of the GPL or the LGPL are applicable instead
30
* of those above. If you wish to allow use of your version of this file only
31
* under the terms of either the GPL or the LGPL, and not to allow others to
32
* use your version of this file under the terms of the NPL, indicate your
33
* decision by deleting the provisions above and replace them with the notice
34
* and other provisions required by the GPL or the LGPL. If you do not delete
35
* the provisions above, a recipient may use your version of this file under
36
* the terms of any one of the NPL, the GPL or the LGPL.
38
* ***** END LICENSE BLOCK ***** */
40
#include "nsFontPackageService.h"
41
#include "nsIComponentManager.h"
42
#include "nsIServiceManager.h"
43
#include "nsIFontEnumerator.h"
52
/* Keep the languages handled here in sync with handled_languages in
53
* mozilla/xpfe/global/resources/content/fontpackage.properties
54
* and the contents of http://www.mozilla.org/projects/intl/fonts/win/redirect/
56
static PRInt8 mJAState = eInit;
57
static PRInt8 mKOState = eInit;
58
static PRInt8 mZHTWState = eInit;
59
static PRInt8 mZHCNState = eInit;
61
NS_IMPL_THREADSAFE_ISUPPORTS2(nsFontPackageService,
62
nsIFontPackageService,
65
/* from nsIFontPackageSercice.h */
66
nsFontPackageService::nsFontPackageService() : mHandler(nsnull)
68
/* member initializers and constructor code */
71
nsFontPackageService::~nsFontPackageService()
76
/* void SetHandler (in nsIFontPackageHandler aHandler); */
77
NS_IMETHODIMP nsFontPackageService::SetHandler(nsIFontPackageHandler *aHandler)
83
/* void FontPackageHandled (in boolean aSuccess, in boolean aReloadPages, in string aFontPackID); */
84
NS_IMETHODIMP nsFontPackageService::FontPackageHandled(PRBool aSuccess, PRBool aReloadPages, const char *aFontPackID)
86
if (strcmp(aFontPackID, "lang:ja") == 0) {
87
mJAState = (aSuccess) ? eInstalled : eInit;
89
else if (strcmp(aFontPackID, "lang:ko") == 0) {
90
mKOState = (aSuccess) ? eInstalled : eInit;
92
else if (strcmp(aFontPackID, "lang:zh-TW") == 0) {
93
mZHTWState = (aSuccess) ? eInstalled : eInit;
95
else if (strcmp(aFontPackID, "lang:zh-CN") == 0) {
96
mZHCNState = (aSuccess) ? eInstalled : eInit;
99
if (*aFontPackID == '\0' && (!aSuccess)) {
100
// if aFontPackID is empty and failed , then reset to eInit.
101
mJAState = mKOState = mZHTWState = mZHCNState = eInit;
107
nsresult nsFontPackageService::CallDownload(const char *aFontPackID, PRInt8 aInState, PRInt8 *aOutState)
111
if (aInState == eInit) {
112
nsCOMPtr<nsIFontEnumerator> fontEnum = do_GetService("@mozilla.org/gfx/fontenumerator;1", &rv);
113
if (NS_SUCCEEDED(rv)) {
114
PRBool have = PR_FALSE;
116
// aFontPackID="lang:xxxx", strip "lang:" and get the actual langID
117
NS_ASSERTION((strncmp(aFontPackID, "lang:", 5) == 0), "Invalid FontPackID format.");
118
const char *langID = &aFontPackID[5];
119
rv = fontEnum->HaveFontFor(langID, &have);
120
if (NS_SUCCEEDED(rv)) {
122
*aOutState = eDownload;
123
rv = mHandler->NeedFontPackage(aFontPackID);
124
if (rv == NS_ERROR_ABORT) {
130
*aOutState = eInstalled;
139
/* void NeedFontPackage (in string aFontPackID); */
140
NS_IMETHODIMP nsFontPackageService::NeedFontPackage(const char *aFontPackID)
144
// create default handler
145
mHandler = do_CreateInstance("@mozilla.org/locale/default-font-package-handler;1", &rv);
146
if (NS_FAILED(rv)) return rv;
149
if (strcmp(aFontPackID, "lang:ja") == 0) {
150
rv = CallDownload(aFontPackID, mJAState, &mJAState);
152
else if (strcmp(aFontPackID, "lang:ko") == 0) {
153
rv = CallDownload(aFontPackID, mKOState, &mKOState);
155
else if (strcmp(aFontPackID, "lang:zh-TW") == 0) {
156
rv = CallDownload(aFontPackID, mZHTWState, &mZHTWState);
158
else if (strcmp(aFontPackID, "lang:zh-CN") == 0) {
159
rv = CallDownload(aFontPackID, mZHCNState, &mZHCNState);