/*
* 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 Transmission
*/
function Transmission(){
this.initialize();
}
Transmission.prototype =
{
/*--------------------------------------------
*
* C O N S T R U C T O R
*
*--------------------------------------------*/
initialize: function()
{
// IE specific fixes here
if ($.browser.msie) {
try {
document.execCommand("BackgroundImageCache", false, true);
} catch(err) {}
$('head').append('');
$('head').append('');
$('.dialog_container').css('height',$(window).height()+'px');
}
// Initialize the helper classes
this.remote = new TransmissionRemote(this);
// Initialize the implementation fields
this._current_search = '';
this._torrents = [ ];
this._rows = [ ];
// Initialize the clutch preferences
Prefs.getClutchPrefs( this );
this.preloadImages();
// Set up user events
$('#pause_all_link').bind('click', this.stopAllClicked );
$('#resume_all_link').bind('click', this.startAllClicked);
$('#pause_selected_link').bind('click', this.stopSelectedClicked );
$('#resume_selected_link').bind('click', this.startSelectedClicked);
$('#remove_link').bind('click', this.removeClicked);
$('#removedata_link').bind('click', this.removeDataClicked);
$('#filter_all_link').parent().bind('click', this.showAllClicked);
$('#filter_downloading_link').parent().bind('click', this.showDownloadingClicked);
$('#filter_seeding_link').parent().bind('click', this.showSeedingClicked);
$('#filter_paused_link').parent().bind('click', this.showPausedClicked);
$('#prefs_save_button').bind('click', this.savePrefsClicked);
$('#prefs_cancel_button').bind('click', this.cancelPrefsClicked);
$('#inspector_tab_info').bind('click', this.inspectorTabClicked);
$('#inspector_tab_activity').bind('click', this.inspectorTabClicked);
if (iPhone) {
$('#torrent_inspector').bind('click', this.hideInspector);
$('#preferences_link').bind('click', this.releaseClutchPreferencesButton);
} else {
$(document).bind('keydown', this.keyDown);
$('#torrent_container').bind('click', this.deselectAll);
$('#open_link').bind('click', this.openTorrentClicked);
$('#filter_toggle_link').bind('click', this.toggleFilterClicked);
$('#inspector_link').bind('click', this.toggleInspectorClicked);
$('#upload_confirm_button').bind('click', this.confirmUploadClicked);
$('#upload_cancel_button').bind('click', this.cancelUploadClicked);
this.setupSearchBox();
this.createContextMenu();
this.createSettingsMenu();
}
// Setup the preference box
this.setupPrefConstraints();
// Setup the prefs gui
this.initializeSettings( );
// Get preferences & torrents from the daemon
this.remote.loadDaemonPrefs( );
this.remote.loadTorrents( );
this.togglePeriodicRefresh( true );
},
preloadImages: function() {
if (iPhone) {
this.loadImages(
'images/buttons/info_activity.png',
'images/buttons/info_general.png',
'images/buttons/toolbar_buttons.png',
'images/graphics/filter_bar.png',
'images/graphics/iphone_chrome.png',
'images/graphics/logo.png',
'images/progress/progress.png'
);
} else {
this.loadImages(
'images/buttons/info_activity.png',
'images/buttons/info_general.png',
'images/buttons/tab_backgrounds.png',
'images/buttons/toolbar_buttons.png',
'images/buttons/torrent_buttons.png',
'images/graphics/chrome.png',
'images/graphics/filter_bar.png',
'images/graphics/logo.png',
'images/graphics/transfer_arrows.png',
'images/progress/progress.png'
);
}
},
loadImages: function() {
for(var i = 0; i").attr("src", arguments[i]);
}
},
/*
* Set up the preference validation
*/
setupPrefConstraints: function() {
// only allow integers for speed limit & port options
$('div.preference input[@type=text]:not(#download_location)').blur( function() {
this.value = this.value.replace(/[^0-9]/gi, '');
if (this.value == '') {
if ($(this).is('#refresh_rate')) {
this.value = 5;
} else {
this.value = 0;
}
}
});
},
/*
* Load the clutch prefs and init the GUI according to those prefs
*/
initializeSettings: function( )
{
Prefs.getClutchPrefs( this );
// iPhone conditions in the section allow us to not
// include transmenu js to save some bandwidth; if we
// start using prefs on iPhone we need to weed
// transmenu refs out of that too.
$('#filter_' + this[Prefs._FilterMode] + '_link').parent().addClass('selected');
if (!iPhone) $('#sort_by_' + this[Prefs._SortMethod] ).selectMenuItem();
if (!iPhone && ( this[Prefs._SortDirection] == Prefs._SortDescending ) )
$('#reverse_sort_order').selectMenuItem();
if( this[Prefs._ShowFilter] )
this.showFilter( );
if( this[Prefs._ShowInspector] )
this.showInspector( );
},
/*
* Set up the search box
*/
setupSearchBox: function()
{
var tr = this;
var search_box = $('#torrent_search');
search_box.bind('keyup click', {transmission: this}, function(event) {
tr.setSearch(this.value);
});
if (!$.browser.safari)
{
search_box.addClass('blur');
search_box[0].value = 'Filter';
search_box.bind('blur', {transmission: this}, function(event) {
if (this.value == '') {
$(this).addClass('blur');
this.value = 'Filter';
tr.setSearch(null);
}
}).bind('focus', {}, function(event) {
if ($(this).is('.blur')) {
this.value = '';
$(this).removeClass('blur');
}
});
}
},
contextStopSelected: function( ) {
transmission.stopSelectedTorrents( );
},
contextStartSelected: function( ) {
transmission.startSelectedTorrents( );
},
contextRemoveSelected: function( ) {
transmission.removeSelectedTorrents( );
},
contextRemoveDataSelected: function( ) {
transmission.removeSelectedTorrentsAndData( );
},
contextVerifySelected: function( ) {
transmission.verifySelectedTorrents( );
},
contextToggleInspector: function( ) {
transmission.toggleInspector( );
},
contextSelectAll: function( ) {
transmission.selectAll( true );
},
contextDeselectAll: function( ) {
transmission.deselectAll( true );
},
/*
* Create the torrent right-click menu
*/
createContextMenu: function() {
var bindings = {
context_pause_selected: this.contextStopSelected,
context_resume_selected: this.contextStartSelected,
context_remove: this.contextRemoveSelected,
context_removedata: this.contextRemoveDataSelected,
context_verify: this.contextVerifySelected,
context_toggle_inspector: this.contextToggleInspector,
context_select_all: this.contextSelectAll,
context_deselect_all: this.contextDeselectAll
};
// Setup the context menu
$('ul#torrent_list').contextMenu('torrent_context_menu', {
bindings: bindings,
menuStyle: Menu.context.menu_style,
itemStyle: Menu.context.item_style,
itemHoverStyle: Menu.context.item_hover_style,
itemDisabledStyle: Menu.context.item_disabled_style,
shadow: false,
boundingElement: $('div#torrent_container'),
boundingRightPad: 20,
boundingBottomPad: 5
});
},
/*
* Create the footer settings menu
*/
createSettingsMenu: function() {
$('#settings_menu').transMenu({
selected_char: '✔',
direction: 'up',
onClick: this.processSettingsMenuEvent
});
$('#unlimited_download_rate').selectMenuItem();
$('#unlimited_upload_rate').selectMenuItem();
},
/*--------------------------------------------
*
* U T I L I T I E S
*
*--------------------------------------------*/
getAllTorrents: function()
{
return this._torrents;
},
getVisibleTorrents: function()
{
var torrents = [ ];
for( var i=0, len=this._rows.length; i div:contains('N/A')").css('color', '#666');
return;
}
name = torrents.length == 1
? torrents[0].name()
: torrents.length+' Transfers Selected';
if( torrents.length == 1 )
{
var t = torrents[0];
if( t._error_message )
{
error = t._error_message ;
}
if( t._comment)
{
comment = t._comment ;
}
if( t._creator )
{
creator = t._creator ;
}
hash = t.hash();
date_created = Math.formatTimestamp( t._creator_date );
}
for( i=0; i div:contains('N/A')").css('color', '#666');
},
/*
* Toggle the visibility of the inspector (used by the context menu)
*/
toggleInspector: function() {
if( this[Prefs._ShowInspector] )
this.hideInspector( );
else
this.showInspector( );
},
showInspector: function() {
$('#torrent_inspector').show();
if (iPhone) {
$('body').addClass('inspector_showing');
this.hideiPhoneAddressbar();
} else {
var w = $('#torrent_inspector').width() + 1 + 'px';
$('#torrent_filter_bar')[0].style.right = w;
$('#torrent_container')[0].style.right = w;
}
setInnerHTML( $('ul li#context_toggle_inspector')[0], 'Hide Inspector' );
this.setPref( Prefs._ShowInspector, true );
this.updateInspector( );
},
/*
* Hide the inspector
*/
hideInspector: function() {
$('#torrent_inspector').hide();
if (iPhone) {
transmsision.deselectAll( );
$('body.inspector_showing').removeClass('inspector_showing');
transmission.hideiPhoneAddressbar();
} else {
$('#torrent_filter_bar')[0].style.right = '0px';
$('#torrent_container')[0].style.right = '0px';
setInnerHTML( $('ul li#context_toggle_inspector')[0], 'Show Inspector' );
}
this.setPref( Prefs._ShowInspector, false );
},
/*
* Toggle the visibility of the filter bar
*/
toggleFilter: function() {
if( this[Prefs._ShowFilter] )
this.hideFilter();
else
this.showFilter();
},
showFilter: function( ) {
var container_top = parseInt($('#torrent_container').position().top) + $('#torrent_filter_bar').height() + 1;
$('#torrent_container').css('top', container_top + 'px');
$('#torrent_filter_bar').show();
this.setPref( Prefs._ShowFilter, true );
},
hideFilter: function()
{
var container_top = parseInt($('#torrent_container').css('top')) - $('#torrent_filter_bar').height() - 1;
$('#torrent_container').css('top', container_top + 'px');
$('#torrent_filter_bar').hide();
this.setPref( Prefs._ShowFilter, false );
this.setFilter( Prefs._FilterAll );
},
/*
* Process got some new torrent data from the server
*/
updateTorrents: function( torrent_list )
{
var torrent_data;
var new_torrents = [];
var torrent_ids = [];
var handled = [];
// refresh existing torrents
for( var i=0, len=torrent_list.length; i