1/** 2 * Copyright �� Jordan Lee, Dave Perrett, Malcolm Jarvis and Bruno Bierbaumer 3 * 4 * This file is licensed under the GPLv2. 5 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html 6 */ 7 8var RPC = { 9 _DaemonVersion : 'version', 10 _DownSpeedLimit : 'speed-limit-down', 11 _DownSpeedLimited : 'speed-limit-down-enabled', 12 _QueueMoveTop : 'queue-move-top', 13 _QueueMoveBottom : 'queue-move-bottom', 14 _QueueMoveUp : 'queue-move-up', 15 _QueueMoveDown : 'queue-move-down', 16 _Root : '../rpc', 17 _TurtleDownSpeedLimit : 'alt-speed-down', 18 _TurtleState : 'alt-speed-enabled', 19 _TurtleUpSpeedLimit : 'alt-speed-up', 20 _UpSpeedLimit : 'speed-limit-up', 21 _UpSpeedLimited : 'speed-limit-up-enabled' 22}; 23 24function TransmissionRemote(controller) 25{ 26 this.initialize(controller); 27 return this; 28} 29 30TransmissionRemote.prototype = 31{ 32 /* 33 * Constructor 34 */ 35 initialize: function(controller) { 36 this._controller = controller; 37 this._error = ''; 38 this._token = ''; 39 }, 40 41 /* 42 * Display an error if an ajax request fails, and stop sending requests 43 * or on a 409, globally set the X-Transmission-Session-Id and resend 44 */ 45 ajaxError: function(request, error_string, exception, ajaxObject) { 46 var token, 47 remote = this; 48 49 // set the Transmission-Session-Id on a 409 50 if (request.status === 409 && (token = request.getResponseHeader('X-Transmission-Session-Id'))){ 51 remote._token = token; 52 $.ajax(ajaxObject); 53 return; 54 } 55 56 remote._error = request.responseText 57 ? request.responseText.trim().replace(/(<([^>]+)>)/ig,"") 58 : ""; 59 if (!remote._error.length) 60 remote._error = 'Server not responding'; 61 62 dialog.confirm('Connection Failed', 63 'Could not connect to the server. You may need to reload the page to reconnect.', 64 'Details', 65 'alert(remote._error);', 66 null, 67 'Dismiss'); 68 remote._controller.togglePeriodicSessionRefresh(false); 69 }, 70 71 appendSessionId: function(XHR) { 72 XHR.setRequestHeader('X-Transmission-Session-Id', this._token); 73 }, 74 75 sendRequest: function(data, callback, context, async) { 76 var remote = this; 77 if (typeof async != 'boolean') 78 async = true; 79 80 var ajaxSettings = { 81 url: RPC._Root, 82 type: 'POST', 83 contentType: 'json', 84 dataType: 'json', 85 cache: false, 86 data: JSON.stringify(data), 87 beforeSend: function(XHR){ remote.appendSessionId(XHR); }, 88 error: function(request, error_string, exception){ remote.ajaxError(request, error_string, exception, ajaxSettings); }, 89 success: callback, 90 context: context, 91 async: async 92 }; 93 94 $.ajax(ajaxSettings); 95 }, 96 97 loadDaemonPrefs: function(callback, context, async) { 98 var o = { method: 'session-get' }; 99 this.sendRequest(o, callback, context, async); 100 }, 101 102 checkPort: function(callback, context, async) { 103 var o = { method: 'port-test' }; 104 this.sendRequest(o, callback, context, async); 105 }, 106 107 loadDaemonStats: function(callback, context, async) { 108 var o = { method: 'session-stats' }; 109 this.sendRequest(o, callback, context, async); 110 }, 111 112 updateTorrents: function(torrentIds, fields, callback, context) { 113 var o = { 114 method: 'torrent-get', 115 'arguments': { 116 'fields': fields 117 } 118 }; 119 if (torrentIds) 120 o['arguments'].ids = torrentIds; 121 this.sendRequest(o, function(response) { 122 var args = response['arguments']; 123 callback.call(context,args.torrents,args.removed); 124 }); 125 }, 126 127 changeFileCommand: function(torrentId, fileIndices, command) { 128 var remote = this, 129 args = { ids: [torrentId] }; 130 args[command] = fileIndices; 131 this.sendRequest({ 132 arguments: args, 133 method: 'torrent-set' 134 }, function() { 135 remote._controller.refreshTorrents([torrentId]); 136 }); 137 }, 138 139 sendTorrentSetRequests: function(method, torrent_ids, args, callback, context) { 140 if (!args) args = { }; 141 args['ids'] = torrent_ids; 142 var o = { 143 method: method, 144 arguments: args 145 }; 146 this.sendRequest(o, callback, context); 147 }, 148 149 sendTorrentActionRequests: function(method, torrent_ids, callback, context) { 150 this.sendTorrentSetRequests(method, torrent_ids, null, callback, context); 151 }, 152 153 startTorrents: function(torrent_ids, noqueue, callback, context) { 154 var name = noqueue ? 'torrent-start-now' : 'torrent-start'; 155 this.sendTorrentActionRequests(name, torrent_ids, callback, context); 156 }, 157 stopTorrents: function(torrent_ids, callback, context) { 158 this.sendTorrentActionRequests('torrent-stop', torrent_ids, callback, context); 159 }, 160 161 moveTorrents: function(torrent_ids, new_location, callback, context) { 162 var remote = this; 163 this.sendTorrentSetRequests( 'torrent-set-location', torrent_ids, 164 {"move": true, "location": new_location}, callback, context); 165 }, 166 167 removeTorrents: function(torrent_ids, callback, context) { 168 this.sendTorrentActionRequests('torrent-remove', torrent_ids, callback, context); 169 }, 170 removeTorrentsAndData: function(torrents) { 171 var remote = this; 172 var o = { 173 method: 'torrent-remove', 174 arguments: { 175 'delete-local-data': true, 176 ids: [ ] 177 } 178 }; 179 180 if (torrents) { 181 for (var i=0, len=torrents.length; i<len; ++i) { 182 o.arguments.ids.push(torrents[i].getId()); 183 } 184 } 185 this.sendRequest(o, function() { 186 remote._controller.refreshTorrents(); 187 }); 188 }, 189 verifyTorrents: function(torrent_ids, callback, context) { 190 this.sendTorrentActionRequests('torrent-verify', torrent_ids, callback, context); 191 }, 192 reannounceTorrents: function(torrent_ids, callback, context) { 193 this.sendTorrentActionRequests('torrent-reannounce', torrent_ids, callback, context); 194 }, 195 addTorrentByUrl: function(url, options) { 196 var remote = this; 197 if (url.match(/^[0-9a-f]{40}$/i)) { 198 url = 'magnet:?xt=urn:btih:'+url; 199 } 200 var o = { 201 method: 'torrent-add', 202 arguments: { 203 paused: (options.paused), 204 filename: url 205 } 206 }; 207 this.sendRequest(o, function() { 208 remote._controller.refreshTorrents(); 209 }); 210 }, 211 savePrefs: function(args) { 212 var remote = this; 213 var o = { 214 method: 'session-set', 215 arguments: args 216 }; 217 this.sendRequest(o, function() { 218 remote._controller.loadDaemonPrefs(); 219 }); 220 }, 221 updateBlocklist: function() { 222 var remote = this; 223 var o = { 224 method: 'blocklist-update' 225 }; 226 this.sendRequest(o, function() { 227 remote._controller.loadDaemonPrefs(); 228 }); 229 }, 230 231 // Added queue calls 232 moveTorrentsToTop: function(torrent_ids, callback, context) { 233 this.sendTorrentActionRequests(RPC._QueueMoveTop, torrent_ids, callback, context); 234 }, 235 moveTorrentsToBottom: function(torrent_ids, callback, context) { 236 this.sendTorrentActionRequests(RPC._QueueMoveBottom, torrent_ids, callback, context); 237 }, 238 moveTorrentsUp: function(torrent_ids, callback, context) { 239 this.sendTorrentActionRequests(RPC._QueueMoveUp, torrent_ids, callback, context); 240 }, 241 moveTorrentsDown: function(torrent_ids, callback, context) { 242 this.sendTorrentActionRequests(RPC._QueueMoveDown, torrent_ids, callback, context); 243 } 244}; 245