Skip to content

Commit

Permalink
fix open pdf rotocol handler for Z standalone
Browse files Browse the repository at this point in the history
  • Loading branch information
Joscha Legewie committed May 9, 2013
1 parent f216350 commit 1b4ac99
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 147 deletions.
3 changes: 0 additions & 3 deletions chrome.manifest
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ skin zotfile default chrome/skin/default/zotfile/

overlay chrome://zotero/content/zoteroPane.xul chrome://zotfile/content/overlay.xul

component {1c32a2c0-ac5e-11e2-9e96-0800200c9a66} components/attachmentService.js
contract @mozilla.org/network/protocol;1?name=zotfile {1c32a2c0-ac5e-11e2-9e96-0800200c9a66}

locale zotfile de-DE chrome/locale/de-DE/
locale zotfile en-US chrome/locale/en-US/
locale zotfile fr-FR chrome/locale/fr-FR/
Expand Down
2 changes: 1 addition & 1 deletion chrome/content/zotfile/include.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ if (!Zotero.ZotFile) {
var zotfileLoader = Components.classes["@mozilla.org/moz/jssubscript-loader;1"]
.getService(Components.interfaces.mozIJSSubScriptLoader);
zotfileLoader.loadSubScript("chrome://zotfile/content/zotfile.js");
zotfileLoader.loadSubScript("chrome://zotfile/content/openPDF-protocol-handler.js");
}

window.addEventListener('load', function(e) {
Expand All @@ -18,4 +19,3 @@ window.addEventListener('load', function(e) {
window.ZoteroPane.document.getElementById('zotero-collections-tree').addEventListener('click', Zotero.ZotFile.updateModifiedAttachmentsSearch, false);
}
}, false);

98 changes: 98 additions & 0 deletions chrome/content/zotfile/openPDF-protocol-handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
var OpenPDFExtension = new function(){
this.newChannel = newChannel;
// this.__defineGetter__('loadAsChrome', function () { return false; });

function newChannel(uri) {
// get components
var ios = Components.classes["@mozilla.org/network/io-service;1"];
var Zotero = Components.classes["@zotero.org/Zotero;1"]
.getService(Components.interfaces.nsISupports)
.wrappedJSObject;
var zz = Zotero.ZotFile;
generateContent:try {
// get arguments from uri
// e.g. zotero://open-pdf/0_EFWJW9U7
var [key, page] = uri.path.substr(1).split('/');
// exit if no key
if(!key) return;
// get zotero item from key
var lkh = Zotero.Items.parseLibraryKeyHash(key);
if (!lkh) return;
var item = Zotero.Items.getByLibraryAndKey(lkh.libraryID, lkh.key);
if(item==false) return;
// if attachment, open file and go to page
if(!item.isAttachment()) return;
// get file and path
var file = item.getFile();
var path = file.path;
// check whether pdf file
if(path.indexOf('.pdf')==-1) return;
// open pdf and go to page (system specific)
if(Zotero.isMac) {
// open pdf file
zz.runProcess('/usr/bin/open', ['-a', 'Preview', path]);
// go to page using applescript
var args = [
'-e', 'tell app "Preview" to activate',
'-e', 'tell app "System Events" to keystroke "g" using {option down, command down}',
'-e', 'tell app "System Events" to keystroke "' + page + '"',
'-e', 'tell app "System Events" to keystroke return']
if (page) zz.runProcess('/usr/bin/osascript', args, false)
}
if(Zotero.isWin) {
// path to Adobe Reader
// 'C:\Program Files (x86)\Adobe\Reader 10.0\Reader\AcroRd32.exe'
// http://stackoverflow.com/questions/11934159/how-extension-can-read-the-registry
var wrk = Components.classes["@mozilla.org/windows-registry-key;1"]
.createInstance(Components.interfaces.nsIWindowsRegKey);
wrk.open(wrk.ROOT_KEY_LOCAL_MACHINE,
"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths",
wrk.ACCESS_READ);
var acrobat = wrk.readStringValue("AcroRd32.exe");
wrk.close();
// open pdf file on page
zz.runProcess(acrobat, ['/A','"path=' + page + '"', path])
}
if(Zotero.isLinux) {
var cmd = zz.prefs.getCharPref('pdfExtraction.openPdfLinux');
// try okular and evince when nothing is set
if (cmd=="") {
// try okular
if (zz.fileExists('/usr/bin/okular')) {
zz.runProcess('/usr/bin/okular', ['-p', page, path]);
}
// try evince
else {
if (zz.fileExists('/usr/bin/evince')) {
zz.runProcess('/usr/bin/evince', ['-p', page, path]);
}
else {
zz.infoWindow('Zotfile', zz.ZFgetString('general.open.pdf'));
}
}
}
// user defined command
else {
// process option to correct format
// e.g. cmd = '/usr/bin/okular -p %(page) %(path)';
cmd = cmd.split(' ')
.map(function(str) {
return str_format(str, {'page': page, 'path': path});
});
// run process
var ex = zz.runProcess(cmd[0], cmd.slice(1));
if(ex==-1) zz.infoWindow('Zotfile', zz.ZFgetString('general.open.pdf'));
}
}
}
catch (e) {
Zotero.debug(e);
throw (e);
}
}
};

var zotero_ext = Components.classes["@mozilla.org/network/protocol;1?name=zotero"].getService();
var OpenPDFSpec = "zotero://open-pdf"
zotero_ext.wrappedJSObject._extensions[OpenPDFSpec] = OpenPDFExtension;

2 changes: 1 addition & 1 deletion chrome/content/zotfile/zotfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -2833,7 +2833,7 @@ Zotero.ZotFile = {
cite=Zotero.ZotFile.replaceWildcard(item, "%a %y:").replace(/_(?!.*_)/," and ").replace(/_/g,", ");
// get uri
var lib = att.libraryID===null ? 0 : att.libraryID;
var uri = 'zotfile://open/' + lib + '_' + att.key + '/' + anno.page;
var uri = 'zotero://open-pdf/' + lib + '_' + att.key + '/' + anno.page;

// add to note text pdfExtractionNoteRemoveHtmlNote
if(anno.content && anno.content != "" &&
Expand Down
142 changes: 0 additions & 142 deletions components/attachmentService.js

This file was deleted.

0 comments on commit 1b4ac99

Please sign in to comment.