from Screens.Screen import Screen
from Components.Harddisk import harddiskmanager, BlockDevice
from Components.Label import Label
from Components.Pixmap import Pixmap
from enigma import eTimer

try:
	from Screens.HarddiskSetup import HarddiskWait, StorageInformation, HARDDISK_INITIALIZE, HARDDISK_CHECK, harddisk_description, storagedevice_description
	print "[PowerMetrixFHD] 024 -OK-- HarddiskSetup"
except:
	print "[PowerMetrixFHD] 024 -NOK- HarddiskSetup"

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

def HarddiskWait__init__(self, session, hdd, stype, numpart = None):
	Screen.__init__(self, session)
	self.hdd = hdd
	self.devicedescription = storagedevice_description
	if not self.hdd.isRemovable:
		self.devicedescription = harddisk_description
	self.isFstabMounted = False
	self.numpart = numpart
	uuid = partitionPath = None
	if not self.numpart:
		self.numpart = self.hdd.numPartitions()

	if self.numpart == 0:
		uuid = harddiskmanager.getPartitionUUID(hdd.device)
		partitionPath = hdd.dev_path
	if self.numpart >= 1:
		uuid = harddiskmanager.getPartitionUUID(hdd.device + str(self.numpart))
		partitionPath = hdd.partitionPath(str(self.numpart))

	if uuid is not None:
		mountpath = harddiskmanager.get_fstab_mountpoint(partitionPath)
		if harddiskmanager.isUUIDpathFsTabMount(uuid, mountpath) or harddiskmanager.isPartitionpathFsTabMount(uuid, mountpath):
			self.isFstabMounted = True

	self.timer = eTimer()
	if stype == HARDDISK_INITIALIZE:
		text = _("Initializing %(desc)s...") % self.devicedescription
		self.timer_conn = self.timer.timeout.connect(self.doInit)
	elif stype == HARDDISK_CHECK:
		text = _("Checking Filesystem...")
		self.timer_conn = self.timer.timeout.connect(self.doCheck)
	self["wait"] = Label(text)
	self["waitback"] = Label()
	self["waitborder"] = Pixmap()
	self.timer.start(1000)

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

def StorageInformationupdateInfo(self):
	init_msg = _("Please initialize this %(desc)s!") % self.devicedescription
	unknownpartition_msg = _("Unsupported!")
	partitionType = None

	if self.UUID is not None:
		p = harddiskmanager.getPartitionbyUUID(self.UUID)
		if p is not None:
			partitionType = p.fsType
	if partitionType is None:
		partitionType = harddiskmanager.getBlkidPartitionType(self.partitionPath)

	if self.UUID is None and self.numPartitions == 0:
		self["introduction"].setText(init_msg)

	if self.UUID is not None:
		self["partition"].setText(_("Partition") + ": " + self.deviceName)
		if partitionType is None:
			self["filesystem"].setText(_("Filesystem") + ": " + unknownpartition_msg)
		else:
			self["filesystem"].setText(_("Filesystem") + ": " + partitionType)
		blkdev = BlockDevice(self.partitionPath)
		self["partition_capacity"].setText(_("Partition capacity") + ": "  + blkdev.capacityString())

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

def doHarddiskSetupChange():
	try:
		HarddiskWait.__init__ = HarddiskWait__init__
		StorageInformation.updateInfo = StorageInformationupdateInfo
		print "[PowerMetrixFHD] 024 -OK-- HarddiskSetup"
	except:
		print "[PowerMetrixFHD] 024 -NOK- HarddiskSetup"
