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 static const char *kPrintDialogURL         = "chrome://global/content/printdialog.xul";
00053 static const char *kPrintProgressDialogURL = "chrome://global/content/printProgress.xul";
00054 static const char *kPrtPrvProgressDialogURL = "chrome://global/content/printPreviewProgress.xul";
00055 static const char *kPageSetupDialogURL     = "chrome://global/content/printPageSetup.xul";
00056 static const char *kPrinterPropertiesURL   = "chrome://global/content/printjoboptions.xul";
00057  
00058 /****************************************************************
00059  ************************* ParamBlock ***************************
00060  ****************************************************************/
00061 
00062 class ParamBlock {
00063 
00064 public:
00065     ParamBlock() 
00066     {
00067         mBlock = 0;
00068     }
00069     ~ParamBlock() 
00070     {
00071         NS_IF_RELEASE(mBlock);
00072     }
00073     nsresult Init() {
00074       return CallCreateInstance(NS_DIALOGPARAMBLOCK_CONTRACTID, &mBlock);
00075     }
00076     nsIDialogParamBlock * operator->() const { return mBlock; }
00077     operator nsIDialogParamBlock * const ()  { return mBlock; }
00078 
00079 private:
00080     nsIDialogParamBlock *mBlock;
00081 };
00082 
00083 /****************************************************************
00084  ***************** nsPrintingPromptService **********************
00085  ****************************************************************/
00086 
00087 NS_IMPL_ISUPPORTS2(nsPrintingPromptService, nsIPrintingPromptService, nsIWebProgressListener)
00088 
00089 nsPrintingPromptService::nsPrintingPromptService() 
00090 {
00091 }
00092 
00093 nsPrintingPromptService::~nsPrintingPromptService() 
00094 {
00095 }
00096 
00097 nsresult
00098 nsPrintingPromptService::Init()
00099 {
00100     nsresult rv;
00101     mWatcher = do_GetService(NS_WINDOWWATCHER_CONTRACTID, &rv);
00102     return rv;
00103 }
00104 
00105 /* void showPrintDialog (in nsIDOMWindow parent, in nsIWebBrowserPrint webBrowserPrint, in nsIPrintSettings printSettings); */
00106 NS_IMETHODIMP 
00107 nsPrintingPromptService::ShowPrintDialog(nsIDOMWindow *parent, nsIWebBrowserPrint *webBrowserPrint, nsIPrintSettings *printSettings)
00108 {
00109     NS_ENSURE_ARG(webBrowserPrint);
00110     NS_ENSURE_ARG(printSettings);
00111 
00112     ParamBlock block;
00113     nsresult rv = block.Init();
00114     if (NS_FAILED(rv))
00115       return rv;
00116 
00117     block->SetInt(0, 0);
00118     return DoDialog(parent, block, webBrowserPrint, printSettings, kPrintDialogURL);
00119 }
00120 
00121 /* 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); */
00122 NS_IMETHODIMP 
00123 nsPrintingPromptService::ShowProgress(nsIDOMWindow*            parent, 
00124                                       nsIWebBrowserPrint*      webBrowserPrint,    // ok to be null
00125                                       nsIPrintSettings*        printSettings,      // ok to be null
00126                                       nsIObserver*             openDialogObserver, // ok to be null
00127                                       PRBool                   isForPrinting,
00128                                       nsIWebProgressListener** webProgressListener,
00129                                       nsIPrintProgressParams** printProgressParams, 
00130                                       PRBool*                  notifyOnOpen)
00131 {
00132     NS_ENSURE_ARG(webProgressListener);
00133     NS_ENSURE_ARG(printProgressParams);
00134     NS_ENSURE_ARG(notifyOnOpen);
00135 
00136     *notifyOnOpen = PR_FALSE;
00137 
00138     nsPrintProgress* prtProgress = new nsPrintProgress();
00139     nsresult rv = prtProgress->QueryInterface(NS_GET_IID(nsIPrintProgress), (void**)getter_AddRefs(mPrintProgress));
00140     NS_ENSURE_SUCCESS(rv, rv);
00141 
00142     rv = prtProgress->QueryInterface(NS_GET_IID(nsIWebProgressListener), (void**)getter_AddRefs(mWebProgressListener));
00143     NS_ENSURE_SUCCESS(rv, rv);
00144 
00145     nsPrintProgressParams* prtProgressParams = new nsPrintProgressParams();
00146     rv = prtProgressParams->QueryInterface(NS_GET_IID(nsIPrintProgressParams), (void**)printProgressParams);
00147     NS_ENSURE_SUCCESS(rv, rv);
00148 
00149     if (printProgressParams) 
00150     {
00151         nsCOMPtr<nsIDOMWindowInternal> parentDOMIntl(do_QueryInterface(parent));
00152 
00153         if (mWatcher && !parentDOMIntl) 
00154         {
00155             nsCOMPtr<nsIDOMWindow> active;
00156             mWatcher->GetActiveWindow(getter_AddRefs(active));
00157             parentDOMIntl = do_QueryInterface(active);
00158         }
00159 
00160         if (parentDOMIntl) 
00161         {
00162             mPrintProgress->OpenProgressDialog(parentDOMIntl, 
00163                                                isForPrinting?kPrintProgressDialogURL:kPrtPrvProgressDialogURL, 
00164                                                *printProgressParams, openDialogObserver, notifyOnOpen);
00165         }
00166     }
00167 
00168     *webProgressListener = NS_STATIC_CAST(nsIWebProgressListener*, this);
00169     NS_ADDREF(*webProgressListener);
00170 
00171     return rv;
00172 }
00173 
00174 /* void showPageSetup (in nsIDOMWindow parent, in nsIPrintSettings printSettings); */
00175 NS_IMETHODIMP 
00176 nsPrintingPromptService::ShowPageSetup(nsIDOMWindow *parent, nsIPrintSettings *printSettings, nsIObserver *aObs)
00177 {
00178     NS_ENSURE_ARG(printSettings);
00179 
00180     ParamBlock block;
00181     nsresult rv = block.Init();
00182     if (NS_FAILED(rv))
00183       return rv;
00184 
00185     block->SetInt(0, 0);
00186     return DoDialog(parent, block, nsnull, printSettings, kPageSetupDialogURL);
00187 }
00188 
00189 /* void showPrinterProperties (in nsIDOMWindow parent, in wstring printerName, in nsIPrintSettings printSettings); */
00190 NS_IMETHODIMP 
00191 nsPrintingPromptService::ShowPrinterProperties(nsIDOMWindow *parent, const PRUnichar *printerName, nsIPrintSettings *printSettings)
00192 {
00193     /* fixme: We simply ignore the |aPrinter| argument here
00194      * We should get the supported printer attributes from the printer and 
00195      * populate the print job options dialog with these data instead of using 
00196      * the "default set" here.
00197      * However, this requires changes on all platforms and is another big chunk
00198      * of patches ... ;-(
00199      */
00200     NS_ENSURE_ARG(printerName);
00201     NS_ENSURE_ARG(printSettings);
00202 
00203     ParamBlock block;
00204     nsresult rv = block.Init();
00205     if (NS_FAILED(rv))
00206       return rv;
00207 
00208     block->SetInt(0, 0);
00209     return DoDialog(parent, block, nsnull, printSettings, kPrinterPropertiesURL);
00210    
00211 }
00212 
00213 nsresult
00214 nsPrintingPromptService::DoDialog(nsIDOMWindow *aParent,
00215                                   nsIDialogParamBlock *aParamBlock, 
00216                                   nsIWebBrowserPrint *aWebBrowserPrint, 
00217                                   nsIPrintSettings* aPS,
00218                                   const char *aChromeURL)
00219 {
00220     NS_ENSURE_ARG(aParamBlock);
00221     NS_ENSURE_ARG(aPS);
00222     NS_ENSURE_ARG(aChromeURL);
00223 
00224     if (!mWatcher)
00225         return NS_ERROR_FAILURE;
00226 
00227     nsresult rv = NS_OK;
00228 
00229     // get a parent, if at all possible
00230     // (though we'd rather this didn't fail, it's OK if it does. so there's
00231     // no failure or null check.)
00232     nsCOMPtr<nsIDOMWindow> activeParent; // retain ownership for method lifetime
00233     if (!aParent) 
00234     {
00235         mWatcher->GetActiveWindow(getter_AddRefs(activeParent));
00236         aParent = activeParent;
00237     }
00238 
00239     // create a nsISupportsArray of the parameters 
00240     // being passed to the window
00241     nsCOMPtr<nsISupportsArray> array;
00242     NS_NewISupportsArray(getter_AddRefs(array));
00243     if (!array) return NS_ERROR_FAILURE;
00244 
00245     nsCOMPtr<nsISupports> psSupports(do_QueryInterface(aPS));
00246     NS_ASSERTION(psSupports, "PrintSettings must be a supports");
00247     array->AppendElement(psSupports);
00248 
00249     if (aWebBrowserPrint) {
00250       nsCOMPtr<nsISupports> wbpSupports(do_QueryInterface(aWebBrowserPrint));
00251       NS_ASSERTION(wbpSupports, "nsIWebBrowserPrint must be a supports");
00252       array->AppendElement(wbpSupports);
00253     }
00254 
00255     nsCOMPtr<nsISupports> blkSupps(do_QueryInterface(aParamBlock));
00256     NS_ASSERTION(blkSupps, "IOBlk must be a supports");
00257     array->AppendElement(blkSupps);
00258 
00259     nsCOMPtr<nsISupports> arguments(do_QueryInterface(array));
00260     NS_ASSERTION(array, "array must be a supports");
00261 
00262 
00263     nsCOMPtr<nsIDOMWindow> dialog;
00264     rv = mWatcher->OpenWindow(aParent, aChromeURL, "_blank",
00265                               "centerscreen,chrome,modal,titlebar", arguments,
00266                               getter_AddRefs(dialog));
00267 
00268     // if aWebBrowserPrint is not null then we are printing
00269     // so we want to pass back NS_ERROR_ABORT on cancel
00270     if (NS_SUCCEEDED(rv) && aWebBrowserPrint) 
00271     {
00272         PRInt32 status;
00273         aParamBlock->GetInt(0, &status);
00274         return status == 0?NS_ERROR_ABORT:NS_OK;
00275     }
00276 
00277     return rv;
00278 }
00279 
00281 // nsIWebProgressListener
00283 
00284 /* void onStateChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in unsigned long aStateFlags, in nsresult aStatus); */
00285 NS_IMETHODIMP 
00286 nsPrintingPromptService::OnStateChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, PRUint32 aStateFlags, nsresult aStatus)
00287 {
00288   if ((aStateFlags & STATE_STOP) && mWebProgressListener) {
00289     mWebProgressListener->OnStateChange(aWebProgress, aRequest, aStateFlags, aStatus);
00290     if (mPrintProgress) {
00291       mPrintProgress->CloseProgressDialog(PR_TRUE);
00292     }
00293     mPrintProgress       = nsnull;
00294     mWebProgressListener = nsnull;
00295   }
00296   return NS_OK;
00297 }
00298 
00299 /* void onProgressChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in long aCurSelfProgress, in long aMaxSelfProgress, in long aCurTotalProgress, in long aMaxTotalProgress); */
00300 NS_IMETHODIMP 
00301 nsPrintingPromptService::OnProgressChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, PRInt32 aCurSelfProgress, PRInt32 aMaxSelfProgress, PRInt32 aCurTotalProgress, PRInt32 aMaxTotalProgress)
00302 {
00303   if (mWebProgressListener) {
00304     return mWebProgressListener->OnProgressChange(aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress);
00305   }
00306   return NS_OK;
00307 }
00308 
00309 /* void onLocationChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in nsIURI location); */
00310 NS_IMETHODIMP 
00311 nsPrintingPromptService::OnLocationChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, nsIURI *location)
00312 {
00313   if (mWebProgressListener) {
00314     return mWebProgressListener->OnLocationChange(aWebProgress, aRequest, location);
00315   }
00316   return NS_OK;
00317 }
00318 
00319 /* void onStatusChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in nsresult aStatus, in wstring aMessage); */
00320 NS_IMETHODIMP 
00321 nsPrintingPromptService::OnStatusChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, nsresult aStatus, const PRUnichar *aMessage)
00322 {
00323   if (mWebProgressListener) {
00324     return mWebProgressListener->OnStatusChange(aWebProgress, aRequest, aStatus, aMessage);
00325   }
00326   return NS_OK;
00327 }
00328 
00329 /* void onSecurityChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in unsigned long state); */
00330 NS_IMETHODIMP 
00331 nsPrintingPromptService::OnSecurityChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, PRUint32 state)
00332 {
00333   if (mWebProgressListener) {
00334     return mWebProgressListener->OnSecurityChange(aWebProgress, aRequest, state);
00335   }
00336   return NS_OK;
00337 }