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: var switchUI = true; 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: michael@0: var fixedLen = 64; michael@0: if (aStr.length > fixedLen) { michael@0: if (doFront) { michael@0: var endStr = aStr.substr(aStr.length-fixedLen, fixedLen); michael@0: var str = "..." + endStr; michael@0: return str; michael@0: } else { michael@0: var frontStr = aStr.substr(0, fixedLen); michael@0: var str = frontStr + "..."; michael@0: return str; michael@0: } michael@0: } michael@0: return aStr; michael@0: } michael@0: michael@0: // all progress notifications are done through the nsIWebProgressListener implementation... michael@0: var progressListener = { michael@0: onStateChange: function(aWebProgress, aRequest, aStateFlags, aStatus) michael@0: { michael@0: if (aStateFlags & Components.interfaces.nsIWebProgressListener.STATE_START) michael@0: { michael@0: // Put progress meter in undetermined mode. michael@0: // dialog.progress.setAttribute( "value", 0 ); michael@0: dialog.progress.setAttribute( "mode", "undetermined" ); michael@0: } michael@0: michael@0: if (aStateFlags & Components.interfaces.nsIWebProgressListener.STATE_STOP) michael@0: { michael@0: // we are done printing michael@0: // Indicate completion in title area. michael@0: var msg = getString( "printComplete" ); michael@0: dialog.title.setAttribute("value", msg); michael@0: michael@0: // Put progress meter at 100%. michael@0: dialog.progress.setAttribute( "value", 100 ); michael@0: dialog.progress.setAttribute( "mode", "normal" ); michael@0: var percentPrint = getString( "progressText" ); michael@0: percentPrint = replaceInsert( percentPrint, 1, 100 ); michael@0: dialog.progressText.setAttribute("value", percentPrint); michael@0: michael@0: var fm = Components.classes["@mozilla.org/focus-manager;1"] michael@0: .getService(Components.interfaces.nsIFocusManager); michael@0: if (fm && fm.activeWindow == window) { michael@0: // This progress dialog is the currently active window. In michael@0: // this case we need to make sure that some other window michael@0: // gets focus before we close this dialog to work around the michael@0: // buggy Windows XP Fax dialog, which ends up parenting michael@0: // itself to the currently focused window and is unable to michael@0: // survive that window going away. What happens without this michael@0: // opener.focus() call on Windows XP is that the fax dialog michael@0: // is opened only to go away when this dialog actually michael@0: // closes (which can happen asynchronously, so the fax michael@0: // dialog just flashes once and then goes away), so w/o this michael@0: // fix, it's impossible to fax on Windows XP w/o manually michael@0: // switching focus to another window (or holding on to the michael@0: // progress dialog with the mouse long enough). michael@0: opener.focus(); michael@0: } michael@0: michael@0: window.close(); michael@0: } michael@0: }, michael@0: michael@0: onProgressChange: function(aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress) michael@0: { michael@0: if (switchUI) michael@0: { michael@0: dialog.tempLabel.setAttribute("hidden", "true"); michael@0: dialog.progress.setAttribute("hidden", "false"); michael@0: dialog.cancel.setAttribute("disabled", "false"); michael@0: michael@0: var progressLabel = getString("progress"); michael@0: if (progressLabel == "") { michael@0: progressLabel = "Progress:"; // better than nothing michael@0: } michael@0: switchUI = false; michael@0: } michael@0: michael@0: if (progressParams) michael@0: { 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 = progressParams.docURL; michael@0: if (docURLStr != docURL && dialog.title != null) { michael@0: docURL = docURLStr; michael@0: if (docTitle == "") { michael@0: dialog.title.value = ellipseString(docURLStr, true); michael@0: } michael@0: } michael@0: } michael@0: michael@0: // Calculate percentage. michael@0: var percent; michael@0: if ( aMaxTotalProgress > 0 ) michael@0: { michael@0: percent = Math.round( (aCurTotalProgress*100)/aMaxTotalProgress ); michael@0: if ( percent > 100 ) michael@0: percent = 100; michael@0: michael@0: dialog.progress.removeAttribute( "mode"); michael@0: michael@0: // Advance progress meter. michael@0: dialog.progress.setAttribute( "value", percent ); michael@0: michael@0: // Update percentage label on progress meter. michael@0: var percentPrint = getString( "progressText" ); michael@0: percentPrint = replaceInsert( percentPrint, 1, percent ); michael@0: dialog.progressText.setAttribute("value", percentPrint); michael@0: } michael@0: else michael@0: { michael@0: // Progress meter should be barber-pole in this case. michael@0: dialog.progress.setAttribute( "mode", "undetermined" ); michael@0: // Update percentage label on progress meter. michael@0: dialog.progressText.setAttribute("value", ""); michael@0: } michael@0: }, michael@0: michael@0: onLocationChange: function(aWebProgress, aRequest, aLocation, aFlags) michael@0: { michael@0: // we can ignore this notification michael@0: }, 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: onSecurityChange: function(aWebProgress, aRequest, state) michael@0: { michael@0: // we can ignore this notification 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: michael@0: throw Components.results.NS_NOINTERFACE; michael@0: } 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 michael@0: && michael@0: elem.childNodes michael@0: && michael@0: elem.childNodes[0] michael@0: && michael@0: elem.childNodes[0].nodeValue ) { michael@0: dialog.strings[ stringId ] = elem.childNodes[0].nodeValue; michael@0: } else { michael@0: // If unable to fetch string, use an empty string. michael@0: dialog.strings[ stringId ] = ""; michael@0: } michael@0: } catch (e) { dialog.strings[ stringId ] = ""; } michael@0: } michael@0: return dialog.strings[ stringId ]; michael@0: } michael@0: michael@0: function loadDialog() michael@0: { michael@0: } michael@0: michael@0: function replaceInsert( text, index, value ) { michael@0: var result = text; michael@0: var regExp = new RegExp( "#"+index ); michael@0: result = result.replace( regExp, value ); michael@0: return result; michael@0: } michael@0: michael@0: function onLoad() { michael@0: michael@0: // Set global variables. michael@0: printProgress = window.arguments[0]; michael@0: if (window.arguments[1]) michael@0: { michael@0: progressParams = window.arguments[1].QueryInterface(Components.interfaces.nsIPrintProgressParams) michael@0: if (progressParams) michael@0: { 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 printProgress.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: dialog.progress = document.getElementById("dialog.progress"); michael@0: dialog.progressText = document.getElementById("dialog.progressText"); michael@0: dialog.progressLabel = document.getElementById("dialog.progressLabel"); michael@0: dialog.tempLabel = document.getElementById("dialog.tempLabel"); michael@0: dialog.cancel = document.getElementById("cancel"); michael@0: michael@0: dialog.progress.setAttribute("hidden", "true"); michael@0: dialog.cancel.setAttribute("disabled", "true"); michael@0: michael@0: var progressLabel = getString("preparing"); michael@0: if (progressLabel == "") { michael@0: progressLabel = "Preparing..."; // better than nothing michael@0: } michael@0: dialog.tempLabel.value = progressLabel; michael@0: michael@0: dialog.title.value = docTitle; michael@0: michael@0: // Set up dialog button callbacks. michael@0: var object = this; michael@0: doSetOKCancel("", function () { return object.onCancel();}); michael@0: michael@0: // Fill dialog. michael@0: loadDialog(); michael@0: michael@0: // set our web progress listener on the helper app launcher michael@0: printProgress.registerListener(progressListener); michael@0: moveToAlertPosition(); michael@0: //We need to delay the set title else dom will overwrite it michael@0: window.setTimeout(doneIniting, 500); michael@0: } michael@0: michael@0: function onUnload() michael@0: { michael@0: if (printProgress) michael@0: { michael@0: try michael@0: { michael@0: printProgress.unregisterListener(progressListener); michael@0: printProgress = null; michael@0: } michael@0: michael@0: catch( exception ) {} michael@0: } 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: { michael@0: printProgress.processCanceledByUser = true; michael@0: } michael@0: catch( exception ) {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: printProgress.doneIniting(); michael@0: }