from Components.MenuList import MenuList
from Components.ScrollLabel import ScrollLabel
from Components.Sources.StaticText import StaticText
from os import path as os_path, listdir

try:
	from Plugins.SystemPlugins.SoftwareManager.SoftwareTools import iSoftwareTools
	from Plugins.SystemPlugins.SoftwareManager.plugin import PluginDetails
	from Plugins.SystemPlugins.SoftwareManager.plugin import IPKGMenu
	print "[PowerMetrixFHD] 010 -OK-- SoftwareManager"
except:
	print "[PowerMetrixFHD] 010 -NOK- SoftwareManager"

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

def PluginDetailssetInfos(self):
	if self.attributes.has_key("screenshot"):
		self.loadThumbnail(self.attributes)
	else:
		self.setThumbnail(noScreenshot = True)

	if self.attributes.has_key("name"):
		self.pluginname = self.attributes["name"]
	else:
		self.pluginname = _("unknown")

	if self.attributes.has_key("author"):
		self.author = self.attributes["author"]
	else:
		self.author = _("unknown")

	if self.attributes.has_key("description"):
		self.description = _(self.attributes["description"].replace("\\n", "\n"))
	else:
		self.description = _("No description available.")

	self["author"].setText(_("Author: ") + self.author)
	self["detailtext"].setText(_(self.description))
	if self.pluginstate in ("installable", "install"):
		if iSoftwareTools.NetworkConnectionAvailable:
			self["key_green"].setText(_("Install"))
		else:
			self["key_green"].setText("")
	else:
		self["key_green"].setText(_("Remove"))

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

def IPKGMenufill_list(self):
	self.flist = []
	self.path = "/etc/apt/sources.list.d/"
	if (os_path.exists(self.path) == False):
		self.entry = False
		return
	for file in listdir(self.path):
		if (file.endswith(".list")):
			if file != "dummy.list":
				self.flist.append((file))
				self.entry = True
				self["filelist"].l.setList(self.flist)

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

def doSoftwareManagerChange():
	try:
		PluginDetails.setInfos = PluginDetailssetInfos
		IPKGMenu.fill_list = IPKGMenufill_list
		print "[PowerMetrixFHD] 010 -OK-- SoftwareManager"
	except:
		print "[PowerMetrixFHD] 010 -NOK- SoftwareManager"
