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