# -*- coding: utf-8 -*-

from Plugins.Bp.geminimain.GeminiDef import *

from urllib import unquote
from Screens.MessageBox import MessageBox

from time import strftime, localtime, time as getTime
from os import system as os_system

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

class ExPlugin():
	def __init__(self):
		from Plugins.Bp.geminimain.plugin import geventNotifier
		from Plugins.Bp.geminimain.gTools import libgeminimain
		
		self.SRisRunning = bool(libgeminimain.getBGstatus() & BG_STREAM)
		geventNotifier.append((EVENT_BACKGROUND, self.__newBGEvent))
		self.Notifier = []
		
	def __newBGEvent(self, what=0, val=0):
		self.SRisRunning=False
		if val & BG_STREAM:
			self.SRisRunning=True
			
		for callback in self.Notifier:
			try:
				callback()
			except AttributeError:
				self.Notifier.remove(callback)
	
	def start(self,session,web=None):
		#print ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>[StartStreamripper]start"
		currPlay = session.nav.getCurrentService()
		
		from Plugins.Bp.geminimain.gLocale import _
		from Plugins.Bp.geminimain.gTools import libgeminimain, getFreeSize
		from Plugins.Bp.geminimain.Cjukeboxevent import cjukeboxevent

		if bool(libgeminimain.getBGstatus() & BG_STREAM)==False and cjukeboxevent.CurrSource!="":
			cmd = "/usr/bin/geminisr '" + cjukeboxevent.CurrSource + "' "
			path = libgeminimain.getStrConfig("SR_path","/media/hdd")
			streamtype = libgeminimain.getIntConfig("SR_conststream", 0)
	
			if streamtype==1:
				file = path + "/ConstStream" + strftime("_%d%m%Y_%H%M", localtime(getTime()))
				cmd += "-A --xs_none --xs_silence_length=0 -a '" + file + "' &"
			else:
				cmd += "-d '" + path + "' &"
			print "[extPlugin]",cmd
			libgeminimain.sendDbus("gdaemon",0,cmd)
			
			if web is None:
				tmpstr = _("Recording is started") + "\n\n" + _("Record Location") + ":\n" + path
				tmpstr += "\n" + _("free") + " %s" %libgeminimain.getHumanSize(getFreeSize(path)) + "\n\n"
				if streamtype == 1:
					tmpstr += _("Recording as a continuous stream")
				else:
					tmpstr += _("Recording as an individual title")
				session.open(MessageBox, tmpstr, MessageBox.TYPE_INFO, timeout=5)
				
				
		elif bool(libgeminimain.getBGstatus() & BG_STREAM)==False and web:
			currentServiceRef = session.nav.getCurrentlyPlayingServiceReference()
			if currentServiceRef is not None:
				url = currentServiceRef.toString().replace("%3a", ":")[23:]
				
			#print ">>>>>>>>>>>>>>>>>>>>>>>>>>", url

			cmd = "/usr/bin/geminisr '" + url + "' "
			path = libgeminimain.getStrConfig("SR_path","/media/hdd")
			streamtype = libgeminimain.getIntConfig("SR_conststream", 0)
	
			if streamtype==1:
				file = path + "/ConstStream" + strftime("_%d%m%Y_%H%M", localtime(getTime()))
				cmd += "-A --xs_none --xs_silence_length=0 -a '" + file + "' &"
			else:
				cmd += "-d '" + path + "' &"
			print "[extPlugin]",cmd
			libgeminimain.sendDbus("gdaemon",0,cmd)

		
	def stop(self,session=None):
		from Plugins.Bp.geminimain.gLocale import _
		from Plugins.Bp.geminimain.gTools import libgeminimain
		if bool(libgeminimain.getBGstatus() & BG_STREAM):
			os_system("/usr/bin/killall -SIGTERM geminisr")
			if session:
				tmpstr = _("One moment please...") + "\nStreamripper " + _("becomes terminates") + "\n" + _("That can last some time\n")
				session.open(MessageBox, tmpstr, MessageBox.TYPE_INFO, timeout=7)
			else:
				from time import sleep
				sleep(5)
				print "[extPlugin], killall -SIGTERM geminisr"
			
	def running(self):
		return self.SRisRunning
		
	def addMenu(self):
		from Plugins.Bp.geminimain.gLocale import _
		if self.SRisRunning:
			return [(_("Stop") + " " + _("Recording"), 101)]
		return [(_("Start") + " " + _("Recording"), 100)]
		
exPlugin = ExPlugin()

from __init__ import addNetcastPlugin
addNetcastPlugin(("exPlugin","start","stop"));
