FoxToPhone: allow to customize the file upload server. Added support for min.us. Reorganization of the uploads pane.

This commit is contained in:
amla70
2011-07-08 18:01:28 +00:00
parent c8c580aeb9
commit 87f0af9241
13 changed files with 408 additions and 249 deletions

View File

@@ -24,3 +24,5 @@ overlay chrome://navigator/content/navigator.xul chrome://sendtophone/content/ff
style chrome://global/content/customizeToolbar.xul chrome://sendtophone/skin/overlay.css
# Support for Mac
override chrome://sendtophone/skin/uploads.css chrome://sendtophone/skin/uploads-mac.css os=Darwin

View File

@@ -181,7 +181,7 @@ sendtophone.pickFile = function(folder)
else
{
fp.init(window, this.getString("SendFileToPhone"), Ci.nsIFilePicker.modeOpenMultiple);
fp.appendFilters(Ci.nsIFilePicker.filterAll | Ci.nsIFilePicker.filterImages);
fp.appendFilters( Ci.nsIFilePicker.filterAll );
}
var rv = fp.show();

View File

@@ -0,0 +1,55 @@
"use strict";
let foxToPhonePreferences =
{
load: function()
{
let fileServerUrl = document.getElementById("extensions.sendtophone.fileServerUrl").value;
let fileserverMenuList = document.getElementById("extensionsSendToPhoneFileServer") ;
switch (fileServerUrl)
{
case '':
fileserverMenuList.value = fileServerUrl;
break;
case 'http://min.us':
fileserverMenuList.value = fileServerUrl;
break;
default:
fileserverMenuList.value = 'Custom';
break;
}
fileserverMenuList.addEventListener("command", function () {
let fileServer = fileserverMenuList.value;
switch (fileServer)
{
case '':
document.getElementById("extensions.sendtophone.fileServerUrl").value = '';
break;
case 'Custom':
break;
default:
document.getElementById("extensions.sendtophone.fileServerUrl").value = fileServer;
break;
}
document.getElementById("hboxFileServerUrl").hidden = ( fileServer != 'Custom');
window.sizeToContent();
}, false);
document.getElementById("hboxFileServerUrl").hidden = ( fileserverMenuList.value != 'Custom');
window.sizeToContent();
}
} ;
this.addEventListener("load", function () {foxToPhonePreferences.load(); }, false);

View File

@@ -12,6 +12,7 @@
<preference id="extensions.sendtophone.protocols.sms" name="extensions.sendtophone.protocols.sms" type="bool"/>
<preference id="extensions.sendtophone.protocols.smsto" name="extensions.sendtophone.protocols.smsto" type="bool"/>
<preference id="extensions.sendtophone.protocols.tel" name="extensions.sendtophone.protocols.tel" type="bool"/>
<preference id="extensions.sendtophone.fileServerUrl" name="extensions.sendtophone.fileServerUrl" type="string"/>
</preferences>
<groupbox style="padding:1em;" label="ProtocolsGroup">
@@ -29,6 +30,35 @@
label="tel:" />
</groupbox>
<groupbox style="padding:1em; min-height:10em;" label="FileServer">
<caption label="File transfers" />
<hbox align="center">
<label value="Server"
flex="1"
control="extensionsSendToPhoneFileServer"/>
<menulist id="extensionsSendToPhoneFileServer">
<menupopup>
<menuitem value="" label="None" />
<menuitem value="http://min.us" label="Min.us" />
<menuitem value="Custom" label="Custom" />
</menupopup>
</menulist>
<description style="width: 300px;height:3em;" id="foxtophoneDisclaimer">
Disclaimer: We (FoxToPhone developers) are not affiliated
with any of the third party hosting services listed here.
You should read their Terms Of Service before using them.
</description>
</hbox>
<hbox align="center" id="hboxFileServerUrl">
<textbox id="extensionsSendToPhoneFileServerUrl" flex="1" preference="extensions.sendtophone.fileServerUrl" />
</hbox>
</groupbox>
</prefpane>
<script type="application/x-javascript" src="options.js" />
</prefwindow>

View File

@@ -14,7 +14,6 @@
limitations under the License.
*/
// Core functions
Components.utils.import("resource://sendtophone/sendtophone.js");
// Protocol handlers

View File

@@ -36,8 +36,7 @@
if (!startEl.length)
startEl = [this];
const XULNS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
return startEl[0].getElementsByTagNameNS(XULNS, "button");
return startEl[0].getElementsByTagNameNS("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul", "button");
]]>
</getter>
</property>
@@ -62,7 +61,7 @@
</xul:vbox>
<xul:button class="cancel mini-button" tooltiptext="&cmd.cancel.label;"
cmd="cmd_cancel" ondblclick="event.stopPropagation();"
oncommand="performCommand('cmd_cancel', this);"/>
oncommand="FoxToPhoneUploadWindow.performCancelCommand(this);"/>
</xul:hbox>
<xul:label xbl:inherits="value=status,tooltiptext=statusTip" flex="1"
crop="right" class="status"/>

View File

@@ -1,16 +1,57 @@
var Cc = Components.classes;
var Ci = Components.interfaces;
"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 gUploadManager = sendtophoneUploadsManager;
let gUploadsView = null;
let gUploadListener = {
UploadsView: null,
function addFile(upload)
{
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);
@@ -28,127 +69,45 @@ function addFile(upload)
dl.setAttribute("id", "upl" + upload.id);
dl.setAttribute("uploadId", upload.id);
gUploadsView.appendChild( dl );
}
var checkTimerEvent =
{
notify: function(timer)
{
if (sendtophoneUploadsManager.isWindowNeeded())
{
if (gUploadsView.children.length==0)
window.close();
this.UploadsView.appendChild( dl );
}
}
}
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 0 ms timeout to avoid flicker while compress -> upload a folder
let checkTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
checkTimer.initWithCallback(checkTimerEvent, 0, Ci.nsITimer.TYPE_ONE_SHOT);
}
};
let FoxToPhoneUploadWindow = {
UploadManager: null,
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)
Startup: function()
{
let dl = aItem;
this.UploadManager = sendtophoneUploadsManager;
switch (aCommand) {
case "cmd_cancel":
return dl.inProgress;
}
return false;
gUploadListener.UploadsView = document.getElementById("UploadsBox");
this.UploadManager.addListener(gUploadListener);
for (let id in this.UploadManager.uploads)
gUploadListener.addFile( this.UploadManager.uploads[id] );
},
doCommand: function(aCommand, aItem)
Shutdown: function()
{
if (this.isCommandEnabled(aCommand, aItem))
this.commands[aCommand](aItem);
this.UploadManager.removeListener(gUploadListener);
},
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)
{
performCancelCommand: function(aItem)
{
let elm = aItem;
while (elm.nodeName != "richlistitem" ||
elm.getAttribute("type") != "upload")
elm = elm.parentNode;
gUploadViewController.doCommand(aCmd, elm);
}
if (elm.inProgress)
this.UploadManager.cancelUpload( parseInt(elm.getAttribute("uploadId"), 10) );
},
function updateStatus(aItem)
{
updateStatus: function(aItem)
{
let currBytes = Number(aItem.getAttribute("currBytes"));
let maxBytes = Number(aItem.getAttribute("maxBytes"));
@@ -165,4 +124,8 @@ function updateStatus(aItem)
aItem.setAttribute("lastSeconds", newLast);
aItem.setAttribute("status", status);
}
}
};

View File

@@ -1,18 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="chrome://global/skin/" type="text/css"?>
<?xml-stylesheet href="chrome://sendtophone/content/upload.css"?>
<?xml-stylesheet href="chrome://sendtophone/skin/uploads.css"?>
<?xml-stylesheet href="chrome://sendtophone/content/upload.css" type="text/css"?>
<?xml-stylesheet href="chrome://sendtophone/skin/uploads.css" type="text/css"?>
<!DOCTYPE window SYSTEM "chrome://sendtophone/locale/overlay.dtd">
<window xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
id="sendtophoneUploadsWindow"
onload="Startup();" onunload="Shutdown();"
onload="FoxToPhoneUploadWindow.Startup();" onunload="FoxToPhoneUploadWindow.Shutdown();"
onclose="return closeWindow(false);">
<script type="application/javascript" src="chrome://sendtophone/content/uploads.js"/>
<richlistbox id="UploadsBox" flex="1">
</richlistbox>
<richlistbox id="UploadsBox" flex="1"></richlistbox>
</window>

View File

@@ -0,0 +1,63 @@
#UploadsBox {
-moz-appearance: none;
margin: 0;
padding: 0;
border-width: 0;
}
/* Upload View Items */
richlistitem[type="upload"] {
padding: 5px;
min-height: 44px !important;
border: 1px solid transparent;
}
richlistitem[type="upload"]:not([selected="true"]):nth-child(odd) {
background-color: -moz-oddtreerow;
}
richlistitem[type="upload"] .status {
font-size: smaller;
color: #555;
}
richlistitem[selected="true"][type="upload"] {
outline: none;
}
richlistbox:focus > richlistitem[selected="true"][type="upload"] .status {
color: highlighttext;
}
richlistitem[type="upload"] button {
-moz-appearance: none;
min-height: 16px;
min-width: 16px;
max-height: 16px;
max-width: 16px;
padding: 0;
margin: 0 1px 0 1px;
}
/**
* Images for buttons in the interface
*/
richlistitem[type="upload"] button {
list-style-image: url(chrome://mozapps/skin/downloads/buttons.png);
}
.cancel {
-moz-image-region: rect(0px, 16px, 16px, 0px);
}
.cancel:hover {
-moz-image-region: rect(0px, 32px, 16px, 16px);
}
.cancel:hover:active {
-moz-image-region: rect(0px, 48px, 16px, 32px);
}
/* prevent flickering when changing states */
.uploadTypeIcon {
min-height: 32px;
min-width: 32px;
-moz-padding-end: 2px;
}

View File

@@ -1,63 +1,63 @@
#UploadsBox {
#sendtophoneUploadsWindow {
background-color: ThreeDFace;
}
/* Upload View */
@media not all and (-moz-windows-classic) {
#UploadsBox {
-moz-appearance: none;
margin: 0;
padding: 0;
border-width: 0;
border-bottom: 2px solid;
-moz-border-bottom-colors: ThreeDHighlight ThreeDLightShadow;
}
}
/* Upload View Items */
richlistitem[type="upload"] {
padding: 5px;
min-height: 44px !important;
border: 1px solid transparent;
padding: 4px 8px 4px 4px;
min-height: 46px;
border-bottom: 1px solid ThreeDLightShadow;
}
richlistitem[type="upload"]:not([selected="true"]):nth-child(odd) {
background-color: -moz-oddtreerow;
richlistitem[type="upload"][selected="true"] {
background-image: url(chrome://mozapps/skin/extensions/itemEnabledFader.png);
}
richlistitem[type="upload"] .status {
font-size: smaller;
color: #555;
richlistitem[type="upload"] .name {
font-size: larger;
}
richlistitem[selected="true"][type="upload"] {
outline: none;
}
richlistbox:focus > richlistitem[selected="true"][type="upload"] .status {
color: highlighttext;
}
richlistitem[type="upload"] button {
.mini-button {
-moz-appearance: none;
min-height: 16px;
min-width: 16px;
max-height: 16px;
max-width: 16px;
list-style-image: url(chrome://mozapps/skin/downloads/downloadButtons.png);
background-color: transparent;
border: none;
padding: 0;
margin: 0 1px 0 1px;
margin: 0;
min-width: 0;
min-height: 0;
}
/**
* Images for buttons in the interface
*/
richlistitem[type="upload"] button {
list-style-image: url(chrome://mozapps/skin/downloads/buttons.png);
.mini-button > .button-box {
padding: 0 !important;
}
.cancel {
-moz-image-region: rect(0px, 16px, 16px, 0px);
}
.cancel:hover {
-moz-image-region: rect(0px, 32px, 16px, 16px);
}
.cancel:hover:active {
-moz-image-region: rect(0px, 48px, 16px, 32px);
.cancel:hover {
-moz-image-region: rect(16px, 32px, 32px, 16px);
}
.cancel:active {
-moz-image-region: rect(32px, 32px, 48px, 16px);
}
.cancel[disabled="true"] {
-moz-image-region: rect(48px, 32px, 64px, 16px);
}
/* prevent flickering when changing states */
.uploadTypeIcon {
min-height: 32px;
min-width: 32px;
-moz-padding-end: 2px;
}

View File

@@ -1,3 +1,5 @@
"use strict";
/* This js module doesn't export anything, it's meant to handle the protocol registration/unregistration */
var EXPORTED_SYMBOLS = [];

View File

@@ -13,6 +13,7 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
"use strict";
// https://developer.mozilla.org/en/JavaScript_code_modules/Using_JavaScript_code_modules
var EXPORTED_SYMBOLS = ["sendtophoneCore"];
@@ -332,9 +333,9 @@ var sendtophoneCore = {
sendtophoneUploadsManager.finishedUpload( progressId );
// Send the zip and delete it afterwards
sendtophoneCore.sendFile(nsZip, function() { nsZip.remove(false) });
sendtophoneCore.sendFile(nsZip, function() { nsZip.remove(false); });
}
)
);
return;
}
@@ -344,6 +345,32 @@ var sendtophoneCore = {
return;
}
if ( uri == "http://min.us" )
{
Minus.SendFile(nsFile, callback);
return;
}
this.sendFileXHR( nsFile, callback, uri, 'upload', function(target, uploadName)
{
var body = target.responseXML,
uploads;
// FoxToPhone custom script
if (body && (uploads = body.documentElement.getElementsByTagName("upload")) && uploads[0])
{
var url = uploads[0].firstChild.data;
sendtophoneCore.send(uploadName, url, "");
return;
}
// error.
sendtophoneCore.alert(uri + "\r\n" + event.target.responseText);
});
},
sendFileXHR: function( nsFile, callback, uri, formElementName, onSuccess )
{
let size = Math.round(nsFile.fileSize / 1024);
let maxSize = this.prefs.getIntPref( "fileUploadMaxKb" );
if (maxSize>0 && size>maxSize)
@@ -371,20 +398,20 @@ var sendtophoneCore = {
bufInStream.init(inStream, 4096);
//Setup the boundary start stream
var boundary = "--SendToPhone-------------" + Math.random();
var boundary = "--SendToPhone-------------" + Math.random().toString(16).substr(2);
var startBoundryStream = Cc["@mozilla.org/io/string-input-stream;1"]
.createInstance(Ci.nsIStringInputStream);
startBoundryStream.setData("\r\n--"+boundary+"\r\n",-1);
startBoundryStream.setData("--"+boundary+"\r\n",-1);
// Setup the boundary end stream
var endBoundryStream = Cc["@mozilla.org/io/string-input-stream;1"]
.createInstance(Ci.nsIStringInputStream);
endBoundryStream.setData("\r\n--"+boundary+"--",-1);
endBoundryStream.setData("\r\n\r\n--"+boundary+"--\r\n",-1);
// Setup the mime-stream - the 'part' of a multi-part mime type
var mimeStream = Cc["@mozilla.org/network/mime-input-stream;1"].createInstance(Ci.nsIMIMEInputStream);
mimeStream.addContentLength = false;
mimeStream.addHeader("Content-disposition","form-data; charset: utf-8; name=\"upload\"; filename=\"" + this.toUTF8(uploadName) + "\"");
mimeStream.addHeader("Content-disposition",'form-data; charset: utf-8; name="' + formElementName + '"; filename="' + this.toUTF8(uploadName) + '"');
mimeStream.addHeader("Content-type", mimeType);
mimeStream.setData(bufInStream);
@@ -404,25 +431,16 @@ var sendtophoneCore = {
req.open('POST', uri, true);
req.setRequestHeader("Content-length",multiStream.available());
req.setRequestHeader("Content-type","multipart/form-data; charset: utf-8; boundary="+boundary);
req.setRequestHeader("Content-type","multipart/form-data; boundary="+boundary);
req.addEventListener("load", function(event)
{
// If there's a callback (to delete temporary files) we call it now
if (callback)
callback();
var body = event.target.responseXML;
var uploads;
if (body && (uploads = body.documentElement.getElementsByTagName("upload")))
{
var data = uploads[0].firstChild.data;
// sendtophoneCore.toConsole(data);
sendtophoneCore.send(uploadName, data, "");
return;
}
// error.
sendtophoneCore.alert(uri + "\r\n" + event.target.responseText);
onSuccess(event.target, uploadName);
}, false);
// Handle errors or aborted uploads
req.addEventListener("error", function(evt)
{
@@ -432,6 +450,7 @@ var sendtophoneCore = {
if (callback)
callback();
}, false);
req.addEventListener("abort", function(evt)
{
// Silent.
@@ -451,8 +470,45 @@ var sendtophoneCore = {
*/
req.send(multiStream);
}
};
// http://min.us/
var Minus = {
prefix: 'http://min.us/api/',
SendFile : function(nsFile, callback) {
// Create a gallery
sendtophoneCore.processXHR( Minus.prefix + 'CreateGallery', 'GET', null, function(req) {
var body = req.responseText;
if (body.substring(0, 1) != '{')
{
sendtophoneCore.alert(sendtophoneCore.getString("ErrorOnSend") + ' (status ' + req.status + ')\r\n' + body);
return;
}
var gallery = JSON.parse( body );
// Send the file
sendtophoneCore.sendFileXHR(nsFile, callback, Minus.prefix +
'UploadItem?editor_id=' + gallery.editor_id + '&key=OK&filename=' + encodeURIComponent(nsFile.leafName), 'file',
function(target, uploadName)
{
var body = target.responseText;
if (body.substring(0, 1) != '{')
{
sendtophoneCore.alert(sendtophoneCore.getString("ErrorOnSend") + ' (status ' + req.status + ')\r\n' + body);
return;
}
var file = JSON.parse( body );
// var url = "http://k.min.us/j" + file.id;
var url = "http://min.us/m" + gallery.reader_id;
sendtophoneCore.send(uploadName, url, "");
});
});
}
};
/* Zipping functions */
@@ -478,7 +534,7 @@ function zipFolder(folder, callback)
// Create a new file
nsFile.append( folder.leafName + ".zip");
nsFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
nsFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 438); // 438 (decimal) = 0666 (octal)
var zipWriter = Components.Constructor("@mozilla.org/zipwriter;1", "nsIZipWriter");
var zipW = new zipWriter();
@@ -497,7 +553,7 @@ function zipFolder(folder, callback)
// Notify that we're done. Now it must be sent and deleted afterwards
callback(nsFile);
}
}
};
zipW.processQueue(observer, null);
}

View File

@@ -1,3 +1,5 @@
"use strict";
var EXPORTED_SYMBOLS = ["sendtophoneUploadsManager"];
const Cc = Components.classes;
@@ -151,15 +153,6 @@ var sendtophoneUploadsManager = {
},
// For use while debugging
toConsole: function(text)
{
var aConsoleService = Cc["@mozilla.org/consoleservice;1"]
.getService(Ci.nsIConsoleService);
aConsoleService.logStringMessage( text );
},
// Check if we might need to show the window.
// Either some folder is being compressed,
// or there's some file that might take longer than 2 seconds.
@@ -182,7 +175,6 @@ var sendtophoneUploadsManager = {
let elapsedTime = (Date.now() - upload.startTime) / 1000;
let speed = upload.currBytes/elapsedTime;
let remainingSecs = (upload.maxBytes - upload.currBytes) / speed;
// this.toConsole(elapsedTime + " " + speed + " " + remainingSecs);
if (remainingSecs > 2)
return true;