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