toolkit/components/printing/content/printProgress.js

branch
TOR_BUG_9701
changeset 14
925c144e1f1f
equal deleted inserted replaced
-1:000000000000 0:1d4e3471a714
1 // -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 // dialog is just an array we'll use to store various properties from the dialog document...
8 var dialog;
9
10 // the printProgress is a nsIPrintProgress object
11 var printProgress = null;
12
13 // random global variables...
14 var targetFile;
15
16 var docTitle = "";
17 var docURL = "";
18 var progressParams = null;
19 var switchUI = true;
20
21 function ellipseString(aStr, doFront)
22 {
23 if (aStr.length > 3 && (aStr.substr(0, 3) == "..." || aStr.substr(aStr.length-4, 3) == "...")) {
24 return aStr;
25 }
26
27 var fixedLen = 64;
28 if (aStr.length > fixedLen) {
29 if (doFront) {
30 var endStr = aStr.substr(aStr.length-fixedLen, fixedLen);
31 var str = "..." + endStr;
32 return str;
33 } else {
34 var frontStr = aStr.substr(0, fixedLen);
35 var str = frontStr + "...";
36 return str;
37 }
38 }
39 return aStr;
40 }
41
42 // all progress notifications are done through the nsIWebProgressListener implementation...
43 var progressListener = {
44 onStateChange: function(aWebProgress, aRequest, aStateFlags, aStatus)
45 {
46 if (aStateFlags & Components.interfaces.nsIWebProgressListener.STATE_START)
47 {
48 // Put progress meter in undetermined mode.
49 // dialog.progress.setAttribute( "value", 0 );
50 dialog.progress.setAttribute( "mode", "undetermined" );
51 }
52
53 if (aStateFlags & Components.interfaces.nsIWebProgressListener.STATE_STOP)
54 {
55 // we are done printing
56 // Indicate completion in title area.
57 var msg = getString( "printComplete" );
58 dialog.title.setAttribute("value", msg);
59
60 // Put progress meter at 100%.
61 dialog.progress.setAttribute( "value", 100 );
62 dialog.progress.setAttribute( "mode", "normal" );
63 var percentPrint = getString( "progressText" );
64 percentPrint = replaceInsert( percentPrint, 1, 100 );
65 dialog.progressText.setAttribute("value", percentPrint);
66
67 var fm = Components.classes["@mozilla.org/focus-manager;1"]
68 .getService(Components.interfaces.nsIFocusManager);
69 if (fm && fm.activeWindow == window) {
70 // This progress dialog is the currently active window. In
71 // this case we need to make sure that some other window
72 // gets focus before we close this dialog to work around the
73 // buggy Windows XP Fax dialog, which ends up parenting
74 // itself to the currently focused window and is unable to
75 // survive that window going away. What happens without this
76 // opener.focus() call on Windows XP is that the fax dialog
77 // is opened only to go away when this dialog actually
78 // closes (which can happen asynchronously, so the fax
79 // dialog just flashes once and then goes away), so w/o this
80 // fix, it's impossible to fax on Windows XP w/o manually
81 // switching focus to another window (or holding on to the
82 // progress dialog with the mouse long enough).
83 opener.focus();
84 }
85
86 window.close();
87 }
88 },
89
90 onProgressChange: function(aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress)
91 {
92 if (switchUI)
93 {
94 dialog.tempLabel.setAttribute("hidden", "true");
95 dialog.progress.setAttribute("hidden", "false");
96 dialog.cancel.setAttribute("disabled", "false");
97
98 var progressLabel = getString("progress");
99 if (progressLabel == "") {
100 progressLabel = "Progress:"; // better than nothing
101 }
102 switchUI = false;
103 }
104
105 if (progressParams)
106 {
107 var docTitleStr = ellipseString(progressParams.docTitle, false);
108 if (docTitleStr != docTitle) {
109 docTitle = docTitleStr;
110 dialog.title.value = docTitle;
111 }
112 var docURLStr = progressParams.docURL;
113 if (docURLStr != docURL && dialog.title != null) {
114 docURL = docURLStr;
115 if (docTitle == "") {
116 dialog.title.value = ellipseString(docURLStr, true);
117 }
118 }
119 }
120
121 // Calculate percentage.
122 var percent;
123 if ( aMaxTotalProgress > 0 )
124 {
125 percent = Math.round( (aCurTotalProgress*100)/aMaxTotalProgress );
126 if ( percent > 100 )
127 percent = 100;
128
129 dialog.progress.removeAttribute( "mode");
130
131 // Advance progress meter.
132 dialog.progress.setAttribute( "value", percent );
133
134 // Update percentage label on progress meter.
135 var percentPrint = getString( "progressText" );
136 percentPrint = replaceInsert( percentPrint, 1, percent );
137 dialog.progressText.setAttribute("value", percentPrint);
138 }
139 else
140 {
141 // Progress meter should be barber-pole in this case.
142 dialog.progress.setAttribute( "mode", "undetermined" );
143 // Update percentage label on progress meter.
144 dialog.progressText.setAttribute("value", "");
145 }
146 },
147
148 onLocationChange: function(aWebProgress, aRequest, aLocation, aFlags)
149 {
150 // we can ignore this notification
151 },
152
153 onStatusChange: function(aWebProgress, aRequest, aStatus, aMessage)
154 {
155 if (aMessage != "")
156 dialog.title.setAttribute("value", aMessage);
157 },
158
159 onSecurityChange: function(aWebProgress, aRequest, state)
160 {
161 // we can ignore this notification
162 },
163
164 QueryInterface : function(iid)
165 {
166 if (iid.equals(Components.interfaces.nsIWebProgressListener) || iid.equals(Components.interfaces.nsISupportsWeakReference))
167 return this;
168
169 throw Components.results.NS_NOINTERFACE;
170 }
171 };
172
173 function getString( stringId ) {
174 // Check if we've fetched this string already.
175 if (!(stringId in dialog.strings)) {
176 // Try to get it.
177 var elem = document.getElementById( "dialog.strings."+stringId );
178 try {
179 if ( elem
180 &&
181 elem.childNodes
182 &&
183 elem.childNodes[0]
184 &&
185 elem.childNodes[0].nodeValue ) {
186 dialog.strings[ stringId ] = elem.childNodes[0].nodeValue;
187 } else {
188 // If unable to fetch string, use an empty string.
189 dialog.strings[ stringId ] = "";
190 }
191 } catch (e) { dialog.strings[ stringId ] = ""; }
192 }
193 return dialog.strings[ stringId ];
194 }
195
196 function loadDialog()
197 {
198 }
199
200 function replaceInsert( text, index, value ) {
201 var result = text;
202 var regExp = new RegExp( "#"+index );
203 result = result.replace( regExp, value );
204 return result;
205 }
206
207 function onLoad() {
208
209 // Set global variables.
210 printProgress = window.arguments[0];
211 if (window.arguments[1])
212 {
213 progressParams = window.arguments[1].QueryInterface(Components.interfaces.nsIPrintProgressParams)
214 if (progressParams)
215 {
216 docTitle = ellipseString(progressParams.docTitle, false);
217 docURL = ellipseString(progressParams.docURL, true);
218 }
219 }
220
221 if ( !printProgress ) {
222 dump( "Invalid argument to printProgress.xul\n" );
223 window.close()
224 return;
225 }
226
227 dialog = new Object;
228 dialog.strings = new Array;
229 dialog.title = document.getElementById("dialog.title");
230 dialog.titleLabel = document.getElementById("dialog.titleLabel");
231 dialog.progress = document.getElementById("dialog.progress");
232 dialog.progressText = document.getElementById("dialog.progressText");
233 dialog.progressLabel = document.getElementById("dialog.progressLabel");
234 dialog.tempLabel = document.getElementById("dialog.tempLabel");
235 dialog.cancel = document.getElementById("cancel");
236
237 dialog.progress.setAttribute("hidden", "true");
238 dialog.cancel.setAttribute("disabled", "true");
239
240 var progressLabel = getString("preparing");
241 if (progressLabel == "") {
242 progressLabel = "Preparing..."; // better than nothing
243 }
244 dialog.tempLabel.value = progressLabel;
245
246 dialog.title.value = docTitle;
247
248 // Set up dialog button callbacks.
249 var object = this;
250 doSetOKCancel("", function () { return object.onCancel();});
251
252 // Fill dialog.
253 loadDialog();
254
255 // set our web progress listener on the helper app launcher
256 printProgress.registerListener(progressListener);
257 moveToAlertPosition();
258 //We need to delay the set title else dom will overwrite it
259 window.setTimeout(doneIniting, 500);
260 }
261
262 function onUnload()
263 {
264 if (printProgress)
265 {
266 try
267 {
268 printProgress.unregisterListener(progressListener);
269 printProgress = null;
270 }
271
272 catch( exception ) {}
273 }
274 }
275
276 // If the user presses cancel, tell the app launcher and close the dialog...
277 function onCancel ()
278 {
279 // Cancel app launcher.
280 try
281 {
282 printProgress.processCanceledByUser = true;
283 }
284 catch( exception ) {return true;}
285
286 // don't Close up dialog by returning false, the backend will close the dialog when everything will be aborted.
287 return false;
288 }
289
290 function doneIniting()
291 {
292 printProgress.doneIniting();
293 }

mercurial