toolkit/components/downloads/nsDownloadManagerUI.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/downloads/nsDownloadManagerUI.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,113 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
     1.9 +
    1.10 +////////////////////////////////////////////////////////////////////////////////
    1.11 +//// Constants
    1.12 +
    1.13 +const Cc = Components.classes;
    1.14 +const Ci = Components.interfaces;
    1.15 +const Cr = Components.results;
    1.16 +const DOWNLOAD_MANAGER_URL = "chrome://mozapps/content/downloads/downloads.xul";
    1.17 +const PREF_FLASH_COUNT = "browser.download.manager.flashCount";
    1.18 +
    1.19 +////////////////////////////////////////////////////////////////////////////////
    1.20 +//// nsDownloadManagerUI class
    1.21 +
    1.22 +function nsDownloadManagerUI() {}
    1.23 +
    1.24 +nsDownloadManagerUI.prototype = {
    1.25 +  classID: Components.ID("7dfdf0d1-aff6-4a34-bad1-d0fe74601642"),
    1.26 +
    1.27 +  //////////////////////////////////////////////////////////////////////////////
    1.28 +  //// nsIDownloadManagerUI
    1.29 +
    1.30 +  show: function show(aWindowContext, aDownload, aReason, aUsePrivateUI)
    1.31 +  {
    1.32 +    if (!aReason)
    1.33 +      aReason = Ci.nsIDownloadManagerUI.REASON_USER_INTERACTED;
    1.34 +
    1.35 +    // First we see if it is already visible
    1.36 +    let window = this.recentWindow;
    1.37 +    if (window) {
    1.38 +      window.focus();
    1.39 +      
    1.40 +      // If we are being asked to show again, with a user interaction reason,
    1.41 +      // set the appropriate variable.
    1.42 +      if (aReason == Ci.nsIDownloadManagerUI.REASON_USER_INTERACTED)
    1.43 +        window.gUserInteracted = true;
    1.44 +      return;
    1.45 +    }
    1.46 +
    1.47 +    let parent = null;
    1.48 +    // We try to get a window to use as the parent here.  If we don't have one,
    1.49 +    // the download manager will close immediately after opening if the pref
    1.50 +    // browser.download.manager.closeWhenDone is set to true.
    1.51 +    try {
    1.52 +      if (aWindowContext)
    1.53 +        parent = aWindowContext.getInterface(Ci.nsIDOMWindow);
    1.54 +    } catch (e) { /* it's OK to not have a parent window */ }
    1.55 +
    1.56 +    // We pass the download manager and the nsIDownload we want selected (if any)
    1.57 +    var params = Cc["@mozilla.org/array;1"].createInstance(Ci.nsIMutableArray);
    1.58 +    params.appendElement(aDownload, false);
    1.59 +
    1.60 +    // Pass in the reason as well
    1.61 +    let reason = Cc["@mozilla.org/supports-PRInt16;1"].
    1.62 +                 createInstance(Ci.nsISupportsPRInt16);
    1.63 +    reason.data = aReason;
    1.64 +    params.appendElement(reason, false);
    1.65 +
    1.66 +    var ww = Cc["@mozilla.org/embedcomp/window-watcher;1"].
    1.67 +             getService(Ci.nsIWindowWatcher);
    1.68 +    ww.openWindow(parent,
    1.69 +                  DOWNLOAD_MANAGER_URL,
    1.70 +                  "Download:Manager",
    1.71 +                  "chrome,dialog=no,resizable",
    1.72 +                  params);
    1.73 +  },
    1.74 +
    1.75 +  get visible() {
    1.76 +    return (null != this.recentWindow);
    1.77 +  },
    1.78 +
    1.79 +  getAttention: function getAttention()
    1.80 +  {
    1.81 +    if (!this.visible)
    1.82 +      throw Cr.NS_ERROR_UNEXPECTED;
    1.83 +
    1.84 +    var prefs = Cc["@mozilla.org/preferences-service;1"].
    1.85 +                getService(Ci.nsIPrefBranch);
    1.86 +    // This preference may not be set, so defaulting to two.
    1.87 +    let flashCount = 2;
    1.88 +    try {
    1.89 +      flashCount = prefs.getIntPref(PREF_FLASH_COUNT);
    1.90 +    } catch (e) { }
    1.91 +
    1.92 +    var win = this.recentWindow.QueryInterface(Ci.nsIDOMChromeWindow);
    1.93 +    win.getAttentionWithCycleCount(flashCount);
    1.94 +  },
    1.95 +
    1.96 +  //////////////////////////////////////////////////////////////////////////////
    1.97 +  //// nsDownloadManagerUI
    1.98 +
    1.99 +  get recentWindow() {
   1.100 +    var wm = Cc["@mozilla.org/appshell/window-mediator;1"].
   1.101 +             getService(Ci.nsIWindowMediator);
   1.102 +    return wm.getMostRecentWindow("Download:Manager");
   1.103 +  },
   1.104 +
   1.105 +  //////////////////////////////////////////////////////////////////////////////
   1.106 +  //// nsISupports
   1.107 +
   1.108 +  QueryInterface: XPCOMUtils.generateQI([Ci.nsIDownloadManagerUI])
   1.109 +};
   1.110 +
   1.111 +////////////////////////////////////////////////////////////////////////////////
   1.112 +//// Module
   1.113 +
   1.114 +let components = [nsDownloadManagerUI];
   1.115 +this.NSGetFactory = XPCOMUtils.generateNSGetFactory(components);
   1.116 +

mercurial