Merge branch 'no-new-collection-on-import'

This commit is contained in:
Simon Kornblith 2012-06-10 22:39:41 -04:00
commit ac19b0490a
4 changed files with 81 additions and 46 deletions

View file

@ -110,7 +110,7 @@ Zotero_File_Exporter.prototype._exportDone = function(obj, worked) {
* capabilities * capabilities
**/ **/
var Zotero_File_Interface = new function() { var Zotero_File_Interface = new function() {
var _importCollection, _unlock; var _unlock;
this.exportFile = exportFile; this.exportFile = exportFile;
this.exportCollection = exportCollection; this.exportCollection = exportCollection;
@ -195,7 +195,7 @@ var Zotero_File_Interface = new function() {
/** /**
* Creates Zotero.Translate instance and shows file picker for file import * Creates Zotero.Translate instance and shows file picker for file import
*/ */
function importFile(file) { function importFile(file, createNewCollectionOverride) {
var translation = new Zotero.Translate.Import(); var translation = new Zotero.Translate.Import();
if(!file) { if(!file) {
var translators = translation.getTranslators(); var translators = translation.getTranslators();
@ -218,9 +218,22 @@ var Zotero_File_Interface = new function() {
file = fp.file; 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); translation.setLocation(file);
// get translators again, bc now we can check against the 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(); translators = translation.getTranslators();
} }
@ -258,14 +271,16 @@ var Zotero_File_Interface = new function() {
var translate = new Zotero.Translate.Import(); var translate = new Zotero.Translate.Import();
translate.setString(str); translate.setString(str);
translate.setHandler("translators", function(obj, item) { translate.setHandler("translators", function(obj, item) {
_importTranslatorsAvailable(obj, item) _importTranslatorsAvailable(obj, item, Zotero.Prefs.get("import.createNewCollection.fromClipboard"));
}); });
translators = translate.getTranslators(); translators = translate.getTranslators();
} }
function _importTranslatorsAvailable(translation, translators) { function _importTranslatorsAvailable(translation, translators, createNewCollection) {
if(translators.length) { if(translators.length) {
var importCollection = null, libraryID = null;
if(translation.location instanceof Components.interfaces.nsIFile) { if(translation.location instanceof Components.interfaces.nsIFile) {
var leafName = translation.location.leafName; var leafName = translation.location.leafName;
var collectionName = (translation.location.isDirectory() || leafName.indexOf(".") === -1 ? 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(); var collectionName = Zotero.getString("fileInterface.imported")+" "+(new Date()).toLocaleString();
} }
// create a new collection to take in imported items if(createNewCollection) {
_importCollection = Zotero.Collections.add(collectionName); // 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 // import items
translation.setTranslator(translators[0]); 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("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(); Zotero.UnresponsiveScriptIndicator.disable();
// show progress indicator // show progress indicator
@ -298,7 +356,7 @@ var Zotero_File_Interface = new function() {
window.setTimeout(function() { window.setTimeout(function() {
Zotero.DB.beginTransaction(); Zotero.DB.beginTransaction();
translation.translate(); translation.translate(libraryID);
}, 0); }, 0);
} else { } else {
// TODO: localize and remove fileInterface.fileFormatUnsupported string // 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 * Creates a bibliography from a collection or saved search
*/ */

View file

@ -424,7 +424,8 @@ ingester.importReferRISDialog.text = Do you want to import items from "%1$S" int
ingester.importReferRISDialog.checkMsg = Always allow for this site ingester.importReferRISDialog.checkMsg = Always allow for this site
ingester.importFile.title = Import File 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.performing = Performing Lookup…
ingester.lookup.error = An error occurred while performing lookup for this item. ingester.lookup.error = An error occurred while performing lookup for this item.

View file

@ -377,7 +377,7 @@ ZoteroCommandLineHandler.prototype = {
.createInstance(Components.interfaces.nsIProtocolHandler).newChannel(uri); .createInstance(Components.interfaces.nsIProtocolHandler).newChannel(uri);
} }
} else { } else {
Zotero.debug("Not handling URL: "+uri.spec); this.Zotero.debug("Not handling URL: "+uri.spec);
} }
} }
@ -393,15 +393,19 @@ ZoteroCommandLineHandler.prototype = {
this.Zotero.Styles.install(file); this.Zotero.Styles.install(file);
} else { } else {
// Ask before importing // Ask before importing
var checkState = {"value":this.Zotero.Prefs.get('import.createNewCollection.fromFileOpenHandler')};
if(Components.classes["@mozilla.org/embedcomp/prompt-service;1"] if(Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
.getService(Components.interfaces.nsIPromptService) .getService(Components.interfaces.nsIPromptService)
.confirm(null, this.Zotero.getString('ingester.importFile.title'), .confirmCheck(null, this.Zotero.getString('ingester.importFile.title'),
this.Zotero.getString('ingester.importFile.text', [file.leafName]))) { this.Zotero.getString('ingester.importFile.text', [file.leafName]),
this.Zotero.getString('ingester.importFile.intoNewCollection'),
checkState)) {
// Perform file import in front window // Perform file import in front window
var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"] var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
.getService(Components.interfaces.nsIWindowMediator); .getService(Components.interfaces.nsIWindowMediator);
var browserWindow = wm.getMostRecentWindow("navigator:browser"); 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);
} }
} }
} }

View file

@ -94,6 +94,9 @@ pref("extensions.zotero.export.bibliographyLocale", '');
pref("extensions.zotero.export.citePaperJournalArticleURL", false); pref("extensions.zotero.export.citePaperJournalArticleURL", false);
pref("extensions.zotero.export.displayCharsetOption", false); pref("extensions.zotero.export.displayCharsetOption", false);
pref("extensions.zotero.import.charset", "auto"); 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.lastInputFile", "");
pref("extensions.zotero.rtfScan.lastOutputFile", ""); pref("extensions.zotero.rtfScan.lastOutputFile", "");