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 = {
|
|
|
|
_DaemonVersion : 'version',
|
|
|
|
_DownSpeedLimit : 'speed-limit-down',
|
|
|
|
_DownSpeedLimited : 'speed-limit-down-enabled',
|
|
|
|
_QueueMoveTop : 'queue-move-top',
|
|
|
|
_QueueMoveBottom : 'queue-move-bottom',
|
|
|
|
_QueueMoveUp : 'queue-move-up',
|
2011-09-14 05:10:51 +00:00
|
|
|
_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
|
|
|
|
2011-08-24 02:04:35 +00:00
|
|
|
function TransmissionRemote(controller)
|
2008-07-10 23:57:46 +00:00
|
|
|
{
|
2011-08-24 02:04:35 +00:00
|
|
|
this.initialize(controller);
|
2008-07-10 23:57:46 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
TransmissionRemote.prototype =
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Constructor
|
|
|
|
*/
|
|
|
|
initialize: function(controller) {
|
|
|
|
this._controller = controller;
|
|
|
|
this._error = '';
|
2009-05-08 15:02:22 +00:00
|
|
|
this._token = '';
|
2008-07-10 23:57:46 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Display an error if an ajax request fails, and stop sending requests
|
2009-05-08 15:02:22 +00:00
|
|
|
* or on a 409, globally set the X-Transmission-Session-Id and resend
|
2008-07-10 23:57:46 +00:00
|
|
|
*/
|
2009-05-08 15:02:22 +00:00
|
|
|
ajaxError: function(request, error_string, exception, ajaxObject) {
|
2011-09-03 05:12:14 +00:00
|
|
|
var token,
|
|
|
|
remote = this;
|
2009-05-08 15:02:22 +00:00
|
|
|
|
|
|
|
// set the Transmission-Session-Id on a 409
|
2011-09-05 07:46:55 +00:00
|
|
|
if (request.status === 409 && (token = request.getResponseHeader('X-Transmission-Session-Id'))){
|
2009-05-08 15:02:22 +00:00
|
|
|
remote._token = token;
|
|
|
|
$.ajax(ajaxObject);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-05-08 02:43:17 +00:00
|
|
|
remote._error = request.responseText
|
|
|
|
? request.responseText.trim().replace(/(<([^>]+)>)/ig,"")
|
|
|
|
: "";
|
2011-08-24 02:04:35 +00:00
|
|
|
if (!remote._error.length)
|
2009-05-08 02:43:17 +00:00
|
|
|
remote._error = 'Server not responding';
|
2010-09-03 00:03:21 +00:00
|
|
|
|
|
|
|
dialog.confirm('Connection Failed',
|
|
|
|
'Could not connect to the server. You may need to reload the page to reconnect.',
|
|
|
|
'Details',
|
|
|
|
'alert(remote._error);',
|
|
|
|
null,
|
|
|
|
'Dismiss');
|
2009-11-14 14:57:47 +00:00
|
|
|
remote._controller.togglePeriodicSessionRefresh(false);
|
2008-07-10 23:57:46 +00:00
|
|
|
},
|
2009-05-08 15:02:22 +00:00
|
|
|
|
|
|
|
appendSessionId: function(XHR) {
|
|
|
|
XHR.setRequestHeader('X-Transmission-Session-Id', this._token);
|
|
|
|
},
|
|
|
|
|
2011-08-28 06:05:46 +00:00
|
|
|
sendRequest: function(data, callback, context, async) {
|
2011-09-03 05:12:14 +00:00
|
|
|
var remote = this;
|
2011-08-24 02:04:35 +00:00
|
|
|
if (typeof async != 'boolean')
|
2010-06-21 13:14:33 +00:00
|
|
|
async = true;
|
2009-07-28 04:53:08 +00:00
|
|
|
|
2010-01-21 01:43:05 +00:00
|
|
|
var ajaxSettings = {
|
2009-03-14 21:33:08 +00:00
|
|
|
url: RPC._Root,
|
|
|
|
type: 'POST',
|
|
|
|
contentType: 'json',
|
|
|
|
dataType: 'json',
|
|
|
|
cache: false,
|
2011-08-28 14:51:49 +00:00
|
|
|
data: JSON.stringify(data),
|
2011-08-24 02:04:35 +00:00
|
|
|
beforeSend: function(XHR){ remote.appendSessionId(XHR); },
|
|
|
|
error: function(request, error_string, exception){ remote.ajaxError(request, error_string, exception, ajaxSettings); },
|
2011-08-28 06:05:46 +00:00
|
|
|
success: callback,
|
|
|
|
context: context,
|
2009-07-28 04:53:08 +00:00
|
|
|
async: async
|
2010-01-21 01:43:05 +00:00
|
|
|
};
|
|
|
|
|
2011-08-24 02:04:35 +00:00
|
|
|
$.ajax(ajaxSettings);
|
2008-07-10 23:57:46 +00:00
|
|
|
},
|
|
|
|
|
2011-08-28 06:05:46 +00:00
|
|
|
loadDaemonPrefs: function(callback, context, async) {
|
2009-03-14 21:33:08 +00:00
|
|
|
var o = { method: 'session-get' };
|
2011-08-28 06:05:46 +00:00
|
|
|
this.sendRequest(o, callback, context, async);
|
2008-07-10 23:57:46 +00:00
|
|
|
},
|
2011-03-10 01:37:58 +00:00
|
|
|
|
2011-08-28 06:05:46 +00:00
|
|
|
checkPort: function(callback, context, async) {
|
2011-03-10 01:37:58 +00:00
|
|
|
var o = { method: 'port-test' };
|
2011-08-28 06:05:46 +00:00
|
|
|
this.sendRequest(o, callback, context, async);
|
2011-03-10 01:37:58 +00:00
|
|
|
},
|
|
|
|
|
2011-08-28 06:05:46 +00:00
|
|
|
loadDaemonStats: function(callback, context, async) {
|
2010-03-11 04:50:04 +00:00
|
|
|
var o = { method: 'session-stats' };
|
2011-08-28 06:05:46 +00:00
|
|
|
this.sendRequest(o, callback, context, async);
|
2010-03-11 04:50:04 +00:00
|
|
|
},
|
|
|
|
|
2011-08-26 22:49:57 +00:00
|
|
|
updateTorrents: function(torrentIds, fields, callback, context) {
|
2009-03-14 21:33:08 +00:00
|
|
|
var o = {
|
|
|
|
method: 'torrent-get',
|
2011-08-28 06:05:46 +00:00
|
|
|
'arguments': {
|
2011-10-15 04:35:20 +00:00
|
|
|
'fields': fields
|
2010-02-06 16:43:48 +00:00
|
|
|
}
|
|
|
|
};
|
2011-08-26 22:49:57 +00:00
|
|
|
if (torrentIds)
|
|
|
|
o['arguments'].ids = torrentIds;
|
|
|
|
this.sendRequest(o, function(response) {
|
|
|
|
var args = response['arguments'];
|
|
|
|
callback.call(context,args.torrents,args.removed);
|
|
|
|
});
|
2009-03-14 21:33:08 +00:00
|
|
|
},
|
2010-06-21 13:14:33 +00:00
|
|
|
|
2011-09-03 04:33:04 +00:00
|
|
|
changeFileCommand: function(torrentId, fileIndices, command) {
|
|
|
|
var remote = this,
|
|
|
|
args = { ids: [torrentId] };
|
|
|
|
args[command] = fileIndices;
|
|
|
|
this.sendRequest({
|
|
|
|
arguments: args,
|
|
|
|
method: 'torrent-set'
|
|
|
|
}, function() {
|
2011-10-21 02:41:53 +00:00
|
|
|
remote._controller.refreshTorrents([torrentId]);
|
2011-08-16 18:49:26 +00:00
|
|
|
});
|
2009-03-14 21:33:08 +00:00
|
|
|
},
|
2010-06-21 13:14:33 +00:00
|
|
|
|
2011-08-28 06:05:46 +00:00
|
|
|
sendTorrentSetRequests: function(method, torrent_ids, args, callback, context) {
|
2009-12-06 03:21:08 +00:00
|
|
|
if (!args) args = { };
|
|
|
|
args['ids'] = torrent_ids;
|
2009-03-14 21:33:08 +00:00
|
|
|
var o = {
|
|
|
|
method: method,
|
2009-12-06 03:21:08 +00:00
|
|
|
arguments: args
|
2009-03-14 21:33:08 +00:00
|
|
|
};
|
2011-08-28 06:05:46 +00:00
|
|
|
this.sendRequest(o, callback, context);
|
2008-07-10 23:57:46 +00:00
|
|
|
},
|
2010-06-21 13:14:33 +00:00
|
|
|
|
2011-08-28 06:05:46 +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
|
|
|
},
|
|
|
|
|
2011-08-28 06:05:46 +00:00
|
|
|
startTorrents: function(torrent_ids, noqueue, callback, context) {
|
2011-08-01 22:24:24 +00:00
|
|
|
var name = noqueue ? 'torrent-start-now' : 'torrent-start';
|
2011-08-28 06:05:46 +00:00
|
|
|
this.sendTorrentActionRequests(name, torrent_ids, callback, context);
|
2008-07-10 23:57:46 +00:00
|
|
|
},
|
2011-08-28 06:05:46 +00:00
|
|
|
stopTorrents: function(torrent_ids, callback, context) {
|
|
|
|
this.sendTorrentActionRequests('torrent-stop', torrent_ids, callback, context);
|
2008-07-10 23:57:46 +00:00
|
|
|
},
|
2011-08-28 06:05:46 +00:00
|
|
|
removeTorrents: function(torrent_ids, callback, context) {
|
|
|
|
this.sendTorrentActionRequests('torrent-remove', torrent_ids, callback, context);
|
2008-07-10 23:57:46 +00:00
|
|
|
},
|
2011-08-24 02:04:35 +00:00
|
|
|
removeTorrentsAndData: function(torrents) {
|
2009-03-18 21:43:11 +00:00
|
|
|
var remote = this;
|
2009-03-18 00:47:25 +00:00
|
|
|
var o = {
|
|
|
|
method: 'torrent-remove',
|
|
|
|
arguments: {
|
|
|
|
'delete-local-data': true,
|
|
|
|
ids: [ ]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-08-24 02:04:35 +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() {
|
2009-05-22 22:45:09 +00:00
|
|
|
remote._controller.refreshTorrents();
|
2011-08-24 02:04:35 +00:00
|
|
|
});
|
2009-02-12 17:20:43 +00:00
|
|
|
},
|
2011-08-28 06:05:46 +00:00
|
|
|
verifyTorrents: function(torrent_ids, callback, context) {
|
|
|
|
this.sendTorrentActionRequests('torrent-verify', torrent_ids, callback, context);
|
2009-03-04 00:19:11 +00:00
|
|
|
},
|
2011-09-02 05:36:00 +00:00
|
|
|
reannounceTorrents: function(torrent_ids, callback, context) {
|
|
|
|
this.sendTorrentActionRequests('torrent-reannounce', torrent_ids, callback, context);
|
2011-05-15 17:46:26 +00:00
|
|
|
},
|
2011-08-24 02:04:35 +00:00
|
|
|
addTorrentByUrl: function(url, options) {
|
2009-03-30 22:35:13 +00:00
|
|
|
var remote = this;
|
2011-08-28 13:57:25 +00:00
|
|
|
if (url.match(/^[0-9a-f]{40}$/i)) {
|
|
|
|
url = 'magnet:?xt=urn:btih:'+url;
|
|
|
|
}
|
2009-03-30 22:35:13 +00:00
|
|
|
var o = {
|
2009-01-20 03:41:19 +00:00
|
|
|
method: 'torrent-add',
|
|
|
|
arguments: {
|
2009-12-06 03:21:16 +00:00
|
|
|
paused: (options.paused),
|
2009-01-20 03:41:19 +00:00
|
|
|
filename: url
|
|
|
|
}
|
2009-03-30 22:35:13 +00:00
|
|
|
};
|
|
|
|
this.sendRequest(o, function() {
|
2009-05-22 22:45:09 +00:00
|
|
|
remote._controller.refreshTorrents();
|
2011-08-24 02:04:35 +00:00
|
|
|
});
|
2009-01-20 03:41:19 +00:00
|
|
|
},
|
2011-08-24 02:04:35 +00:00
|
|
|
savePrefs: function(args) {
|
2008-07-10 23:57:46 +00:00
|
|
|
var remote = this;
|
2009-03-14 21:33:08 +00:00
|
|
|
var o = {
|
|
|
|
method: 'session-set',
|
|
|
|
arguments: args
|
|
|
|
};
|
2011-08-24 02:04:35 +00:00
|
|
|
this.sendRequest(o, function() {
|
2009-07-28 04:53:04 +00:00
|
|
|
remote._controller.loadDaemonPrefs();
|
2011-08-24 02:04:35 +00:00
|
|
|
});
|
2009-12-06 03:21:08 +00:00
|
|
|
},
|
2011-03-10 01:37:58 +00:00
|
|
|
updateBlocklist: function() {
|
|
|
|
var remote = this;
|
|
|
|
var o = {
|
2011-08-24 02:04:35 +00:00
|
|
|
method: 'blocklist-update'
|
2011-03-10 01:37:58 +00:00
|
|
|
};
|
2011-08-24 02:04:35 +00:00
|
|
|
this.sendRequest(o, function() {
|
2011-03-10 01:37:58 +00:00
|
|
|
remote._controller.loadDaemonPrefs();
|
2011-08-24 02:04:35 +00:00
|
|
|
});
|
2011-08-06 15:19:29 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
// Added queue calls
|
2011-08-28 06:05:46 +00:00
|
|
|
moveTorrentsToTop: function(torrent_ids, callback, context) {
|
|
|
|
this.sendTorrentActionRequests(RPC._QueueMoveTop, torrent_ids, callback, context);
|
2011-08-06 15:19:29 +00:00
|
|
|
},
|
2011-08-28 06:05:46 +00:00
|
|
|
moveTorrentsToBottom: function(torrent_ids, callback, context) {
|
|
|
|
this.sendTorrentActionRequests(RPC._QueueMoveBottom, torrent_ids, callback, context);
|
2011-08-06 15:19:29 +00:00
|
|
|
},
|
2011-08-28 06:05:46 +00:00
|
|
|
moveTorrentsUp: function(torrent_ids, callback, context) {
|
|
|
|
this.sendTorrentActionRequests(RPC._QueueMoveUp, torrent_ids, callback, context);
|
2011-08-06 15:19:29 +00:00
|
|
|
},
|
2011-08-28 06:05:46 +00:00
|
|
|
moveTorrentsDown: function(torrent_ids, callback, context) {
|
|
|
|
this.sendTorrentActionRequests(RPC._QueueMoveDown, torrent_ids, callback, context);
|
2008-08-07 19:41:33 +00:00
|
|
|
}
|
2008-07-10 23:57:46 +00:00
|
|
|
};
|