From 68365e2297d61ecc95080188a785d6ffff20f1d2 Mon Sep 17 00:00:00 2001 From: Simon Kornblith Date: Sun, 10 Jun 2012 11:05:39 -0400 Subject: [PATCH] Allow user to control whether files are imported into a new collection. Adds three new preferences controlling whether a new collection is created upon import: - extensions.zotero.import.createNewCollection.fromFile Controls whether a new collection is created by import from cog menu - extensions.zotero.import.createNewCollection.fromClipboard Controls whether a new collection is created by import from clipboard - extensions.zotero.import.createNewCollection.fromFileOpenHandler Controls whether a new collection is created when import is initiated by double-clicking a file or by dragging it over Zotero. This preference applies only to Zotero Standalone, and is configurable in the dialog that appears asking the user to confirm the import. Closes #19, [papercuts] (26) Clipboard import into an open collection --- chrome/content/zotero/fileInterface.js | 109 ++++++++++++------- chrome/locale/en-US/zotero/zotero.properties | 3 +- components/zotero-service.js | 12 +- defaults/preferences/zotero.js | 3 + 4 files changed, 81 insertions(+), 46 deletions(-) diff --git a/chrome/content/zotero/fileInterface.js b/chrome/content/zotero/fileInterface.js index d74c8af86..a588ccacb 100644 --- a/chrome/content/zotero/fileInterface.js +++ b/chrome/content/zotero/fileInterface.js @@ -110,7 +110,7 @@ Zotero_File_Exporter.prototype._exportDone = function(obj, worked) { * capabilities **/ var Zotero_File_Interface = new function() { - var _importCollection, _unlock; + var _unlock; this.exportFile = exportFile; this.exportCollection = exportCollection; @@ -195,7 +195,7 @@ var Zotero_File_Interface = new function() { /** * Creates Zotero.Translate instance and shows file picker for file import */ - function importFile(file) { + function importFile(file, createNewCollectionOverride) { var translation = new Zotero.Translate.Import(); if(!file) { var translators = translation.getTranslators(); @@ -218,9 +218,22 @@ var Zotero_File_Interface = new function() { file = fp.file; } + var createNewCollection = createNewCollectionOverride; + if(createNewCollectionOverride === undefined) { + createNewCollection = Zotero.Prefs.get("import.createNewCollection.fromFile"); + } else if(!createNewCollectionOverride) { + try { + if (!ZoteroPane.collectionsView.editable) { + ZoteroPane.collectionsView.selectLibrary(null); + } + } catch(e) {} + } + translation.setLocation(file); // get translators again, bc now we can check against the file - translation.setHandler("translators", function(obj, item) { _importTranslatorsAvailable(obj, item) }); + translation.setHandler("translators", function(obj, item) { + _importTranslatorsAvailable(obj, item, createNewCollection); + }); translators = translation.getTranslators(); } @@ -258,14 +271,16 @@ var Zotero_File_Interface = new function() { var translate = new Zotero.Translate.Import(); translate.setString(str); translate.setHandler("translators", function(obj, item) { - _importTranslatorsAvailable(obj, item) + _importTranslatorsAvailable(obj, item, Zotero.Prefs.get("import.createNewCollection.fromClipboard")); }); translators = translate.getTranslators(); } - function _importTranslatorsAvailable(translation, translators) { + 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 @@ -281,14 +296,57 @@ var Zotero_File_Interface = new function() { var collectionName = Zotero.getString("fileInterface.imported")+" "+(new Date()).toLocaleString(); } - // create a new collection to take in imported items - _importCollection = Zotero.Collections.add(collectionName); + if(createNewCollection) { + // Create a new collection to take imported items + importCollection = Zotero.Collections.add(collectionName); + } else { + // Import into currently selected collection + try { + libraryID = ZoteroPane.getSelectedLibraryID(); + importCollection = ZoteroPane.getSelectedCollection(); + } catch(e) {} + } // import items translation.setTranslator(translators[0]); - translation.setHandler("collectionDone", _importCollectionDone); + + if(importCollection) { + /* + * Saves collections after they've been imported. Input item is of the + * type outputted by Zotero.Collection.toArray(); only receives top-level + * collections + */ + translation.setHandler("collectionDone", function(obj, collection) { + collection.parent = importCollection.id; + collection.save(); + }); + } + translation.setHandler("itemDone", Zotero_File_Interface.updateProgress); - translation.setHandler("done", _importDone); + + /* + * closes items imported indicator + */ + translation.setHandler("done", function(obj, worked) { + // add items to import collection + if(importCollection) { + importCollection.addItems([item.id for each(item in obj.newItems)]); + } + + Zotero.DB.commitTransaction(); + + Zotero_File_Interface.Progress.close(); + Zotero.UnresponsiveScriptIndicator.enable(); + + if (worked) { + if(importCollection) { + Zotero.Notifier.trigger('refresh', 'collection', importCollection.id); + } + } else { + if(importCollection) importCollection.erase(); + window.alert(Zotero.getString("fileInterface.importError")); + } + }); Zotero.UnresponsiveScriptIndicator.disable(); // show progress indicator @@ -298,7 +356,7 @@ var Zotero_File_Interface = new function() { window.setTimeout(function() { Zotero.DB.beginTransaction(); - translation.translate(); + translation.translate(libraryID); }, 0); } else { // TODO: localize and remove fileInterface.fileFormatUnsupported string @@ -324,37 +382,6 @@ var Zotero_File_Interface = new function() { } } - /* - * Saves collections after they've been imported. Input item is of the type - * outputted by Zotero.Collection.toArray(); only receives top-level - * collections - */ - function _importCollectionDone(obj, collection) { - collection.parent = _importCollection.id; - collection.save(); - } - - /* - * closes items imported indicator - */ - function _importDone(obj, worked) { - // add items to import collection - _importCollection.addItems([item.id for each(item in obj.newItems)]); - - Zotero.DB.commitTransaction(); - - Zotero_File_Interface.Progress.close(); - Zotero.UnresponsiveScriptIndicator.enable(); - - if (worked) { - Zotero.Notifier.trigger('refresh', 'collection', _importCollection.id); - } - else { - _importCollection.erase(); - window.alert(Zotero.getString("fileInterface.importError")); - } - } - /* * Creates a bibliography from a collection or saved search */ diff --git a/chrome/locale/en-US/zotero/zotero.properties b/chrome/locale/en-US/zotero/zotero.properties index 2431ba905..53b9c2076 100644 --- a/chrome/locale/en-US/zotero/zotero.properties +++ b/chrome/locale/en-US/zotero/zotero.properties @@ -423,7 +423,8 @@ ingester.importReferRISDialog.text = Do you want to import items from "%1$S" int ingester.importReferRISDialog.checkMsg = Always allow for this site ingester.importFile.title = Import File -ingester.importFile.text = Do you want to import the file "%S"?\n\nItems will be added to a new collection. +ingester.importFile.text = Do you want to import the file "%S"? +ingester.importFile.intoNewCollection = Import into new collection ingester.lookup.performing = Performing Lookup… ingester.lookup.error = An error occurred while performing lookup for this item. diff --git a/components/zotero-service.js b/components/zotero-service.js index 7dc14b44f..52806c489 100644 --- a/components/zotero-service.js +++ b/components/zotero-service.js @@ -373,7 +373,7 @@ ZoteroCommandLineHandler.prototype = { .createInstance(Components.interfaces.nsIProtocolHandler).newChannel(uri); } } else { - Zotero.debug("Not handling URL: "+uri.spec); + this.Zotero.debug("Not handling URL: "+uri.spec); } } @@ -389,15 +389,19 @@ ZoteroCommandLineHandler.prototype = { this.Zotero.Styles.install(file); } else { // Ask before importing + var checkState = {"value":this.Zotero.Prefs.get('import.createNewCollection.fromFileOpenHandler')}; if(Components.classes["@mozilla.org/embedcomp/prompt-service;1"] .getService(Components.interfaces.nsIPromptService) - .confirm(null, this.Zotero.getString('ingester.importFile.title'), - this.Zotero.getString('ingester.importFile.text', [file.leafName]))) { + .confirmCheck(null, this.Zotero.getString('ingester.importFile.title'), + this.Zotero.getString('ingester.importFile.text', [file.leafName]), + this.Zotero.getString('ingester.importFile.intoNewCollection'), + checkState)) { // Perform file import in front window var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"] .getService(Components.interfaces.nsIWindowMediator); var browserWindow = wm.getMostRecentWindow("navigator:browser"); - browserWindow.Zotero_File_Interface.importFile(file); + browserWindow.Zotero_File_Interface.importFile(file, checkState.value); + this.Zotero.Prefs.set('import.createNewCollection.fromFileOpenHandler', checkState.value); } } } diff --git a/defaults/preferences/zotero.js b/defaults/preferences/zotero.js index a8473dbf6..80f4ab3a4 100644 --- a/defaults/preferences/zotero.js +++ b/defaults/preferences/zotero.js @@ -94,6 +94,9 @@ pref("extensions.zotero.export.bibliographyLocale", ''); pref("extensions.zotero.export.citePaperJournalArticleURL", false); pref("extensions.zotero.export.displayCharsetOption", false); pref("extensions.zotero.import.charset", "auto"); +pref("extensions.zotero.import.createNewCollection.fromFile", true); +pref("extensions.zotero.import.createNewCollection.fromClipboard", true); +pref("extensions.zotero.import.createNewCollection.fromFileOpenHandler", true); pref("extensions.zotero.rtfScan.lastInputFile", ""); pref("extensions.zotero.rtfScan.lastOutputFile", "");