~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/intl/locale/src/nsFontPackageService.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
4
 *
 
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/
 
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
 *  Frank Yung-Fong Tang <ftang@netscape.com>
 
24
 *
 
25
 *
 
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.
 
37
 *
 
38
 * ***** END LICENSE BLOCK ***** */
 
39
 
 
40
#include "nsFontPackageService.h"
 
41
#include "nsIComponentManager.h"
 
42
#include "nsIServiceManager.h"
 
43
#include "nsIFontEnumerator.h" 
 
44
#include "plstr.h"
 
45
 
 
46
enum {
 
47
  eInit = 0,
 
48
  eDownload,
 
49
  eInstalled
 
50
};
 
51
 
 
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/
 
55
 */
 
56
static PRInt8 mJAState = eInit;
 
57
static PRInt8 mKOState = eInit;
 
58
static PRInt8 mZHTWState = eInit;
 
59
static PRInt8 mZHCNState = eInit;
 
60
 
 
61
NS_IMPL_THREADSAFE_ISUPPORTS2(nsFontPackageService, 
 
62
   nsIFontPackageService,
 
63
   nsIFontPackageProxy)
 
64
 
 
65
/* from nsIFontPackageSercice.h */
 
66
nsFontPackageService::nsFontPackageService() : mHandler(nsnull)
 
67
{
 
68
  /* member initializers and constructor code */
 
69
}
 
70
 
 
71
nsFontPackageService::~nsFontPackageService()
 
72
{
 
73
  /* destructor code */
 
74
}
 
75
 
 
76
/* void SetHandler (in nsIFontPackageHandler aHandler); */
 
77
NS_IMETHODIMP nsFontPackageService::SetHandler(nsIFontPackageHandler *aHandler)
 
78
{
 
79
    mHandler = aHandler;
 
80
    return NS_OK;
 
81
}
 
82
 
 
83
/* void FontPackageHandled (in boolean aSuccess, in boolean aReloadPages, in string aFontPackID); */
 
84
NS_IMETHODIMP nsFontPackageService::FontPackageHandled(PRBool aSuccess, PRBool aReloadPages, const char *aFontPackID)
 
85
{
 
86
  if (strcmp(aFontPackID, "lang:ja") == 0) {
 
87
    mJAState = (aSuccess) ? eInstalled : eInit;
 
88
  }
 
89
  else if (strcmp(aFontPackID, "lang:ko") == 0) {
 
90
    mKOState = (aSuccess) ? eInstalled : eInit;
 
91
  }
 
92
  else if (strcmp(aFontPackID, "lang:zh-TW") == 0) {
 
93
    mZHTWState = (aSuccess) ? eInstalled : eInit;
 
94
  }
 
95
  else if (strcmp(aFontPackID, "lang:zh-CN") == 0) {
 
96
    mZHCNState = (aSuccess) ? eInstalled : eInit;
 
97
  }
 
98
 
 
99
  if (*aFontPackID == '\0' && (!aSuccess)) {
 
100
    // if aFontPackID is empty and failed , then reset to eInit.
 
101
    mJAState = mKOState = mZHTWState = mZHCNState = eInit;
 
102
  }
 
103
 
 
104
  return NS_OK;
 
105
}
 
106
 
 
107
nsresult nsFontPackageService::CallDownload(const char *aFontPackID, PRInt8 aInState, PRInt8 *aOutState)
 
108
{
 
109
  nsresult rv = NS_OK;
 
110
 
 
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;
 
115
 
 
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)) { 
 
121
        if (!have) {
 
122
          *aOutState = eDownload;
 
123
          rv = mHandler->NeedFontPackage(aFontPackID);
 
124
          if (rv == NS_ERROR_ABORT) {
 
125
            *aOutState = eInit;
 
126
            rv = NS_OK;
 
127
          }
 
128
        }
 
129
        else  {
 
130
          *aOutState = eInstalled;
 
131
        }
 
132
      }
 
133
    }
 
134
  }
 
135
 
 
136
  return rv;
 
137
}
 
138
 
 
139
/* void NeedFontPackage (in string aFontPackID); */
 
140
NS_IMETHODIMP nsFontPackageService::NeedFontPackage(const char *aFontPackID)
 
141
{
 
142
  nsresult rv = NS_OK;
 
143
  if (!mHandler) {
 
144
    // create default handler
 
145
    mHandler = do_CreateInstance("@mozilla.org/locale/default-font-package-handler;1", &rv);
 
146
    if (NS_FAILED(rv)) return rv;
 
147
  }
 
148
 
 
149
  if (strcmp(aFontPackID, "lang:ja") == 0) {
 
150
    rv = CallDownload(aFontPackID, mJAState, &mJAState);
 
151
  }
 
152
  else if (strcmp(aFontPackID, "lang:ko") == 0) {
 
153
    rv = CallDownload(aFontPackID, mKOState, &mKOState);
 
154
  }
 
155
  else if (strcmp(aFontPackID, "lang:zh-TW") == 0) {
 
156
    rv = CallDownload(aFontPackID, mZHTWState, &mZHTWState);
 
157
  }
 
158
  else if (strcmp(aFontPackID, "lang:zh-CN") == 0) {
 
159
    rv = CallDownload(aFontPackID, mZHCNState, &mZHCNState);
 
160
  }
 
161
 
 
162
  return rv;
 
163
}