mirror of
https://github.com/fergalmoran/chrometophone.git
synced 2025-12-22 09:41:51 +00:00
132 lines
3.4 KiB
JavaScript
132 lines
3.4 KiB
JavaScript
"use strict";
|
|
|
|
let Cc = Components.classes;
|
|
let 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 gUploadListener = {
|
|
UploadsView: null,
|
|
|
|
fileAdded: function(data)
|
|
{
|
|
this.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
|
|
FoxToPhoneUploadWindow.updateStatus(item);
|
|
},
|
|
fileFinished: function(data)
|
|
{
|
|
let item = document.getElementById("upl" + data.id);
|
|
this.UploadsView.removeChild(item);
|
|
|
|
// If no more pending uploads, close the tab.
|
|
// Use a 0 ms timeout to avoid flicker while compress -> upload a folder
|
|
let checkTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
|
|
checkTimer.initWithCallback( this.checkTimerEvent, 0, Ci.nsITimer.TYPE_ONE_SHOT );
|
|
},
|
|
|
|
checkTimerEvent :
|
|
{
|
|
notify: function(timer)
|
|
{
|
|
if (!sendtophoneUploadsManager.isWindowNeeded())
|
|
{
|
|
if (gUploadListener.UploadsView.children.length==0)
|
|
window.close();
|
|
}
|
|
}
|
|
},
|
|
|
|
addFile: function(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);
|
|
|
|
this.UploadsView.appendChild( dl );
|
|
}
|
|
|
|
};
|
|
|
|
let FoxToPhoneUploadWindow = {
|
|
UploadManager: null,
|
|
|
|
Startup: function()
|
|
{
|
|
this.UploadManager = sendtophoneUploadsManager;
|
|
|
|
gUploadListener.UploadsView = document.getElementById("UploadsBox");
|
|
|
|
this.UploadManager.addListener(gUploadListener);
|
|
|
|
for (let id in this.UploadManager.uploads)
|
|
gUploadListener.addFile( this.UploadManager.uploads[id] );
|
|
},
|
|
|
|
Shutdown: function()
|
|
{
|
|
this.UploadManager.removeListener(gUploadListener);
|
|
},
|
|
|
|
performCancelCommand: function(aItem)
|
|
{
|
|
let elm = aItem;
|
|
|
|
while (elm.nodeName != "richlistitem" ||
|
|
elm.getAttribute("type") != "upload")
|
|
elm = elm.parentNode;
|
|
|
|
if (elm.inProgress)
|
|
this.UploadManager.cancelUpload( parseInt(elm.getAttribute("uploadId"), 10) );
|
|
},
|
|
|
|
updateStatus: function(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);
|
|
}
|
|
|
|
};
|
|
|
|
|