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