/* * Copyright (C) 2005-2010 Team XBMC * http://www.xbmc.org * * This Program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2, or (at your option) * any later version. * * This Program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with XBMC; see the file COPYING. If not, write to * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. * http://www.gnu.org/copyleft/gpl.html * */ var NowPlayingManager = function() { this.init(); return true; } NowPlayingManager.prototype = { updateCounter: 0, init: function() { $('#pbPause').hide(); /* Assume we are not playing something */ this.bindPlaybackControls(); this.updateState(); $('#nextTrack').bind('click', jQuery.proxy(this.showPlaylist, this)); $('#nowPlayingPlaylist').bind('click', function() {return false;}); $(window).bind('click', jQuery.proxy(this.hidePlaylist, this)); }, updateState: function() { jQuery.post(JSON_RPC, '{"jsonrpc": "2.0", "method": "Player.GetActivePlayers", "id": 1}', jQuery.proxy(function(data) { if (data && data.result) { if (data.result.audio) { this.activePlayer = 'Audio'; this.stopVideoPlaylistUpdate(); this.displayAudioNowPlaying(); } else if (data.result.video) { this.activePlayer = 'Video'; this.stopAudioPlaylistUpdate(); this.displayVideoNowPlaying(); } else { this.stopRefreshTime(); } } setTimeout(jQuery.proxy(this.updateState, this), 1000); }, this), 'json'); }, bindPlaybackControls: function() { $('#pbNext').bind('click', jQuery.proxy(this.nextTrack, this)); $('#pbPrev').bind('click', jQuery.proxy(this.prevTrack, this)); $('#pbStop').bind('click', jQuery.proxy(this.stopTrack, this)); $('#pbPlay').bind('click', jQuery.proxy(this.playPauseTrack, this)); $('#pbPause').bind('click', jQuery.proxy(this.playPauseTrack, this)); }, showPlaylist: function() { $('#nextText').html('Playlist: '); $('#nowPlayingPlaylist').show(); return false; }, hidePlaylist: function() { $('#nextText').html('Next: '); $('#nowPlayingPlaylist').hide(); return false; }, nextTrack: function() { if (this.activePlayer) { jQuery.post(JSON_RPC + '?SkipNext', '{"jsonrpc": "2.0", "method": "' + this.activePlayer + 'Player.SkipNext", "id": 1}', jQuery.proxy(function(data) { if (data && data.result == 'OK') { //this.updateAudioPlaylist(true); } }, this), 'json'); } }, prevTrack: function() { if (this.activePlayer) { jQuery.post(JSON_RPC + '?SkipPrevious', '{"jsonrpc": "2.0", "method": "' + this.activePlayer + 'Player.SkipPrevious", "id": 1}', jQuery.proxy(function(data) { if (data && data.result == 'OK') { //this.updateAudioPlaylist(true); } }, this), 'json'); } }, stopTrack: function() { if (this.activePlayer) { jQuery.post(JSON_RPC + '?Stop', '{"jsonrpc": "2.0", "method": "' + this.activePlayer + 'Player.Stop", "id": 1}', jQuery.proxy(function(data) { if (data && data.result == 'OK') { this.playing = false; this.paused = false; this.trackBaseTime = 0; this.showPlayButton(); } }, this), 'json'); } }, playPauseTrack: function() { if (this.activePlayer) { var method = this.activePlayer + ((this.playing || this.paused) ? 'Player.PlayPause' : 'Playlist.Play'); jQuery.post(JSON_RPC + '?PlayPause', '{"jsonrpc": "2.0", "method": "' + method + '", "id": 1}', jQuery.proxy(function(data) { if (data && data.result) { this.playing = data.result.playing; this.paused = data.result.paused; if (this.playing) { this.showPauseButton(); } else { this.showPlayButton(); } } }, this), 'json'); } }, showPauseButton: function() { $('#pbPause').show(); $('#pbPlay').hide(); }, showPlayButton: function() { $('#pbPause').hide(); $('#pbPlay').show(); }, displayAudioNowPlaying: function() { if (!this.autoRefreshAudioPlaylist) { this.autoRefreshAudioPlaylist = true; this.updateAudioPlaylist(); } }, displayVideoNowPlaying: function() { if (!this.autoRefreshVideoPlaylist) { this.autoRefreshVideoPlaylist = true; this.updateVideoPlaylist(); } }, playPlaylistItem: function(sender) { var sequenceId = $(sender.currentTarget).attr('seq'); if (!this.activePlaylistItem || (this.activePlaylistItem !== undefined && sequenceId != this.activePlaylistItem.seq)) { jQuery.post(JSON_RPC + '?PlaylistItemPlay', '{"jsonrpc": "2.0", "method": "' + this.activePlayer + 'Playlist.Play", "params": ' + sequenceId + ', "id": 1}', function() {}, 'json'); } this.hidePlaylist(); }, playlistChanged: function(newPlaylist) { if (this.activePlaylist && !newPlaylist || !this.activePlaylist && newPlaylist) { return true; } if (!this.activePlaylist && !newPlaylist) { return false; } if (this.activePlaylist.length != newPlaylist.length) { return true; } for (var i = 0; i < newPlaylist.length; i++) { if (!this.comparePlaylistItems(this.activePlaylist[i], newPlaylist[i])) { return true; } } return false; }, updateAudioPlaylist: function() { jQuery.ajax({ type: 'POST', url: JSON_RPC + '?updateAudioPlaylist', data: '{"jsonrpc": "2.0", "method": "AudioPlaylist.GetItems", "params": { "fields": ["title", "album", "artist", "duration"] }, "id": 1}', success: jQuery.proxy(function(data) { if (data && data.result && data.result.items && data.result.total > 0) { //Compare new playlist to active playlist, only redraw if a change is noticed. if (this.playlistChanged(data.result.items) || (this.activePlaylistItem && (this.activePlaylistItem.seq != data.result.current))) { var ul = $('