/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. #include "nsPrintingPromptService.h"
  38. #include "nsIComponentManager.h"
  39. #include "nsIDialogParamBlock.h"
  40. #include "nsIDOMWindow.h"
  41. #include "nsIServiceManager.h"
  42. #include "nsISupportsUtils.h"
  43. #include "nsISupportsArray.h"
  44. #include "nsString.h"
  45. #include "nsIPrintDialogService.h"
  46. // Printing Progress Includes
  47. #include "nsPrintProgress.h"
  48. #include "nsPrintProgressParams.h"
  49. static const char *kPrintDialogURL = "chrome://global/content/printdialog.xul";
  50. static const char *kPrintProgressDialogURL = "chrome://global/content/printProgress.xul";
  51. static const char *kPrtPrvProgressDialogURL = "chrome://global/content/printPreviewProgress.xul";
  52. static const char *kPageSetupDialogURL = "chrome://global/content/printPageSetup.xul";
  53. static const char *kPrinterPropertiesURL = "chrome://global/content/printjoboptions.xul";
  54. /****************************************************************
  55. ************************* ParamBlock ***************************
  56. ****************************************************************/
  57. class ParamBlock {
  58. public:
  59. ParamBlock()
  60. {
  61. mBlock = 0;
  62. }
  63. ~ParamBlock()
  64. {
  65. NS_IF_RELEASE(mBlock);
  66. }
  67. nsresult Init() {
  68. return CallCreateInstance(NS_DIALOGPARAMBLOCK_CONTRACTID, &mBlock);
  69. }
  70. nsIDialogParamBlock * operator->() const { return mBlock; }
  71. operator nsIDialogParamBlock * const () { return mBlock; }
  72. private:
  73. nsIDialogParamBlock *mBlock;
  74. };
  75. /****************************************************************
  76. ***************** nsPrintingPromptService **********************
  77. ****************************************************************/
  78. NS_IMPL_ISUPPORTS2(nsPrintingPromptService, nsIPrintingPromptService, nsIWebProgressListener)
  79. nsPrintingPromptService::nsPrintingPromptService()
  80. {
  81. }
  82. nsPrintingPromptService::~nsPrintingPromptService()
  83. {
  84. }
  85. nsresult
  86. nsPrintingPromptService::Init()
  87. {
  88. nsresult rv;
  89. mWatcher = do_GetService(NS_WINDOWWATCHER_CONTRACTID, &rv);
  90. return rv;
  91. }
  92. /* void showPrintDialog (in nsIDOMWindow parent, in nsIWebBrowserPrint webBrowserPrint, in nsIPrintSettings printSettings); */
  93. NS_IMETHODIMP
  94. nsPrintingPromptService::ShowPrintDialog(nsIDOMWindow *parent, nsIWebBrowserPrint *webBrowserPrint, nsIPrintSettings *printSettings)
  95. {
  96. NS_ENSURE_ARG(webBrowserPrint);
  97. NS_ENSURE_ARG(printSettings);
  98. // Try to access a component dialog
  99. nsCOMPtr<nsIPrintDialogService> dlgPrint(do_GetService(
  100. NS_PRINTDIALOGSERVICE_CONTRACTID));
  101. if (dlgPrint)
  102. return dlgPrint->Show(parent, printSettings, webBrowserPrint);
  103. // Show the built-in dialog instead
  104. ParamBlock block;
  105. nsresult rv = block.Init();
  106. if (NS_FAILED(rv))
  107. return rv;
  108. block->SetInt(0, 0);
  109. return DoDialog(parent, block, webBrowserPrint, printSettings, kPrintDialogURL);
  110. }
  111. /* 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); */
  112. NS_IMETHODIMP
  113. nsPrintingPromptService::ShowProgress(nsIDOMWindow* parent,
  114. nsIWebBrowserPrint* webBrowserPrint, // ok to be null
  115. nsIPrintSettings* printSettings, // ok to be null
  116. nsIObserver* openDialogObserver, // ok to be null
  117. bool isForPrinting,
  118. nsIWebProgressListener** webProgressListener,
  119. nsIPrintProgressParams** printProgressParams,
  120. bool* notifyOnOpen)
  121. {
  122. NS_ENSURE_ARG(webProgressListener);
  123. NS_ENSURE_ARG(printProgressParams);
  124. NS_ENSURE_ARG(notifyOnOpen);
  125. *notifyOnOpen = false;
  126. nsPrintProgress* prtProgress = new nsPrintProgress(printSettings);
  127. mPrintProgress = prtProgress;
  128. mWebProgressListener = prtProgress;
  129. nsCOMPtr<nsIPrintProgressParams> prtProgressParams = new nsPrintProgressParams();
  130. nsCOMPtr<nsIDOMWindow> parentWindow = parent;
  131. if (mWatcher && !parentWindow) {
  132. mWatcher->GetActiveWindow(getter_AddRefs(parentWindow));
  133. }
  134. if (parentWindow) {
  135. mPrintProgress->OpenProgressDialog(parentWindow,
  136. isForPrinting ? kPrintProgressDialogURL : kPrtPrvProgressDialogURL,
  137. prtProgressParams, openDialogObserver, notifyOnOpen);
  138. }
  139. prtProgressParams.forget(printProgressParams);
  140. NS_ADDREF(*webProgressListener = this);
  141. return NS_OK;
  142. }
  143. /* void showPageSetup (in nsIDOMWindow parent, in nsIPrintSettings printSettings); */
  144. NS_IMETHODIMP
  145. nsPrintingPromptService::ShowPageSetup(nsIDOMWindow *parent, nsIPrintSettings *printSettings, nsIObserver *aObs)
  146. {
  147. NS_ENSURE_ARG(printSettings);
  148. // Try to access a component dialog
  149. nsCOMPtr<nsIPrintDialogService> dlgPrint(do_GetService(
  150. NS_PRINTDIALOGSERVICE_CONTRACTID));
  151. if (dlgPrint)
  152. return dlgPrint->ShowPageSetup(parent, printSettings);
  153. ParamBlock block;
  154. nsresult rv = block.Init();
  155. if (NS_FAILED(rv))
  156. return rv;
  157. block->SetInt(0, 0);
  158. return DoDialog(parent, block, nsnull, printSettings, kPageSetupDialogURL);
  159. }
  160. /* void showPrinterProperties (in nsIDOMWindow parent, in wstring printerName, in nsIPrintSettings printSettings); */
  161. NS_IMETHODIMP
  162. nsPrintingPromptService::ShowPrinterProperties(nsIDOMWindow *parent, const PRUnichar *printerName, nsIPrintSettings *printSettings)
  163. {
  164. /* fixme: We simply ignore the |aPrinter| argument here
  165. * We should get the supported printer attributes from the printer and
  166. * populate the print job options dialog with these data instead of using
  167. * the "default set" here.
  168. * However, this requires changes on all platforms and is another big chunk
  169. * of patches ... ;-(
  170. */
  171. NS_ENSURE_ARG(printerName);
  172. NS_ENSURE_ARG(printSettings);
  173. ParamBlock block;
  174. nsresult rv = block.Init();
  175. if (NS_FAILED(rv))
  176. return rv;
  177. block->SetInt(0, 0);
  178. return DoDialog(parent, block, nsnull, printSettings, kPrinterPropertiesURL);
  179. }
  180. nsresult
  181. nsPrintingPromptService::DoDialog(nsIDOMWindow *aParent,
  182. nsIDialogParamBlock *aParamBlock,
  183. nsIWebBrowserPrint *aWebBrowserPrint,
  184. nsIPrintSettings* aPS,
  185. const char *aChromeURL)
  186. {
  187. NS_ENSURE_ARG(aParamBlock);
  188. NS_ENSURE_ARG(aPS);
  189. NS_ENSURE_ARG(aChromeURL);
  190. if (!mWatcher)
  191. return NS_ERROR_FAILURE;
  192. nsresult rv = NS_OK;
  193. // get a parent, if at all possible
  194. // (though we'd rather this didn't fail, it's OK if it does. so there's
  195. // no failure or null check.)
  196. nsCOMPtr<nsIDOMWindow> activeParent; // retain ownership for method lifetime
  197. if (!aParent)
  198. {
  199. mWatcher->GetActiveWindow(getter_AddRefs(activeParent));
  200. aParent = activeParent;
  201. }
  202. // create a nsISupportsArray of the parameters
  203. // being passed to the window
  204. nsCOMPtr<nsISupportsArray> array;
  205. NS_NewISupportsArray(getter_AddRefs(array));
  206. if (!array) return NS_ERROR_FAILURE;
  207. nsCOMPtr<nsISupports> psSupports(do_QueryInterface(aPS));
  208. NS_ASSERTION(psSupports, "PrintSettings must be a supports");
  209. array->AppendElement(psSupports);
  210. if (aWebBrowserPrint) {
  211. nsCOMPtr<nsISupports> wbpSupports(do_QueryInterface(aWebBrowserPrint));
  212. NS_ASSERTION(wbpSupports, "nsIWebBrowserPrint must be a supports");
  213. array->AppendElement(wbpSupports);
  214. }
  215. nsCOMPtr<nsISupports> blkSupps(do_QueryInterface(aParamBlock));
  216. NS_ASSERTION(blkSupps, "IOBlk must be a supports");
  217. array->AppendElement(blkSupps);
  218. nsCOMPtr<nsISupports> arguments(do_QueryInterface(array));
  219. NS_ASSERTION(array, "array must be a supports");
  220. nsCOMPtr<nsIDOMWindow> dialog;
  221. rv = mWatcher->OpenWindow(aParent, aChromeURL, "_blank",
  222. "centerscreen,chrome,modal,titlebar", arguments,
  223. getter_AddRefs(dialog));
  224. // if aWebBrowserPrint is not null then we are printing
  225. // so we want to pass back NS_ERROR_ABORT on cancel
  226. if (NS_SUCCEEDED(rv) && aWebBrowserPrint)
  227. {
  228. PRInt32 status;
  229. aParamBlock->GetInt(0, &status);
  230. return status == 0?NS_ERROR_ABORT:NS_OK;
  231. }
  232. return rv;
  233. }
  234. //////////////////////////////////////////////////////////////////////
  235. // nsIWebProgressListener
  236. //////////////////////////////////////////////////////////////////////
  237. /* void onStateChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in unsigned long aStateFlags, in nsresult aStatus); */
  238. NS_IMETHODIMP
  239. nsPrintingPromptService::OnStateChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, PRUint32 aStateFlags, nsresult aStatus)
  240. {
  241. if ((aStateFlags & STATE_STOP) && mWebProgressListener) {
  242. mWebProgressListener->OnStateChange(aWebProgress, aRequest, aStateFlags, aStatus);
  243. if (mPrintProgress) {
  244. mPrintProgress->CloseProgressDialog(true);
  245. }
  246. mPrintProgress = nsnull;
  247. mWebProgressListener = nsnull;
  248. }
  249. return NS_OK;
  250. }
  251. /* void onProgressChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in long aCurSelfProgress, in long aMaxSelfProgress, in long aCurTotalProgress, in long aMaxTotalProgress); */
  252. NS_IMETHODIMP
  253. nsPrintingPromptService::OnProgressChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, PRInt32 aCurSelfProgress, PRInt32 aMaxSelfProgress, PRInt32 aCurTotalProgress, PRInt32 aMaxTotalProgress)
  254. {
  255. if (mWebProgressListener) {
  256. return mWebProgressListener->OnProgressChange(aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress);
  257. }
  258. return NS_OK;
  259. }
  260. /* void onLocationChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in nsIURI location, in unsigned long aFlags); */
  261. NS_IMETHODIMP
  262. nsPrintingPromptService::OnLocationChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, nsIURI *location, PRUint32 aFlags)
  263. {
  264. if (mWebProgressListener) {
  265. return mWebProgressListener->OnLocationChange(aWebProgress, aRequest, location, aFlags);
  266. }
  267. return NS_OK;
  268. }
  269. /* void onStatusChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in nsresult aStatus, in wstring aMessage); */
  270. NS_IMETHODIMP
  271. nsPrintingPromptService::OnStatusChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, nsresult aStatus, const PRUnichar *aMessage)
  272. {
  273. if (mWebProgressListener) {
  274. return mWebProgressListener->OnStatusChange(aWebProgress, aRequest, aStatus, aMessage);
  275. }
  276. return NS_OK;
  277. }
  278. /* void onSecurityChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in unsigned long state); */
  279. NS_IMETHODIMP
  280. nsPrintingPromptService::OnSecurityChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, PRUint32 state)
  281. {
  282. if (mWebProgressListener) {
  283. return mWebProgressListener->OnSecurityChange(aWebProgress, aRequest, state);
  284. }
  285. return NS_OK;
  286. }