home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / firefox-3.0.14 / chrome / browser.jar / content / browser / preferences / applications.js < prev    next >
Encoding:
JavaScript  |  2008-10-24  |  67.4 KB  |  1,820 lines

  1. /*
  2. //@line 44 "/build/buildd/firefox-3.0-3.0.14+build2+nobinonly/build-tree/mozilla/browser/components/preferences/applications.js"
  3.  */
  4.  
  5. //****************************************************************************//
  6. // Constants & Enumeration Values
  7.  
  8. /*
  9. //@line 51 "/build/buildd/firefox-3.0-3.0.14+build2+nobinonly/build-tree/mozilla/browser/components/preferences/applications.js"
  10. */
  11. var Cc = Components.classes;
  12. var Ci = Components.interfaces;
  13. var Cr = Components.results;
  14. /*
  15. //@line 57 "/build/buildd/firefox-3.0-3.0.14+build2+nobinonly/build-tree/mozilla/browser/components/preferences/applications.js"
  16. */
  17.  
  18. const TYPE_MAYBE_FEED = "application/vnd.mozilla.maybe.feed";
  19. const TYPE_MAYBE_VIDEO_FEED = "application/vnd.mozilla.maybe.video.feed";
  20. const TYPE_MAYBE_AUDIO_FEED = "application/vnd.mozilla.maybe.audio.feed";
  21.  
  22. const PREF_DISABLED_PLUGIN_TYPES = "plugin.disable_full_page_plugin_for_types";
  23.  
  24. // Preferences that affect which entries to show in the list.
  25. const PREF_SHOW_PLUGINS_IN_LIST = "browser.download.show_plugins_in_list";
  26. const PREF_HIDE_PLUGINS_WITHOUT_EXTENSIONS =
  27.   "browser.download.hide_plugins_without_extensions";
  28.  
  29. /*
  30.  * Preferences where we store handling information about the feed type.
  31.  *
  32.  * browser.feeds.handler
  33.  * - "bookmarks", "reader" (clarified further using the .default preference),
  34.  *   or "ask" -- indicates the default handler being used to process feeds;
  35.  *   "bookmarks" is obsolete; to specify that the handler is bookmarks,
  36.  *   set browser.feeds.handler.default to "bookmarks";
  37.  *
  38.  * browser.feeds.handler.default
  39.  * - "bookmarks", "client" or "web" -- indicates the chosen feed reader used
  40.  *   to display feeds, either transiently (i.e., when the "use as default"
  41.  *   checkbox is unchecked, corresponds to when browser.feeds.handler=="ask")
  42.  *   or more permanently (i.e., the item displayed in the dropdown in Feeds
  43.  *   preferences)
  44.  *
  45.  * browser.feeds.handler.webservice
  46.  * - the URL of the currently selected web service used to read feeds
  47.  *
  48.  * browser.feeds.handlers.application
  49.  * - nsILocalFile, stores the current client-side feed reading app if one has
  50.  *   been chosen
  51.  */
  52. const PREF_FEED_SELECTED_APP    = "browser.feeds.handlers.application";
  53. const PREF_FEED_SELECTED_WEB    = "browser.feeds.handlers.webservice";
  54. const PREF_FEED_SELECTED_ACTION = "browser.feeds.handler";
  55. const PREF_FEED_SELECTED_READER = "browser.feeds.handler.default";
  56.  
  57. const PREF_VIDEO_FEED_SELECTED_APP    = "browser.videoFeeds.handlers.application";
  58. const PREF_VIDEO_FEED_SELECTED_WEB    = "browser.videoFeeds.handlers.webservice";
  59. const PREF_VIDEO_FEED_SELECTED_ACTION = "browser.videoFeeds.handler";
  60. const PREF_VIDEO_FEED_SELECTED_READER = "browser.videoFeeds.handler.default";
  61.  
  62. const PREF_AUDIO_FEED_SELECTED_APP    = "browser.audioFeeds.handlers.application";
  63. const PREF_AUDIO_FEED_SELECTED_WEB    = "browser.audioFeeds.handlers.webservice";
  64. const PREF_AUDIO_FEED_SELECTED_ACTION = "browser.audioFeeds.handler";
  65. const PREF_AUDIO_FEED_SELECTED_READER = "browser.audioFeeds.handler.default";
  66.  
  67. // The nsHandlerInfoAction enumeration values in nsIHandlerInfo identify
  68. // the actions the application can take with content of various types.
  69. // But since nsIHandlerInfo doesn't support plugins, there's no value
  70. // identifying the "use plugin" action, so we use this constant instead.
  71. const kActionUsePlugin = 5;
  72.  
  73. const ICON_URL_APP      = "chrome://browser/skin/preferences/application.png";
  74.  
  75. // For CSS. Can be one of "ask", "save", "plugin" or "feed". If absent, the icon URL
  76. // was set by us to a custom handler icon and CSS should not try to override it.
  77. const APP_ICON_ATTR_NAME = "appHandlerIcon";
  78.  
  79. //****************************************************************************//
  80. // Utilities
  81.  
  82. function getDisplayNameForFile(aFile) {
  83. /*
  84. //@line 149 "/build/buildd/firefox-3.0-3.0.14+build2+nobinonly/build-tree/mozilla/browser/components/preferences/applications.js"
  85. */
  86.  
  87.   return Cc["@mozilla.org/network/io-service;1"].
  88.          getService(Ci.nsIIOService).
  89.          newFileURI(aFile).
  90.          QueryInterface(Ci.nsIURL).
  91.          fileName;
  92. }
  93.  
  94. function getLocalHandlerApp(aFile) {
  95.   var localHandlerApp = Cc["@mozilla.org/uriloader/local-handler-app;1"].
  96.                         createInstance(Ci.nsILocalHandlerApp);
  97.   localHandlerApp.name = getDisplayNameForFile(aFile);
  98.   localHandlerApp.executable = aFile;
  99.  
  100.   return localHandlerApp;
  101. }
  102.  
  103. /**
  104.  * An enumeration of items in a JS array.
  105.  *
  106.  * FIXME: use ArrayConverter once it lands (bug 380839).
  107.  * 
  108.  * @constructor
  109.  */
  110. function ArrayEnumerator(aItems) {
  111.   this._index = 0;
  112.   this._contents = aItems;
  113. }
  114.  
  115. ArrayEnumerator.prototype = {
  116.   _index: 0,
  117.  
  118.   hasMoreElements: function() {
  119.     return this._index < this._contents.length;
  120.   },
  121.  
  122.   getNext: function() {
  123.     return this._contents[this._index++];
  124.   }
  125. };
  126.  
  127. function isFeedType(t) {
  128.   return t == TYPE_MAYBE_FEED || t == TYPE_MAYBE_VIDEO_FEED || t == TYPE_MAYBE_AUDIO_FEED;
  129. }
  130.  
  131. //****************************************************************************//
  132. // HandlerInfoWrapper
  133.  
  134. /**
  135.  * This object wraps nsIHandlerInfo with some additional functionality
  136.  * the Applications prefpane needs to display and allow modification of
  137.  * the list of handled types.
  138.  * 
  139.  * We create an instance of this wrapper for each entry we might display
  140.  * in the prefpane, and we compose the instances from various sources,
  141.  * including navigator.plugins and the handler service.
  142.  *
  143.  * We don't implement all the original nsIHandlerInfo functionality,
  144.  * just the stuff that the prefpane needs.
  145.  * 
  146.  * In theory, all of the custom functionality in this wrapper should get
  147.  * pushed down into nsIHandlerInfo eventually.
  148.  */
  149. function HandlerInfoWrapper(aType, aHandlerInfo) {
  150.   this._type = aType;
  151.   this.wrappedHandlerInfo = aHandlerInfo;
  152. }
  153.  
  154. HandlerInfoWrapper.prototype = {
  155.   // The wrapped nsIHandlerInfo object.  In general, this object is private,
  156.   // but there are a couple cases where callers access it directly for things
  157.   // we haven't (yet?) implemented, so we make it a public property.
  158.   wrappedHandlerInfo: null,
  159.  
  160.  
  161.   //**************************************************************************//
  162.   // Convenience Utils
  163.  
  164.   _handlerSvc: Cc["@mozilla.org/uriloader/handler-service;1"].
  165.                getService(Ci.nsIHandlerService),
  166.  
  167.   // Retrieve this as nsIPrefBranch and then immediately QI to nsIPrefBranch2
  168.   // so both interfaces are available to callers.
  169.   _prefSvc: Cc["@mozilla.org/preferences-service;1"].
  170.             getService(Ci.nsIPrefBranch).
  171.             QueryInterface(Ci.nsIPrefBranch2),
  172.  
  173.   _categoryMgr: Cc["@mozilla.org/categorymanager;1"].
  174.                 getService(Ci.nsICategoryManager),
  175.  
  176.   element: function(aID) {
  177.     return document.getElementById(aID);
  178.   },
  179.  
  180.  
  181.   //**************************************************************************//
  182.   // nsIHandlerInfo
  183.  
  184.   // The MIME type or protocol scheme.
  185.   _type: null,
  186.   get type() {
  187.     return this._type;
  188.   },
  189.  
  190.   get description() {
  191.     if (this.wrappedHandlerInfo.description)
  192.       return this.wrappedHandlerInfo.description;
  193.  
  194.     if (this.primaryExtension) {
  195.       var extension = this.primaryExtension.toUpperCase();
  196.       return this.element("bundlePreferences").getFormattedString("fileEnding",
  197.                                                                   [extension]);
  198.     }
  199.  
  200.     return this.type;
  201.   },
  202.  
  203.   get preferredApplicationHandler() {
  204.     return this.wrappedHandlerInfo.preferredApplicationHandler;
  205.   },
  206.  
  207.   set preferredApplicationHandler(aNewValue) {
  208.     this.wrappedHandlerInfo.preferredApplicationHandler = aNewValue;
  209.  
  210.     // Make sure the preferred handler is in the set of possible handlers.
  211.     if (aNewValue)
  212.       this.addPossibleApplicationHandler(aNewValue)
  213.   },
  214.  
  215.   get possibleApplicationHandlers() {
  216.     return this.wrappedHandlerInfo.possibleApplicationHandlers;
  217.   },
  218.  
  219.   addPossibleApplicationHandler: function(aNewHandler) {
  220.     var possibleApps = this.possibleApplicationHandlers.enumerate();
  221.     while (possibleApps.hasMoreElements()) {
  222.       if (possibleApps.getNext().equals(aNewHandler))
  223.         return;
  224.     }
  225.     this.possibleApplicationHandlers.appendElement(aNewHandler, false);
  226.   },
  227.  
  228.   removePossibleApplicationHandler: function(aHandler) {
  229.     var defaultApp = this.preferredApplicationHandler;
  230.     if (defaultApp && aHandler.equals(defaultApp)) {
  231.       // If the app we remove was the default app, we must make sure
  232.       // it won't be used anymore
  233.       this.alwaysAskBeforeHandling = true;
  234.       this.preferredApplicationHandler = null;
  235.     }
  236.  
  237.     var handlers = this.possibleApplicationHandlers;
  238.     for (var i = 0; i < handlers.length; ++i) {
  239.       var handler = handlers.queryElementAt(i, Ci.nsIHandlerApp);
  240.       if (handler.equals(aHandler)) {
  241.         handlers.removeElementAt(i);
  242.         break;
  243.       }
  244.     }
  245.   },
  246.  
  247.   get hasDefaultHandler() {
  248.     return this.wrappedHandlerInfo.hasDefaultHandler;
  249.   },
  250.  
  251.   get defaultDescription() {
  252.     return this.wrappedHandlerInfo.defaultDescription;
  253.   },
  254.  
  255.   // What to do with content of this type.
  256.   get preferredAction() {
  257.     // If we have an enabled plugin, then the action is to use that plugin.
  258.     if (this.plugin && !this.isDisabledPluginType)
  259.       return kActionUsePlugin;
  260.  
  261.     // If the action is to use a helper app, but we don't have a preferred
  262.     // handler app, then switch to using the system default, if any; otherwise
  263.     // fall back to saving to disk, which is the default action in nsMIMEInfo.
  264.     // Note: "save to disk" is an invalid value for protocol info objects,
  265.     // but the alwaysAskBeforeHandling getter will detect that situation
  266.     // and always return true in that case to override this invalid value.
  267.     if (this.wrappedHandlerInfo.preferredAction == Ci.nsIHandlerInfo.useHelperApp &&
  268.         !gApplicationsPane.isValidHandlerApp(this.preferredApplicationHandler)) {
  269.       if (this.wrappedHandlerInfo.hasDefaultHandler)
  270.         return Ci.nsIHandlerInfo.useSystemDefault;
  271.       else
  272.         return Ci.nsIHandlerInfo.saveToDisk;
  273.     }
  274.  
  275.     return this.wrappedHandlerInfo.preferredAction;
  276.   },
  277.  
  278.   set preferredAction(aNewValue) {
  279.     // We don't modify the preferred action if the new action is to use a plugin
  280.     // because handler info objects don't understand our custom "use plugin"
  281.     // value.  Also, leaving it untouched means that we can automatically revert
  282.     // to the old setting if the user ever removes the plugin.
  283.  
  284.     if (aNewValue != kActionUsePlugin)
  285.       this.wrappedHandlerInfo.preferredAction = aNewValue;
  286.   },
  287.  
  288.   get alwaysAskBeforeHandling() {
  289.     // If this type is handled only by a plugin, we can't trust the value
  290.     // in the handler info object, since it'll be a default based on the absence
  291.     // of any user configuration, and the default in that case is to always ask,
  292.     // even though we never ask for content handled by a plugin, so special case
  293.     // plugin-handled types by returning false here.
  294.     if (this.plugin && this.handledOnlyByPlugin)
  295.       return false;
  296.  
  297.     // If this is a protocol type and the preferred action is "save to disk",
  298.     // which is invalid for such types, then return true here to override that
  299.     // action.  This could happen when the preferred action is to use a helper
  300.     // app, but the preferredApplicationHandler is invalid, and there isn't
  301.     // a default handler, so the preferredAction getter returns save to disk
  302.     // instead.
  303.     if (!(this.wrappedHandlerInfo instanceof Ci.nsIMIMEInfo) &&
  304.         this.preferredAction == Ci.nsIHandlerInfo.saveToDisk)
  305.       return true;
  306.  
  307.     return this.wrappedHandlerInfo.alwaysAskBeforeHandling;
  308.   },
  309.  
  310.   set alwaysAskBeforeHandling(aNewValue) {
  311.     this.wrappedHandlerInfo.alwaysAskBeforeHandling = aNewValue;
  312.   },
  313.  
  314.  
  315.   //**************************************************************************//
  316.   // nsIMIMEInfo
  317.  
  318.   // The primary file extension associated with this type, if any.
  319.   //
  320.   // XXX Plugin objects contain an array of MimeType objects with "suffixes"
  321.   // properties; if this object has an associated plugin, shouldn't we check
  322.   // those properties for an extension?
  323.   get primaryExtension() {
  324.     try {
  325.       if (this.wrappedHandlerInfo instanceof Ci.nsIMIMEInfo &&
  326.           this.wrappedHandlerInfo.primaryExtension)
  327.         return this.wrappedHandlerInfo.primaryExtension
  328.     } catch(ex) {}
  329.  
  330.     return null;
  331.   },
  332.  
  333.  
  334.   //**************************************************************************//
  335.   // Plugin Handling
  336.  
  337.   // A plugin that can handle this type, if any.
  338.   //
  339.   // Note: just because we have one doesn't mean it *will* handle the type.
  340.   // That depends on whether or not the type is in the list of types for which
  341.   // plugin handling is disabled.
  342.   plugin: null,
  343.  
  344.   // Whether or not this type is only handled by a plugin or is also handled
  345.   // by some user-configured action as specified in the handler info object.
  346.   //
  347.   // Note: we can't just check if there's a handler info object for this type,
  348.   // because OS and user configuration is mixed up in the handler info object,
  349.   // so we always need to retrieve it for the OS info and can't tell whether
  350.   // it represents only OS-default information or user-configured information.
  351.   //
  352.   // FIXME: once handler info records are broken up into OS-provided records
  353.   // and user-configured records, stop using this boolean flag and simply
  354.   // check for the presence of a user-configured record to determine whether
  355.   // or not this type is only handled by a plugin.  Filed as bug 395142.
  356.   handledOnlyByPlugin: undefined,
  357.  
  358.   get isDisabledPluginType() {
  359.     return this._getDisabledPluginTypes().indexOf(this.type) != -1;
  360.   },
  361.  
  362.   _getDisabledPluginTypes: function() {
  363.     var types = "";
  364.  
  365.     if (this._prefSvc.prefHasUserValue(PREF_DISABLED_PLUGIN_TYPES))
  366.       types = this._prefSvc.getCharPref(PREF_DISABLED_PLUGIN_TYPES);
  367.  
  368.     // Only split if the string isn't empty so we don't end up with an array
  369.     // containing a single empty string.
  370.     if (types != "")
  371.       return types.split(",");
  372.  
  373.     return [];
  374.   },
  375.  
  376.   disablePluginType: function() {
  377.     var disabledPluginTypes = this._getDisabledPluginTypes();
  378.  
  379.     if (disabledPluginTypes.indexOf(this.type) == -1)
  380.       disabledPluginTypes.push(this.type);
  381.  
  382.     this._prefSvc.setCharPref(PREF_DISABLED_PLUGIN_TYPES,
  383.                               disabledPluginTypes.join(","));
  384.  
  385.     // Update the category manager so existing browser windows update.
  386.     this._categoryMgr.deleteCategoryEntry("Gecko-Content-Viewers",
  387.                                           this.type,
  388.                                           false);
  389.   },
  390.  
  391.   enablePluginType: function() {
  392.     var disabledPluginTypes = this._getDisabledPluginTypes();
  393.  
  394.     var type = this.type;
  395.     disabledPluginTypes = disabledPluginTypes.filter(function(v) v != type);
  396.  
  397.     this._prefSvc.setCharPref(PREF_DISABLED_PLUGIN_TYPES,
  398.                               disabledPluginTypes.join(","));
  399.  
  400.     // Update the category manager so existing browser windows update.
  401.     this._categoryMgr.
  402.       addCategoryEntry("Gecko-Content-Viewers",
  403.                        this.type,
  404.                        "@mozilla.org/content/plugin/document-loader-factory;1",
  405.                        false,
  406.                        true);
  407.   },
  408.  
  409.  
  410.   //**************************************************************************//
  411.   // Storage
  412.  
  413.   store: function() {
  414.     this._handlerSvc.store(this.wrappedHandlerInfo);
  415.   },
  416.  
  417.  
  418.   //**************************************************************************//
  419.   // Icons
  420.  
  421.   get smallIcon() {
  422.     return this._getIcon(16);
  423.   },
  424.  
  425.   get largeIcon() {
  426.     return this._getIcon(32);
  427.   },
  428.  
  429.   _getIcon: function(aSize) {
  430.     if (this.primaryExtension)
  431.       return "moz-icon://goat." + this.primaryExtension + "?size=" + aSize;
  432.  
  433.     if (this.wrappedHandlerInfo instanceof Ci.nsIMIMEInfo)
  434.       return "moz-icon://goat?size=" + aSize + "&contentType=" + this.type;
  435.  
  436.     // FIXME: consider returning some generic icon when we can't get a URL for
  437.     // one (for example in the case of protocol schemes).  Filed as bug 395141.
  438.     return null;
  439.   }
  440.  
  441. };
  442.  
  443.  
  444. //****************************************************************************//
  445. // Feed Handler Info
  446.  
  447. /**
  448.  * This object implements nsIHandlerInfo for the feed types.  It's a separate
  449.  * object because we currently store handling information for the feed type
  450.  * in a set of preferences rather than the nsIHandlerService-managed datastore.
  451.  * 
  452.  * This object inherits from HandlerInfoWrapper in order to get functionality
  453.  * that isn't special to the feed type.
  454.  * 
  455.  * XXX Should we inherit from HandlerInfoWrapper?  After all, we override
  456.  * most of that wrapper's properties and methods, and we have to dance around
  457.  * the fact that the wrapper expects to have a wrappedHandlerInfo, which we
  458.  * don't provide.
  459.  */
  460.  
  461. function FeedHandlerInfo(aMIMEType) {
  462.   HandlerInfoWrapper.call(this, aMIMEType, null);
  463. }
  464.  
  465. FeedHandlerInfo.prototype = {
  466.   __proto__: HandlerInfoWrapper.prototype,
  467.  
  468.   //**************************************************************************//
  469.   // Convenience Utils
  470.  
  471.   _converterSvc:
  472.     Cc["@mozilla.org/embeddor.implemented/web-content-handler-registrar;1"].
  473.     getService(Ci.nsIWebContentConverterService),
  474.  
  475.   _shellSvc:
  476. //@line 541 "/build/buildd/firefox-3.0-3.0.14+build2+nobinonly/build-tree/mozilla/browser/components/preferences/applications.js"
  477.     getShellService(),
  478. //@line 545 "/build/buildd/firefox-3.0-3.0.14+build2+nobinonly/build-tree/mozilla/browser/components/preferences/applications.js"
  479.  
  480.  
  481.   //**************************************************************************//
  482.   // nsIHandlerInfo
  483.  
  484.   get description() {
  485.     return this.element("bundlePreferences").getString(this._appPrefLabel);
  486.   },
  487.  
  488.   get preferredApplicationHandler() {
  489.     switch (this.element(this._prefSelectedReader).value) {
  490.       case "client":
  491.         var file = this.element(this._prefSelectedApp).value;
  492.         if (file)
  493.           return getLocalHandlerApp(file);
  494.  
  495.         return null;
  496.  
  497.       case "web":
  498.         var uri = this.element(this._prefSelectedWeb).value;
  499.         if (!uri)
  500.           return null;
  501.         return this._converterSvc.getWebContentHandlerByURI(this.type, uri);
  502.  
  503.       case "bookmarks":
  504.       default:
  505.         // When the pref is set to bookmarks, we handle feeds internally,
  506.         // we don't forward them to a local or web handler app, so there is
  507.         // no preferred handler.
  508.         return null;
  509.     }
  510.   },
  511.  
  512.   set preferredApplicationHandler(aNewValue) {
  513.     if (aNewValue instanceof Ci.nsILocalHandlerApp) {
  514.       this.element(this._prefSelectedApp).value = aNewValue.executable;
  515.       this.element(this._prefSelectedReader).value = "client";
  516.     }
  517.     else if (aNewValue instanceof Ci.nsIWebContentHandlerInfo) {
  518.       this.element(this._prefSelectedWeb).value = aNewValue.uri;
  519.       this.element(this._prefSelectedReader).value = "web";
  520.       // Make the web handler be the new "auto handler" for feeds.
  521.       // Note: we don't have to unregister the auto handler when the user picks
  522.       // a non-web handler (local app, Live Bookmarks, etc.) because the service
  523.       // only uses the "auto handler" when the selected reader is a web handler.
  524.       // We also don't have to unregister it when the user turns on "always ask"
  525.       // (i.e. preview in browser), since that also overrides the auto handler.
  526.       this._converterSvc.setAutoHandler(this.type, aNewValue);
  527.     }
  528.   },
  529.  
  530.   _possibleApplicationHandlers: null,
  531.  
  532.   get possibleApplicationHandlers() {
  533.     if (this._possibleApplicationHandlers)
  534.       return this._possibleApplicationHandlers;
  535.  
  536.     // A minimal implementation of nsIMutableArray.  It only supports the two
  537.     // methods its callers invoke, namely appendElement and nsIArray::enumerate.
  538.     this._possibleApplicationHandlers = {
  539.       _inner: [],
  540.       _removed: [],
  541.  
  542.       QueryInterface: function(aIID) {
  543.         if (aIID.equals(Ci.nsIMutableArray) ||
  544.             aIID.equals(Ci.nsIArray) ||
  545.             aIID.equals(Ci.nsISupports))
  546.           return this;
  547.  
  548.         throw Cr.NS_ERROR_NO_INTERFACE;
  549.       },
  550.  
  551.       get length() {
  552.         return this._inner.length;
  553.       },
  554.  
  555.       enumerate: function() {
  556.         return new ArrayEnumerator(this._inner);
  557.       },
  558.  
  559.       appendElement: function(aHandlerApp, aWeak) {
  560.         this._inner.push(aHandlerApp);
  561.       },
  562.  
  563.       removeElementAt: function(aIndex) {
  564.         this._removed.push(this._inner[aIndex]);
  565.         this._inner.splice(aIndex, 1);
  566.       },
  567.  
  568.       queryElementAt: function(aIndex, aInterface) {
  569.         return this._inner[aIndex].QueryInterface(aInterface);
  570.       }
  571.     };
  572.  
  573.     // Add the selected local app if it's different from the OS default handler.
  574.     // Unlike for other types, we can store only one local app at a time for the
  575.     // feed type, since we store it in a preference that historically stores
  576.     // only a single path.  But we display all the local apps the user chooses
  577.     // while the prefpane is open, only dropping the list when the user closes
  578.     // the prefpane, for maximum usability and consistency with other types.
  579.     var preferredAppFile = this.element(this._prefSelectedApp).value;
  580.     if (preferredAppFile) {
  581.       let preferredApp = getLocalHandlerApp(preferredAppFile);
  582.       let defaultApp = this._defaultApplicationHandler;
  583.       if (!defaultApp || !defaultApp.equals(preferredApp))
  584.         this._possibleApplicationHandlers.appendElement(preferredApp, false);
  585.     }
  586.  
  587.     // Add the registered web handlers.  There can be any number of these.
  588.     var webHandlers = this._converterSvc.getContentHandlers(this.type, {});
  589.     for each (let webHandler in webHandlers)
  590.       this._possibleApplicationHandlers.appendElement(webHandler, false);
  591.  
  592.     return this._possibleApplicationHandlers;
  593.   },
  594.  
  595.   __defaultApplicationHandler: undefined,
  596.   get _defaultApplicationHandler() {
  597.     if (typeof this.__defaultApplicationHandler != "undefined")
  598.       return this.__defaultApplicationHandler;
  599.  
  600.     var defaultFeedReader = null;
  601. //@line 668 "/build/buildd/firefox-3.0-3.0.14+build2+nobinonly/build-tree/mozilla/browser/components/preferences/applications.js"
  602.     try {
  603.       defaultFeedReader = this._shellSvc.defaultFeedReader;
  604.     }
  605.     catch(ex) {
  606.       // no default reader or _shellSvc is null
  607.     }
  608. //@line 675 "/build/buildd/firefox-3.0-3.0.14+build2+nobinonly/build-tree/mozilla/browser/components/preferences/applications.js"
  609.  
  610.     if (defaultFeedReader) {
  611.       let handlerApp = Cc["@mozilla.org/uriloader/local-handler-app;1"].
  612.                        createInstance(Ci.nsIHandlerApp);
  613.       handlerApp.name = getDisplayNameForFile(defaultFeedReader);
  614.       handlerApp.QueryInterface(Ci.nsILocalHandlerApp);
  615.       handlerApp.executable = defaultFeedReader;
  616.  
  617.       this.__defaultApplicationHandler = handlerApp;
  618.     }
  619.     else {
  620.       this.__defaultApplicationHandler = null;
  621.     }
  622.  
  623.     return this.__defaultApplicationHandler;
  624.   },
  625.  
  626.   get hasDefaultHandler() {
  627. //@line 694 "/build/buildd/firefox-3.0-3.0.14+build2+nobinonly/build-tree/mozilla/browser/components/preferences/applications.js"
  628.     try {
  629.       if (this._shellSvc.defaultFeedReader)
  630.         return true;
  631.     }
  632.     catch(ex) {
  633.       // no default reader or _shellSvc is null
  634.     }
  635. //@line 702 "/build/buildd/firefox-3.0-3.0.14+build2+nobinonly/build-tree/mozilla/browser/components/preferences/applications.js"
  636.  
  637.     return false;
  638.   },
  639.  
  640.   get defaultDescription() {
  641.     if (this.hasDefaultHandler)
  642.       return this._defaultApplicationHandler.name;
  643.  
  644.     // Should we instead return null?
  645.     return "";
  646.   },
  647.  
  648.   // What to do with content of this type.
  649.   get preferredAction() {
  650.     switch (this.element(this._prefSelectedAction).value) {
  651.  
  652.       case "bookmarks":
  653.         return Ci.nsIHandlerInfo.handleInternally;
  654.  
  655.       case "reader": {
  656.         let preferredApp = this.preferredApplicationHandler;
  657.         let defaultApp = this._defaultApplicationHandler;
  658.  
  659.         // If we have a valid preferred app, return useSystemDefault if it's
  660.         // the default app; otherwise return useHelperApp.
  661.         if (gApplicationsPane.isValidHandlerApp(preferredApp)) {
  662.           if (defaultApp && defaultApp.equals(preferredApp))
  663.             return Ci.nsIHandlerInfo.useSystemDefault;
  664.  
  665.           return Ci.nsIHandlerInfo.useHelperApp;
  666.         }
  667.  
  668.         // The pref is set to "reader", but we don't have a valid preferred app.
  669.         // What do we do now?  Not sure this is the best option (perhaps we
  670.         // should direct the user to the default app, if any), but for now let's
  671.         // direct the user to live bookmarks.
  672.         return Ci.nsIHandlerInfo.handleInternally;
  673.       }
  674.  
  675.       // If the action is "ask", then alwaysAskBeforeHandling will override
  676.       // the action, so it doesn't matter what we say it is, it just has to be
  677.       // something that doesn't cause the controller to hide the type.
  678.       case "ask":
  679.       default:
  680.         return Ci.nsIHandlerInfo.handleInternally;
  681.     }
  682.   },
  683.  
  684.   set preferredAction(aNewValue) {
  685.     switch (aNewValue) {
  686.  
  687.       case Ci.nsIHandlerInfo.handleInternally:
  688.         this.element(this._prefSelectedReader).value = "bookmarks";
  689.         break;
  690.  
  691.       case Ci.nsIHandlerInfo.useHelperApp:
  692.         this.element(this._prefSelectedAction).value = "reader";
  693.         // The controller has already set preferredApplicationHandler
  694.         // to the new helper app.
  695.         break;
  696.  
  697.       case Ci.nsIHandlerInfo.useSystemDefault:
  698.         this.element(this._prefSelectedAction).value = "reader";
  699.         this.preferredApplicationHandler = this._defaultApplicationHandler;
  700.         break;
  701.     }
  702.   },
  703.  
  704.   get alwaysAskBeforeHandling() {
  705.     return this.element(this._prefSelectedAction).value == "ask";
  706.   },
  707.  
  708.   set alwaysAskBeforeHandling(aNewValue) {
  709.     if (aNewValue == true)
  710.       this.element(this._prefSelectedAction).value = "ask";
  711.     else
  712.       this.element(this._prefSelectedAction).value = "reader";
  713.   },
  714.  
  715.   // Whether or not we are currently storing the action selected by the user.
  716.   // We use this to suppress notification-triggered updates to the list when
  717.   // we make changes that may spawn such updates, specifically when we change
  718.   // the action for the feed type, which results in feed preference updates,
  719.   // which spawn "pref changed" notifications that would otherwise cause us
  720.   // to rebuild the view unnecessarily.
  721.   _storingAction: false,
  722.  
  723.  
  724.   //**************************************************************************//
  725.   // nsIMIMEInfo
  726.  
  727.   get primaryExtension() {
  728.     return "xml";
  729.   },
  730.  
  731.  
  732.   //**************************************************************************//
  733.   // Storage
  734.  
  735.   // Changes to the preferred action and handler take effect immediately
  736.   // (we write them out to the preferences right as they happen),
  737.   // so we when the controller calls store() after modifying the handlers,
  738.   // the only thing we need to store is the removal of possible handlers
  739.   // XXX Should we hold off on making the changes until this method gets called?
  740.   store: function() {
  741.     for each (let app in this._possibleApplicationHandlers._removed) {
  742.       if (app instanceof Ci.nsILocalHandlerApp) {
  743.         let pref = this.element(PREF_FEED_SELECTED_APP);
  744.         var preferredAppFile = pref.value;
  745.         if (preferredAppFile) {
  746.           let preferredApp = getLocalHandlerApp(preferredAppFile);
  747.           if (app.equals(preferredApp))
  748.             pref.reset();
  749.         }
  750.       }
  751.       else {
  752.         app.QueryInterface(Ci.nsIWebContentHandlerInfo);
  753.         this._converterSvc.removeContentHandler(app.contentType, app.uri);
  754.       }
  755.     }
  756.     this._possibleApplicationHandlers._removed = [];
  757.   },
  758.  
  759.  
  760.   //**************************************************************************//
  761.   // Icons
  762.  
  763.   get smallIcon() {
  764.     return this._smallIcon;
  765.   },
  766.  
  767.   get largeIcon() {
  768.     return this._largeIcon;
  769.   }
  770.  
  771. };
  772.  
  773. var feedHandlerInfo = {
  774.   __proto__: new FeedHandlerInfo(TYPE_MAYBE_FEED),
  775.   _prefSelectedApp: PREF_FEED_SELECTED_APP, 
  776.   _prefSelectedWeb: PREF_FEED_SELECTED_WEB, 
  777.   _prefSelectedAction: PREF_FEED_SELECTED_ACTION, 
  778.   _prefSelectedReader: PREF_FEED_SELECTED_READER,
  779.   _smallIcon: "chrome://browser/skin/feeds/feedIcon16.png",
  780.   _largeIcon: "chrome://browser/skin/feeds/feedIcon.png",
  781.   _appPrefLabel: "webFeed"
  782. }
  783.  
  784. var videoFeedHandlerInfo = {
  785.   __proto__: new FeedHandlerInfo(TYPE_MAYBE_VIDEO_FEED),
  786.   _prefSelectedApp: PREF_VIDEO_FEED_SELECTED_APP, 
  787.   _prefSelectedWeb: PREF_VIDEO_FEED_SELECTED_WEB, 
  788.   _prefSelectedAction: PREF_VIDEO_FEED_SELECTED_ACTION, 
  789.   _prefSelectedReader: PREF_VIDEO_FEED_SELECTED_READER,
  790.   _smallIcon: "chrome://browser/skin/feeds/videoFeedIcon16.png",
  791.   _largeIcon: "chrome://browser/skin/feeds/videoFeedIcon.png",
  792.   _appPrefLabel: "videoPodcastFeed"
  793. }
  794.  
  795. var audioFeedHandlerInfo = {
  796.   __proto__: new FeedHandlerInfo(TYPE_MAYBE_AUDIO_FEED),
  797.   _prefSelectedApp: PREF_AUDIO_FEED_SELECTED_APP, 
  798.   _prefSelectedWeb: PREF_AUDIO_FEED_SELECTED_WEB, 
  799.   _prefSelectedAction: PREF_AUDIO_FEED_SELECTED_ACTION, 
  800.   _prefSelectedReader: PREF_AUDIO_FEED_SELECTED_READER,
  801.   _smallIcon: "chrome://browser/skin/feeds/audioFeedIcon16.png",
  802.   _largeIcon: "chrome://browser/skin/feeds/audioFeedIcon.png",
  803.   _appPrefLabel: "audioPodcastFeed"
  804. }
  805.  
  806.  
  807. //****************************************************************************//
  808. // Prefpane Controller
  809.  
  810. var gApplicationsPane = {
  811.   // The set of types the app knows how to handle.  A hash of HandlerInfoWrapper
  812.   // objects, indexed by type.
  813.   _handledTypes: {},
  814.   
  815.   // The list of types we can show, sorted by the sort column/direction.
  816.   // An array of HandlerInfoWrapper objects.  We build this list when we first
  817.   // load the data and then rebuild it when users change a pref that affects
  818.   // what types we can show or change the sort column/direction.
  819.   // Note: this isn't necessarily the list of types we *will* show; if the user
  820.   // provides a filter string, we'll only show the subset of types in this list
  821.   // that match that string.
  822.   _visibleTypes: [],
  823.  
  824.   // A count of the number of times each visible type description appears.
  825.   // We use these counts to determine whether or not to annotate descriptions
  826.   // with their types to distinguish duplicate descriptions from each other.
  827.   // A hash of integer counts, indexed by string description.
  828.   _visibleTypeDescriptionCount: {},
  829.  
  830.  
  831.   //**************************************************************************//
  832.   // Convenience & Performance Shortcuts
  833.  
  834.   // These get defined by init().
  835.   _brandShortName : null,
  836.   _prefsBundle    : null,
  837.   _list           : null,
  838.   _filter         : null,
  839.  
  840.   // Retrieve this as nsIPrefBranch and then immediately QI to nsIPrefBranch2
  841.   // so both interfaces are available to callers.
  842.   _prefSvc      : Cc["@mozilla.org/preferences-service;1"].
  843.                   getService(Ci.nsIPrefBranch).
  844.                   QueryInterface(Ci.nsIPrefBranch2),
  845.  
  846.   _mimeSvc      : Cc["@mozilla.org/mime;1"].
  847.                   getService(Ci.nsIMIMEService),
  848.  
  849.   _helperAppSvc : Cc["@mozilla.org/uriloader/external-helper-app-service;1"].
  850.                   getService(Ci.nsIExternalHelperAppService),
  851.  
  852.   _handlerSvc   : Cc["@mozilla.org/uriloader/handler-service;1"].
  853.                   getService(Ci.nsIHandlerService),
  854.  
  855.   _ioSvc        : Cc["@mozilla.org/network/io-service;1"].
  856.                   getService(Ci.nsIIOService),
  857.  
  858.  
  859.   //**************************************************************************//
  860.   // Initialization & Destruction
  861.  
  862.   init: function() {
  863.     // Initialize shortcuts to some commonly accessed elements & values.
  864.     this._brandShortName =
  865.       document.getElementById("bundleBrand").getString("brandShortName");
  866.     this._prefsBundle = document.getElementById("bundlePreferences");
  867.     this._list = document.getElementById("handlersView");
  868.     this._filter = document.getElementById("filter");
  869.  
  870.     // Observe preferences that influence what we display so we can rebuild
  871.     // the view when they change.
  872.     this._prefSvc.addObserver(PREF_SHOW_PLUGINS_IN_LIST, this, false);
  873.     this._prefSvc.addObserver(PREF_HIDE_PLUGINS_WITHOUT_EXTENSIONS, this, false);
  874.     this._prefSvc.addObserver(PREF_FEED_SELECTED_APP, this, false);
  875.     this._prefSvc.addObserver(PREF_FEED_SELECTED_WEB, this, false);
  876.     this._prefSvc.addObserver(PREF_FEED_SELECTED_ACTION, this, false);
  877.     this._prefSvc.addObserver(PREF_FEED_SELECTED_READER, this, false);
  878.  
  879.     this._prefSvc.addObserver(PREF_VIDEO_FEED_SELECTED_APP, this, false);
  880.     this._prefSvc.addObserver(PREF_VIDEO_FEED_SELECTED_WEB, this, false);
  881.     this._prefSvc.addObserver(PREF_VIDEO_FEED_SELECTED_ACTION, this, false);
  882.     this._prefSvc.addObserver(PREF_VIDEO_FEED_SELECTED_READER, this, false);
  883.  
  884.     this._prefSvc.addObserver(PREF_AUDIO_FEED_SELECTED_APP, this, false);
  885.     this._prefSvc.addObserver(PREF_AUDIO_FEED_SELECTED_WEB, this, false);
  886.     this._prefSvc.addObserver(PREF_AUDIO_FEED_SELECTED_ACTION, this, false);
  887.     this._prefSvc.addObserver(PREF_AUDIO_FEED_SELECTED_READER, this, false);
  888.  
  889.  
  890.     // Listen for window unload so we can remove our preference observers.
  891.     window.addEventListener("unload", this, false);
  892.  
  893.     // Figure out how we should be sorting the list.  We persist sort settings
  894.     // across sessions, so we can't assume the default sort column/direction.
  895.     // XXX should we be using the XUL sort service instead?
  896.     if (document.getElementById("actionColumn").hasAttribute("sortDirection")) {
  897.       this._sortColumn = document.getElementById("actionColumn");
  898.       // The typeColumn element always has a sortDirection attribute,
  899.       // either because it was persisted or because the default value
  900.       // from the xul file was used.  If we are sorting on the other
  901.       // column, we should remove it.
  902.       document.getElementById("typeColumn").removeAttribute("sortDirection");
  903.     }
  904.     else 
  905.       this._sortColumn = document.getElementById("typeColumn");
  906.  
  907.     // Load the data and build the list of handlers.
  908.     // By doing this in a timeout, we let the preferences dialog resize itself
  909.     // to an appropriate size before we add a bunch of items to the list.
  910.     // Otherwise, if there are many items, and the Applications prefpane
  911.     // is the one that gets displayed when the user first opens the dialog,
  912.     // the dialog might stretch too much in an attempt to fit them all in.
  913.     // XXX Shouldn't we perhaps just set a max-height on the richlistbox?
  914.     var _delayedPaneLoad = function(self) {
  915.       self._loadData();
  916.       self._rebuildVisibleTypes();
  917.       self._sortVisibleTypes();
  918.       self._rebuildView();
  919.  
  920.       // Notify observers that the UI is now ready
  921.       Cc["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService).
  922.       notifyObservers(window, "app-handler-pane-loaded", null);
  923.     }
  924.     setTimeout(_delayedPaneLoad, 0, this);
  925.   },
  926.  
  927.   destroy: function() {
  928.     window.removeEventListener("unload", this, false);
  929.     this._prefSvc.removeObserver(PREF_SHOW_PLUGINS_IN_LIST, this);
  930.     this._prefSvc.removeObserver(PREF_HIDE_PLUGINS_WITHOUT_EXTENSIONS, this);
  931.     this._prefSvc.removeObserver(PREF_FEED_SELECTED_APP, this);
  932.     this._prefSvc.removeObserver(PREF_FEED_SELECTED_WEB, this);
  933.     this._prefSvc.removeObserver(PREF_FEED_SELECTED_ACTION, this);
  934.     this._prefSvc.removeObserver(PREF_FEED_SELECTED_READER, this);
  935.  
  936.     this._prefSvc.removeObserver(PREF_VIDEO_FEED_SELECTED_APP, this);
  937.     this._prefSvc.removeObserver(PREF_VIDEO_FEED_SELECTED_WEB, this);
  938.     this._prefSvc.removeObserver(PREF_VIDEO_FEED_SELECTED_ACTION, this);
  939.     this._prefSvc.removeObserver(PREF_VIDEO_FEED_SELECTED_READER, this);
  940.  
  941.     this._prefSvc.removeObserver(PREF_AUDIO_FEED_SELECTED_APP, this);
  942.     this._prefSvc.removeObserver(PREF_AUDIO_FEED_SELECTED_WEB, this);
  943.     this._prefSvc.removeObserver(PREF_AUDIO_FEED_SELECTED_ACTION, this);
  944.     this._prefSvc.removeObserver(PREF_AUDIO_FEED_SELECTED_READER, this);
  945.   },
  946.  
  947.  
  948.   //**************************************************************************//
  949.   // nsISupports
  950.  
  951.   QueryInterface: function(aIID) {
  952.     if (aIID.equals(Ci.nsIObserver) ||
  953.         aIID.equals(Ci.nsIDOMEventListener ||
  954.         aIID.equals(Ci.nsISupports)))
  955.       return this;
  956.  
  957.     throw Cr.NS_ERROR_NO_INTERFACE;
  958.   },
  959.  
  960.  
  961.   //**************************************************************************//
  962.   // nsIObserver
  963.  
  964.   observe: function (aSubject, aTopic, aData) {
  965.     // Rebuild the list when there are changes to preferences that influence
  966.     // whether or not to show certain entries in the list.
  967.     if (aTopic == "nsPref:changed" && !this._storingAction) {
  968.       // These two prefs alter the list of visible types, so we have to rebuild
  969.       // that list when they change.
  970.       if (aData == PREF_SHOW_PLUGINS_IN_LIST ||
  971.           aData == PREF_HIDE_PLUGINS_WITHOUT_EXTENSIONS) {
  972.         this._rebuildVisibleTypes();
  973.         this._sortVisibleTypes();
  974.       }
  975.  
  976.       // All the prefs we observe can affect what we display, so we rebuild
  977.       // the view when any of them changes.
  978.       this._rebuildView();
  979.     }
  980.   },
  981.  
  982.  
  983.   //**************************************************************************//
  984.   // nsIDOMEventListener
  985.  
  986.   handleEvent: function(aEvent) {
  987.     if (aEvent.type == "unload") {
  988.       this.destroy();
  989.     }
  990.   },
  991.  
  992.  
  993.   //**************************************************************************//
  994.   // Composed Model Construction
  995.  
  996.   _loadData: function() {
  997.     this._loadFeedHandler();
  998.     this._loadPluginHandlers();
  999.     this._loadApplicationHandlers();
  1000.   },
  1001.  
  1002.   _loadFeedHandler: function() {
  1003.     this._handledTypes[TYPE_MAYBE_FEED] = feedHandlerInfo;
  1004.     feedHandlerInfo.handledOnlyByPlugin = false;
  1005.  
  1006.     this._handledTypes[TYPE_MAYBE_VIDEO_FEED] = videoFeedHandlerInfo;
  1007.     videoFeedHandlerInfo.handledOnlyByPlugin = false;
  1008.  
  1009.     this._handledTypes[TYPE_MAYBE_AUDIO_FEED] = audioFeedHandlerInfo;
  1010.     audioFeedHandlerInfo.handledOnlyByPlugin = false;
  1011.   },
  1012.  
  1013.   /**
  1014.    * Load the set of handlers defined by plugins.
  1015.    *
  1016.    * Note: if there's more than one plugin for a given MIME type, we assume
  1017.    * the last one is the one that the application will use.  That may not be
  1018.    * correct, but it's how we've been doing it for years.
  1019.    *
  1020.    * Perhaps we should instead query navigator.mimeTypes for the set of types
  1021.    * supported by the application and then get the plugin from each MIME type's
  1022.    * enabledPlugin property.  But if there's a plugin for a type, we need
  1023.    * to know about it even if it isn't enabled, since we're going to give
  1024.    * the user an option to enable it.
  1025.    * 
  1026.    * I'll also note that my reading of nsPluginTag::RegisterWithCategoryManager
  1027.    * suggests that enabledPlugin is only determined during registration
  1028.    * and does not get updated when plugin.disable_full_page_plugin_for_types
  1029.    * changes (unless modification of that preference spawns reregistration).
  1030.    * So even if we could use enabledPlugin to get the plugin that would be used,
  1031.    * we'd still need to check the pref ourselves to find out if it's enabled.
  1032.    */
  1033.   _loadPluginHandlers: function() {
  1034.     for (let i = 0; i < navigator.plugins.length; ++i) {
  1035.       let plugin = navigator.plugins[i];
  1036.       for (let j = 0; j < plugin.length; ++j) {
  1037.         let type = plugin[j].type;
  1038.  
  1039.         let handlerInfoWrapper;
  1040.         if (type in this._handledTypes)
  1041.           handlerInfoWrapper = this._handledTypes[type];
  1042.         else {
  1043.           let wrappedHandlerInfo =
  1044.             this._mimeSvc.getFromTypeAndExtension(type, null);
  1045.           handlerInfoWrapper = new HandlerInfoWrapper(type, wrappedHandlerInfo);
  1046.           handlerInfoWrapper.handledOnlyByPlugin = true;
  1047.           this._handledTypes[type] = handlerInfoWrapper;
  1048.         }
  1049.  
  1050.         handlerInfoWrapper.plugin = plugin;
  1051.       }
  1052.     }
  1053.   },
  1054.  
  1055.   /**
  1056.    * Load the set of handlers defined by the application datastore.
  1057.    */
  1058.   _loadApplicationHandlers: function() {
  1059.     var wrappedHandlerInfos = this._handlerSvc.enumerate();
  1060.     while (wrappedHandlerInfos.hasMoreElements()) {
  1061.       let wrappedHandlerInfo =
  1062.         wrappedHandlerInfos.getNext().QueryInterface(Ci.nsIHandlerInfo);
  1063.       let type = wrappedHandlerInfo.type;
  1064.  
  1065.       let handlerInfoWrapper;
  1066.       if (type in this._handledTypes)
  1067.         handlerInfoWrapper = this._handledTypes[type];
  1068.       else {
  1069.         handlerInfoWrapper = new HandlerInfoWrapper(type, wrappedHandlerInfo);
  1070.         this._handledTypes[type] = handlerInfoWrapper;
  1071.       }
  1072.  
  1073.       handlerInfoWrapper.handledOnlyByPlugin = false;
  1074.     }
  1075.   },
  1076.  
  1077.  
  1078.   //**************************************************************************//
  1079.   // View Construction
  1080.  
  1081.   _rebuildVisibleTypes: function() {
  1082.     // Reset the list of visible types and the visible type description counts.
  1083.     this._visibleTypes = [];
  1084.     this._visibleTypeDescriptionCount = {};
  1085.  
  1086.     // Get the preferences that help determine what types to show.
  1087.     var showPlugins = this._prefSvc.getBoolPref(PREF_SHOW_PLUGINS_IN_LIST);
  1088.     var hidePluginsWithoutExtensions =
  1089.       this._prefSvc.getBoolPref(PREF_HIDE_PLUGINS_WITHOUT_EXTENSIONS);
  1090.  
  1091.     for (let type in this._handledTypes) {
  1092.       let handlerInfo = this._handledTypes[type];
  1093.  
  1094.       // Hide plugins without associated extensions if so prefed so we don't
  1095.       // show a whole bunch of obscure types handled by plugins on Mac.
  1096.       // Note: though protocol types don't have extensions, we still show them;
  1097.       // the pref is only meant to be applied to MIME types, since plugins are
  1098.       // only associated with MIME types.
  1099.       // FIXME: should we also check the "suffixes" property of the plugin?
  1100.       // Filed as bug 395135.
  1101.       if (hidePluginsWithoutExtensions && handlerInfo.handledOnlyByPlugin &&
  1102.           handlerInfo.wrappedHandlerInfo instanceof Ci.nsIMIMEInfo &&
  1103.           !handlerInfo.primaryExtension)
  1104.         continue;
  1105.  
  1106.       // Hide types handled only by plugins if so prefed.
  1107.       if (handlerInfo.handledOnlyByPlugin && !showPlugins)
  1108.         continue;
  1109.  
  1110.       // We couldn't find any reason to exclude the type, so include it.
  1111.       this._visibleTypes.push(handlerInfo);
  1112.  
  1113.       if (handlerInfo.description in this._visibleTypeDescriptionCount)
  1114.         this._visibleTypeDescriptionCount[handlerInfo.description]++;
  1115.       else
  1116.         this._visibleTypeDescriptionCount[handlerInfo.description] = 1;
  1117.     }
  1118.   },
  1119.  
  1120.   _rebuildView: function() {
  1121.     // Clear the list of entries.
  1122.     while (this._list.childNodes.length > 1)
  1123.       this._list.removeChild(this._list.lastChild);
  1124.  
  1125.     var visibleTypes = this._visibleTypes;
  1126.  
  1127.     // If the user is filtering the list, then only show matching types.
  1128.     if (this._filter.value)
  1129.       visibleTypes = visibleTypes.filter(this._matchesFilter, this);
  1130.  
  1131.     for each (let visibleType in visibleTypes) {
  1132.       let item = document.createElement("richlistitem");
  1133.       item.setAttribute("type", visibleType.type);
  1134.       item.setAttribute("typeDescription", this._describeType(visibleType));
  1135.       if (visibleType.smallIcon)
  1136.         item.setAttribute("typeIcon", visibleType.smallIcon);
  1137.       item.setAttribute("actionDescription",
  1138.                         this._describePreferredAction(visibleType));
  1139.  
  1140.       if (!this._setIconClassForPreferredAction(visibleType, item)) {
  1141.         item.setAttribute("actionIcon",
  1142.                           this._getIconURLForPreferredAction(visibleType));
  1143.       }
  1144.  
  1145.       this._list.appendChild(item);
  1146.     }
  1147.  
  1148.     this._selectLastSelectedType();
  1149.   },
  1150.  
  1151.   _matchesFilter: function(aType) {
  1152.     var filterValue = this._filter.value.toLowerCase();
  1153.     return this._describeType(aType).toLowerCase().indexOf(filterValue) != -1 ||
  1154.            this._describePreferredAction(aType).toLowerCase().indexOf(filterValue) != -1;
  1155.   },
  1156.  
  1157.   /**
  1158.    * Describe, in a human-readable fashion, the type represented by the given
  1159.    * handler info object.  Normally this is just the description provided by
  1160.    * the info object, but if more than one object presents the same description,
  1161.    * then we annotate the duplicate descriptions with the type itself to help
  1162.    * users distinguish between those types.
  1163.    *
  1164.    * @param aHandlerInfo {nsIHandlerInfo} the type being described
  1165.    * @returns {string} a description of the type
  1166.    */
  1167.   _describeType: function(aHandlerInfo) {
  1168.     if (this._visibleTypeDescriptionCount[aHandlerInfo.description] > 1)
  1169.       return this._prefsBundle.getFormattedString("typeDescriptionWithType",
  1170.                                                   [aHandlerInfo.description,
  1171.                                                    aHandlerInfo.type]);
  1172.  
  1173.     return aHandlerInfo.description;
  1174.   },
  1175.  
  1176.   /**
  1177.    * Describe, in a human-readable fashion, the preferred action to take on
  1178.    * the type represented by the given handler info object.
  1179.    *
  1180.    * XXX Should this be part of the HandlerInfoWrapper interface?  It would
  1181.    * violate the separation of model and view, but it might make more sense
  1182.    * nonetheless (f.e. it would make sortTypes easier).
  1183.    *
  1184.    * @param aHandlerInfo {nsIHandlerInfo} the type whose preferred action
  1185.    *                                      is being described
  1186.    * @returns {string} a description of the action
  1187.    */
  1188.   _describePreferredAction: function(aHandlerInfo) {
  1189.     // alwaysAskBeforeHandling overrides the preferred action, so if that flag
  1190.     // is set, then describe that behavior instead.  For most types, this is
  1191.     // the "alwaysAsk" string, but for the feed type we show something special.
  1192.     if (aHandlerInfo.alwaysAskBeforeHandling) {
  1193.       if (isFeedType(aHandlerInfo.type))
  1194.         return this._prefsBundle.getFormattedString("previewInApp",
  1195.                                                     [this._brandShortName]);
  1196.       else
  1197.         return this._prefsBundle.getString("alwaysAsk");
  1198.     }
  1199.  
  1200.     switch (aHandlerInfo.preferredAction) {
  1201.       case Ci.nsIHandlerInfo.saveToDisk:
  1202.         return this._prefsBundle.getString("saveFile");
  1203.  
  1204.       case Ci.nsIHandlerInfo.useHelperApp:
  1205.         var preferredApp = aHandlerInfo.preferredApplicationHandler;
  1206.         var name;
  1207.         if (preferredApp instanceof Ci.nsILocalHandlerApp)
  1208.           name = getDisplayNameForFile(preferredApp.executable);
  1209.         else
  1210.           name = preferredApp.name;
  1211.         return this._prefsBundle.getFormattedString("useApp", [name]);
  1212.  
  1213.       case Ci.nsIHandlerInfo.handleInternally:
  1214.         // For the feed type, handleInternally means live bookmarks.
  1215.         if (isFeedType(aHandlerInfo.type)) 
  1216.           return this._prefsBundle.getFormattedString("addLiveBookmarksInApp",
  1217.                                                       [this._brandShortName]);
  1218.  
  1219.         // For other types, handleInternally looks like either useHelperApp
  1220.         // or useSystemDefault depending on whether or not there's a preferred
  1221.         // handler app.
  1222.         if (this.isValidHandlerApp(aHandlerInfo.preferredApplicationHandler))
  1223.           return aHandlerInfo.preferredApplicationHandler.name;
  1224.  
  1225.         return aHandlerInfo.defaultDescription;
  1226.  
  1227.         // XXX Why don't we say the app will handle the type internally?
  1228.         // Is it because the app can't actually do that?  But if that's true,
  1229.         // then why would a preferredAction ever get set to this value
  1230.         // in the first place?
  1231.  
  1232.       case Ci.nsIHandlerInfo.useSystemDefault:
  1233.         return this._prefsBundle.getFormattedString("useDefault",
  1234.                                                     [aHandlerInfo.defaultDescription]);
  1235.  
  1236.       case kActionUsePlugin:
  1237.         return this._prefsBundle.getFormattedString("usePluginIn",
  1238.                                                     [aHandlerInfo.plugin.name,
  1239.                                                      this._brandShortName]);
  1240.     }
  1241.   },
  1242.  
  1243.   _selectLastSelectedType: function() {
  1244.     // If the list is disabled by the pref.downloads.disable_button.edit_actions
  1245.     // preference being locked, then don't select the type, as that would cause
  1246.     // it to appear selected, with a different background and an actions menu
  1247.     // that makes it seem like you can choose an action for the type.
  1248.     if (this._list.disabled)
  1249.       return;
  1250.  
  1251.     var lastSelectedType = this._list.getAttribute("lastSelectedType");
  1252.     if (!lastSelectedType)
  1253.       return;
  1254.  
  1255.     var item = this._list.getElementsByAttribute("type", lastSelectedType)[0];
  1256.     if (!item)
  1257.       return;
  1258.  
  1259.     this._list.selectedItem = item;
  1260.   },
  1261.  
  1262.   /**
  1263.    * Whether or not the given handler app is valid.
  1264.    *
  1265.    * @param aHandlerApp {nsIHandlerApp} the handler app in question
  1266.    *
  1267.    * @returns {boolean} whether or not it's valid
  1268.    */
  1269.   isValidHandlerApp: function(aHandlerApp) {
  1270.     if (!aHandlerApp)
  1271.       return false;
  1272.  
  1273.     if (aHandlerApp instanceof Ci.nsILocalHandlerApp)
  1274.       return this._isValidHandlerExecutable(aHandlerApp.executable);
  1275.  
  1276.     if (aHandlerApp instanceof Ci.nsIWebHandlerApp)
  1277.       return aHandlerApp.uriTemplate;
  1278.  
  1279.     if (aHandlerApp instanceof Ci.nsIWebContentHandlerInfo)
  1280.       return aHandlerApp.uri;
  1281.  
  1282.     return false;
  1283.   },
  1284.  
  1285.   _isValidHandlerExecutable: function(aExecutable) {
  1286.     return aExecutable &&
  1287.            aExecutable.exists() &&
  1288.            aExecutable.isExecutable() &&
  1289. // XXXben - we need to compare this with the running instance executable
  1290. //          just don't know how to do that via script...
  1291. // XXXmano TBD: can probably add this to nsIShellService
  1292.    aExecutable.leafName != "firefox-bin";
  1293. //@line 1367 "/build/buildd/firefox-3.0-3.0.14+build2+nobinonly/build-tree/mozilla/browser/components/preferences/applications.js"
  1294.   },
  1295.  
  1296.   /**
  1297.    * Rebuild the actions menu for the selected entry.  Gets called by
  1298.    * the richlistitem constructor when an entry in the list gets selected.
  1299.    */
  1300.   rebuildActionsMenu: function() {
  1301.     var typeItem = this._list.selectedItem;
  1302.     var handlerInfo = this._handledTypes[typeItem.type];
  1303.     var menu =
  1304.       document.getAnonymousElementByAttribute(typeItem, "class", "actionsMenu");
  1305.     var menuPopup = menu.menupopup;
  1306.  
  1307.     // Clear out existing items.
  1308.     while (menuPopup.hasChildNodes())
  1309.       menuPopup.removeChild(menuPopup.lastChild);
  1310.  
  1311.     {
  1312.       var askMenuItem = document.createElement("menuitem");
  1313.       askMenuItem.setAttribute("alwaysAsk", "true");
  1314.       let label;
  1315.       if (isFeedType(handlerInfo.type))
  1316.         label = this._prefsBundle.getFormattedString("previewInApp",
  1317.                                                      [this._brandShortName]);
  1318.       else
  1319.         label = this._prefsBundle.getString("alwaysAsk");
  1320.       askMenuItem.setAttribute("label", label);
  1321.       askMenuItem.setAttribute("tooltiptext", label);
  1322.       askMenuItem.setAttribute(APP_ICON_ATTR_NAME, "ask");
  1323.       menuPopup.appendChild(askMenuItem);
  1324.     }
  1325.  
  1326.     // Create a menu item for saving to disk.
  1327.     // Note: this option isn't available to protocol types, since we don't know
  1328.     // what it means to save a URL having a certain scheme to disk, nor is it
  1329.     // available to feeds, since the feed code doesn't implement the capability.
  1330.     if ((handlerInfo.wrappedHandlerInfo instanceof Ci.nsIMIMEInfo) &&
  1331.         !isFeedType(handlerInfo.type)) {
  1332.       var saveMenuItem = document.createElement("menuitem");
  1333.       saveMenuItem.setAttribute("action", Ci.nsIHandlerInfo.saveToDisk);
  1334.       let label = this._prefsBundle.getString("saveFile");
  1335.       saveMenuItem.setAttribute("label", label);
  1336.       saveMenuItem.setAttribute("tooltiptext", label);
  1337.       saveMenuItem.setAttribute(APP_ICON_ATTR_NAME, "save");
  1338.       menuPopup.appendChild(saveMenuItem);
  1339.     }
  1340.  
  1341.     // If this is the feed type, add a Live Bookmarks item.
  1342.     if (isFeedType(handlerInfo.type)) {
  1343.       var internalMenuItem = document.createElement("menuitem");
  1344.       internalMenuItem.setAttribute("action", Ci.nsIHandlerInfo.handleInternally);
  1345.       let label = this._prefsBundle.getFormattedString("addLiveBookmarksInApp",
  1346.                                                        [this._brandShortName]);
  1347.       internalMenuItem.setAttribute("label", label);
  1348.       internalMenuItem.setAttribute("tooltiptext", label);
  1349.       internalMenuItem.setAttribute(APP_ICON_ATTR_NAME, "feed");
  1350.       menuPopup.appendChild(internalMenuItem);
  1351.     }
  1352.  
  1353.     // Add a separator to distinguish these items from the helper app items
  1354.     // that follow them.
  1355.     let menuItem = document.createElement("menuseparator");
  1356.     menuPopup.appendChild(menuItem);
  1357.  
  1358.     // Create a menu item for the OS default application, if any.
  1359.     if (handlerInfo.hasDefaultHandler) {
  1360.       var defaultMenuItem = document.createElement("menuitem");
  1361.       defaultMenuItem.setAttribute("action", Ci.nsIHandlerInfo.useSystemDefault);
  1362.       let label = this._prefsBundle.getFormattedString("useDefault",
  1363.                                                        [handlerInfo.defaultDescription]);
  1364.       defaultMenuItem.setAttribute("label", label);
  1365.       defaultMenuItem.setAttribute("tooltiptext", handlerInfo.defaultDescription);
  1366.       defaultMenuItem.setAttribute("image", this._getIconURLForSystemDefault(handlerInfo));
  1367.  
  1368.       menuPopup.appendChild(defaultMenuItem);
  1369.     }
  1370.  
  1371.     // Create menu items for possible handlers.
  1372.     let preferredApp = handlerInfo.preferredApplicationHandler;
  1373.     let possibleApps = handlerInfo.possibleApplicationHandlers.enumerate();
  1374.     var possibleAppMenuItems = [];
  1375.     while (possibleApps.hasMoreElements()) {
  1376.       let possibleApp = possibleApps.getNext();
  1377.       if (!this.isValidHandlerApp(possibleApp))
  1378.         continue;
  1379.  
  1380.       let menuItem = document.createElement("menuitem");
  1381.       menuItem.setAttribute("action", Ci.nsIHandlerInfo.useHelperApp);
  1382.       let label;
  1383.       if (possibleApp instanceof Ci.nsILocalHandlerApp)
  1384.         label = getDisplayNameForFile(possibleApp.executable);
  1385.       else
  1386.         label = possibleApp.name;
  1387.       label = this._prefsBundle.getFormattedString("useApp", [label]);
  1388.       menuItem.setAttribute("label", label);
  1389.       menuItem.setAttribute("tooltiptext", label);
  1390.       menuItem.setAttribute("image", this._getIconURLForHandlerApp(possibleApp));
  1391.  
  1392.       // Attach the handler app object to the menu item so we can use it
  1393.       // to make changes to the datastore when the user selects the item.
  1394.       menuItem.handlerApp = possibleApp;
  1395.  
  1396.       menuPopup.appendChild(menuItem);
  1397.       possibleAppMenuItems.push(menuItem);
  1398.     }
  1399.  
  1400.     // Create a menu item for the plugin.
  1401.     if (handlerInfo.plugin) {
  1402.       var pluginMenuItem = document.createElement("menuitem");
  1403.       pluginMenuItem.setAttribute("action", kActionUsePlugin);
  1404.       let label = this._prefsBundle.getFormattedString("usePluginIn",
  1405.                                                        [handlerInfo.plugin.name,
  1406.                                                         this._brandShortName]);
  1407.       pluginMenuItem.setAttribute("label", label);
  1408.       pluginMenuItem.setAttribute("tooltiptext", label);
  1409.       pluginMenuItem.setAttribute(APP_ICON_ATTR_NAME, "plugin");
  1410.       menuPopup.appendChild(pluginMenuItem);
  1411.     }
  1412.  
  1413.     // Create a menu item for selecting a local application.
  1414. //@line 1494 "/build/buildd/firefox-3.0-3.0.14+build2+nobinonly/build-tree/mozilla/browser/components/preferences/applications.js"
  1415.     {
  1416.       let menuItem = document.createElement("menuitem");
  1417.       menuItem.setAttribute("oncommand", "gApplicationsPane.chooseApp(event)");
  1418.       let label = this._prefsBundle.getString("useOtherApp");
  1419.       menuItem.setAttribute("label", label);
  1420.       menuItem.setAttribute("tooltiptext", label);
  1421.       menuPopup.appendChild(menuItem);
  1422.     }
  1423.  
  1424.     // Create a menu item for managing applications.
  1425.     if (possibleAppMenuItems.length) {
  1426.       let menuItem = document.createElement("menuseparator");
  1427.       menuPopup.appendChild(menuItem);
  1428.       menuItem = document.createElement("menuitem");
  1429.       menuItem.setAttribute("oncommand", "gApplicationsPane.manageApp(event)");
  1430.       menuItem.setAttribute("label", this._prefsBundle.getString("manageApp"));
  1431.       menuPopup.appendChild(menuItem);
  1432.     }
  1433.  
  1434.     // Select the item corresponding to the preferred action.  If the always
  1435.     // ask flag is set, it overrides the preferred action.  Otherwise we pick
  1436.     // the item identified by the preferred action (when the preferred action
  1437.     // is to use a helper app, we have to pick the specific helper app item).
  1438.     if (handlerInfo.alwaysAskBeforeHandling)
  1439.       menu.selectedItem = askMenuItem;
  1440.     else switch (handlerInfo.preferredAction) {
  1441.       case Ci.nsIHandlerInfo.handleInternally:
  1442.         menu.selectedItem = internalMenuItem;
  1443.         break;
  1444.       case Ci.nsIHandlerInfo.useSystemDefault:
  1445.         menu.selectedItem = defaultMenuItem;
  1446.         break;
  1447.       case Ci.nsIHandlerInfo.useHelperApp:
  1448.         if (preferredApp)
  1449.           menu.selectedItem = 
  1450.             possibleAppMenuItems.filter(function(v) v.handlerApp.equals(preferredApp))[0];
  1451.         break;
  1452.       case kActionUsePlugin:
  1453.         menu.selectedItem = pluginMenuItem;
  1454.         break;
  1455.       case Ci.nsIHandlerInfo.saveToDisk:
  1456.         menu.selectedItem = saveMenuItem;
  1457.         break;
  1458.     }
  1459.   },
  1460.  
  1461.  
  1462.   //**************************************************************************//
  1463.   // Sorting & Filtering
  1464.  
  1465.   _sortColumn: null,
  1466.  
  1467.   /**
  1468.    * Sort the list when the user clicks on a column header.
  1469.    */
  1470.   sort: function (event) {
  1471.     var column = event.target;
  1472.  
  1473.     // If the user clicked on a new sort column, remove the direction indicator
  1474.     // from the old column.
  1475.     if (this._sortColumn && this._sortColumn != column)
  1476.       this._sortColumn.removeAttribute("sortDirection");
  1477.  
  1478.     this._sortColumn = column;
  1479.  
  1480.     // Set (or switch) the sort direction indicator.
  1481.     if (column.getAttribute("sortDirection") == "ascending")
  1482.       column.setAttribute("sortDirection", "descending");
  1483.     else
  1484.       column.setAttribute("sortDirection", "ascending");
  1485.  
  1486.     this._sortVisibleTypes();
  1487.     this._rebuildView();
  1488.   },
  1489.  
  1490.   /**
  1491.    * Sort the list of visible types by the current sort column/direction.
  1492.    */
  1493.   _sortVisibleTypes: function() {
  1494.     if (!this._sortColumn)
  1495.       return;
  1496.  
  1497.     var t = this;
  1498.  
  1499.     function sortByType(a, b) {
  1500.       return t._describeType(a).toLowerCase().
  1501.              localeCompare(t._describeType(b).toLowerCase());
  1502.     }
  1503.  
  1504.     function sortByAction(a, b) {
  1505.       return t._describePreferredAction(a).toLowerCase().
  1506.              localeCompare(t._describePreferredAction(b).toLowerCase());
  1507.     }
  1508.  
  1509.     switch (this._sortColumn.getAttribute("value")) {
  1510.       case "type":
  1511.         this._visibleTypes.sort(sortByType);
  1512.         break;
  1513.       case "action":
  1514.         this._visibleTypes.sort(sortByAction);
  1515.         break;
  1516.     }
  1517.  
  1518.     if (this._sortColumn.getAttribute("sortDirection") == "descending")
  1519.       this._visibleTypes.reverse();
  1520.   },
  1521.  
  1522.   /**
  1523.    * Filter the list when the user enters a filter term into the filter field.
  1524.    */
  1525.   filter: function() {
  1526.     if (this._filter.value == "") {
  1527.       this.clearFilter();
  1528.       return;
  1529.     }
  1530.  
  1531.     this._rebuildView();
  1532.  
  1533.     document.getElementById("clearFilter").disabled = false;
  1534.   },
  1535.  
  1536.   _filterTimeout: null,
  1537.  
  1538.   onFilterInput: function() {
  1539.     if (this._filterTimeout)
  1540.       clearTimeout(this._filterTimeout);
  1541.    
  1542.     this._filterTimeout = setTimeout("gApplicationsPane.filter()", 500);
  1543.   },
  1544.  
  1545.   onFilterKeyPress: function(aEvent) {
  1546.     if (aEvent.keyCode == KeyEvent.DOM_VK_ESCAPE)
  1547.       this.clearFilter();
  1548.   },
  1549.   
  1550.   clearFilter: function() {
  1551.     this._filter.value = "";
  1552.     this._rebuildView();
  1553.  
  1554.     this._filter.focus();
  1555.     document.getElementById("clearFilter").disabled = true;
  1556.   },
  1557.  
  1558.   focusFilterBox: function() {
  1559.     this._filter.focus();
  1560.     this._filter.select();
  1561.   },
  1562.  
  1563.  
  1564.   //**************************************************************************//
  1565.   // Changes
  1566.  
  1567.   onSelectAction: function(aActionItem) {
  1568.     this._storingAction = true;
  1569.  
  1570.     try {
  1571.       this._storeAction(aActionItem);
  1572.     }
  1573.     finally {
  1574.       this._storingAction = false;
  1575.     }
  1576.   },
  1577.  
  1578.   _storeAction: function(aActionItem) {
  1579.     var typeItem = this._list.selectedItem;
  1580.     var handlerInfo = this._handledTypes[typeItem.type];
  1581.  
  1582.     if (aActionItem.hasAttribute("alwaysAsk")) {
  1583.       handlerInfo.alwaysAskBeforeHandling = true;
  1584.     }
  1585.     else if (aActionItem.hasAttribute("action")) {
  1586.       let action = parseInt(aActionItem.getAttribute("action"));
  1587.  
  1588.       // Set the plugin state if we're enabling or disabling a plugin.
  1589.       if (action == kActionUsePlugin)
  1590.         handlerInfo.enablePluginType();
  1591.       else if (handlerInfo.plugin && !handlerInfo.isDisabledPluginType)
  1592.         handlerInfo.disablePluginType();
  1593.  
  1594.       // Set the preferred application handler.
  1595.       // We leave the existing preferred app in the list when we set
  1596.       // the preferred action to something other than useHelperApp so that
  1597.       // legacy datastores that don't have the preferred app in the list
  1598.       // of possible apps still include the preferred app in the list of apps
  1599.       // the user can choose to handle the type.
  1600.       if (action == Ci.nsIHandlerInfo.useHelperApp)
  1601.         handlerInfo.preferredApplicationHandler = aActionItem.handlerApp;
  1602.  
  1603.       // Set the "always ask" flag.
  1604.       handlerInfo.alwaysAskBeforeHandling = false;
  1605.  
  1606.       // Set the preferred action.
  1607.       handlerInfo.preferredAction = action;
  1608.     }
  1609.  
  1610.     handlerInfo.store();
  1611.  
  1612.     // Make sure the handler info object is flagged to indicate that there is
  1613.     // now some user configuration for the type.
  1614.     handlerInfo.handledOnlyByPlugin = false;
  1615.  
  1616.     // Update the action label and image to reflect the new preferred action.
  1617.     typeItem.setAttribute("actionDescription",
  1618.                           this._describePreferredAction(handlerInfo));
  1619.     if (!this._setIconClassForPreferredAction(handlerInfo, typeItem)) {
  1620.       typeItem.setAttribute("actionIcon",
  1621.                             this._getIconURLForPreferredAction(handlerInfo));
  1622.     }
  1623.   },
  1624.  
  1625.   manageApp: function(aEvent) {
  1626.     // Don't let the normal "on select action" handler get this event,
  1627.     // as we handle it specially ourselves.
  1628.     aEvent.stopPropagation();
  1629.  
  1630.     var typeItem = this._list.selectedItem;
  1631.     var handlerInfo = this._handledTypes[typeItem.type];
  1632.  
  1633.     document.documentElement.openSubDialog("chrome://browser/content/preferences/applicationManager.xul",
  1634.                                            "", handlerInfo);
  1635.  
  1636.     // Rebuild the actions menu so that we revert to the previous selection,
  1637.     // or "Always ask" if the previous default application has been removed
  1638.     this.rebuildActionsMenu();
  1639.  
  1640.     // update the richlistitem too. Will be visible when selecting another row
  1641.     typeItem.setAttribute("actionDescription",
  1642.                           this._describePreferredAction(handlerInfo));
  1643.     if (!this._setIconClassForPreferredAction(handlerInfo, typeItem)) {
  1644.       typeItem.setAttribute("actionIcon",
  1645.                             this._getIconURLForPreferredAction(handlerInfo));
  1646.     }
  1647.   },
  1648.  
  1649.   chooseApp: function(aEvent) {
  1650.     // Don't let the normal "on select action" handler get this event,
  1651.     // as we handle it specially ourselves.
  1652.     aEvent.stopPropagation();
  1653.  
  1654.     var handlerApp;
  1655.  
  1656. //@line 1765 "/build/buildd/firefox-3.0-3.0.14+build2+nobinonly/build-tree/mozilla/browser/components/preferences/applications.js"
  1657.     var fp = Cc["@mozilla.org/filepicker;1"].createInstance(Ci.nsIFilePicker);
  1658.     var winTitle = this._prefsBundle.getString("fpTitleChooseApp");
  1659.     fp.init(window, winTitle, Ci.nsIFilePicker.modeOpen);
  1660.     fp.appendFilters(Ci.nsIFilePicker.filterApps);
  1661.  
  1662.     // Prompt the user to pick an app.  If they pick one, and it's a valid
  1663.     // selection, then add it to the list of possible handlers.
  1664.     if (fp.show() == Ci.nsIFilePicker.returnOK && fp.file &&
  1665.         this._isValidHandlerExecutable(fp.file)) {
  1666.       handlerApp = Cc["@mozilla.org/uriloader/local-handler-app;1"].
  1667.                    createInstance(Ci.nsILocalHandlerApp);
  1668.       handlerApp.name = getDisplayNameForFile(fp.file);
  1669.       handlerApp.executable = fp.file;
  1670.  
  1671.       // Add the app to the type's list of possible handlers.
  1672.       let handlerInfo = this._handledTypes[this._list.selectedItem.type];
  1673.       handlerInfo.addPossibleApplicationHandler(handlerApp);
  1674.     }
  1675. //@line 1784 "/build/buildd/firefox-3.0-3.0.14+build2+nobinonly/build-tree/mozilla/browser/components/preferences/applications.js"
  1676.  
  1677.     // Rebuild the actions menu whether the user picked an app or canceled.
  1678.     // If they picked an app, we want to add the app to the menu and select it.
  1679.     // If they canceled, we want to go back to their previous selection.
  1680.     this.rebuildActionsMenu();
  1681.  
  1682.     // If the user picked a new app from the menu, select it.
  1683.     if (handlerApp) {
  1684.       let typeItem = this._list.selectedItem;
  1685.       let actionsMenu =
  1686.         document.getAnonymousElementByAttribute(typeItem, "class", "actionsMenu");
  1687.       let menuItems = actionsMenu.menupopup.childNodes;
  1688.       for (let i = 0; i < menuItems.length; i++) {
  1689.         let menuItem = menuItems[i];
  1690.         if (menuItem.handlerApp && menuItem.handlerApp.equals(handlerApp)) {
  1691.           actionsMenu.selectedIndex = i;
  1692.           this.onSelectAction(menuItem);
  1693.           break;
  1694.         }
  1695.       }
  1696.     }
  1697.   },
  1698.  
  1699.   // Mark which item in the list was last selected so we can reselect it
  1700.   // when we rebuild the list or when the user returns to the prefpane.
  1701.   onSelectionChanged: function() {
  1702.     if (this._list.selectedItem)
  1703.       this._list.setAttribute("lastSelectedType",
  1704.                               this._list.selectedItem.getAttribute("type"));
  1705.   },
  1706.  
  1707.   _setIconClassForPreferredAction: function(aHandlerInfo, aElement) {
  1708.     // If this returns true, the attribute that CSS sniffs for was set to something
  1709.     // so you shouldn't manually set an icon URI.
  1710.     // This removes the existing actionIcon attribute if any, even if returning false.
  1711.     aElement.removeAttribute("actionIcon");
  1712.  
  1713.     if (aHandlerInfo.alwaysAskBeforeHandling) {
  1714.       aElement.setAttribute(APP_ICON_ATTR_NAME, "ask");
  1715.       return true;
  1716.     }
  1717.  
  1718.     switch (aHandlerInfo.preferredAction) {
  1719.       case Ci.nsIHandlerInfo.saveToDisk:
  1720.         aElement.setAttribute(APP_ICON_ATTR_NAME, "save");
  1721.         return true;
  1722.  
  1723.       case Ci.nsIHandlerInfo.handleInternally:
  1724.         if (isFeedType(aHandlerInfo.type)) {
  1725.           aElement.setAttribute(APP_ICON_ATTR_NAME, "feed");
  1726.           return true;
  1727.         }
  1728.         break;
  1729.  
  1730.       case kActionUsePlugin:
  1731.         aElement.setAttribute(APP_ICON_ATTR_NAME, "plugin");
  1732.         return true;
  1733.     }
  1734.     aElement.removeAttribute(APP_ICON_ATTR_NAME);
  1735.     return false;
  1736.   },
  1737.  
  1738.   _getIconURLForPreferredAction: function(aHandlerInfo) {
  1739.     switch (aHandlerInfo.preferredAction) {
  1740.       case Ci.nsIHandlerInfo.useSystemDefault:
  1741.         return this._getIconURLForSystemDefault(aHandlerInfo);
  1742.  
  1743.       case Ci.nsIHandlerInfo.useHelperApp:
  1744.         let (preferredApp = aHandlerInfo.preferredApplicationHandler) {
  1745.           if (this.isValidHandlerApp(preferredApp))
  1746.             return this._getIconURLForHandlerApp(preferredApp);
  1747.         }
  1748.         break;
  1749.  
  1750.       // This should never happen, but if preferredAction is set to some weird
  1751.       // value, then fall back to the generic application icon.
  1752.       default:
  1753.         return ICON_URL_APP;
  1754.     }
  1755.   },
  1756.  
  1757.   _getIconURLForHandlerApp: function(aHandlerApp) {
  1758.     if (aHandlerApp instanceof Ci.nsILocalHandlerApp)
  1759.       return this._getIconURLForFile(aHandlerApp.executable);
  1760.  
  1761.     if (aHandlerApp instanceof Ci.nsIWebHandlerApp)
  1762.       return this._getIconURLForWebApp(aHandlerApp.uriTemplate);
  1763.  
  1764.     if (aHandlerApp instanceof Ci.nsIWebContentHandlerInfo)
  1765.       return this._getIconURLForWebApp(aHandlerApp.uri)
  1766.  
  1767.     // We know nothing about other kinds of handler apps.
  1768.     return "";
  1769.   },
  1770.  
  1771.   _getIconURLForFile: function(aFile) {
  1772.     var fph = this._ioSvc.getProtocolHandler("file").
  1773.               QueryInterface(Ci.nsIFileProtocolHandler);
  1774.     var urlSpec = fph.getURLSpecFromFile(aFile);
  1775.  
  1776.     return "moz-icon://" + urlSpec + "?size=16";
  1777.   },
  1778.  
  1779.   _getIconURLForWebApp: function(aWebAppURITemplate) {
  1780.     var uri = this._ioSvc.newURI(aWebAppURITemplate, null, null);
  1781.  
  1782.     // Unfortunately we can't use the favicon service to get the favicon,
  1783.     // because the service looks in the annotations table for a record with
  1784.     // the exact URL we give it, and users won't have such records for URLs
  1785.     // they don't visit, and users won't visit the web app's URL template,
  1786.     // they'll only visit URLs derived from that template (i.e. with %s
  1787.     // in the template replaced by the URL of the content being handled).
  1788.  
  1789.     if (/^https?/.test(uri.scheme))
  1790.       return uri.prePath + "/favicon.ico";
  1791.  
  1792.     return "";
  1793.   },
  1794.  
  1795.   _getIconURLForSystemDefault: function(aHandlerInfo) {
  1796.     // Handler info objects for MIME types on some OSes implement a property bag
  1797.     // interface from which we can get an icon for the default app, so if we're
  1798.     // dealing with a MIME type on one of those OSes, then try to get the icon.
  1799.     if ("wrappedHandlerInfo" in aHandlerInfo) {
  1800.       let wrappedHandlerInfo = aHandlerInfo.wrappedHandlerInfo;
  1801.  
  1802.       if (wrappedHandlerInfo instanceof Ci.nsIMIMEInfo &&
  1803.           wrappedHandlerInfo instanceof Ci.nsIPropertyBag) {
  1804.         try {
  1805.           let url = wrappedHandlerInfo.getProperty("defaultApplicationIconURL");
  1806.           if (url)
  1807.             return url + "?size=16";
  1808.         }
  1809.         catch(ex) {}
  1810.       }
  1811.     }
  1812.  
  1813.     // If this isn't a MIME type object on an OS that supports retrieving
  1814.     // the icon, or if we couldn't retrieve the icon for some other reason,
  1815.     // then use a generic icon.
  1816.     return ICON_URL_APP;
  1817.   }
  1818.  
  1819. };
  1820.