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