From d34fe5b69f0d1200fc5c46e5772c0cb773ecdb39 Mon Sep 17 00:00:00 2001 From: amla70 Date: Sun, 8 Aug 2010 10:17:18 +0000 Subject: [PATCH] Uploads manager. Based on the default Downloads Manager. --- .../chrome/content/ff-overlay.js | 7 +- .../chrome/content/upload.css | 16 ++ .../chrome/content/upload.xml | 100 ++++++++++ .../chrome/content/uploads.js | 160 ++++++++++++++++ .../chrome/content/uploads.xul | 31 ++++ .../chrome/locale/en/uploads.dtd | 3 + .../chrome/locale/es/overlay.properties | 4 +- .../chrome/locale/es/uploads.dtd | 3 + .../chrome/skin/uploads.css | 63 +++++++ .../modules/sendtophone.js | 74 ++++++-- .../modules/uploadsManager.js | 172 ++++++++++++++++++ 11 files changed, 609 insertions(+), 24 deletions(-) create mode 100644 third_party/firefox_sendtophone/chrome/content/upload.css create mode 100644 third_party/firefox_sendtophone/chrome/content/upload.xml create mode 100644 third_party/firefox_sendtophone/chrome/content/uploads.js create mode 100644 third_party/firefox_sendtophone/chrome/content/uploads.xul create mode 100644 third_party/firefox_sendtophone/chrome/locale/en/uploads.dtd create mode 100644 third_party/firefox_sendtophone/chrome/locale/es/uploads.dtd create mode 100644 third_party/firefox_sendtophone/chrome/skin/uploads.css create mode 100644 third_party/firefox_sendtophone/modules/uploadsManager.js diff --git a/third_party/firefox_sendtophone/chrome/content/ff-overlay.js b/third_party/firefox_sendtophone/chrome/content/ff-overlay.js index eab4333..bf288fe 100644 --- a/third_party/firefox_sendtophone/chrome/content/ff-overlay.js +++ b/third_party/firefox_sendtophone/chrome/content/ff-overlay.js @@ -108,7 +108,7 @@ sendtophone.checkDrag = function(event) sendtophone.doDrop = function(event) { - var dt = event.dataTransfer + var dt = event.dataTransfer; var types = dt.types; var supportedTypes = ["application/x-moz-file", "text/x-moz-url", "text/uri-list", "text/plain"]; types = supportedTypes.filter(function (value) types.contains(value)); @@ -137,10 +137,8 @@ sendtophone.doDrop = function(event) for (var i = 0; i < dt.mozItemCount; i++) { var file = dt.mozGetDataAt("application/x-moz-file", i); - if (file instanceof Components.interfaces.nsIFile ) - { + if (file instanceof Ci.nsIFile ) sendtophoneCore.sendFile(file); - } else this.alert(this.strings.getString("InvalidFile")); } @@ -172,3 +170,4 @@ sendtophone.pickFile = function(folder) } } + diff --git a/third_party/firefox_sendtophone/chrome/content/upload.css b/third_party/firefox_sendtophone/chrome/content/upload.css new file mode 100644 index 0000000..86fe300 --- /dev/null +++ b/third_party/firefox_sendtophone/chrome/content/upload.css @@ -0,0 +1,16 @@ + +richlistitem[type="upload"][state="0"] { + -moz-binding: url('chrome://sendtophone/content/upload.xml#uploading'); + -moz-box-orient: vertical; +} + +richlistitem[type="upload"][state="1"] { + -moz-binding: url('chrome://sendtophone/content/upload.xml#compressing'); + -moz-box-orient: vertical; +} + +/* Only focus buttons in the selected item*/ +richlistitem[type="upload"]:not([selected="true"]) button { + -moz-user-focus: none; +} + diff --git a/third_party/firefox_sendtophone/chrome/content/upload.xml b/third_party/firefox_sendtophone/chrome/content/upload.xml new file mode 100644 index 0000000..c5340e9 --- /dev/null +++ b/third_party/firefox_sendtophone/chrome/content/upload.xml @@ -0,0 +1,100 @@ + + + + + %downloadDTD; +]> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/third_party/firefox_sendtophone/chrome/content/uploads.js b/third_party/firefox_sendtophone/chrome/content/uploads.js new file mode 100644 index 0000000..1e0a7b7 --- /dev/null +++ b/third_party/firefox_sendtophone/chrome/content/uploads.js @@ -0,0 +1,160 @@ +var Cc = Components.classes; +var Ci = Components.interfaces; +let Cu = Components.utils; +Cu.import("resource://gre/modules/DownloadUtils.jsm"); +Cu.import("resource://gre/modules/PluralForm.jsm"); + +Cu.import("resource://sendtophone/uploadsManager.js"); + +let gUploadManager = sendtophoneUploadsManager; +let gUploadsView = null; + +function addFile(upload) +{ + let dl = document.createElement("richlistitem"); + + dl.setAttribute("file", upload.file.path); + dl.setAttribute("target", upload.file.leafName); + dl.setAttribute("image", "moz-icon://" + upload.file.path + "?size=32"); + + dl.setAttribute("state", upload.state); + dl.setAttribute("startTime", upload.startTime); + dl.setAttribute("currBytes", upload.currBytes); + dl.setAttribute("maxBytes", upload.maxBytes); + dl.setAttribute("lastSeconds", Infinity); + + // Initialize other attributes + dl.setAttribute("type", "upload"); + dl.setAttribute("id", "upl" + upload.id); + dl.setAttribute("uploadId", upload.id); + + gUploadsView.appendChild( dl ); +} + +function checkPendingUploads() +{ + if (gUploadsView.children.length==0) + window.close(); +} + +function cancelUpload(item) +{ + gUploadManager.cancelUpload( parseInt(item.getAttribute("uploadId"), 10) ); +} + +let gUploadListener = { + fileAdded: function(data) + { + addFile(data); + }, + progressUpdate: function(data) + { + let item = document.getElementById( "upl" + data.id); + item.setAttribute("currBytes", data.currBytes); + item.setAttribute("maxBytes", data.maxBytes); + + let percentComplete = Math.round(100 * data.currBytes / data.maxBytes); + item.setAttribute("progress", percentComplete); + + // Status text + updateStatus(item); + }, + fileFinished: function(data) + { + let item = document.getElementById("upl" + data.id); + gUploadsView.removeChild(item); + + // If no more pending uploads, close the tab. + // Use a 100ms timeout to avoid flicker while compress -> upload a folder + window.setTimeout( checkPendingUploads, 100); + } +}; + + +function Startup() +{ + gUploadsView = document.getElementById("UploadsBox"); + + gUploadManager.addListener(gUploadListener); + + for (let id in gUploadManager.uploads) + addFile( gUploadManager.uploads[id] ); + +} + +function Shutdown() +{ + gUploadManager.removeListener(gUploadListener); +} + + +//////////////////////////////////////////////////////////////////////////////// +//// Command Updating and Command Handlers + +var gUploadViewController = { + isCommandEnabled: function(aCommand, aItem) + { + let dl = aItem; + + switch (aCommand) { + case "cmd_cancel": + return dl.inProgress; + } + return false; + }, + + doCommand: function(aCommand, aItem) + { + if (this.isCommandEnabled(aCommand, aItem)) + this.commands[aCommand](aItem); + }, + + commands: { + cmd_cancel: function(aSelectedItem) { + cancelUpload(aSelectedItem); + } + } +}; + +/** + * Helper function to do commands. + * + * @param aCmd + * The command to be performed. + * @param aItem + * The richlistitem that represents the download that will have the + * command performed on it. If this is null, the command is performed on + * all downloads. If the item passed in is not a richlistitem that + * represents a download, it will walk up the parent nodes until it finds + * a DOM node that is. + */ +function performCommand(aCmd, aItem) +{ + let elm = aItem; + + while (elm.nodeName != "richlistitem" || + elm.getAttribute("type") != "upload") + elm = elm.parentNode; + + gUploadViewController.doCommand(aCmd, elm); +} + +function updateStatus(aItem) +{ + let currBytes = Number(aItem.getAttribute("currBytes")); + let maxBytes = Number(aItem.getAttribute("maxBytes")); + + let elapsedTime = (Date.now() - Number(aItem.getAttribute("startTime"))) / 1000; + // If we don't have an active upload, assume 0 bytes/sec + let speed = (currBytes>0) ? currBytes/elapsedTime : 0; + let lastSec = Number(aItem.getAttribute("lastSeconds")); + + let status, newLast; + [status, newLast] = + DownloadUtils.getDownloadStatus(currBytes, maxBytes, speed, lastSec); + + // Update lastSeconds to be the new value + aItem.setAttribute("lastSeconds", newLast); + + aItem.setAttribute("status", status); +} diff --git a/third_party/firefox_sendtophone/chrome/content/uploads.xul b/third_party/firefox_sendtophone/chrome/content/uploads.xul new file mode 100644 index 0000000..bbd27ad --- /dev/null +++ b/third_party/firefox_sendtophone/chrome/content/uploads.xul @@ -0,0 +1,31 @@ + + + + + + + + + + + +