Task improvements, run fsck in background
[openblackhole/openblackhole-enigma2.git] / lib / python / Screens / HarddiskSetup.py
1 from Screen import Screen
2 from Components.ActionMap import ActionMap
3 from Components.Harddisk import harddiskmanager                 #global harddiskmanager
4 from Components.MenuList import MenuList
5 from Components.Label import Label
6 from Components.Pixmap import Pixmap
7 from Screens.MessageBox import MessageBox
8 import Components.Task
9 from enigma import eTimer
10
11 class HarddiskWait(Screen):
12         def doInit(self):
13                 self.timer.stop()
14                 result = self.hdd.initialize()
15                 self.close(result)
16
17         def doCheck(self):
18                 self.timer.stop()
19                 Components.Task.job_manager.AddJob(self.hdd.createCheckJob())
20                 self.close(None)
21
22         def __init__(self, session, hdd, type):
23                 Screen.__init__(self, session)
24                 self.hdd = hdd
25                 self.timer = eTimer()
26                 if type == HarddiskSetup.HARDDISK_INITIALIZE:
27                         text = _("Initializing storage device...")
28                         self.timer.callback.append(self.doInit)
29                 else:
30                         text = _("Checking Filesystem...")
31                         self.timer.callback.append(self.doCheck)
32                 self["wait"] = Label(text)
33                 self.timer.start(100)
34
35 class HarddiskSetup(Screen):
36         HARDDISK_INITIALIZE = 1
37         HARDDISK_CHECK = 2
38
39         def __init__(self, session, hdd, type = None):
40                 Screen.__init__(self, session)
41                 self.hdd = hdd
42
43                 if type not in (self.HARDDISK_INITIALIZE, self.HARDDISK_CHECK):
44                         self.type = self.HARDDISK_INITIALIZE
45                 else:
46                         self.type = type
47
48                 self["model"] = Label(_("Model: ") + hdd.model())
49                 self["capacity"] = Label(_("Capacity: ") + hdd.capacity())
50                 self["bus"] = Label(_("Bus: ") + hdd.bus())
51                 self["initialize"] = Pixmap()
52
53                 if self.type == self.HARDDISK_INITIALIZE:
54                         text = _("Initialize")
55                 else:
56                         text = _("Check")
57                 self["initializetext"] = Label(text)
58
59                 self["actions"] = ActionMap(["OkCancelActions"],
60                 {
61                         "ok": self.close,
62                         "cancel": self.close
63                 })
64                 
65                 self["shortcuts"] = ActionMap(["ShortcutActions"],
66                 {
67                         "red": self.hddQuestion
68                 })
69
70         def hddReady(self, result):
71                 print "Result: " + str(result)
72                 if result is None:
73                         # todo: Notify about background task?
74                         self.close()
75                 elif (result != 0):
76                         if self.type == self.HARDDISK_INITIALIZE:
77                                 message = _("Unable to initialize device.\nError: ")
78                         else:
79                                 message = _("Unable to complete filesystem check.\nError: ")
80                         self.session.open(MessageBox, message + str(self.hdd.errorList[0 - result]), MessageBox.TYPE_ERROR)
81                 else:
82                         self.close()
83
84         def hddQuestion(self):
85                 if self.type == self.HARDDISK_INITIALIZE:
86                         message = _("Do you really want to initialize the device?\nAll data on the disk will be lost!")
87                 else:
88                         message = _("Do you really want to check the filesystem?\nThis could take lots of time!")
89                 self.session.openWithCallback(self.hddConfirmed, MessageBox, message)
90
91         def hddConfirmed(self, confirmed):
92                 if not confirmed:
93                         return
94
95                 print "this will start either the initialize or the fsck now!"
96                 self.session.openWithCallback(self.hddReady, HarddiskWait, self.hdd, self.type)
97
98 class HarddiskSelection(Screen):
99         def __init__(self, session):
100                 Screen.__init__(self, session)
101                 
102                 if harddiskmanager.HDDCount() == 0:
103                         tlist = []
104                         tlist.append((_("no storage devices found"), 0))
105                         self["hddlist"] = MenuList(tlist)
106                 else:                   
107                         self["hddlist"] = MenuList(harddiskmanager.HDDList())
108                 
109                 self["actions"] = ActionMap(["OkCancelActions"],
110                 {
111                         "ok": self.okbuttonClick ,
112                         "cancel": self.close
113                 })
114
115         def okbuttonClick(self):
116                 selection = self["hddlist"].getCurrent()
117                 if selection[1] != 0:
118                         self.session.open(HarddiskSetup, selection[1], HarddiskSetup.HARDDISK_INITIALIZE)
119
120 # This is actually just HarddiskSelection but with correct type
121 class HarddiskFsckSelection(HarddiskSelection):
122         def __init__(self, session):
123                 HarddiskSelection.__init__(self, session)
124                 self.skinName = "HarddiskSelection"
125
126         def okbuttonClick(self):
127                 selection = self["hddlist"].getCurrent()
128                 if selection[1] != 0:
129                         self.session.open(HarddiskSetup, selection[1], HarddiskSetup.HARDDISK_CHECK)