from enigma import *
from Screens.Screen import Screen
from Screens.MessageBox import MessageBox
from Components.ActionMap import ActionMap
from Components.ScrollLabel import ScrollLabel
from Components.GUIComponent import *
from Components.Label import Label
from Components.MenuList import MenuList
from Plugins.Plugin import PluginDescriptor

import string
import os

class GameStarter(Screen):
	skin = """
		<screen position="185, 140" size="350,300" title="Tuxbox Games" >
			<widget name="tracks" position="0,0" size="350,270" scrollbarMode="showOnDemand" />
		</screen>"""

        def __init__(self, session, args = None):
        	self.skin = GameStarter.skin
		Screen.__init__(self, session)
		self.container=eConsoleAppContainer()
		self.container.appClosed.get().append(self.finished)
		self.path = "/usr/games/"
		
		self["actions"] = ActionMap(["WizardActions"],
		{
			"ok": self.KeyOk,
			"back": self.close
		}, -1)

		self.tlist = []
		self["tracks"] = MenuList(self.tlist)
		self.fill_list()

	def fill_list(self):
		self.tlist = []

		if os.path.exists(self.path):
			dir = os.listdir(self.path)
			for x in dir:
				self.tlist.append(x)
				self.entry = True
				
		self["tracks"].l.setList(self.tlist)

	def KeyOk(self):
		self.runapp()

	def runapp(self):
		self.sel = self["tracks"].getCurrent()
		self.val = self.path + self.sel
		self.hide()
		eDBoxLCD.getInstance().lock()
		eRCInput.getInstance().lock()
		fbClass.getInstance().lock()
		self.container.execute(self.val)

	def finished(self,retval):
		fbClass.getInstance().unlock()
		eRCInput.getInstance().unlock()
		eDBoxLCD.getInstance().unlock()
		self.show()

def main(session, **kwargs):
	session.open(GameStarter)

def Plugins(**kwargs):
	return PluginDescriptor(name="Tuxbox Games", description="Tuxbox Games", where = PluginDescriptor.WHERE_PLUGINMENU, fnc=main)
	