michael@0: // -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: // dialog is just an array we'll use to store various properties from the dialog document... michael@0: var dialog; michael@0: michael@0: // the printProgress is a nsIPrintProgress object michael@0: var printProgress = null; michael@0: michael@0: // random global variables... michael@0: var targetFile; michael@0: michael@0: var docTitle = ""; michael@0: var docURL = ""; michael@0: var progressParams = null; michael@0: michael@0: function ellipseString(aStr, doFront) michael@0: { michael@0: if (aStr.length > 3 && (aStr.substr(0, 3) == "..." || aStr.substr(aStr.length-4, 3) == "...")) michael@0: return aStr; michael@0: michael@0: var fixedLen = 64; michael@0: if (aStr.length <= fixedLen) michael@0: return aStr; michael@0: michael@0: if (doFront) michael@0: return "..." + aStr.substr(aStr.length-fixedLen, fixedLen); michael@0: michael@0: return aStr.substr(0, fixedLen) + "..."; michael@0: } michael@0: michael@0: // all progress notifications are done through the nsIWebProgressListener implementation... michael@0: var progressListener = { michael@0: michael@0: onStateChange: function (aWebProgress, aRequest, aStateFlags, aStatus) michael@0: { michael@0: if (aStateFlags & Components.interfaces.nsIWebProgressListener.STATE_STOP) michael@0: window.close(); michael@0: }, michael@0: michael@0: onProgressChange: function (aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress) michael@0: { michael@0: if (!progressParams) michael@0: return; michael@0: var docTitleStr = ellipseString(progressParams.docTitle, false); michael@0: if (docTitleStr != docTitle) { michael@0: docTitle = docTitleStr; michael@0: dialog.title.value = docTitle; michael@0: } michael@0: var docURLStr = ellipseString(progressParams.docURL, true); michael@0: if (docURLStr != docURL && dialog.title != null) { michael@0: docURL = docURLStr; michael@0: if (docTitle == "") michael@0: dialog.title.value = docURLStr; michael@0: } michael@0: }, michael@0: michael@0: onLocationChange: function (aWebProgress, aRequest, aLocation, aFlags) {}, michael@0: onSecurityChange: function (aWebProgress, aRequest, state) {}, michael@0: michael@0: onStatusChange: function (aWebProgress, aRequest, aStatus, aMessage) michael@0: { michael@0: if (aMessage) michael@0: dialog.title.setAttribute("value", aMessage); michael@0: }, michael@0: michael@0: QueryInterface: function (iid) michael@0: { michael@0: if (iid.equals(Components.interfaces.nsIWebProgressListener) || iid.equals(Components.interfaces.nsISupportsWeakReference)) michael@0: return this; michael@0: throw Components.results.NS_NOINTERFACE; michael@0: } michael@0: } michael@0: michael@0: function onLoad() { michael@0: // Set global variables. michael@0: printProgress = window.arguments[0]; michael@0: if (window.arguments[1]) { michael@0: progressParams = window.arguments[1].QueryInterface(Components.interfaces.nsIPrintProgressParams) michael@0: if (progressParams) { michael@0: docTitle = ellipseString(progressParams.docTitle, false); michael@0: docURL = ellipseString(progressParams.docURL, true); michael@0: } michael@0: } michael@0: michael@0: if (!printProgress) { michael@0: dump( "Invalid argument to printPreviewProgress.xul\n" ); michael@0: window.close() michael@0: return; michael@0: } michael@0: michael@0: dialog = new Object; michael@0: dialog.strings = new Array; michael@0: dialog.title = document.getElementById("dialog.title"); michael@0: dialog.titleLabel = document.getElementById("dialog.titleLabel"); michael@0: michael@0: dialog.title.value = docTitle; michael@0: michael@0: // set our web progress listener on the helper app launcher michael@0: printProgress.registerListener(progressListener); michael@0: moveToAlertPosition(); michael@0: michael@0: //We need to delay the set title else dom will overwrite it michael@0: window.setTimeout(doneIniting, 100); michael@0: } michael@0: michael@0: function onUnload() michael@0: { michael@0: if (!printProgress) michael@0: return; michael@0: try { michael@0: printProgress.unregisterListener(progressListener); michael@0: printProgress = null; michael@0: } michael@0: catch(e) {} michael@0: } michael@0: michael@0: function getString (stringId) { michael@0: // Check if we've fetched this string already. michael@0: if (!(stringId in dialog.strings)) { michael@0: // Try to get it. michael@0: var elem = document.getElementById( "dialog.strings."+stringId); michael@0: try { michael@0: if (elem && elem.childNodes && elem.childNodes[0] && michael@0: elem.childNodes[0].nodeValue) michael@0: dialog.strings[stringId] = elem.childNodes[0].nodeValue; michael@0: // If unable to fetch string, use an empty string. michael@0: else michael@0: dialog.strings[stringId] = ""; michael@0: } catch (e) { dialog.strings[stringId] = ""; } michael@0: } michael@0: return dialog.strings[stringId]; michael@0: } michael@0: michael@0: // If the user presses cancel, tell the app launcher and close the dialog... michael@0: function onCancel () michael@0: { michael@0: // Cancel app launcher. michael@0: try { michael@0: printProgress.processCanceledByUser = true; michael@0: } michael@0: catch(e) {return true;} michael@0: michael@0: // don't Close up dialog by returning false, the backend will close the dialog when everything will be aborted. michael@0: return false; michael@0: } michael@0: michael@0: function doneIniting() michael@0: { michael@0: // called by function timeout in onLoad michael@0: printProgress.doneIniting(); michael@0: }