Allow custom handler for PDF files

Configurable via extensions.zotero.fileHandler.pdf hidden pref for now,
though we'll probably make it a visible pref.

We also appear to have been doing blocking launches when launch()
failed, which may have been causing UI hangs when opening files on some
Linux installations. (I'm not sure if that's an issue with recent
Firefox builds. launch() works on Ubuntu 17.10.) All launches are now
async.

This is a rewritten version of PR #1450 by @ehhc.

Closes #1450
This commit is contained in:
Dan Stillman 2018-03-05 19:29:03 -05:00
parent f6926ccda6
commit da09a3bb96
3 changed files with 35 additions and 16 deletions

View file

@ -1110,21 +1110,8 @@ Services.scriptloader.loadSubScript("resource://zotero/polyfill.js");
else { else {
var pref = "fallbackLauncher.unix"; var pref = "fallbackLauncher.unix";
} }
var path = Zotero.Prefs.get(pref); let launcher = Zotero.Prefs.get(pref);
this.launchFileWithApplication(file.path, launcher);
var exec = Components.classes["@mozilla.org/file/local;1"]
.createInstance(Components.interfaces.nsILocalFile);
exec.initWithPath(path);
if (!exec.exists()) {
throw new Error(path + " does not exist");
}
var proc = Components.classes["@mozilla.org/process/util;1"]
.createInstance(Components.interfaces.nsIProcess);
proc.init(exec);
var args = [file.path];
proc.runw(true, args, args.length);
} }
catch (e) { catch (e) {
Zotero.debug(e); Zotero.debug(e);
@ -1142,8 +1129,22 @@ Services.scriptloader.loadSubScript("resource://zotero/polyfill.js");
nsIEPS.loadUrl(uri); nsIEPS.loadUrl(uri);
} }
} }
};
/**
* Launch a file with the given application
*/
this.launchFileWithApplication = function (filePath, applicationPath) {
var exec = Zotero.File.pathToFile(applicationPath);
if (!exec.exists()) {
throw new Error("'" + applicationPath + "' does not exist");
} }
// Async, but we don't want to block
Zotero.Utilities.Internal.exec(applicationPath, [filePath]);
};
/** /**
* Launch an HTTP URL externally, the best way we can * Launch an HTTP URL externally, the best way we can

View file

@ -4199,6 +4199,21 @@ var ZoteroPane = new function()
} }
else { else {
Zotero.Notifier.trigger('open', 'file', itemID); Zotero.Notifier.trigger('open', 'file', itemID);
// Custom PDF handler
if (item.attachmentContentType === 'application/pdf') {
let pdfHandler = Zotero.Prefs.get("fileHandler.pdf");
if (pdfHandler) {
if (yield OS.File.exists(pdfHandler)) {
Zotero.launchFileWithApplication(file.path, pdfHandler);
continue;
}
else {
Zotero.logError(`${pdfHandler} not found -- launching file normally`);
}
}
}
Zotero.launchFile(file); Zotero.launchFile(file);
} }
} }

View file

@ -184,6 +184,9 @@ pref("extensions.zotero.ingester.allowedSites", "");
pref("extensions.zotero.connector.repo.lastCheck.localTime", 0); pref("extensions.zotero.connector.repo.lastCheck.localTime", 0);
pref("extensions.zotero.connector.repo.lastCheck.repoTime", 0); pref("extensions.zotero.connector.repo.lastCheck.repoTime", 0);
// Custom file handlers
pref("extensions.zotero.fileHandler.pdf", "");
// File/URL opening executable if launch() fails // File/URL opening executable if launch() fails
pref("extensions.zotero.fallbackLauncher.unix", "/usr/bin/xdg-open"); pref("extensions.zotero.fallbackLauncher.unix", "/usr/bin/xdg-open");
pref("extensions.zotero.fallbackLauncher.windows", ""); pref("extensions.zotero.fallbackLauncher.windows", "");