Skip to content

Commit

Permalink
Merge changes from upstream Zotero
Browse files Browse the repository at this point in the history
  • Loading branch information
fbennett committed Mar 4, 2019
2 parents 09039c9 + 3c6ab77 commit 7fa0f6c
Show file tree
Hide file tree
Showing 14 changed files with 197 additions and 33 deletions.
7 changes: 6 additions & 1 deletion chrome/content/zotero/fileInterface.js
Expand Up @@ -296,6 +296,7 @@ var Zotero_File_Interface = new function() {
* @param {nsIFile|string|null} [options.file=null] - File to import, or none to show a filepicker
* @param {Boolean} [options.addToLibraryRoot=false]
* @param {Boolean} [options.createNewCollection=true] - Put items in a new collection
* @param {Boolean} [options.linkFiles=false] - Link to files instead of storing them
* @param {Function} [options.onBeforeImport] - Callback to receive translation object, useful
* for displaying progress in a different way. This also causes an error to be throw
* instead of shown in the main window.
Expand All @@ -315,6 +316,7 @@ var Zotero_File_Interface = new function() {
var file = options.file ? Zotero.File.pathToFile(options.file) : null;
var createNewCollection = options.createNewCollection;
var addToLibraryRoot = options.addToLibraryRoot;
var linkFiles = options.linkFiles;
var onBeforeImport = options.onBeforeImport;

if (createNewCollection === undefined && !addToLibraryRoot) {
Expand Down Expand Up @@ -359,6 +361,7 @@ var Zotero_File_Interface = new function() {
translation,
createNewCollection,
addToLibraryRoot,
linkFiles,
defaultNewCollectionPrefix,
onBeforeImport
});
Expand Down Expand Up @@ -412,6 +415,7 @@ var Zotero_File_Interface = new function() {
var translation = options.translation;
var addToLibraryRoot = options.addToLibraryRoot;
var createNewCollection = options.createNewCollection;
var linkFiles = options.linkFiles;
var defaultNewCollectionPrefix = options.defaultNewCollectionPrefix;
var onBeforeImport = options.onBeforeImport;

Expand Down Expand Up @@ -518,7 +522,8 @@ var Zotero_File_Interface = new function() {
try {
yield translation.translate({
libraryID,
collections: importCollection ? [importCollection.id] : null
collections: importCollection ? [importCollection.id] : null,
linkFiles
});
} catch(e) {
if (!showProgressWindow) {
Expand Down
39 changes: 23 additions & 16 deletions chrome/content/zotero/import/importWizard.js
Expand Up @@ -31,6 +31,17 @@ var Zotero_Import_Wizard = {
}
}

// Update labels
document.getElementById('file-handling-store').label = Zotero.getString(
'import.fileHandling.store',
Zotero.appName
);
document.getElementById('file-handling-link').label = Zotero.getString('import.fileHandling.link');
document.getElementById('file-handling-description').textContent = Zotero.getString(
'import.fileHandling.description',
Zotero.appName
);

Zotero.Translators.init(); // async
},

Expand Down Expand Up @@ -160,20 +171,6 @@ var Zotero_Import_Wizard = {
},


onImportStart: async function () {
if (!this._file) {
let index = document.getElementById('file-list').selectedIndex;
this._file = this._dbs[index].path;
}
this._disableCancel();
this._wizard.canRewind = false;
this._wizard.canAdvance = false;
await this.doImport({
createNewCollection: document.getElementById('create-collection-checkbox').hasAttribute('checked')
});
},


onBeforeImport: async function (translation) {
// Unrecognized translator
if (!translation) {
Expand All @@ -196,12 +193,22 @@ var Zotero_Import_Wizard = {
},


doImport: async function (options) {
onImportStart: async function () {
if (!this._file) {
let index = document.getElementById('file-list').selectedIndex;
this._file = this._dbs[index].path;
}
this._disableCancel();
this._wizard.canRewind = false;
this._wizard.canAdvance = false;

try {
let result = await Zotero_File_Interface.importFile({
file: this._file,
onBeforeImport: this.onBeforeImport.bind(this),
addToLibraryRoot: !options.createNewCollection
addToLibraryRoot: !document.getElementById('create-collection-checkbox')
.hasAttribute('checked'),
linkFiles: document.getElementById('file-handling-radio').selectedIndex == 1
});

// Cancelled by user or due to error
Expand Down
10 changes: 10 additions & 0 deletions chrome/content/zotero/import/importWizard.xul
Expand Up @@ -54,6 +54,16 @@
onpagerewound="return Zotero_Import_Wizard.goToStart()"
onpageadvanced="Zotero_Import_Wizard.onImportStart()">
<checkbox id="create-collection-checkbox" label="&zotero.import.createCollection;" checked="true" />

<vbox id="file-handling-options">
<label control="file-handling-radio" value="&zotero.import.fileHandling;"/>
<radiogroup id="file-handling-radio">
<radio id="file-handling-store" selected="true"/>
<radio id="file-handling-link"/>
</radiogroup>
<description id="file-handling-description"/>
</vbox>

</wizardpage>

<wizardpage pageid="page-progress"
Expand Down
18 changes: 13 additions & 5 deletions chrome/content/zotero/import/mendeley/mendeleyImport.js
Expand Up @@ -6,6 +6,7 @@ Services.scriptloader.loadSubScript("chrome://zotero/content/include.js");

var Zotero_Import_Mendeley = function () {
this.createNewCollection = null;
this.linkFiles = null;
this.newItems = [];

this._db;
Expand Down Expand Up @@ -39,7 +40,9 @@ Zotero_Import_Mendeley.prototype.getTranslators = async function () {

Zotero_Import_Mendeley.prototype.setTranslator = function () {};

Zotero_Import_Mendeley.prototype.translate = async function (options) {
Zotero_Import_Mendeley.prototype.translate = async function (options = {}) {
this._linkFiles = options.linkFiles;

if (true) {
Services.scriptloader.loadSubScript("chrome://zotero/content/import/mendeley/mendeleySchemaMap.js");
}
Expand Down Expand Up @@ -108,6 +111,11 @@ Zotero_Import_Mendeley.prototype.translate = async function (options) {
let docURLs = urls.get(document.id);
let docFiles = files.get(document.id);

// If there's a single PDF file, use "PDF" for the attachment title
if (docFiles && docFiles.length == 1 && docFiles[0].fileURL.endsWith('.pdf')) {
docFiles[0].title = 'PDF';
}

// If there's a single PDF file and a single PDF URL and the file exists, make an
// imported_url attachment instead of separate file and linked_url attachments
if (docURLs && docFiles) {
Expand All @@ -121,7 +129,6 @@ Zotero_Import_Mendeley.prototype.translate = async function (options) {
if (x.fileURL.endsWith('.pdf')) {
x.title = 'PDF';
x.url = pdfURLs[0];
x.contentType = 'application/pdf';
}
});
// Remove PDF URL from URLs array
Expand Down Expand Up @@ -985,10 +992,11 @@ Zotero_Import_Mendeley.prototype._saveFilesAndAnnotations = async function (file
let options = {
libraryID,
parentItemID,
file: realPath
file: realPath,
title: file.title
};
// If file is in Mendeley downloads folder, import it
if (this._isDownloadedFile(path)) {
// If we're not set to link files or file is in Mendeley downloads folder, import it
if (!this._linkFiles || this._isDownloadedFile(path)) {
if (file.url) {
options.title = file.title;
options.url = file.url;
Expand Down
15 changes: 11 additions & 4 deletions chrome/content/zotero/xpcom/attachments.js
Expand Up @@ -42,6 +42,7 @@ Zotero.Attachments = new function(){
* @param {nsIFile|String} [options.file] - File to add
* @param {Integer} [options.libraryID]
* @param {Integer[]|String[]} [options.parentItemID] - Parent item to add item to
* @param {String} [options.title]
* @param {Integer[]} [options.collections] - Collection keys or ids to add new item to
* @param {String} [options.fileBaseName]
* @param {String} [options.contentType]
Expand All @@ -57,6 +58,7 @@ Zotero.Attachments = new function(){
var path = file.path;
var leafName = file.leafName;
var parentItemID = options.parentItemID;
var title = options.title;
var collections = options.collections;
var fileBaseName = options.fileBaseName;
var contentType = options.contentType;
Expand Down Expand Up @@ -91,7 +93,7 @@ Zotero.Attachments = new function(){
else if (libraryID) {
attachmentItem.libraryID = libraryID;
}
attachmentItem.setField('title', newName);
attachmentItem.setField('title', title != undefined ? title : newName);
attachmentItem.parentID = parentItemID;
attachmentItem.attachmentLinkMode = this.LINK_MODE_IMPORTED_FILE;
if (collections) {
Expand Down Expand Up @@ -151,7 +153,10 @@ Zotero.Attachments = new function(){
/**
* @param {nsIFile|String} options.file - File to add
* @param {Integer[]|String[]} [options.parentItemID] - Parent item to add item to
* @param {String} [options.title]
* @param {Integer[]} [options.collections] - Collection keys or ids to add new item to
* @param {String} [options.contentType] - Content type
* @param {String} [options.charset] - Character set
* @param {Object} [options.saveOptions] - Options to pass to Zotero.Item::save()
* @return {Promise<Zotero.Item>}
*/
Expand All @@ -160,20 +165,22 @@ Zotero.Attachments = new function(){

var file = Zotero.File.pathToFile(options.file);
var parentItemID = options.parentItemID;
var title = options.title;
var collections = options.collections;
var contentType = options.contentType || (yield Zotero.MIME.getMIMETypeFromFile(file));
var charset = options.charset;
var saveOptions = options.saveOptions;

if (parentItemID && collections) {
throw new Error("parentItemID and collections cannot both be provided");
}

var title = file.leafName;
var contentType = yield Zotero.MIME.getMIMETypeFromFile(file);
var item = yield _addToDB({
file,
title,
title: title != undefined ? title : file.leafName,
linkMode: this.LINK_MODE_LINKED_FILE,
contentType,
charset,
parentItemID,
collections,
saveOptions
Expand Down
1 change: 1 addition & 0 deletions chrome/content/zotero/xpcom/data/cachedTypes.js
Expand Up @@ -460,6 +460,7 @@ Zotero.ItemTypes = new function() {
// HiDPI images available
case 'attachment-link':
case 'attachment-pdf':
case 'attachment-pdf-link':
case 'attachment-snapshot':
case 'attachment-web-link':
case 'artwork':
Expand Down
13 changes: 7 additions & 6 deletions chrome/content/zotero/xpcom/data/item.js
Expand Up @@ -4214,12 +4214,13 @@ Zotero.Item.prototype.getImageSrc = function() {
if (itemType == 'attachment') {
var linkMode = this.attachmentLinkMode;

// Quick hack to use PDF icon for imported files and URLs --
// extend to support other document types later
if ((linkMode == Zotero.Attachments.LINK_MODE_IMPORTED_FILE ||
linkMode == Zotero.Attachments.LINK_MODE_IMPORTED_URL) &&
this.attachmentContentType == 'application/pdf') {
itemType += '-pdf';
if (this.attachmentContentType == 'application/pdf') {
if (linkMode == Zotero.Attachments.LINK_MODE_LINKED_FILE) {
itemType += '-pdf-link';
}
else {
itemType += '-pdf';
}
}
else if (linkMode == Zotero.Attachments.LINK_MODE_IMPORTED_FILE) {
itemType += "-file";
Expand Down
3 changes: 3 additions & 0 deletions chrome/content/zotero/xpcom/translation/translate.js
Expand Up @@ -1287,6 +1287,7 @@ Zotero.Translate.Base.prototype = {
* or NULL for default library;
* if FALSE, don't save items
* @param {Boolean} [saveAttachments=true] Exclude attachments (e.g., snapshots) on import
* @param {Boolean} [linkFiles=false] - Save linked files instead of stored files
* @returns {Promise} Promise resolved with saved items
* when translation complete
*/
Expand Down Expand Up @@ -1326,6 +1327,7 @@ Zotero.Translate.Base.prototype = {
}
this._collections = options.collections;
this._saveAttachments = options.saveAttachments === undefined || options.saveAttachments;
this._linkFiles = options.linkFiles;
this._forceTagType = options.forceTagType;
this._saveOptions = options.saveOptions;

Expand Down Expand Up @@ -2438,6 +2440,7 @@ Zotero.Translate.Import.prototype._prepareTranslation = Zotero.Promise.method(fu
collections: this._collections,
forceTagType: this._forceTagType,
attachmentMode: Zotero.Translate.ItemSaver[(this._saveAttachments ? "ATTACHMENT_MODE_FILE" : "ATTACHMENT_MODE_IGNORE")],
linkFiles: this._linkFiles,
baseURI,
saveOptions: Object.assign(
{
Expand Down
21 changes: 20 additions & 1 deletion chrome/content/zotero/xpcom/translation/translate_item.js
Expand Up @@ -31,6 +31,7 @@
* <li>libraryID - ID of library in which items should be saved</li>
* <li>collections - New collections to create (used during Import translation</li>
* <li>attachmentMode - One of Zotero.Translate.ItemSaver.ATTACHMENT_* specifying how attachments should be saved</li>
* <li>linkFiles - Save attachments as linked files instead of stored files</li>
* <li>forceTagType - Force tags to specified tag type</li>
* <li>cookieSandbox - Cookie sandbox for attachment requests</li>
* <li>proxy - A proxy to deproxify item URLs</li>
Expand All @@ -53,6 +54,7 @@ Zotero.Translate.ItemSaver = function(options) {
// If group filesEditable==false, don't save attachments
this.attachmentMode = Zotero.Libraries.get(this._libraryID).filesEditable ? options.attachmentMode :
Zotero.Translate.ItemSaver.ATTACHMENT_MODE_IGNORE;
this._linkFiles = options.linkFiles;
this._forceTagType = options.forceTagType;
this._referrer = options.referrer;
this._cookieSandbox = options.cookieSandbox;
Expand Down Expand Up @@ -605,7 +607,24 @@ Zotero.Translate.ItemSaver.prototype = {
title: attachment.title || undefined,
collections: !parentItemID ? this._collections : undefined
});
} else {
}
else if (this._linkFiles
// Don't link if it's a path to the current storage directory
&& !Zotero.File.directoryContains(Zotero.DataDirectory.getSubdirectory('storage'), file.path)) {
attachment.linkMode = "linked_file";
newItem = yield Zotero.Attachments.linkFromFile({
file,
parentItemID,
collections: !parentItemID ? this._collections : undefined
});
if (attachment.title) {
newItem.setField("title", attachment.title);
}
if (attachment.url) {
newItem.setNote(attachment.url);
}
}
else {
if (attachment.url) {
attachment.linkMode = "imported_url";
newItem = yield Zotero.Attachments.importSnapshotFromFile({
Expand Down
22 changes: 22 additions & 0 deletions chrome/skin/default/zotero/importWizard.css
Expand Up @@ -33,10 +33,32 @@ wizard[currentpageid="page-file-list"] .wizard-header {
margin-bottom: 6px;
}

#file-handling-options {
margin-top: 2em;
}

#file-handling-options > label {
font-size: 14px;
}

#file-handling-options radiogroup {
font-size: 13px;
margin-left: 1em;
}

#file-handling-options description {
margin-top: .6em;
font-size: 11px;
}

listbox, #result-description, #result-description-html {
font-size: 13px;
}

#result-description-html {
line-height: 1.5;
}

#result-description-html a {
text-decoration: underline;
}
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified chrome/skin/default/zotero/treeitem-attachment-pdf.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 7fa0f6c

Please sign in to comment.