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';
|
|
|
|
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';
|
|
|
|
|
|
|
|
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-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';
|
2008-07-10 23:57:46 +00:00
|
|
|
|
|
|
|
dialog.confirm('Connection Failed',
|
|
|
|
'Could not connect to the server. You may need to reload the page to reconnect.',
|
|
|
|
'Details',
|
2009-05-08 02:43:17 +00:00
|
|
|
'alert(remote._error);',
|
2008-07-10 23:57:46 +00:00
|
|
|
null,
|
|
|
|
'Dismiss');
|
2009-05-08 02:43:17 +00:00
|
|
|
remote._controller.togglePeriodicRefresh(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-03-14 21:33:08 +00:00
|
|
|
sendRequest: function( data, success ) {
|
2009-05-08 02:43:17 +00:00
|
|
|
remote = this;
|
2009-03-14 21:33:08 +00:00
|
|
|
$.ajax( {
|
|
|
|
url: RPC._Root,
|
|
|
|
type: 'POST',
|
|
|
|
contentType: 'json',
|
|
|
|
dataType: 'json',
|
|
|
|
cache: false,
|
|
|
|
data: $.toJSON(data),
|
2009-05-08 15:02:22 +00:00
|
|
|
beforeSend: function(XHR){ remote.appendSessionId(XHR) },
|
|
|
|
error: function(request, error_string, exception){ remote.ajaxError(request, error_string, exception, this) },
|
2009-03-14 21:33:08 +00:00
|
|
|
success: success
|
|
|
|
} );
|
2008-07-10 23:57:46 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
loadDaemonPrefs: function() {
|
|
|
|
var tr = this._controller;
|
2009-03-14 21:33:08 +00:00
|
|
|
var o = { method: 'session-get' };
|
|
|
|
this.sendRequest( o, function(data) {
|
2008-07-16 20:06:28 +00:00
|
|
|
var o = data.arguments;
|
2008-07-10 23:57:46 +00:00
|
|
|
Prefs.getClutchPrefs( o );
|
|
|
|
tr.updatePrefs( o );
|
2009-03-14 21:33:08 +00:00
|
|
|
} );
|
2008-07-10 23:57:46 +00:00
|
|
|
},
|
|
|
|
|
2009-03-14 21:33:08 +00:00
|
|
|
loadTorrents: function(update_files) {
|
2008-07-10 23:57:46 +00:00
|
|
|
var tr = this._controller;
|
2009-03-14 21:33:08 +00:00
|
|
|
var o = {
|
|
|
|
method: 'torrent-get',
|
|
|
|
arguments: { fields: [
|
|
|
|
'addedDate', 'announceURL', 'comment', 'creator',
|
|
|
|
'dateCreated', 'downloadedEver', 'error', 'errorString',
|
|
|
|
'eta', 'hashString', 'haveUnchecked', 'haveValid', 'id',
|
|
|
|
'isPrivate', 'leechers', 'leftUntilDone', 'name',
|
|
|
|
'peersConnected', 'peersGettingFromUs', 'peersSendingToUs',
|
|
|
|
'rateDownload', 'rateUpload', 'seeders', 'sizeWhenDone',
|
|
|
|
'status', 'swarmSpeed', 'totalSize', 'uploadedEver' ]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if (update_files) {
|
|
|
|
o.arguments.fields.push('files');
|
|
|
|
o.arguments.fields.push('wanted');
|
|
|
|
o.arguments.fields.push('priorities');
|
|
|
|
}
|
|
|
|
this.sendRequest( o, function(data) {
|
|
|
|
tr.updateAllTorrents( data.arguments.torrents );
|
|
|
|
} );
|
2008-07-10 23:57:46 +00:00
|
|
|
},
|
2009-03-14 21:33:08 +00:00
|
|
|
|
|
|
|
loadTorrentFiles: function( torrent_ids ) {
|
|
|
|
var tr = this._controller;
|
|
|
|
this.sendRequest( {
|
|
|
|
method: 'torrent-get',
|
|
|
|
arguments: { fields: [ 'files', 'wanted', 'priorities'] },
|
|
|
|
ids: torrent_ids
|
|
|
|
}, function(data) {
|
|
|
|
tr.updateTorrentsData( data.arguments.torrents );
|
|
|
|
} );
|
|
|
|
},
|
|
|
|
|
|
|
|
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( ) {
|
|
|
|
remote.loadTorrentFiles( torrent_ids );
|
|
|
|
} );
|
|
|
|
},
|
|
|
|
|
2008-07-10 23:57:46 +00:00
|
|
|
sendTorrentCommand: function( method, torrents ) {
|
|
|
|
var remote = this;
|
2009-03-14 21:33:08 +00:00
|
|
|
var o = {
|
|
|
|
method: method,
|
|
|
|
arguments: { ids: [ ] }
|
|
|
|
};
|
2008-07-10 23:57:46 +00:00
|
|
|
if( torrents != null )
|
|
|
|
for( var i=0, len=torrents.length; i<len; ++i )
|
|
|
|
o.arguments.ids.push( torrents[i].id() );
|
2009-03-14 21:33:08 +00:00
|
|
|
this.sendRequest( o, function( ) {
|
2008-07-10 23:57:46 +00:00
|
|
|
remote.loadTorrents();
|
2009-03-14 21:33:08 +00:00
|
|
|
} );
|
2008-07-10 23:57:46 +00:00
|
|
|
},
|
2009-03-14 21:33:08 +00:00
|
|
|
|
2008-07-10 23:57:46 +00:00
|
|
|
startTorrents: function( torrents ) {
|
|
|
|
this.sendTorrentCommand( 'torrent-start', torrents );
|
|
|
|
},
|
|
|
|
stopTorrents: function( torrents ) {
|
|
|
|
this.sendTorrentCommand( 'torrent-stop', torrents );
|
|
|
|
},
|
|
|
|
removeTorrents: function( torrents ) {
|
|
|
|
this.sendTorrentCommand( 'torrent-remove', torrents );
|
|
|
|
},
|
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-02-12 17:20:43 +00:00
|
|
|
remote.loadTorrents();
|
2009-03-18 00:47:25 +00:00
|
|
|
} );
|
2009-02-12 17:20:43 +00:00
|
|
|
},
|
2009-03-04 00:19:11 +00:00
|
|
|
verifyTorrents: function( torrents ) {
|
|
|
|
this.sendTorrentCommand( 'torrent-verify', torrents );
|
|
|
|
},
|
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: {
|
|
|
|
paused: (options.paused ? 'true' : 'false'),
|
|
|
|
filename: url
|
|
|
|
}
|
2009-03-30 22:35:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
this.sendRequest(o, function() {
|
|
|
|
remote.loadTorrents();
|
|
|
|
} );
|
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() {
|
2008-07-10 23:57:46 +00:00
|
|
|
remote.loadDaemonPrefs();
|
2009-03-14 21:33:08 +00:00
|
|
|
} );
|
2008-08-07 19:41:33 +00:00
|
|
|
}
|
2008-07-10 23:57:46 +00:00
|
|
|
};
|