1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/embedding/components/printingui/src/unixshared/nsPrintingPromptService.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,307 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "nsPrintingPromptService.h" 1.10 + 1.11 +#include "nsIComponentManager.h" 1.12 +#include "nsIDialogParamBlock.h" 1.13 +#include "nsIDOMWindow.h" 1.14 +#include "nsIServiceManager.h" 1.15 +#include "nsISupportsUtils.h" 1.16 +#include "nsISupportsArray.h" 1.17 +#include "nsString.h" 1.18 +#include "nsIPrintDialogService.h" 1.19 + 1.20 +// Printing Progress Includes 1.21 +#include "nsPrintProgress.h" 1.22 +#include "nsPrintProgressParams.h" 1.23 + 1.24 +static const char *kPrintDialogURL = "chrome://global/content/printdialog.xul"; 1.25 +static const char *kPrintProgressDialogURL = "chrome://global/content/printProgress.xul"; 1.26 +static const char *kPrtPrvProgressDialogURL = "chrome://global/content/printPreviewProgress.xul"; 1.27 +static const char *kPageSetupDialogURL = "chrome://global/content/printPageSetup.xul"; 1.28 +static const char *kPrinterPropertiesURL = "chrome://global/content/printjoboptions.xul"; 1.29 + 1.30 +/**************************************************************** 1.31 + ************************* ParamBlock *************************** 1.32 + ****************************************************************/ 1.33 + 1.34 +class ParamBlock { 1.35 + 1.36 +public: 1.37 + ParamBlock() 1.38 + { 1.39 + mBlock = 0; 1.40 + } 1.41 + ~ParamBlock() 1.42 + { 1.43 + NS_IF_RELEASE(mBlock); 1.44 + } 1.45 + nsresult Init() { 1.46 + return CallCreateInstance(NS_DIALOGPARAMBLOCK_CONTRACTID, &mBlock); 1.47 + } 1.48 + nsIDialogParamBlock * operator->() const { return mBlock; } 1.49 + operator nsIDialogParamBlock * const () { return mBlock; } 1.50 + 1.51 +private: 1.52 + nsIDialogParamBlock *mBlock; 1.53 +}; 1.54 + 1.55 +/**************************************************************** 1.56 + ***************** nsPrintingPromptService ********************** 1.57 + ****************************************************************/ 1.58 + 1.59 +NS_IMPL_ISUPPORTS(nsPrintingPromptService, nsIPrintingPromptService, nsIWebProgressListener) 1.60 + 1.61 +nsPrintingPromptService::nsPrintingPromptService() 1.62 +{ 1.63 +} 1.64 + 1.65 +nsPrintingPromptService::~nsPrintingPromptService() 1.66 +{ 1.67 +} 1.68 + 1.69 +nsresult 1.70 +nsPrintingPromptService::Init() 1.71 +{ 1.72 + nsresult rv; 1.73 + mWatcher = do_GetService(NS_WINDOWWATCHER_CONTRACTID, &rv); 1.74 + return rv; 1.75 +} 1.76 + 1.77 +/* void showPrintDialog (in nsIDOMWindow parent, in nsIWebBrowserPrint webBrowserPrint, in nsIPrintSettings printSettings); */ 1.78 +NS_IMETHODIMP 1.79 +nsPrintingPromptService::ShowPrintDialog(nsIDOMWindow *parent, nsIWebBrowserPrint *webBrowserPrint, nsIPrintSettings *printSettings) 1.80 +{ 1.81 + NS_ENSURE_ARG(webBrowserPrint); 1.82 + NS_ENSURE_ARG(printSettings); 1.83 + 1.84 + // Try to access a component dialog 1.85 + nsCOMPtr<nsIPrintDialogService> dlgPrint(do_GetService( 1.86 + NS_PRINTDIALOGSERVICE_CONTRACTID)); 1.87 + if (dlgPrint) 1.88 + return dlgPrint->Show(parent, printSettings, webBrowserPrint); 1.89 + 1.90 + // Show the built-in dialog instead 1.91 + ParamBlock block; 1.92 + nsresult rv = block.Init(); 1.93 + if (NS_FAILED(rv)) 1.94 + return rv; 1.95 + 1.96 + block->SetInt(0, 0); 1.97 + return DoDialog(parent, block, webBrowserPrint, printSettings, kPrintDialogURL); 1.98 +} 1.99 + 1.100 +/* 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); */ 1.101 +NS_IMETHODIMP 1.102 +nsPrintingPromptService::ShowProgress(nsIDOMWindow* parent, 1.103 + nsIWebBrowserPrint* webBrowserPrint, // ok to be null 1.104 + nsIPrintSettings* printSettings, // ok to be null 1.105 + nsIObserver* openDialogObserver, // ok to be null 1.106 + bool isForPrinting, 1.107 + nsIWebProgressListener** webProgressListener, 1.108 + nsIPrintProgressParams** printProgressParams, 1.109 + bool* notifyOnOpen) 1.110 +{ 1.111 + NS_ENSURE_ARG(webProgressListener); 1.112 + NS_ENSURE_ARG(printProgressParams); 1.113 + NS_ENSURE_ARG(notifyOnOpen); 1.114 + 1.115 + *notifyOnOpen = false; 1.116 + 1.117 + nsPrintProgress* prtProgress = new nsPrintProgress(printSettings); 1.118 + mPrintProgress = prtProgress; 1.119 + mWebProgressListener = prtProgress; 1.120 + 1.121 + nsCOMPtr<nsIPrintProgressParams> prtProgressParams = new nsPrintProgressParams(); 1.122 + 1.123 + nsCOMPtr<nsIDOMWindow> parentWindow = parent; 1.124 + 1.125 + if (mWatcher && !parentWindow) { 1.126 + mWatcher->GetActiveWindow(getter_AddRefs(parentWindow)); 1.127 + } 1.128 + 1.129 + if (parentWindow) { 1.130 + mPrintProgress->OpenProgressDialog(parentWindow, 1.131 + isForPrinting ? kPrintProgressDialogURL : kPrtPrvProgressDialogURL, 1.132 + prtProgressParams, openDialogObserver, notifyOnOpen); 1.133 + } 1.134 + 1.135 + prtProgressParams.forget(printProgressParams); 1.136 + NS_ADDREF(*webProgressListener = this); 1.137 + 1.138 + return NS_OK; 1.139 +} 1.140 + 1.141 +/* void showPageSetup (in nsIDOMWindow parent, in nsIPrintSettings printSettings); */ 1.142 +NS_IMETHODIMP 1.143 +nsPrintingPromptService::ShowPageSetup(nsIDOMWindow *parent, nsIPrintSettings *printSettings, nsIObserver *aObs) 1.144 +{ 1.145 + NS_ENSURE_ARG(printSettings); 1.146 + 1.147 + // Try to access a component dialog 1.148 + nsCOMPtr<nsIPrintDialogService> dlgPrint(do_GetService( 1.149 + NS_PRINTDIALOGSERVICE_CONTRACTID)); 1.150 + if (dlgPrint) 1.151 + return dlgPrint->ShowPageSetup(parent, printSettings); 1.152 + 1.153 + ParamBlock block; 1.154 + nsresult rv = block.Init(); 1.155 + if (NS_FAILED(rv)) 1.156 + return rv; 1.157 + 1.158 + block->SetInt(0, 0); 1.159 + return DoDialog(parent, block, nullptr, printSettings, kPageSetupDialogURL); 1.160 +} 1.161 + 1.162 +/* void showPrinterProperties (in nsIDOMWindow parent, in wstring printerName, in nsIPrintSettings printSettings); */ 1.163 +NS_IMETHODIMP 1.164 +nsPrintingPromptService::ShowPrinterProperties(nsIDOMWindow *parent, const char16_t *printerName, nsIPrintSettings *printSettings) 1.165 +{ 1.166 + /* fixme: We simply ignore the |aPrinter| argument here 1.167 + * We should get the supported printer attributes from the printer and 1.168 + * populate the print job options dialog with these data instead of using 1.169 + * the "default set" here. 1.170 + * However, this requires changes on all platforms and is another big chunk 1.171 + * of patches ... ;-( 1.172 + */ 1.173 + NS_ENSURE_ARG(printerName); 1.174 + NS_ENSURE_ARG(printSettings); 1.175 + 1.176 + ParamBlock block; 1.177 + nsresult rv = block.Init(); 1.178 + if (NS_FAILED(rv)) 1.179 + return rv; 1.180 + 1.181 + block->SetInt(0, 0); 1.182 + return DoDialog(parent, block, nullptr, printSettings, kPrinterPropertiesURL); 1.183 + 1.184 +} 1.185 + 1.186 +nsresult 1.187 +nsPrintingPromptService::DoDialog(nsIDOMWindow *aParent, 1.188 + nsIDialogParamBlock *aParamBlock, 1.189 + nsIWebBrowserPrint *aWebBrowserPrint, 1.190 + nsIPrintSettings* aPS, 1.191 + const char *aChromeURL) 1.192 +{ 1.193 + NS_ENSURE_ARG(aParamBlock); 1.194 + NS_ENSURE_ARG(aPS); 1.195 + NS_ENSURE_ARG(aChromeURL); 1.196 + 1.197 + if (!mWatcher) 1.198 + return NS_ERROR_FAILURE; 1.199 + 1.200 + nsresult rv = NS_OK; 1.201 + 1.202 + // get a parent, if at all possible 1.203 + // (though we'd rather this didn't fail, it's OK if it does. so there's 1.204 + // no failure or null check.) 1.205 + nsCOMPtr<nsIDOMWindow> activeParent; // retain ownership for method lifetime 1.206 + if (!aParent) 1.207 + { 1.208 + mWatcher->GetActiveWindow(getter_AddRefs(activeParent)); 1.209 + aParent = activeParent; 1.210 + } 1.211 + 1.212 + // create a nsISupportsArray of the parameters 1.213 + // being passed to the window 1.214 + nsCOMPtr<nsISupportsArray> array; 1.215 + NS_NewISupportsArray(getter_AddRefs(array)); 1.216 + if (!array) return NS_ERROR_FAILURE; 1.217 + 1.218 + nsCOMPtr<nsISupports> psSupports(do_QueryInterface(aPS)); 1.219 + NS_ASSERTION(psSupports, "PrintSettings must be a supports"); 1.220 + array->AppendElement(psSupports); 1.221 + 1.222 + if (aWebBrowserPrint) { 1.223 + nsCOMPtr<nsISupports> wbpSupports(do_QueryInterface(aWebBrowserPrint)); 1.224 + NS_ASSERTION(wbpSupports, "nsIWebBrowserPrint must be a supports"); 1.225 + array->AppendElement(wbpSupports); 1.226 + } 1.227 + 1.228 + nsCOMPtr<nsISupports> blkSupps(do_QueryInterface(aParamBlock)); 1.229 + NS_ASSERTION(blkSupps, "IOBlk must be a supports"); 1.230 + array->AppendElement(blkSupps); 1.231 + 1.232 + nsCOMPtr<nsISupports> arguments(do_QueryInterface(array)); 1.233 + NS_ASSERTION(array, "array must be a supports"); 1.234 + 1.235 + 1.236 + nsCOMPtr<nsIDOMWindow> dialog; 1.237 + rv = mWatcher->OpenWindow(aParent, aChromeURL, "_blank", 1.238 + "centerscreen,chrome,modal,titlebar", arguments, 1.239 + getter_AddRefs(dialog)); 1.240 + 1.241 + // if aWebBrowserPrint is not null then we are printing 1.242 + // so we want to pass back NS_ERROR_ABORT on cancel 1.243 + if (NS_SUCCEEDED(rv) && aWebBrowserPrint) 1.244 + { 1.245 + int32_t status; 1.246 + aParamBlock->GetInt(0, &status); 1.247 + return status == 0?NS_ERROR_ABORT:NS_OK; 1.248 + } 1.249 + 1.250 + return rv; 1.251 +} 1.252 + 1.253 +////////////////////////////////////////////////////////////////////// 1.254 +// nsIWebProgressListener 1.255 +////////////////////////////////////////////////////////////////////// 1.256 + 1.257 +/* void onStateChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in unsigned long aStateFlags, in nsresult aStatus); */ 1.258 +NS_IMETHODIMP 1.259 +nsPrintingPromptService::OnStateChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, uint32_t aStateFlags, nsresult aStatus) 1.260 +{ 1.261 + if ((aStateFlags & STATE_STOP) && mWebProgressListener) { 1.262 + mWebProgressListener->OnStateChange(aWebProgress, aRequest, aStateFlags, aStatus); 1.263 + if (mPrintProgress) { 1.264 + mPrintProgress->CloseProgressDialog(true); 1.265 + } 1.266 + mPrintProgress = nullptr; 1.267 + mWebProgressListener = nullptr; 1.268 + } 1.269 + return NS_OK; 1.270 +} 1.271 + 1.272 +/* void onProgressChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in long aCurSelfProgress, in long aMaxSelfProgress, in long aCurTotalProgress, in long aMaxTotalProgress); */ 1.273 +NS_IMETHODIMP 1.274 +nsPrintingPromptService::OnProgressChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, int32_t aCurSelfProgress, int32_t aMaxSelfProgress, int32_t aCurTotalProgress, int32_t aMaxTotalProgress) 1.275 +{ 1.276 + if (mWebProgressListener) { 1.277 + return mWebProgressListener->OnProgressChange(aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress); 1.278 + } 1.279 + return NS_OK; 1.280 +} 1.281 + 1.282 +/* void onLocationChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in nsIURI location, in unsigned long aFlags); */ 1.283 +NS_IMETHODIMP 1.284 +nsPrintingPromptService::OnLocationChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, nsIURI *location, uint32_t aFlags) 1.285 +{ 1.286 + if (mWebProgressListener) { 1.287 + return mWebProgressListener->OnLocationChange(aWebProgress, aRequest, location, aFlags); 1.288 + } 1.289 + return NS_OK; 1.290 +} 1.291 + 1.292 +/* void onStatusChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in nsresult aStatus, in wstring aMessage); */ 1.293 +NS_IMETHODIMP 1.294 +nsPrintingPromptService::OnStatusChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, nsresult aStatus, const char16_t *aMessage) 1.295 +{ 1.296 + if (mWebProgressListener) { 1.297 + return mWebProgressListener->OnStatusChange(aWebProgress, aRequest, aStatus, aMessage); 1.298 + } 1.299 + return NS_OK; 1.300 +} 1.301 + 1.302 +/* void onSecurityChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in unsigned long state); */ 1.303 +NS_IMETHODIMP 1.304 +nsPrintingPromptService::OnSecurityChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, uint32_t state) 1.305 +{ 1.306 + if (mWebProgressListener) { 1.307 + return mWebProgressListener->OnSecurityChange(aWebProgress, aRequest, state); 1.308 + } 1.309 + return NS_OK; 1.310 +}