diff --git a/chrome/content/zotero/browser.js b/chrome/content/zotero/browser.js index e02fcfe1a..d99657f89 100644 --- a/chrome/content/zotero/browser.js +++ b/chrome/content/zotero/browser.js @@ -50,8 +50,6 @@ var Zotero_Browser = new function() { this.tabClose = tabClose; this.resize = resize; this.updateStatus = updateStatus; - this.finishScraping = finishScraping; - this.itemDone = itemDone; this.tabbrowser = null; this.appcontent = null; @@ -447,51 +445,6 @@ var Zotero_Browser = new function() { } } - /* - * Callback to be executed when scraping is complete - */ - function finishScraping(obj, returnValue) { - if(!returnValue) { - Zotero_Browser.progress.show(); - Zotero_Browser.progress.changeHeadline(Zotero.getString("ingester.scrapeError")); - // Include link to Known Translator Issues page - var url = "http://www.zotero.org/documentation/known_translator_issues"; - var linkText = '' - + Zotero.getString('ingester.scrapeErrorDescription.linkText') + ''; - Zotero_Browser.progress.addDescription(Zotero.getString("ingester.scrapeErrorDescription", linkText)); - Zotero_Browser.progress.startCloseTimer(8000); - } else { - Zotero_Browser.progress.startCloseTimer(); - } - Zotero_Browser.isScraping = false; - } - - - /* - * Callback to be executed when an item has been finished - */ - function itemDone(obj, dbItem, item, collection) { - var title = item.title; - var icon = Zotero.ItemTypes.getImageSrc(item.itemType); - Zotero_Browser.progress.show(); - Zotero_Browser.progress.changeHeadline(Zotero.getString("ingester.scraping")); - Zotero_Browser.progress.addLines([title], [icon]); - - // add item to collection, if one was specified - if(collection) { - collection.addItem(dbItem.id); - } - - if(Zotero_Browser.isScraping) { - // initialize close timer between item saves in case translator doesn't call done - Zotero_Browser.progress.startCloseTimer(10000); // is this long enough? - } else { - // if we aren't supposed to be scraping now, the translator is broken; assume we're - // done - Zotero_Browser.progress.startCloseTimer(); - } - } - /** * Called when status bar icon is right-clicked */ @@ -725,6 +678,28 @@ Zotero_Browser.Tab.prototype.translate = function(libraryID, collectionID, trans var collection = false; } + if(Zotero.isConnector) { + Zotero.Connector.callMethod("getSelectedCollection", {}, function(response, status) { + if(status !== 200) return; + Zotero_Browser.progress.changeHeadline(Zotero.getString("ingester.scrapingTo"), + "chrome://zotero/skin/treesource-"+(response.id ? "collection" : "library")+".png", + response.name+"\u2026"); + }); + } else { + var name; + if(collection) { + name = collection.name; + } else if(libraryID) { + name = Zotero.Libraries.getName(libraryID); + } else { + name = Zotero.getString("pane.collections.library"); + } + + Zotero_Browser.progress.changeHeadline(Zotero.getString("ingester.scrapingTo"), + "chrome://zotero/skin/treesource-"+(collection ? "collection" : "library")+".png", + name+"\u2026"); + } + var me = this; // use first translator available @@ -733,8 +708,54 @@ Zotero_Browser.Tab.prototype.translate = function(libraryID, collectionID, trans this.page.translate.clearHandlers("done"); this.page.translate.clearHandlers("itemDone"); - this.page.translate.setHandler("done", function(obj, item) { Zotero_Browser.finishScraping(obj, item) }); - this.page.translate.setHandler("itemDone", function(obj, dbItem, item) { Zotero_Browser.itemDone(obj, dbItem, item, collection) }); + this.page.translate.setHandler("done", function(obj, returnValue) { + if(!returnValue) { + Zotero_Browser.progress.show(); + Zotero_Browser.progress.changeHeadline(Zotero.getString("ingester.scrapeError")); + // Include link to Known Translator Issues page + var url = "http://www.zotero.org/documentation/known_translator_issues"; + var linkText = '' + + Zotero.getString('ingester.scrapeErrorDescription.linkText') + ''; + Zotero_Browser.progress.addDescription(Zotero.getString("ingester.scrapeErrorDescription", linkText)); + Zotero_Browser.progress.startCloseTimer(8000); + } else { + Zotero_Browser.progress.startCloseTimer(); + } + Zotero_Browser.isScraping = false; + }); + + var attachmentsMap = new WeakMap(); + + this.page.translate.setHandler("itemDone", function(obj, dbItem, item) { + Zotero_Browser.progress.show(); + var itemProgress = new Zotero_Browser.progress.ItemProgress(Zotero.ItemTypes.getImageSrc(item.itemType), + item.title); + itemProgress.setProgress(100); + for(var i=0; i - - diff --git a/chrome/content/zotero/xpcom/attachments.js b/chrome/content/zotero/xpcom/attachments.js index ec98767d9..76c357c4f 100644 --- a/chrome/content/zotero/xpcom/attachments.js +++ b/chrome/content/zotero/xpcom/attachments.js @@ -220,6 +220,7 @@ Zotero.Attachments = new function(){ var urlRe = /^https?:\/\/[^\s]*$/; var matches = urlRe.exec(url); if (!matches) { + if(callback) callback(false); throw ("Invalid URL '" + url + "' in Zotero.Attachments.importFromURL()"); } @@ -297,9 +298,11 @@ Zotero.Attachments = new function(){ if (mimeType == 'application/pdf' && Zotero.MIME.sniffForMIMEType(str) != 'application/pdf') { - Zotero.debug("Downloaded PDF did not have MIME type " - + "'application/pdf' in Attachments.importFromURL()", 2); + var errString = "Downloaded PDF did not have MIME type " + + "'application/pdf' in Attachments.importFromURL()"; + Zotero.debug(errString, 2); attachmentItem.erase(); + if(callback) callback(false, new Error(errString)); return; } @@ -311,6 +314,8 @@ Zotero.Attachments = new function(){ Zotero.Notifier.trigger('add', 'item', itemID); Zotero.Notifier.trigger('modify', 'item', sourceItemID); + + if(callback) callback(attachmentItem); // We don't have any way of knowing that the file // is flushed to disk, so we just wait a second @@ -325,6 +330,7 @@ Zotero.Attachments = new function(){ catch (e) { // Clean up attachmentItem.erase(); + if(callback) callback(false, e); throw (e); } @@ -346,8 +352,6 @@ Zotero.Attachments = new function(){ nsIURL.spec = url; wbp.saveURI(nsIURL, null, null, null, null, file); - if(callback) callback(attachmentItem); - return attachmentItem; } catch (e){ @@ -553,7 +557,7 @@ Zotero.Attachments = new function(){ Zotero.Fulltext.indexDocument(document, itemID); Zotero.Notifier.trigger('refresh', 'item', itemID); if (callback) { - callback(); + callback(attachmentItem); } }; } @@ -612,6 +616,7 @@ Zotero.Attachments = new function(){ // Clean up var item = Zotero.Items.get(itemID); item.erase(); + if(callback) callback(false, e); throw (e); } diff --git a/chrome/content/zotero/xpcom/connector/translate_item.js b/chrome/content/zotero/xpcom/connector/translate_item.js index e0dbcd74a..5d224e3cb 100644 --- a/chrome/content/zotero/xpcom/connector/translate_item.js +++ b/chrome/content/zotero/xpcom/connector/translate_item.js @@ -115,7 +115,7 @@ Zotero.Translate.ItemSaver.prototype = { if(typedArraysSupported) { // Get rid of attachments that we won't be able to save properly and add ids for(var j=0; j elements are turned into XUL links */ - function addDescription(text) { - if(_windowLoaded) { - var newHB = _progressWindow.document.createElement("hbox"); - newHB.setAttribute("class", "zotero-progress-item-hbox"); - var newDescription = _progressWindow.document.createElement("description"); - - var parts = Zotero.Utilities.parseMarkup(text); - for each(var part in parts) { - if (part.type == 'text') { - var elem = _progressWindow.document.createTextNode(part.text); + this.addDescription = _deferUntilWindowLoad(function addDescription(text) { + var newHB = _progressWindow.document.createElement("hbox"); + newHB.setAttribute("class", "zotero-progress-item-hbox"); + var newDescription = _progressWindow.document.createElement("description"); + + var parts = Zotero.Utilities.parseMarkup(text); + for each(var part in parts) { + if (part.type == 'text') { + var elem = _progressWindow.document.createTextNode(part.text); + } + else if (part.type == 'link') { + var elem = _progressWindow.document.createElement('label'); + elem.setAttribute('value', part.text); + elem.setAttribute('class', 'zotero-text-link'); + for (var i in part.attributes) { + elem.setAttribute(i, part.attributes[i]); } - else if (part.type == 'link') { - var elem = _progressWindow.document.createElement('label'); - elem.setAttribute('value', part.text); - elem.setAttribute('class', 'zotero-text-link'); - for (var i in part.attributes) { - elem.setAttribute(i, part.attributes[i]); - } - } - - newDescription.appendChild(elem); } - newHB.appendChild(newDescription); - _progressWindow.document.getElementById("zotero-progress-text-box").appendChild(newHB); - - _move(); - } else { - _loadDescription = text; + newDescription.appendChild(elem); } - } + + newHB.appendChild(newDescription); + _progressWindow.document.getElementById("zotero-progress-text-box").appendChild(newHB); + + _move(); + }); - - function startCloseTimer(ms, requireMouseOver) { + /** + * Sets a timer to close the progress window. If a previous close timer was set, + * clears it. + * @param {Integer} ms The number of milliseconds to wait before closing the progress + * window. + * @param {Boolean} [requireMouseOver] If true, wait until the mouse has touched the + * window before closing. + */ + this.startCloseTimer = function startCloseTimer(ms, requireMouseOver) { if (_windowLoaded || _windowLoading) { if (requireMouseOver && !_mouseWasOver) { return; @@ -261,10 +254,14 @@ Zotero.ProgressWindow = function(_window){ } _timeoutID = _progressWindow.setTimeout(_timeout, ms); + _closing = true; } } - function close() { + /** + * Immediately closes the progress window if it is open. + */ + this.close = function close() { _disableTimeout(); _windowLoaded = false; _windowLoading = false; @@ -275,23 +272,104 @@ Zotero.ProgressWindow = function(_window){ } catch(ex) {} } + /** + * Creates a new object representing a line in the progressWindow. This is the OO + * version of addLines() above. + */ + this.ItemProgress = _deferUntilWindowLoad(function(iconSrc, title, parentItemProgress) { + this._itemText = _progressWindow.document.createElement("description"); + this._itemText.appendChild(_progressWindow.document.createTextNode(title)); + this._itemText.setAttribute("class", "zotero-progress-item-label"); + this._itemText.setAttribute("crop", "end"); + + this._image = _progressWindow.document.createElement("hbox"); + this._image.setAttribute("class", "zotero-progress-item-icon"); + this._image.setAttribute("flex", 0); + this._image.style.width = "16px"; + this._image.style.backgroundRepeat = "no-repeat"; + this.setIcon(iconSrc); + + this._hbox = _progressWindow.document.createElement("hbox"); + this._hbox.setAttribute("class", "zotero-progress-item-hbox"); + if(parentItemProgress) { + this._hbox.style.marginLeft = "16px"; + this._hbox.zoteroIsChildItem; + } else { + this._hbox.setAttribute("parent", "true"); + } + this._hbox.style.opacity = "0.5"; + + this._hbox.appendChild(this._image); + this._hbox.appendChild(this._itemText); + + var container = _progressWindow.document.getElementById("zotero-progress-text-box"); + if(parentItemProgress) { + var nextItem = parentItemProgress._hbox.nextSibling; + while(nextItem && nextItem.zoteroIsChildItem) { + nextItem = nextItem.nextSibling; + } + container.insertBefore(this._hbox, nextItem); + } else { + container.appendChild(this._hbox); + } + + _move(); + }); + + /** + * Sets the current save progress for this item. + * @param {Integer} percent A percentage from 0 to 100. + */ + this.ItemProgress.prototype.setProgress = _deferUntilWindowLoad(function(percent) { + if(percent != 0 && percent != 100) { + // Indication of partial progress, so we will use the circular indicator + this._image.style.backgroundImage = "url('chrome://zotero/skin/progress_arcs.png')"; + this._image.style.backgroundPosition = "-"+(Math.round(percent/100*nArcs)*16)+"px 0"; + this._hbox.style.opacity = percent/200+.5; + this._hbox.style.filter = "alpha(opacity = "+(percent/2+50)+")"; + } else if(percent == 100) { + this._image.style.backgroundImage = "url('"+this._iconSrc+"')"; + this._image.style.backgroundPosition = ""; + this._hbox.style.opacity = "1"; + this._hbox.style.filter = ""; + } + }); + + /** + * Sets the icon for this item. + * @param {Integer} percent A percentage from 0 to 100. + */ + this.ItemProgress.prototype.setIcon = _deferUntilWindowLoad(function(iconSrc) { + this._image.style.backgroundImage = "url('"+iconSrc+"')"; + this._image.style.backgroundPosition = ""; + this._iconSrc = iconSrc; + }); + + /** + * Indicates that an error occurred saving this item. + */ + this.ItemProgress.prototype.setError = _deferUntilWindowLoad(function() { + this._image.style.backgroundImage = "url('chrome://zotero/skin/cross.png')"; + this._image.style.backgroundPosition = ""; + this._itemText.style.color = "red"; + this._hbox.style.opacity = "1"; + this._hbox.style.filter = ""; + }); + function _onWindowLoaded() { _windowLoading = false; _windowLoaded = true; _move(); - // do things we delayed because the window was loading - changeHeadline(_loadHeadline); - addLines(_loadLines, _loadIcons); - if (_loadDescription) { - addDescription(_loadDescription); - } - // reset parameters - _loadHeadline = ''; - _loadLines = []; - _loadIcons = []; - _loadDescription = null; + // do things we delayed because the window was loading + for(var i=0; i<_deferredUntilWindowLoad.length; i++) { + _deferredUntilWindowLoad[i].apply(_deferredUntilWindowLoadThis[i], + _deferredUntilWindowLoadArgs[i]); + } + _deferredUntilWindowLoad = []; + _deferredUntilWindowLoadThis = []; + _deferredUntilWindowLoadArgs = []; } function _move() { @@ -303,8 +381,8 @@ Zotero.ProgressWindow = function(_window){ } function _timeout() { - close(); // could check to see if we're really supposed to close yet - // (in case multiple scrapers are operating at once) + self.close(); // could check to see if we're really supposed to close yet + // (in case multiple scrapers are operating at once) _timeoutID = false; } @@ -319,7 +397,6 @@ Zotero.ProgressWindow = function(_window){ _timeoutID = false; } - /* * Disable the close timer when the mouse is over the window */ @@ -328,8 +405,7 @@ Zotero.ProgressWindow = function(_window){ _disableTimeout(); } - - /* + /** * Start the close timer when the mouse leaves the window * * Note that this onmouseout doesn't work correctly on popups in Fx2, @@ -345,11 +421,27 @@ Zotero.ProgressWindow = function(_window){ && (e.screenY >= top) && e.screenY <= (top + this.outerHeight)) { return; } - startCloseTimer(); + if(_closing) self.startCloseTimer(); } - function _onMouseUp(e) { - close(); + self.close(); + } + + /** + * Wraps a function to ensure it isn't called until the window is loaded + */ + function _deferUntilWindowLoad(fn) { + return function() { + if(_window.closed) return; + + if(_windowLoaded) { + fn.apply(this, Array.prototype.slice.call(arguments)); + } else { + _deferredUntilWindowLoad.push(fn); + _deferredUntilWindowLoadThis.push(this); + _deferredUntilWindowLoadArgs.push(Array.prototype.slice.call(arguments)); + } + } } } diff --git a/chrome/content/zotero/xpcom/server_connector.js b/chrome/content/zotero/xpcom/server_connector.js index d259249d0..2f8eb27f3 100755 --- a/chrome/content/zotero/xpcom/server_connector.js +++ b/chrome/content/zotero/xpcom/server_connector.js @@ -27,6 +27,35 @@ const CONNECTOR_API_VERSION = 2; Zotero.Server.Connector = function() {}; Zotero.Server.Connector._waitingForSelection = {}; Zotero.Server.Connector.Data = {}; +Zotero.Server.Connector.AttachmentProgressManager = new function() { + var attachmentsInProgress = new WeakMap(), + attachmentProgress = {}, + i = 1; + + /** + * Adds attachments to attachment progress manager + */ + this.add = function(attachments) { + for(var i=0; i