# rbclient_xchat v0.1
# Copyright (C) 2006 - Luis Ferreira <anarka@anarka.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 of the License, 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 this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
# 
# Needs rhythmbox 0.9.5
# Uses rhythmbox-client
# Made using copy & paste from the following scripts:
#
# amarok_xchat by sven kissner (aka chimaera)
# simple script to display the song currently played in amarok
#  
# xchatrb.py 
# Implements command /psong, which prints the currently playing song
# in RhythmBox.
# xchatrb.py (c) 2003 Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
# 		      G-LiTe / <stephan@wilkogazu.nl>

rb_command_help_text = """Rhythmbox control. Usage:
/rb play    Prints currently playing on Rhythmbox
/rb prev    Change to the previous song
/rb next    Change to the next song
/rb toggle  Toggle play/pause mode"""

__module_name__ = "rbclient_xchat" 
__module_version__ = "0.1" 
__module_description__ = "python module for xchat to display playing tracks in rhythmbox" 
 
import xchat 
import commands

def rb_play(word, word_eol, userdata):
    xchat.command('me is listening to ' + commands.getoutput('rhythmbox-client --print-playing-format %ta\ -\ %at\ -\ %tN\ -\ %tt\ "(%td)"'))
    return xchat.EAT_ALL

def rb_next(word, word_eol, userdata):
    commands.getoutput('rhythmbox-client --next')
    return xchat.EAT_ALL

def rb_prev(word, word_eol, userdata):
    commands.getoutput('rhythmbox-client --previous')
    return xchat.EAT_ALL

def rb_pause(word, word_eol, userdata):
    commands.getoutput('rhythmbox-client --pause')
    return xchat.EAT_ALL

def rb_toggle(word, word_eol, userdata):
    commands.getoutput('rhythmbox-client --play-pause')
    return xchat.EAT_ALL

def rb_command_cb(word, word_eol, userdata):
    try:
	if word[1].lower() == 'play':
	    return rb_play(word[1:], word_eol[1:], userdata)
	if word[1].lower() == 'next':
	    return rb_next(word[1:], word_eol[1:], userdata)
	if word[1].lower() == 'prev':
	    return rb_prev(word[1:], word_eol[1:], userdata)
	if word[1].lower() == 'toggle':
	    return rb_toggle(word[1:], word_eol[1:], userdata)
	if word[1].lower() == 'help':
	    print rb_command_help_text
    except IndexError:
	print rb_command_help_text
    return xchat.EAT_ALL

xchat.hook_command("rb", rb_command_cb, help=rb_command_help_text)

xchat.prnt('rbclient_xchat v0.1 loaded... for help type /rb help')
