2013-01-17 07:51:03 +01:00
|
|
|
app.controller('DownloadCtrl', [ '$scope', '$rpc', '$utils',
|
|
|
|
function(scope, rpc, utils) {
|
2013-01-17 17:18:23 +01:00
|
|
|
|
|
|
|
scope.active = [], scope.waiting = [], scope.stopped = [];
|
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
|
|
|
|
scope.remove = function(d) {
|
2013-01-22 13:11:38 +01:00
|
|
|
var method = 'remove';
|
|
|
|
if (scope.getType(d) == 'stopped')
|
|
|
|
method = 'removeDownloadResult';
|
|
|
|
|
2013-01-22 11:12:40 +01:00
|
|
|
rpc.once(method, [d.gid]);
|
|
|
|
}
|
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) {
|
|
|
|
rpc.once('removeDownloadResult', [d.gid], function() {
|
|
|
|
rpc.once('addUri', uris);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
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-01-17 07:51:03 +01:00
|
|
|
console.log('got active data');
|
2013-01-15 09:24:09 +01:00
|
|
|
scope.$apply(function() {
|
2013-01-21 15:07:55 +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-01-15 09:24:09 +01:00
|
|
|
scope.$apply(function() {
|
2013-01-21 15:07:55 +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-01-15 09:24:09 +01:00
|
|
|
scope.$apply(function() {
|
2013-01-21 15:07:55 +01:00
|
|
|
utils.mergeMap(data[0], scope.stopped, scope.getCtx);
|
2013-01-15 09:24:09 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
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-01-22 11:12:40 +01:00
|
|
|
return scope.active
|
2013-01-17 17:18:23 +01:00
|
|
|
.concat(scope.waiting).concat(scope.stopped);
|
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) {
|
|
|
|
ctx[e] = d[e];
|
|
|
|
});
|
2013-01-19 18:46:22 +01:00
|
|
|
|
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-01-22 13:11:38 +01:00
|
|
|
scope.hasStatus = function(d, status) {
|
|
|
|
return d.status == status;
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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-01-15 09:24:09 +01:00
|
|
|
}]);
|