Line data Source code
1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /* This Source Code Form is subject to the terms of the Mozilla Public
3 : * License, v. 2.0. If a copy of the MPL was not distributed with this
4 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 :
6 : #include "nsPrintingPromptService.h"
7 :
8 : #include "nsArray.h"
9 : #include "nsIComponentManager.h"
10 : #include "nsIDialogParamBlock.h"
11 : #include "nsIDOMWindow.h"
12 : #include "nsIServiceManager.h"
13 : #include "nsISupportsUtils.h"
14 : #include "nsString.h"
15 : #include "nsIPrintDialogService.h"
16 :
17 : // Printing Progress Includes
18 : #include "nsPrintProgress.h"
19 : #include "nsPrintProgressParams.h"
20 :
21 : static const char *kPrintDialogURL = "chrome://global/content/printdialog.xul";
22 : static const char *kPrintProgressDialogURL = "chrome://global/content/printProgress.xul";
23 : static const char *kPrtPrvProgressDialogURL = "chrome://global/content/printPreviewProgress.xul";
24 : static const char *kPageSetupDialogURL = "chrome://global/content/printPageSetup.xul";
25 : static const char *kPrinterPropertiesURL = "chrome://global/content/printjoboptions.xul";
26 :
27 : /****************************************************************
28 : ************************* ParamBlock ***************************
29 : ****************************************************************/
30 :
31 : class ParamBlock {
32 :
33 : public:
34 0 : ParamBlock()
35 0 : {
36 0 : mBlock = 0;
37 0 : }
38 0 : ~ParamBlock()
39 0 : {
40 0 : NS_IF_RELEASE(mBlock);
41 0 : }
42 0 : nsresult Init() {
43 0 : return CallCreateInstance(NS_DIALOGPARAMBLOCK_CONTRACTID, &mBlock);
44 : }
45 0 : nsIDialogParamBlock * operator->() const MOZ_NO_ADDREF_RELEASE_ON_RETURN { return mBlock; }
46 0 : operator nsIDialogParamBlock * () const { return mBlock; }
47 :
48 : private:
49 : nsIDialogParamBlock *mBlock;
50 : };
51 :
52 : /****************************************************************
53 : ***************** nsPrintingPromptService **********************
54 : ****************************************************************/
55 :
56 0 : NS_IMPL_ISUPPORTS(nsPrintingPromptService, nsIPrintingPromptService, nsIWebProgressListener)
57 :
58 0 : nsPrintingPromptService::nsPrintingPromptService()
59 : {
60 0 : }
61 :
62 0 : nsPrintingPromptService::~nsPrintingPromptService()
63 : {
64 0 : }
65 :
66 : nsresult
67 0 : nsPrintingPromptService::Init()
68 : {
69 : nsresult rv;
70 0 : mWatcher = do_GetService(NS_WINDOWWATCHER_CONTRACTID, &rv);
71 0 : return rv;
72 : }
73 :
74 : NS_IMETHODIMP
75 0 : nsPrintingPromptService::ShowPrintDialog(mozIDOMWindowProxy *parent,
76 : nsIWebBrowserPrint *webBrowserPrint,
77 : nsIPrintSettings *printSettings)
78 : {
79 0 : NS_ENSURE_ARG(webBrowserPrint);
80 0 : NS_ENSURE_ARG(printSettings);
81 :
82 : // Try to access a component dialog
83 : nsCOMPtr<nsIPrintDialogService> dlgPrint(do_GetService(
84 0 : NS_PRINTDIALOGSERVICE_CONTRACTID));
85 0 : if (dlgPrint)
86 0 : return dlgPrint->Show(nsPIDOMWindowOuter::From(parent),
87 0 : printSettings, webBrowserPrint);
88 :
89 : // Show the built-in dialog instead
90 0 : ParamBlock block;
91 0 : nsresult rv = block.Init();
92 0 : if (NS_FAILED(rv))
93 0 : return rv;
94 :
95 0 : block->SetInt(0, 0);
96 0 : return DoDialog(parent, block, webBrowserPrint, printSettings, kPrintDialogURL);
97 : }
98 :
99 : NS_IMETHODIMP
100 0 : nsPrintingPromptService::ShowProgress(mozIDOMWindowProxy* parent,
101 : nsIWebBrowserPrint* webBrowserPrint, // ok to be null
102 : nsIPrintSettings* printSettings, // ok to be null
103 : nsIObserver* openDialogObserver, // ok to be null
104 : bool isForPrinting,
105 : nsIWebProgressListener** webProgressListener,
106 : nsIPrintProgressParams** printProgressParams,
107 : bool* notifyOnOpen)
108 : {
109 0 : NS_ENSURE_ARG(webProgressListener);
110 0 : NS_ENSURE_ARG(printProgressParams);
111 0 : NS_ENSURE_ARG(notifyOnOpen);
112 :
113 0 : *notifyOnOpen = false;
114 :
115 0 : nsPrintProgress* prtProgress = new nsPrintProgress(printSettings);
116 0 : mPrintProgress = prtProgress;
117 0 : mWebProgressListener = prtProgress;
118 :
119 0 : nsCOMPtr<nsIPrintProgressParams> prtProgressParams = new nsPrintProgressParams();
120 :
121 0 : nsCOMPtr<mozIDOMWindowProxy> parentWindow = parent;
122 :
123 0 : if (mWatcher && !parentWindow) {
124 0 : mWatcher->GetActiveWindow(getter_AddRefs(parentWindow));
125 : }
126 :
127 0 : if (parentWindow) {
128 0 : mPrintProgress->OpenProgressDialog(parentWindow,
129 : isForPrinting ? kPrintProgressDialogURL : kPrtPrvProgressDialogURL,
130 0 : prtProgressParams, openDialogObserver, notifyOnOpen);
131 : }
132 :
133 0 : prtProgressParams.forget(printProgressParams);
134 0 : NS_ADDREF(*webProgressListener = this);
135 :
136 0 : return NS_OK;
137 : }
138 :
139 : NS_IMETHODIMP
140 0 : nsPrintingPromptService::ShowPageSetup(mozIDOMWindowProxy *parent,
141 : nsIPrintSettings *printSettings,
142 : nsIObserver *aObs)
143 : {
144 0 : NS_ENSURE_ARG(printSettings);
145 :
146 : // Try to access a component dialog
147 : nsCOMPtr<nsIPrintDialogService> dlgPrint(do_GetService(
148 0 : NS_PRINTDIALOGSERVICE_CONTRACTID));
149 0 : if (dlgPrint)
150 0 : return dlgPrint->ShowPageSetup(nsPIDOMWindowOuter::From(parent),
151 0 : printSettings);
152 :
153 0 : ParamBlock block;
154 0 : nsresult rv = block.Init();
155 0 : if (NS_FAILED(rv))
156 0 : return rv;
157 :
158 0 : block->SetInt(0, 0);
159 0 : return DoDialog(parent, block, nullptr, printSettings, kPageSetupDialogURL);
160 : }
161 :
162 : NS_IMETHODIMP
163 0 : nsPrintingPromptService::ShowPrinterProperties(mozIDOMWindowProxy *parent,
164 : const char16_t *printerName,
165 : nsIPrintSettings *printSettings)
166 : {
167 : /* fixme: We simply ignore the |aPrinter| argument here
168 : * We should get the supported printer attributes from the printer and
169 : * populate the print job options dialog with these data instead of using
170 : * the "default set" here.
171 : * However, this requires changes on all platforms and is another big chunk
172 : * of patches ... ;-(
173 : */
174 0 : NS_ENSURE_ARG(printerName);
175 0 : NS_ENSURE_ARG(printSettings);
176 :
177 0 : ParamBlock block;
178 0 : nsresult rv = block.Init();
179 0 : if (NS_FAILED(rv))
180 0 : return rv;
181 :
182 0 : block->SetInt(0, 0);
183 0 : return DoDialog(parent, block, nullptr, printSettings, kPrinterPropertiesURL);
184 :
185 : }
186 :
187 : nsresult
188 0 : nsPrintingPromptService::DoDialog(mozIDOMWindowProxy *aParent,
189 : nsIDialogParamBlock *aParamBlock,
190 : nsIWebBrowserPrint *aWebBrowserPrint,
191 : nsIPrintSettings* aPS,
192 : const char *aChromeURL)
193 : {
194 0 : NS_ENSURE_ARG(aParamBlock);
195 0 : NS_ENSURE_ARG(aPS);
196 0 : NS_ENSURE_ARG(aChromeURL);
197 :
198 0 : if (!mWatcher)
199 0 : return NS_ERROR_FAILURE;
200 :
201 : // get a parent, if at all possible
202 : // (though we'd rather this didn't fail, it's OK if it does. so there's
203 : // no failure or null check.)
204 0 : nsCOMPtr<mozIDOMWindowProxy> activeParent;
205 0 : if (!aParent)
206 : {
207 0 : mWatcher->GetActiveWindow(getter_AddRefs(activeParent));
208 0 : aParent = activeParent;
209 : }
210 :
211 : // create a nsIMutableArray of the parameters
212 : // being passed to the window
213 0 : nsCOMPtr<nsIMutableArray> array = nsArray::Create();
214 :
215 0 : nsCOMPtr<nsISupports> psSupports(do_QueryInterface(aPS));
216 0 : NS_ASSERTION(psSupports, "PrintSettings must be a supports");
217 0 : array->AppendElement(psSupports, /*weak =*/ false);
218 :
219 0 : if (aWebBrowserPrint) {
220 0 : nsCOMPtr<nsISupports> wbpSupports(do_QueryInterface(aWebBrowserPrint));
221 0 : NS_ASSERTION(wbpSupports, "nsIWebBrowserPrint must be a supports");
222 0 : array->AppendElement(wbpSupports, /*weak =*/ false);
223 : }
224 :
225 0 : nsCOMPtr<nsISupports> blkSupps(do_QueryInterface(aParamBlock));
226 0 : NS_ASSERTION(blkSupps, "IOBlk must be a supports");
227 0 : array->AppendElement(blkSupps, /*weak =*/ false);
228 :
229 0 : nsCOMPtr<mozIDOMWindowProxy> dialog;
230 0 : nsresult rv = mWatcher->OpenWindow(aParent, aChromeURL, "_blank",
231 : "centerscreen,chrome,modal,titlebar", array,
232 0 : getter_AddRefs(dialog));
233 :
234 : // if aWebBrowserPrint is not null then we are printing
235 : // so we want to pass back NS_ERROR_ABORT on cancel
236 0 : if (NS_SUCCEEDED(rv) && aWebBrowserPrint)
237 : {
238 : int32_t status;
239 0 : aParamBlock->GetInt(0, &status);
240 0 : return status == 0?NS_ERROR_ABORT:NS_OK;
241 : }
242 :
243 0 : return rv;
244 : }
245 :
246 : //////////////////////////////////////////////////////////////////////
247 : // nsIWebProgressListener
248 : //////////////////////////////////////////////////////////////////////
249 :
250 : NS_IMETHODIMP
251 0 : nsPrintingPromptService::OnStateChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, uint32_t aStateFlags, nsresult aStatus)
252 : {
253 0 : if ((aStateFlags & STATE_STOP) && mWebProgressListener) {
254 0 : mWebProgressListener->OnStateChange(aWebProgress, aRequest, aStateFlags, aStatus);
255 0 : if (mPrintProgress) {
256 0 : mPrintProgress->CloseProgressDialog(true);
257 : }
258 0 : mPrintProgress = nullptr;
259 0 : mWebProgressListener = nullptr;
260 : }
261 0 : return NS_OK;
262 : }
263 :
264 : NS_IMETHODIMP
265 0 : nsPrintingPromptService::OnProgressChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, int32_t aCurSelfProgress, int32_t aMaxSelfProgress, int32_t aCurTotalProgress, int32_t aMaxTotalProgress)
266 : {
267 0 : if (mWebProgressListener) {
268 0 : return mWebProgressListener->OnProgressChange(aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress);
269 : }
270 0 : return NS_OK;
271 : }
272 :
273 : NS_IMETHODIMP
274 0 : nsPrintingPromptService::OnLocationChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, nsIURI *location, uint32_t aFlags)
275 : {
276 0 : if (mWebProgressListener) {
277 0 : return mWebProgressListener->OnLocationChange(aWebProgress, aRequest, location, aFlags);
278 : }
279 0 : return NS_OK;
280 : }
281 :
282 : NS_IMETHODIMP
283 0 : nsPrintingPromptService::OnStatusChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, nsresult aStatus, const char16_t *aMessage)
284 : {
285 0 : if (mWebProgressListener) {
286 0 : return mWebProgressListener->OnStatusChange(aWebProgress, aRequest, aStatus, aMessage);
287 : }
288 0 : return NS_OK;
289 : }
290 :
291 : NS_IMETHODIMP
292 0 : nsPrintingPromptService::OnSecurityChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, uint32_t state)
293 : {
294 0 : if (mWebProgressListener) {
295 0 : return mWebProgressListener->OnSecurityChange(aWebProgress, aRequest, state);
296 : }
297 0 : return NS_OK;
298 : }
|