1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
var {XPCOMUtils} = ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
XPCOMUtils.defineLazyModuleGetters(this, {
DownloadUtils: "resource://gre/modules/DownloadUtils.jsm",
DownloadsCommon: "resource:///modules/DownloadsCommon.jsm",
DownloadHistory: "resource://gre/modules/DownloadHistory.jsm",
});
function DownloadTreeView() {
this._dlList = [];
this._searchTerms = [];
this.dateTimeFormatter =
new Services.intl.DateTimeFormat(undefined,
{dateStyle: "short",
timeStyle: "long"});
}
DownloadTreeView.prototype = {
QueryInterface: XPCOMUtils.generateQI([Ci.nsITreeView]),
// ***** nsITreeView attributes and methods *****
get rowCount() {
return this._dlList.length;
},
selection: null,
getRowProperties: function(aRow) {
let dl = this._dlList[aRow];
// (in)active
let properties = dl.isActive ? "active": "inactive";
// resumable
if (dl.hasPartialData)
properties += " resumable";
// Download states
let state = DownloadsCommon.stateOfDownload(dl);
switch (state) {
case DownloadsCommon.DOWNLOAD_PAUSED:
properties += " paused";
break;
case DownloadsCommon.DOWNLOAD_DOWNLOADING:
properties += " downloading";
break;
case DownloadsCommon.DOWNLOAD_FINISHED:
properties += " finished";
break;
case DownloadsCommon.DOWNLOAD_FAILED:
properties += " failed";
break;
case DownloadsCommon.DOWNLOAD_CANCELED:
properties += " canceled";
break;
case DownloadsCommon.DOWNLOAD_BLOCKED_PARENTAL: // Parental Controls
case DownloadsCommon.DOWNLOAD_BLOCKED_POLICY: // Security Zone Policy
case DownloadsCommon.DOWNLOAD_DIRTY: // possible virus/spyware
properties += " blocked";
break;
}
return properties;
},
getCellProperties: function(aRow, aColumn) {
// Append all row properties to the cell
return this.getRowProperties(aRow);
},
getColumnProperties: function(aColumn) { return ""; },
isContainer: function(aRow) { return false; },
isContainerOpen: function(aRow) { return false; },
isContainerEmpty: function(aRow) { return false; },
isSeparator: function(aRow) { return false; },
isSorted: function() { return false; },
canDrop: function(aIdx, aOrientation) { return false; },
drop: function(aIdx, aOrientation) { },
getParentIndex: function(aRow) { return -1; },
hasNextSibling: function(aRow, aAfterIdx) { return false; },
getLevel: function(aRow) { return 0; },
getImageSrc: function(aRow, aColumn) {
if (aColumn.id == "Name")
return "moz-icon://" + this._dlList[aRow].target.path + "?size=16";
return "";
},
getProgressMode: function(aRow, aColumn) {
if (aColumn.id == "Progress")
return this._dlList[aRow].progressMode;
return Ci.nsITreeView.PROGRESS_NONE;
},
getCellValue: function(aRow, aColumn) {
if (aColumn.id == "Progress")
return this._dlList[aRow].progress;
return "";
},
getCellText: function(aRow, aColumn) {
let dl = this._dlList[aRow];
switch (aColumn.id) {
case "Name":
return dl.displayName;
case "Status":
return DownloadsCommon.stateOfDownloadText(dl);
case "Progress":
if (dl.isActive)
return dl.progress;
return DownloadsCommon.stateOfDownloadText(dl);
case "ProgressPercent":
return dl.succeeded ? 100 : dl.progress;
case "TimeRemaining":
return DownloadsCommon.getTimeRemaining(dl);
case "Transferred":
return DownloadsCommon.getTransferredBytes(dl);
case "TransferRate":
let state = DownloadsCommon.stateOfDownload(dl);
switch (state) {
case DownloadsCommon.DOWNLOAD_DOWNLOADING:
let [rate, unit] = DownloadUtils.convertByteUnits(dl.speed);
return this._dlbundle.getFormattedString("speedFormat", [rate, unit]);
case DownloadsCommon.DOWNLOAD_PAUSED:
return this._dlbundle.getString("statePaused");
case DownloadsCommon.DOWNLOAD_NOTSTARTED:
return this._dlbundle.getString("stateNotStarted");
}
return "";
case "TimeElapsed":
// With no end time persisted in the downloads backend this is
// utterly useless unless the download is progressing.
if (DownloadsCommon.stateOfDownload(dl) ==
DownloadsCommon.DOWNLOAD_DOWNLOADING && dl.startTime) {
let seconds = (Date.now() - dl.startTime) / 1000;
let [time1, unit1, time2, unit2] =
DownloadUtils.convertTimeUnits(seconds);
if (seconds < 3600 || time2 == 0) {
return this._dlbundle.getFormattedString("timeSingle", [time1, unit1]);
}
return this._dlbundle.getFormattedString("timeDouble", [time1, unit1, time2, unit2]);
}
return "";
case "StartTime":
if (dl.startTime) {
return this.dateTimeFormatter.format(dl.startTime);
}
return "";
case "EndTime":
// This might end with an exception if it is an unsupported uri
// scheme.
let metaData = DownloadHistory.getPlacesMetaDataFor(dl.source.url);
if (metaData.endTime) {
return this.dateTimeFormatter.format(metaData.endTime);
}
return "";
case "Source":
return dl.source.url;
}
return "";
},
setTree: function(aTree) {
this._tree = aTree;
this._dlbundle = document.getElementById("dmBundle");
},
toggleOpenState: function(aRow) { },
cycleHeader: function(aColumn) { },
selectionChanged: function() { },
cycleCell: function(aRow, aColumn) {
var dl = this._dlList[aRow];
switch (aColumn.id) {
case "ActionPlay":
if (dl.stopped) {
if (!dl.succeeded)
dl.start();
} else {
if (dl.hasPartialData)
dl.cancel();
}
break;
case "ActionStop":
if (dl.isActive)
cancelDownload(dl);
else
removeDownload(dl);
break;
}
},
isEditable: function(aRow, aColumn) { return false; },
isSelectable: function(aRow, aColumn) { return false; },
setCellValue: function(aRow, aColumn, aText) { },
setCellText: function(aRow, aColumn, aText) { },
// ***** local public methods *****
addDownload: function(aDownload) {
aDownload.progressMode = Ci.nsITreeView.PROGRESS_NONE;
aDownload.lastSec = Infinity;
let state = DownloadsCommon.stateOfDownload(aDownload);
switch (state) {
case DownloadsCommon.DOWNLOAD_DOWNLOADING:
aDownload.endTime = Date.now();
// At this point, we know if we are an indeterminate download or not.
aDownload.progressMode = aDownload.hasProgress ?
Ci.nsITreeView.PROGRESS_UNDETERMINED :
Ci.nsITreeView.PROGRESS_NORMAL;
case DownloadsCommon.DOWNLOAD_NOTSTARTED:
case DownloadsCommon.DOWNLOAD_PAUSED:
aDownload.isActive = 1;
break;
default:
aDownload.isActive = 0;
break;
}
// prepend in natural sorting
aDownload.listIndex = this._lastListIndex--;
// Prepend data to the download list
this._dlList.unshift(aDownload);
// Tell the tree we added 1 row at index 0
this._tree.rowCountChanged(0, 1);
// Data has changed, so re-sorting might be needed
this.sortView("", "", aDownload, 0);
window.updateCommands("tree-select");
},
updateDownload: function(aDownload) {
var row = this._dlList.indexOf(aDownload);
if (row == -1) {
// No download row found to update, but as it's obviously going on,
// add it to the list now (can happen with very fast, e.g. local dls)
this.onDownloadAdded(aDownload);
return;
}
let state = DownloadsCommon.stateOfDownload(aDownload);
switch (state) {
case DownloadsCommon.DOWNLOAD_DOWNLOADING:
// At this point, we know if we are an indeterminate download or not.
aDownload.progressMode = aDownload.hasProgress ?
Ci.nsITreeView.PROGRESS_NORMAL : Ci.nsITreeView.PROGRESS_UNDETERMINED;
case DownloadsCommon.DOWNLOAD_NOTSTARTED:
case DownloadsCommon.DOWNLOAD_PAUSED:
aDownload.isActive = 1;
break;
default:
aDownload.isActive = 0;
aDownload.progressMode = Ci.nsITreeView.PROGRESS_NONE;
// This preference may not be set, so defaulting to two.
var flashCount = 2;
try {
flashCount = Services.prefs.getIntPref(PREF_FLASH_COUNT);
} catch (e) { }
getAttentionWithCycleCount(flashCount);
break;
}
// Repaint the tree row
this._tree.invalidateRow(row);
// Data has changed, so re-sorting might be needed
this.sortView("", "", aDownload, row);
window.updateCommands("tree-select");
},
removeDownload: function(aDownload) {
var row = this._dlList.indexOf(aDownload);
// Make sure we have an item to remove
if (row == -1)
return;
var index = this.selection.currentIndex;
var wasSingleSelection = this.selection.count == 1;
// Remove data from the download list
this._dlList.splice(row, 1);
// Tell the tree we removed 1 row at the given row index
this._tree.rowCountChanged(row, -1);
// Update selection if only removed download was selected
if (wasSingleSelection && this.selection.count == 0) {
index = Math.min(index, this.rowCount - 1);
if (index >= 0)
this.selection.select(index);
}
window.updateCommands("tree-select");
},
searchView: function(aInput) {
// Stringify the previous search
var prevSearch = this._searchTerms.join(" ");
// Array of space-separated lower-case search terms
this._searchTerms = aInput.trim().toLowerCase().split(/\s+/);
// Don't rebuild the download list if the search didn't change
if (this._searchTerms.join(" ") == prevSearch)
return;
// Cache the current selection
this._cacheSelection();
// Rebuild the tree with set search terms
//this.initTree();
// Restore the selection
this._restoreSelection();
},
sortView: function(aColumnID, aDirection, aDownload, aRow) {
var sortAscending = aDirection != "descending";
if (aColumnID == "" && aDirection == "") {
// Re-sort in already selected/cached order
var sortedColumn = this._tree.columns.getSortedColumn();
if (sortedColumn) {
aColumnID = sortedColumn.id;
sortAscending = sortedColumn.element.getAttribute("sortDirection") != "descending";
}
// no need for else, use default case of switch, sortAscending is true
}
// Compare function for two _dlList items
var compfunc = function(a, b) {
// Active downloads are always at the beginning
// i.e. 0 for .isActive is larger (!) than 1
if (a.isActive < b.isActive)
return 1;
if (a.isActive > b.isActive)
return -1;
// Same active/inactive state, sort normally
var comp_a = null;
var comp_b = null;
switch (aColumnID) {
case "Name":
comp_a = a.displayName.toLowerCase();
comp_b = b.displayName.toLowerCase();
break;
case "Status":
comp_a = DownloadsCommon.stateOfDownload(a);
comp_b = DownloadsCommon.stateOfDownload(b);
break;
case "Progress":
case "ProgressPercent":
// Use original sorting for inactive entries
// Use only one isActive to be sure we do the same
comp_a = a.isActive ? a.progress : a.listIndex;
comp_b = a.isActive ? b.progress : b.listIndex;
break;
case "TimeRemaining":
comp_a = a.isActive ? a.lastSec : a.listIndex;
comp_b = a.isActive ? b.lastSec : b.listIndex;
break;
case "Transferred":
comp_a = a.currentBytes;
comp_b = b.currentBytes;
break;
case "TransferRate":
comp_a = a.isActive ? a.speed : a.listIndex;
comp_b = a.isActive ? b.speed : b.listIndex;
break;
case "TimeElapsed":
comp_a = (a.endTime && a.startTime && (a.endTime > a.startTime))
? a.endTime - a.startTime
: 0;
comp_b = (b.endTime && b.startTime && (b.endTime > b.startTime))
? b.endTime - b.startTime
: 0;
break;
case "StartTime":
comp_a = a.startTime;
comp_b = b.startTime;
break;
case "EndTime":
comp_a = a.endTime;
comp_b = b.endTime;
break;
case "Source":
comp_a = a.source.url;
comp_b = b.source.url;
break;
case "unsorted": // Special case for reverting to original order
default:
comp_a = a.listIndex;
comp_b = b.listIndex;
}
if (comp_a > comp_b)
return sortAscending ? 1 : -1;
if (comp_a < comp_b)
return sortAscending ? -1 : 1;
return 0;
}
// Cache the current selection
this._cacheSelection();
// Do the actual sorting of the array
this._dlList.sort(compfunc);
var row = this._dlList.indexOf(aDownload);
if (row == -1)
// Repaint the tree
this._tree.invalidate();
else if (row == aRow)
// No effect
this._selectionCache = null;
else if (row < aRow)
// Download moved up from aRow to row
this._tree.invalidateRange(row, aRow);
else
// Download moved down from aRow to row
this._tree.invalidateRange(aRow, row)
// Restore the selection
this._restoreSelection();
},
getRowData: function(aRow) {
return this._dlList[aRow];
},
getActiveDownloads: function() {
return this._dlList.filter(dld => !dld.stopped);
},
// ***** local member vars *****
_tree: null,
_dlBundle: null,
_lastListIndex: 0,
_selectionCache: null,
// ***** local helper functions *****
// Cache IDs of selected downloads for later restoration
_cacheSelection: function() {
// Abort if there's already something cached
if (this._selectionCache)
return;
this._selectionCache = [];
if (this.selection.count < 1)
return;
// Walk all selected rows and cache their download IDs
var start = {};
var end = {};
var numRanges = this.selection.getRangeCount();
for (let rg = 0; rg < numRanges; rg++){
this.selection.getRangeAt(rg, start, end);
for (let row = start.value; row <= end.value; row++){
this._selectionCache.push(this._dlList[row]);
}
}
},
// Restore selection from cached IDs (as possible)
_restoreSelection: function() {
// Abort if the cache is empty
if (!this._selectionCache)
return;
this.selection.clearSelection();
for (let dl of this._selectionCache) {
// Find out what row this is now and if possible, add it to the selection
var row = this._dlList.indexOf(dl);
if (row != -1)
this.selection.rangedSelect(row, row, true);
}
// Work done, clear the cache
this._selectionCache = null;
},
};
|