mirror of
https://github.com/gryf/pentadactyl-pm.git
synced 2025-12-21 14:18:00 +01:00
Automagically launch files from links in the :download manager.
This commit is contained in:
@@ -7,7 +7,7 @@
|
|||||||
Components.utils.import("resource://dactyl/bootstrap.jsm");
|
Components.utils.import("resource://dactyl/bootstrap.jsm");
|
||||||
defineModule("downloads", {
|
defineModule("downloads", {
|
||||||
exports: ["Download", "Downloads", "downloads"],
|
exports: ["Download", "Downloads", "downloads"],
|
||||||
use: ["io", "services", "template", "util"]
|
use: ["io", "prefs", "services", "template", "util"]
|
||||||
}, this);
|
}, this);
|
||||||
|
|
||||||
Cu.import("resource://gre/modules/DownloadUtils.jsm", this);
|
Cu.import("resource://gre/modules/DownloadUtils.jsm", this);
|
||||||
@@ -19,17 +19,19 @@ var states = iter([v, k.slice(prefix.length).toLowerCase()]
|
|||||||
.toObject();
|
.toObject();
|
||||||
|
|
||||||
var Download = Class("Download", {
|
var Download = Class("Download", {
|
||||||
init: function init(id, document) {
|
init: function init(id, list) {
|
||||||
let self = XPCSafeJSObjectWrapper(services.downloadManager.getDownload(id));
|
let self = XPCSafeJSObjectWrapper(services.downloadManager.getDownload(id));
|
||||||
self.__proto__ = this;
|
self.__proto__ = this;
|
||||||
this.instance = this;
|
this.instance = this;
|
||||||
|
this.list = list;
|
||||||
|
|
||||||
this.nodes = {};
|
this.nodes = {};
|
||||||
util.xmlToDom(
|
util.xmlToDom(
|
||||||
<li highlight="Download" key="row" xmlns:dactyl={NS} xmlns={XHTML}>
|
<li highlight="Download" key="row" xmlns:dactyl={NS} xmlns={XHTML}>
|
||||||
<span highlight="DownloadTitle">
|
<span highlight="DownloadTitle">
|
||||||
<span highlight="Link">
|
<span highlight="Link">
|
||||||
<a key="title" href={self.target.spec} path={self.targetFile.path}>{self.displayName}</a>
|
<a key="launch" dactyl:command="download.command"
|
||||||
|
href={self.target.spec} path={self.targetFile.path}>{self.displayName}</a>
|
||||||
<span highlight="LinkInfo">{self.targetFile.path}</span>
|
<span highlight="LinkInfo">{self.targetFile.path}</span>
|
||||||
</span>
|
</span>
|
||||||
</span>
|
</span>
|
||||||
@@ -50,7 +52,7 @@ var Download = Class("Download", {
|
|||||||
<span highlight="DownloadTime" key="time"/>
|
<span highlight="DownloadTime" key="time"/>
|
||||||
<a highlight="DownloadSource" key="source" href={self.source.spec}>{self.source.spec}</a>
|
<a highlight="DownloadSource" key="source" href={self.source.spec}>{self.source.spec}</a>
|
||||||
</li>,
|
</li>,
|
||||||
document, this.nodes);
|
this.list.document, this.nodes);
|
||||||
|
|
||||||
for (let [key, node] in Iterator(this.nodes)) {
|
for (let [key, node] in Iterator(this.nodes)) {
|
||||||
node.dactylDownload = self;
|
node.dactylDownload = self;
|
||||||
@@ -81,6 +83,7 @@ var Download = Class("Download", {
|
|||||||
allowed: Class.memoize(function () let (self = this) ({
|
allowed: Class.memoize(function () let (self = this) ({
|
||||||
get cancel() self.cancelable && self.inState(["downloading", "paused", "starting"]),
|
get cancel() self.cancelable && self.inState(["downloading", "paused", "starting"]),
|
||||||
get delete() !this.cancel && self.targetFile.exists(),
|
get delete() !this.cancel && self.targetFile.exists(),
|
||||||
|
get launch() self.targetFile.exists() && self.inState(["finished"]),
|
||||||
get pause() self.inState(["downloading"]),
|
get pause() self.inState(["downloading"]),
|
||||||
get remove() self.inState(["blocked_parental", "blocked_policy",
|
get remove() self.inState(["blocked_parental", "blocked_policy",
|
||||||
"canceled", "dirty", "failed", "finished"]),
|
"canceled", "dirty", "failed", "finished"]),
|
||||||
@@ -102,6 +105,31 @@ var Download = Class("Download", {
|
|||||||
delete: function delete() {
|
delete: function delete() {
|
||||||
this.targetFile.remove(false);
|
this.targetFile.remove(false);
|
||||||
this.updateStatus();
|
this.updateStatus();
|
||||||
|
},
|
||||||
|
launch: function launch() {
|
||||||
|
let self = this;
|
||||||
|
// Behavior mimics that of the builtin Download Manager.
|
||||||
|
function action() {
|
||||||
|
try {
|
||||||
|
if (this.MIMEInfo && this.MIMEInfo.preferredAction == this.MIMEInfo.useHelperApp)
|
||||||
|
this.MIMEInfo.launchWithFile(file)
|
||||||
|
else
|
||||||
|
file.launch();
|
||||||
|
}
|
||||||
|
catch (e) {
|
||||||
|
services.externalProtocol.loadUrl(this.target);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let file = io.File(this.targetFile);
|
||||||
|
if (file.isExecutable() && prefs.get("browser.download.manager.alertOnEXEOpen", true))
|
||||||
|
this.list.modules.commandline.input("This will launch an executable download. Continue? (yes/[no]) ",
|
||||||
|
function (resp) {
|
||||||
|
if (resp && resp.match(/^y(es)?$/i))
|
||||||
|
action.call(self);
|
||||||
|
});
|
||||||
|
else
|
||||||
|
action.call(this);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -181,7 +209,7 @@ var DownloadList = Class("DownloadList",
|
|||||||
|
|
||||||
addDownload: function addDownload(id) {
|
addDownload: function addDownload(id) {
|
||||||
if (!(id in this.downloads)) {
|
if (!(id in this.downloads)) {
|
||||||
this.downloads[id] = Download(id, this.document);
|
this.downloads[id] = Download(id, this);
|
||||||
|
|
||||||
let index = values(this.downloads).sort(function (a, b) a.compare(b))
|
let index = values(this.downloads).sort(function (a, b) a.compare(b))
|
||||||
.indexOf(this.downloads[id]);
|
.indexOf(this.downloads[id]);
|
||||||
|
|||||||
@@ -38,6 +38,7 @@ var Services = Module("Services", {
|
|||||||
this.add("downloadManager", "@mozilla.org/download-manager;1", Ci.nsIDownloadManager);
|
this.add("downloadManager", "@mozilla.org/download-manager;1", Ci.nsIDownloadManager);
|
||||||
this.add("environment", "@mozilla.org/process/environment;1", Ci.nsIEnvironment);
|
this.add("environment", "@mozilla.org/process/environment;1", Ci.nsIEnvironment);
|
||||||
this.add("extensionManager", "@mozilla.org/extensions/manager;1", Ci.nsIExtensionManager);
|
this.add("extensionManager", "@mozilla.org/extensions/manager;1", Ci.nsIExtensionManager);
|
||||||
|
this.add("externalProtocol", "@mozilla.org/uriloader/external-protocol-service;1", Ci.nsIExternalProtocolService);
|
||||||
this.add("favicon", "@mozilla.org/browser/favicon-service;1", Ci.nsIFaviconService);
|
this.add("favicon", "@mozilla.org/browser/favicon-service;1", Ci.nsIFaviconService);
|
||||||
this.add("focus", "@mozilla.org/focus-manager;1", Ci.nsIFocusManager);
|
this.add("focus", "@mozilla.org/focus-manager;1", Ci.nsIFocusManager);
|
||||||
this.add("fuel", "@mozilla.org/fuel/application;1", Ci.extIApplication);
|
this.add("fuel", "@mozilla.org/fuel/application;1", Ci.extIApplication);
|
||||||
|
|||||||
Reference in New Issue
Block a user