Skip to content

Commit

Permalink
Allow custom handler for PDF files
Browse files Browse the repository at this point in the history
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 zotero#1450 by @ehhc.

Closes zotero#1450
  • Loading branch information
dstillman committed Mar 6, 2018
1 parent f6926cc commit da09a3b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 16 deletions.
33 changes: 17 additions & 16 deletions chrome/content/zotero/xpcom/zotero.js
Expand Up @@ -1110,21 +1110,8 @@ Services.scriptloader.loadSubScript("resource://zotero/polyfill.js");
else {
var pref = "fallbackLauncher.unix";
}
var path = Zotero.Prefs.get(pref);

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);
let launcher = Zotero.Prefs.get(pref);
this.launchFileWithApplication(file.path, launcher);
}
catch (e) {
Zotero.debug(e);
Expand All @@ -1142,7 +1129,21 @@ Services.scriptloader.loadSubScript("resource://zotero/polyfill.js");
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]);
};


/**
Expand Down
15 changes: 15 additions & 0 deletions chrome/content/zotero/zoteroPane.js
Expand Up @@ -4199,6 +4199,21 @@ var ZoteroPane = new function()
}
else {
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);
}
}
Expand Down
3 changes: 3 additions & 0 deletions defaults/preferences/zotero.js
Expand Up @@ -184,6 +184,9 @@ pref("extensions.zotero.ingester.allowedSites", "");
pref("extensions.zotero.connector.repo.lastCheck.localTime", 0);
pref("extensions.zotero.connector.repo.lastCheck.repoTime", 0);

// Custom file handlers
pref("extensions.zotero.fileHandler.pdf", "");

// File/URL opening executable if launch() fails
pref("extensions.zotero.fallbackLauncher.unix", "/usr/bin/xdg-open");
pref("extensions.zotero.fallbackLauncher.windows", "");
Expand Down

0 comments on commit da09a3b

Please sign in to comment.