Commit 0076ed20 authored by Paolo Amadini's avatar Paolo Amadini
Browse files

Bug 1457027 - Part 5 - Move _describeType to HandlerInfoWrapper. r=jaws

MozReview-Commit-ID: NjmxPwLngc

--HG--
extra : rebase_source : 597e6eb0aa0c03626e5ca3b30735143ddcecb527
parent b9c14b46
Loading
Loading
Loading
Loading
+4 −5
Original line number Diff line number Diff line
@@ -15,18 +15,17 @@ var gAppManagerDialog = {
    this.handlerInfo = window.arguments[0];
    Services.scriptloader.loadSubScript("chrome://browser/content/preferences/in-content/main.js",
      window);
    var pane = gMainPane;

    const appDescElem = document.getElementById("appDescription");
    if (this.handlerInfo.type == TYPE_MAYBE_FEED) {
      document.l10n.setAttributes(appDescElem, "app-manager-handle-webfeeds");
    } else if (this.handlerInfo.wrappedHandlerInfo instanceof Ci.nsIMIMEInfo) {
      document.l10n.setAttributes(appDescElem, "app-manager-handle-file", {
        type: pane._describeType(this.handlerInfo)
        type: this.handlerInfo.typeDescription,
      });
    } else {
      document.l10n.setAttributes(appDescElem, "app-manager-handle-protocol", {
        type: pane._describeType(this.handlerInfo)
        type: this.handlerInfo.typeDescription,
      });
    }

@@ -34,12 +33,12 @@ var gAppManagerDialog = {
    var apps = this.handlerInfo.possibleApplicationHandlers.enumerate();
    while (apps.hasMoreElements()) {
      let app = apps.getNext();
      if (!pane.isValidHandlerApp(app))
      if (!gMainPane.isValidHandlerApp(app))
        continue;

      app.QueryInterface(Ci.nsIHandlerApp);
      var item = list.appendItem(app.name);
      item.setAttribute("image", pane._getIconURLForHandlerApp(app));
      item.setAttribute("image", gMainPane._getIconURLForHandlerApp(app));
      item.className = "listitem-iconic";
      item.app = app;
    }
+40 −35
Original line number Diff line number Diff line
@@ -258,12 +258,6 @@ var gMainPane = {
  // that match that string.
  _visibleTypes: [],

  // A count of the number of times each visible type description appears.
  // We use these counts to determine whether or not to annotate descriptions
  // with their types to distinguish duplicate descriptions from each other.
  // A hash of integer counts, indexed by string description.
  _visibleTypeDescriptionCount: {},

  // browser.startup.page values
  STARTUP_PREF_BLANK: 0,
  STARTUP_PREF_HOMEPAGE: 1,
@@ -1453,9 +1447,13 @@ var gMainPane = {
  // View Construction

  async _rebuildVisibleTypes() {
    // Reset the list of visible types and the visible type description counts.
    this._visibleTypes = [];
    this._visibleTypeDescriptionCount = {};

    // Map whose keys are string descriptions and values are references to the
    // first visible HandlerInfoWrapper that has this description. We use this
    // to determine whether or not to annotate descriptions with their types to
    // distinguish duplicate descriptions from each other.
    let visibleDescriptions = new Map();

    // Get the preferences that help determine what types to show.
    var showPlugins = Services.prefs.getBoolPref(PREF_SHOW_PLUGINS_IN_LIST);
@@ -1489,10 +1487,19 @@ var gMainPane = {
      // We couldn't find any reason to exclude the type, so include it.
      this._visibleTypes.push(handlerInfo);

      if (handlerInfo.description in this._visibleTypeDescriptionCount)
        this._visibleTypeDescriptionCount[handlerInfo.description]++;
      else
        this._visibleTypeDescriptionCount[handlerInfo.description] = 1;
      let otherHandlerInfo = visibleDescriptions.get(handlerInfo.description);
      if (!otherHandlerInfo) {
        // This is the first type with this description that we encountered
        // while rebuilding the _visibleTypes array this time. Make sure the
        // flag is reset so we won't add the type to the description.
        handlerInfo.disambiguateDescription = false;
        visibleDescriptions.set(handlerInfo.description, handlerInfo);
      } else {
        // There is at least another type with this description. Make sure we
        // add the type to the description on both HandlerInfoWrapper objects.
        handlerInfo.disambiguateDescription = true;
        otherHandlerInfo.disambiguateDescription = true;
      }
    }
  },

@@ -1513,7 +1520,7 @@ var gMainPane = {
    for (let visibleType of visibleTypes) {
      let item = document.createElement("richlistitem");
      item.setAttribute("type", visibleType.type);
      item.setAttribute("typeDescription", this._describeType(visibleType));
      item.setAttribute("typeDescription", visibleType.typeDescription);
      if (visibleType.smallIcon)
        item.setAttribute("typeIcon", visibleType.smallIcon);
      item.setAttribute("actionDescription",
@@ -1534,29 +1541,10 @@ var gMainPane = {

  _matchesFilter(aType) {
    var filterValue = this._filter.value.toLowerCase();
    return this._describeType(aType).toLowerCase().includes(filterValue) ||
    return aType.typeDescription.toLowerCase().includes(filterValue) ||
      this._describePreferredAction(aType).toLowerCase().includes(filterValue);
  },

  /**
   * Describe, in a human-readable fashion, the type represented by the given
   * handler info object.  Normally this is just the description provided by
   * the info object, but if more than one object presents the same description,
   * then we annotate the duplicate descriptions with the type itself to help
   * users distinguish between those types.
   *
   * @param aHandlerInfo {nsIHandlerInfo} the type being described
   * @returns {string} a description of the type
   */
  _describeType(aHandlerInfo) {
    if (this._visibleTypeDescriptionCount[aHandlerInfo.description] > 1)
      return gMainPane._prefsBundle.getFormattedString("typeDescriptionWithType",
        [aHandlerInfo.description,
        aHandlerInfo.type]);

    return aHandlerInfo.description;
  },

  /**
   * Describe, in a human-readable fashion, the preferred action to take on
   * the type represented by the given handler info object.
@@ -1948,8 +1936,8 @@ var gMainPane = {
    var t = this;

    function sortByType(a, b) {
      return t._describeType(a).toLowerCase().
        localeCompare(t._describeType(b).toLowerCase());
      return a.typeDescription.toLowerCase().
        localeCompare(b.typeDescription.toLowerCase());
    }

    function sortByAction(a, b) {
@@ -2628,6 +2616,7 @@ class HandlerInfoWrapper {
  constructor(type, handlerInfo) {
    this.type = type;
    this.wrappedHandlerInfo = handlerInfo;
    this.disambiguateDescription = false;

    // A plugin that can handle this type, if any.
    //
@@ -2664,6 +2653,22 @@ class HandlerInfoWrapper {
    return this.type;
  }

  /**
   * Describe, in a human-readable fashion, the type represented by the given
   * handler info object.  Normally this is just the description, but if more
   * than one object presents the same description, "disambiguateDescription"
   * is set and we annotate the duplicate descriptions with the type itself
   * to help users distinguish between those types.
   */
  get typeDescription() {
    if (this.disambiguateDescription) {
      return gMainPane._prefsBundle.getFormattedString(
        "typeDescriptionWithType", [this.description, this.type]);
    }

    return this.description;
  }

  get preferredApplicationHandler() {
    return this.wrappedHandlerInfo.preferredApplicationHandler;
  }