Bug 1460392 - Port bug 1457027 to TB: Part 6 - Move _describePreferredAction to HandlerInfoWrapper. r=aceman
authorRichard Marti <richard.marti@gmail.com>
Sat, 22 Sep 2018 10:05:03 +0200
changeset 33265 cc895db45e990bb2d1dada0f87b708a4310045aa
parent 33264 f686a4e62dc8801634ed319ae2c829239c5822d8
child 33266 eca38904a853f703807ce93917e6704492f476e8
push id387
push userclokep@gmail.com
push dateMon, 10 Dec 2018 21:30:47 +0000
reviewersaceman
bugs1460392, 1457027
Bug 1460392 - Port bug 1457027 to TB: Part 6 - Move _describePreferredAction to HandlerInfoWrapper. r=aceman
mail/components/preferences/applications.js
--- a/mail/components/preferences/applications.js
+++ b/mail/components/preferences/applications.js
@@ -143,16 +143,73 @@ class HandlerInfoWrapper {
     if (this.disambiguateDescription) {
       return this._prefsBundle.getFormattedString(
         "typeDetailsWithTypeAndExt", [this.description, this.type]);
     }
 
     return this.description;
   }
 
+  /**
+   * Describe, in a human-readable fashion, the preferred action to take on
+   * the type represented by the given handler info object.
+   */
+  get actionDescription() {
+    // alwaysAskBeforeHandling overrides the preferred action, so if that flag
+    // is set, then describe that behavior instead.  For most types, this is
+    // the "alwaysAsk" string, but for the feed type we show something special.
+    if (this.alwaysAskBeforeHandling) {
+      return gApplicationsPane._prefsBundle.getString("alwaysAsk");
+    }
+
+    switch (this.preferredAction) {
+      case Ci.nsIHandlerInfo.saveToDisk:
+        return gApplicationsPane._prefsBundle.getString("saveFile");
+
+      case Ci.nsIHandlerInfo.useHelperApp:
+        var preferredApp = this.preferredApplicationHandler;
+        var name;
+        if (preferredApp instanceof Ci.nsILocalHandlerApp)
+          name = getDisplayNameForFile(preferredApp.executable);
+        else
+          name = preferredApp.name;
+        return gApplicationsPane._prefsBundle.getFormattedString("useApp", [name]);
+
+      case Ci.nsIHandlerInfo.handleInternally:
+        if (this instanceof InternalHandlerInfoWrapper) {
+          return gApplicationsPane._prefsBundle.getFormattedString("previewInApp",
+            [gApplicationsPane._brandShortName]);
+        }
+
+        // For other types, handleInternally looks like either useHelperApp
+        // or useSystemDefault depending on whether or not there's a preferred
+        // handler app.
+        if (gApplicationsPane.isValidHandlerApp(this.preferredApplicationHandler))
+          return this.preferredApplicationHandler.name;
+
+        return this.defaultDescription;
+
+      // XXX Why don't we say the app will handle the type internally?
+      // Is it because the app can't actually do that?  But if that's true,
+      // then why would a preferredAction ever get set to this value
+      // in the first place?
+
+      case Ci.nsIHandlerInfo.useSystemDefault:
+        return gApplicationsPane._prefsBundle.getFormattedString("useDefault",
+          [this.defaultDescription]);
+
+      case kActionUsePlugin:
+        return gApplicationsPane._prefsBundle.getFormattedString("usePluginIn",
+          [this.pluginName,
+          gApplicationsPane._brandShortName]);
+      default:
+        throw new Error(`Unexpected preferredAction: ${this.preferredAction}`);
+    }
+  }
+
   get preferredApplicationHandler() {
     return this.wrappedHandlerInfo.preferredApplicationHandler;
   }
 
   set preferredApplicationHandler(aNewValue) {
     this.wrappedHandlerInfo.preferredApplicationHandler = aNewValue;
 
     // Make sure the preferred handler is in the set of possible handlers.
@@ -1040,18 +1097,17 @@ var gApplicationsPane = {
     for (let visibleType of visibleTypes) {
       let item = document.createElement("richlistitem");
       item.setAttribute("type", visibleType.type);
       item.setAttribute("typeDescription", visibleType.typeDescription);
       item.setAttribute("shortTypeDescription", visibleType.description);
       item.setAttribute("shortTypeDetails", this._typeDetails(visibleType));
       if (visibleType.smallIcon)
         item.setAttribute("typeIcon", visibleType.smallIcon);
-      item.setAttribute("actionDescription",
-                        this._describePreferredAction(visibleType));
+      item.setAttribute("actionDescription", visibleType.actionDescription);
 
       if (!this._setIconClassForPreferredAction(visibleType, item)) {
         item.setAttribute("actionIcon",
                           this._getIconURLForPreferredAction(visibleType));
       }
 
       this._list.appendChild(item);
 
@@ -1059,26 +1115,26 @@ var gApplicationsPane = {
         this._list.selectedItem = item;
       }
     }
   },
 
   _matchesFilter(aType) {
     var filterValue = this._filter.value.toLowerCase();
     return aType.typeDescription.toLowerCase().includes(filterValue) ||
-           this._describePreferredAction(aType).toLowerCase().includes(filterValue);
+           aType.actionDescription.toLowerCase().includes(filterValue);
   },
 
-  /**
-   * Get the details for the type represented by the given handler info
-   * object.
-   *
-   * @param aHandlerInfo {nsIHandlerInfo} the type to get the extensions for.
-   * @return {string} the extensions for the type
-   */
+ /**
+  * Get the details for the type represented by the given handler info
+  * object.
+  *
+  * @param aHandlerInfo {nsIHandlerInfo} the type to get the extensions for.
+  * @return {string} the extensions for the type
+  */
   _typeDetails(aHandlerInfo) {
     let exts = [];
     if (aHandlerInfo.wrappedHandlerInfo instanceof Ci.nsIMIMEInfo) {
       let extIter = aHandlerInfo.wrappedHandlerInfo.getFileExtensions();
       while (extIter.hasMore()) {
         let ext = "." + extIter.getNext();
         if (!exts.includes(ext))
           exts.push(ext);
@@ -1094,76 +1150,16 @@ var gApplicationsPane = {
       return this._prefsBundle.getFormattedString("typeDetailsWithTypeOrExt",
                                                   [ aHandlerInfo.type]);
     }
     if (exts)
       return this._prefsBundle.getFormattedString("typeDetailsWithTypeOrExt",
                                                   [exts]);
     return exts;
   },
-  /**
-   * Describe, in a human-readable fashion, the preferred action to take on
-   * the type represented by the given handler info object.
-   *
-   * XXX Should this be part of the HandlerInfoWrapper interface?  It would
-   * violate the separation of model and view, but it might make more sense
-   * nonetheless (f.e. it would make sortTypes easier).
-   *
-   * @param aHandlerInfo {nsIHandlerInfo} the type whose preferred action
-   *                                      is being described
-   * @return {string} a description of the action
-   */
-  _describePreferredAction(aHandlerInfo) {
-    // alwaysAskBeforeHandling overrides the preferred action, so if that flag
-    // is set, then describe that behavior instead.  For most types, this is
-    // the "alwaysAsk" string, but for the feed type we show something special.
-    if (aHandlerInfo.alwaysAskBeforeHandling)
-        return this._prefsBundle.getString("alwaysAsk");
-
-    switch (aHandlerInfo.preferredAction) {
-      case Ci.nsIHandlerInfo.saveToDisk:
-        return this._prefsBundle.getString("saveFile");
-
-      case Ci.nsIHandlerInfo.useHelperApp:
-        var preferredApp = aHandlerInfo.preferredApplicationHandler;
-        var name;
-        if (preferredApp instanceof Ci.nsILocalHandlerApp)
-          name = getDisplayNameForFile(preferredApp.executable);
-        else
-          name = preferredApp.name;
-        return this._prefsBundle.getFormattedString("useApp", [name]);
-
-      case Ci.nsIHandlerInfo.handleInternally:
-        // For other types, handleInternally looks like either useHelperApp
-        // or useSystemDefault depending on whether or not there's a preferred
-        // handler app.
-        if (this.isValidHandlerApp(aHandlerInfo.preferredApplicationHandler))
-          return aHandlerInfo.preferredApplicationHandler.name;
-
-        return aHandlerInfo.defaultDescription;
-
-        // XXX Why don't we say the app will handle the type internally?
-        // Is it because the app can't actually do that?  But if that's true,
-        // then why would a preferredAction ever get set to this value
-        // in the first place?
-
-      case Ci.nsIHandlerInfo.useSystemDefault:
-        return this._prefsBundle.getFormattedString("useDefault",
-                                                    [aHandlerInfo.defaultDescription]);
-
-      case kActionUsePlugin:
-        return this._prefsBundle.getFormattedString("usePluginIn",
-                                                    [aHandlerInfo.plugin.name,
-                                                     this._brandShortName]);
-      default:
-        // Hopefully this never happens.
-        Cu.reportError("No description for action " + aHandlerInfo.preferredAction + " found!");
-        return "";
-    }
-  },
 
   /**
    * Whether or not the given handler app is valid.
    * @param aHandlerApp {nsIHandlerApp} the handler app in question
    * @return {boolean} whether or not it's valid
    */
   isValidHandlerApp(aHandlerApp) {
     if (!aHandlerApp)
@@ -1258,17 +1254,17 @@ var gApplicationsPane = {
     }
 
     // Create menu items for possible handlers.
     let preferredApp = handlerInfo.preferredApplicationHandler;
     let possibleApps = handlerInfo.possibleApplicationHandlers.enumerate();
     var possibleAppMenuItems = [];
     while (possibleApps.hasMoreElements()) {
       let possibleApp = possibleApps.getNext();
-      if (!this.isValidHandlerApp(possibleApp))
+      if (!gApplicationsPane.isValidHandlerApp(possibleApp))
         continue;
 
       let menuItem = document.createElement("menuitem");
       menuItem.setAttribute("action", Ci.nsIHandlerInfo.useHelperApp);
       let label;
       if (possibleApp instanceof Ci.nsILocalHandlerApp)
         label = getDisplayNameForFile(possibleApp.executable);
       else
@@ -1395,26 +1391,24 @@ var gApplicationsPane = {
 
   /**
    * Sort the list of visible types by the current sort column/direction.
    */
   _sortVisibleTypes() {
     if (!this._sortColumn)
       return;
 
-    var t = this;
-
     function sortByType(a, b) {
       return a.typeDescription.toLowerCase()
               .localeCompare(b.typeDescription.toLowerCase());
     }
 
     function sortByAction(a, b) {
-      return t._describePreferredAction(a).toLowerCase()
-              .localeCompare(t._describePreferredAction(b).toLowerCase());
+      return a.actionDescription.toLowerCase()
+              .localeCompare(b.actionDescription.toLowerCase());
     }
 
     switch (this._sortColumn.getAttribute("value")) {
       case "type":
         this._visibleTypes.sort(sortByType);
         break;
       case "action":
         this._visibleTypes.sort(sortByAction);
@@ -1488,18 +1482,17 @@ var gApplicationsPane = {
 
     handlerInfo.store();
 
     // Make sure the handler info object is flagged to indicate that there is
     // now some user configuration for the type.
     handlerInfo.handledOnlyByPlugin = false;
 
     // Update the action label and image to reflect the new preferred action.
-    typeItem.setAttribute("actionDescription",
-                          this._describePreferredAction(handlerInfo));
+    typeItem.setAttribute("actionDescription", handlerInfo.actionDescription);
     if (!this._setIconClassForPreferredAction(handlerInfo, typeItem)) {
       typeItem.setAttribute("actionIcon",
                             this._getIconURLForPreferredAction(handlerInfo));
     }
   },
 
   manageApp(aEvent) {
     // Don't let the normal "on select action" handler get this event,
@@ -1510,18 +1503,17 @@ var gApplicationsPane = {
     var handlerInfo = this._handledTypes[typeItem.type];
 
     let closingCallback = () => {
       // Rebuild the actions menu so that we revert to the previous selection,
       // or "Always ask" if the previous default application has been removed.
       this.rebuildActionsMenu();
 
       // Update the richlistitem too. Will be visible when selecting another row.
-      typeItem.setAttribute("actionDescription",
-                            this._describePreferredAction(handlerInfo));
+      typeItem.setAttribute("actionDescription", handlerInfo.actionDescription);
       if (!this._setIconClassForPreferredAction(handlerInfo, typeItem)) {
         typeItem.setAttribute("actionIcon",
                               this._getIconURLForPreferredAction(handlerInfo));
       }
     };
 
     gSubDialog.open(
       "chrome://messenger/content/preferences/applicationManager.xul",
@@ -1669,17 +1661,17 @@ var gApplicationsPane = {
 
   _getIconURLForPreferredAction(aHandlerInfo) {
     switch (aHandlerInfo.preferredAction) {
       case Ci.nsIHandlerInfo.useSystemDefault:
         return this._getIconURLForSystemDefault(aHandlerInfo);
 
       case Ci.nsIHandlerInfo.useHelperApp:
         let preferredApp = aHandlerInfo.preferredApplicationHandler;
-        if (this.isValidHandlerApp(preferredApp))
+        if (gApplicationsPane.isValidHandlerApp(preferredApp))
           return this._getIconURLForHandlerApp(preferredApp);
     }
     // This should never happen, but if preferredAction is set to some weird
     // value, then fall back to the generic application icon.
     return ICON_URL_APP;
   },
 
   _getIconURLForHandlerApp(aHandlerApp) {