commit c6be453564944fff1a207dbe260e3f3ec3f0486d
parent 5c02a81e816854b950a12e0b11c74b11a8f3844e
Author: Simon Kornblith <simon@simonster.com>
Date: Sun, 10 Jun 2012 22:36:00 -0400
Attachment progress in connector
Diffstat:
2 files changed, 78 insertions(+), 6 deletions(-)
diff --git a/chrome/content/zotero/xpcom/connector/translate_item.js b/chrome/content/zotero/xpcom/connector/translate_item.js
@@ -79,10 +79,23 @@ Zotero.Translate.ItemSaver.prototype = {
payload.cookie = this._cookie;
}
- Zotero.Connector.callMethod("saveItems", payload, function(success, status) {
- if(success !== false) {
+ Zotero.Connector.callMethod("saveItems", payload, function(data, status) {
+ if(data !== false) {
Zotero.debug("Translate: Save via Standalone succeeded");
+ var haveAttachments = false;
+ if(data.items) {
+ for(var i=0; i<data.items.length; i++) {
+ var attachments = items[i].attachments = data.items[i].attachments;
+ for(var j=0; j<attachments.length; j++) {
+ if(attachments[j].id) {
+ attachmentCallback(attachments[j], 0);
+ haveAttachments = true;
+ }
+ }
+ }
+ }
callback(true, items);
+ if(haveAttachments) me._pollForProgress(items, attachmentCallback);
} else if(Zotero.isFx) {
callback(false, new Error("Save via Standalone failed with "+status));
} else {
@@ -92,6 +105,60 @@ Zotero.Translate.ItemSaver.prototype = {
},
/**
+ * Polls for updates to attachment progress
+ * @param items Items in Zotero.Item.toArray() format
+ * @param {Function} attachmentCallback A callback that receives information about attachment
+ * save progress. The callback will be called as attachmentCallback(attachment, false, error)
+ * on failure or attachmentCallback(attachment, progressPercent) periodically during saving.
+ * attachmentCallback() will be called with all attachments that will be saved
+ */
+ "_pollForProgress":function(items, attachmentCallback) {
+ var attachments = [];
+ var progressIDs = [];
+ var previousStatus = [];
+ for(var i=0; i<items.length; i++) {
+ var itemAttachments = items[i].attachments;
+ for(var j=0; j<itemAttachments.length; j++) {
+ if(itemAttachments[j].id) {
+ attachments.push(itemAttachments[j]);
+ progressIDs.push(itemAttachments[j].id);
+ previousStatus.push(0);
+ }
+ }
+ }
+
+ var nPolls = 0;
+ var poll = function() {
+ Zotero.Connector.callMethod("attachmentProgress", progressIDs, function(currentStatus, status) {
+ if(currentStatus) {
+ for(var i=0; i<attachments.length; i++) {
+ if(currentStatus[i] === 100 || currentStatus[i] === false) {
+ attachmentCallback(attachments[i], currentStatus[i]);
+ attachments.splice(i, 1);
+ progressIDs.splice(i, 1);
+ previousStatus.splice(i, 1);
+ currentStatus.splice(i, 1);
+ i--;
+ } else if(currentStatus[i] !== previousStatus[i]) {
+ attachmentCallback(attachments[i], currentStatus[i]);
+ previousStatus[i] = currentStatus[i];
+ }
+ }
+
+ if(nPolls++ < 60 && attachments.length) {
+ setTimeout(poll, 1000);
+ }
+ } else {
+ for(var i=0; i<attachments.length; i++) {
+ attachmentCallback(attachments[i], false, "Lost connection to Zotero Standalone");
+ }
+ }
+ });
+ };
+ poll();
+ },
+
+ /**
* Saves items to server
* @param items Items in Zotero.Item.toArray() format
* @param {Function} callback A callback to be executed when saving is complete. If saving
@@ -101,6 +168,7 @@ Zotero.Translate.ItemSaver.prototype = {
* @param {Function} attachmentCallback A callback that receives information about attachment
* save progress. The callback will be called as attachmentCallback(attachment, false, error)
* on failure or attachmentCallback(attachment, progressPercent) periodically during saving.
+ * attachmentCallback() will be called with all attachments that will be saved
*/
"_saveToServer":function(items, callback, attachmentCallback) {
var newItems = [], typedArraysSupported = false;
@@ -115,7 +183,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<item.attachments.length; j++) {
- if(item.attachments[j].url && item.attachments[j].mimeType !== "text/html") {
+ if(!item.attachments[j].url || item.attachments[j].mimeType === "text/html") {
item.attachments.splice(j--, 1);
} else {
item.attachments[j].id = Zotero.Utilities.randomString();
diff --git a/chrome/content/zotero/xpcom/server_connector.js b/chrome/content/zotero/xpcom/server_connector.js
@@ -30,7 +30,7 @@ Zotero.Server.Connector.Data = {};
Zotero.Server.Connector.AttachmentProgressManager = new function() {
var attachmentsInProgress = new WeakMap(),
attachmentProgress = {},
- i = 1;
+ id = 1;
/**
* Adds attachments to attachment progress manager
@@ -38,9 +38,9 @@ Zotero.Server.Connector.AttachmentProgressManager = new function() {
this.add = function(attachments) {
for(var i=0; i<attachments.length; i++) {
var attachment = attachments[i];
- attachmentsInProgress.set(attachment, (attachment.id = i++));
+ attachmentsInProgress.set(attachment, (attachment.id = id++));
}
- }
+ };
/**
* Called on attachment progress
@@ -275,6 +275,7 @@ Zotero.Server.Connector.SavePage.prototype = {
if(collection) {
collection.addItem(item.id);
}
+ Zotero.Server.Connector.AttachmentProgressManager.add(jsonItem.attachments);
jsonItems.push(jsonItem);
});
@@ -329,6 +330,9 @@ Zotero.Server.Connector.SaveItem.prototype = {
var cookieSandbox = data["uri"] && data["cookie"] ? new Zotero.CookieSandbox(null, data["uri"],
data["cookie"]) : null;
+ for(var i=0; i<data.items.length; i++) {
+ Zotero.Server.Connector.AttachmentProgressManager.add(data.items[i].attachments);
+ }
// save items
var itemSaver = new Zotero.Translate.ItemSaver(libraryID,