PageRenderTime 40ms CodeModel.GetById 13ms app.highlight 23ms RepoModel.GetById 1ms app.codeStats 0ms

/embedding/components/printingui/src/unixshared/nsPrintingPromptService.cpp

http://github.com/zpao/v8monkey
C++ | 339 lines | 214 code | 53 blank | 72 comment | 22 complexity | 0b2addeb8ae22043b3ccf042581181a8 MD5 | raw file
  1/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2/* ***** BEGIN LICENSE BLOCK *****
  3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4 *
  5 * The contents of this file are subject to the Mozilla Public License Version
  6 * 1.1 (the "License"); you may not use this file except in compliance with
  7 * the License. You may obtain a copy of the License at
  8 * http://www.mozilla.org/MPL/
  9 *
 10 * Software distributed under the License is distributed on an "AS IS" basis,
 11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 12 * for the specific language governing rights and limitations under the
 13 * License.
 14 *
 15 * The Original Code is mozilla.org code.
 16 *
 17 * The Initial Developer of the Original Code is
 18 * Netscape Communications Corporation.
 19 * Portions created by the Initial Developer are Copyright (C) 2001
 20 * the Initial Developer. All Rights Reserved.
 21 *
 22 * Contributor(s):
 23 *
 24 * Alternatively, the contents of this file may be used under the terms of
 25 * either the GNU General Public License Version 2 or later (the "GPL"), or
 26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 27 * in which case the provisions of the GPL or the LGPL are applicable instead
 28 * of those above. If you wish to allow use of your version of this file only
 29 * under the terms of either the GPL or the LGPL, and not to allow others to
 30 * use your version of this file under the terms of the MPL, indicate your
 31 * decision by deleting the provisions above and replace them with the notice
 32 * and other provisions required by the GPL or the LGPL. If you do not delete
 33 * the provisions above, a recipient may use your version of this file under
 34 * the terms of any one of the MPL, the GPL or the LGPL.
 35 *
 36 * ***** END LICENSE BLOCK ***** */
 37
 38#include "nsPrintingPromptService.h"
 39
 40#include "nsIComponentManager.h"
 41#include "nsIDialogParamBlock.h"
 42#include "nsIDOMWindow.h"
 43#include "nsIServiceManager.h"
 44#include "nsISupportsUtils.h"
 45#include "nsISupportsArray.h"
 46#include "nsString.h"
 47#include "nsIPrintDialogService.h"
 48
 49// Printing Progress Includes
 50#include "nsPrintProgress.h"
 51#include "nsPrintProgressParams.h"
 52
 53static const char *kPrintDialogURL         = "chrome://global/content/printdialog.xul";
 54static const char *kPrintProgressDialogURL = "chrome://global/content/printProgress.xul";
 55static const char *kPrtPrvProgressDialogURL = "chrome://global/content/printPreviewProgress.xul";
 56static const char *kPageSetupDialogURL     = "chrome://global/content/printPageSetup.xul";
 57static const char *kPrinterPropertiesURL   = "chrome://global/content/printjoboptions.xul";
 58 
 59/****************************************************************
 60 ************************* ParamBlock ***************************
 61 ****************************************************************/
 62
 63class ParamBlock {
 64
 65public:
 66    ParamBlock() 
 67    {
 68        mBlock = 0;
 69    }
 70    ~ParamBlock() 
 71    {
 72        NS_IF_RELEASE(mBlock);
 73    }
 74    nsresult Init() {
 75      return CallCreateInstance(NS_DIALOGPARAMBLOCK_CONTRACTID, &mBlock);
 76    }
 77    nsIDialogParamBlock * operator->() const { return mBlock; }
 78    operator nsIDialogParamBlock * const ()  { return mBlock; }
 79
 80private:
 81    nsIDialogParamBlock *mBlock;
 82};
 83
 84/****************************************************************
 85 ***************** nsPrintingPromptService **********************
 86 ****************************************************************/
 87
 88NS_IMPL_ISUPPORTS2(nsPrintingPromptService, nsIPrintingPromptService, nsIWebProgressListener)
 89
 90nsPrintingPromptService::nsPrintingPromptService() 
 91{
 92}
 93
 94nsPrintingPromptService::~nsPrintingPromptService() 
 95{
 96}
 97
 98nsresult
 99nsPrintingPromptService::Init()
100{
101    nsresult rv;
102    mWatcher = do_GetService(NS_WINDOWWATCHER_CONTRACTID, &rv);
103    return rv;
104}
105
106/* void showPrintDialog (in nsIDOMWindow parent, in nsIWebBrowserPrint webBrowserPrint, in nsIPrintSettings printSettings); */
107NS_IMETHODIMP 
108nsPrintingPromptService::ShowPrintDialog(nsIDOMWindow *parent, nsIWebBrowserPrint *webBrowserPrint, nsIPrintSettings *printSettings)
109{
110    NS_ENSURE_ARG(webBrowserPrint);
111    NS_ENSURE_ARG(printSettings);
112
113    // Try to access a component dialog
114    nsCOMPtr<nsIPrintDialogService> dlgPrint(do_GetService(
115                                             NS_PRINTDIALOGSERVICE_CONTRACTID));
116    if (dlgPrint)
117      return dlgPrint->Show(parent, printSettings, webBrowserPrint);
118
119    // Show the built-in dialog instead
120    ParamBlock block;
121    nsresult rv = block.Init();
122    if (NS_FAILED(rv))
123      return rv;
124
125    block->SetInt(0, 0);
126    return DoDialog(parent, block, webBrowserPrint, printSettings, kPrintDialogURL);
127}
128
129/* 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); */
130NS_IMETHODIMP 
131nsPrintingPromptService::ShowProgress(nsIDOMWindow*            parent, 
132                                      nsIWebBrowserPrint*      webBrowserPrint,    // ok to be null
133                                      nsIPrintSettings*        printSettings,      // ok to be null
134                                      nsIObserver*             openDialogObserver, // ok to be null
135                                      bool                     isForPrinting,
136                                      nsIWebProgressListener** webProgressListener,
137                                      nsIPrintProgressParams** printProgressParams, 
138                                      bool*                  notifyOnOpen)
139{
140    NS_ENSURE_ARG(webProgressListener);
141    NS_ENSURE_ARG(printProgressParams);
142    NS_ENSURE_ARG(notifyOnOpen);
143
144    *notifyOnOpen = false;
145
146    nsPrintProgress* prtProgress = new nsPrintProgress(printSettings);
147    mPrintProgress = prtProgress;
148    mWebProgressListener = prtProgress;
149
150    nsCOMPtr<nsIPrintProgressParams> prtProgressParams = new nsPrintProgressParams();
151
152    nsCOMPtr<nsIDOMWindow> parentWindow = parent;
153
154    if (mWatcher && !parentWindow) {
155        mWatcher->GetActiveWindow(getter_AddRefs(parentWindow));
156    }
157
158    if (parentWindow) {
159        mPrintProgress->OpenProgressDialog(parentWindow,
160                                           isForPrinting ? kPrintProgressDialogURL : kPrtPrvProgressDialogURL,
161                                           prtProgressParams, openDialogObserver, notifyOnOpen);
162    }
163
164    prtProgressParams.forget(printProgressParams);
165    NS_ADDREF(*webProgressListener = this);
166
167    return NS_OK;
168}
169
170/* void showPageSetup (in nsIDOMWindow parent, in nsIPrintSettings printSettings); */
171NS_IMETHODIMP 
172nsPrintingPromptService::ShowPageSetup(nsIDOMWindow *parent, nsIPrintSettings *printSettings, nsIObserver *aObs)
173{
174    NS_ENSURE_ARG(printSettings);
175
176    // Try to access a component dialog
177    nsCOMPtr<nsIPrintDialogService> dlgPrint(do_GetService(
178                                             NS_PRINTDIALOGSERVICE_CONTRACTID));
179    if (dlgPrint)
180      return dlgPrint->ShowPageSetup(parent, printSettings);
181
182    ParamBlock block;
183    nsresult rv = block.Init();
184    if (NS_FAILED(rv))
185      return rv;
186
187    block->SetInt(0, 0);
188    return DoDialog(parent, block, nsnull, printSettings, kPageSetupDialogURL);
189}
190
191/* void showPrinterProperties (in nsIDOMWindow parent, in wstring printerName, in nsIPrintSettings printSettings); */
192NS_IMETHODIMP 
193nsPrintingPromptService::ShowPrinterProperties(nsIDOMWindow *parent, const PRUnichar *printerName, nsIPrintSettings *printSettings)
194{
195    /* fixme: We simply ignore the |aPrinter| argument here
196     * We should get the supported printer attributes from the printer and 
197     * populate the print job options dialog with these data instead of using 
198     * the "default set" here.
199     * However, this requires changes on all platforms and is another big chunk
200     * of patches ... ;-(
201     */
202    NS_ENSURE_ARG(printerName);
203    NS_ENSURE_ARG(printSettings);
204
205    ParamBlock block;
206    nsresult rv = block.Init();
207    if (NS_FAILED(rv))
208      return rv;
209
210    block->SetInt(0, 0);
211    return DoDialog(parent, block, nsnull, printSettings, kPrinterPropertiesURL);
212   
213}
214
215nsresult
216nsPrintingPromptService::DoDialog(nsIDOMWindow *aParent,
217                                  nsIDialogParamBlock *aParamBlock, 
218                                  nsIWebBrowserPrint *aWebBrowserPrint, 
219                                  nsIPrintSettings* aPS,
220                                  const char *aChromeURL)
221{
222    NS_ENSURE_ARG(aParamBlock);
223    NS_ENSURE_ARG(aPS);
224    NS_ENSURE_ARG(aChromeURL);
225
226    if (!mWatcher)
227        return NS_ERROR_FAILURE;
228
229    nsresult rv = NS_OK;
230
231    // get a parent, if at all possible
232    // (though we'd rather this didn't fail, it's OK if it does. so there's
233    // no failure or null check.)
234    nsCOMPtr<nsIDOMWindow> activeParent; // retain ownership for method lifetime
235    if (!aParent) 
236    {
237        mWatcher->GetActiveWindow(getter_AddRefs(activeParent));
238        aParent = activeParent;
239    }
240
241    // create a nsISupportsArray of the parameters 
242    // being passed to the window
243    nsCOMPtr<nsISupportsArray> array;
244    NS_NewISupportsArray(getter_AddRefs(array));
245    if (!array) return NS_ERROR_FAILURE;
246
247    nsCOMPtr<nsISupports> psSupports(do_QueryInterface(aPS));
248    NS_ASSERTION(psSupports, "PrintSettings must be a supports");
249    array->AppendElement(psSupports);
250
251    if (aWebBrowserPrint) {
252      nsCOMPtr<nsISupports> wbpSupports(do_QueryInterface(aWebBrowserPrint));
253      NS_ASSERTION(wbpSupports, "nsIWebBrowserPrint must be a supports");
254      array->AppendElement(wbpSupports);
255    }
256
257    nsCOMPtr<nsISupports> blkSupps(do_QueryInterface(aParamBlock));
258    NS_ASSERTION(blkSupps, "IOBlk must be a supports");
259    array->AppendElement(blkSupps);
260
261    nsCOMPtr<nsISupports> arguments(do_QueryInterface(array));
262    NS_ASSERTION(array, "array must be a supports");
263
264
265    nsCOMPtr<nsIDOMWindow> dialog;
266    rv = mWatcher->OpenWindow(aParent, aChromeURL, "_blank",
267                              "centerscreen,chrome,modal,titlebar", arguments,
268                              getter_AddRefs(dialog));
269
270    // if aWebBrowserPrint is not null then we are printing
271    // so we want to pass back NS_ERROR_ABORT on cancel
272    if (NS_SUCCEEDED(rv) && aWebBrowserPrint) 
273    {
274        PRInt32 status;
275        aParamBlock->GetInt(0, &status);
276        return status == 0?NS_ERROR_ABORT:NS_OK;
277    }
278
279    return rv;
280}
281
282//////////////////////////////////////////////////////////////////////
283// nsIWebProgressListener
284//////////////////////////////////////////////////////////////////////
285
286/* void onStateChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in unsigned long aStateFlags, in nsresult aStatus); */
287NS_IMETHODIMP 
288nsPrintingPromptService::OnStateChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, PRUint32 aStateFlags, nsresult aStatus)
289{
290  if ((aStateFlags & STATE_STOP) && mWebProgressListener) {
291    mWebProgressListener->OnStateChange(aWebProgress, aRequest, aStateFlags, aStatus);
292    if (mPrintProgress) {
293      mPrintProgress->CloseProgressDialog(true);
294    }
295    mPrintProgress       = nsnull;
296    mWebProgressListener = nsnull;
297  }
298  return NS_OK;
299}
300
301/* void onProgressChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in long aCurSelfProgress, in long aMaxSelfProgress, in long aCurTotalProgress, in long aMaxTotalProgress); */
302NS_IMETHODIMP 
303nsPrintingPromptService::OnProgressChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, PRInt32 aCurSelfProgress, PRInt32 aMaxSelfProgress, PRInt32 aCurTotalProgress, PRInt32 aMaxTotalProgress)
304{
305  if (mWebProgressListener) {
306    return mWebProgressListener->OnProgressChange(aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress);
307  }
308  return NS_OK;
309}
310
311/* void onLocationChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in nsIURI location, in unsigned long aFlags); */
312NS_IMETHODIMP 
313nsPrintingPromptService::OnLocationChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, nsIURI *location, PRUint32 aFlags)
314{
315  if (mWebProgressListener) {
316    return mWebProgressListener->OnLocationChange(aWebProgress, aRequest, location, aFlags);
317  }
318  return NS_OK;
319}
320
321/* void onStatusChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in nsresult aStatus, in wstring aMessage); */
322NS_IMETHODIMP 
323nsPrintingPromptService::OnStatusChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, nsresult aStatus, const PRUnichar *aMessage)
324{
325  if (mWebProgressListener) {
326    return mWebProgressListener->OnStatusChange(aWebProgress, aRequest, aStatus, aMessage);
327  }
328  return NS_OK;
329}
330
331/* void onSecurityChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in unsigned long state); */
332NS_IMETHODIMP 
333nsPrintingPromptService::OnSecurityChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, PRUint32 state)
334{
335  if (mWebProgressListener) {
336    return mWebProgressListener->OnSecurityChange(aWebProgress, aRequest, state);
337  }
338  return NS_OK;
339}