home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / transmission / web / javascript / transmission.remote.js < prev   
Encoding:
JavaScript  |  2009-03-30  |  4.2 KB  |  157 lines

  1. /*
  2.  * Copyright ¬© Dave Perrett and Malcolm Jarvis
  3.  * This code is licensed under the GPL version 2.
  4.  * For details, see http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  5.  *
  6.  * Class TransmissionRemote
  7.  */
  8.  
  9. function RPC() { }
  10. //Prefs.prototype = { }
  11.  
  12. // Constants
  13. RPC._Root                   = '/transmission/rpc';
  14. RPC._Encryption             = 'encryption';
  15. RPC._EncryptionPreferred    = 'preferred';
  16. RPC._EncryptionRequired     = 'required';
  17. RPC._UpSpeedLimit           = 'speed-limit-up';
  18. RPC._DownSpeedLimit         = 'speed-limit-down';
  19. RPC._DownloadDir            = 'download-dir';
  20. RPC._PeerPort               = 'port';
  21. RPC._UpSpeedLimited         = 'speed-limit-up-enabled';
  22. RPC._DownSpeedLimited       = 'speed-limit-down-enabled';
  23.  
  24. function TransmissionRemote( controller )
  25. {
  26.     this.initialize( controller );
  27.     return this;
  28. }
  29.  
  30. TransmissionRemote.prototype =
  31. {
  32.     /*
  33.      * Constructor
  34.      */
  35.     initialize: function(controller) {
  36.         this._controller = controller;
  37.         this._error = '';
  38.     },
  39.  
  40.     /*
  41.      * Display an error if an ajax request fails, and stop sending requests
  42.      */
  43.     ajaxError: function(request, error_string, exception) {
  44.         this._error = request.responseText
  45.                     ? request.responseText.trim().replace(/(<([^>]+)>)/ig,"")
  46.                     : "";
  47.         if( !this._error.length )
  48.             this._error = 'Server not responding';
  49.         
  50.         dialog.confirm('Connection Failed', 
  51.             'Could not connect to the server. You may need to reload the page to reconnect.', 
  52.             'Details',
  53.             'alert(transmission.remote._error);',
  54.             null,
  55.             'Dismiss');
  56.         transmission.togglePeriodicRefresh(false);
  57.     },
  58.  
  59.     sendRequest: function( url, data, success, contentType )
  60.     {
  61.         var o = { };
  62.         o.cache = false;
  63.         o.contentType = contentType;
  64.         o.data = data;
  65.         o.dataType = 'json';
  66.         o.error = this.ajaxError;
  67.         o.success = success;
  68.         o.type = 'POST';
  69.         o.url = url;
  70.         $.ajax( o );
  71.     },
  72.  
  73.     loadDaemonPrefs: function() {
  74.         var tr = this._controller;
  75.         var o = { };
  76.         o.method = 'session-get';
  77.         this.sendRequest( RPC._Root, $.toJSON(o), function(data) {
  78.             var o = data.arguments;
  79.             Prefs.getClutchPrefs( o );
  80.             tr.updatePrefs( o );
  81.         }, "json" );
  82.     },
  83.  
  84.     loadTorrents: function() {
  85.         var tr = this._controller;
  86.         var o = { };
  87.         o.method = 'torrent-get'
  88.         o.arguments = { };
  89.         o.arguments.fields = [
  90.             'addedDate', 'announceURL', 'comment', 'creator',
  91.             'dateCreated', 'downloadedEver', 'error', 'errorString',
  92.             'eta', 'hashString', 'haveUnchecked', 'haveValid', 'id',
  93.             'isPrivate', 'leechers', 'leftUntilDone', 'name',
  94.             'peersConnected', 'peersGettingFromUs', 'peersSendingToUs',
  95.             'rateDownload', 'rateUpload', 'seeders', 'sizeWhenDone',
  96.             'status', 'swarmSpeed', 'totalSize', 'uploadedEver' ];
  97.         this.sendRequest( RPC._Root, $.toJSON(o), function(data) {
  98.             tr.updateTorrents( data.arguments.torrents );
  99.         }, "json" );
  100.     },
  101.  
  102.     sendTorrentCommand: function( method, torrents ) {
  103.         var remote = this;
  104.         var o = { };
  105.         o.method = method;
  106.         o.arguments = { };
  107.         o.arguments.ids = [ ];
  108.         if( torrents != null )
  109.             for( var i=0, len=torrents.length; i<len; ++i )
  110.                 o.arguments.ids.push( torrents[i].id() );
  111.         this.sendRequest( RPC._Root, $.toJSON(o), function( ) {
  112.             remote.loadTorrents();
  113.         }, "json" );
  114.     },
  115.     startTorrents: function( torrents ) {
  116.         this.sendTorrentCommand( 'torrent-start', torrents );
  117.     },
  118.     stopTorrents: function( torrents ) {
  119.         this.sendTorrentCommand( 'torrent-stop', torrents );
  120.     },
  121.     removeTorrents: function( torrents ) {
  122.         this.sendTorrentCommand( 'torrent-remove', torrents );
  123.     },
  124.     removeTorrentsAndData: function( torrents ) {
  125.         var remote = this,
  126.             o = { };
  127.         o.method = 'torrent-remove';
  128.         o.arguments = { };
  129.         o.arguments['delete-local-data'] = true;
  130.         o.arguments.ids = [ ];
  131.         if( torrents != null )
  132.             for( var i=0, len=torrents.length; i<len; ++i )
  133.                 o.arguments.ids.push( torrents[i].id() );
  134.         this.sendRequest( RPC._Root, $.toJSON(o), function( ) {
  135.             remote.loadTorrents();
  136.         }, "json" );
  137.     },
  138.     addTorrentByUrl: function( url, options ) {
  139.         this.sendRequest( RPC._Root, $.toJSON({
  140.             method: 'torrent-add',
  141.             arguments: {
  142.                 paused: (options.paused ? 'true' : 'false'),
  143.                 filename: url
  144.             }
  145.         }) );
  146.     },
  147.     savePrefs: function( args ) {
  148.         var remote = this;
  149.         var o = { };
  150.         o.method = 'session-set';
  151.         o.arguments = args;
  152.         this.sendRequest( RPC._Root, $.toJSON(o), function(){
  153.             remote.loadDaemonPrefs();
  154.         }, "json" );
  155.     }
  156. };
  157.