2011-09-14 05:10:51 +00:00
|
|
|
/**
|
|
|
|
* Copyright © Jordan Lee, Dave Perrett, Malcolm Jarvis and Bruno Bierbaumer
|
2008-07-10 23:57:46 +00:00
|
|
|
*
|
2011-09-14 05:10:51 +00:00
|
|
|
* This file is licensed under the GPLv2.
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
2008-07-10 23:57:46 +00:00
|
|
|
*/
|
|
|
|
|
2011-09-02 05:36:00 +00:00
|
|
|
var RPC = {
|
2016-03-10 19:05:13 +00:00
|
|
|
_DaemonVersion: 'version',
|
|
|
|
_DownSpeedLimit: 'speed-limit-down',
|
|
|
|
_DownSpeedLimited: 'speed-limit-down-enabled',
|
|
|
|
_QueueMoveTop: 'queue-move-top',
|
|
|
|
_QueueMoveBottom: 'queue-move-bottom',
|
|
|
|
_QueueMoveUp: 'queue-move-up',
|
|
|
|
_QueueMoveDown: 'queue-move-down',
|
|
|
|
_Root: '../rpc',
|
|
|
|
_TurtleDownSpeedLimit: 'alt-speed-down',
|
|
|
|
_TurtleState: 'alt-speed-enabled',
|
|
|
|
_TurtleUpSpeedLimit: 'alt-speed-up',
|
|
|
|
_UpSpeedLimit: 'speed-limit-up',
|
|
|
|
_UpSpeedLimited: 'speed-limit-up-enabled'
|
2011-09-02 05:36:00 +00:00
|
|
|
};
|
2008-07-10 23:57:46 +00:00
|
|
|
|
2016-03-10 19:05:13 +00:00
|
|
|
function TransmissionRemote(controller) {
|
|
|
|
this.initialize(controller);
|
|
|
|
return this;
|
2008-07-10 23:57:46 +00:00
|
|
|
}
|
|
|
|
|
2016-03-10 19:05:13 +00:00
|
|
|
TransmissionRemote.prototype = {
|
|
|
|
/*
|
|
|
|
* Constructor
|
|
|
|
*/
|
|
|
|
initialize: function (controller) {
|
|
|
|
this._controller = controller;
|
|
|
|
this._error = '';
|
|
|
|
this._token = '';
|
|
|
|
},
|
2008-07-10 23:57:46 +00:00
|
|
|
|
2016-03-10 19:05:13 +00:00
|
|
|
/*
|
|
|
|
* Display an error if an ajax request fails, and stop sending requests
|
|
|
|
* or on a 409, globally set the X-Transmission-Session-Id and resend
|
|
|
|
*/
|
|
|
|
ajaxError: function (request, error_string, exception, ajaxObject) {
|
|
|
|
var token;
|
|
|
|
var remote = this;
|
2009-05-08 15:02:22 +00:00
|
|
|
|
2016-03-10 19:05:13 +00:00
|
|
|
// set the Transmission-Session-Id on a 409
|
|
|
|
if (request.status === 409 && (token = request.getResponseHeader('X-Transmission-Session-Id'))) {
|
|
|
|
remote._token = token;
|
|
|
|
$.ajax(ajaxObject);
|
|
|
|
return;
|
|
|
|
};
|
2009-05-08 15:02:22 +00:00
|
|
|
|
2016-03-10 19:05:13 +00:00
|
|
|
remote._error = request.responseText ? request.responseText.trim().replace(/(<([^>]+)>)/ig, "") : "";
|
|
|
|
if (!remote._error.length) {
|
|
|
|
remote._error = 'Server not responding';
|
|
|
|
};
|
2010-09-03 00:03:21 +00:00
|
|
|
|
2016-03-10 19:05:13 +00:00
|
|
|
dialog.confirm('Connection Failed',
|
|
|
|
'Could not connect to the server. You may need to reload the page to reconnect.',
|
|
|
|
'Details',
|
|
|
|
function () {
|
|
|
|
alert(remote._error);
|
|
|
|
},
|
|
|
|
'Dismiss');
|
|
|
|
remote._controller.togglePeriodicSessionRefresh(false);
|
|
|
|
},
|
2009-05-08 15:02:22 +00:00
|
|
|
|
2016-03-10 19:05:13 +00:00
|
|
|
appendSessionId: function (XHR) {
|
|
|
|
if (this._token) {
|
|
|
|
XHR.setRequestHeader('X-Transmission-Session-Id', this._token);
|
|
|
|
};
|
|
|
|
},
|
2009-05-08 15:02:22 +00:00
|
|
|
|
2016-03-10 19:05:13 +00:00
|
|
|
sendRequest: function (data, callback, context, async) {
|
|
|
|
var remote = this;
|
|
|
|
if (typeof async != 'boolean') {
|
|
|
|
async = true;
|
|
|
|
};
|
2009-07-28 04:53:08 +00:00
|
|
|
|
2016-03-10 19:05:13 +00:00
|
|
|
var ajaxSettings = {
|
|
|
|
url: RPC._Root,
|
|
|
|
type: 'POST',
|
|
|
|
contentType: 'json',
|
|
|
|
dataType: 'json',
|
|
|
|
cache: false,
|
|
|
|
data: JSON.stringify(data),
|
|
|
|
beforeSend: function (XHR) {
|
|
|
|
remote.appendSessionId(XHR);
|
|
|
|
},
|
|
|
|
error: function (request, error_string, exception) {
|
|
|
|
remote.ajaxError(request, error_string, exception, ajaxSettings);
|
|
|
|
},
|
|
|
|
success: callback,
|
|
|
|
context: context,
|
|
|
|
async: async
|
|
|
|
};
|
2010-01-21 01:43:05 +00:00
|
|
|
|
2016-03-10 19:05:13 +00:00
|
|
|
$.ajax(ajaxSettings);
|
|
|
|
},
|
2008-07-10 23:57:46 +00:00
|
|
|
|
2016-03-10 19:05:13 +00:00
|
|
|
loadDaemonPrefs: function (callback, context, async) {
|
|
|
|
var o = {
|
|
|
|
method: 'session-get'
|
|
|
|
};
|
|
|
|
this.sendRequest(o, callback, context, async);
|
|
|
|
},
|
2015-05-05 19:12:48 +00:00
|
|
|
|
2016-03-10 19:05:13 +00:00
|
|
|
checkPort: function (callback, context, async) {
|
|
|
|
var o = {
|
|
|
|
method: 'port-test'
|
|
|
|
};
|
|
|
|
this.sendRequest(o, callback, context, async);
|
|
|
|
},
|
2013-10-20 18:39:07 +00:00
|
|
|
|
2016-03-10 19:05:13 +00:00
|
|
|
renameTorrent: function (torrentIds, oldpath, newname, callback, context) {
|
|
|
|
var o = {
|
|
|
|
method: 'torrent-rename-path',
|
|
|
|
arguments: {
|
|
|
|
'ids': torrentIds,
|
|
|
|
'path': oldpath,
|
|
|
|
'name': newname
|
|
|
|
}
|
|
|
|
};
|
|
|
|
this.sendRequest(o, callback, context);
|
|
|
|
},
|
2013-10-20 18:39:07 +00:00
|
|
|
|
2016-03-10 19:05:13 +00:00
|
|
|
loadDaemonStats: function (callback, context, async) {
|
|
|
|
var o = {
|
|
|
|
method: 'session-stats'
|
|
|
|
};
|
|
|
|
this.sendRequest(o, callback, context, async);
|
|
|
|
},
|
2010-03-11 04:50:04 +00:00
|
|
|
|
2016-03-10 19:05:13 +00:00
|
|
|
updateTorrents: function (torrentIds, fields, callback, context) {
|
|
|
|
var o = {
|
|
|
|
method: 'torrent-get',
|
|
|
|
arguments: {
|
|
|
|
'fields': fields
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if (torrentIds) {
|
|
|
|
o['arguments'].ids = torrentIds;
|
|
|
|
};
|
|
|
|
this.sendRequest(o, function (response) {
|
|
|
|
var args = response['arguments'];
|
|
|
|
callback.call(context, args.torrents, args.removed);
|
|
|
|
});
|
|
|
|
},
|
2010-06-21 13:14:33 +00:00
|
|
|
|
2016-03-10 19:05:13 +00:00
|
|
|
getFreeSpace: function (dir, callback, context) {
|
|
|
|
var remote = this;
|
|
|
|
var o = {
|
|
|
|
method: 'free-space',
|
|
|
|
arguments: {
|
|
|
|
path: dir
|
|
|
|
}
|
|
|
|
};
|
|
|
|
this.sendRequest(o, function (response) {
|
|
|
|
var args = response['arguments'];
|
|
|
|
callback.call(context, args.path, args['size-bytes']);
|
|
|
|
});
|
|
|
|
},
|
2013-02-10 18:33:04 +00:00
|
|
|
|
2016-03-10 19:05:13 +00:00
|
|
|
changeFileCommand: function (torrentId, fileIndices, command) {
|
|
|
|
var remote = this,
|
|
|
|
args = {
|
|
|
|
ids: [torrentId]
|
|
|
|
};
|
|
|
|
args[command] = fileIndices;
|
|
|
|
this.sendRequest({
|
|
|
|
arguments: args,
|
|
|
|
method: 'torrent-set'
|
|
|
|
}, function () {
|
|
|
|
remote._controller.refreshTorrents([torrentId]);
|
|
|
|
});
|
|
|
|
},
|
2010-06-21 13:14:33 +00:00
|
|
|
|
2016-03-10 19:05:13 +00:00
|
|
|
sendTorrentSetRequests: function (method, torrent_ids, args, callback, context) {
|
|
|
|
if (!args) {
|
|
|
|
args = {};
|
|
|
|
};
|
|
|
|
args['ids'] = torrent_ids;
|
|
|
|
var o = {
|
|
|
|
method: method,
|
|
|
|
arguments: args
|
|
|
|
};
|
|
|
|
this.sendRequest(o, callback, context);
|
|
|
|
},
|
2010-06-21 13:14:33 +00:00
|
|
|
|
2016-03-10 19:05:13 +00:00
|
|
|
sendTorrentActionRequests: function (method, torrent_ids, callback, context) {
|
|
|
|
this.sendTorrentSetRequests(method, torrent_ids, null, callback, context);
|
|
|
|
},
|
2009-12-06 03:21:08 +00:00
|
|
|
|
2016-03-10 19:05:13 +00:00
|
|
|
startTorrents: function (torrent_ids, noqueue, callback, context) {
|
|
|
|
var name = noqueue ? 'torrent-start-now' : 'torrent-start';
|
|
|
|
this.sendTorrentActionRequests(name, torrent_ids, callback, context);
|
|
|
|
},
|
|
|
|
stopTorrents: function (torrent_ids, callback, context) {
|
|
|
|
this.sendTorrentActionRequests('torrent-stop', torrent_ids, callback, context);
|
|
|
|
},
|
2012-08-18 15:46:22 +00:00
|
|
|
|
2016-03-10 19:05:13 +00:00
|
|
|
moveTorrents: function (torrent_ids, new_location, callback, context) {
|
|
|
|
var remote = this;
|
|
|
|
this.sendTorrentSetRequests('torrent-set-location', torrent_ids, {
|
|
|
|
"move": true,
|
|
|
|
"location": new_location
|
|
|
|
}, callback, context);
|
|
|
|
},
|
2012-08-18 15:46:22 +00:00
|
|
|
|
2016-03-10 19:05:13 +00:00
|
|
|
removeTorrents: function (torrent_ids, callback, context) {
|
|
|
|
this.sendTorrentActionRequests('torrent-remove', torrent_ids, callback, context);
|
|
|
|
},
|
|
|
|
removeTorrentsAndData: function (torrents) {
|
|
|
|
var remote = this;
|
|
|
|
var o = {
|
|
|
|
method: 'torrent-remove',
|
|
|
|
arguments: {
|
|
|
|
'delete-local-data': true,
|
|
|
|
ids: []
|
|
|
|
}
|
|
|
|
};
|
2009-03-18 00:47:25 +00:00
|
|
|
|
2016-03-10 19:05:13 +00:00
|
|
|
if (torrents) {
|
|
|
|
for (var i = 0, len = torrents.length; i < len; ++i) {
|
|
|
|
o.arguments.ids.push(torrents[i].getId());
|
|
|
|
};
|
|
|
|
};
|
|
|
|
this.sendRequest(o, function () {
|
|
|
|
remote._controller.refreshTorrents();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
verifyTorrents: function (torrent_ids, callback, context) {
|
|
|
|
this.sendTorrentActionRequests('torrent-verify', torrent_ids, callback, context);
|
|
|
|
},
|
|
|
|
reannounceTorrents: function (torrent_ids, callback, context) {
|
|
|
|
this.sendTorrentActionRequests('torrent-reannounce', torrent_ids, callback, context);
|
|
|
|
},
|
|
|
|
addTorrentByUrl: function (url, options) {
|
|
|
|
var remote = this;
|
|
|
|
if (url.match(/^[0-9a-f]{40}$/i)) {
|
|
|
|
url = 'magnet:?xt=urn:btih:' + url;
|
|
|
|
}
|
|
|
|
var o = {
|
|
|
|
method: 'torrent-add',
|
|
|
|
arguments: {
|
|
|
|
paused: (options.paused),
|
|
|
|
filename: url
|
|
|
|
}
|
|
|
|
};
|
|
|
|
this.sendRequest(o, function () {
|
|
|
|
remote._controller.refreshTorrents();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
savePrefs: function (args) {
|
|
|
|
var remote = this;
|
|
|
|
var o = {
|
|
|
|
method: 'session-set',
|
|
|
|
arguments: args
|
|
|
|
};
|
|
|
|
this.sendRequest(o, function () {
|
|
|
|
remote._controller.loadDaemonPrefs();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
updateBlocklist: function () {
|
|
|
|
var remote = this;
|
|
|
|
var o = {
|
|
|
|
method: 'blocklist-update'
|
|
|
|
};
|
|
|
|
this.sendRequest(o, function () {
|
|
|
|
remote._controller.loadDaemonPrefs();
|
|
|
|
});
|
|
|
|
},
|
2011-08-06 15:19:29 +00:00
|
|
|
|
2016-03-10 19:05:13 +00:00
|
|
|
// Added queue calls
|
|
|
|
moveTorrentsToTop: function (torrent_ids, callback, context) {
|
|
|
|
this.sendTorrentActionRequests(RPC._QueueMoveTop, torrent_ids, callback, context);
|
|
|
|
},
|
|
|
|
moveTorrentsToBottom: function (torrent_ids, callback, context) {
|
|
|
|
this.sendTorrentActionRequests(RPC._QueueMoveBottom, torrent_ids, callback, context);
|
|
|
|
},
|
|
|
|
moveTorrentsUp: function (torrent_ids, callback, context) {
|
|
|
|
this.sendTorrentActionRequests(RPC._QueueMoveUp, torrent_ids, callback, context);
|
|
|
|
},
|
|
|
|
moveTorrentsDown: function (torrent_ids, callback, context) {
|
|
|
|
this.sendTorrentActionRequests(RPC._QueueMoveDown, torrent_ids, callback, context);
|
|
|
|
}
|
2008-07-10 23:57:46 +00:00
|
|
|
};
|