2007-03-22 10:30:00 -07:00
|
|
|
# -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
# ***** BEGIN LICENSE BLOCK *****
|
|
|
|
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
#
|
|
|
|
# The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
# 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
# the License. You may obtain a copy of the License at
|
|
|
|
# http://www.mozilla.org/MPL/
|
|
|
|
#
|
|
|
|
# Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
# for the specific language governing rights and limitations under the
|
|
|
|
# License.
|
|
|
|
#
|
|
|
|
# The Original Code is Mozilla.org Code.
|
|
|
|
#
|
|
|
|
# The Initial Developer of the Original Code is
|
|
|
|
# Netscape Communications Corporation.
|
|
|
|
# Portions created by the Initial Developer are Copyright (C) 2001
|
|
|
|
# the Initial Developer. All Rights Reserved.
|
|
|
|
#
|
|
|
|
# Contributor(s):
|
|
|
|
# Blake Ross <blakeross@telocity.com> (Original Author)
|
|
|
|
# Ben Goodger <ben@bengoodger.com> (v2.0)
|
|
|
|
# Dan Mosedale <dmose@mozilla.org>
|
|
|
|
# Fredrik Holmqvist <thesuckiestemail@yahoo.se>
|
|
|
|
# Josh Aas <josh@mozilla.com>
|
2007-08-11 14:53:21 -07:00
|
|
|
# Shawn Wilsher <me@shawnwilsher.com> (v3.0)
|
2007-10-01 17:44:48 -07:00
|
|
|
# Edward Lee <edward.lee@engineering.uiuc.edu>
|
2008-11-03 15:22:21 -08:00
|
|
|
# Ehsan Akhgari <ehsan.akhgari@gmail.com>
|
2007-03-22 10:30:00 -07:00
|
|
|
#
|
|
|
|
# Alternatively, the contents of this file may be used under the terms of
|
|
|
|
# either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
# in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
# of those above. If you wish to allow use of your version of this file only
|
|
|
|
# under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
# use your version of this file under the terms of the MPL, indicate your
|
|
|
|
# decision by deleting the provisions above and replace them with the notice
|
|
|
|
# and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
# the provisions above, a recipient may use your version of this file under
|
|
|
|
# the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
#
|
|
|
|
# ***** END LICENSE BLOCK *****
|
|
|
|
|
2007-11-11 15:48:43 -08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// Globals
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
const PREF_BDM_CLOSEWHENDONE = "browser.download.manager.closeWhenDone";
|
|
|
|
const PREF_BDM_ALERTONEXEOPEN = "browser.download.manager.alertOnEXEOpen";
|
2009-08-11 10:37:38 -07:00
|
|
|
const PREF_BDM_SCANWHENDONE = "browser.download.manager.scanWhenDone";
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-05-21 17:03:33 -07:00
|
|
|
const nsLocalFile = Components.Constructor("@mozilla.org/file/local;1",
|
|
|
|
"nsILocalFile", "initWithPath");
|
|
|
|
|
2007-08-11 14:53:21 -07:00
|
|
|
var Cc = Components.classes;
|
2007-06-11 22:24:58 -07:00
|
|
|
var Ci = Components.interfaces;
|
2008-01-22 18:15:08 -08:00
|
|
|
let Cu = Components.utils;
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
Cu.import("resource://gre/modules/DownloadUtils.jsm");
|
2008-01-26 23:53:31 -08:00
|
|
|
Cu.import("resource://gre/modules/PluralForm.jsm");
|
2007-05-23 16:12:03 -07:00
|
|
|
|
2007-11-11 15:48:43 -08:00
|
|
|
const nsIDM = Ci.nsIDownloadManager;
|
|
|
|
|
2008-01-08 21:43:36 -08:00
|
|
|
let gDownloadManager = Cc["@mozilla.org/download-manager;1"].getService(nsIDM);
|
|
|
|
let gDownloadListener = null;
|
|
|
|
let gDownloadsView = null;
|
|
|
|
let gSearchBox = null;
|
2008-02-26 09:52:17 -08:00
|
|
|
let gSearchTerms = [];
|
2008-01-08 21:43:36 -08:00
|
|
|
let gBuilder = 0;
|
2007-12-15 17:35:38 -08:00
|
|
|
|
2008-04-08 22:33:39 -07:00
|
|
|
// This variable is used when performing commands on download items and gives
|
|
|
|
// the command the ability to do something after all items have been operated
|
|
|
|
// on. The following convention is used to handle the value of the variable:
|
|
|
|
// whenever we aren't performing a command, the value is |undefined|; just
|
|
|
|
// before executing commands, the value will be set to |null|; and when
|
|
|
|
// commands want to create a callback, they set the value to be a callback
|
|
|
|
// function to be executed after all download items have been visited.
|
|
|
|
let gPerformAllCallback;
|
|
|
|
|
2007-12-15 17:35:38 -08:00
|
|
|
// Control the performance of the incremental list building by setting how many
|
|
|
|
// milliseconds to wait before building more of the list and how many items to
|
|
|
|
// add between each delay.
|
2007-12-21 19:45:54 -08:00
|
|
|
const gListBuildDelay = 300;
|
|
|
|
const gListBuildChunk = 3;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-02-26 09:52:17 -08:00
|
|
|
// Array of download richlistitem attributes to check when searching
|
|
|
|
const gSearchAttributes = [
|
|
|
|
"target",
|
|
|
|
"status",
|
|
|
|
"dateTime",
|
|
|
|
];
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// If the user has interacted with the window in a significant way, we should
|
|
|
|
// not auto-close the window. Tough UI decisions about what is "significant."
|
|
|
|
var gUserInteracted = false;
|
|
|
|
|
2007-10-01 17:44:48 -07:00
|
|
|
// These strings will be converted to the corresponding ones from the string
|
|
|
|
// bundle on startup.
|
2008-01-08 21:43:36 -08:00
|
|
|
let gStr = {
|
2007-10-01 17:44:48 -07:00
|
|
|
paused: "paused",
|
2008-01-21 19:43:02 -08:00
|
|
|
cannotPause: "cannotPause",
|
2007-11-11 15:52:49 -08:00
|
|
|
doneStatus: "doneStatus",
|
|
|
|
doneSize: "doneSize",
|
|
|
|
doneSizeUnknown: "doneSizeUnknown",
|
2007-11-11 15:56:14 -08:00
|
|
|
stateFailed: "stateFailed",
|
|
|
|
stateCanceled: "stateCanceled",
|
2008-03-21 17:23:45 -07:00
|
|
|
stateBlockedParentalControls: "stateBlocked",
|
2008-04-04 00:08:24 -07:00
|
|
|
stateBlockedPolicy: "stateBlockedPolicy",
|
2007-11-30 22:49:07 -08:00
|
|
|
stateDirty: "stateDirty",
|
2007-12-14 02:00:16 -08:00
|
|
|
yesterday: "yesterday",
|
|
|
|
monthDate: "monthDate",
|
2008-01-26 13:02:32 -08:00
|
|
|
downloadsTitleFiles: "downloadsTitleFiles",
|
|
|
|
downloadsTitlePercent: "downloadsTitlePercent",
|
2007-10-01 17:44:48 -07:00
|
|
|
fileExecutableSecurityWarningTitle: "fileExecutableSecurityWarningTitle",
|
|
|
|
fileExecutableSecurityWarningDontAsk: "fileExecutableSecurityWarningDontAsk"
|
|
|
|
};
|
|
|
|
|
2007-12-15 17:35:38 -08:00
|
|
|
// The statement to query for downloads that are active or match the search
|
2008-11-03 15:22:21 -08:00
|
|
|
let gStmt = null;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-11-11 15:48:43 -08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// Start/Stop Observers
|
2007-10-01 17:44:48 -07:00
|
|
|
|
2007-11-11 15:48:43 -08:00
|
|
|
function downloadCompleted(aDownload)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-04-24 22:59:31 -07:00
|
|
|
// The download is changing state, so update the clear list button
|
|
|
|
updateClearListButton();
|
|
|
|
|
2007-11-11 15:48:43 -08:00
|
|
|
// Wrap this in try...catch since this can be called while shutting down...
|
2007-03-22 10:30:00 -07:00
|
|
|
// it doesn't really matter if it fails then since well.. we're shutting down
|
|
|
|
// and there's no UI to update!
|
|
|
|
try {
|
2007-08-11 14:53:21 -07:00
|
|
|
let dl = getDownload(aDownload.id);
|
|
|
|
|
2007-09-04 10:29:12 -07:00
|
|
|
// Update attributes now that we've finished
|
|
|
|
dl.setAttribute("startTime", Math.round(aDownload.startTime / 1000));
|
2007-11-11 15:58:50 -08:00
|
|
|
dl.setAttribute("endTime", Date.now());
|
2007-11-11 15:52:49 -08:00
|
|
|
dl.setAttribute("currBytes", aDownload.amountTransferred);
|
|
|
|
dl.setAttribute("maxBytes", aDownload.size);
|
2007-09-04 10:29:12 -07:00
|
|
|
|
2008-02-26 09:52:17 -08:00
|
|
|
// Move the download below active if it should stay in the list
|
|
|
|
if (downloadMatchesSearch(dl)) {
|
2007-12-15 17:35:38 -08:00
|
|
|
// Iterate down until we find a non-active download
|
|
|
|
let next = dl.nextSibling;
|
|
|
|
while (next && next.inProgress)
|
|
|
|
next = next.nextSibling;
|
|
|
|
|
2008-06-11 07:11:14 -07:00
|
|
|
// Move the item
|
2007-12-15 17:35:38 -08:00
|
|
|
gDownloadsView.insertBefore(dl, next);
|
|
|
|
} else {
|
2007-10-07 10:21:57 -07:00
|
|
|
removeFromView(dl);
|
2007-12-15 17:35:38 -08:00
|
|
|
}
|
2007-08-11 14:53:21 -07:00
|
|
|
|
|
|
|
// getTypeFromFile fails if it can't find a type for this file.
|
2007-03-22 10:30:00 -07:00
|
|
|
try {
|
|
|
|
// Refresh the icon, so that executable icons are shown.
|
2007-11-21 12:57:38 -08:00
|
|
|
var mimeService = Cc["@mozilla.org/mime;1"].
|
2007-08-11 14:53:21 -07:00
|
|
|
getService(Ci.nsIMIMEService);
|
2007-03-22 10:30:00 -07:00
|
|
|
var contentType = mimeService.getTypeFromFile(aDownload.targetFile);
|
|
|
|
|
2007-05-21 17:03:33 -07:00
|
|
|
var listItem = getDownload(aDownload.id)
|
2007-03-22 10:30:00 -07:00
|
|
|
var oldImage = listItem.getAttribute("image");
|
2007-08-11 14:53:21 -07:00
|
|
|
// Tacking on contentType bypasses cache
|
2007-03-22 10:30:00 -07:00
|
|
|
listItem.setAttribute("image", oldImage + "&contentType=" + contentType);
|
2007-05-21 17:03:33 -07:00
|
|
|
} catch (e) { }
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-10-27 13:46:50 -07:00
|
|
|
if (gDownloadManager.activeDownloadCount == 0)
|
2007-03-22 10:30:00 -07:00
|
|
|
document.title = document.documentElement.getAttribute("statictitle");
|
|
|
|
}
|
2007-05-21 17:03:33 -07:00
|
|
|
catch (e) { }
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-05-21 17:03:33 -07:00
|
|
|
function autoRemoveAndClose(aDownload)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-08-11 14:53:21 -07:00
|
|
|
var pref = Cc["@mozilla.org/preferences-service;1"].
|
|
|
|
getService(Ci.nsIPrefBranch);
|
2007-05-21 17:03:33 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (gDownloadManager.activeDownloadCount == 0) {
|
|
|
|
// For the moment, just use the simple heuristic that if this window was
|
2007-08-11 14:53:21 -07:00
|
|
|
// opened by the download process, rather than by the user, it should
|
|
|
|
// auto-close if the pref is set that way. If the user opened it themselves,
|
2008-02-08 14:22:35 -08:00
|
|
|
// it should not close until they explicitly close it. Additionally, the
|
|
|
|
// preference to control the feature may not be set, so defaulting to
|
|
|
|
// keeping the window open.
|
|
|
|
let autoClose = false;
|
|
|
|
try {
|
|
|
|
autoClose = pref.getBoolPref(PREF_BDM_CLOSEWHENDONE);
|
|
|
|
} catch (e) { }
|
2007-08-11 14:53:21 -07:00
|
|
|
var autoOpened =
|
|
|
|
!window.opener || window.opener.location.href == window.location.href;
|
2007-08-14 21:21:06 -07:00
|
|
|
if (autoClose && autoOpened && !gUserInteracted) {
|
2007-03-22 10:30:00 -07:00
|
|
|
gCloseDownloadManager();
|
2007-06-15 14:19:08 -07:00
|
|
|
return true;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2007-11-11 15:48:43 -08:00
|
|
|
|
2007-06-15 14:19:08 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-08-11 14:53:21 -07:00
|
|
|
// This function can be overwritten by extensions that wish to place the
|
2007-11-11 15:48:43 -08:00
|
|
|
// Download Window in another part of the UI.
|
2007-03-22 10:30:00 -07:00
|
|
|
function gCloseDownloadManager()
|
|
|
|
{
|
|
|
|
window.close();
|
|
|
|
}
|
|
|
|
|
2007-11-11 15:48:43 -08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2007-08-11 14:53:21 -07:00
|
|
|
//// Download Event Handlers
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-08-11 14:53:21 -07:00
|
|
|
function cancelDownload(aDownload)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-08-11 14:53:21 -07:00
|
|
|
gDownloadManager.cancelDownload(aDownload.getAttribute("dlid"));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-11-11 15:48:43 -08:00
|
|
|
// XXXben -
|
2007-03-22 10:30:00 -07:00
|
|
|
// If we got here because we resumed the download, we weren't using a temp file
|
|
|
|
// because we used saveURL instead. (this is because the proper download mechanism
|
|
|
|
// employed by the helper app service isn't fully accessible yet... should be fixed...
|
|
|
|
// talk to bz...)
|
2007-11-11 15:48:43 -08:00
|
|
|
// the upshot is we have to delete the file if it exists.
|
2007-08-11 14:53:21 -07:00
|
|
|
var f = getLocalFileFromNativePathOrUrl(aDownload.getAttribute("file"));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-11-11 15:48:43 -08:00
|
|
|
if (f.exists())
|
2007-03-22 10:30:00 -07:00
|
|
|
f.remove(false);
|
|
|
|
}
|
|
|
|
|
2007-08-11 14:53:21 -07:00
|
|
|
function pauseDownload(aDownload)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-08-11 14:53:21 -07:00
|
|
|
var id = aDownload.getAttribute("dlid");
|
2007-05-21 17:03:33 -07:00
|
|
|
gDownloadManager.pauseDownload(id);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-08-11 14:53:21 -07:00
|
|
|
function resumeDownload(aDownload)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-08-11 14:53:21 -07:00
|
|
|
gDownloadManager.resumeDownload(aDownload.getAttribute("dlid"));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-08-11 14:53:21 -07:00
|
|
|
function removeDownload(aDownload)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-08-11 14:53:21 -07:00
|
|
|
gDownloadManager.removeDownload(aDownload.getAttribute("dlid"));
|
2007-09-18 23:06:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function retryDownload(aDownload)
|
|
|
|
{
|
|
|
|
removeFromView(aDownload);
|
|
|
|
gDownloadManager.retryDownload(aDownload.getAttribute("dlid"));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-08-11 14:53:21 -07:00
|
|
|
function showDownload(aDownload)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-08-11 14:53:21 -07:00
|
|
|
var f = getLocalFileFromNativePathOrUrl(aDownload.getAttribute("file"));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-08-11 14:53:21 -07:00
|
|
|
try {
|
2007-12-18 11:56:09 -08:00
|
|
|
// Show the directory containing the file and select the file
|
2007-08-11 14:53:21 -07:00
|
|
|
f.reveal();
|
2007-12-18 11:56:09 -08:00
|
|
|
} catch (e) {
|
|
|
|
// If reveal fails for some reason (e.g., it's not implemented on unix or
|
|
|
|
// the file doesn't exist), try using the parent if we have it.
|
|
|
|
let parent = f.parent.QueryInterface(Ci.nsILocalFile);
|
|
|
|
if (!parent)
|
|
|
|
return;
|
|
|
|
|
|
|
|
try {
|
|
|
|
// "Double click" the parent directory to show where the file should be
|
|
|
|
parent.launch();
|
|
|
|
} catch (e) {
|
|
|
|
// If launch also fails (probably because it's not implemented), let the
|
|
|
|
// OS handler try to open the parent
|
2007-08-11 14:53:21 -07:00
|
|
|
openExternal(parent);
|
2007-12-18 11:56:09 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-12 01:14:08 -07:00
|
|
|
function onDownloadDblClick(aEvent)
|
|
|
|
{
|
2007-12-18 00:47:48 -08:00
|
|
|
// Only do the default action for double primary clicks
|
2008-06-04 08:05:09 -07:00
|
|
|
if (aEvent.button == 0 && aEvent.target.selected)
|
2007-12-18 00:47:48 -08:00
|
|
|
doDefaultForSelected();
|
2007-08-12 01:14:08 -07:00
|
|
|
}
|
|
|
|
|
2007-08-11 14:53:21 -07:00
|
|
|
function openDownload(aDownload)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-08-11 14:53:21 -07:00
|
|
|
var f = getLocalFileFromNativePathOrUrl(aDownload.getAttribute("file"));
|
|
|
|
if (f.isExecutable()) {
|
|
|
|
var dontAsk = false;
|
|
|
|
var pref = Cc["@mozilla.org/preferences-service;1"].
|
|
|
|
getService(Ci.nsIPrefBranch);
|
|
|
|
try {
|
|
|
|
dontAsk = !pref.getBoolPref(PREF_BDM_ALERTONEXEOPEN);
|
|
|
|
} catch (e) { }
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-10-28 21:53:30 -07:00
|
|
|
#ifdef XP_WIN
|
2009-08-10 19:58:54 -07:00
|
|
|
#ifndef WINCE
|
2008-10-28 21:53:30 -07:00
|
|
|
// On Vista and above, we rely on native security prompting for
|
2009-08-11 10:37:38 -07:00
|
|
|
// downloaded content unless it's disabled.
|
2008-10-28 21:53:30 -07:00
|
|
|
try {
|
2009-08-11 10:37:38 -07:00
|
|
|
var sysInfo = Cc["@mozilla.org/system-info;1"].
|
|
|
|
getService(Ci.nsIPropertyBag2);
|
|
|
|
if (parseFloat(sysInfo.getProperty("version")) >= 6 &&
|
|
|
|
pref.getBoolPref(PREF_BDM_SCANWHENDONE)) {
|
|
|
|
dontAsk = true;
|
|
|
|
}
|
2008-10-28 21:53:30 -07:00
|
|
|
} catch (ex) { }
|
2009-08-10 19:58:54 -07:00
|
|
|
#endif
|
2008-10-28 21:53:30 -07:00
|
|
|
#endif
|
|
|
|
|
2007-08-11 14:53:21 -07:00
|
|
|
if (!dontAsk) {
|
|
|
|
var strings = document.getElementById("downloadStrings");
|
|
|
|
var name = aDownload.getAttribute("target");
|
|
|
|
var message = strings.getFormattedString("fileExecutableSecurityWarning", [name, name]);
|
|
|
|
|
2007-10-01 17:44:48 -07:00
|
|
|
let title = gStr.fileExecutableSecurityWarningTitle;
|
|
|
|
let dontAsk = gStr.fileExecutableSecurityWarningDontAsk;
|
2007-08-11 14:53:21 -07:00
|
|
|
|
|
|
|
var promptSvc = Cc["@mozilla.org/embedcomp/prompt-service;1"].
|
|
|
|
getService(Ci.nsIPromptService);
|
|
|
|
var checkbox = { value: false };
|
|
|
|
var open = promptSvc.confirmCheck(window, title, message, dontAsk, checkbox);
|
|
|
|
|
2007-11-11 15:48:43 -08:00
|
|
|
if (!open)
|
2007-08-11 14:53:21 -07:00
|
|
|
return;
|
|
|
|
pref.setBoolPref(PREF_BDM_ALERTONEXEOPEN, !checkbox.value);
|
2007-11-11 15:48:43 -08:00
|
|
|
}
|
2007-08-11 14:53:21 -07:00
|
|
|
}
|
|
|
|
try {
|
2009-10-26 18:39:23 -07:00
|
|
|
try {
|
|
|
|
let download = gDownloadManager.getDownload(aDownload.getAttribute("dlid"));
|
|
|
|
let mimeInfo = download.MIMEInfo;
|
|
|
|
if (mimeInfo.preferredAction == mimeInfo.useHelperApp) {
|
|
|
|
mimeInfo.launchWithFile(f);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} catch (ex) {
|
|
|
|
}
|
2007-08-11 14:53:21 -07:00
|
|
|
f.launch();
|
|
|
|
} catch (ex) {
|
|
|
|
// if launch fails, try sending it through the system's external
|
|
|
|
// file: URL handler
|
|
|
|
openExternal(f);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-08-17 16:05:26 -07:00
|
|
|
function openReferrer(aDownload)
|
|
|
|
{
|
2007-10-27 13:37:44 -07:00
|
|
|
openURL(getReferrerOrSource(aDownload));
|
2007-08-17 16:05:26 -07:00
|
|
|
}
|
|
|
|
|
2007-09-05 14:19:12 -07:00
|
|
|
function copySourceLocation(aDownload)
|
|
|
|
{
|
|
|
|
var uri = aDownload.getAttribute("uri");
|
|
|
|
var clipboard = Cc["@mozilla.org/widget/clipboardhelper;1"].
|
|
|
|
getService(Ci.nsIClipboardHelper);
|
|
|
|
|
2008-04-08 22:33:39 -07:00
|
|
|
// Check if we should initialize a callback
|
|
|
|
if (gPerformAllCallback === null) {
|
|
|
|
let uris = [];
|
|
|
|
gPerformAllCallback = function(aURI) aURI ? uris.push(aURI) :
|
|
|
|
clipboard.copyString(uris.join("\n"));
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have a callback to use, so use it to add a uri
|
|
|
|
if (typeof gPerformAllCallback == "function")
|
|
|
|
gPerformAllCallback(uri);
|
|
|
|
else {
|
|
|
|
// It's a plain copy source, so copy it
|
|
|
|
clipboard.copyString(uri);
|
|
|
|
}
|
2007-09-05 14:19:12 -07:00
|
|
|
}
|
|
|
|
|
2008-04-22 20:57:05 -07:00
|
|
|
/**
|
|
|
|
* Remove the currently shown downloads from the download list.
|
|
|
|
*/
|
|
|
|
function clearDownloadList() {
|
|
|
|
// Clear the whole list if there's no search
|
|
|
|
if (gSearchTerms == "") {
|
|
|
|
gDownloadManager.cleanUp();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove each download starting from the end until we hit a download
|
|
|
|
// that is in progress
|
|
|
|
let item;
|
|
|
|
while ((item = gDownloadsView.lastChild) && !item.inProgress)
|
|
|
|
removeDownload(item);
|
|
|
|
|
|
|
|
// Clear the input as if the user did it and move focus to the list
|
|
|
|
gSearchBox.value = "";
|
|
|
|
gSearchBox.doCommand();
|
|
|
|
gDownloadsView.focus();
|
|
|
|
}
|
|
|
|
|
2008-01-06 16:14:00 -08:00
|
|
|
// This is called by the progress listener.
|
2007-03-22 10:30:00 -07:00
|
|
|
var gLastComputedMean = -1;
|
|
|
|
var gLastActiveDownloads = 0;
|
|
|
|
function onUpdateProgress()
|
|
|
|
{
|
2008-01-26 13:02:32 -08:00
|
|
|
let numActiveDownloads = gDownloadManager.activeDownloadCount;
|
|
|
|
|
|
|
|
// Use the default title and reset "last" values if there's no downloads
|
|
|
|
if (numActiveDownloads == 0) {
|
2007-03-22 10:30:00 -07:00
|
|
|
document.title = document.documentElement.getAttribute("statictitle");
|
|
|
|
gLastComputedMean = -1;
|
2008-01-26 13:02:32 -08:00
|
|
|
gLastActiveDownloads = 0;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return;
|
|
|
|
}
|
2007-08-11 14:53:21 -07:00
|
|
|
|
|
|
|
// Establish the mean transfer speed and amount downloaded.
|
2007-03-22 10:30:00 -07:00
|
|
|
var mean = 0;
|
|
|
|
var base = 0;
|
2007-08-12 03:29:25 -07:00
|
|
|
var dls = gDownloadManager.activeDownloads;
|
2007-08-11 14:53:21 -07:00
|
|
|
while (dls.hasMoreElements()) {
|
2008-01-26 13:02:32 -08:00
|
|
|
let dl = dls.getNext().QueryInterface(Ci.nsIDownload);
|
2007-08-25 13:39:33 -07:00
|
|
|
if (dl.percentComplete < 100 && dl.size > 0) {
|
|
|
|
mean += dl.amountTransferred;
|
|
|
|
base += dl.size;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-01-26 13:02:32 -08:00
|
|
|
// Calculate the percent transferred, unless we don't have a total file size
|
|
|
|
let title = gStr.downloadsTitlePercent;
|
|
|
|
if (base == 0)
|
|
|
|
title = gStr.downloadsTitleFiles;
|
|
|
|
else
|
2007-03-22 10:30:00 -07:00
|
|
|
mean = Math.floor((mean / base) * 100);
|
|
|
|
|
2007-08-11 14:53:21 -07:00
|
|
|
// Update title of window
|
2007-03-22 10:30:00 -07:00
|
|
|
if (mean != gLastComputedMean || gLastActiveDownloads != numActiveDownloads) {
|
|
|
|
gLastComputedMean = mean;
|
|
|
|
gLastActiveDownloads = numActiveDownloads;
|
|
|
|
|
2008-01-26 13:02:32 -08:00
|
|
|
// Get the correct plural form and insert number of downloads and percent
|
|
|
|
title = PluralForm.get(numActiveDownloads, title);
|
|
|
|
title = replaceInsert(title, 1, numActiveDownloads);
|
|
|
|
title = replaceInsert(title, 2, mean);
|
|
|
|
|
|
|
|
document.title = title;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-11 15:48:43 -08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2007-08-11 14:53:21 -07:00
|
|
|
//// Startup, Shutdown
|
2007-11-11 15:48:43 -08:00
|
|
|
|
|
|
|
function Startup()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-12-15 17:35:38 -08:00
|
|
|
gDownloadsView = document.getElementById("downloadView");
|
|
|
|
gSearchBox = document.getElementById("searchbox");
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-10-01 17:44:48 -07:00
|
|
|
// convert strings to those in the string bundle
|
|
|
|
let (sb = document.getElementById("downloadStrings")) {
|
|
|
|
let getStr = function(string) sb.getString(string);
|
|
|
|
for (let [name, value] in Iterator(gStr))
|
|
|
|
gStr[name] = typeof value == "string" ? getStr(value) : value.map(getStr);
|
|
|
|
}
|
|
|
|
|
2008-11-03 15:22:21 -08:00
|
|
|
initStatement();
|
2008-02-26 09:52:17 -08:00
|
|
|
buildDownloadList(true);
|
2007-05-21 17:03:33 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// The DownloadProgressListener (DownloadProgressListener.js) handles progress
|
2007-11-11 15:48:43 -08:00
|
|
|
// notifications.
|
2007-08-11 14:53:21 -07:00
|
|
|
gDownloadListener = new DownloadProgressListener();
|
2007-05-25 16:47:53 -07:00
|
|
|
gDownloadManager.addListener(gDownloadListener);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-04-02 20:05:22 -07:00
|
|
|
// If the UI was displayed because the user interacted, we need to make sure
|
|
|
|
// we update gUserInteracted accordingly.
|
|
|
|
if (window.arguments[1] == Ci.nsIDownloadManagerUI.REASON_USER_INTERACTED)
|
|
|
|
gUserInteracted = true;
|
|
|
|
|
2007-08-11 14:53:21 -07:00
|
|
|
// downloads can finish before Startup() does, so check if the window should
|
|
|
|
// close and act accordingly
|
2007-06-15 14:19:08 -07:00
|
|
|
if (!autoRemoveAndClose())
|
|
|
|
gDownloadsView.focus();
|
2007-10-01 16:24:08 -07:00
|
|
|
|
2008-01-08 21:43:36 -08:00
|
|
|
let obs = Cc["@mozilla.org/observer-service;1"].
|
2007-10-27 13:37:44 -07:00
|
|
|
getService(Ci.nsIObserverService);
|
2007-10-07 10:21:57 -07:00
|
|
|
obs.addObserver(gDownloadObserver, "download-manager-remove-download", false);
|
2008-11-03 15:22:21 -08:00
|
|
|
obs.addObserver(gDownloadObserver, "private-browsing", false);
|
2008-02-21 12:49:56 -08:00
|
|
|
|
|
|
|
// Clear the search box and move focus to the list on escape from the box
|
|
|
|
gSearchBox.addEventListener("keypress", function(e) {
|
|
|
|
if (e.keyCode == e.DOM_VK_ESCAPE) {
|
|
|
|
// Move focus to the list instead of closing the window
|
|
|
|
gDownloadsView.focus();
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
2008-08-05 14:33:41 -07:00
|
|
|
}, false);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-11-11 15:48:43 -08:00
|
|
|
function Shutdown()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-05-25 16:47:53 -07:00
|
|
|
gDownloadManager.removeListener(gDownloadListener);
|
2007-10-01 16:24:08 -07:00
|
|
|
|
2008-01-08 21:43:36 -08:00
|
|
|
let obs = Cc["@mozilla.org/observer-service;1"].
|
2007-10-27 13:37:44 -07:00
|
|
|
getService(Ci.nsIObserverService);
|
2008-11-03 15:22:21 -08:00
|
|
|
obs.removeObserver(gDownloadObserver, "private-browsing");
|
2007-10-07 10:21:57 -07:00
|
|
|
obs.removeObserver(gDownloadObserver, "download-manager-remove-download");
|
2007-12-15 17:35:38 -08:00
|
|
|
|
|
|
|
clearTimeout(gBuilder);
|
|
|
|
gStmt.reset();
|
|
|
|
gStmt.finalize();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-01-08 21:43:36 -08:00
|
|
|
let gDownloadObserver = {
|
2007-10-07 10:21:57 -07:00
|
|
|
observe: function gdo_observe(aSubject, aTopic, aData) {
|
|
|
|
switch (aTopic) {
|
|
|
|
case "download-manager-remove-download":
|
2008-11-05 15:39:24 -08:00
|
|
|
// A null subject here indicates "remove multiple", so we just rebuild.
|
2007-10-07 10:21:57 -07:00
|
|
|
if (!aSubject) {
|
|
|
|
// Rebuild the default view
|
2008-02-26 09:52:17 -08:00
|
|
|
buildDownloadList(true);
|
2007-10-07 10:21:57 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, remove a single download
|
2008-01-08 21:43:36 -08:00
|
|
|
let id = aSubject.QueryInterface(Ci.nsISupportsPRUint32);
|
|
|
|
let dl = getDownload(id.data);
|
2007-10-07 10:21:57 -07:00
|
|
|
removeFromView(dl);
|
|
|
|
break;
|
2008-11-03 15:22:21 -08:00
|
|
|
case "private-browsing":
|
|
|
|
if (aData == "enter" || aData == "exit") {
|
2009-03-19 10:42:27 -07:00
|
|
|
// We need to reset the title here, because otherwise the title of
|
|
|
|
// the download manager would still reflect the progress of current
|
|
|
|
// active downloads, if any, after switching the private browsing
|
|
|
|
// mode, even though the downloads will no longer be accessible.
|
|
|
|
// If any download is auto-started after switching the private
|
|
|
|
// browsing mode, the title will be updated as needed by the progress
|
|
|
|
// listener.
|
|
|
|
document.title = document.documentElement.getAttribute("statictitle");
|
|
|
|
|
2008-11-12 12:13:12 -08:00
|
|
|
// We might get this notification before the download manager
|
|
|
|
// service, so the new database connection might not be ready
|
|
|
|
// yet. Defer this until all private-browsing notifications
|
|
|
|
// have been processed.
|
|
|
|
setTimeout(function() {
|
|
|
|
initStatement();
|
|
|
|
buildDownloadList(true);
|
|
|
|
}, 0);
|
2008-11-03 15:22:21 -08:00
|
|
|
}
|
|
|
|
break;
|
2007-10-07 10:21:57 -07:00
|
|
|
}
|
2007-10-01 16:24:08 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2007-11-11 15:48:43 -08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// View Context Menus
|
|
|
|
|
2007-08-21 09:37:39 -07:00
|
|
|
var gContextMenus = [
|
|
|
|
// DOWNLOAD_DOWNLOADING
|
2007-12-14 10:26:06 -08:00
|
|
|
[
|
|
|
|
"menuitem_pause"
|
|
|
|
, "menuitem_cancel"
|
|
|
|
, "menuseparator"
|
|
|
|
, "menuitem_show"
|
|
|
|
, "menuseparator"
|
2007-12-15 17:11:27 -08:00
|
|
|
, "menuitem_openReferrer"
|
2007-12-14 10:26:06 -08:00
|
|
|
, "menuitem_copyLocation"
|
2008-04-12 12:42:03 -07:00
|
|
|
, "menuseparator"
|
2008-04-19 00:36:39 -07:00
|
|
|
, "menuitem_selectAll"
|
2007-12-14 10:26:06 -08:00
|
|
|
],
|
2007-08-21 09:37:39 -07:00
|
|
|
// DOWNLOAD_FINISHED
|
2007-12-14 10:26:06 -08:00
|
|
|
[
|
|
|
|
"menuitem_open"
|
|
|
|
, "menuitem_show"
|
|
|
|
, "menuseparator"
|
2007-12-15 17:11:27 -08:00
|
|
|
, "menuitem_openReferrer"
|
2007-12-14 10:26:06 -08:00
|
|
|
, "menuitem_copyLocation"
|
|
|
|
, "menuseparator"
|
2008-04-19 00:36:39 -07:00
|
|
|
, "menuitem_selectAll"
|
|
|
|
, "menuseparator"
|
2007-12-14 10:26:06 -08:00
|
|
|
, "menuitem_removeFromList"
|
|
|
|
],
|
2007-08-21 09:37:39 -07:00
|
|
|
// DOWNLOAD_FAILED
|
2007-12-14 10:26:06 -08:00
|
|
|
[
|
|
|
|
"menuitem_retry"
|
|
|
|
, "menuseparator"
|
2007-12-15 17:11:27 -08:00
|
|
|
, "menuitem_openReferrer"
|
2007-12-14 10:26:06 -08:00
|
|
|
, "menuitem_copyLocation"
|
|
|
|
, "menuseparator"
|
2008-04-19 00:36:39 -07:00
|
|
|
, "menuitem_selectAll"
|
|
|
|
, "menuseparator"
|
2007-12-14 10:26:06 -08:00
|
|
|
, "menuitem_removeFromList"
|
|
|
|
],
|
2007-08-21 09:37:39 -07:00
|
|
|
// DOWNLOAD_CANCELED
|
2007-12-14 10:26:06 -08:00
|
|
|
[
|
|
|
|
"menuitem_retry"
|
|
|
|
, "menuseparator"
|
2007-12-15 17:11:27 -08:00
|
|
|
, "menuitem_openReferrer"
|
2007-12-14 10:26:06 -08:00
|
|
|
, "menuitem_copyLocation"
|
|
|
|
, "menuseparator"
|
2008-04-19 00:36:39 -07:00
|
|
|
, "menuitem_selectAll"
|
|
|
|
, "menuseparator"
|
2007-12-14 10:26:06 -08:00
|
|
|
, "menuitem_removeFromList"
|
|
|
|
],
|
2007-08-21 09:37:39 -07:00
|
|
|
// DOWNLOAD_PAUSED
|
2007-12-14 10:26:06 -08:00
|
|
|
[
|
|
|
|
"menuitem_resume"
|
|
|
|
, "menuitem_cancel"
|
|
|
|
, "menuseparator"
|
|
|
|
, "menuitem_show"
|
|
|
|
, "menuseparator"
|
2007-12-15 17:11:27 -08:00
|
|
|
, "menuitem_openReferrer"
|
2007-12-14 10:26:06 -08:00
|
|
|
, "menuitem_copyLocation"
|
2008-04-12 12:42:03 -07:00
|
|
|
, "menuseparator"
|
2008-04-19 00:36:39 -07:00
|
|
|
, "menuitem_selectAll"
|
2007-12-14 10:26:06 -08:00
|
|
|
],
|
2007-08-21 09:37:39 -07:00
|
|
|
// DOWNLOAD_QUEUED
|
2007-12-14 10:26:06 -08:00
|
|
|
[
|
|
|
|
"menuitem_cancel"
|
|
|
|
, "menuseparator"
|
|
|
|
, "menuitem_show"
|
|
|
|
, "menuseparator"
|
2007-12-15 17:11:27 -08:00
|
|
|
, "menuitem_openReferrer"
|
2007-12-14 10:26:06 -08:00
|
|
|
, "menuitem_copyLocation"
|
2008-04-12 12:42:03 -07:00
|
|
|
, "menuseparator"
|
2008-04-19 00:36:39 -07:00
|
|
|
, "menuitem_selectAll"
|
2007-12-14 10:26:06 -08:00
|
|
|
],
|
2008-03-21 17:23:45 -07:00
|
|
|
// DOWNLOAD_BLOCKED_PARENTAL
|
2007-12-14 10:26:06 -08:00
|
|
|
[
|
2007-12-15 17:11:27 -08:00
|
|
|
"menuitem_openReferrer"
|
|
|
|
, "menuitem_copyLocation"
|
2007-12-14 10:26:06 -08:00
|
|
|
, "menuseparator"
|
2008-04-19 00:36:39 -07:00
|
|
|
, "menuitem_selectAll"
|
|
|
|
, "menuseparator"
|
2007-12-14 10:26:06 -08:00
|
|
|
, "menuitem_removeFromList"
|
|
|
|
],
|
2007-09-17 13:55:06 -07:00
|
|
|
// DOWNLOAD_SCANNING
|
2007-12-14 10:26:06 -08:00
|
|
|
[
|
|
|
|
"menuitem_show"
|
|
|
|
, "menuseparator"
|
2007-12-15 17:11:27 -08:00
|
|
|
, "menuitem_openReferrer"
|
2007-12-14 10:26:06 -08:00
|
|
|
, "menuitem_copyLocation"
|
2008-04-12 12:42:03 -07:00
|
|
|
, "menuseparator"
|
2008-04-19 00:36:39 -07:00
|
|
|
, "menuitem_selectAll"
|
2007-12-14 10:26:06 -08:00
|
|
|
],
|
2007-11-30 22:49:07 -08:00
|
|
|
// DOWNLOAD_DIRTY
|
2008-03-21 17:23:45 -07:00
|
|
|
[
|
|
|
|
"menuitem_openReferrer"
|
|
|
|
, "menuitem_copyLocation"
|
|
|
|
, "menuseparator"
|
2008-04-19 00:36:39 -07:00
|
|
|
, "menuitem_selectAll"
|
|
|
|
, "menuseparator"
|
2008-03-21 17:23:45 -07:00
|
|
|
, "menuitem_removeFromList"
|
|
|
|
],
|
|
|
|
// DOWNLOAD_BLOCKED_POLICY
|
2007-12-14 10:26:06 -08:00
|
|
|
[
|
2007-12-15 17:11:27 -08:00
|
|
|
"menuitem_openReferrer"
|
|
|
|
, "menuitem_copyLocation"
|
2007-12-14 10:26:06 -08:00
|
|
|
, "menuseparator"
|
2008-04-19 00:36:39 -07:00
|
|
|
, "menuitem_selectAll"
|
|
|
|
, "menuseparator"
|
2007-12-14 10:26:06 -08:00
|
|
|
, "menuitem_removeFromList"
|
|
|
|
]
|
2007-03-22 10:30:00 -07:00
|
|
|
];
|
|
|
|
|
|
|
|
function buildContextMenu(aEvent)
|
|
|
|
{
|
|
|
|
if (aEvent.target.id != "downloadContextMenu")
|
|
|
|
return false;
|
2007-11-11 15:48:43 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
var popup = document.getElementById("downloadContextMenu");
|
|
|
|
while (popup.hasChildNodes())
|
|
|
|
popup.removeChild(popup.firstChild);
|
2007-11-11 15:48:43 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (gDownloadsView.selectedItem) {
|
2008-01-06 16:14:00 -08:00
|
|
|
let dl = gDownloadsView.selectedItem;
|
|
|
|
let idx = parseInt(dl.getAttribute("state"));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (idx < 0)
|
|
|
|
idx = 0;
|
2007-11-11 15:48:43 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
var menus = gContextMenus[idx];
|
2008-01-06 16:14:00 -08:00
|
|
|
for (let i = 0; i < menus.length; ++i) {
|
|
|
|
let menuitem = document.getElementById(menus[i]).cloneNode(true);
|
|
|
|
let cmd = menuitem.getAttribute("cmd");
|
|
|
|
if (cmd)
|
|
|
|
menuitem.disabled = !gDownloadViewController.isCommandEnabled(cmd, dl);
|
|
|
|
|
|
|
|
popup.appendChild(menuitem);
|
|
|
|
}
|
2007-11-11 15:48:43 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return true;
|
|
|
|
}
|
2007-11-11 15:48:43 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return false;
|
|
|
|
}
|
2007-11-11 15:48:43 -08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2007-08-11 14:53:21 -07:00
|
|
|
//// Drag and Drop
|
2007-03-22 10:30:00 -07:00
|
|
|
var gDownloadDNDObserver =
|
|
|
|
{
|
2010-02-07 07:44:48 -08:00
|
|
|
onDragStart: function (aEvent)
|
|
|
|
{
|
|
|
|
if (!gDownloadsView.selectedItem)
|
|
|
|
return;
|
|
|
|
var dl = gDownloadsView.selectedItem;
|
|
|
|
var f = getLocalFileFromNativePathOrUrl(dl.getAttribute("file"));
|
|
|
|
if (!f.exists())
|
|
|
|
return;
|
|
|
|
|
|
|
|
var dt = aEvent.dataTransfer;
|
|
|
|
dt.mozSetDataAt("application/x-moz-file", f, 0);
|
|
|
|
dt.effectAllowed = "copyMove";
|
|
|
|
},
|
|
|
|
|
2009-06-20 17:04:07 -07:00
|
|
|
onDragOver: function (aEvent)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-06-20 17:04:07 -07:00
|
|
|
var types = aEvent.dataTransfer.types;
|
|
|
|
if (types.contains("text/uri-list") ||
|
|
|
|
types.contains("text/x-moz-url") ||
|
|
|
|
types.contains("text/plain"))
|
|
|
|
aEvent.preventDefault();
|
2007-03-22 10:30:00 -07:00
|
|
|
},
|
2007-11-11 15:48:43 -08:00
|
|
|
|
2009-06-20 17:04:07 -07:00
|
|
|
onDrop: function(aEvent)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-06-20 17:04:07 -07:00
|
|
|
var dt = aEvent.dataTransfer;
|
|
|
|
var url = dt.getData("URL");
|
|
|
|
var name;
|
|
|
|
if (!url) {
|
|
|
|
url = dt.getData("text/x-moz-url") || dt.getData("text/plain");
|
|
|
|
[url, name] = url.split("\n");
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2009-06-20 17:04:07 -07:00
|
|
|
if (url)
|
|
|
|
saveURL(url, name ? name : url, null, true, true);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-11 15:48:43 -08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2007-08-11 14:53:21 -07:00
|
|
|
//// Command Updating and Command Handlers
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
var gDownloadViewController = {
|
2008-01-06 16:14:00 -08:00
|
|
|
isCommandEnabled: function(aCommand, aItem)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-01-06 16:14:00 -08:00
|
|
|
let dl = aItem;
|
2008-01-21 19:43:02 -08:00
|
|
|
let download = null; // used for getting an nsIDownload object
|
2007-08-11 14:53:21 -07:00
|
|
|
|
2007-07-11 02:26:09 -07:00
|
|
|
switch (aCommand) {
|
2007-08-11 14:53:21 -07:00
|
|
|
case "cmd_cancel":
|
|
|
|
return dl.inProgress;
|
2008-04-15 22:28:59 -07:00
|
|
|
case "cmd_open": {
|
2008-01-08 21:43:36 -08:00
|
|
|
let file = getLocalFileFromNativePathOrUrl(dl.getAttribute("file"));
|
2007-08-11 14:53:21 -07:00
|
|
|
return dl.openable && file.exists();
|
2008-04-15 22:28:59 -07:00
|
|
|
}
|
|
|
|
case "cmd_show": {
|
|
|
|
let file = getLocalFileFromNativePathOrUrl(dl.getAttribute("file"));
|
|
|
|
return file.exists();
|
|
|
|
}
|
2007-08-11 14:53:21 -07:00
|
|
|
case "cmd_pause":
|
2008-01-21 19:43:02 -08:00
|
|
|
download = gDownloadManager.getDownload(dl.getAttribute("dlid"));
|
|
|
|
return dl.inProgress && !dl.paused && download.resumable;
|
2007-08-11 14:53:21 -07:00
|
|
|
case "cmd_pauseResume":
|
2008-01-21 19:43:02 -08:00
|
|
|
download = gDownloadManager.getDownload(dl.getAttribute("dlid"));
|
|
|
|
return (dl.inProgress || dl.paused) && download.resumable;
|
2007-08-11 14:53:21 -07:00
|
|
|
case "cmd_resume":
|
2008-01-21 19:43:02 -08:00
|
|
|
download = gDownloadManager.getDownload(dl.getAttribute("dlid"));
|
|
|
|
return dl.paused && download.resumable;
|
2007-08-17 16:05:26 -07:00
|
|
|
case "cmd_openReferrer":
|
2007-12-15 17:11:27 -08:00
|
|
|
return dl.hasAttribute("referrer");
|
2007-11-13 12:12:38 -08:00
|
|
|
case "cmd_removeFromList":
|
2007-08-11 14:53:21 -07:00
|
|
|
case "cmd_retry":
|
|
|
|
return dl.removable;
|
2007-09-05 14:19:12 -07:00
|
|
|
case "cmd_copyLocation":
|
2007-08-11 14:53:21 -07:00
|
|
|
return true;
|
2007-07-11 02:26:09 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
return false;
|
|
|
|
},
|
2007-11-11 15:48:43 -08:00
|
|
|
|
2008-01-06 16:14:00 -08:00
|
|
|
doCommand: function(aCommand, aItem)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-01-06 16:14:00 -08:00
|
|
|
if (this.isCommandEnabled(aCommand, aItem))
|
|
|
|
this.commands[aCommand](aItem);
|
2007-07-11 02:26:09 -07:00
|
|
|
},
|
2007-11-11 15:48:43 -08:00
|
|
|
|
2007-07-11 02:26:09 -07:00
|
|
|
commands: {
|
2007-08-11 14:53:21 -07:00
|
|
|
cmd_cancel: function(aSelectedItem) {
|
|
|
|
cancelDownload(aSelectedItem);
|
|
|
|
},
|
|
|
|
cmd_open: function(aSelectedItem) {
|
|
|
|
openDownload(aSelectedItem);
|
|
|
|
},
|
2007-08-17 16:05:26 -07:00
|
|
|
cmd_openReferrer: function(aSelectedItem) {
|
|
|
|
openReferrer(aSelectedItem);
|
|
|
|
},
|
2007-08-11 14:53:21 -07:00
|
|
|
cmd_pause: function(aSelectedItem) {
|
|
|
|
pauseDownload(aSelectedItem);
|
|
|
|
},
|
|
|
|
cmd_pauseResume: function(aSelectedItem) {
|
2008-01-26 19:28:32 -08:00
|
|
|
if (aSelectedItem.paused)
|
|
|
|
this.cmd_resume(aSelectedItem);
|
2007-08-11 14:53:21 -07:00
|
|
|
else
|
2008-01-26 19:28:32 -08:00
|
|
|
this.cmd_pause(aSelectedItem);
|
2007-07-11 02:26:09 -07:00
|
|
|
},
|
2007-11-13 12:12:38 -08:00
|
|
|
cmd_removeFromList: function(aSelectedItem) {
|
2007-08-11 14:53:21 -07:00
|
|
|
removeDownload(aSelectedItem);
|
|
|
|
},
|
|
|
|
cmd_resume: function(aSelectedItem) {
|
|
|
|
resumeDownload(aSelectedItem);
|
|
|
|
},
|
|
|
|
cmd_retry: function(aSelectedItem) {
|
|
|
|
retryDownload(aSelectedItem);
|
|
|
|
},
|
|
|
|
cmd_show: function(aSelectedItem) {
|
|
|
|
showDownload(aSelectedItem);
|
|
|
|
},
|
2007-09-05 14:19:12 -07:00
|
|
|
cmd_copyLocation: function(aSelectedItem) {
|
|
|
|
copySourceLocation(aSelectedItem);
|
2007-10-27 13:42:48 -07:00
|
|
|
},
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2008-01-06 16:14:00 -08:00
|
|
|
/**
|
|
|
|
* Helper function to do commands.
|
|
|
|
*
|
|
|
|
* @param aCmd
|
|
|
|
* The command to be performed.
|
|
|
|
* @param aItem
|
|
|
|
* The richlistitem that represents the download that will have the
|
2008-04-08 22:33:39 -07:00
|
|
|
* 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
|
2008-01-06 16:14:00 -08:00
|
|
|
* 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;
|
|
|
|
if (!elm) {
|
2008-04-08 22:33:39 -07:00
|
|
|
// If we don't have a desired download item, do the command for all
|
|
|
|
// selected items. Initialize the callback to null so commands know to add
|
|
|
|
// a callback if they want. We will call the callback with empty arguments
|
|
|
|
// after performing the command on each selected download item.
|
|
|
|
gPerformAllCallback = null;
|
|
|
|
|
|
|
|
// Convert the nodelist into an array to keep a copy of the download items
|
|
|
|
let items = [];
|
|
|
|
for (let i = gDownloadsView.selectedItems.length; --i >= 0; )
|
|
|
|
items.unshift(gDownloadsView.selectedItems[i]);
|
|
|
|
|
|
|
|
// Do the command for each download item
|
|
|
|
for each (let item in items)
|
|
|
|
performCommand(aCmd, item);
|
|
|
|
|
|
|
|
// Call the callback with no arguments and reset because we're done
|
|
|
|
if (typeof gPerformAllCallback == "function")
|
|
|
|
gPerformAllCallback();
|
|
|
|
gPerformAllCallback = undefined;
|
|
|
|
|
|
|
|
return;
|
2008-01-06 16:14:00 -08:00
|
|
|
} else {
|
|
|
|
while (elm.nodeName != "richlistitem" ||
|
|
|
|
elm.getAttribute("type") != "download")
|
|
|
|
elm = elm.parentNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
gDownloadViewController.doCommand(aCmd, elm);
|
|
|
|
}
|
|
|
|
|
2007-11-22 01:22:25 -08:00
|
|
|
function setSearchboxFocus()
|
|
|
|
{
|
2007-12-15 17:35:38 -08:00
|
|
|
gSearchBox.focus();
|
|
|
|
gSearchBox.select();
|
2007-11-22 01:22:25 -08:00
|
|
|
}
|
|
|
|
|
2007-08-11 14:53:21 -07:00
|
|
|
function openExternal(aFile)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-08-11 14:53:21 -07:00
|
|
|
var uri = Cc["@mozilla.org/network/io-service;1"].
|
|
|
|
getService(Ci.nsIIOService).newFileURI(aFile);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-08-11 14:53:21 -07:00
|
|
|
var protocolSvc = Cc["@mozilla.org/uriloader/external-protocol-service;1"].
|
|
|
|
getService(Ci.nsIExternalProtocolService);
|
|
|
|
protocolSvc.loadUrl(uri);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-11-11 15:48:43 -08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2007-12-15 17:35:38 -08:00
|
|
|
//// Utility Functions
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a download richlistitem with the provided attributes. Some attributes
|
|
|
|
* are *required* while optional ones will only be set on the item if provided.
|
|
|
|
*
|
|
|
|
* @param aAttrs
|
|
|
|
* An object that must have the following properties: dlid, file,
|
|
|
|
* target, uri, state, progress, startTime, endTime, currBytes,
|
|
|
|
* maxBytes; optional properties: referrer
|
|
|
|
* @return An initialized download richlistitem
|
|
|
|
*/
|
|
|
|
function createDownloadItem(aAttrs)
|
|
|
|
{
|
2008-01-08 21:43:36 -08:00
|
|
|
let dl = document.createElement("richlistitem");
|
2007-12-15 17:35:38 -08:00
|
|
|
|
|
|
|
// Copy the attributes from the argument into the item
|
|
|
|
for (let attr in aAttrs)
|
|
|
|
dl.setAttribute(attr, aAttrs[attr]);
|
|
|
|
|
|
|
|
// Initialize other attributes
|
|
|
|
dl.setAttribute("type", "download");
|
|
|
|
dl.setAttribute("id", "dl" + aAttrs.dlid);
|
|
|
|
dl.setAttribute("image", "moz-icon://" + aAttrs.file + "?size=32");
|
|
|
|
dl.setAttribute("lastSeconds", Infinity);
|
|
|
|
|
|
|
|
// Initialize more complex attributes
|
|
|
|
updateTime(dl);
|
|
|
|
updateStatus(dl);
|
|
|
|
|
|
|
|
try {
|
|
|
|
let file = getLocalFileFromNativePathOrUrl(aAttrs.file);
|
|
|
|
dl.setAttribute("path", file.nativePath || file.path);
|
|
|
|
return dl;
|
|
|
|
} catch (e) {
|
|
|
|
// aFile might not be a file: url or a valid native path
|
|
|
|
// see bug #392386 for details
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2007-08-11 14:53:21 -07:00
|
|
|
|
2008-01-06 16:14:00 -08:00
|
|
|
/**
|
|
|
|
* Updates the disabled state of the buttons of a downlaod.
|
|
|
|
*
|
|
|
|
* @param aItem
|
|
|
|
* The richlistitem representing the download.
|
|
|
|
*/
|
|
|
|
function updateButtons(aItem)
|
|
|
|
{
|
|
|
|
let buttons = aItem.buttons;
|
|
|
|
|
|
|
|
for (let i = 0; i < buttons.length; ++i) {
|
|
|
|
let cmd = buttons[i].getAttribute("cmd");
|
|
|
|
let enabled = gDownloadViewController.isCommandEnabled(cmd, aItem);
|
|
|
|
buttons[i].disabled = !enabled;
|
2008-01-21 19:43:02 -08:00
|
|
|
|
|
|
|
if ("cmd_pause" == cmd && !enabled) {
|
|
|
|
// We need to add the tooltip indicating that the download cannot be
|
|
|
|
// paused now.
|
|
|
|
buttons[i].setAttribute("tooltiptext", gStr.cannotPause);
|
|
|
|
}
|
2008-01-06 16:14:00 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-01 17:44:48 -07:00
|
|
|
/**
|
|
|
|
* Updates the status for a download item depending on its state
|
|
|
|
*
|
|
|
|
* @param aItem
|
|
|
|
* The richlistitem that has various download attributes.
|
|
|
|
* @param aDownload
|
|
|
|
* The nsDownload from the backend. This is an optional parameter, but
|
|
|
|
* is useful for certain states such as DOWNLOADING.
|
|
|
|
*/
|
|
|
|
function updateStatus(aItem, aDownload) {
|
2008-01-08 21:43:36 -08:00
|
|
|
let status = "";
|
|
|
|
let statusTip = "";
|
2007-10-01 17:44:48 -07:00
|
|
|
|
2008-01-08 21:43:36 -08:00
|
|
|
let state = Number(aItem.getAttribute("state"));
|
2007-10-01 17:44:48 -07:00
|
|
|
switch (state) {
|
2007-11-11 15:48:43 -08:00
|
|
|
case nsIDM.DOWNLOAD_PAUSED:
|
2008-01-22 18:15:08 -08:00
|
|
|
{
|
2008-01-08 21:43:36 -08:00
|
|
|
let currBytes = Number(aItem.getAttribute("currBytes"));
|
|
|
|
let maxBytes = Number(aItem.getAttribute("maxBytes"));
|
2007-10-01 17:44:48 -07:00
|
|
|
|
2008-01-22 18:15:08 -08:00
|
|
|
let transfer = DownloadUtils.getTransferTotal(currBytes, maxBytes);
|
|
|
|
status = replaceInsert(gStr.paused, 1, transfer);
|
2007-10-01 17:44:48 -07:00
|
|
|
|
2008-01-22 18:15:08 -08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case nsIDM.DOWNLOAD_DOWNLOADING:
|
|
|
|
{
|
|
|
|
let currBytes = Number(aItem.getAttribute("currBytes"));
|
|
|
|
let maxBytes = Number(aItem.getAttribute("maxBytes"));
|
|
|
|
// If we don't have an active download, assume 0 bytes/sec
|
2008-01-08 21:43:36 -08:00
|
|
|
let speed = aDownload ? aDownload.speed : 0;
|
2008-01-22 18:15:08 -08:00
|
|
|
let lastSec = Number(aItem.getAttribute("lastSeconds"));
|
2007-10-01 17:44:48 -07:00
|
|
|
|
2008-01-22 18:15:08 -08:00
|
|
|
let newLast;
|
|
|
|
[status, newLast] =
|
|
|
|
DownloadUtils.getDownloadStatus(currBytes, maxBytes, speed, lastSec);
|
2007-10-01 17:44:48 -07:00
|
|
|
|
2008-01-22 18:15:08 -08:00
|
|
|
// Update lastSeconds to be the new value
|
|
|
|
aItem.setAttribute("lastSeconds", newLast);
|
2007-10-01 17:44:48 -07:00
|
|
|
|
2007-11-11 15:52:49 -08:00
|
|
|
break;
|
2008-01-22 18:15:08 -08:00
|
|
|
}
|
2007-11-11 15:52:49 -08:00
|
|
|
case nsIDM.DOWNLOAD_FINISHED:
|
2007-11-11 15:56:14 -08:00
|
|
|
case nsIDM.DOWNLOAD_FAILED:
|
|
|
|
case nsIDM.DOWNLOAD_CANCELED:
|
2008-03-21 17:23:45 -07:00
|
|
|
case nsIDM.DOWNLOAD_BLOCKED_PARENTAL:
|
|
|
|
case nsIDM.DOWNLOAD_BLOCKED_POLICY:
|
2007-11-30 22:49:07 -08:00
|
|
|
case nsIDM.DOWNLOAD_DIRTY:
|
2008-01-22 18:15:08 -08:00
|
|
|
{
|
2007-11-11 15:52:49 -08:00
|
|
|
let (stateSize = {}) {
|
|
|
|
stateSize[nsIDM.DOWNLOAD_FINISHED] = function() {
|
|
|
|
// Display the file size, but show "Unknown" for negative sizes
|
|
|
|
let fileSize = Number(aItem.getAttribute("maxBytes"));
|
|
|
|
let sizeText = gStr.doneSizeUnknown;
|
|
|
|
if (fileSize >= 0) {
|
2008-01-22 18:15:08 -08:00
|
|
|
let [size, unit] = DownloadUtils.convertByteUnits(fileSize);
|
2007-11-11 15:52:49 -08:00
|
|
|
sizeText = replaceInsert(gStr.doneSize, 1, size);
|
|
|
|
sizeText = replaceInsert(sizeText, 2, unit);
|
|
|
|
}
|
|
|
|
return sizeText;
|
|
|
|
};
|
2007-11-11 15:56:14 -08:00
|
|
|
stateSize[nsIDM.DOWNLOAD_FAILED] = function() gStr.stateFailed;
|
|
|
|
stateSize[nsIDM.DOWNLOAD_CANCELED] = function() gStr.stateCanceled;
|
2008-03-21 17:23:45 -07:00
|
|
|
stateSize[nsIDM.DOWNLOAD_BLOCKED_PARENTAL] = function() gStr.stateBlockedParentalControls;
|
|
|
|
stateSize[nsIDM.DOWNLOAD_BLOCKED_POLICY] = function() gStr.stateBlockedPolicy;
|
2007-11-30 22:49:07 -08:00
|
|
|
stateSize[nsIDM.DOWNLOAD_DIRTY] = function() gStr.stateDirty;
|
2007-11-11 15:52:49 -08:00
|
|
|
|
|
|
|
// Insert 1 is the download size or download state
|
|
|
|
status = replaceInsert(gStr.doneStatus, 1, stateSize[state]());
|
|
|
|
}
|
|
|
|
|
2008-01-22 18:15:08 -08:00
|
|
|
let [displayHost, fullHost] =
|
|
|
|
DownloadUtils.getURIHost(getReferrerOrSource(aItem));
|
2007-12-26 15:37:13 -08:00
|
|
|
// Insert 2 is the eTLD + 1 or other variations of the host
|
|
|
|
status = replaceInsert(status, 2, displayHost);
|
|
|
|
// Set the tooltip to be the full host
|
|
|
|
statusTip = fullHost;
|
2007-11-11 15:52:49 -08:00
|
|
|
|
2007-10-01 17:44:48 -07:00
|
|
|
break;
|
2008-01-22 18:15:08 -08:00
|
|
|
}
|
2007-10-01 17:44:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
aItem.setAttribute("status", status);
|
2007-12-26 15:37:13 -08:00
|
|
|
aItem.setAttribute("statusTip", statusTip != "" ? statusTip : status);
|
2007-10-01 17:44:48 -07:00
|
|
|
}
|
|
|
|
|
2007-12-14 02:00:16 -08:00
|
|
|
/**
|
|
|
|
* Updates the time that gets shown for completed download items
|
|
|
|
*
|
|
|
|
* @param aItem
|
|
|
|
* The richlistitem representing a download in the UI
|
|
|
|
*/
|
|
|
|
function updateTime(aItem)
|
|
|
|
{
|
|
|
|
// Don't bother updating for things that aren't finished
|
|
|
|
if (aItem.inProgress)
|
|
|
|
return;
|
|
|
|
|
2008-01-08 21:43:36 -08:00
|
|
|
let dts = Cc["@mozilla.org/intl/scriptabledateformat;1"].
|
2007-12-21 15:18:05 -08:00
|
|
|
getService(Ci.nsIScriptableDateFormat);
|
|
|
|
|
2007-12-14 02:00:16 -08:00
|
|
|
// Figure out when today begins
|
2008-01-08 21:43:36 -08:00
|
|
|
let now = new Date();
|
|
|
|
let today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
|
2007-12-14 02:00:16 -08:00
|
|
|
|
|
|
|
// Get the end time to display
|
2008-01-08 21:43:36 -08:00
|
|
|
let end = new Date(parseInt(aItem.getAttribute("endTime")));
|
2007-12-14 02:00:16 -08:00
|
|
|
|
|
|
|
// Figure out if the end time is from today, yesterday, this week, etc.
|
2008-01-08 21:43:36 -08:00
|
|
|
let dateTime;
|
2007-12-14 02:00:16 -08:00
|
|
|
if (end >= today) {
|
|
|
|
// Download finished after today started, show the time
|
|
|
|
dateTime = dts.FormatTime("", dts.timeFormatNoSeconds,
|
|
|
|
end.getHours(), end.getMinutes(), 0);
|
|
|
|
} else if (today - end < (24 * 60 * 60 * 1000)) {
|
|
|
|
// Download finished after yesterday started, show yesterday
|
|
|
|
dateTime = gStr.yesterday;
|
|
|
|
} else if (today - end < (6 * 24 * 60 * 60 * 1000)) {
|
|
|
|
// Download finished after last week started, show day of week
|
|
|
|
dateTime = end.toLocaleFormat("%A");
|
|
|
|
} else {
|
|
|
|
// Download must have been from some time ago.. show month/day
|
2008-01-08 21:43:36 -08:00
|
|
|
let month = end.toLocaleFormat("%B");
|
2007-12-21 14:55:56 -08:00
|
|
|
// Remove leading 0 by converting the date string to a number
|
2008-01-08 21:43:36 -08:00
|
|
|
let date = Number(end.toLocaleFormat("%d"));
|
2007-12-14 02:00:16 -08:00
|
|
|
dateTime = replaceInsert(gStr.monthDate, 1, month);
|
|
|
|
dateTime = replaceInsert(dateTime, 2, date);
|
|
|
|
}
|
|
|
|
|
|
|
|
aItem.setAttribute("dateTime", dateTime);
|
2007-12-21 15:18:05 -08:00
|
|
|
|
|
|
|
// Set the tooltip to be the full date and time
|
2008-01-08 21:43:36 -08:00
|
|
|
let dateTimeTip = dts.FormatDateTime("",
|
2007-12-21 15:18:05 -08:00
|
|
|
dts.dateFormatLong,
|
|
|
|
dts.timeFormatNoSeconds,
|
|
|
|
end.getFullYear(),
|
|
|
|
end.getMonth() + 1,
|
|
|
|
end.getDate(),
|
|
|
|
end.getHours(),
|
|
|
|
end.getMinutes(),
|
|
|
|
0);
|
|
|
|
|
|
|
|
aItem.setAttribute("dateTimeTip", dateTimeTip);
|
2007-12-14 02:00:16 -08:00
|
|
|
}
|
|
|
|
|
2007-10-01 17:44:48 -07:00
|
|
|
/**
|
2008-01-22 18:15:08 -08:00
|
|
|
* Helper function to replace a placeholder string with a real string
|
2007-10-01 17:44:48 -07:00
|
|
|
*
|
2008-01-22 18:15:08 -08:00
|
|
|
* @param aText
|
|
|
|
* Source text containing placeholder (e.g., #1)
|
|
|
|
* @param aIndex
|
|
|
|
* Index number of placeholder to replace
|
|
|
|
* @param aValue
|
|
|
|
* New string to put in place of placeholder
|
|
|
|
* @return The string with placeholder replaced with the new string
|
2007-10-01 17:44:48 -07:00
|
|
|
*/
|
|
|
|
function replaceInsert(aText, aIndex, aValue)
|
|
|
|
{
|
|
|
|
return aText.replace("#" + aIndex, aValue);
|
|
|
|
}
|
|
|
|
|
2007-12-18 00:47:48 -08:00
|
|
|
/**
|
|
|
|
* Perform the default action for the currently selected download item
|
|
|
|
*/
|
|
|
|
function doDefaultForSelected()
|
|
|
|
{
|
|
|
|
// Make sure we have something selected
|
2008-01-08 21:43:36 -08:00
|
|
|
let item = gDownloadsView.selectedItem;
|
2007-12-18 00:47:48 -08:00
|
|
|
if (!item)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Get the default action (first item in the menu)
|
2008-01-08 21:43:36 -08:00
|
|
|
let state = Number(item.getAttribute("state"));
|
|
|
|
let menuitem = document.getElementById(gContextMenus[state][0]);
|
2007-12-18 00:47:48 -08:00
|
|
|
|
|
|
|
// Try to do the action if the command is enabled
|
2008-01-06 16:14:00 -08:00
|
|
|
gDownloadViewController.doCommand(menuitem.getAttribute("cmd"), item);
|
2007-12-18 00:47:48 -08:00
|
|
|
}
|
|
|
|
|
2007-09-18 23:06:34 -07:00
|
|
|
function removeFromView(aDownload)
|
|
|
|
{
|
2007-10-07 10:21:57 -07:00
|
|
|
// Make sure we have an item to remove
|
|
|
|
if (!aDownload) return;
|
|
|
|
|
2008-01-08 21:43:36 -08:00
|
|
|
let index = gDownloadsView.selectedIndex;
|
2007-09-18 23:06:34 -07:00
|
|
|
gDownloadsView.removeChild(aDownload);
|
|
|
|
gDownloadsView.selectedIndex = Math.min(index, gDownloadsView.itemCount - 1);
|
2007-12-15 17:35:38 -08:00
|
|
|
|
2008-04-24 22:59:31 -07:00
|
|
|
// We might have removed the last item, so update the clear list button
|
|
|
|
updateClearListButton();
|
2007-09-18 23:06:34 -07:00
|
|
|
}
|
|
|
|
|
2007-10-27 13:37:44 -07:00
|
|
|
function getReferrerOrSource(aDownload)
|
|
|
|
{
|
|
|
|
// Give the referrer if we have it set
|
|
|
|
if (aDownload.hasAttribute("referrer"))
|
|
|
|
return aDownload.getAttribute("referrer");
|
|
|
|
|
|
|
|
// Otherwise, provide the source
|
|
|
|
return aDownload.getAttribute("uri");
|
|
|
|
}
|
|
|
|
|
2007-08-11 14:53:21 -07:00
|
|
|
/**
|
2007-12-15 17:35:38 -08:00
|
|
|
* Initiate building the download list to have the active downloads followed by
|
|
|
|
* completed ones filtered by the search term if necessary.
|
2008-02-26 09:52:17 -08:00
|
|
|
*
|
|
|
|
* @param aForceBuild
|
|
|
|
* Force the list to be built even if the search terms don't change
|
2007-08-11 14:53:21 -07:00
|
|
|
*/
|
2008-02-26 09:52:17 -08:00
|
|
|
function buildDownloadList(aForceBuild)
|
2007-08-11 14:53:21 -07:00
|
|
|
{
|
2008-02-26 09:52:17 -08:00
|
|
|
// Stringify the previous search
|
2008-03-18 10:59:27 -07:00
|
|
|
let prevSearch = gSearchTerms.join(" ");
|
2008-02-26 09:52:17 -08:00
|
|
|
|
|
|
|
// Array of space-separated lower-case search terms
|
|
|
|
gSearchTerms = gSearchBox.value.replace(/^\s+|\s+$/g, "").
|
|
|
|
toLowerCase().split(/\s+/);
|
|
|
|
|
|
|
|
// Unless forced, don't rebuild the download list if the search didn't change
|
|
|
|
if (!aForceBuild && gSearchTerms.join(" ") == prevSearch)
|
|
|
|
return;
|
|
|
|
|
2007-12-15 17:35:38 -08:00
|
|
|
// Clear out values before using them
|
|
|
|
clearTimeout(gBuilder);
|
|
|
|
gStmt.reset();
|
|
|
|
|
|
|
|
// Clear the list before adding items by replacing with a shallow copy
|
|
|
|
let (empty = gDownloadsView.cloneNode(false)) {
|
|
|
|
gDownloadsView.parentNode.replaceChild(empty, gDownloadsView);
|
|
|
|
gDownloadsView = empty;
|
|
|
|
}
|
2007-08-17 15:19:08 -07:00
|
|
|
|
2007-12-15 17:35:38 -08:00
|
|
|
try {
|
|
|
|
gStmt.bindInt32Parameter(0, nsIDM.DOWNLOAD_NOTSTARTED);
|
|
|
|
gStmt.bindInt32Parameter(1, nsIDM.DOWNLOAD_DOWNLOADING);
|
|
|
|
gStmt.bindInt32Parameter(2, nsIDM.DOWNLOAD_PAUSED);
|
|
|
|
gStmt.bindInt32Parameter(3, nsIDM.DOWNLOAD_QUEUED);
|
|
|
|
gStmt.bindInt32Parameter(4, nsIDM.DOWNLOAD_SCANNING);
|
|
|
|
} catch (e) {
|
|
|
|
// Something must have gone wrong when binding, so clear and quit
|
|
|
|
gStmt.reset();
|
|
|
|
return;
|
2007-10-29 11:31:27 -07:00
|
|
|
}
|
2007-12-15 17:35:38 -08:00
|
|
|
|
2007-12-21 14:58:07 -08:00
|
|
|
// Take a quick break before we actually start building the list
|
|
|
|
gBuilder = setTimeout(function() {
|
|
|
|
// Start building the list and select the first item
|
|
|
|
stepListBuilder(1);
|
|
|
|
gDownloadsView.selectedIndex = 0;
|
2008-04-24 22:59:31 -07:00
|
|
|
|
|
|
|
// We just tried to add a single item, so we probably need to enable
|
|
|
|
updateClearListButton();
|
2007-12-21 14:58:07 -08:00
|
|
|
}, 0);
|
2007-10-29 11:31:27 -07:00
|
|
|
}
|
|
|
|
|
2007-08-11 14:53:21 -07:00
|
|
|
/**
|
2007-12-15 17:35:38 -08:00
|
|
|
* Incrementally build the download list by adding at most the requested number
|
|
|
|
* of items if there are items to add. After doing that, it will schedule
|
|
|
|
* another chunk of items specified by gListBuildDelay and gListBuildChunk.
|
2007-08-11 14:53:21 -07:00
|
|
|
*
|
2007-12-15 17:35:38 -08:00
|
|
|
* @param aNumItems
|
|
|
|
* Number of items to add to the list before taking a break
|
2007-08-11 14:53:21 -07:00
|
|
|
*/
|
2007-12-15 17:35:38 -08:00
|
|
|
function stepListBuilder(aNumItems) {
|
|
|
|
try {
|
|
|
|
// If we're done adding all items, we can quit
|
2008-04-05 21:26:18 -07:00
|
|
|
if (!gStmt.executeStep()) {
|
2008-04-24 22:59:31 -07:00
|
|
|
// Send a notification that we finished, but wait for clear list to update
|
2008-05-06 16:25:36 -07:00
|
|
|
updateClearListButton();
|
2008-04-24 22:59:31 -07:00
|
|
|
setTimeout(function() Cc["@mozilla.org/observer-service;1"].
|
|
|
|
getService(Ci.nsIObserverService).
|
|
|
|
notifyObservers(window, "download-manager-ui-done", null), 0);
|
2008-04-05 21:26:18 -07:00
|
|
|
|
2007-12-15 17:35:38 -08:00
|
|
|
return;
|
2008-04-05 21:26:18 -07:00
|
|
|
}
|
2007-12-15 17:35:38 -08:00
|
|
|
|
|
|
|
// Try to get the attribute values from the statement
|
|
|
|
let attrs = {
|
|
|
|
dlid: gStmt.getInt64(0),
|
|
|
|
file: gStmt.getString(1),
|
|
|
|
target: gStmt.getString(2),
|
|
|
|
uri: gStmt.getString(3),
|
|
|
|
state: gStmt.getInt32(4),
|
|
|
|
startTime: Math.round(gStmt.getInt64(5) / 1000),
|
|
|
|
endTime: Math.round(gStmt.getInt64(6) / 1000),
|
|
|
|
currBytes: gStmt.getInt64(8),
|
|
|
|
maxBytes: gStmt.getInt64(9)
|
|
|
|
};
|
|
|
|
|
|
|
|
// Only add the referrer if it's not null
|
|
|
|
let (referrer = gStmt.getString(7)) {
|
|
|
|
if (referrer)
|
|
|
|
attrs.referrer = referrer;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-12-15 17:35:38 -08:00
|
|
|
// If the download is active, grab the real progress, otherwise default 100
|
2008-02-26 09:52:17 -08:00
|
|
|
let isActive = gStmt.getInt32(10);
|
|
|
|
attrs.progress = isActive ? gDownloadManager.getDownload(attrs.dlid).
|
|
|
|
percentComplete : 100;
|
2007-08-11 14:53:21 -07:00
|
|
|
|
2008-02-26 09:52:17 -08:00
|
|
|
// Make the item and add it to the end if it's active or matches the search
|
2007-12-15 17:35:38 -08:00
|
|
|
let item = createDownloadItem(attrs);
|
2008-02-26 09:52:17 -08:00
|
|
|
if (item && (isActive || downloadMatchesSearch(item))) {
|
2008-06-11 07:11:14 -07:00
|
|
|
// Add item to the end
|
2007-12-15 17:35:38 -08:00
|
|
|
gDownloadsView.appendChild(item);
|
2008-01-06 16:14:00 -08:00
|
|
|
|
|
|
|
// Because of the joys of XBL, we can't update the buttons until the
|
|
|
|
// download object is in the document.
|
|
|
|
updateButtons(item);
|
2008-02-26 09:52:17 -08:00
|
|
|
} else {
|
|
|
|
// We didn't add an item, so bump up the number of items to process, but
|
|
|
|
// not a whole number so that we eventually do pause for a chunk break
|
|
|
|
aNumItems += .9;
|
2007-12-15 17:35:38 -08:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
// Something went wrong when stepping or getting values, so clear and quit
|
|
|
|
gStmt.reset();
|
|
|
|
return;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-12-15 17:35:38 -08:00
|
|
|
// Add another item to the list if we should; otherwise, let the UI update
|
|
|
|
// and continue later
|
|
|
|
if (aNumItems > 1) {
|
|
|
|
stepListBuilder(aNumItems - 1);
|
|
|
|
} else {
|
|
|
|
// Use a shorter delay for earlier downloads to display them faster
|
2007-12-21 19:45:54 -08:00
|
|
|
let delay = Math.min(gDownloadsView.itemCount * 10, gListBuildDelay);
|
2007-12-15 17:35:38 -08:00
|
|
|
gBuilder = setTimeout(stepListBuilder, delay, gListBuildChunk);
|
2007-08-11 14:53:21 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2007-12-15 17:35:38 -08:00
|
|
|
* Add a download to the front of the download list
|
2007-08-11 14:53:21 -07:00
|
|
|
*
|
2007-12-15 17:35:38 -08:00
|
|
|
* @param aDownload
|
|
|
|
* The nsIDownload to make into a richlistitem
|
2007-08-11 14:53:21 -07:00
|
|
|
*/
|
2007-12-15 17:35:38 -08:00
|
|
|
function prependList(aDownload)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-01-08 21:43:36 -08:00
|
|
|
let attrs = {
|
2007-12-15 17:35:38 -08:00
|
|
|
dlid: aDownload.id,
|
|
|
|
file: aDownload.target.spec,
|
|
|
|
target: aDownload.displayName,
|
|
|
|
uri: aDownload.source.spec,
|
|
|
|
state: aDownload.state,
|
|
|
|
progress: aDownload.percentComplete,
|
|
|
|
startTime: Math.round(aDownload.startTime / 1000),
|
|
|
|
endTime: Date.now(),
|
|
|
|
currBytes: aDownload.amountTransferred,
|
|
|
|
maxBytes: aDownload.size
|
|
|
|
};
|
|
|
|
|
|
|
|
// Make the item and add it to the beginning
|
2008-01-08 21:43:36 -08:00
|
|
|
let item = createDownloadItem(attrs);
|
2007-12-15 17:35:38 -08:00
|
|
|
if (item) {
|
2008-06-11 07:11:14 -07:00
|
|
|
// Add item to the beginning
|
2007-12-15 17:35:38 -08:00
|
|
|
gDownloadsView.insertBefore(item, gDownloadsView.firstChild);
|
2008-01-06 16:14:00 -08:00
|
|
|
|
|
|
|
// Because of the joys of XBL, we can't update the buttons until the
|
|
|
|
// download object is in the document.
|
|
|
|
updateButtons(item);
|
2008-04-24 22:59:31 -07:00
|
|
|
|
|
|
|
// We might have added an item to an empty list, so update button
|
|
|
|
updateClearListButton();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-26 09:52:17 -08:00
|
|
|
/**
|
|
|
|
* Check if the download matches the current search term based on the texts
|
|
|
|
* shown to the user. All search terms are checked to see if each matches any
|
|
|
|
* of the displayed texts.
|
|
|
|
*
|
|
|
|
* @param aItem
|
|
|
|
* Download richlistitem to check if it matches the current search
|
|
|
|
* @return Boolean true if it matches the search; false otherwise
|
|
|
|
*/
|
|
|
|
function downloadMatchesSearch(aItem)
|
|
|
|
{
|
|
|
|
// Search through the download attributes that are shown to the user and
|
|
|
|
// make it into one big string for easy combined searching
|
|
|
|
let combinedSearch = "";
|
|
|
|
for each (let attr in gSearchAttributes)
|
|
|
|
combinedSearch += aItem.getAttribute(attr).toLowerCase() + " ";
|
|
|
|
|
|
|
|
// Make sure each of the terms are found
|
|
|
|
for each (let term in gSearchTerms)
|
2009-06-28 09:03:01 -07:00
|
|
|
if (combinedSearch.indexOf(term) == -1)
|
2008-02-26 09:52:17 -08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-11-11 15:48:43 -08:00
|
|
|
// we should be using real URLs all the time, but until
|
2007-03-22 10:30:00 -07:00
|
|
|
// bug 239948 is fully fixed, this will do...
|
2007-08-20 11:15:44 -07:00
|
|
|
//
|
|
|
|
// note, this will thrown an exception if the native path
|
|
|
|
// is not valid (for example a native Windows path on a Mac)
|
|
|
|
// see bug #392386 for details
|
2007-03-22 10:30:00 -07:00
|
|
|
function getLocalFileFromNativePathOrUrl(aPathOrUrl)
|
|
|
|
{
|
|
|
|
if (aPathOrUrl.substring(0,7) == "file://") {
|
|
|
|
// if this is a URL, get the file from that
|
2008-01-08 21:43:36 -08:00
|
|
|
let ioSvc = Cc["@mozilla.org/network/io-service;1"].
|
2007-08-11 14:53:21 -07:00
|
|
|
getService(Ci.nsIIOService);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// XXX it's possible that using a null char-set here is bad
|
|
|
|
const fileUrl = ioSvc.newURI(aPathOrUrl, null, null).
|
2007-08-11 14:53:21 -07:00
|
|
|
QueryInterface(Ci.nsIFileURL);
|
|
|
|
return fileUrl.file.clone().QueryInterface(Ci.nsILocalFile);
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
|
|
|
// if it's a pathname, create the nsILocalFile directly
|
2007-05-21 17:03:33 -07:00
|
|
|
var f = new nsLocalFile(aPathOrUrl);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
}
|
2008-04-24 22:59:31 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the disabled state of the clear list button based on whether or not
|
|
|
|
* there are items in the list that can potentially be removed.
|
|
|
|
*/
|
|
|
|
function updateClearListButton()
|
|
|
|
{
|
|
|
|
let button = document.getElementById("clearListButton");
|
|
|
|
// The button is enabled if we have items in the list and we can clean up
|
|
|
|
button.disabled = !(gDownloadsView.itemCount && gDownloadManager.canCleanUp);
|
|
|
|
}
|
2008-11-03 15:22:21 -08:00
|
|
|
|
|
|
|
function getDownload(aID)
|
|
|
|
{
|
|
|
|
return document.getElementById("dl" + aID);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize the statement which is used to retrieve the list of downloads.
|
|
|
|
*
|
|
|
|
* This function gets called both at startup, and when entering the private
|
|
|
|
* browsing mode (because the database connection is changed when entering
|
|
|
|
* the private browsing mode, and a new statement should be initialized.
|
|
|
|
*/
|
|
|
|
function initStatement()
|
|
|
|
{
|
|
|
|
if (gStmt)
|
|
|
|
gStmt.finalize();
|
|
|
|
|
|
|
|
gStmt = gDownloadManager.DBConnection.createStatement(
|
|
|
|
"SELECT id, target, name, source, state, startTime, endTime, referrer, " +
|
|
|
|
"currBytes, maxBytes, state IN (?1, ?2, ?3, ?4, ?5) isActive " +
|
|
|
|
"FROM moz_downloads " +
|
|
|
|
"ORDER BY isActive DESC, endTime DESC, startTime DESC");
|
|
|
|
}
|