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