michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsPrintingPromptService.h" michael@0: michael@0: #include "nsIComponentManager.h" michael@0: #include "nsIDialogParamBlock.h" michael@0: #include "nsIDOMWindow.h" michael@0: #include "nsIServiceManager.h" michael@0: #include "nsISupportsUtils.h" michael@0: #include "nsISupportsArray.h" michael@0: #include "nsString.h" michael@0: #include "nsIPrintDialogService.h" michael@0: michael@0: // Printing Progress Includes michael@0: #include "nsPrintProgress.h" michael@0: #include "nsPrintProgressParams.h" michael@0: michael@0: static const char *kPrintDialogURL = "chrome://global/content/printdialog.xul"; michael@0: static const char *kPrintProgressDialogURL = "chrome://global/content/printProgress.xul"; michael@0: static const char *kPrtPrvProgressDialogURL = "chrome://global/content/printPreviewProgress.xul"; michael@0: static const char *kPageSetupDialogURL = "chrome://global/content/printPageSetup.xul"; michael@0: static const char *kPrinterPropertiesURL = "chrome://global/content/printjoboptions.xul"; michael@0: michael@0: /**************************************************************** michael@0: ************************* ParamBlock *************************** michael@0: ****************************************************************/ michael@0: michael@0: class ParamBlock { michael@0: michael@0: public: michael@0: ParamBlock() michael@0: { michael@0: mBlock = 0; michael@0: } michael@0: ~ParamBlock() michael@0: { michael@0: NS_IF_RELEASE(mBlock); michael@0: } michael@0: nsresult Init() { michael@0: return CallCreateInstance(NS_DIALOGPARAMBLOCK_CONTRACTID, &mBlock); michael@0: } michael@0: nsIDialogParamBlock * operator->() const { return mBlock; } michael@0: operator nsIDialogParamBlock * const () { return mBlock; } michael@0: michael@0: private: michael@0: nsIDialogParamBlock *mBlock; michael@0: }; michael@0: michael@0: /**************************************************************** michael@0: ***************** nsPrintingPromptService ********************** michael@0: ****************************************************************/ michael@0: michael@0: NS_IMPL_ISUPPORTS(nsPrintingPromptService, nsIPrintingPromptService, nsIWebProgressListener) michael@0: michael@0: nsPrintingPromptService::nsPrintingPromptService() michael@0: { michael@0: } michael@0: michael@0: nsPrintingPromptService::~nsPrintingPromptService() michael@0: { michael@0: } michael@0: michael@0: nsresult michael@0: nsPrintingPromptService::Init() michael@0: { michael@0: nsresult rv; michael@0: mWatcher = do_GetService(NS_WINDOWWATCHER_CONTRACTID, &rv); michael@0: return rv; michael@0: } michael@0: michael@0: /* void showPrintDialog (in nsIDOMWindow parent, in nsIWebBrowserPrint webBrowserPrint, in nsIPrintSettings printSettings); */ michael@0: NS_IMETHODIMP michael@0: nsPrintingPromptService::ShowPrintDialog(nsIDOMWindow *parent, nsIWebBrowserPrint *webBrowserPrint, nsIPrintSettings *printSettings) michael@0: { michael@0: NS_ENSURE_ARG(webBrowserPrint); michael@0: NS_ENSURE_ARG(printSettings); michael@0: michael@0: // Try to access a component dialog michael@0: nsCOMPtr dlgPrint(do_GetService( michael@0: NS_PRINTDIALOGSERVICE_CONTRACTID)); michael@0: if (dlgPrint) michael@0: return dlgPrint->Show(parent, printSettings, webBrowserPrint); michael@0: michael@0: // Show the built-in dialog instead michael@0: ParamBlock block; michael@0: nsresult rv = block.Init(); michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: michael@0: block->SetInt(0, 0); michael@0: return DoDialog(parent, block, webBrowserPrint, printSettings, kPrintDialogURL); michael@0: } michael@0: michael@0: /* void showProgress (in nsIDOMWindow parent, in nsIWebBrowserPrint webBrowserPrint, in nsIPrintSettings printSettings, in nsIObserver openDialogObserver, in boolean isForPrinting, out nsIWebProgressListener webProgressListener, out nsIPrintProgressParams printProgressParams, out boolean notifyOnOpen); */ michael@0: NS_IMETHODIMP michael@0: nsPrintingPromptService::ShowProgress(nsIDOMWindow* parent, michael@0: nsIWebBrowserPrint* webBrowserPrint, // ok to be null michael@0: nsIPrintSettings* printSettings, // ok to be null michael@0: nsIObserver* openDialogObserver, // ok to be null michael@0: bool isForPrinting, michael@0: nsIWebProgressListener** webProgressListener, michael@0: nsIPrintProgressParams** printProgressParams, michael@0: bool* notifyOnOpen) michael@0: { michael@0: NS_ENSURE_ARG(webProgressListener); michael@0: NS_ENSURE_ARG(printProgressParams); michael@0: NS_ENSURE_ARG(notifyOnOpen); michael@0: michael@0: *notifyOnOpen = false; michael@0: michael@0: nsPrintProgress* prtProgress = new nsPrintProgress(printSettings); michael@0: mPrintProgress = prtProgress; michael@0: mWebProgressListener = prtProgress; michael@0: michael@0: nsCOMPtr prtProgressParams = new nsPrintProgressParams(); michael@0: michael@0: nsCOMPtr parentWindow = parent; michael@0: michael@0: if (mWatcher && !parentWindow) { michael@0: mWatcher->GetActiveWindow(getter_AddRefs(parentWindow)); michael@0: } michael@0: michael@0: if (parentWindow) { michael@0: mPrintProgress->OpenProgressDialog(parentWindow, michael@0: isForPrinting ? kPrintProgressDialogURL : kPrtPrvProgressDialogURL, michael@0: prtProgressParams, openDialogObserver, notifyOnOpen); michael@0: } michael@0: michael@0: prtProgressParams.forget(printProgressParams); michael@0: NS_ADDREF(*webProgressListener = this); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: /* void showPageSetup (in nsIDOMWindow parent, in nsIPrintSettings printSettings); */ michael@0: NS_IMETHODIMP michael@0: nsPrintingPromptService::ShowPageSetup(nsIDOMWindow *parent, nsIPrintSettings *printSettings, nsIObserver *aObs) michael@0: { michael@0: NS_ENSURE_ARG(printSettings); michael@0: michael@0: // Try to access a component dialog michael@0: nsCOMPtr dlgPrint(do_GetService( michael@0: NS_PRINTDIALOGSERVICE_CONTRACTID)); michael@0: if (dlgPrint) michael@0: return dlgPrint->ShowPageSetup(parent, printSettings); michael@0: michael@0: ParamBlock block; michael@0: nsresult rv = block.Init(); michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: michael@0: block->SetInt(0, 0); michael@0: return DoDialog(parent, block, nullptr, printSettings, kPageSetupDialogURL); michael@0: } michael@0: michael@0: /* void showPrinterProperties (in nsIDOMWindow parent, in wstring printerName, in nsIPrintSettings printSettings); */ michael@0: NS_IMETHODIMP michael@0: nsPrintingPromptService::ShowPrinterProperties(nsIDOMWindow *parent, const char16_t *printerName, nsIPrintSettings *printSettings) michael@0: { michael@0: /* fixme: We simply ignore the |aPrinter| argument here michael@0: * We should get the supported printer attributes from the printer and michael@0: * populate the print job options dialog with these data instead of using michael@0: * the "default set" here. michael@0: * However, this requires changes on all platforms and is another big chunk michael@0: * of patches ... ;-( michael@0: */ michael@0: NS_ENSURE_ARG(printerName); michael@0: NS_ENSURE_ARG(printSettings); michael@0: michael@0: ParamBlock block; michael@0: nsresult rv = block.Init(); michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: michael@0: block->SetInt(0, 0); michael@0: return DoDialog(parent, block, nullptr, printSettings, kPrinterPropertiesURL); michael@0: michael@0: } michael@0: michael@0: nsresult michael@0: nsPrintingPromptService::DoDialog(nsIDOMWindow *aParent, michael@0: nsIDialogParamBlock *aParamBlock, michael@0: nsIWebBrowserPrint *aWebBrowserPrint, michael@0: nsIPrintSettings* aPS, michael@0: const char *aChromeURL) michael@0: { michael@0: NS_ENSURE_ARG(aParamBlock); michael@0: NS_ENSURE_ARG(aPS); michael@0: NS_ENSURE_ARG(aChromeURL); michael@0: michael@0: if (!mWatcher) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: nsresult rv = NS_OK; michael@0: michael@0: // get a parent, if at all possible michael@0: // (though we'd rather this didn't fail, it's OK if it does. so there's michael@0: // no failure or null check.) michael@0: nsCOMPtr activeParent; // retain ownership for method lifetime michael@0: if (!aParent) michael@0: { michael@0: mWatcher->GetActiveWindow(getter_AddRefs(activeParent)); michael@0: aParent = activeParent; michael@0: } michael@0: michael@0: // create a nsISupportsArray of the parameters michael@0: // being passed to the window michael@0: nsCOMPtr array; michael@0: NS_NewISupportsArray(getter_AddRefs(array)); michael@0: if (!array) return NS_ERROR_FAILURE; michael@0: michael@0: nsCOMPtr psSupports(do_QueryInterface(aPS)); michael@0: NS_ASSERTION(psSupports, "PrintSettings must be a supports"); michael@0: array->AppendElement(psSupports); michael@0: michael@0: if (aWebBrowserPrint) { michael@0: nsCOMPtr wbpSupports(do_QueryInterface(aWebBrowserPrint)); michael@0: NS_ASSERTION(wbpSupports, "nsIWebBrowserPrint must be a supports"); michael@0: array->AppendElement(wbpSupports); michael@0: } michael@0: michael@0: nsCOMPtr blkSupps(do_QueryInterface(aParamBlock)); michael@0: NS_ASSERTION(blkSupps, "IOBlk must be a supports"); michael@0: array->AppendElement(blkSupps); michael@0: michael@0: nsCOMPtr arguments(do_QueryInterface(array)); michael@0: NS_ASSERTION(array, "array must be a supports"); michael@0: michael@0: michael@0: nsCOMPtr dialog; michael@0: rv = mWatcher->OpenWindow(aParent, aChromeURL, "_blank", michael@0: "centerscreen,chrome,modal,titlebar", arguments, michael@0: getter_AddRefs(dialog)); michael@0: michael@0: // if aWebBrowserPrint is not null then we are printing michael@0: // so we want to pass back NS_ERROR_ABORT on cancel michael@0: if (NS_SUCCEEDED(rv) && aWebBrowserPrint) michael@0: { michael@0: int32_t status; michael@0: aParamBlock->GetInt(0, &status); michael@0: return status == 0?NS_ERROR_ABORT:NS_OK; michael@0: } michael@0: michael@0: return rv; michael@0: } michael@0: michael@0: ////////////////////////////////////////////////////////////////////// michael@0: // nsIWebProgressListener michael@0: ////////////////////////////////////////////////////////////////////// michael@0: michael@0: /* void onStateChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in unsigned long aStateFlags, in nsresult aStatus); */ michael@0: NS_IMETHODIMP michael@0: nsPrintingPromptService::OnStateChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, uint32_t aStateFlags, nsresult aStatus) michael@0: { michael@0: if ((aStateFlags & STATE_STOP) && mWebProgressListener) { michael@0: mWebProgressListener->OnStateChange(aWebProgress, aRequest, aStateFlags, aStatus); michael@0: if (mPrintProgress) { michael@0: mPrintProgress->CloseProgressDialog(true); michael@0: } michael@0: mPrintProgress = nullptr; michael@0: mWebProgressListener = nullptr; michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: /* void onProgressChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in long aCurSelfProgress, in long aMaxSelfProgress, in long aCurTotalProgress, in long aMaxTotalProgress); */ michael@0: NS_IMETHODIMP michael@0: nsPrintingPromptService::OnProgressChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, int32_t aCurSelfProgress, int32_t aMaxSelfProgress, int32_t aCurTotalProgress, int32_t aMaxTotalProgress) michael@0: { michael@0: if (mWebProgressListener) { michael@0: return mWebProgressListener->OnProgressChange(aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress); michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: /* void onLocationChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in nsIURI location, in unsigned long aFlags); */ michael@0: NS_IMETHODIMP michael@0: nsPrintingPromptService::OnLocationChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, nsIURI *location, uint32_t aFlags) michael@0: { michael@0: if (mWebProgressListener) { michael@0: return mWebProgressListener->OnLocationChange(aWebProgress, aRequest, location, aFlags); michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: /* void onStatusChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in nsresult aStatus, in wstring aMessage); */ michael@0: NS_IMETHODIMP michael@0: nsPrintingPromptService::OnStatusChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, nsresult aStatus, const char16_t *aMessage) michael@0: { michael@0: if (mWebProgressListener) { michael@0: return mWebProgressListener->OnStatusChange(aWebProgress, aRequest, aStatus, aMessage); michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: /* void onSecurityChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in unsigned long state); */ michael@0: NS_IMETHODIMP michael@0: nsPrintingPromptService::OnSecurityChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, uint32_t state) michael@0: { michael@0: if (mWebProgressListener) { michael@0: return mWebProgressListener->OnSecurityChange(aWebProgress, aRequest, state); michael@0: } michael@0: return NS_OK; michael@0: }