toolkit/components/printing/content/printProgress.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/printing/content/printProgress.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,293 @@
     1.4 +// -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     1.5 +
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +// dialog is just an array we'll use to store various properties from the dialog document...
    1.11 +var dialog;
    1.12 +
    1.13 +// the printProgress is a nsIPrintProgress object
    1.14 +var printProgress = null; 
    1.15 +
    1.16 +// random global variables...
    1.17 +var targetFile;
    1.18 +
    1.19 +var docTitle = "";
    1.20 +var docURL   = "";
    1.21 +var progressParams = null;
    1.22 +var switchUI = true;
    1.23 +
    1.24 +function ellipseString(aStr, doFront)
    1.25 +{
    1.26 +  if (aStr.length > 3 && (aStr.substr(0, 3) == "..." || aStr.substr(aStr.length-4, 3) == "...")) {
    1.27 +    return aStr;
    1.28 +  }
    1.29 +
    1.30 +  var fixedLen = 64;
    1.31 +  if (aStr.length > fixedLen) {
    1.32 +    if (doFront) {
    1.33 +      var endStr = aStr.substr(aStr.length-fixedLen, fixedLen);
    1.34 +      var str = "..." + endStr;
    1.35 +      return str;
    1.36 +    } else {
    1.37 +      var frontStr = aStr.substr(0, fixedLen);
    1.38 +      var str = frontStr + "...";
    1.39 +      return str;
    1.40 +    }
    1.41 +  }
    1.42 +  return aStr;
    1.43 +}
    1.44 +
    1.45 +// all progress notifications are done through the nsIWebProgressListener implementation...
    1.46 +var progressListener = {
    1.47 +    onStateChange: function(aWebProgress, aRequest, aStateFlags, aStatus)
    1.48 +    {
    1.49 +      if (aStateFlags & Components.interfaces.nsIWebProgressListener.STATE_START)
    1.50 +      {
    1.51 +        // Put progress meter in undetermined mode.
    1.52 +        // dialog.progress.setAttribute( "value", 0 );
    1.53 +        dialog.progress.setAttribute( "mode", "undetermined" );
    1.54 +      }
    1.55 +      
    1.56 +      if (aStateFlags & Components.interfaces.nsIWebProgressListener.STATE_STOP)
    1.57 +      {
    1.58 +        // we are done printing 
    1.59 +        // Indicate completion in title area.
    1.60 +        var msg = getString( "printComplete" );
    1.61 +        dialog.title.setAttribute("value", msg);
    1.62 +
    1.63 +        // Put progress meter at 100%.
    1.64 +        dialog.progress.setAttribute( "value", 100 );
    1.65 +        dialog.progress.setAttribute( "mode", "normal" );
    1.66 +        var percentPrint = getString( "progressText" );
    1.67 +        percentPrint = replaceInsert( percentPrint, 1, 100 );
    1.68 +        dialog.progressText.setAttribute("value", percentPrint);
    1.69 +
    1.70 +        var fm = Components.classes["@mozilla.org/focus-manager;1"]
    1.71 +                     .getService(Components.interfaces.nsIFocusManager);
    1.72 +        if (fm && fm.activeWindow == window) {
    1.73 +          // This progress dialog is the currently active window. In
    1.74 +          // this case we need to make sure that some other window
    1.75 +          // gets focus before we close this dialog to work around the
    1.76 +          // buggy Windows XP Fax dialog, which ends up parenting
    1.77 +          // itself to the currently focused window and is unable to
    1.78 +          // survive that window going away. What happens without this
    1.79 +          // opener.focus() call on Windows XP is that the fax dialog
    1.80 +          // is opened only to go away when this dialog actually
    1.81 +          // closes (which can happen asynchronously, so the fax
    1.82 +          // dialog just flashes once and then goes away), so w/o this
    1.83 +          // fix, it's impossible to fax on Windows XP w/o manually
    1.84 +          // switching focus to another window (or holding on to the
    1.85 +          // progress dialog with the mouse long enough).
    1.86 +          opener.focus();
    1.87 +        }
    1.88 +
    1.89 +        window.close();
    1.90 +      }
    1.91 +    },
    1.92 +    
    1.93 +    onProgressChange: function(aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress)
    1.94 +    {
    1.95 +      if (switchUI) 
    1.96 +      {
    1.97 +        dialog.tempLabel.setAttribute("hidden", "true");
    1.98 +        dialog.progress.setAttribute("hidden", "false");
    1.99 +        dialog.cancel.setAttribute("disabled", "false");
   1.100 +
   1.101 +        var progressLabel = getString("progress");
   1.102 +        if (progressLabel == "") {
   1.103 +          progressLabel = "Progress:"; // better than nothing
   1.104 +        }
   1.105 +        switchUI = false;
   1.106 +      }
   1.107 +
   1.108 +      if (progressParams)
   1.109 +      {
   1.110 +        var docTitleStr = ellipseString(progressParams.docTitle, false);
   1.111 +        if (docTitleStr != docTitle) {
   1.112 +          docTitle = docTitleStr;
   1.113 +          dialog.title.value = docTitle;
   1.114 +        }
   1.115 +        var docURLStr = progressParams.docURL;
   1.116 +        if (docURLStr != docURL && dialog.title != null) {
   1.117 +          docURL = docURLStr;
   1.118 +          if (docTitle == "") {
   1.119 +            dialog.title.value = ellipseString(docURLStr, true);
   1.120 +          }
   1.121 +        }
   1.122 +      }
   1.123 +
   1.124 +      // Calculate percentage.
   1.125 +      var percent;
   1.126 +      if ( aMaxTotalProgress > 0 ) 
   1.127 +      {
   1.128 +        percent = Math.round( (aCurTotalProgress*100)/aMaxTotalProgress );
   1.129 +        if ( percent > 100 )
   1.130 +          percent = 100;
   1.131 +        
   1.132 +        dialog.progress.removeAttribute( "mode");
   1.133 +        
   1.134 +        // Advance progress meter.
   1.135 +        dialog.progress.setAttribute( "value", percent );
   1.136 +
   1.137 +        // Update percentage label on progress meter.
   1.138 +        var percentPrint = getString( "progressText" );
   1.139 +        percentPrint = replaceInsert( percentPrint, 1, percent );
   1.140 +        dialog.progressText.setAttribute("value", percentPrint);
   1.141 +      } 
   1.142 +      else 
   1.143 +      {
   1.144 +        // Progress meter should be barber-pole in this case.
   1.145 +        dialog.progress.setAttribute( "mode", "undetermined" );
   1.146 +        // Update percentage label on progress meter.
   1.147 +        dialog.progressText.setAttribute("value", "");
   1.148 +      }
   1.149 +    },
   1.150 +
   1.151 +	  onLocationChange: function(aWebProgress, aRequest, aLocation, aFlags)
   1.152 +    {
   1.153 +      // we can ignore this notification
   1.154 +    },
   1.155 +
   1.156 +    onStatusChange: function(aWebProgress, aRequest, aStatus, aMessage)
   1.157 +    {
   1.158 +      if (aMessage != "")
   1.159 +        dialog.title.setAttribute("value", aMessage);
   1.160 +    },
   1.161 +
   1.162 +    onSecurityChange: function(aWebProgress, aRequest, state)
   1.163 +    {
   1.164 +      // we can ignore this notification
   1.165 +    },
   1.166 +
   1.167 +    QueryInterface : function(iid)
   1.168 +    {
   1.169 +     if (iid.equals(Components.interfaces.nsIWebProgressListener) || iid.equals(Components.interfaces.nsISupportsWeakReference))
   1.170 +      return this;
   1.171 +     
   1.172 +     throw Components.results.NS_NOINTERFACE;
   1.173 +    }
   1.174 +};
   1.175 +
   1.176 +function getString( stringId ) {
   1.177 +   // Check if we've fetched this string already.
   1.178 +   if (!(stringId in dialog.strings)) {
   1.179 +      // Try to get it.
   1.180 +      var elem = document.getElementById( "dialog.strings."+stringId );
   1.181 +      try {
   1.182 +        if ( elem
   1.183 +           &&
   1.184 +           elem.childNodes
   1.185 +           &&
   1.186 +           elem.childNodes[0]
   1.187 +           &&
   1.188 +           elem.childNodes[0].nodeValue ) {
   1.189 +         dialog.strings[ stringId ] = elem.childNodes[0].nodeValue;
   1.190 +        } else {
   1.191 +          // If unable to fetch string, use an empty string.
   1.192 +          dialog.strings[ stringId ] = "";
   1.193 +        }
   1.194 +      } catch (e) { dialog.strings[ stringId ] = ""; }
   1.195 +   }
   1.196 +   return dialog.strings[ stringId ];
   1.197 +}
   1.198 +
   1.199 +function loadDialog() 
   1.200 +{
   1.201 +}
   1.202 +
   1.203 +function replaceInsert( text, index, value ) {
   1.204 +   var result = text;
   1.205 +   var regExp = new RegExp( "#"+index );
   1.206 +   result = result.replace( regExp, value );
   1.207 +   return result;
   1.208 +}
   1.209 +
   1.210 +function onLoad() {
   1.211 +
   1.212 +    // Set global variables.
   1.213 +    printProgress = window.arguments[0];
   1.214 +    if (window.arguments[1])
   1.215 +    {
   1.216 +      progressParams = window.arguments[1].QueryInterface(Components.interfaces.nsIPrintProgressParams)
   1.217 +      if (progressParams)
   1.218 +      {
   1.219 +        docTitle = ellipseString(progressParams.docTitle, false);
   1.220 +        docURL   = ellipseString(progressParams.docURL, true);
   1.221 +      }
   1.222 +    }
   1.223 +
   1.224 +    if ( !printProgress ) {
   1.225 +        dump( "Invalid argument to printProgress.xul\n" );
   1.226 +        window.close()
   1.227 +        return;
   1.228 +    }
   1.229 +
   1.230 +    dialog = new Object;
   1.231 +    dialog.strings = new Array;
   1.232 +    dialog.title        = document.getElementById("dialog.title");
   1.233 +    dialog.titleLabel   = document.getElementById("dialog.titleLabel");
   1.234 +    dialog.progress     = document.getElementById("dialog.progress");
   1.235 +    dialog.progressText = document.getElementById("dialog.progressText");
   1.236 +    dialog.progressLabel = document.getElementById("dialog.progressLabel");
   1.237 +    dialog.tempLabel    = document.getElementById("dialog.tempLabel");
   1.238 +    dialog.cancel       = document.getElementById("cancel");
   1.239 +
   1.240 +    dialog.progress.setAttribute("hidden", "true");
   1.241 +    dialog.cancel.setAttribute("disabled", "true");
   1.242 +
   1.243 +    var progressLabel = getString("preparing");
   1.244 +    if (progressLabel == "") {
   1.245 +      progressLabel = "Preparing..."; // better than nothing
   1.246 +    }
   1.247 +    dialog.tempLabel.value = progressLabel;
   1.248 +
   1.249 +    dialog.title.value = docTitle;
   1.250 +
   1.251 +    // Set up dialog button callbacks.
   1.252 +    var object = this;
   1.253 +    doSetOKCancel("", function () { return object.onCancel();});
   1.254 +
   1.255 +    // Fill dialog.
   1.256 +    loadDialog();
   1.257 +
   1.258 +    // set our web progress listener on the helper app launcher
   1.259 +    printProgress.registerListener(progressListener);
   1.260 +    moveToAlertPosition();
   1.261 +    //We need to delay the set title else dom will overwrite it
   1.262 +    window.setTimeout(doneIniting, 500);
   1.263 +}
   1.264 +
   1.265 +function onUnload() 
   1.266 +{
   1.267 +  if (printProgress)
   1.268 +  {
   1.269 +   try 
   1.270 +   {
   1.271 +     printProgress.unregisterListener(progressListener);
   1.272 +     printProgress = null;
   1.273 +   }
   1.274 +    
   1.275 +   catch( exception ) {}
   1.276 +  }
   1.277 +}
   1.278 +
   1.279 +// If the user presses cancel, tell the app launcher and close the dialog...
   1.280 +function onCancel () 
   1.281 +{
   1.282 +  // Cancel app launcher.
   1.283 +   try 
   1.284 +   {
   1.285 +     printProgress.processCanceledByUser = true;
   1.286 +   }
   1.287 +   catch( exception ) {return true;}
   1.288 +    
   1.289 +  // don't Close up dialog by returning false, the backend will close the dialog when everything will be aborted.
   1.290 +  return false;
   1.291 +}
   1.292 +
   1.293 +function doneIniting() 
   1.294 +{
   1.295 +  printProgress.doneIniting();
   1.296 +}

mercurial