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 :
53 : static const char *kPrintDialogURL = "chrome://global/content/printdialog.xul";
54 : static const char *kPrintProgressDialogURL = "chrome://global/content/printProgress.xul";
55 : static const char *kPrtPrvProgressDialogURL = "chrome://global/content/printPreviewProgress.xul";
56 : static const char *kPageSetupDialogURL = "chrome://global/content/printPageSetup.xul";
57 : static const char *kPrinterPropertiesURL = "chrome://global/content/printjoboptions.xul";
58 :
59 : /****************************************************************
60 : ************************* ParamBlock ***************************
61 : ****************************************************************/
62 :
63 : class ParamBlock {
64 :
65 : public:
66 0 : ParamBlock()
67 : {
68 0 : mBlock = 0;
69 0 : }
70 0 : ~ParamBlock()
71 : {
72 0 : NS_IF_RELEASE(mBlock);
73 0 : }
74 0 : nsresult Init() {
75 0 : return CallCreateInstance(NS_DIALOGPARAMBLOCK_CONTRACTID, &mBlock);
76 : }
77 0 : nsIDialogParamBlock * operator->() const { return mBlock; }
78 0 : operator nsIDialogParamBlock * const () { return mBlock; }
79 :
80 : private:
81 : nsIDialogParamBlock *mBlock;
82 : };
83 :
84 : /****************************************************************
85 : ***************** nsPrintingPromptService **********************
86 : ****************************************************************/
87 :
88 0 : NS_IMPL_ISUPPORTS2(nsPrintingPromptService, nsIPrintingPromptService, nsIWebProgressListener)
89 :
90 0 : nsPrintingPromptService::nsPrintingPromptService()
91 : {
92 0 : }
93 :
94 0 : nsPrintingPromptService::~nsPrintingPromptService()
95 : {
96 0 : }
97 :
98 : nsresult
99 0 : nsPrintingPromptService::Init()
100 : {
101 : nsresult rv;
102 0 : mWatcher = do_GetService(NS_WINDOWWATCHER_CONTRACTID, &rv);
103 0 : return rv;
104 : }
105 :
106 : /* void showPrintDialog (in nsIDOMWindow parent, in nsIWebBrowserPrint webBrowserPrint, in nsIPrintSettings printSettings); */
107 : NS_IMETHODIMP
108 0 : nsPrintingPromptService::ShowPrintDialog(nsIDOMWindow *parent, nsIWebBrowserPrint *webBrowserPrint, nsIPrintSettings *printSettings)
109 : {
110 0 : NS_ENSURE_ARG(webBrowserPrint);
111 0 : NS_ENSURE_ARG(printSettings);
112 :
113 : // Try to access a component dialog
114 : nsCOMPtr<nsIPrintDialogService> dlgPrint(do_GetService(
115 0 : NS_PRINTDIALOGSERVICE_CONTRACTID));
116 0 : if (dlgPrint)
117 0 : return dlgPrint->Show(parent, printSettings, webBrowserPrint);
118 :
119 : // Show the built-in dialog instead
120 0 : ParamBlock block;
121 0 : nsresult rv = block.Init();
122 0 : if (NS_FAILED(rv))
123 0 : return rv;
124 :
125 0 : block->SetInt(0, 0);
126 0 : 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); */
130 : NS_IMETHODIMP
131 0 : nsPrintingPromptService::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 0 : NS_ENSURE_ARG(webProgressListener);
141 0 : NS_ENSURE_ARG(printProgressParams);
142 0 : NS_ENSURE_ARG(notifyOnOpen);
143 :
144 0 : *notifyOnOpen = false;
145 :
146 0 : nsPrintProgress* prtProgress = new nsPrintProgress(printSettings);
147 0 : mPrintProgress = prtProgress;
148 0 : mWebProgressListener = prtProgress;
149 :
150 0 : nsCOMPtr<nsIPrintProgressParams> prtProgressParams = new nsPrintProgressParams();
151 :
152 0 : nsCOMPtr<nsIDOMWindow> parentWindow = parent;
153 :
154 0 : if (mWatcher && !parentWindow) {
155 0 : mWatcher->GetActiveWindow(getter_AddRefs(parentWindow));
156 : }
157 :
158 0 : if (parentWindow) {
159 0 : mPrintProgress->OpenProgressDialog(parentWindow,
160 : isForPrinting ? kPrintProgressDialogURL : kPrtPrvProgressDialogURL,
161 0 : prtProgressParams, openDialogObserver, notifyOnOpen);
162 : }
163 :
164 0 : prtProgressParams.forget(printProgressParams);
165 0 : NS_ADDREF(*webProgressListener = this);
166 :
167 0 : return NS_OK;
168 : }
169 :
170 : /* void showPageSetup (in nsIDOMWindow parent, in nsIPrintSettings printSettings); */
171 : NS_IMETHODIMP
172 0 : nsPrintingPromptService::ShowPageSetup(nsIDOMWindow *parent, nsIPrintSettings *printSettings, nsIObserver *aObs)
173 : {
174 0 : NS_ENSURE_ARG(printSettings);
175 :
176 : // Try to access a component dialog
177 : nsCOMPtr<nsIPrintDialogService> dlgPrint(do_GetService(
178 0 : NS_PRINTDIALOGSERVICE_CONTRACTID));
179 0 : if (dlgPrint)
180 0 : return dlgPrint->ShowPageSetup(parent, printSettings);
181 :
182 0 : ParamBlock block;
183 0 : nsresult rv = block.Init();
184 0 : if (NS_FAILED(rv))
185 0 : return rv;
186 :
187 0 : block->SetInt(0, 0);
188 0 : return DoDialog(parent, block, nsnull, printSettings, kPageSetupDialogURL);
189 : }
190 :
191 : /* void showPrinterProperties (in nsIDOMWindow parent, in wstring printerName, in nsIPrintSettings printSettings); */
192 : NS_IMETHODIMP
193 0 : nsPrintingPromptService::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 0 : NS_ENSURE_ARG(printerName);
203 0 : NS_ENSURE_ARG(printSettings);
204 :
205 0 : ParamBlock block;
206 0 : nsresult rv = block.Init();
207 0 : if (NS_FAILED(rv))
208 0 : return rv;
209 :
210 0 : block->SetInt(0, 0);
211 0 : return DoDialog(parent, block, nsnull, printSettings, kPrinterPropertiesURL);
212 :
213 : }
214 :
215 : nsresult
216 0 : nsPrintingPromptService::DoDialog(nsIDOMWindow *aParent,
217 : nsIDialogParamBlock *aParamBlock,
218 : nsIWebBrowserPrint *aWebBrowserPrint,
219 : nsIPrintSettings* aPS,
220 : const char *aChromeURL)
221 : {
222 0 : NS_ENSURE_ARG(aParamBlock);
223 0 : NS_ENSURE_ARG(aPS);
224 0 : NS_ENSURE_ARG(aChromeURL);
225 :
226 0 : if (!mWatcher)
227 0 : return NS_ERROR_FAILURE;
228 :
229 0 : 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 0 : nsCOMPtr<nsIDOMWindow> activeParent; // retain ownership for method lifetime
235 0 : if (!aParent)
236 : {
237 0 : mWatcher->GetActiveWindow(getter_AddRefs(activeParent));
238 0 : aParent = activeParent;
239 : }
240 :
241 : // create a nsISupportsArray of the parameters
242 : // being passed to the window
243 0 : nsCOMPtr<nsISupportsArray> array;
244 0 : NS_NewISupportsArray(getter_AddRefs(array));
245 0 : if (!array) return NS_ERROR_FAILURE;
246 :
247 0 : nsCOMPtr<nsISupports> psSupports(do_QueryInterface(aPS));
248 0 : NS_ASSERTION(psSupports, "PrintSettings must be a supports");
249 0 : array->AppendElement(psSupports);
250 :
251 0 : if (aWebBrowserPrint) {
252 0 : nsCOMPtr<nsISupports> wbpSupports(do_QueryInterface(aWebBrowserPrint));
253 0 : NS_ASSERTION(wbpSupports, "nsIWebBrowserPrint must be a supports");
254 0 : array->AppendElement(wbpSupports);
255 : }
256 :
257 0 : nsCOMPtr<nsISupports> blkSupps(do_QueryInterface(aParamBlock));
258 0 : NS_ASSERTION(blkSupps, "IOBlk must be a supports");
259 0 : array->AppendElement(blkSupps);
260 :
261 0 : nsCOMPtr<nsISupports> arguments(do_QueryInterface(array));
262 0 : NS_ASSERTION(array, "array must be a supports");
263 :
264 :
265 0 : nsCOMPtr<nsIDOMWindow> dialog;
266 0 : rv = mWatcher->OpenWindow(aParent, aChromeURL, "_blank",
267 : "centerscreen,chrome,modal,titlebar", arguments,
268 0 : 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 0 : if (NS_SUCCEEDED(rv) && aWebBrowserPrint)
273 : {
274 : PRInt32 status;
275 0 : aParamBlock->GetInt(0, &status);
276 0 : return status == 0?NS_ERROR_ABORT:NS_OK;
277 : }
278 :
279 0 : 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); */
287 : NS_IMETHODIMP
288 0 : nsPrintingPromptService::OnStateChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, PRUint32 aStateFlags, nsresult aStatus)
289 : {
290 0 : if ((aStateFlags & STATE_STOP) && mWebProgressListener) {
291 0 : mWebProgressListener->OnStateChange(aWebProgress, aRequest, aStateFlags, aStatus);
292 0 : if (mPrintProgress) {
293 0 : mPrintProgress->CloseProgressDialog(true);
294 : }
295 0 : mPrintProgress = nsnull;
296 0 : mWebProgressListener = nsnull;
297 : }
298 0 : 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); */
302 : NS_IMETHODIMP
303 0 : nsPrintingPromptService::OnProgressChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, PRInt32 aCurSelfProgress, PRInt32 aMaxSelfProgress, PRInt32 aCurTotalProgress, PRInt32 aMaxTotalProgress)
304 : {
305 0 : if (mWebProgressListener) {
306 0 : return mWebProgressListener->OnProgressChange(aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress);
307 : }
308 0 : return NS_OK;
309 : }
310 :
311 : /* void onLocationChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in nsIURI location, in unsigned long aFlags); */
312 : NS_IMETHODIMP
313 0 : nsPrintingPromptService::OnLocationChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, nsIURI *location, PRUint32 aFlags)
314 : {
315 0 : if (mWebProgressListener) {
316 0 : return mWebProgressListener->OnLocationChange(aWebProgress, aRequest, location, aFlags);
317 : }
318 0 : return NS_OK;
319 : }
320 :
321 : /* void onStatusChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in nsresult aStatus, in wstring aMessage); */
322 : NS_IMETHODIMP
323 0 : nsPrintingPromptService::OnStatusChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, nsresult aStatus, const PRUnichar *aMessage)
324 : {
325 0 : if (mWebProgressListener) {
326 0 : return mWebProgressListener->OnStatusChange(aWebProgress, aRequest, aStatus, aMessage);
327 : }
328 0 : return NS_OK;
329 : }
330 :
331 : /* void onSecurityChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in unsigned long state); */
332 : NS_IMETHODIMP
333 0 : nsPrintingPromptService::OnSecurityChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, PRUint32 state)
334 : {
335 0 : if (mWebProgressListener) {
336 0 : return mWebProgressListener->OnSecurityChange(aWebProgress, aRequest, state);
337 : }
338 0 : return NS_OK;
339 : }
|