2013-01-29 13:47:40 +01:00
|
|
|
angular
|
|
|
|
.module('webui.ctrls.download', [
|
2013-02-25 11:50:42 +01:00
|
|
|
'webui.services.utils', 'webui.services.rpc', 'webui.services.alerts',
|
|
|
|
'webui.services.settings', 'webui.services.modals'
|
2013-01-29 13:47:40 +01:00
|
|
|
])
|
2013-02-25 11:50:42 +01:00
|
|
|
.controller('DownloadCtrl', [
|
|
|
|
'$scope', '$rpc', '$utils', '$alerts', '$modals',
|
|
|
|
'$fileSettings', '$activeInclude', '$waitingExclude',
|
2013-07-17 16:10:24 +02:00
|
|
|
// for document title
|
|
|
|
'$window',
|
2013-02-25 11:50:42 +01:00
|
|
|
function(
|
|
|
|
scope, rpc, utils, alerts, modals,
|
2013-07-17 16:10:24 +02:00
|
|
|
fsettings, activeInclude, waitingExclude, window
|
2013-02-25 11:50:42 +01:00
|
|
|
) {
|
2013-01-17 17:18:23 +01:00
|
|
|
scope.active = [], scope.waiting = [], scope.stopped = [];
|
2013-03-20 08:45:28 +01:00
|
|
|
scope.gstats = {};
|
2013-01-15 09:24:09 +01:00
|
|
|
|
2013-01-22 11:12:40 +01:00
|
|
|
// pause the download
|
|
|
|
// d: the download ctx
|
|
|
|
scope.pause = function(d) {
|
|
|
|
rpc.once('forcePause', [d.gid]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// resume the download
|
|
|
|
// d: the download ctx
|
|
|
|
scope.resume = function(d) {
|
|
|
|
rpc.once('unpause', [d.gid]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove the download,
|
|
|
|
// put it in stopped list if active,
|
|
|
|
// otherwise permanantly remove it
|
|
|
|
// d: the download ctx
|
2013-03-18 15:59:40 +01:00
|
|
|
scope.remove = function(d, cb) {
|
2013-01-22 13:11:38 +01:00
|
|
|
var method = 'remove';
|
2013-03-18 15:59:40 +01:00
|
|
|
|
2013-01-22 13:11:38 +01:00
|
|
|
if (scope.getType(d) == 'stopped')
|
|
|
|
method = 'removeDownloadResult';
|
|
|
|
|
2013-03-18 15:59:40 +01:00
|
|
|
rpc.once(method, [d.gid], cb);
|
|
|
|
|
|
|
|
// also remove it from client cache assuming that it will be deleted in the aria2 list,
|
|
|
|
// but we could be wrong but the cache will update in next global update
|
|
|
|
var downloads = [scope.active, scope.waiting, scope.stopped], ind = -1, i;
|
|
|
|
for (i = 0; i < downloads.length; i++) {
|
|
|
|
ind = downloads[i].indexOf(d);
|
|
|
|
if (ind != -1) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ind == -1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
downloads[i].splice(ind, 1);
|
2013-01-22 11:12:40 +01:00
|
|
|
}
|
2013-01-15 09:24:09 +01:00
|
|
|
|
2013-01-22 13:11:38 +01:00
|
|
|
scope.restart = function(d) {
|
|
|
|
var uris =
|
|
|
|
_.chain(d.files).map(function(f) { return f.uris })
|
|
|
|
.filter(function(uris) { return uris.length })
|
|
|
|
.map(function(uris) {
|
|
|
|
return _.chain(uris)
|
|
|
|
.map(function(u) { return u.uri })
|
|
|
|
.uniq().value();
|
|
|
|
}).value();
|
|
|
|
|
|
|
|
if (uris.length > 0) {
|
2013-03-18 15:59:40 +01:00
|
|
|
scope.remove(d, function() {
|
|
|
|
rpc.once('addUri', uris, angular.noop, true);
|
2013-01-22 13:11:38 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2013-01-22 11:12:40 +01:00
|
|
|
|
|
|
|
// start filling in the model of active,
|
|
|
|
// waiting and stopped download
|
2013-01-15 09:24:09 +01:00
|
|
|
rpc.subscribe('tellActive', [], function(data) {
|
2013-03-12 07:51:44 +01:00
|
|
|
utils.mergeMap(data[0], scope.active, scope.getCtx);
|
2013-01-15 09:24:09 +01:00
|
|
|
});
|
|
|
|
|
2013-01-21 15:07:55 +01:00
|
|
|
rpc.subscribe('tellWaiting', [0, 1000], function(data) {
|
2013-03-12 07:51:44 +01:00
|
|
|
utils.mergeMap(data[0], scope.waiting, scope.getCtx);
|
2013-01-15 09:24:09 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
2013-01-21 15:07:55 +01:00
|
|
|
rpc.subscribe('tellStopped', [0, 1000], function(data) {
|
2013-03-12 07:51:44 +01:00
|
|
|
utils.mergeMap(data[0], scope.stopped, scope.getCtx);
|
2013-01-15 09:24:09 +01:00
|
|
|
});
|
|
|
|
|
2013-03-19 21:57:52 +01:00
|
|
|
rpc.subscribe('getGlobalStat', [], function(data) {
|
|
|
|
scope.gstats = data[0];
|
2013-07-17 16:10:24 +02:00
|
|
|
window.document.title = utils.getTitle(scope.gstats);
|
|
|
|
|
2013-03-19 21:57:52 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
rpc.once('getVersion', [], function(data) {
|
|
|
|
scope.miscellaneous = data[0];
|
|
|
|
});
|
|
|
|
|
|
|
|
// total number of downloads, updates dynamically as downloads are
|
|
|
|
// stored in scope
|
|
|
|
scope.totalDownloads = 0;
|
|
|
|
|
|
|
|
// download search filter
|
|
|
|
scope.downloadFilter = "";
|
|
|
|
|
|
|
|
scope.filterDownloads = function(downloads) {
|
|
|
|
var filter = scope.downloadFilter;
|
|
|
|
if (!filter.length) return downloads;
|
|
|
|
return _.filter(downloads, function(d) {
|
|
|
|
if (!d.files.length) return true;
|
|
|
|
|
|
|
|
return _.filter(d.files, function(f) {
|
2013-08-02 16:12:10 +02:00
|
|
|
return f.path.toLowerCase().indexOf(filter.toLowerCase()) != -1;
|
|
|
|
// return f.path.search(filter) != -1;
|
2013-03-19 21:57:52 +01:00
|
|
|
}).length;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-03-16 09:13:06 +01:00
|
|
|
// max downloads shown in one page
|
|
|
|
scope.pageSize = 10;
|
|
|
|
|
|
|
|
// current displayed page
|
|
|
|
scope.currentPage = 1;
|
|
|
|
|
2013-03-16 14:33:10 +01:00
|
|
|
scope.pageControlRadius = 3;
|
2013-03-16 09:13:06 +01:00
|
|
|
|
|
|
|
// total maximum pages
|
|
|
|
scope.totalPages = 0;
|
|
|
|
|
2013-03-19 21:57:52 +01:00
|
|
|
// total amount of downloads returned by aria2
|
|
|
|
scope.totalAria2Downloads = function() {
|
|
|
|
return scope.active.length
|
|
|
|
+ scope.waiting.length
|
|
|
|
+ scope.stopped.length;
|
|
|
|
}
|
|
|
|
|
2013-01-22 11:12:40 +01:00
|
|
|
// actual downloads used by the view
|
2013-01-15 09:24:09 +01:00
|
|
|
scope.getDownloads = function() {
|
2013-03-19 21:57:52 +01:00
|
|
|
var downloads =
|
|
|
|
scope.filterDownloads(
|
|
|
|
scope.active.concat( scope.waiting ).concat( scope.stopped )
|
|
|
|
)
|
|
|
|
;
|
2013-03-16 09:13:06 +01:00
|
|
|
|
|
|
|
scope.totalDownloads = downloads.length;
|
|
|
|
|
|
|
|
scope.totalPages = Math.ceil(scope.totalDownloads / scope.pageSize)
|
|
|
|
|
2013-03-18 15:59:40 +01:00
|
|
|
// fix the bug when downloads are deleted until no left on a specific page
|
|
|
|
if (scope.currentPage > scope.totalPages)
|
|
|
|
scope.currentPage = scope.totalPages;
|
|
|
|
|
2013-03-16 09:13:06 +01:00
|
|
|
downloads = downloads.slice( (scope.currentPage - 1) * scope.pageSize );
|
|
|
|
downloads.splice( scope.pageSize );
|
|
|
|
|
|
|
|
return downloads;
|
|
|
|
}
|
|
|
|
|
|
|
|
scope.setPage = function(pageNumber) {
|
|
|
|
scope.currentPage = pageNumber;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the pages to be displayed
|
|
|
|
scope.getPages = function() {
|
2013-03-16 14:33:10 +01:00
|
|
|
var minPage = scope.currentPage - scope.pageControlRadius;
|
2013-03-16 09:13:06 +01:00
|
|
|
|
|
|
|
if (minPage < 1) minPage = 1;
|
|
|
|
|
2013-03-16 14:33:10 +01:00
|
|
|
var maxPage = scope.currentPage + scope.pageControlRadius;
|
2013-03-16 09:13:06 +01:00
|
|
|
|
|
|
|
if (maxPage > scope.totalPages)
|
|
|
|
maxPage = scope.totalPages;
|
|
|
|
|
|
|
|
return _.range(minPage, maxPage + 1);
|
2013-01-15 09:24:09 +01:00
|
|
|
}
|
2013-01-19 18:46:22 +01:00
|
|
|
|
2013-01-22 11:12:40 +01:00
|
|
|
// convert the donwload form aria2 to once used by the view,
|
|
|
|
// minor additions of some fields and checks
|
2013-01-17 17:18:23 +01:00
|
|
|
scope.getCtx = function(d, ctx) {
|
|
|
|
ctx = ctx || {};
|
2013-01-19 18:46:22 +01:00
|
|
|
|
2013-01-22 08:53:30 +01:00
|
|
|
_.each([
|
2013-01-22 13:11:38 +01:00
|
|
|
'totalLength', 'completedLength', 'uploadLength', 'dir',
|
|
|
|
'pieceLength', 'downloadSpeed', 'uploadSpeed', 'files',
|
2013-01-24 13:19:57 +01:00
|
|
|
'status', 'gid', 'bitfield', 'numPieces', 'connections',
|
|
|
|
'bittorrent'
|
2013-01-22 08:53:30 +01:00
|
|
|
], function(e) {
|
2013-01-28 15:18:21 +01:00
|
|
|
if (ctx[e] != d[e])
|
|
|
|
ctx[e] = d[e];
|
2013-01-22 08:53:30 +01:00
|
|
|
});
|
2013-01-19 18:46:22 +01:00
|
|
|
|
2013-01-28 15:18:21 +01:00
|
|
|
// collapse the download details initially
|
|
|
|
if (ctx.collapsed === undefined)
|
|
|
|
ctx.collapsed = true;
|
|
|
|
|
2013-01-22 13:11:38 +01:00
|
|
|
return ctx;
|
|
|
|
};
|
|
|
|
|
|
|
|
scope.canRestart = function(d) {
|
|
|
|
if (['active', 'paused'].indexOf(d.status) == -1
|
|
|
|
&& !d.bittorrent)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
2013-01-19 18:46:22 +01:00
|
|
|
|
2013-03-18 10:51:03 +01:00
|
|
|
scope.hasStatus = function hasStatus(d, status) {
|
|
|
|
if (_.isArray(status)) {
|
|
|
|
if (status.length == 0) return false;
|
|
|
|
return hasStatus(d, status[0]) || hasStatus(d, status.slice(1));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return d.status == status;
|
|
|
|
}
|
2013-01-22 13:11:38 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// get time left for the download with
|
|
|
|
// current download speed,
|
|
|
|
// could be smarter by different heuristics
|
|
|
|
scope.getEta = function(d) {
|
|
|
|
return (d.totalLength-d.completedLength) / d.downloadSpeed;
|
|
|
|
}
|
|
|
|
|
|
|
|
// gets the progress in percentages
|
|
|
|
scope.getProgress = function(d) {
|
2013-01-19 18:46:22 +01:00
|
|
|
var percentage = (d.completedLength / d.totalLength)*100;
|
|
|
|
percentage = percentage.toFixed(2);
|
|
|
|
if(!percentage) percentage = 0;
|
|
|
|
|
2013-01-22 13:11:38 +01:00
|
|
|
return percentage;
|
|
|
|
};
|
2013-01-17 17:18:23 +01:00
|
|
|
|
2013-01-22 13:11:38 +01:00
|
|
|
// gets a pretty name for the download
|
|
|
|
scope.getName = function(d) {
|
2013-01-17 17:18:23 +01:00
|
|
|
if (d.bittorrent && d.bittorrent.info) {
|
2013-01-22 13:11:38 +01:00
|
|
|
return d.bittorrent.info.name;
|
2013-01-17 17:18:23 +01:00
|
|
|
}
|
|
|
|
|
2013-01-22 13:11:38 +01:00
|
|
|
return utils.getFileName(
|
|
|
|
d.files[0].path || d.files[0].uris[0].uri
|
|
|
|
);
|
2013-01-15 09:24:09 +01:00
|
|
|
}
|
|
|
|
|
2013-01-22 13:11:38 +01:00
|
|
|
// gets the type for the download as classified by the aria2 rpc calls
|
|
|
|
scope.getType = function(d) {
|
|
|
|
var type = d.status;
|
|
|
|
if (type == "paused") type = "waiting";
|
|
|
|
if (["error", "removed", "complete"].indexOf(type) != -1)
|
|
|
|
type = "stopped";
|
|
|
|
return type;
|
|
|
|
};
|
|
|
|
|
2013-02-25 11:50:42 +01:00
|
|
|
scope.showSettings = function(d) {
|
2013-03-16 09:13:06 +01:00
|
|
|
var type = scope.getType(d)
|
2013-02-25 11:50:42 +01:00
|
|
|
, settings = {};
|
|
|
|
|
|
|
|
rpc.once('getOption', [d.gid], function(data) {
|
|
|
|
var vals = data[0];
|
|
|
|
|
|
|
|
for (var i in fsettings) {
|
|
|
|
if (type == 'active' && activeInclude.indexOf(i) == -1) continue;
|
|
|
|
|
|
|
|
if (type == 'waiting' && waitingExclude.indexOf(i) != -1) continue;
|
|
|
|
|
|
|
|
settings[i] = fsettings[i];
|
|
|
|
settings[i].val = vals[i] || settings[i].val;
|
|
|
|
}
|
|
|
|
|
|
|
|
modals.invoke('settings', settings, scope.getName(d) + ' settings', function(settings) {
|
|
|
|
var sets = {};
|
|
|
|
for (var i in settings) { sets[i] = settings[i].val };
|
|
|
|
|
|
|
|
rpc.once('changeOption', [d.gid, sets]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-01-15 09:24:09 +01:00
|
|
|
}]);
|