Add universal progress queues (#1573)
This commit is contained in:
parent
b6065a7af6
commit
ae7ef109b7
14 changed files with 430 additions and 273 deletions
|
@ -3,7 +3,7 @@
|
|||
<!DOCTYPE window SYSTEM "chrome://zotero/locale/zotero.dtd">
|
||||
|
||||
<window xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
|
||||
title="&zotero.progress.title;" width="550" height="230"
|
||||
width="550" height="230"
|
||||
id="zotero-progress">
|
||||
<vbox style="padding:10px" flex="1">
|
||||
<label id="label" control="progress-indicator" value=""/>
|
||||
|
@ -17,9 +17,9 @@
|
|||
<treecols>
|
||||
<treecol id="success-col" style="width:20px;"/>
|
||||
<splitter class="tree-splitter" hidden="true"/>
|
||||
<treecol label="&zotero.recognizePDF.pdfName.label;" id="pdf-col" flex="1"/>
|
||||
<treecol id="col1" flex="1"/>
|
||||
<splitter class="tree-splitter"/>
|
||||
<treecol label="&zotero.recognizePDF.itemName.label;" id="item-col" flex="2"/>
|
||||
<treecol id="col2" flex="2"/>
|
||||
</treecols>
|
||||
<treechildren id="treechildren"/>
|
||||
</tree>
|
234
chrome/content/zotero/xpcom/progressQueue.js
Normal file
234
chrome/content/zotero/xpcom/progressQueue.js
Normal file
|
@ -0,0 +1,234 @@
|
|||
/*
|
||||
***** BEGIN LICENSE BLOCK *****
|
||||
|
||||
Copyright © 2018 Center for History and New Media
|
||||
George Mason University, Fairfax, Virginia, USA
|
||||
http://zotero.org
|
||||
|
||||
This file is part of Zotero.
|
||||
|
||||
Zotero is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
Zotero is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with Zotero. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
***** END LICENSE BLOCK *****
|
||||
*/
|
||||
|
||||
Zotero.ProgressQueue = function (options) {
|
||||
let _id = options.id;
|
||||
let _title = options.title;
|
||||
let _columns = options.columns;
|
||||
|
||||
let _listeners = {};
|
||||
let _rows = [];
|
||||
|
||||
let _dialog = null;
|
||||
|
||||
let _progressQueue = this;
|
||||
|
||||
/**
|
||||
* @return {Zotero.ProgressQueueDialog}
|
||||
*/
|
||||
this.getDialog = function() {
|
||||
if(!_dialog) {
|
||||
_dialog = new Zotero.ProgressQueueDialog(_progressQueue);
|
||||
}
|
||||
return _dialog;
|
||||
};
|
||||
|
||||
/**
|
||||
* @return {Number}
|
||||
*/
|
||||
this.getID = function () {
|
||||
return _id;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @return {String}
|
||||
*/
|
||||
this.getTitle = function () {
|
||||
return _title;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @return {String[]}
|
||||
*/
|
||||
this.getColumns = function () {
|
||||
return _columns;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Add listener
|
||||
* @param {String} name Event name
|
||||
* @param callback
|
||||
*/
|
||||
this.addListener = function (name, callback) {
|
||||
_listeners[name] = callback;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Remove listener
|
||||
* @param {String} name Event name
|
||||
*/
|
||||
this.removeListener = function (name) {
|
||||
delete _listeners[name];
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Returns all rows
|
||||
* @return {Object[]}
|
||||
*/
|
||||
this.getRows = function () {
|
||||
return _rows;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Returns rows count
|
||||
* @return {Number}
|
||||
*/
|
||||
this.getTotal = function () {
|
||||
return _rows.length;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Returns processed rows count
|
||||
* @return {Number}
|
||||
*/
|
||||
this.getProcessedTotal = function () {
|
||||
return _rows.filter(row => row.status > Zotero.ProgressQueue.ROW_PROCESSING).length;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Stop processing items
|
||||
*/
|
||||
this.cancel = function () {
|
||||
_rows = [];
|
||||
if (_listeners.empty) {
|
||||
_listeners.empty();
|
||||
}
|
||||
|
||||
if (_listeners.cancel) {
|
||||
_listeners.cancel();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Add item for processing
|
||||
* @param {Zotero.Item} item
|
||||
*/
|
||||
this.addRow = function(item) {
|
||||
this.deleteRow(item.id);
|
||||
|
||||
let row = {
|
||||
id: item.id,
|
||||
status: Zotero.ProgressQueue.ROW_QUEUED,
|
||||
fileName: item.getField('title'),
|
||||
message: ''
|
||||
};
|
||||
|
||||
_rows.push(row);
|
||||
|
||||
if (_listeners.rowadded) {
|
||||
_listeners.rowadded(row);
|
||||
}
|
||||
|
||||
if (_listeners.nonempty) {
|
||||
_listeners.nonempty();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Update row status and message
|
||||
* @param {Number} itemID
|
||||
* @param {Number} status
|
||||
* @param {String} message
|
||||
*/
|
||||
this.updateRow = function(itemID, status, message) {
|
||||
for (let row of _rows) {
|
||||
if (row.id === itemID) {
|
||||
row.status = status;
|
||||
row.message = message;
|
||||
if (_listeners.rowupdated) {
|
||||
_listeners.rowupdated({
|
||||
id: row.id,
|
||||
status,
|
||||
message: message || ''
|
||||
});
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Delete row
|
||||
* @param {Number} itemID
|
||||
*/
|
||||
this.deleteRow = function(itemID) {
|
||||
let row = _rows.find(x => x.id === itemID);
|
||||
if(row) {
|
||||
_rows.splice(_rows.indexOf(row), 1);
|
||||
if (_listeners.rowdeleted) {
|
||||
_listeners.rowdeleted({
|
||||
id: row.id
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
Zotero.ProgressQueue.ROW_QUEUED = 1;
|
||||
Zotero.ProgressQueue.ROW_PROCESSING = 2;
|
||||
Zotero.ProgressQueue.ROW_FAILED = 3;
|
||||
Zotero.ProgressQueue.ROW_SUCCEEDED = 4;
|
||||
|
||||
|
||||
Zotero.ProgressQueues = new function () {
|
||||
let _queues = [];
|
||||
|
||||
/**
|
||||
* @param {Object} options
|
||||
* @return {Zotero.ProgressQueue}
|
||||
*/
|
||||
this.create = function (options) {
|
||||
let queue = new Zotero.ProgressQueue(options);
|
||||
_queues.push(queue);
|
||||
return queue;
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {Number} id
|
||||
* @return {Zotero.ProgressQueue}
|
||||
*/
|
||||
this.get = function (id) {
|
||||
return _queues.find(queue => queue.getID() === id);
|
||||
};
|
||||
|
||||
/**
|
||||
* @return {Zotero.ProgressQueue[]}
|
||||
*/
|
||||
this.getAll = function () {
|
||||
return _queues;
|
||||
};
|
||||
};
|
|
@ -23,30 +23,33 @@
|
|||
***** END LICENSE BLOCK *****
|
||||
*/
|
||||
|
||||
/**
|
||||
* @fileOverview Tools for automatically retrieving a citation for the given PDF
|
||||
*/
|
||||
|
||||
/**
|
||||
* Front end for recognizing PDFs
|
||||
* @namespace
|
||||
*/
|
||||
|
||||
var Zotero_RecognizePDF_Dialog = new function () {
|
||||
Zotero.ProgressQueueDialog = function (progressQueue) {
|
||||
const SUCCESS_IMAGE = 'chrome://zotero/skin/tick.png';
|
||||
const FAILURE_IMAGE = 'chrome://zotero/skin/cross.png';
|
||||
const LOADING_IMAGE = 'chrome://zotero/skin/arrow_refresh.png';
|
||||
|
||||
let _progressQueue = this.progressQueue = progressQueue;
|
||||
|
||||
let _progressWindow = null;
|
||||
let _progressIndicator = null;
|
||||
let _rowIDs = [];
|
||||
|
||||
this.open = function() {
|
||||
this.open = function () {
|
||||
if (_progressWindow) {
|
||||
_progressWindow.focus();
|
||||
return;
|
||||
}
|
||||
_progressWindow = window.openDialog('chrome://zotero/content/recognizePDFDialog.xul', '', 'chrome,close=yes,resizable=yes,dependent,dialog,centerscreen');
|
||||
|
||||
let win = Services.wm.getMostRecentWindow("navigator:browser");
|
||||
if (win) {
|
||||
_progressWindow = win.openDialog("chrome://zotero/content/progressQueueDialog.xul",
|
||||
"", "chrome,close=yes,resizable=yes,dependent,dialog,centerscreen");
|
||||
}
|
||||
else {
|
||||
_progressWindow = Services.ww.openWindow(null, "chrome://zotero/content/progressQueueDialog.xul",
|
||||
"", "chrome,close=yes,resizable=yes,dependent,dialog,centerscreen", null);
|
||||
}
|
||||
|
||||
_progressWindow.addEventListener('pageshow', _onWindowLoaded.bind(this), false);
|
||||
};
|
||||
|
||||
|
@ -55,13 +58,13 @@ var Zotero_RecognizePDF_Dialog = new function () {
|
|||
}
|
||||
|
||||
function _getImageByStatus(status) {
|
||||
if (status === Zotero.RecognizePDF.ROW_PROCESSING) {
|
||||
if (status === Zotero.ProgressQueue.ROW_PROCESSING) {
|
||||
return LOADING_IMAGE;
|
||||
}
|
||||
else if (status === Zotero.RecognizePDF.ROW_FAILED) {
|
||||
else if (status === Zotero.ProgressQueue.ROW_FAILED) {
|
||||
return FAILURE_IMAGE;
|
||||
}
|
||||
else if (status === Zotero.RecognizePDF.ROW_SUCCEEDED) {
|
||||
else if (status === Zotero.ProgressQueue.ROW_SUCCEEDED) {
|
||||
return SUCCESS_IMAGE;
|
||||
}
|
||||
return '';
|
||||
|
@ -93,8 +96,18 @@ var Zotero_RecognizePDF_Dialog = new function () {
|
|||
}
|
||||
|
||||
function _onWindowLoaded() {
|
||||
let rows = Zotero.RecognizePDF.getRows();
|
||||
let rows = _progressQueue.getRows();
|
||||
_rowIDs = [];
|
||||
|
||||
_progressWindow.document.title = Zotero.getString(_progressQueue.getTitle());
|
||||
|
||||
let col1 = _progressWindow.document.getElementById('col1');
|
||||
let col2 = _progressWindow.document.getElementById('col2');
|
||||
|
||||
let columns = _progressQueue.getColumns();
|
||||
col1.setAttribute('label', Zotero.getString(columns[0]));
|
||||
col2.setAttribute('label', Zotero.getString(columns[1]));
|
||||
|
||||
let treechildren = _progressWindow.document.getElementById('treechildren');
|
||||
|
||||
for (let row of rows) {
|
||||
|
@ -113,7 +126,7 @@ var Zotero_RecognizePDF_Dialog = new function () {
|
|||
_progressWindow.document.getElementById('cancel-button')
|
||||
.addEventListener('command', function () {
|
||||
close();
|
||||
Zotero.RecognizePDF.cancel();
|
||||
_progressQueue.cancel();
|
||||
}, false);
|
||||
|
||||
_progressWindow.document.getElementById('minimize-button')
|
||||
|
@ -124,23 +137,23 @@ var Zotero_RecognizePDF_Dialog = new function () {
|
|||
_progressWindow.document.getElementById('close-button')
|
||||
.addEventListener('command', function () {
|
||||
close();
|
||||
Zotero.RecognizePDF.cancel();
|
||||
_progressQueue.cancel();
|
||||
}, false);
|
||||
|
||||
_progressWindow.addEventListener('keypress', function (e) {
|
||||
if (e.keyCode === KeyEvent.DOM_VK_ESCAPE) {
|
||||
if (e.keyCode === _progressWindow.KeyEvent.DOM_VK_ESCAPE) {
|
||||
// If done processing, Esc is equivalent to Close rather than Minimize
|
||||
if (Zotero.RecognizePDF.getTotal() == Zotero.RecognizePDF.getProcessedTotal()) {
|
||||
Zotero.RecognizePDF.cancel();
|
||||
if (_progressQueue.getTotal() === _progressQueue.getProcessedTotal()) {
|
||||
_progressQueue.cancel();
|
||||
}
|
||||
close();
|
||||
}
|
||||
});
|
||||
|
||||
_progressWindow.addEventListener('unload', function () {
|
||||
Zotero.RecognizePDF.removeListener('rowadded');
|
||||
Zotero.RecognizePDF.removeListener('rowupdated');
|
||||
Zotero.RecognizePDF.removeListener('rowdeleted');
|
||||
_progressQueue.removeListener('rowadded');
|
||||
_progressQueue.removeListener('rowupdated');
|
||||
_progressQueue.removeListener('rowdeleted');
|
||||
_progressWindow = null;
|
||||
_progressIndicator = null;
|
||||
_rowIDs = [];
|
||||
|
@ -148,14 +161,14 @@ var Zotero_RecognizePDF_Dialog = new function () {
|
|||
|
||||
_updateProgress();
|
||||
|
||||
Zotero.RecognizePDF.addListener('rowadded', function (row) {
|
||||
_progressQueue.addListener('rowadded', function (row) {
|
||||
_rowIDs.push(row.id);
|
||||
let treeitem = _rowToTreeItem(row);
|
||||
treechildren.appendChild(treeitem);
|
||||
_updateProgress();
|
||||
});
|
||||
|
||||
Zotero.RecognizePDF.addListener('rowupdated', function (row) {
|
||||
_progressQueue.addListener('rowupdated', function (row) {
|
||||
let itemIcon = _progressWindow.document.getElementById('item-' + row.id + '-icon');
|
||||
let itemTitle = _progressWindow.document.getElementById('item-' + row.id + '-title');
|
||||
|
||||
|
@ -164,7 +177,7 @@ var Zotero_RecognizePDF_Dialog = new function () {
|
|||
_updateProgress();
|
||||
});
|
||||
|
||||
Zotero.RecognizePDF.addListener('rowdeleted', function (row) {
|
||||
_progressQueue.addListener('rowdeleted', function (row) {
|
||||
_rowIDs.splice(_rowIDs.indexOf(row.id), 1);
|
||||
let treeitem = _progressWindow.document.getElementById('item-' + row.id);
|
||||
treeitem.parentNode.removeChild(treeitem);
|
||||
|
@ -174,20 +187,20 @@ var Zotero_RecognizePDF_Dialog = new function () {
|
|||
|
||||
function _updateProgress() {
|
||||
if (!_progressWindow) return;
|
||||
let total = Zotero.RecognizePDF.getTotal();
|
||||
let processed = Zotero.RecognizePDF.getProcessedTotal();
|
||||
let total = _progressQueue.getTotal();
|
||||
let processed = _progressQueue.getProcessedTotal();
|
||||
_progressIndicator.value = processed * 100 / total;
|
||||
if (processed === total) {
|
||||
_progressWindow.document.getElementById("cancel-button").hidden = true;
|
||||
_progressWindow.document.getElementById("minimize-button").hidden = true;
|
||||
_progressWindow.document.getElementById("close-button").hidden = false;
|
||||
_progressWindow.document.getElementById("label").value = Zotero.getString('recognizePDF.complete.label');
|
||||
_progressWindow.document.getElementById("label").value = Zotero.getString('general.finished');
|
||||
}
|
||||
else {
|
||||
_progressWindow.document.getElementById("cancel-button").hidden = false;
|
||||
_progressWindow.document.getElementById("minimize-button").hidden = false;
|
||||
_progressWindow.document.getElementById("close-button").hidden = true;
|
||||
_progressWindow.document.getElementById("label").value = Zotero.getString('recognizePDF.recognizing.label');
|
||||
_progressWindow.document.getElementById("label").value = Zotero.getString('general.processing');
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -208,12 +221,15 @@ var Zotero_RecognizePDF_Dialog = new function () {
|
|||
|
||||
if (item.parentItemID) itemID = item.parentItemID;
|
||||
|
||||
if (window.ZoteroOverlay) {
|
||||
window.ZoteroOverlay.toggleDisplay(true);
|
||||
let win = Services.wm.getMostRecentWindow("navigator:browser");
|
||||
if (win) {
|
||||
if (win.ZoteroOverlay) {
|
||||
win.ZoteroOverlay.toggleDisplay(true);
|
||||
}
|
||||
|
||||
win.ZoteroPane.selectItem(itemID, false, true);
|
||||
win.focus();
|
||||
}
|
||||
|
||||
window.ZoteroPane.selectItem(itemID, false, true);
|
||||
window.focus();
|
||||
}
|
||||
}
|
||||
};
|
|
@ -28,35 +28,99 @@ Zotero.RecognizePDF = new function () {
|
|||
const MAX_PAGES = 5;
|
||||
const UNRECOGNIZE_TIMEOUT = 86400 * 1000;
|
||||
|
||||
this.ROW_QUEUED = 1;
|
||||
this.ROW_PROCESSING = 2;
|
||||
this.ROW_FAILED = 3;
|
||||
this.ROW_SUCCEEDED = 4;
|
||||
|
||||
let _newItems = new WeakMap();
|
||||
|
||||
let _listeners = {};
|
||||
let _rows = [];
|
||||
let _queue = [];
|
||||
let _queueProcessing = false;
|
||||
let _processingItemID = null;
|
||||
|
||||
let _progressQueue = Zotero.ProgressQueues.create({
|
||||
id: 'recognize',
|
||||
title: 'recognizePDF.title',
|
||||
columns: [
|
||||
'recognizePDF.pdfName.label',
|
||||
'recognizePDF.itemName.label'
|
||||
]
|
||||
});
|
||||
|
||||
_progressQueue.addListener('cancel', function () {
|
||||
_queue = [];
|
||||
});
|
||||
|
||||
|
||||
/**
|
||||
* Add listener
|
||||
* @param name Event name
|
||||
* @param callback
|
||||
* Triggers queue processing and returns when all items in the queue are processed
|
||||
* @return {Promise}
|
||||
*/
|
||||
this.addListener = function (name, callback) {
|
||||
_listeners[name] = callback;
|
||||
};
|
||||
async function _processQueue() {
|
||||
await Zotero.Schema.schemaUpdatePromise;
|
||||
|
||||
if (_queueProcessing) return;
|
||||
_queueProcessing = true;
|
||||
|
||||
while (1) {
|
||||
// While all current progress queue usages are related with
|
||||
// online APIs, check internet connectivity here
|
||||
if (Zotero.HTTP.browserIsOffline()) {
|
||||
await Zotero.Promise.delay(OFFLINE_RECHECK_DELAY);
|
||||
continue;
|
||||
}
|
||||
|
||||
let itemID = _queue.pop();
|
||||
if (!itemID) break;
|
||||
|
||||
_processingItemID = itemID;
|
||||
|
||||
_progressQueue.updateRow(itemID, Zotero.ProgressQueue.ROW_PROCESSING, Zotero.getString('general.processing'));
|
||||
|
||||
try {
|
||||
let item = await Zotero.Items.getAsync(itemID);
|
||||
|
||||
if (!item) {
|
||||
throw new Error();
|
||||
}
|
||||
|
||||
let res = await _processItem(item);
|
||||
_progressQueue.updateRow(itemID, Zotero.ProgressQueue.ROW_SUCCEEDED, item.getField('title'));
|
||||
}
|
||||
catch (e) {
|
||||
Zotero.logError(e);
|
||||
|
||||
_progressQueue.updateRow(
|
||||
itemID,
|
||||
Zotero.ProgressQueue.ROW_FAILED,
|
||||
e instanceof Zotero.Exception.Alert
|
||||
? e.message
|
||||
: Zotero.getString('general.error')
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
_queueProcessing = false;
|
||||
_processingItemID = null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Remove listener
|
||||
* @param name Event name
|
||||
* Adds items to the queue and triggers processing
|
||||
* @param {Zotero.Item[]} items
|
||||
*/
|
||||
this.removeListener = function (name) {
|
||||
delete _listeners[name];
|
||||
this.recognizeItems = function (items) {
|
||||
for (let item of items) {
|
||||
if(
|
||||
_processingItemID === item.id ||
|
||||
_queue.includes(item.id) ||
|
||||
!this.canRecognize(item)
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
_queue.unshift(item.id);
|
||||
_progressQueue.addRow(item);
|
||||
}
|
||||
_processQueue();
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Checks whether a given PDF could theoretically be recognized
|
||||
* @param {Zotero.Item} item
|
||||
|
@ -68,17 +132,6 @@ Zotero.RecognizePDF = new function () {
|
|||
&& item.isTopLevelItem();
|
||||
};
|
||||
|
||||
/**
|
||||
* Adds items to the queue and starts processing it
|
||||
* @param items {Zotero.Item}
|
||||
*/
|
||||
this.recognizeItems = function (items) {
|
||||
for (let item of items) {
|
||||
_addItem(item);
|
||||
}
|
||||
_processQueue();
|
||||
};
|
||||
|
||||
|
||||
this.autoRecognizeItems = function (items) {
|
||||
if (!Zotero.Prefs.get('autoRecognizeFiles')) return;
|
||||
|
@ -92,45 +145,7 @@ Zotero.RecognizePDF = new function () {
|
|||
return;
|
||||
}
|
||||
this.recognizeItems(pdfs);
|
||||
var win = Services.wm.getMostRecentWindow("navigator:browser");
|
||||
if (win) {
|
||||
win.Zotero_RecognizePDF_Dialog.open();
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns all rows
|
||||
* @return {Array}
|
||||
*/
|
||||
this.getRows = function () {
|
||||
return _rows;
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns rows count
|
||||
* @return {Number}
|
||||
*/
|
||||
this.getTotal = function () {
|
||||
return _rows.length;
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns processed rows count
|
||||
* @return {Number}
|
||||
*/
|
||||
this.getProcessedTotal = function () {
|
||||
return _rows.filter(row => row.status > Zotero.RecognizePDF.ROW_PROCESSING).length;
|
||||
};
|
||||
|
||||
/**
|
||||
* Stop processing items
|
||||
*/
|
||||
this.cancel = function () {
|
||||
_queue = [];
|
||||
_rows = [];
|
||||
if (_listeners['empty']) {
|
||||
_listeners['empty']();
|
||||
}
|
||||
Zotero.ProgressQueues.get('recognize').getDialog().open();
|
||||
};
|
||||
|
||||
|
||||
|
@ -213,140 +228,15 @@ Zotero.RecognizePDF = new function () {
|
|||
};
|
||||
|
||||
|
||||
/**
|
||||
* Add item for processing
|
||||
* @param item
|
||||
* @return {null}
|
||||
*/
|
||||
function _addItem(item) {
|
||||
for (let row of _rows) {
|
||||
if (row.id === item.id) {
|
||||
if (row.status > Zotero.RecognizePDF.ROW_PROCESSING) {
|
||||
_deleteRow(row.id);
|
||||
break;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
let row = {
|
||||
id: item.id,
|
||||
status: Zotero.RecognizePDF.ROW_QUEUED,
|
||||
fileName: item.getField('title'),
|
||||
message: ''
|
||||
};
|
||||
|
||||
_rows.unshift(row);
|
||||
_queue.unshift(item.id);
|
||||
|
||||
if (_listeners['rowadded']) {
|
||||
_listeners['rowadded'](row);
|
||||
}
|
||||
|
||||
if (_listeners['nonempty'] && _rows.length === 1) {
|
||||
_listeners['nonempty']();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update row status and message
|
||||
* @param itemID
|
||||
* @param status
|
||||
* @param message
|
||||
*/
|
||||
function _updateRow(itemID, status, message) {
|
||||
for (let row of _rows) {
|
||||
if (row.id === itemID) {
|
||||
row.status = status;
|
||||
row.message = message;
|
||||
if (_listeners['rowupdated']) {
|
||||
_listeners['rowupdated']({
|
||||
id: row.id,
|
||||
status,
|
||||
message: message || ''
|
||||
});
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete row
|
||||
* @param itemID
|
||||
*/
|
||||
function _deleteRow(itemID) {
|
||||
for (let i = 0; i < _rows.length; i++) {
|
||||
let row = _rows[i];
|
||||
if (row.id === itemID) {
|
||||
_rows.splice(i, 1);
|
||||
if (_listeners['rowdeleted']) {
|
||||
_listeners['rowdeleted']({
|
||||
id: row.id
|
||||
});
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Triggers queue processing and returns when all items in the queue are processed
|
||||
* @return {Promise}
|
||||
*/
|
||||
async function _processQueue() {
|
||||
await Zotero.Schema.schemaUpdatePromise;
|
||||
|
||||
if (_queueProcessing) return;
|
||||
_queueProcessing = true;
|
||||
|
||||
while (1) {
|
||||
if (Zotero.HTTP.browserIsOffline()) {
|
||||
await Zotero.Promise.delay(OFFLINE_RECHECK_DELAY);
|
||||
continue;
|
||||
}
|
||||
|
||||
let itemID = _queue.shift();
|
||||
if (!itemID) break;
|
||||
|
||||
_updateRow(itemID, Zotero.RecognizePDF.ROW_PROCESSING, Zotero.getString('general.processing'));
|
||||
|
||||
try {
|
||||
let newItem = await _processItem(itemID);
|
||||
|
||||
if (newItem) {
|
||||
_updateRow(itemID, Zotero.RecognizePDF.ROW_SUCCEEDED, newItem.getField('title'));
|
||||
}
|
||||
else {
|
||||
_updateRow(itemID, Zotero.RecognizePDF.ROW_FAILED, Zotero.getString('recognizePDF.noMatches'));
|
||||
}
|
||||
}
|
||||
catch (e) {
|
||||
Zotero.logError(e);
|
||||
|
||||
_updateRow(
|
||||
itemID,
|
||||
Zotero.RecognizePDF.ROW_FAILED,
|
||||
e instanceof Zotero.Exception.Alert
|
||||
? e.message
|
||||
: Zotero.getString('recognizePDF.error')
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
_queueProcessing = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes the item and places it as a children of the new item
|
||||
* @param itemID
|
||||
* @return {Promise}
|
||||
*/
|
||||
async function _processItem(itemID) {
|
||||
let attachment = await Zotero.Items.getAsync(itemID);
|
||||
|
||||
if (!attachment || attachment.parentItemID) {
|
||||
throw new Zotero.Exception.Alert('recognizePDF.error');
|
||||
async function _processItem(attachment) {
|
||||
// Make sure the attachment still doesn't have a parent
|
||||
if (attachment.parentItemID) {
|
||||
throw new Error('Already has parent');
|
||||
}
|
||||
|
||||
var zp = Zotero.getActiveZoteroPane();
|
||||
|
@ -361,7 +251,7 @@ Zotero.RecognizePDF = new function () {
|
|||
|
||||
let parentItem = await _recognize(attachment);
|
||||
if (!parentItem) {
|
||||
return null;
|
||||
throw new Zotero.Exception.Alert("recognizePDF.noMatches");
|
||||
}
|
||||
|
||||
// Put new item in same collections as the old one
|
||||
|
@ -450,7 +340,8 @@ Zotero.RecognizePDF = new function () {
|
|||
Zotero.logError(e);
|
||||
try {
|
||||
cacheFile.remove(false);
|
||||
} catch(e) {
|
||||
}
|
||||
catch (e) {
|
||||
Zotero.logError(e);
|
||||
}
|
||||
throw new Zotero.Exception.Alert("recognizePDF.couldNotRead");
|
||||
|
|
|
@ -74,14 +74,27 @@ var ZoteroPane = new function()
|
|||
// Set key down handler
|
||||
document.getElementById('appcontent').addEventListener('keydown', ZoteroPane_Local.handleKeyDown, true);
|
||||
|
||||
// Hide or show the PDF recognizer button
|
||||
Zotero.RecognizePDF.addListener('empty', function (row) {
|
||||
document.getElementById('zotero-tb-recognize').hidden = true;
|
||||
});
|
||||
|
||||
Zotero.RecognizePDF.addListener('nonempty', function (row) {
|
||||
document.getElementById('zotero-tb-recognize').hidden = false;
|
||||
});
|
||||
// Init toolbar buttons for all progress queues
|
||||
let progressQueueButtons = document.getElementById('zotero-pq-buttons');
|
||||
let progressQueues = Zotero.ProgressQueues.getAll();
|
||||
for (let progressQueue of progressQueues) {
|
||||
let button = document.createElement('toolbarbutton');
|
||||
button.id = 'zotero-tb-pq-' + progressQueue.getID();
|
||||
button.hidden = progressQueue.getTotal() < 1;
|
||||
button.addEventListener('command', function () {
|
||||
Zotero.ProgressQueues.get(progressQueue.getID()).getDialog().open();
|
||||
}, false);
|
||||
|
||||
progressQueue.addListener('empty', function () {
|
||||
button.hidden = true;
|
||||
});
|
||||
|
||||
progressQueue.addListener('nonempty', function () {
|
||||
button.hidden = false;
|
||||
});
|
||||
|
||||
progressQueueButtons.appendChild(button);
|
||||
}
|
||||
|
||||
_loaded = true;
|
||||
|
||||
|
@ -4468,7 +4481,7 @@ var ZoteroPane = new function()
|
|||
|
||||
this.recognizeSelected = function() {
|
||||
Zotero.RecognizePDF.recognizeItems(ZoteroPane.getSelectedItems());
|
||||
Zotero_RecognizePDF_Dialog.open();
|
||||
Zotero.ProgressQueues.get('recognize').getDialog().open();
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -42,7 +42,7 @@
|
|||
<script src="fileInterface.js"/>
|
||||
<script src="reportInterface.js"/>
|
||||
<script src="timelineInterface.js"/>
|
||||
<script src="recognizePDFDialog.js"/>
|
||||
<script src="progressQueueDialog.js"/>
|
||||
<script src="lookup.js"/>
|
||||
<script src="locateMenu.js" type="application/javascript;version=1.8"/>
|
||||
|
||||
|
@ -185,8 +185,8 @@
|
|||
</hbox>
|
||||
</hbox>
|
||||
|
||||
<toolbarbutton id="zotero-tb-recognize" hidden="true"
|
||||
oncommand="Zotero_RecognizePDF_Dialog.open()"/>
|
||||
<hbox id="zotero-pq-buttons">
|
||||
</hbox>
|
||||
|
||||
<toolbarbutton id="zotero-tb-sync-error" hidden="true"/>
|
||||
|
||||
|
|
|
@ -288,9 +288,6 @@
|
|||
<!ENTITY zotero.feedSettings.cleanupReadAfter.label1 "Remove read feed items after">
|
||||
<!ENTITY zotero.feedSettings.cleanupReadAfter.label2 "day(s)">
|
||||
|
||||
<!ENTITY zotero.recognizePDF.pdfName.label "PDF Name">
|
||||
<!ENTITY zotero.recognizePDF.itemName.label "Item Name">
|
||||
|
||||
<!ENTITY zotero.rtfScan.title "RTF Scan">
|
||||
<!ENTITY zotero.rtfScan.cancel.label "Cancel">
|
||||
<!ENTITY zotero.rtfScan.citation.label "Citation">
|
||||
|
|
|
@ -66,6 +66,7 @@ general.copyToClipboard = Copy to Clipboard
|
|||
general.cancel = Cancel
|
||||
general.clear = Clear
|
||||
general.processing = Processing
|
||||
general.finished = Finished
|
||||
general.submitted = Submitted
|
||||
general.thanksForHelpingImprove = Thanks for helping to improve %S!
|
||||
general.describeProblem = Briefly describe the problem:
|
||||
|
@ -1069,6 +1070,7 @@ proxies.notification.settings.button = Proxy Settings…
|
|||
proxies.recognized.message = Adding this proxy will allow Zotero to recognize items from its pages and will automatically redirect future requests to %1$S through %2$S.
|
||||
proxies.recognized.add = Add Proxy
|
||||
|
||||
recognizePDF.title = PDF Metadata Retrieval
|
||||
recognizePDF.noOCR = PDF does not contain OCRed text
|
||||
recognizePDF.couldNotRead = Could not read text from PDF
|
||||
recognizePDF.noMatches = No matching references found
|
||||
|
@ -1077,6 +1079,8 @@ recognizePDF.error = An unexpected error occurred
|
|||
recognizePDF.recognizing.label = Retrieving Metadata…
|
||||
recognizePDF.complete.label = Metadata Retrieval Complete
|
||||
recognizePDF.reportMetadata = Report Incorrect Metadata
|
||||
recognizePDF.pdfName.label = PDF Name
|
||||
recognizePDF.itemName.label = Item Name
|
||||
|
||||
rtfScan.openTitle = Select a file to scan
|
||||
rtfScan.scanning.label = Scanning RTF Document…
|
||||
|
|
|
@ -608,11 +608,11 @@
|
|||
text-align: right;
|
||||
}
|
||||
|
||||
#zotero-tb-recognize {
|
||||
#zotero-tb-pq-recognize {
|
||||
list-style-image: url(chrome://zotero/skin/pdf-search.png);
|
||||
}
|
||||
|
||||
#zotero-tb-recognize .toolbarbutton-icon {
|
||||
#zotero-tb-pq-recognize .toolbarbutton-icon {
|
||||
width: 18px;
|
||||
margin-top: 1px;
|
||||
}
|
||||
|
@ -764,7 +764,7 @@
|
|||
#zotero-tb-advanced-search { list-style-image: url('chrome://zotero/skin/toolbar-advanced-search@2x.png'); }
|
||||
#zotero-tb-locate { list-style-image: url('chrome://zotero/skin/toolbar-go-arrow@2x.png'); }
|
||||
#zotero-tb-sync-stop { list-style-image: url(chrome://zotero/skin/control_stop_blue@2x.png); }
|
||||
#zotero-tb-recognize { list-style-image: url(chrome://zotero/skin/pdf-search@2x.png); }
|
||||
#zotero-tb-pq-recognize { list-style-image: url(chrome://zotero/skin/pdf-search@2x.png); }
|
||||
#zotero-tb-sync-error { list-style-image: url(chrome://zotero/skin/error@2x.png); }
|
||||
#zotero-tb-sync-error[state=warning] { list-style-image: url(chrome://zotero/skin/warning@2x.png); }
|
||||
#zotero-tb-sync { list-style-image: url(chrome://zotero/skin/arrow_rotate_static@2x.png); }
|
||||
|
|
|
@ -105,6 +105,8 @@ const xpcomFilesLocal = [
|
|||
'mime',
|
||||
'notifier',
|
||||
'openPDF',
|
||||
'progressQueue',
|
||||
'progressQueueDialog',
|
||||
'quickCopy',
|
||||
'recognizePDF',
|
||||
'report',
|
||||
|
|
|
@ -20,9 +20,9 @@ function waitForDOMEvent(target, event, capture) {
|
|||
}
|
||||
|
||||
async function waitForRecognizer() {
|
||||
var win = await waitForWindow('chrome://zotero/content/recognizePDFDialog.xul')
|
||||
var win = await waitForWindow('chrome://zotero/content/progressQueueDialog.xul')
|
||||
// Wait for status to show as complete
|
||||
var completeStr = Zotero.getString("recognizePDF.complete.label");
|
||||
var completeStr = Zotero.getString("general.finished");
|
||||
while (win.document.getElementById("label").value != completeStr) {
|
||||
await Zotero.Promise.delay(20);
|
||||
}
|
||||
|
|
|
@ -932,7 +932,7 @@ describe("Zotero.ItemTreeView", function() {
|
|||
|
||||
var progressWindow = await recognizerPromise;
|
||||
progressWindow.close();
|
||||
Zotero.RecognizePDF.cancel();
|
||||
Zotero.ProgressQueues.get('recognize').cancel();
|
||||
assert.isFalse(item.isTopLevelItem());
|
||||
|
||||
Zotero.HTTP.mock = null;
|
||||
|
|
|
@ -16,10 +16,10 @@ describe("PDF Recognition", function() {
|
|||
});
|
||||
|
||||
afterEach(function() {
|
||||
for(let win of getWindows("chrome://zotero/content/recognizePDFDialog.xul")) {
|
||||
for(let win of getWindows("chrome://zotero/content/progressQueueDialog.xul")) {
|
||||
win.close();
|
||||
}
|
||||
Zotero.RecognizePDF.cancel();
|
||||
Zotero.ProgressQueues.get('recognize').cancel();
|
||||
});
|
||||
|
||||
after(function() {
|
||||
|
@ -50,8 +50,8 @@ describe("PDF Recognition", function() {
|
|||
assert.lengthOf(modifiedIDs, 2);
|
||||
|
||||
// Wait for status to show as complete
|
||||
var progressWindow = getWindows("chrome://zotero/content/recognizePDFDialog.xul")[0];
|
||||
var completeStr = Zotero.getString("recognizePDF.complete.label");
|
||||
var progressWindow = getWindows("chrome://zotero/content/progressQueueDialog.xul")[0];
|
||||
var completeStr = Zotero.getString("general.finished");
|
||||
while (progressWindow.document.getElementById("label").value != completeStr) {
|
||||
await Zotero.Promise.delay(20);
|
||||
}
|
||||
|
@ -84,8 +84,8 @@ describe("PDF Recognition", function() {
|
|||
assert.lengthOf(modifiedIDs, 1);
|
||||
|
||||
// Wait for status to show as complete
|
||||
var progressWindow = getWindows("chrome://zotero/content/recognizePDFDialog.xul")[0];
|
||||
var completeStr = Zotero.getString("recognizePDF.complete.label");
|
||||
var progressWindow = getWindows("chrome://zotero/content/progressQueueDialog.xul")[0];
|
||||
var completeStr = Zotero.getString("general.finished");
|
||||
while (progressWindow.document.getElementById("label").value != completeStr) {
|
||||
await Zotero.Promise.delay(20);
|
||||
}
|
||||
|
@ -118,8 +118,8 @@ describe("PDF Recognition", function() {
|
|||
assert.lengthOf(modifiedIDs, 1);
|
||||
|
||||
// Wait for status to show as complete
|
||||
var progressWindow = getWindows("chrome://zotero/content/recognizePDFDialog.xul")[0];
|
||||
var completeStr = Zotero.getString("recognizePDF.complete.label");
|
||||
var progressWindow = getWindows("chrome://zotero/content/progressQueueDialog.xul")[0];
|
||||
var completeStr = Zotero.getString("general.finished");
|
||||
while (progressWindow.document.getElementById("label").value != completeStr) {
|
||||
await Zotero.Promise.delay(20);
|
||||
}
|
||||
|
@ -149,8 +149,8 @@ describe("PDF Recognition", function() {
|
|||
assert.lengthOf(modifiedIDs, 1);
|
||||
|
||||
// Wait for status to show as complete
|
||||
var progressWindow = getWindows("chrome://zotero/content/recognizePDFDialog.xul")[0];
|
||||
var completeStr = Zotero.getString("recognizePDF.complete.label");
|
||||
var progressWindow = getWindows("chrome://zotero/content/progressQueueDialog.xul")[0];
|
||||
var completeStr = Zotero.getString("general.finished");
|
||||
while (progressWindow.document.getElementById("label").value != completeStr) {
|
||||
await Zotero.Promise.delay(20);
|
||||
}
|
||||
|
@ -179,8 +179,8 @@ describe("PDF Recognition", function() {
|
|||
assert.lengthOf(modifiedIDs, 2);
|
||||
|
||||
// Wait for status to show as complete
|
||||
var progressWindow = getWindows("chrome://zotero/content/recognizePDFDialog.xul")[0];
|
||||
var completeStr = Zotero.getString("recognizePDF.complete.label");
|
||||
var progressWindow = getWindows("chrome://zotero/content/progressQueueDialog.xul")[0];
|
||||
var completeStr = Zotero.getString("general.finished");
|
||||
while (progressWindow.document.getElementById("label").value != completeStr) {
|
||||
await Zotero.Promise.delay(20);
|
||||
}
|
||||
|
@ -209,8 +209,8 @@ describe("PDF Recognition", function() {
|
|||
assert.lengthOf(modifiedIDs, 2);
|
||||
|
||||
// Wait for status to show as complete
|
||||
var progressWindow = getWindows("chrome://zotero/content/recognizePDFDialog.xul")[0];
|
||||
var completeStr = Zotero.getString("recognizePDF.complete.label");
|
||||
var progressWindow = getWindows("chrome://zotero/content/progressQueueDialog.xul")[0];
|
||||
var completeStr = Zotero.getString("general.finished");
|
||||
while (progressWindow.document.getElementById("label").value != completeStr) {
|
||||
await Zotero.Promise.delay(20);
|
||||
}
|
||||
|
|
|
@ -843,7 +843,7 @@ describe("Connector Server", function () {
|
|||
|
||||
var progressWindow = await recognizerPromise;
|
||||
progressWindow.close();
|
||||
Zotero.RecognizePDF.cancel();
|
||||
Zotero.ProgressQueues.get('recognize').cancel();
|
||||
assert.isFalse(item.isTopLevelItem());
|
||||
|
||||
stub.restore();
|
||||
|
|
Loading…
Reference in a new issue