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