Back to index

lightning-sunbird  0.9+nobinonly
nsFontPackageHandler.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) 2001
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Roy Yokoyama <yokoyama@netscape.com>
00024  *   Simon Montagu <smontagu@netscape.com>
00025  *   Seth Spitzer <sspitzer@netscape.com>
00026  *
00027  * Alternatively, the contents of this file may be used under the terms of
00028  * either of the GNU General Public License Version 2 or later (the "GPL"),
00029  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00030  * in which case the provisions of the GPL or the LGPL are applicable instead
00031  * of those above. If you wish to allow use of your version of this file only
00032  * under the terms of either the GPL or the LGPL, and not to allow others to
00033  * use your version of this file under the terms of the MPL, indicate your
00034  * decision by deleting the provisions above and replace them with the notice
00035  * and other provisions required by the GPL or the LGPL. If you do not delete
00036  * the provisions above, a recipient may use your version of this file under
00037  * the terms of any one of the MPL, the GPL or the LGPL.
00038  *
00039  * ***** END LICENSE BLOCK ***** */
00040 
00041 #include "nsCOMPtr.h"
00042 #include "nsString.h"
00043 #include "nsIServiceManager.h"
00044 #include "nsIDOMWindow.h"
00045 #include "nsIFontPackageService.h"
00046 #include "nsIFontPackageHandler.h"
00047 #include "nsILocale.h"
00048 #include "nsIWindowWatcher.h"
00049 #include "nsReadableUtils.h"
00050 #include "nsFontPackageHandler.h"
00051 #include "nsIDOMDocument.h"
00052 #include "nsIDOMElement.h"
00053 #include "nsIWindowMediator.h"
00054 #include "nsISupportsPrimitives.h"
00055 #include "nsIStringBundle.h"
00056 #include "nsUnicharUtils.h"
00057 
00058 //==============================================================================
00059 /* Implementation file */
00060 NS_IMPL_THREADSAFE_ISUPPORTS1(nsFontPackageHandler, nsIFontPackageHandler)
00061 
00062 nsFontPackageHandler::nsFontPackageHandler()
00063 {
00064 }
00065 
00066 nsFontPackageHandler::~nsFontPackageHandler()
00067 {
00068 }
00069 
00070 /* void NeedFontPackage (in string aFontPackID); */
00071 NS_IMETHODIMP nsFontPackageHandler::NeedFontPackage(const char *aFontPackID)
00072 {
00073   // no FontPackage is passed, return
00074   NS_ENSURE_ARG_POINTER(aFontPackID);
00075 
00076   if (!strlen(aFontPackID))
00077     return NS_ERROR_UNEXPECTED;
00078 
00079   nsresult rv;
00080   nsCOMPtr <nsIStringBundleService> bundleService = do_GetService(NS_STRINGBUNDLE_CONTRACTID, &rv);
00081   NS_ENSURE_SUCCESS(rv,rv);
00082   
00083   nsCOMPtr <nsIStringBundle> bundle;
00084   rv = bundleService->CreateBundle("chrome://global/locale/fontpackage.properties", getter_AddRefs(bundle));
00085   NS_ENSURE_SUCCESS(rv,rv);
00086 
00087   nsXPIDLString handledLanguages;
00088   rv = bundle->GetStringFromName(NS_LITERAL_STRING("handled_languages").get(), getter_Copies(handledLanguages));
00089   NS_ENSURE_SUCCESS(rv, rv);
00090   
00091   // aFontPackID is of the form lang:xx or lang:xx-YY
00092   const char *colonPos = strchr(aFontPackID,':');
00093   if (!colonPos || !*(colonPos + 1))
00094     return NS_ERROR_UNEXPECTED;
00095   
00096   // turn (const char *)xx-YY into (PRUnichar *)xx-yy
00097   nsAutoString langCode;
00098   CopyASCIItoUCS2(nsDependentCString(colonPos + 1), langCode);
00099   ToLowerCase(langCode);
00100 
00101   // check for xx or xx-yy in handled_languages
00102   // if not handled, return now, don't show the font dialog
00103   if (!FindInReadable(langCode, handledLanguages))
00104     return NS_OK; // XXX should be error?
00105 
00106   // check whether the topmost window is a mailnews window. If it is,
00107   // or if any of the calls fail, return NS_ERROR_ABORT
00108   nsCOMPtr<nsIWindowMediator> windowMediator = do_GetService(NS_WINDOWMEDIATOR_CONTRACTID, &rv);
00109   NS_ENSURE_SUCCESS(rv, NS_ERROR_ABORT);
00110 
00111   nsCOMPtr<nsISimpleEnumerator> windowEnum;
00112   rv = windowMediator->GetZOrderDOMWindowEnumerator(nsnull, PR_TRUE, getter_AddRefs(windowEnum));
00113   NS_ENSURE_SUCCESS(rv, NS_ERROR_ABORT);
00114 
00115   nsCOMPtr<nsISupports> windowSupports;
00116   nsCOMPtr<nsIDOMWindow> topMostWindow;
00117   nsCOMPtr<nsIDOMDocument> domDocument;
00118   nsCOMPtr<nsIDOMElement> domElement;
00119   nsAutoString windowType;
00120   PRBool more;
00121   windowEnum->HasMoreElements(&more);
00122   if (more) {
00123     rv = windowEnum->GetNext(getter_AddRefs(windowSupports));
00124     NS_ENSURE_SUCCESS(rv, NS_ERROR_ABORT);
00125     NS_ENSURE_TRUE(windowSupports, NS_ERROR_ABORT);
00126 
00127     topMostWindow = do_QueryInterface(windowSupports, &rv);
00128     NS_ENSURE_SUCCESS(rv, NS_ERROR_ABORT);
00129     NS_ENSURE_TRUE(topMostWindow, NS_ERROR_ABORT);
00130 
00131     rv = topMostWindow->GetDocument(getter_AddRefs(domDocument));
00132     NS_ENSURE_SUCCESS(rv, NS_ERROR_ABORT);
00133     NS_ENSURE_TRUE(domDocument, NS_ERROR_ABORT);
00134 
00135     rv = domDocument->GetDocumentElement(getter_AddRefs(domElement));
00136     NS_ENSURE_SUCCESS(rv, NS_ERROR_ABORT);
00137     NS_ENSURE_TRUE(domElement, NS_ERROR_ABORT);
00138 
00139     rv = domElement->GetAttribute(NS_LITERAL_STRING("windowtype"), windowType);
00140     NS_ENSURE_SUCCESS(rv, NS_ERROR_ABORT);
00141 
00142     if (windowType.EqualsLiteral("mail:3pane") ||
00143         windowType.EqualsLiteral("mail:messageWindow")) {
00144       return NS_ERROR_ABORT;
00145     }
00146   }
00147 
00148   nsCOMPtr<nsIWindowWatcher> windowWatch = do_GetService(NS_WINDOWWATCHER_CONTRACTID, &rv);
00149   NS_ENSURE_SUCCESS(rv,rv);
00150 
00151   nsCOMPtr<nsISupportsString> langID = do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID, &rv);
00152   NS_ENSURE_SUCCESS(rv,rv);
00153 
00154   langID->SetData(langCode);
00155 
00156   nsCOMPtr<nsIDOMWindow> dialog;
00157   rv = windowWatch->OpenWindow(nsnull, "chrome://global/content/fontpackage.xul", 
00158               "_blank",
00159               "chrome,centerscreen,titlebar,resizeable=no", // XXX should this be modal?
00160               langID, 
00161               getter_AddRefs(dialog));
00162 
00163   nsCOMPtr<nsIFontPackageService> fontService(do_GetService(NS_FONTPACKAGESERVICE_CONTRACTID));
00164   NS_ENSURE_TRUE(fontService, NS_ERROR_FAILURE);
00165 
00166   fontService->FontPackageHandled(NS_SUCCEEDED(rv), PR_FALSE, aFontPackID);
00167     return rv;
00168   }