|
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 |
|
20 function ellipseString(aStr, doFront) |
|
21 { |
|
22 if (aStr.length > 3 && (aStr.substr(0, 3) == "..." || aStr.substr(aStr.length-4, 3) == "...")) |
|
23 return aStr; |
|
24 |
|
25 var fixedLen = 64; |
|
26 if (aStr.length <= fixedLen) |
|
27 return aStr; |
|
28 |
|
29 if (doFront) |
|
30 return "..." + aStr.substr(aStr.length-fixedLen, fixedLen); |
|
31 |
|
32 return aStr.substr(0, fixedLen) + "..."; |
|
33 } |
|
34 |
|
35 // all progress notifications are done through the nsIWebProgressListener implementation... |
|
36 var progressListener = { |
|
37 |
|
38 onStateChange: function (aWebProgress, aRequest, aStateFlags, aStatus) |
|
39 { |
|
40 if (aStateFlags & Components.interfaces.nsIWebProgressListener.STATE_STOP) |
|
41 window.close(); |
|
42 }, |
|
43 |
|
44 onProgressChange: function (aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress) |
|
45 { |
|
46 if (!progressParams) |
|
47 return; |
|
48 var docTitleStr = ellipseString(progressParams.docTitle, false); |
|
49 if (docTitleStr != docTitle) { |
|
50 docTitle = docTitleStr; |
|
51 dialog.title.value = docTitle; |
|
52 } |
|
53 var docURLStr = ellipseString(progressParams.docURL, true); |
|
54 if (docURLStr != docURL && dialog.title != null) { |
|
55 docURL = docURLStr; |
|
56 if (docTitle == "") |
|
57 dialog.title.value = docURLStr; |
|
58 } |
|
59 }, |
|
60 |
|
61 onLocationChange: function (aWebProgress, aRequest, aLocation, aFlags) {}, |
|
62 onSecurityChange: function (aWebProgress, aRequest, state) {}, |
|
63 |
|
64 onStatusChange: function (aWebProgress, aRequest, aStatus, aMessage) |
|
65 { |
|
66 if (aMessage) |
|
67 dialog.title.setAttribute("value", aMessage); |
|
68 }, |
|
69 |
|
70 QueryInterface: function (iid) |
|
71 { |
|
72 if (iid.equals(Components.interfaces.nsIWebProgressListener) || iid.equals(Components.interfaces.nsISupportsWeakReference)) |
|
73 return this; |
|
74 throw Components.results.NS_NOINTERFACE; |
|
75 } |
|
76 } |
|
77 |
|
78 function onLoad() { |
|
79 // Set global variables. |
|
80 printProgress = window.arguments[0]; |
|
81 if (window.arguments[1]) { |
|
82 progressParams = window.arguments[1].QueryInterface(Components.interfaces.nsIPrintProgressParams) |
|
83 if (progressParams) { |
|
84 docTitle = ellipseString(progressParams.docTitle, false); |
|
85 docURL = ellipseString(progressParams.docURL, true); |
|
86 } |
|
87 } |
|
88 |
|
89 if (!printProgress) { |
|
90 dump( "Invalid argument to printPreviewProgress.xul\n" ); |
|
91 window.close() |
|
92 return; |
|
93 } |
|
94 |
|
95 dialog = new Object; |
|
96 dialog.strings = new Array; |
|
97 dialog.title = document.getElementById("dialog.title"); |
|
98 dialog.titleLabel = document.getElementById("dialog.titleLabel"); |
|
99 |
|
100 dialog.title.value = docTitle; |
|
101 |
|
102 // set our web progress listener on the helper app launcher |
|
103 printProgress.registerListener(progressListener); |
|
104 moveToAlertPosition(); |
|
105 |
|
106 //We need to delay the set title else dom will overwrite it |
|
107 window.setTimeout(doneIniting, 100); |
|
108 } |
|
109 |
|
110 function onUnload() |
|
111 { |
|
112 if (!printProgress) |
|
113 return; |
|
114 try { |
|
115 printProgress.unregisterListener(progressListener); |
|
116 printProgress = null; |
|
117 } |
|
118 catch(e) {} |
|
119 } |
|
120 |
|
121 function getString (stringId) { |
|
122 // Check if we've fetched this string already. |
|
123 if (!(stringId in dialog.strings)) { |
|
124 // Try to get it. |
|
125 var elem = document.getElementById( "dialog.strings."+stringId); |
|
126 try { |
|
127 if (elem && elem.childNodes && elem.childNodes[0] && |
|
128 elem.childNodes[0].nodeValue) |
|
129 dialog.strings[stringId] = elem.childNodes[0].nodeValue; |
|
130 // If unable to fetch string, use an empty string. |
|
131 else |
|
132 dialog.strings[stringId] = ""; |
|
133 } catch (e) { dialog.strings[stringId] = ""; } |
|
134 } |
|
135 return dialog.strings[stringId]; |
|
136 } |
|
137 |
|
138 // If the user presses cancel, tell the app launcher and close the dialog... |
|
139 function onCancel () |
|
140 { |
|
141 // Cancel app launcher. |
|
142 try { |
|
143 printProgress.processCanceledByUser = true; |
|
144 } |
|
145 catch(e) {return true;} |
|
146 |
|
147 // don't Close up dialog by returning false, the backend will close the dialog when everything will be aborted. |
|
148 return false; |
|
149 } |
|
150 |
|
151 function doneIniting() |
|
152 { |
|
153 // called by function timeout in onLoad |
|
154 printProgress.doneIniting(); |
|
155 } |