2011-03-07 17:47:01 +00:00
|
|
|
/*
|
|
|
|
***** BEGIN LICENSE BLOCK *****
|
|
|
|
|
|
|
|
Copyright © 2011 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
|
2011-05-18 18:34:22 +00:00
|
|
|
it under the terms of the GNU Affero General Public License as published by
|
2011-03-07 17:47:01 +00:00
|
|
|
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
|
2011-05-18 18:34:22 +00:00
|
|
|
GNU Affero General Public License for more details.
|
2011-03-07 17:47:01 +00:00
|
|
|
|
2011-05-18 18:34:22 +00:00
|
|
|
You should have received a copy of the GNU Affero General Public License
|
2011-03-07 17:47:01 +00:00
|
|
|
along with Zotero. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
***** END LICENSE BLOCK *****
|
|
|
|
*/
|
|
|
|
var Zotero_DownloadOverlay = new function() {
|
2011-03-15 18:50:48 +00:00
|
|
|
const PDF_MIME_TYPE = "application/pdf";
|
|
|
|
const ALLOW_LIST = [
|
|
|
|
"application/pdf",
|
|
|
|
"application/postscript",
|
|
|
|
"application/xhtml+xml",
|
|
|
|
"text/html",
|
|
|
|
"text/plain",
|
|
|
|
/^audio\//,
|
|
|
|
/^image\//,
|
|
|
|
/^video\//,
|
|
|
|
/^application\/vnd\.oasis\.opendocument\./,
|
|
|
|
/^application\/vnd\.ms-/,
|
|
|
|
/^application\/vnd\.openxmlformats-officedocument/,
|
|
|
|
/^application\/vnd\.lotus-/,
|
|
|
|
/^application\/vnd\.wolfram\./,
|
|
|
|
"application/vnd.wordperfect",
|
|
|
|
"application/wordperfect5.1",
|
|
|
|
"application/msword",
|
|
|
|
"application/x-latex"
|
|
|
|
];
|
|
|
|
|
2011-03-07 17:47:01 +00:00
|
|
|
/**
|
|
|
|
* Saves this item, if we are supposed to save it.
|
|
|
|
*
|
|
|
|
* @return {Boolean} True if an item was saved, false if we were not supposed to save
|
|
|
|
*/
|
2015-11-10 20:52:09 +00:00
|
|
|
this.handleSave = Zotero.Promise.coroutine(function* () {
|
2011-03-07 17:47:01 +00:00
|
|
|
if(!document.getElementById('zotero-radio').selected) return false;
|
|
|
|
|
2011-03-15 18:50:48 +00:00
|
|
|
var retrieveMetadata = document.getElementById('zotero-recognizePDF').selected;
|
|
|
|
|
2011-03-07 17:47:01 +00:00
|
|
|
var url = dialog.mLauncher.source.spec;
|
2011-03-15 18:50:48 +00:00
|
|
|
Zotero.debug("Zotero_DownloadOverlay: Downloading from "+url);
|
2011-03-07 17:47:01 +00:00
|
|
|
|
|
|
|
// set up progress window
|
|
|
|
var win = Components.classes["@mozilla.org/appshell/window-mediator;1"]
|
|
|
|
.getService(Components.interfaces.nsIWindowMediator)
|
|
|
|
.getMostRecentWindow("navigator:browser");
|
2011-08-26 21:49:47 +00:00
|
|
|
var libraryID, collection;
|
|
|
|
try {
|
2015-03-16 19:17:45 +00:00
|
|
|
let itemGroup = win.ZoteroPane.getCollectionTreeRow();
|
2015-12-10 06:09:40 +00:00
|
|
|
if (itemGroup.filesEditable && !itemGroup.isPublications()) {
|
2011-08-26 21:49:47 +00:00
|
|
|
libraryID = win.ZoteroPane.getSelectedLibraryID();
|
|
|
|
collection = win.ZoteroPane.getSelectedCollection();
|
|
|
|
}
|
2014-10-10 21:35:07 +00:00
|
|
|
// TODO: Just show an error instead?
|
|
|
|
else {
|
2014-10-10 22:01:21 +00:00
|
|
|
Zotero.debug("Cannot save files to library " + itemGroup.ref.libraryID
|
2015-12-10 06:09:40 +00:00
|
|
|
+ " -- saving to My Library instead", 2);
|
2015-11-10 20:52:09 +00:00
|
|
|
libraryID = Zotero.Libraries.userLibraryID;
|
2014-10-10 21:35:07 +00:00
|
|
|
}
|
|
|
|
} catch(e) {
|
|
|
|
Zotero.debug(e, 1);
|
|
|
|
};
|
2011-03-07 17:47:01 +00:00
|
|
|
|
2011-03-15 18:50:48 +00:00
|
|
|
var recognizePDF = document.getElementById('zotero-recognizePDF').checked
|
2011-08-26 22:27:02 +00:00
|
|
|
&& !document.getElementById('zotero-recognizePDF').hidden
|
|
|
|
&& !document.getElementById('zotero-recognizePDF').disabled;
|
2015-11-10 20:52:09 +00:00
|
|
|
var contentType = dialog.mLauncher.MIMEInfo.MIMEType;
|
2011-03-15 18:50:48 +00:00
|
|
|
|
2015-11-10 20:52:09 +00:00
|
|
|
// mimic dialog cancellation
|
|
|
|
dialog.onCancel();
|
2011-03-07 17:47:01 +00:00
|
|
|
|
|
|
|
// show progress dialog
|
2011-07-11 17:38:25 +00:00
|
|
|
var progressWin = new Zotero.ProgressWindow();
|
|
|
|
progressWin.changeHeadline(Zotero.getString("save.link"));
|
|
|
|
progressWin.show();
|
2011-03-07 17:47:01 +00:00
|
|
|
|
|
|
|
// perform import
|
2015-11-10 20:52:09 +00:00
|
|
|
try {
|
|
|
|
var item = yield Zotero.Attachments.importFromURL({
|
|
|
|
libraryID,
|
|
|
|
url,
|
|
|
|
collections: collection ? [collection.id] : [],
|
|
|
|
contentType
|
2015-12-10 06:09:40 +00:00
|
|
|
});
|
2015-11-10 20:52:09 +00:00
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
if (!win) return;
|
|
|
|
progressWin.addDescription(Zotero.getString("save.link.error"));
|
|
|
|
progressWin.startCloseTimer(8000);
|
|
|
|
Zotero.logError(e);
|
|
|
|
return false;
|
|
|
|
}
|
2011-03-07 17:47:01 +00:00
|
|
|
|
2015-11-10 20:52:09 +00:00
|
|
|
if(!win) return;
|
|
|
|
|
|
|
|
progressWin.addLines([item.getDisplayTitle()], [item.getImageSrc()]);
|
|
|
|
progressWin.startCloseTimer();
|
2015-12-10 06:09:40 +00:00
|
|
|
if (collection) {
|
|
|
|
yield collection.addItem(item.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
yield win.ZoteroPane.selectItem(item.id);
|
2015-11-10 20:52:09 +00:00
|
|
|
|
|
|
|
if(recognizePDF) {
|
|
|
|
var timer = Components.classes["@mozilla.org/timer;1"]
|
|
|
|
.createInstance(Components.interfaces.nsITimer);
|
|
|
|
timer.init(function() {
|
|
|
|
try {
|
|
|
|
if (item && item.getFile()) {
|
|
|
|
timer.cancel();
|
|
|
|
var recognizer = new win.Zotero_RecognizePDF.ItemRecognizer();
|
|
|
|
recognizer.recognizeItems([item]);
|
|
|
|
}
|
|
|
|
} catch(e) { dump(e.toSource()) };
|
|
|
|
}, 1000, Components.interfaces.nsITimer.TYPE_REPEATING_SLACK);
|
|
|
|
}
|
2011-03-07 17:47:01 +00:00
|
|
|
|
|
|
|
return true;
|
2015-11-10 20:52:09 +00:00
|
|
|
});
|
2011-03-07 17:47:01 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when mode in dialog has been changed
|
|
|
|
*/
|
|
|
|
this.modeChanged = function() {
|
2011-03-15 18:50:48 +00:00
|
|
|
var zoteroSelected = document.getElementById('zotero-radio').selected;
|
|
|
|
|
|
|
|
// don't allow user to remember Zotero option for file type; i'm not sure anyone wants this
|
|
|
|
// to happen automatically
|
|
|
|
if(zoteroSelected) document.getElementById('rememberChoice').selected = false;
|
|
|
|
document.getElementById('rememberChoice').disabled = zoteroSelected;
|
|
|
|
|
|
|
|
// disable recognizePDF checkbox as necessary
|
2011-08-26 21:49:47 +00:00
|
|
|
if(!Zotero.Fulltext.pdfConverterIsRegistered()) {
|
|
|
|
document.getElementById('zotero-noPDFTools-description').hidden = !zoteroSelected;
|
|
|
|
document.getElementById('zotero-recognizePDF').disabled = true;
|
|
|
|
window.sizeToContent();
|
|
|
|
} else {
|
|
|
|
document.getElementById('zotero-recognizePDF').disabled = !zoteroSelected;
|
|
|
|
}
|
|
|
|
|
|
|
|
Zotero_DownloadOverlay.updateLibraryNote();
|
2011-03-07 17:47:01 +00:00
|
|
|
};
|
|
|
|
|
2011-08-26 21:49:47 +00:00
|
|
|
/**
|
|
|
|
* Determines whether the note stating that the item will be saved to "My Library" is shown
|
|
|
|
*/
|
|
|
|
this.updateLibraryNote = function() {
|
|
|
|
var zoteroSelected = document.getElementById('zotero-radio').selected;
|
|
|
|
var zp = Zotero.getActiveZoteroPane(), canSave = true;
|
|
|
|
try {
|
Async DB megacommit
Promise-based rewrite of most of the codebase, with asynchronous database and file access -- see https://github.com/zotero/zotero/issues/518 for details.
WARNING: This includes backwards-incompatible schema changes.
An incomplete list of other changes:
- Schema overhaul
- Replace main tables with new versions with updated schema
- Enable real foreign key support and remove previous triggers
- Don't use NULLs for local libraryID, which broke the UNIQUE index
preventing object key duplication. All code (Zotero and third-party)
using NULL for the local library will need to be updated to use 0
instead (already done for Zotero code)
- Add 'compatibility' DB version that can be incremented manually to break DB
compatibility with previous versions. 'userdata' upgrades will no longer
automatically break compatibility.
- Demote creators and tags from first-class objects to item properties
- New API syncing properties
- 'synced'/'version' properties to data objects
- 'etag' to groups
- 'version' to libraries
- Create Zotero.DataObject that other objects inherit from
- Consolidate data object loading into Zotero.DataObjects
- Change object reloading so that only the loaded and changed parts of objects are reloaded, instead of reloading all data from the database (with some exceptions, including item primary data)
- Items and collections now have .parentItem and .parentKey properties, replacing item.getSource() and item.getSourceKey()
- New function Zotero.serial(fn), to wrap an async function such that all calls are run serially
- New function Zotero.Utilities.Internal.forEachChunkAsync(arr, chunkSize, func)
- Add tag selector loading message
- Various API and name changes, since everything was breaking anyway
Known broken things:
- Syncing (will be completely rewritten for API syncing)
- Translation architecture (needs promise-based rewrite)
- Duplicates view
- DB integrity check (from schema changes)
- Dragging (may be difficult to fix)
Lots of other big and little things are certainly broken, particularly with the UI, which can be affected by async code in all sorts of subtle ways.
2014-08-06 21:38:05 +00:00
|
|
|
canSave = zp.getCollectionTreeRow().filesEditable;
|
2011-08-26 21:49:47 +00:00
|
|
|
} catch(e) {
|
|
|
|
Zotero.logError(e);
|
|
|
|
};
|
|
|
|
document.getElementById('zotero-saveToLibrary-description').hidden = !zoteroSelected || canSave;
|
|
|
|
window.sizeToContent();
|
|
|
|
}
|
|
|
|
|
2011-03-07 17:47:01 +00:00
|
|
|
/**
|
|
|
|
* Called when the save dialog is opened
|
|
|
|
*/
|
|
|
|
this.init = function() {
|
2011-08-17 04:13:42 +00:00
|
|
|
if(Zotero.isConnector) return;
|
|
|
|
|
2011-03-15 18:50:48 +00:00
|
|
|
// Disable for filetypes people probably don't want to save
|
|
|
|
var show = false;
|
|
|
|
var mimeType = dialog.mLauncher.MIMEInfo.MIMEType.toLowerCase();
|
2016-05-03 21:51:12 +00:00
|
|
|
for (let elem of ALLOW_LIST) {
|
2011-03-15 18:50:48 +00:00
|
|
|
if(typeof elem === "string") {
|
|
|
|
if(elem === mimeType) {
|
|
|
|
document.getElementById('zotero-container').hidden = false;
|
2011-08-17 04:13:42 +00:00
|
|
|
document.getElementById('zotero-radio').disabled = false;
|
2011-03-15 18:50:48 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if(elem.test(mimeType)) {
|
|
|
|
document.getElementById('zotero-container').hidden = false;
|
2011-08-17 04:13:42 +00:00
|
|
|
document.getElementById('zotero-radio').disabled = false;
|
2011-03-15 18:50:48 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-07 17:47:01 +00:00
|
|
|
// Hook in event listener to ondialogaccept
|
|
|
|
document.documentElement.setAttribute('ondialogaccept',
|
2015-11-10 20:52:09 +00:00
|
|
|
'Zotero_DownloadOverlay.handleSave().then(function (saved) { if (!saved) {'
|
2011-03-07 17:47:01 +00:00
|
|
|
+ document.documentElement.getAttribute('ondialogaccept')
|
2015-11-10 20:52:09 +00:00
|
|
|
+'}})');
|
2011-03-07 17:47:01 +00:00
|
|
|
|
|
|
|
// Hook in event listener for mode change
|
|
|
|
var radios = document.getElementById('mode').
|
|
|
|
addEventListener("command", Zotero_DownloadOverlay.modeChanged, false);
|
2011-03-15 18:50:48 +00:00
|
|
|
|
|
|
|
// Set label on retrieve PDF option
|
|
|
|
if(mimeType === PDF_MIME_TYPE) {
|
|
|
|
var recognizePDF = document.getElementById('zotero-recognizePDF');
|
|
|
|
recognizePDF.label = Zotero.getString("pane.items.menu.recognizePDF");
|
|
|
|
recognizePDF.hidden = false;
|
|
|
|
recognizePDF.disabled = true;
|
2011-08-26 22:27:02 +00:00
|
|
|
if(!Zotero.Fulltext.pdfConverterIsRegistered()) {
|
|
|
|
recognizePDF.checked = false;
|
|
|
|
}
|
2011-03-15 18:50:48 +00:00
|
|
|
}
|
2011-03-07 17:47:01 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2011-08-26 21:49:47 +00:00
|
|
|
window.addEventListener("load", Zotero_DownloadOverlay.init, false);
|
|
|
|
window.addEventListener("activate", Zotero_DownloadOverlay.updateLibraryNote, false);
|