##
## ServiceNumber Renderer
## by and AliAbdul
##
## Example usage in the skin.xml:
## <widget source="session.CurrentService" render="ServiceNumber" position="90,406" size="30,24" font="Regular;20" transparent="1" />
##
from Components.VariableText import VariableText
from enigma import eLabel, iPlayableService, iServiceInformation, eServiceReference, eServiceCenter
from Renderer import Renderer

##########################################################################

class ServiceNumber(Renderer, VariableText):
	def __init__(self):
		Renderer.__init__(self)
		VariableText.__init__(self)
		self.list = self.getList()
	GUI_WIDGET = eLabel

	def changed(self, what):
		service = self.source.service
		info = service and service.info()
		if info is None:
			return
		
		ref = info.getInfoString(iServiceInformation.sServiceref)
		name = info.getName().replace('\xc2\x86', '').replace('\xc2\x87', '')
		if not self.list.__contains__(ref):
			self.text = name
		else:
			idx = 1
			for x in self.list:
				if x == ref:
					self.text = ("%s. %s") % (str(idx), name)
					break
				else:
					idx += 1

	def getList(self):
		list = []
		
		serviceHandler = eServiceCenter.getInstance()
		services = serviceHandler.list(eServiceReference('1:7:1:0:0:0:0:0:0:0:(type == 1) || (type == 17) || (type == 195) || (type == 25) FROM BOUQUET "bouquets.tv" ORDER BY bouquet'))
		bouquets = services and services.getContent("SN", True)
		
		if len(bouquets) > 0:
			for bouquet in bouquets:
				services = serviceHandler.list(eServiceReference(bouquet[0]))
				channels = services and services.getContent("SN", True)
				
				for channel in channels:
					list.append(channel[0])
		else:
			print "[ServiceNumber] Error? Bouquets: " + str(bouquets)
		
		return list
