2008-07-10 23:57:46 +00:00
|
|
|
/*
|
|
|
|
* Copyright © Dave Perrett and Malcolm Jarvis
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
function RPC() { }
|
|
|
|
//Prefs.prototype = { }
|
|
|
|
|
|
|
|
// Constants
|
|
|
|
RPC._Root = '/transmission/rpc';
|
2009-12-20 21:06:20 +00:00
|
|
|
RPC._DaemonVersion = 'version';
|
2008-07-10 23:57:46 +00:00
|
|
|
RPC._Encryption = 'encryption';
|
|
|
|
RPC._EncryptionPreferred = 'preferred';
|
|
|
|
RPC._EncryptionRequired = 'required';
|
|
|
|
RPC._UpSpeedLimit = 'speed-limit-up';
|
|
|
|
RPC._DownSpeedLimit = 'speed-limit-down';
|
|
|
|
RPC._DownloadDir = 'download-dir';
|
2009-03-25 19:18:00 +00:00
|
|
|
RPC._PeerPort = 'peer-port';
|
2008-07-10 23:57:46 +00:00
|
|
|
RPC._UpSpeedLimited = 'speed-limit-up-enabled';
|
|
|
|
RPC._DownSpeedLimited = 'speed-limit-down-enabled';
|
2009-11-10 05:42:57 +00:00
|
|
|
RPC._TurtleState = 'alt-speed-enabled';
|
|
|
|
RPC._TurtleUpSpeedLimit = 'alt-speed-up';
|
|
|
|
RPC._TurtleDownSpeedLimit = 'alt-speed-down';
|
|
|
|
RPC._TurtleTimeEnabled = 'alt-speed-time-enabled';
|
|
|
|
RPC._TurtleTimeBegin = 'alt-speed-time-begin';
|
|
|
|
RPC._TurtleTimeEnd = 'alt-speed-time-end';
|
|
|
|
RPC._TurtleTimeDay = 'alt-speed-time-day';
|
2010-03-17 19:23:03 +00:00
|
|
|
RPC._StartAddedTorrent = 'start-added-torrents';
|
2008-07-10 23:57:46 +00:00
|
|
|
|
|
|
|
function TransmissionRemote( controller )
|
|
|
|
{
|
|
|
|
this.initialize( controller );
|
|
|
|
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) {
|
2009-05-24 16:56:29 +00:00
|
|
|
var token;
|
2009-05-08 02:43:17 +00:00
|
|
|
remote = this;
|
2009-05-08 15:02:22 +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 02:43:17 +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
|
|
|
|
|
|
|
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-05-08 02:43:17 +00:00
|
|
|
remote._controller.togglePeriodicRefresh(false);
|
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);
|
|
|
|
},
|
|
|
|
|
2009-07-28 04:53:08 +00:00
|
|
|
sendRequest: function( data, success, async ) {
|
2009-05-08 02:43:17 +00:00
|
|
|
remote = this;
|
2009-07-28 04:53:08 +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,
|
2010-09-03 00:03:21 +00:00
|
|
|
data: $.toJSON(data),
|
2009-05-08 15:02:22 +00:00
|
|
|
beforeSend: function(XHR){ remote.appendSessionId(XHR) },
|
2010-01-21 01:43:05 +00:00
|
|
|
error: function(request, error_string, exception){ remote.ajaxError(request, error_string, exception, ajaxSettings) },
|
2009-07-28 04:53:08 +00:00
|
|
|
success: success,
|
|
|
|
async: async
|
2010-01-21 01:43:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
$.ajax( ajaxSettings );
|
2008-07-10 23:57:46 +00:00
|
|
|
},
|
|
|
|
|
2009-07-28 04:53:08 +00:00
|
|
|
loadDaemonPrefs: function( callback, async ) {
|
2008-07-10 23:57:46 +00:00
|
|
|
var tr = this._controller;
|
2009-03-14 21:33:08 +00:00
|
|
|
var o = { method: 'session-get' };
|
2009-07-28 04:53:08 +00:00
|
|
|
this.sendRequest( o, callback, async );
|
2008-07-10 23:57:46 +00:00
|
|
|
},
|
|
|
|
|
2010-03-11 04:50:04 +00:00
|
|
|
loadDaemonStats: function( callback, async ) {
|
|
|
|
var tr = this._controller;
|
|
|
|
var o = { method: 'session-stats' };
|
|
|
|
this.sendRequest( o, callback, async );
|
|
|
|
},
|
|
|
|
|
2009-05-22 22:45:09 +00:00
|
|
|
getInitialDataFor: function(torrent_ids, callback) {
|
2009-03-14 21:33:08 +00:00
|
|
|
var o = {
|
|
|
|
method: 'torrent-get',
|
2009-05-22 22:45:09 +00:00
|
|
|
arguments: {
|
2010-02-06 16:43:48 +00:00
|
|
|
fields: Torrent._StaticFields.concat( Torrent._MetaDataFields,
|
2010-06-21 13:14:33 +00:00
|
|
|
Torrent._DynamicFields,
|
|
|
|
[ 'files', 'fileStats' ] )
|
2010-02-06 16:43:48 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if(torrent_ids)
|
|
|
|
o.arguments.ids = torrent_ids;
|
|
|
|
|
|
|
|
this.sendRequest( o, function(data){ callback(data.arguments.torrents)} );
|
|
|
|
},
|
|
|
|
|
|
|
|
getMetaDataFor: function(torrent_ids, callback) {
|
|
|
|
var o = {
|
|
|
|
method: 'torrent-get',
|
|
|
|
arguments: {
|
|
|
|
fields: Torrent._StaticFields.concat( Torrent._MetaDataFields,
|
2010-06-21 13:14:33 +00:00
|
|
|
[ 'files', 'fileStats' ] )
|
2009-03-14 21:33:08 +00:00
|
|
|
}
|
|
|
|
};
|
2009-05-22 22:45:09 +00:00
|
|
|
|
|
|
|
if(torrent_ids)
|
|
|
|
o.arguments.ids = torrent_ids;
|
|
|
|
|
|
|
|
this.sendRequest( o, function(data){ callback(data.arguments.torrents)} );
|
2008-07-10 23:57:46 +00:00
|
|
|
},
|
2009-05-22 22:45:09 +00:00
|
|
|
|
|
|
|
getUpdatedDataFor: function(torrent_ids, callback) {
|
|
|
|
var o = {
|
|
|
|
method: 'torrent-get',
|
|
|
|
arguments: {
|
|
|
|
'ids': torrent_ids,
|
2009-08-07 00:39:07 +00:00
|
|
|
fields: [ 'id' ].concat(Torrent._DynamicFields)
|
2009-05-22 22:45:09 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.sendRequest( o, function(data){ callback(data.arguments.torrents, data.arguments.removed)} );
|
|
|
|
},
|
|
|
|
|
2009-03-14 21:33:08 +00:00
|
|
|
loadTorrentFiles: function( torrent_ids ) {
|
|
|
|
var tr = this._controller;
|
|
|
|
this.sendRequest( {
|
|
|
|
method: 'torrent-get',
|
2009-05-26 15:11:44 +00:00
|
|
|
arguments: { fields: [ 'id', 'fileStats'], ids: torrent_ids }
|
2009-03-14 21:33:08 +00:00
|
|
|
}, function(data) {
|
2009-05-22 22:45:09 +00:00
|
|
|
tr.updateTorrentsFileData( data.arguments.torrents );
|
2009-03-14 21:33:08 +00:00
|
|
|
} );
|
|
|
|
},
|
2010-06-21 13:14:33 +00:00
|
|
|
|
2009-03-14 21:33:08 +00:00
|
|
|
changeFileCommand: function( command, torrent, file ) {
|
|
|
|
var remote = this;
|
|
|
|
var torrent_ids = [ torrent.id() ];
|
|
|
|
var o = {
|
|
|
|
method: 'torrent-set',
|
|
|
|
arguments: { ids: torrent_ids }
|
|
|
|
};
|
|
|
|
o.arguments[command] = [ file._index ];
|
|
|
|
this.sendRequest( o, function( ) {
|
2009-06-30 03:37:40 +00:00
|
|
|
remote._controller.refreshTorrents( torrent_ids );
|
2009-03-14 21:33:08 +00:00
|
|
|
} );
|
|
|
|
},
|
2010-06-21 13:14:33 +00:00
|
|
|
|
2009-12-06 03:21:08 +00:00
|
|
|
sendTorrentSetRequests: function( method, torrent_ids, args, callback ) {
|
|
|
|
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
|
|
|
};
|
2009-08-12 02:53:45 +00:00
|
|
|
|
|
|
|
this.sendRequest( o, function( data ) {
|
|
|
|
callback();
|
|
|
|
});
|
2008-07-10 23:57:46 +00:00
|
|
|
},
|
2010-06-21 13:14:33 +00:00
|
|
|
|
2009-12-06 03:21:08 +00:00
|
|
|
sendTorrentActionRequests: function( method, torrent_ids, callback ) {
|
|
|
|
this.sendTorrentSetRequests( method, torrent_ids, null, callback );
|
|
|
|
},
|
|
|
|
|
2009-08-12 02:53:45 +00:00
|
|
|
startTorrents: function( torrent_ids, callback ) {
|
|
|
|
this.sendTorrentActionRequests( 'torrent-start', torrent_ids, callback );
|
2008-07-10 23:57:46 +00:00
|
|
|
},
|
2009-08-12 02:53:45 +00:00
|
|
|
stopTorrents: function( torrent_ids, callback ) {
|
|
|
|
this.sendTorrentActionRequests( 'torrent-stop', torrent_ids, callback );
|
2008-07-10 23:57:46 +00:00
|
|
|
},
|
2009-08-12 02:53:45 +00:00
|
|
|
removeTorrents: function( torrent_ids, callback ) {
|
|
|
|
this.sendTorrentActionRequests( 'torrent-remove', torrent_ids, callback );
|
2008-07-10 23:57:46 +00:00
|
|
|
},
|
2009-02-12 17:20:43 +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: [ ]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-02-12 17:20:43 +00:00
|
|
|
if( torrents != null )
|
|
|
|
for( var i=0, len=torrents.length; i<len; ++i )
|
|
|
|
o.arguments.ids.push( torrents[i].id() );
|
2009-03-18 00:47:25 +00:00
|
|
|
this.sendRequest( o, function( ) {
|
2009-05-22 22:45:09 +00:00
|
|
|
remote._controller.refreshTorrents();
|
2009-03-18 00:47:25 +00:00
|
|
|
} );
|
2009-02-12 17:20:43 +00:00
|
|
|
},
|
2009-08-12 02:53:45 +00:00
|
|
|
verifyTorrents: function( torrent_ids, callback ) {
|
|
|
|
this.sendTorrentActionRequests( 'torrent-verify', torrent_ids, callback );
|
2009-03-04 00:19:11 +00:00
|
|
|
},
|
2009-01-20 03:41:19 +00:00
|
|
|
addTorrentByUrl: function( url, options ) {
|
2009-03-30 22:35:13 +00:00
|
|
|
var remote = this;
|
|
|
|
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();
|
2009-03-30 22:35:13 +00:00
|
|
|
} );
|
2009-01-20 03:41:19 +00:00
|
|
|
},
|
2008-07-10 23:57:46 +00:00
|
|
|
savePrefs: function( args ) {
|
|
|
|
var remote = this;
|
2009-03-14 21:33:08 +00:00
|
|
|
var o = {
|
|
|
|
method: 'session-set',
|
|
|
|
arguments: args
|
|
|
|
};
|
|
|
|
this.sendRequest( o, function() {
|
2009-07-28 04:53:04 +00:00
|
|
|
remote._controller.loadDaemonPrefs();
|
2009-03-14 21:33:08 +00:00
|
|
|
} );
|
2009-12-06 03:21:08 +00:00
|
|
|
},
|
|
|
|
filesSelectAll: function( torrent_ids, files, callback ) {
|
|
|
|
this.sendTorrentSetRequests( 'torrent-set', torrent_ids, { 'files-wanted': files }, callback );
|
|
|
|
},
|
|
|
|
filesDeselectAll: function( torrent_ids, files, callback ) {
|
|
|
|
this.sendTorrentSetRequests( 'torrent-set', torrent_ids, { 'files-unwanted': files }, callback );
|
2008-08-07 19:41:33 +00:00
|
|
|
}
|
2008-07-10 23:57:46 +00:00
|
|
|
};
|