enigma2 (20120327 rel32 -> 20120430 master)
[enigma2.git] / usr / lib / enigma2 / python / Plugins / Extensions / DVDBurn / MediumToolbox.py
1 from Screens.Screen import Screen
2 from Components.ActionMap import ActionMap
3 from Components.Sources.StaticText import StaticText
4 from Components.Sources.Progress import Progress
5 from Components.Task import Task, Job, job_manager, Condition
6 from Components.ScrollLabel import ScrollLabel
7 from Components.Harddisk import harddiskmanager
8 from Components.Console import Console
9 from Plugins.SystemPlugins.Hotplug.plugin import hotplugNotifier
10
11 class MediumToolbox(Screen):
12         skin = """
13                 <screen name="MediumToolbox" position="center,center"  size="560,445" title="Medium toolbox" >
14                     <ePixmap pixmap="skin_default/buttons/red.png" position="0,0" size="140,40" alphatest="on" />
15                     <ePixmap pixmap="skin_default/buttons/green.png" position="140,0" size="140,40" alphatest="on" />
16                     <ePixmap pixmap="skin_default/buttons/yellow.png" position="280,0" size="140,40" alphatest="on" />
17                     <widget source="key_red" render="Label" position="0,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#9f1313" transparent="1" />
18                     <widget source="key_green" render="Label" position="140,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#1f771f" transparent="1" />
19                     <widget source="key_yellow" render="Label" position="280,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#a08500" transparent="1" />
20                     <widget source="info" render="Label" position="20,60" size="520,100" font="Regular;20" />
21                     <widget name="details" position="20,200" size="520,200" font="Regular;16" />
22                     <widget source="space_bar" render="Progress" position="10,410" size="540,26" borderWidth="1" backgroundColor="#254f7497" />
23                     <widget source="space_label" render="Label" position="20,414" size="520,22" zPosition="2" font="Regular;18" halign="center" transparent="1" foregroundColor="#000000" />
24                 </screen>"""
25
26         def __init__(self, session):
27                 Screen.__init__(self, session)
28                 
29                 self["key_red"] = StaticText(_("Exit"))
30                 self["key_green"] = StaticText(_("Update"))
31                 self["key_yellow"] = StaticText()
32                 
33                 self["space_label"] = StaticText()
34                 self["space_bar"] = Progress()
35                 
36                 self.mediuminfo = [ ]
37                 self.formattable = False
38                 self["details"] = ScrollLabel()
39                 self["info"] = StaticText()
40
41                 self["toolboxactions"] = ActionMap(["ColorActions", "MediumToolbox", "OkCancelActions"],
42                 {
43                     "red": self.exit,
44                     "green": self.update,
45                     "yellow": self.format,
46                     "cancel": self.exit,
47                     "pageUp": self.pageUp,
48                     "pageDown": self.pageDown
49                 })
50                 self.update()
51                 hotplugNotifier.append(self.update)
52                 self.onLayoutFinish.append(self.layoutFinished)
53
54         def layoutFinished(self):
55                 self.setTitle(_("Medium toolbox"))
56
57         def pageUp(self):
58                 self["details"].pageUp()
59
60         def pageDown(self):
61                 self["details"].pageDown()
62
63         def update(self, dev="", action=""):
64                 self["space_label"].text = _("Please wait... Loading list...")
65                 self["info"].text = ""
66                 self["details"].setText("")
67                 self.Console = Console()
68                 cmd = "dvd+rw-mediainfo /dev/" + harddiskmanager.getCD()
69                 self.Console.ePopen(cmd, self.mediainfoCB)
70
71         def format(self):
72                 if self.formattable:
73                         job = DVDformatJob(self)
74                         job_manager.AddJob(job)
75                         from Screens.TaskView import JobView
76                         self.session.openWithCallback(self.formatCB, JobView, job)
77         
78         def formatCB(self, in_background):
79                 self.update()
80
81         def mediainfoCB(self, mediuminfo, retval, extra_args):
82                 formatted_capacity = 0
83                 read_capacity = 0
84                 capacity = 0
85                 used = 0
86                 infotext = ""
87                 mediatype = ""
88                 for line in mediuminfo.splitlines():
89                         if line.find("Mounted Media:") > -1:
90                                 mediatype = line.rsplit(',',1)[1][1:]
91                                 if mediatype.find("RW") > 0 or mediatype.find("RAM") > 0 or mediatype.find("RE"):
92                                         self.formattable = True
93                                 else:
94                                         self.formattable = False
95                         elif line.find("Legacy lead-out at:") > -1:
96                                 used = int(line.rsplit('=',1)[1]) / 1048576.0
97                                 print "[dvd+rw-mediainfo] lead out used =", used
98                         elif line.find("formatted:") > -1:
99                                 formatted_capacity = int(line.rsplit('=',1)[1]) / 1048576.0
100                                 print "[dvd+rw-mediainfo] formatted capacity =", formatted_capacity
101                         elif formatted_capacity == 0 and line.find("READ CAPACITY:") > -1:
102                                 read_capacity = int(line.rsplit('=',1)[1]) / 1048576.0
103                                 print "[dvd+rw-mediainfo] READ CAPACITY =", read_capacity
104                 for line in mediuminfo.splitlines():
105                         if line.find("Free Blocks:") > -1:
106                                 try:
107                                         size = eval(line[14:].replace("KB","*1024"))
108                                 except:
109                                         size = 0
110                                 if size > 0:
111                                         capacity = size / 1048576
112                                         if used:
113                                                 used = capacity-used
114                                         print "[dvd+rw-mediainfo] free blocks capacity=%d, used=%d" % (capacity, used)
115                         elif line.find("Disc status:") > -1:
116                                 if line.find("blank") > -1:
117                                         print "[dvd+rw-mediainfo] Disc status blank capacity=%d, used=0" % (capacity)
118                                         capacity = used
119                                         used = 0
120                                 elif line.find("complete") > -1 and formatted_capacity == 0:
121                                         print "[dvd+rw-mediainfo] Disc status complete capacity=0, used=%d" % (capacity)
122                                         used = read_capacity
123                                         capacity = 1
124                                 else:
125                                         capacity = formatted_capacity
126                         infotext += line+'\n'
127                 if capacity and used > capacity:
128                         used = read_capacity or capacity
129                         capacity = formatted_capacity or capacity
130                 print "capacity", capacity, "used", used, "read_capacity", read_capacity, "formatted_capacity", formatted_capacity
131                 self["details"].setText(infotext)
132                 if self.formattable:
133                         self["key_yellow"].text = _("Format")
134                 else:
135                         self["key_yellow"].text = ""
136                 percent = 100 * used / (capacity or 1)
137                 if capacity > 9900:
138                         self["space_label"].text = "%d / %d MB" % (used, capacity) + " (%.2f%% " % percent + _("BLUDISC RECORDABLE") + ")"
139                         self["space_bar"].value = int(percent)
140                 elif capacity > 4600 and capacity < 9900:
141                         self["space_label"].text = "%d / %d MB" % (used, capacity) + " (%.2f%% " % percent + _("of a DUAL layer medium used.") + ")"
142                         self["space_bar"].value = int(percent)
143                 elif capacity > 1:
144                         self["space_label"].text = "%d / %d MB" % (used, capacity) + " (%.2f%% " % percent + _("of a SINGLE layer medium used.") + ")"
145                         self["space_bar"].value = int(percent)
146                 elif capacity == 1 and used > 0:
147                         self["space_label"].text = "%d MB " % (used) + _("on READ ONLY medium.")
148                         self["space_bar"].value = int(percent)
149                 else:
150                         self["space_label"].text = _("Medium is not a writeable DVD!")
151                         self["space_bar"].value = 0
152                 free = capacity-used
153                 if free < 2:
154                         free = 0
155                 self["info"].text = "Media-Type:\t\t%s\nFree capacity:\t\t%d MB" % (mediatype or "NO DVD", free)
156
157         def exit(self):
158                 del self.Console
159                 hotplugNotifier.remove(self.update)
160                 self.close()
161
162 class DVDformatJob(Job):
163         def __init__(self, toolbox):
164                 Job.__init__(self, _("Recordable media toolbox"))
165                 self.toolbox = toolbox
166                 DVDformatTask(self)
167                 
168         def retry(self):
169                 self.tasks[0].args += self.tasks[0].retryargs
170                 Job.retry(self)
171
172 class DVDformatTaskPostcondition(Condition):
173         RECOVERABLE = True
174         def check(self, task):
175                 return task.error is None
176
177         def getErrorMessage(self, task):
178                 return {
179                         task.ERROR_ALREADYFORMATTED: _("This DVD RW medium is already formatted - reformatting will erase all content on the disc."),
180                         task.ERROR_NOTWRITEABLE: _("Medium is not a writeable DVD!"),
181                         task.ERROR_UNKNOWN: _("An unknown error occured!")
182                 }[task.error]
183
184 class DVDformatTask(Task):
185         ERROR_ALREADYFORMATTED, ERROR_NOTWRITEABLE, ERROR_UNKNOWN = range(3)
186         def __init__(self, job, extra_args=[]):
187                 Task.__init__(self, job, ("RW medium format"))
188                 self.toolbox = job.toolbox
189                 self.postconditions.append(DVDformatTaskPostcondition())
190                 self.setTool("dvd+rw-format")
191                 self.args += [ "/dev/" + harddiskmanager.getCD() ]
192                 self.end = 1100
193                 self.retryargs = [ ]
194
195         def prepare(self):
196                 self.error = None
197
198         def processOutputLine(self, line):
199                 if line.startswith("- media is already formatted"):
200                         self.error = self.ERROR_ALREADYFORMATTED
201                         self.retryargs = [ "-force" ]
202                 #if line.startswith("- media is not blank") or 
203                 if line.startswith("  -format=full  to perform full (lengthy) reformat;"):
204                         self.error = self.ERROR_ALREADYFORMATTED
205                         self.retryargs = [ "-blank" ]
206                 elif line.startswith("                to eliminate or adjust Spare Area."):
207                         self.error = self.ERROR_ALREADYFORMATTED
208                         self.retryargs = [ "-ssa=default" ]     
209                 if line.startswith(":-( mounted media doesn't appear to be"):
210                         self.error = self.ERROR_NOTWRITEABLE
211
212         def processOutput(self, data):
213                 print "[DVDformatTask processOutput]  ", data
214                 if data.endswith('%'):
215                         data= data.replace('\x08','')
216                         self.progress = int(float(data[:-1])*10)
217                 else:
218                         Task.processOutput(self, data)