#if 0 /*
# -----------------------------------------------------------------------
# mythtv.py - a plugin to use the tv-part from mythtv in freevo
# -----------------------------------------------------------------------
# $Id: 
# -----------------------------------------------------------------------
# $Log: 
# -----------------------------------------------------------------------
# Freevo - A Home Theater PC framework
# Copyright (C) 2003 Krister Lagerstrom, et al. 
# Please see the file freevo/Docs/CREDITS for a complete list of authors.
#
# 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 MER-
# CHANTABILITY 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
#
# ----------------------------------------------------------------------- */
#endif


import plugin
import menu
import config
import os
import rc
from item import Item

import signal
import childapp
from event import *

class PluginInterface(plugin.MainMenuPlugin):
    """
    A plugin to use the tv-part from mythtv in freevo.

    Usage:
    plugin.remove('tv')
    plugin.activate('mythtv')
    in local_conf.py

    You can also set the following variables:
    MYTHTV_CMD = '/usr/bin/mythtv'
    MYTHEPG_CMD = '/usr/bin/mythepg'
    MYTHPROGFIND_CMD = '/usr/bin/mythprogfind'
    MYTHFRONTEND_CMD = '/usr/bin/mythfrontend'
    MYTH_EVENT_HANDLING = 1
    """

    def items(self, parent):
        import skin

	skin = skin.get_singleton()
	menu_items = skin.settings.mainmenu.items
	
    	icon = ""
	outicon = ""
	
	if menu_items['tv'].icon:
	    icon = os.path.join(skin.settings.icon_dir, menu_items['tv'].icon)
	if menu_items['tv'].outicon:
	    outicon = os.path.join(skin.settings.icon_dir, menu_items['tv'].outicon)
	
        return ( menu.MenuItem( menu_items['tv'].name, icon=icon,
				action=MythMenu().main_menu, type='main',
				image=menu_items['tv'].image, parent=parent, outicon=outicon), )

    def config(self):
        """
	config variables for this plugin
	"""
	return [ ('MYTHTV_CMD', '/usr/bin/mythtv', 'mythtv command'),
	         ('MYTHEPG_CMD', '/usr/bin/mythepg', 'mythepg command'),
		 ('MYTHPROGFIND_CMD', '/usr/bin/mythprogfind', 'mythprogfind command'),
		 ('MYTHFRONTEND_CMD', '/usr/bin/mythfrontend', 'mythfrontend command'),
		 ('MYTH_EVENT_HANDLING', 1, 'let mythtv to the eventhandling (lirc)'),]
	

class MythMenu(Item):
    def __init__(self):
        Item.__init__(self)
	self.type = 'tv'

    def main_menu(self, arg, menuw):
        items = []
	items.append(menu.MenuItem(_('Watch TV'), action=self.watch_tv))
	items.append(menu.MenuItem(_('Program Guide'), action=self.epg))
	items.append(menu.MenuItem(_('Program Finder'), action=self.progfinder))
	items.append(menu.MenuItem(_('MythTV Frontend'), action=self.frontend))

	menuw.pushmenu(menu.Menu(_('MythTV Main Menu'), items, item_types = 'tv'))

    def watch_tv(self, arg, menuw):
        MythTV().start(command=config.MYTHTV_CMD)

    def epg(self, arg, menuw):
        MythTV().start(command=config.MYTHEPG_CMD)

    def progfinder(self, arg, menuw):
        MythTV().start(command=config.MYTHPROGFIND_CMD)

    def frontend(self, arg, menuw):
        MythTV().start(command=config.MYTHFRONTEND_CMD)


class MythTV:
    def __init__(self):
        self.thread = childapp.ChildThread()
	self.thread.stop_osd = True
	self.app_mode = 'tv'

    def start(self, command):
	self.mode = 'tv'
	self.prev_app = rc.app()
	rc.app(self)
	self.thread.start(MythtvApp, (command))
    
    def stop(self):
	self.thread.stop()
        rc.app(self.prev_app)

    def eventhandler(self, event, menuw=None):
        """
	let mythtv do all eventhandling while it's running and return
	true to freevo. (at least until I find a better way...)
	"""
        if event == PLAY_END:
	    self.stop()
	    return True
	
	elif event:
	    if config.MYTH_EVENT_HANDLING:
	        return True
	    else:
	        return False
	    

class MythtvApp(childapp.ChildApp):
    def __init__(self, (app)):
        childapp.ChildApp.__init__(self, app)
    
    def kill(self):
        childapp.ChildApp.kill(self, signal.SIGINT)

    def stopped(self):
        rc.post_event(PLAY_END)

