Back to index

lightning-sunbird  0.9+nobinonly
nsPrintingPromptService.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "nsPrintingPromptService.h"
00039 
00040 #include "nsIComponentManager.h"
00041 #include "nsIDialogParamBlock.h"
00042 #include "nsIDOMWindow.h"
00043 #include "nsIServiceManager.h"
00044 #include "nsISupportsUtils.h"
00045 #include "nsISupportsArray.h"
00046 #include "nsString.h"
00047 
00048 // Printing Progress Includes
00049 #include "nsPrintProgress.h"
00050 #include "nsPrintProgressParams.h"
00051 
00052 // Print Service Includes
00053 #include "nsIPrintOptions.h"
00054 #include "nsIServiceManager.h"
00055 #include "nsGfxCIID.h"
00056 static const char sPrintOptionsContractID[] = "@mozilla.org/gfx/printsettings-service;1";
00057 
00058 static const char *kPrintDialogURL         = "chrome://global/content/printdialog.xul";
00059 static const char *kPrintProgressDialogURL = "chrome://global/content/printProgress.xul";
00060 static const char *kPrtPrvProgressDialogURL = "chrome://global/content/printPreviewProgress.xul";
00061 static const char *kPageSetupDialogURL     = "chrome://global/content/printPageSetup.xul";
00062 
00063 /****************************************************************
00064  ************************* ParamBlock ***************************
00065  ****************************************************************/
00066 
00067 class ParamBlock {
00068 
00069 public:
00070     ParamBlock() 
00071     {
00072         mBlock = 0;
00073     }
00074     ~ParamBlock() 
00075     {
00076         NS_IF_RELEASE(mBlock);
00077     }
00078     nsresult Init() {
00079       return CallCreateInstance(NS_DIALOGPARAMBLOCK_CONTRACTID, &mBlock);
00080     }
00081     nsIDialogParamBlock * operator->() const { return mBlock; }
00082     operator nsIDialogParamBlock * const ()  { return mBlock; }
00083 
00084 private:
00085     nsIDialogParamBlock *mBlock;
00086 };
00087 
00088 /****************************************************************
00089  ***************** nsPrintingPromptService **********************
00090  ****************************************************************/
00091 
00092 NS_IMPL_ISUPPORTS2(nsPrintingPromptService, nsIPrintingPromptService, nsIWebProgressListener)
00093 
00094 nsPrintingPromptService::nsPrintingPromptService() 
00095 {
00096 }
00097 
00098 nsPrintingPromptService::~nsPrintingPromptService() 
00099 {
00100 }
00101 
00102 nsresult
00103 nsPrintingPromptService::Init()
00104 {
00105     nsresult rv;
00106     mWatcher = do_GetService(NS_WINDOWWATCHER_CONTRACTID, &rv);
00107     return rv;
00108 }
00109 
00110 /* void showPrintDialog (in nsIDOMWindow parent, in nsIWebBrowserPrint webBrowserPrint, in nsIPrintSettings printSettings); */
00111 NS_IMETHODIMP 
00112 nsPrintingPromptService::ShowPrintDialog(nsIDOMWindow *parent, nsIWebBrowserPrint *webBrowserPrint, nsIPrintSettings *printSettings)
00113 {
00114     NS_ENSURE_ARG(webBrowserPrint);
00115     NS_ENSURE_ARG(printSettings);
00116 
00117     ParamBlock block;
00118     nsresult rv = block.Init();
00119     if (NS_FAILED(rv))
00120       return rv;
00121 
00122     block->SetInt(0, 0);
00123     return DoDialog(parent, block, webBrowserPrint, printSettings, kPrintDialogURL);
00124 }
00125 
00126 /* 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); */
00127 NS_IMETHODIMP 
00128 nsPrintingPromptService::ShowProgress(nsIDOMWindow*           parent, 
00129                                       nsIWebBrowserPrint*      webBrowserPrint,    // ok to be null
00130                                       nsIPrintSettings*        printSettings,      // ok to be null
00131                                       nsIObserver*             openDialogObserver, // ok to be null
00132                                       PRBool                   isForPrinting,
00133                                       nsIWebProgressListener** webProgressListener,
00134                                       nsIPrintProgressParams** printProgressParams,
00135                                       PRBool*                  notifyOnOpen)
00136 {
00137     NS_ENSURE_ARG(webProgressListener);
00138     NS_ENSURE_ARG(printProgressParams);
00139     NS_ENSURE_ARG(notifyOnOpen);
00140 
00141     *notifyOnOpen = PR_FALSE;
00142 
00143     nsPrintProgress* prtProgress = new nsPrintProgress();
00144     nsresult rv = prtProgress->QueryInterface(NS_GET_IID(nsIPrintProgress), (void**)getter_AddRefs(mPrintProgress));
00145     NS_ENSURE_SUCCESS(rv, rv);
00146 
00147     rv = prtProgress->QueryInterface(NS_GET_IID(nsIWebProgressListener), (void**)getter_AddRefs(mWebProgressListener));
00148     NS_ENSURE_SUCCESS(rv, rv);
00149 
00150     nsPrintProgressParams* prtProgressParams = new nsPrintProgressParams();
00151     rv = prtProgressParams->QueryInterface(NS_GET_IID(nsIPrintProgressParams), (void**)printProgressParams);
00152     NS_ENSURE_SUCCESS(rv, rv);
00153 
00154     if (printProgressParams) 
00155     {
00156         nsCOMPtr<nsIDOMWindowInternal> parentDOMIntl(do_QueryInterface(parent));
00157 
00158         if (mWatcher && !parentDOMIntl) 
00159         {
00160             nsCOMPtr<nsIDOMWindow> active;
00161             mWatcher->GetActiveWindow(getter_AddRefs(active));
00162             parentDOMIntl = do_QueryInterface(active);
00163         }
00164 
00165         if (parentDOMIntl) 
00166         {
00167             mPrintProgress->OpenProgressDialog(parentDOMIntl, 
00168                                                isForPrinting?kPrintProgressDialogURL:kPrtPrvProgressDialogURL, 
00169                                                *printProgressParams, openDialogObserver, notifyOnOpen);
00170         }
00171     }
00172 
00173     *webProgressListener = NS_STATIC_CAST(nsIWebProgressListener*, this);
00174     NS_ADDREF(*webProgressListener);
00175 
00176     return rv;
00177 }
00178 
00179 /* void showPageSetup (in nsIDOMWindow parent, in nsIPrintSettings printSettings); */
00180 NS_IMETHODIMP 
00181 nsPrintingPromptService::ShowPageSetup(nsIDOMWindow *parent, nsIPrintSettings *printSettings, nsIObserver *aObs)
00182 {
00183     NS_ENSURE_ARG(printSettings);
00184 
00185     ParamBlock block;
00186     nsresult rv = block.Init();
00187     if (NS_FAILED(rv))
00188       return rv;
00189 
00190     block->SetInt(0, 0);
00191     return DoDialog(parent, block, nsnull, printSettings, kPageSetupDialogURL);
00192 }
00193 
00194 /* void showPrinterProperties (in nsIDOMWindow parent, in wstring printerName, in nsIPrintSettings printSettings); */
00195 NS_IMETHODIMP 
00196 nsPrintingPromptService::ShowPrinterProperties(nsIDOMWindow *parent, const PRUnichar *printerName, nsIPrintSettings *printSettings)
00197 {
00198   nsresult rv = NS_ERROR_FAILURE;
00199   nsCOMPtr<nsIPrintOptions> printService = do_GetService(sPrintOptionsContractID, &rv);
00200   if (NS_SUCCEEDED(rv)) {
00201     PRBool displayed;
00202     rv = printService->DisplayJobProperties(printerName, printSettings, &displayed);
00203   }
00204   return rv;
00205 }
00206 
00207 nsresult
00208 nsPrintingPromptService::DoDialog(nsIDOMWindow *aParent,
00209                                   nsIDialogParamBlock *aParamBlock, 
00210                                   nsIWebBrowserPrint *aWebBrowserPrint, 
00211                                   nsIPrintSettings* aPS,
00212                                   const char *aChromeURL)
00213 {
00214     NS_ENSURE_ARG(aParamBlock);
00215     NS_ENSURE_ARG(aPS);
00216     NS_ENSURE_ARG(aChromeURL);
00217 
00218     if (!mWatcher)
00219         return NS_ERROR_FAILURE;
00220 
00221     nsresult rv = NS_OK;
00222 
00223     // get a parent, if at all possible
00224     // (though we'd rather this didn't fail, it's OK if it does. so there's
00225     // no failure or null check.)
00226     nsCOMPtr<nsIDOMWindow> activeParent; // retain ownership for method lifetime
00227     if (!aParent) 
00228     {
00229         mWatcher->GetActiveWindow(getter_AddRefs(activeParent));
00230         aParent = activeParent;
00231     }
00232 
00233     // create a nsISupportsArray of the parameters 
00234     // being passed to the window
00235     nsCOMPtr<nsISupportsArray> array;
00236     NS_NewISupportsArray(getter_AddRefs(array));
00237     if (!array) return NS_ERROR_FAILURE;
00238 
00239     nsCOMPtr<nsISupports> psSupports(do_QueryInterface(aPS));
00240     NS_ASSERTION(psSupports, "PrintSettings must be a supports");
00241     array->AppendElement(psSupports);
00242 
00243     if (aWebBrowserPrint) {
00244       nsCOMPtr<nsISupports> wbpSupports(do_QueryInterface(aWebBrowserPrint));
00245       NS_ASSERTION(wbpSupports, "nsIWebBrowserPrint must be a supports");
00246       array->AppendElement(wbpSupports);
00247     }
00248 
00249     nsCOMPtr<nsISupports> blkSupps(do_QueryInterface(aParamBlock));
00250     NS_ASSERTION(blkSupps, "IOBlk must be a supports");
00251     array->AppendElement(blkSupps);
00252 
00253     nsCOMPtr<nsISupports> arguments(do_QueryInterface(array));
00254     NS_ASSERTION(array, "array must be a supports");
00255 
00256 
00257     nsCOMPtr<nsIDOMWindow> dialog;
00258     rv = mWatcher->OpenWindow(aParent, aChromeURL, "_blank",
00259                               "centerscreen,chrome,modal,titlebar", arguments,
00260                               getter_AddRefs(dialog));
00261 
00262     // if aWebBrowserPrint is not null then we are printing
00263     // so we want to pass back NS_ERROR_ABORT on cancel
00264     if (NS_SUCCEEDED(rv) && aWebBrowserPrint) 
00265     {
00266         PRInt32 status;
00267         aParamBlock->GetInt(0, &status);
00268         return status == 0?NS_ERROR_ABORT:NS_OK;
00269     }
00270 
00271     return rv;
00272 }
00273 
00275 // nsIWebProgressListener
00277 
00278 /* void onStateChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in unsigned long aStateFlags, in nsresult aStatus); */
00279 NS_IMETHODIMP 
00280 nsPrintingPromptService::OnStateChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, PRUint32 aStateFlags, nsresult aStatus)
00281 {
00282   if ((aStateFlags & STATE_STOP) && mWebProgressListener) {
00283     mWebProgressListener->OnStateChange(aWebProgress, aRequest, aStateFlags, aStatus);
00284     if (mPrintProgress) {
00285       mPrintProgress->CloseProgressDialog(PR_TRUE);
00286     }
00287     mPrintProgress       = nsnull;
00288     mWebProgressListener = nsnull;
00289   }
00290   return NS_OK;
00291 }
00292 
00293 /* void onProgressChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in long aCurSelfProgress, in long aMaxSelfProgress, in long aCurTotalProgress, in long aMaxTotalProgress); */
00294 NS_IMETHODIMP 
00295 nsPrintingPromptService::OnProgressChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, PRInt32 aCurSelfProgress, PRInt32 aMaxSelfProgress, PRInt32 aCurTotalProgress, PRInt32 aMaxTotalProgress)
00296 {
00297   if (mWebProgressListener) {
00298     return mWebProgressListener->OnProgressChange(aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress);
00299   }
00300   return NS_OK;
00301 }
00302 
00303 /* void onLocationChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in nsIURI location); */
00304 NS_IMETHODIMP 
00305 nsPrintingPromptService::OnLocationChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, nsIURI *location)
00306 {
00307   if (mWebProgressListener) {
00308     return mWebProgressListener->OnLocationChange(aWebProgress, aRequest, location);
00309   }
00310   return NS_OK;
00311 }
00312 
00313 /* void onStatusChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in nsresult aStatus, in wstring aMessage); */
00314 NS_IMETHODIMP 
00315 nsPrintingPromptService::OnStatusChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, nsresult aStatus, const PRUnichar *aMessage)
00316 {
00317   if (mWebProgressListener) {
00318     return mWebProgressListener->OnStatusChange(aWebProgress, aRequest, aStatus, aMessage);
00319   }
00320   return NS_OK;
00321 }
00322 
00323 /* void onSecurityChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in unsigned long state); */
00324 NS_IMETHODIMP 
00325 nsPrintingPromptService::OnSecurityChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, PRUint32 state)
00326 {
00327   if (mWebProgressListener) {
00328     return mWebProgressListener->OnSecurityChange(aWebProgress, aRequest, state);
00329   }
00330   return NS_OK;
00331 }