/* * Copyright © Dave Perrett, Malcolm Jarvis and Bruno Bierbaumer * This code is licensed under the GPL version 2. * For details, see http://www.gnu.org/licenses/old-licenses/gpl-2.0.html * * Class TransmissionRemote */ var RPC = { _Root : '../rpc', _DaemonVersion : 'version', _Encryption : 'encryption', _EncryptionPreferred : 'preferred', _EncryptionRequired : 'required', _UpSpeedLimit : 'speed-limit-up', _DownSpeedLimit : 'speed-limit-down', _DownloadDir : 'download-dir', _PeerPort : 'peer-port', _UpSpeedLimited : 'speed-limit-up-enabled', _DownSpeedLimited : 'speed-limit-down-enabled', _TurtleState : 'alt-speed-enabled', _TurtleUpSpeedLimit : 'alt-speed-up', _TurtleDownSpeedLimit : 'alt-speed-down', _TurtleTimeEnabled : 'alt-speed-time-enabled', _TurtleTimeBegin : 'alt-speed-time-begin', _TurtleTimeEnd : 'alt-speed-time-end', _TurtleTimeDay : 'alt-speed-time-day', _PeerLimitGlobal : 'peer-limit-global', _PeerLimitPerTorrent : 'peer-limit-per-torrent', _PexEnabled : 'pex-enabled', _DhtEnabled : 'dht-enabled', _LpdEnabled : 'lpd-enabled', _BlocklistEnabled : 'blocklist-enabled', _BlocklistURL : 'blocklist-url', _BlocklistSize : 'blocklist-size', _UtpEnabled : 'utp-enabled', _PeerPortRandom : 'peer-port-random-on-start', _PortForwardingEnabled : 'port-forwarding-enabled', _StartAddedTorrent : 'start-added-torrents', _QueueMoveTop : 'queue-move-top', _QueueMoveBottom : 'queue-move-bottom', _QueueMoveUp : 'queue-move-up', _QueueMoveDown : 'queue-move-down' }; function TransmissionRemote(controller) { this.initialize(controller); return this; } TransmissionRemote.prototype = { /* * Constructor */ initialize: function(controller) { this._controller = controller; this._error = ''; this._token = ''; }, /* * 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, remote = this; // 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; } remote._error = request.responseText ? request.responseText.trim().replace(/(<([^>]+)>)/ig,"") : ""; if (!remote._error.length) remote._error = 'Server not responding'; 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'); remote._controller.togglePeriodicSessionRefresh(false); }, appendSessionId: function(XHR) { XHR.setRequestHeader('X-Transmission-Session-Id', this._token); }, sendRequest: function(data, callback, context, async) { var remote = this; if (typeof async != 'boolean') async = true; 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 }; $.ajax(ajaxSettings); }, loadDaemonPrefs: function(callback, context, async) { var o = { method: 'session-get' }; this.sendRequest(o, callback, context, async); }, checkPort: function(callback, context, async) { var o = { method: 'port-test' }; this.sendRequest(o, callback, context, async); }, loadDaemonStats: function(callback, context, async) { var o = { method: 'session-stats' }; this.sendRequest(o, callback, context, async); }, 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); }); }, 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(torrent_ids); }); }, 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); }, sendTorrentActionRequests: function(method, torrent_ids, callback, context) { this.sendTorrentSetRequests(method, torrent_ids, null, callback, context); }, 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); }, 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: [ ] } }; if (torrents) { for (var i=0, len=torrents.length; i