Skip to content

Commit

Permalink
Zotero 5.0.75: use async FilePicker
Browse files Browse the repository at this point in the history
  • Loading branch information
wshanks committed Sep 9, 2019
1 parent 7597b75 commit 92056ee
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 104 deletions.
2 changes: 1 addition & 1 deletion addon/chrome/content/lyz/include.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Only create main object once
if (!Zotero.Lyz) {
const lyzLoader = Components.classes["@mozilla.org/moz/jssubscript-loader;1"]
.getService(Components.interfaces.mozIJSSubScriptLoader);
.getService(Components.interfaces.mozIJSSubScriptLoader);
lyzLoader.loadSubScript("chrome://lyz/content/lyz.js");
window.addEventListener('load', function(e) { Zotero.Lyz.init(); }, false);
}
195 changes: 92 additions & 103 deletions addon/chrome/content/lyz/lyz.js
Original file line number Diff line number Diff line change
Expand Up @@ -406,46 +406,79 @@ Zotero.Lyz = {
var keyhash = Zotero.Items.parseLibraryKeyHash(key);
return Zotero.Items.getByLibraryAndKey(keyhash.libraryID, keyhash.key);
},



dialog_FilePickerOpen : function(win, title, filter_title, filter) {
var nsIFilePicker = Components.interfaces.nsIFilePicker;
var fp = Components.classes["@mozilla.org/filepicker;1"]
.createInstance(nsIFilePicker);
fp.init(win, title, nsIFilePicker.modeOpen);
getFilePicker: async function() {
const version = /^(\d+)\.(\d+)\.(\d+)/.exec(Zotero.version)
const preFP = version[1] == 5 && version[2] == 0 && version[3] < 75
if (preFP) {
var nsIFilePicker = Components.interfaces.nsIFilePicker
var fp = Components.classes["@mozilla.org/filepicker;1"]
.createInstance(nsIFilePicker)
} else {
var FilePicker = require('zotero/filePicker').default
var fp = new FilePicker()
}

return [preFP, fp]
},

dialog_FilePickerOpen: async function(win, title, filter_title, filter) {
var [preFP, fp] = await this.getFilePicker()
fp.init(win, title, fp.modeOpen);
fp.appendFilter(filter_title, filter);
var res = fp.show();
if (res == nsIFilePicker.returnOK) {
return fp.file;
if (preFP) {
var res = fp.show()
} else {
var res = await fp.show()
}
if (res == fp.returnOK) {
if (preFP) {
return fp.file.path
} else {
return fp.file
}
}
return null;
},

dialog_FilePickerSave : function(win, title, filter_title, filter) {
var nsIFilePicker = Components.interfaces.nsIFilePicker;
var fp = Components.classes["@mozilla.org/filepicker;1"]
.createInstance(nsIFilePicker);
fp.init(win, title, nsIFilePicker.modeSave);
dialog_FilePickerSave: async function(win, title, filter_title, filter) {
var [preFP, fp] = await this.getFilePicker()
fp.init(win, title, fp.modeSave);
fp.appendFilter(filter_title, filter);
var res = fp.show();
if (res == nsIFilePicker.returnOK) {
if (preFP) {
var res = fp.show()
} else {
var res = await fp.show()
}
var path
if (res == fp.returnOK) {
// add default extension, guess there must be other way to do it
// but this is what came to my mind first
if (fp.file.path.split(".").length < 2) {
if (preFP) {
path = fp.file.path
} else {
path = fp.file
}

if (path.split(".").length < 2) {
// this is weird, but I don't know how to set new path
var file = Components.classes["@mozilla.org/file/local;1"]
.createInstance(Components.interfaces.nsIFile);
file.initWithPath(fp.file.path + ".bib");
file.initWithPath(path + ".bib");
file.create(file.NORMAL_FILE_TYPE, 0666);
return file;
} else {// overwrite the file if it exists
this.replace = true;
return fp.file;
return path
}
} else if (res == nsIFilePicker.returnReplace) {
} else if (res == fp.returnReplace) {
this.replace = true;
return fp.file;
if (preFP) {
path = fp.file.path
} else {
path = fp.file
}
return path
} else {
return false;
}
Expand Down Expand Up @@ -503,9 +536,6 @@ Zotero.Lyz = {
},

test: function() {
if (this.lyzDisableCheck()) {
return
}
var win = this.wm.getMostRecentWindow("navigator:browser");
var t = prompt("Command", "server-get-filename");
if (!t) {
Expand All @@ -526,24 +556,24 @@ Zotero.Lyz = {
win.alert("DONE");
},

checkDocInDB: Zotero.Promise.coroutine(function*() {
checkDocInDB: async function() {
var doc, res;
var win = this.wm.getMostRecentWindow("navigator:browser");
doc = this.lyxGetDoc();
if (!doc) {
win.alert("Could not retrieve document name.");
return null;
}
res = yield this.DB.queryAsync("SELECT doc,bib FROM docs WHERE doc = ?",[doc]);
res = await this.DB.queryAsync("SELECT doc,bib FROM docs WHERE doc = ?",[doc]);
if (res.length === 0) {
return [ res, doc ];
}
return [res[0].bib, doc];
}),
},

addNewDocument: Zotero.Promise.coroutine(function*(doc, bib) {
yield this.DB.queryAsync("INSERT INTO docs (doc,bib) VALUES(?,?)",[doc,bib]);
}),
addNewDocument: async function(doc, bib) {
await this.DB.queryAsync("INSERT INTO docs (doc,bib) VALUES(?,?)",[doc,bib]);
},

exportToBibtex: Zotero.Promise.coroutine(function*(items, bib, zids) {
// returns hash {id:[citekey,text]}
Expand Down Expand Up @@ -735,10 +765,7 @@ Zotero.Lyz = {
return [ citekey, text ];
}),

checkAndCite: Zotero.Promise.coroutine(function*() {
if (this.lyzDisableCheck()) {
return
}
checkAndCite: async function() {
// export citation to Bibtex
var win = this.wm.getMostRecentWindow("navigator:browser");
var zitems = win.ZoteroPane.getSelectedItems();
Expand All @@ -748,10 +775,9 @@ Zotero.Lyz = {
return;
}
// check document name
var res = yield this.checkDocInDB();
var res = await this.checkDocInDB();
var bib = res[0];
var doc = res[1];
var bib_file;
var items;
var keys;
var entries_text = "";
Expand All @@ -764,25 +790,24 @@ Zotero.Lyz = {
// FIXME: the buttons don't show correctly, STD_YES_NO_BUTTONS doesn't work
res = win.confirm(t, "BibTex database selection");
if (res) {
bib_file = this.dialog_FilePickerSave(win,
bib = await this.dialog_FilePickerSave(win,
"Select Bibtex file for " + doc, "Bibtex", "*.bib");
if (!bib_file)
if (!bib)
return;

} else {
bib_file = this.dialog_FilePickerOpen(win,
bib = await this.dialog_FilePickerOpen(win,
"Select Bibtex file for " + doc, "Bibtex", "*.bib");
if (!bib_file)
if (!bib)
return;
}

bib = bib_file.path;
if (bib_file)
yield this.addNewDocument(doc, bib);
if (bib)
await this.addNewDocument(doc, bib);
else
return;//file dialog canceled
}
items = yield this.exportToBibtex(zitems, bib);
items = await this.exportToBibtex(zitems, bib);
keys = [];
var zids = [];

Expand All @@ -792,10 +817,10 @@ Zotero.Lyz = {
keys.push(citekey);
//check database, if not in, append to entries_text
//single key can be associated with several bibtex files
res = yield this.DB.queryAsync("SELECT key FROM keys WHERE bib=? AND zid=?",[bib,zid]);
res = await this.DB.queryAsync("SELECT key FROM keys WHERE bib=? AND zid=?",[bib,zid]);

if (res.length === 0) {
yield this.DB.queryAsync("INSERT INTO keys VALUES(null,?,?,?)",[citekey,bib,zid]);
await this.DB.queryAsync("INSERT INTO keys VALUES(null,?,?,?)",[citekey,bib,zid]);
zids.push(zid);
entries_text += text;
} else if (res[0].key != citekey) {
Expand Down Expand Up @@ -828,12 +853,9 @@ Zotero.Lyz = {
} else {
res = this._lyxAskServer("citation-insert:" + keys.join(","));
}
}),
},

updateBibtexAll: Zotero.Promise.coroutine(function*() {
if (this.lyzDisableCheck()) {
return
}
//first update from the bibtex file

yield this.updateFromBibtexFile();
Expand Down Expand Up @@ -946,9 +968,6 @@ Zotero.Lyz = {
}),

dbDeleteBib: Zotero.Promise.coroutine(function*() {
if (this.lyzDisableCheck()) {
return
}
var win = this.wm.getMostRecentWindow("navigator:browser");
var dic = yield this.DB.queryAsync("SELECT bib FROM keys GROUP BY bib");
var params = {
Expand All @@ -966,6 +985,9 @@ Zotero.Lyz = {
if (params.out) {
bib = params.out.item;
}
if (!bib) {
return
}
res = win.confirm("You are about to delete record of BibTeX database:\n" +
bib +
"\nRecord about associated documents will also be deleted.\n",
Expand All @@ -977,9 +999,6 @@ Zotero.Lyz = {
}),

dbDeleteDoc: Zotero.Promise.coroutine(function*(doc, bib) {
if (this.lyzDisableCheck()) {
return
}
var win = this.wm.getMostRecentWindow("navigator:browser");
var dic = yield this.DB.queryAsync("SELECT doc FROM docs");
var params = {
Expand All @@ -997,19 +1016,19 @@ Zotero.Lyz = {
if (params.out) {
doc = params.out.item;
}
if (!doc) {
return
}
res = win.confirm("Do you really want to delete the LyZ database record of the document\n" +
doc + "?");
if (!res)
return;
yield this.DB.queryAsync("DELETE FROM docs WHERE doc=?",[doc]);
}),

dbRenameDoc: Zotero.Promise.coroutine(function*() {
if (this.lyzDisableCheck()) {
return
}
dbRenameDoc: async function() {
var win = this.wm.getMostRecentWindow("navigator:browser");
var dic = yield this.DB.queryAsync("SELECT id,doc FROM docs");
var dic = await this.DB.queryAsync("SELECT id,doc FROM docs");
var params = {
inn : {
items : dic,
Expand All @@ -1025,48 +1044,18 @@ Zotero.Lyz = {
if (params.out) {
doc = params.out.item;
}
var newfname = this.dialog_FilePickerOpen(win,
"Select LyX document for " + doc, "LyX", "*.lyx").path;
var newfname = await this.dialog_FilePickerOpen(win,
"Select LyX document for " + doc, "LyX", "*.lyx")
// have to replace \ with / on windows because lyxserver returns unix style paths
newfname = newfname.replace(/\\/g, "/");
if (!newfname)
return;
yield this.DB.queryAsync("UPDATE docs SET doc=? WHERE doc=?",[newfname,doc]);
}),

dbRenameDoc: Zotero.Promise.coroutine(function*() {
if (this.lyzDisableCheck()) {
return
}
var win = this.wm.getMostRecentWindow("navigator:browser");
var dic = yield this.DB.queryAsync("SELECT id,doc FROM docs");
var params = {
inn : {
items : dic,
type : "doc"
},
out : null
};
var res = win.openDialog("chrome://lyz/content/select.xul", "",
"chrome, dialog, modal, centerscreen, resizable=yes", params);
if (!params.out)
return;
var doc;
if (params.out) {
doc = params.out.item;
}
var newfname = this.dialog_FilePickerOpen(win,
"Select LyX document for " + doc, "LyX", "*.lyx").path;
// have to replace \ with / on windows because lyxserver returns unix style paths
newfname = newfname.replace(/\\/g, "/");
if (!newfname)
return;
yield this.DB.queryAsync("UPDATE docs SET doc=? WHERE doc=?",[newfname,doc]);
}),
await this.DB.queryAsync("UPDATE docs SET doc=? WHERE doc=?",[newfname,doc]);
},

dbRenameBib: Zotero.Promise.coroutine(function*() {
dbRenameBib: async function() {
var win = this.wm.getMostRecentWindow("navigator:browser");
var dic = yield this.DB.queryAsync("SELECT DISTINCT bib FROM docs");
var dic = await this.DB.queryAsync("SELECT DISTINCT bib FROM docs");
var params = {
inn : {
items : dic,
Expand All @@ -1082,13 +1071,13 @@ Zotero.Lyz = {
if (params.out) {
bib = params.out.item;
}
newfname = this.dialog_FilePickerOpen(win, "Select Bibtex file for " +
bib, "Bibtex", "*.bib").path;
newfname = await this.dialog_FilePickerOpen(win, "Select Bibtex file for " +
bib, "Bibtex", "*.bib")
if (!newfname)
return;
yield this.DB.queryAsync("UPDATE docs SET bib=? WHERE bib=?",[newfname,bib]);
yield this.DB.queryAsync("UPDATE keys SET bib=? WHERE bib=?",[newfname,bib]);
}),
await this.DB.queryAsync("UPDATE docs SET bib=? WHERE bib=?",[newfname,bib]);
await this.DB.queryAsync("UPDATE keys SET bib=? WHERE bib=?",[newfname,bib]);
},

shutdown: Zotero.Promise.coroutine(function*() {
yield Zotero.Lyz.DB.closeDatabase(true)
Expand Down

0 comments on commit 92056ee

Please sign in to comment.