# -*- coding: utf-8 -*-
from Plugins.Bp.geminimain.gLocale import _
from Plugins.Bp.geminimain.WidgetVariableList import WidgetVariableList
from Plugins.Bp.geminimain.WidgetConfig import gConfigSelection, gConfigDirectory, gConfigWindow, gConfigOnOff, CONFIGSEPERATOR
from Plugins.Bp.geminimain.WidgetMessageBox import gMessageBox
from Plugins.Bp.geminimain.gTools import libgeminimain, cleanexit, pathExists, pathRemove, MENUSEPERATOR
from Plugins.Bp.geminimain.plugin import cgevent,dbusNotifier
from Plugins.Bp.geminidreamnetcast.plugins.exPlugin import exPlugin

from Components.config import config, NoSave, getConfigListEntry

#---------------------------------------------------------------------

class Cgsr(WidgetVariableList):
	GETSTATUS=1
	PLUGINSTARTED=3
	PLUGINSTOPPED=4
	def __init__(self, session, name=None):
		WidgetVariableList.__init__ (self, session, _("Streamripper"), icon="geminisr/icon_gsr.png", NameBlue=_("Setup"), selectionEnabled=0)
		self.skinName = [self.__class__.__name__, "VariableList3Button_" + self.__class__.__name__, "VariableList3Button"]
		
		self.__RecLocation=None
		self.__exPlugin = exPlugin

		dbusNotifier.append(("streamripper", self.__newSRevent))

		self.__exPlugin.Notifier.append(self.__FillList)
		if self.__exPlugin.running():
			libgeminimain.sendDbus("streamripper",self.PLUGINSTARTED,"")
			
		self.onLayoutFinish.append(self.__FillList)
		
	def KeyExit(self):
		self.__exPlugin.Notifier.remove(self.__FillList)
		dbusNotifier.remove(("streamripper", self.__newSRevent))
		if self.__exPlugin.running():
			libgeminimain.sendDbus("streamripper",self.PLUGINSTOPPED,"")
		self.close()
		cleanexit(__name__)
		
	def __FillList(self,debugfile=None):
		self.__RecLocation=None
		self.SetRB()
		self.SetGB()
		
		list=[]
		if self.__exPlugin.running()==False:
			list.append((_("Streamripper") + " " + _("not running"),None,None))
		else:
			self.SetRB(_("Stop"))
			if debugfile:
				try:
					file = open(debugfile, 'r')
					lines = file.readlines()
					file.close()
					
					if len(lines)>6:
						list.append((_("Version") + ":   " + lines[0].strip(),None,None))
						list.append((_("Status") + ":   " + lines[1].strip(),None,None))
						list.append(MENUSEPERATOR)
						text = _("Recorded %s tracks in %s\n") %(lines[2].strip(), lines[6].strip())
						list.append((text,None,None))
						if lines[7]!="./\n":
							self.__RecLocation=lines[7].strip()
							self.SetGB(_("Record Location"))
							list.append((_("Record Location") + ":   " + self.__RecLocation,None,None))
						list.append(MENUSEPERATOR)
						list.append((_("Server") + ":   " + lines[4].strip(),None,None))
						list.append((_("Name") + ":   " + lines[3].strip(),None,None))
						list.append((_("Bitrate") + ":   " + lines[5].strip() + " kbps",None,None))
						if len(lines)>7:
							list.append(MENUSEPERATOR)
							list.append((_("Title") + ":   " + lines[8].strip(),None,None))
							list.append((_("Size") + ":   " + lines[9].strip(),None,None))
				except:
					list.append((_("Error reading file %s!") %debugfile,None,None))
			else:
				list.append((_("Streamripper") + " " + _("running"),None,None))
				list.append((_("Please wait..."),None,None))
		self.SetList(list)
		
	def __newSRevent(self, func, value):
		if func==self.GETSTATUS:
			self.__FillList(value)

	def KeyRed(self):
		if self.__exPlugin.running():
			self.__exPlugin.stop(self.session)
		
	def KeyBlue(self):
		self.session.open(SRSetup)
		
	def KeyGreen(self):
		if self.__RecLocation:
			from Plugins.Bp.geminimain.FileBrowser import gBrowser
			self.session.open(gBrowser, setPath=self.__RecLocation+ "/")
		
#-------------------------------------------------------------------------
		
class SRSetup(gConfigWindow):
	def __init__(self, session):

		config.bp.dir = NoSave(gConfigDirectory(default=cgevent.getConfig("SR_path","/media/hdd/Streamripper"), visible_width=35))
		config.bp.stream = NoSave(gConfigOnOff(default=cgevent.getConfig("SR_conststream", 0)))
		
		list = []
		list.append(getConfigListEntry(_("record directory"), config.bp.dir))
		list.append(getConfigListEntry(_("constant stream"), config.bp.stream))
		gConfigWindow.__init__ (self, session, list=list)
	
	def KeyGreen(self):
		cgevent.setConfig("SR_path", config.bp.dir.value)
		cgevent.setConfig("SR_conststream", config.bp.stream.value)
		self.close()

	def KeyExit(self):
		self.close()
