From 6db380b13211d4aad397581cb28785ae03b44c32 Mon Sep 17 00:00:00 2001 From: Simon Kornblith Date: Fri, 12 Jun 2015 16:57:38 -0400 Subject: [PATCH] Fix importing files and add a basic test Still need to make the progress indicator work again. Also there may be some performance to be gained by pooling item saves into a transaction if one is already open. --- chrome/content/zotero/fileInterface.js | 227 +++++++++------------- chrome/content/zotero/xpcom/utilities.js | 1 + test/tests/data/Test Import Translator.js | 46 +++++ test/tests/fileInterfaceTest.js | 40 ++++ 4 files changed, 180 insertions(+), 134 deletions(-) create mode 100644 test/tests/data/Test Import Translator.js create mode 100644 test/tests/fileInterfaceTest.js diff --git a/chrome/content/zotero/fileInterface.js b/chrome/content/zotero/fileInterface.js index 6a64ca064..d4a4f25b9 100644 --- a/chrome/content/zotero/fileInterface.js +++ b/chrome/content/zotero/fileInterface.js @@ -91,7 +91,7 @@ Zotero_File_Exporter.prototype.save = Zotero.Promise.coroutine(function* () { translation.setTranslator(io.selectedTranslator); translation.setDisplayOptions(io.displayOptions); translation.setHandler("itemDone", function () { - Zotero_File_Interface.updateProgress(translation, false); + Zotero.updateZoteroPaneProgressMeter(translation.getProgress()); }); translation.setHandler("done", this._exportDone); Zotero.UnresponsiveScriptIndicator.disable(); @@ -124,7 +124,6 @@ var Zotero_File_Interface = new function() { this.exportCollection = exportCollection; this.exportItemsToClipboard = exportItemsToClipboard; this.exportItems = exportItems; - this.importFile = importFile; this.bibliographyFromCollection = bibliographyFromCollection; this.bibliographyFromItems = bibliographyFromItems; this.copyItemsToClipboard = copyItemsToClipboard; @@ -209,7 +208,7 @@ var Zotero_File_Interface = new function() { /** * Creates Zotero.Translate instance and shows file picker for file import */ - function importFile(file, createNewCollection) { + this.importFile = Zotero.Promise.coroutine(function* (file, createNewCollection) { if(createNewCollection === undefined) { createNewCollection = true; } else if(!createNewCollection) { @@ -221,7 +220,8 @@ var Zotero_File_Interface = new function() { } var translation = new Zotero.Translate.Import(); - (file ? Zotero.Promise.resolve(file) : translation.getTranslators().then(function(translators) { + if (!file) { + let translators = yield translation.getTranslators(); const nsIFilePicker = Components.interfaces.nsIFilePicker; var fp = Components.classes["@mozilla.org/filepicker;1"] .createInstance(nsIFilePicker); @@ -237,24 +237,18 @@ var Zotero_File_Interface = new function() { return false; } - return fp.file; - })).then(function(file) { - if(!file) return; // no file if user cancelled + file = fp.file; + } - translation.setLocation(file); - // get translators again, bc now we can check against the file - translation.setHandler("translators", function(obj, item) { - _importTranslatorsAvailable(obj, item, createNewCollection); - }); - translators = translation.getTranslators(); - }).done(); - } + translation.setLocation(file); + yield _finishImport(translation, createNewCollection); + }); /** * Imports from clipboard */ - this.importFromClipboard = function () { + this.importFromClipboard = Zotero.Promise.coroutine(function* () { var str = Zotero.Utilities.Internal.getClipboard("text/unicode"); if(!str) { var ps = Components.classes["@mozilla.org/embedcomp/prompt-service;1"] @@ -262,105 +256,22 @@ var Zotero_File_Interface = new function() { ps.alert(null, "", Zotero.getString('fileInterface.importClipboardNoDataError')); } - var translate = new Zotero.Translate.Import(); - translate.setString(str); + var translation = new Zotero.Translate.Import(); + translation.setString(str); try { if (!ZoteroPane.collectionsView.editable) { ZoteroPane.collectionsView.selectLibrary(null); } } catch(e) {} - translate.setHandler("translators", function(obj, item) { - _importTranslatorsAvailable(obj, item, false); - }); - translators = translate.getTranslators(); - } + yield _finishImport(translation, false); + }); - function _importTranslatorsAvailable(translation, translators, createNewCollection) { - if(translators.length) { - var importCollection = null, libraryID = null; - - if(translation.location instanceof Components.interfaces.nsIFile) { - var leafName = translation.location.leafName; - var collectionName = (translation.location.isDirectory() || leafName.indexOf(".") === -1 ? leafName - : leafName.substr(0, leafName.lastIndexOf("."))); - var allCollections = Zotero.getCollections(); // TODO: Replace with Zotero.Collections.getBy* - for(var i=0; i x.name == 'allTypesAndFields'); + assert.equal(importedCollection.length, 1); + let childItems = importedCollection[0].getChildItems(); + let savedItems = {}; + for (let i=0; i