Fix Open/Save dialog and use of hidden browser when saving file types not handled natively using "Save Link as Zotero Snapshot" or drag-and-drop to create a standalone item

This need testing.
This commit is contained in:
Dan Stillman 2009-05-28 09:52:52 +00:00
parent 8712f49426
commit 017342a85e
4 changed files with 344 additions and 211 deletions

View file

@ -628,16 +628,16 @@ var ZoteroPane = new function()
return false;
}
if (!this.canEdit(row)) {
this.displayCannotEditLibraryMessage();
return;
}
// Currently selected row
if (row === undefined) {
row = this.collectionsView.selection.currentIndex;
}
if (!this.canEdit(row)) {
this.displayCannotEditLibraryMessage();
return;
}
if (row !== undefined) {
var itemGroup = this.collectionsView._getItemAtRow(row);
var libraryID = itemGroup.ref.libraryID;
@ -2255,30 +2255,6 @@ var ZoteroPane = new function()
* regardless of automaticSnapshots pref
*/
this.addItemFromDocument = function (doc, itemType, saveSnapshot, row) {
if (!Zotero.stateCheck()) {
this.displayErrorMessage(true);
return false;
}
// Currently selected row
if (row === undefined) {
row = this.collectionsView.selection.currentIndex;
}
if (!this.canEdit(row)) {
this.displayCannotEditLibraryMessage();
return;
}
if (row !== undefined) {
var itemGroup = this.collectionsView._getItemAtRow(row);
var libraryID = itemGroup.ref.libraryID;
}
else {
var libraryID = null;
var itemGroup = null;
}
var progressWin = new Zotero.ProgressWindow();
progressWin.changeHeadline(Zotero.getString('ingester.scraping'));
var icon = 'chrome://zotero/skin/treeitem-webpage.png';
@ -2286,12 +2262,6 @@ var ZoteroPane = new function()
progressWin.show();
progressWin.startCloseTimer();
var data = {
title: doc.title,
url: doc.location.href,
accessDate: "CURRENT_TIMESTAMP"
}
// TODO: this, needless to say, is a temporary hack
if (itemType == 'temporaryPDFHack') {
itemType = null;
@ -2315,6 +2285,36 @@ var ZoteroPane = new function()
}
if (isPDF) {
//
// Duplicate newItem() checks here
//
if (!Zotero.stateCheck()) {
this.displayErrorMessage(true);
return false;
}
// Currently selected row
if (row === undefined) {
row = this.collectionsView.selection.currentIndex;
}
if (!this.canEdit(row)) {
this.displayCannotEditLibraryMessage();
return;
}
if (row !== undefined) {
var itemGroup = this.collectionsView._getItemAtRow(row);
var libraryID = itemGroup.ref.libraryID;
}
else {
var libraryID = null;
var itemGroup = null;
}
//
//
//
if (libraryID) {
var pr = Components.classes["@mozilla.org/network/default-prompt;1"]
.getService(Components.interfaces.nsIPrompt);
@ -2328,6 +2328,7 @@ var ZoteroPane = new function()
else {
var collectionID = false;
}
Zotero.Attachments.importFromDocument(doc, false, false, collectionID);
return;
}
@ -2337,6 +2338,11 @@ var ZoteroPane = new function()
if (!itemType) {
itemType = 'webpage';
}
var data = {
title: doc.title,
url: doc.location.href,
accessDate: "CURRENT_TIMESTAMP"
}
itemType = Zotero.ItemTypes.getID(itemType);
var item = this.newItem(itemType, data, row);
@ -2369,6 +2375,9 @@ var ZoteroPane = new function()
return this.addItemFromPage(itemType, saveSnapshot, row);
}
Zotero.MIME.getMIMETypeFromURL(url, function (mimeType, hasNativeHandler) {
// If native type, save using a hidden browser
if (hasNativeHandler) {
var processor = function (doc) {
ZoteroPane.addItemFromDocument(doc, itemType, saveSnapshot, row);
};
@ -2381,6 +2390,99 @@ var ZoteroPane = new function()
Zotero.Utilities.HTTP.processDocuments([url], processor, done, exception);
}
// Otherwise create placeholder item, attach attachment, and update from that
else {
// TODO: this, needless to say, is a temporary hack
if (itemType == 'temporaryPDFHack') {
itemType = null;
if (mimeType == 'application/pdf') {
//
// Duplicate newItem() checks here
//
if (!Zotero.stateCheck()) {
ZoteroPane.displayErrorMessage(true);
return false;
}
// Currently selected row
if (row === undefined) {
row = ZoteroPane.collectionsView.selection.currentIndex;
}
if (!ZoteroPane.canEdit(row)) {
ZoteroPane.displayCannotEditLibraryMessage();
return;
}
if (row !== undefined) {
var itemGroup = ZoteroPane.collectionsView._getItemAtRow(row);
var libraryID = itemGroup.ref.libraryID;
}
else {
var libraryID = null;
var itemGroup = null;
}
//
//
//
if (libraryID) {
var pr = Components.classes["@mozilla.org/network/default-prompt;1"]
.getService(Components.interfaces.nsIPrompt);
pr.alert("", "Files cannot currently be added to group libraries.");
return;
}
if (itemGroup && itemGroup.isCollection()) {
var collectionID = itemGroup.ref.id;
}
else {
var collectionID = false;
}
Zotero.Attachments.importFromURL(url, false, false, collectionID);
return;
}
}
if (!itemType) {
itemType = 'webpage';
}
var item = ZoteroPane.newItem(itemType, {}, row);
if (item.libraryID) {
var group = Zotero.Groups.getByLibraryID(item.libraryID);
filesEditable = group.filesEditable;
}
else {
filesEditable = true;
}
// Save snapshot if explicitly enabled or automatically pref is set and not explicitly disabled
if (saveSnapshot || (saveSnapshot !== false && Zotero.Prefs.get('automaticSnapshots'))) {
var link = false;
if (link) {
//Zotero.Attachments.linkFromURL(doc, item.id);
}
else if (filesEditable) {
var attachmentItem = Zotero.Attachments.importFromURL(url, item.id, false, false, false, mimeType);
if (attachmentItem) {
item.setField('title', attachmentItem.getField('title'));
item.setField('url', attachmentItem.getField('url'));
item.setField('accessDate', attachmentItem.getField('accessDate'));
item.save();
}
}
}
return item.id;
}
});
}
/*

View file

@ -63,7 +63,7 @@
<!-- TODO: localize and remove zotero.contextMenu.saveLinkAsItem/saveImageAsSnapshot -->
<menuitem id="zotero-context-save-link-as-item" class="menu-iconic"
label="Save Link as Zotero Item" hidden="true"
oncommand="ZoteroPane.addItemFromURL(window.gContextMenu.linkURL)"/>
oncommand="ZoteroPane.addItemFromURL(window.gContextMenu.linkURL, 'temporaryPDFHack')"/>
<menuitem id="zotero-context-save-image-as-item" class="menu-iconic"
label="Save Image as Zotero Item" hidden="true"
oncommand="ZoteroPane.addItemFromURL(window.gContextMenu.onImage ? (window.gContextMenu.mediaURL ? window.gContextMenu.mediaURL : window.gContextMenu.imageURL) : window.gContextMenu.bgImageURL, 'artwork')"/>

View file

@ -187,7 +187,7 @@ Zotero.Attachments = new function(){
}
function importFromURL(url, sourceItemID, forceTitle, forceFileBaseName, parentCollectionIDs){
function importFromURL(url, sourceItemID, forceTitle, forceFileBaseName, parentCollectionIDs, mimeType) {
Zotero.debug('Importing attachment from URL');
if (sourceItemID && parentCollectionIDs) {
@ -206,32 +206,8 @@ Zotero.Attachments = new function(){
throw ("Invalid URL '" + url + "' in Zotero.Attachments.importFromURL()");
}
Zotero.Utilities.HTTP.doHead(url, function(obj){
if (obj.status != 200 && obj.status != 204) {
Zotero.debug("Attachment HEAD request returned with status code "
+ obj.status + " in Attachments.importFromURL()", 2);
var mimeType = '';
}
else {
var mimeType = obj.channel.contentType;
}
var nsIURL = Components.classes["@mozilla.org/network/standard-url;1"]
.createInstance(Components.interfaces.nsIURL);
nsIURL.spec = url;
// Override MIME type to application/pdf if extension is .pdf --
// workaround for sites that respond to the HEAD request with an
// invalid MIME type (https://www.zotero.org/trac/ticket/460)
//
// Downloaded file is inspected below and deleted if actually HTML
if (nsIURL.fileName.match(/pdf$/) || url.match(/pdf$/)) {
mimeType = 'application/pdf';
}
// If we can load this natively, use a hidden browser (so we can
// get the charset and title and index the document)
if (Zotero.MIME.hasNativeHandler(mimeType, ext)){
// Save using a hidden browser
var nativeHandlerImport = function () {
var browser = Zotero.Browser.createHiddenBrowser();
var imported = false;
var onpageshow = function() {
@ -254,10 +230,10 @@ Zotero.Attachments = new function(){
};
browser.addEventListener("pageshow", onpageshow, false);
browser.loadURI(url);
}
};
// Otherwise use a remote web page persist
else {
// Save using remote web browser persist
var externalHandlerImport = function (mimeType) {
if (forceFileBaseName) {
var ext = _getExtensionFromURL(url, mimeType);
var fileName = forceFileBaseName + (ext != '' ? '.' + ext : '');
@ -360,7 +336,12 @@ Zotero.Attachments = new function(){
Zotero.Notifier.enable();
}
var nsIURL = Components.classes["@mozilla.org/network/standard-url;1"]
.createInstance(Components.interfaces.nsIURL);
nsIURL.spec = url;
wbp.saveURI(nsIURL, null, null, null, null, file);
return attachmentItem;
}
catch (e){
Zotero.DB.rollbackTransaction();
@ -379,8 +360,28 @@ Zotero.Attachments = new function(){
throw (e);
}
}
var process = function (mimeType, hasNativeHandler) {
// If we can load this natively, use a hidden browser
// (so we can get the charset and title and index the document)
if (hasNativeHandler) {
nativeHandlerImport();
}
// Otherwise use a remote web page persist
else {
return externalHandlerImport(mimeType);
}
}
if (mimeType) {
return process(mimeType);
}
else {
Zotero.MIME.getMIMETypeFromURL(url, function (mimeType, hasNativeHandler) {
process(mimeType, hasNativeHandler);
});
}
}
/*
@ -425,9 +426,7 @@ Zotero.Attachments = new function(){
if (!mimeType) {
// If we don't have the MIME type, do a HEAD request for it
Zotero.Utilities.HTTP.doHead(url, function(obj){
var mimeType = obj.channel.contentType;
Zotero.MIME.getMIMETypeFromURL(url, function (mimeType) {
if (mimeType) {
var disabled = Zotero.Notifier.disable();

View file

@ -275,6 +275,38 @@ Zotero.MIME = new function(){
}
this.getMIMETypeFromURL = function (url, callback) {
Zotero.Utilities.HTTP.doHead(url, function(xmlhttp) {
if (xmlhttp.status != 200 && xmlhttp.status != 204) {
Zotero.debug("Attachment HEAD request returned with status code "
+ xmlhttp.status + " in Zotero.MIME.getMIMETypeFromURL()", 2);
var mimeType = '';
}
else {
var mimeType = xmlhttp.channel.contentType;
}
var nsIURL = Components.classes["@mozilla.org/network/standard-url;1"]
.createInstance(Components.interfaces.nsIURL);
nsIURL.spec = url;
// Override MIME type to application/pdf if extension is .pdf --
// workaround for sites that respond to the HEAD request with an
// invalid MIME type (https://www.zotero.org/trac/ticket/460)
//
// Downloaded file is inspected in attachment code and deleted if actually HTML
if (nsIURL.fileName.match(/pdf$/) || url.match(/pdf$/)) {
mimeType = 'application/pdf';
}
var ext = nsIURL.fileExtension;
var hasNativeHandler = Zotero.MIME.hasNativeHandler(mimeType, ext)
callback(mimeType, hasNativeHandler);
});
}
/*
* Determine if a MIME type can be handled natively
* or if it needs to be passed off to a plugin or external helper app