Back to index

lightning-sunbird  0.9+nobinonly
nsFontPackageService.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  *   Frank Yung-Fong Tang <ftang@netscape.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "nsFontPackageService.h"
00040 #include "nsIComponentManager.h"
00041 #include "nsIServiceManager.h"
00042 #include "nsIFontEnumerator.h" 
00043 #include "plstr.h"
00044 
00045 enum {
00046   eInit = 0,
00047   eDownload,
00048   eInstalled
00049 };
00050 
00051 /* Keep the languages handled here in sync with handled_languages in
00052  * mozilla/xpfe/global/resources/content/fontpackage.properties
00053  * and the contents of http://www.mozilla.org/projects/intl/fonts/win/redirect/
00054  */
00055 static PRInt8 mJAState = eInit;
00056 static PRInt8 mKOState = eInit;
00057 static PRInt8 mZHTWState = eInit;
00058 static PRInt8 mZHCNState = eInit;
00059 
00060 NS_IMPL_THREADSAFE_ISUPPORTS2(nsFontPackageService, 
00061    nsIFontPackageService,
00062    nsIFontPackageProxy)
00063 
00064 /* from nsIFontPackageSercice.h */
00065 nsFontPackageService::nsFontPackageService() : mHandler(nsnull)
00066 {
00067   /* member initializers and constructor code */
00068 }
00069 
00070 nsFontPackageService::~nsFontPackageService()
00071 {
00072   /* destructor code */
00073 }
00074 
00075 /* void SetHandler (in nsIFontPackageHandler aHandler); */
00076 NS_IMETHODIMP nsFontPackageService::SetHandler(nsIFontPackageHandler *aHandler)
00077 {
00078     mHandler = aHandler;
00079     return NS_OK;
00080 }
00081 
00082 /* void FontPackageHandled (in boolean aSuccess, in boolean aReloadPages, in string aFontPackID); */
00083 NS_IMETHODIMP nsFontPackageService::FontPackageHandled(PRBool aSuccess, PRBool aReloadPages, const char *aFontPackID)
00084 {
00085   if (strcmp(aFontPackID, "lang:ja") == 0) {
00086     mJAState = (aSuccess) ? eInstalled : eInit;
00087   }
00088   else if (strcmp(aFontPackID, "lang:ko") == 0) {
00089     mKOState = (aSuccess) ? eInstalled : eInit;
00090   }
00091   else if (strcmp(aFontPackID, "lang:zh-TW") == 0) {
00092     mZHTWState = (aSuccess) ? eInstalled : eInit;
00093   }
00094   else if (strcmp(aFontPackID, "lang:zh-CN") == 0) {
00095     mZHCNState = (aSuccess) ? eInstalled : eInit;
00096   }
00097 
00098   if (*aFontPackID == '\0' && (!aSuccess)) {
00099     // if aFontPackID is empty and failed , then reset to eInit.
00100     mJAState = mKOState = mZHTWState = mZHCNState = eInit;
00101   }
00102 
00103   return NS_OK;
00104 }
00105 
00106 nsresult nsFontPackageService::CallDownload(const char *aFontPackID, PRInt8 aInState, PRInt8 *aOutState)
00107 {
00108   nsresult rv = NS_OK;
00109 
00110   if (aInState == eInit)  {
00111     nsCOMPtr<nsIFontEnumerator> fontEnum = do_GetService("@mozilla.org/gfx/fontenumerator;1", &rv);
00112     if (NS_SUCCEEDED(rv)) { 
00113       PRBool have = PR_FALSE;
00114 
00115       // aFontPackID="lang:xxxx", strip "lang:" and get the actual langID
00116       NS_ASSERTION((strncmp(aFontPackID, "lang:", 5) == 0), "Invalid FontPackID format.");
00117       const char *langID = &aFontPackID[5]; 
00118       rv = fontEnum->HaveFontFor(langID, &have);
00119       if (NS_SUCCEEDED(rv)) { 
00120         if (!have) {
00121           *aOutState = eDownload;
00122           rv = mHandler->NeedFontPackage(aFontPackID);
00123           if (rv == NS_ERROR_ABORT) {
00124             *aOutState = eInit;
00125             rv = NS_OK;
00126           }
00127         }
00128         else  {
00129           *aOutState = eInstalled;
00130         }
00131       }
00132     }
00133   }
00134 
00135   return rv;
00136 }
00137 
00138 /* void NeedFontPackage (in string aFontPackID); */
00139 NS_IMETHODIMP nsFontPackageService::NeedFontPackage(const char *aFontPackID)
00140 {
00141   nsresult rv = NS_OK;
00142   if (!mHandler) {
00143     // create default handler
00144     mHandler = do_CreateInstance("@mozilla.org/locale/default-font-package-handler;1", &rv);
00145     if (NS_FAILED(rv)) return rv;
00146   }
00147 
00148   if (strcmp(aFontPackID, "lang:ja") == 0) {
00149     rv = CallDownload(aFontPackID, mJAState, &mJAState);
00150   }
00151   else if (strcmp(aFontPackID, "lang:ko") == 0) {
00152     rv = CallDownload(aFontPackID, mKOState, &mKOState);
00153   }
00154   else if (strcmp(aFontPackID, "lang:zh-TW") == 0) {
00155     rv = CallDownload(aFontPackID, mZHTWState, &mZHTWState);
00156   }
00157   else if (strcmp(aFontPackID, "lang:zh-CN") == 0) {
00158     rv = CallDownload(aFontPackID, mZHCNState, &mZHCNState);
00159   }
00160 
00161   return rv;
00162 }