Make sure the mcut program get executable after downloading he plugin
[enigma2-plugins.git] / moviecut / src_py / plugin.py
1 from Plugins.Plugin import PluginDescriptor
2 from Screens.Screen import Screen
3 from Screens.MessageBox import MessageBox
4 from Screens.ChoiceBox import ChoiceBox
5 from Screens.LocationBox import MovieLocationBox
6 import Screens.Standby
7 from Components.config import config, ConfigText, ConfigSelection, ConfigNothing, getConfigListEntry
8 from Components.ActionMap import ActionMap
9 from Components.ConfigList import ConfigList, ConfigListScreen
10 from Components.Sources.StaticText import StaticText
11 from enigma import eTimer, eServiceCenter, iServiceInformation, eConsoleAppContainer
12 from os import access, chmod, X_OK
13
14 def main(session, service, **kwargs):
15         # Hack to make sure it is executable
16         mcut_path = "/usr/lib/enigma2/python/Plugins/Extensions/MovieCut/bin/mcut"
17         if not access(mcut_path, X_OK):
18                 chmod(mcut_path, 493)
19         session.open(MovieCut, service, **kwargs)
20
21 def Plugins(**kwargs):
22         return PluginDescriptor(name="MovieCut", description=_("Execute cuts..."), where = PluginDescriptor.WHERE_MOVIELIST, fnc=main)
23
24
25 class MovieCut(ChoiceBox):
26         def __init__(self, session, service):
27                 self.service = service
28                 serviceHandler = eServiceCenter.getInstance()
29                 path = self.service.getPath()
30                 info = serviceHandler.info(self.service)
31                 if not info:
32                         self.name = path
33                 else:
34                         self.name = info.getName(self.service)
35                 tlist = [
36                         (_("Don't cut"), "CALLFUNC", self.confirmed0),
37                         (_("Replace the original movie with the cut movie"), "CALLFUNC", self.confirmed1),
38                         (_("Place the cut movie in a new file ending with \" cut\""), "CALLFUNC", self.confirmed2),
39                         (_("Advanced cut specification..."), "CALLFUNC", self.confirmed3),
40                 ]
41                 ChoiceBox.__init__(self, session, _("How would you like to cut \"%s\"?") % (self.name), list = tlist, selection = 0)
42                 self.skinName = "ChoiceBox"
43
44         def confirmed0(self, arg):
45                 self.close()
46
47         def confirmed1(self, arg):
48                 MovieCutSpawn(self.session, self, [mcut_path, "-r", self.service.getPath()], self.name)
49
50         def confirmed2(self, arg):
51                 MovieCutSpawn(self.session, self, [mcut_path, self.service.getPath()], self.name)
52
53         def confirmed3(self, arg):
54                 serviceHandler = eServiceCenter.getInstance()
55                 info = serviceHandler.info(self.service)
56                 path = self.service.getPath()
57                 self.name = info.getName(self.service)
58                 descr = info.getInfoString(self.service, iServiceInformation.sDescription)
59                 self.session.openWithCallback(self.advcutConfirmed, AdvancedCutInput, self.name, path, descr)
60
61         def advcutConfirmed(self, ret):
62                 if len(ret) <= 1 or not ret[0]:
63                         self.close()
64                         return
65                 clist = [mcut_path]
66                 if ret[1] == True:
67                         clist.append("-r")
68                 clist.append(self.service.getPath())
69                 if ret[2] != False:
70                         clist += ["-o", ret[2]]
71                 if ret[3] != False:
72                         clist += ["-n", ret[3]]
73                 if ret[4] != False:
74                         clist += ["-d", ret[4]]
75                 if ret[5] != False:
76                         clist.append("-c")
77                         clist += ret[5]
78                 MovieCutSpawn(self.session, self, clist, self.name)
79                 
80 class AdvancedCutInput(Screen, ConfigListScreen):
81         def __init__(self, session, name, path, descr):
82                 Screen.__init__(self, session)
83                 self.skinName = [ "AdvancedCutInput", "Setup" ]
84
85                 self["key_green"] = StaticText(_("OK"))
86                 self["key_red"] = StaticText(_("Cancel"))
87
88                 if self.baseName(path) == self.baseName(name):
89                         title = ""
90                 else:
91                         title = name
92                 dir = self.dirName(path)
93                 file = self.baseName(path) + " cut"
94                 self.input_replace = ConfigSelection(choices = [("no", _("No")), ("yes", _("Yes"))], default = "no")
95                 self.input_file = ConfigText(default = file, fixed_size = False, visible_width = 45)
96                 self.input_title = ConfigText(default = title, fixed_size = False, visible_width = 45)
97                 self.input_descr = ConfigText(default = descr, fixed_size = False, visible_width = 45)
98                 tmp = config.movielist.videodirs.value
99                 if not dir in tmp:
100                         tmp.append(dir)
101                 self.input_dir = ConfigSelection(choices = tmp, default = dir)
102                 self.input_manual = ConfigSelection(choices = [("no", _("Cutlist")), ("yes", _("Manual specification"))], default = "no")
103                 self.input_space = ConfigNothing()
104                 self.input_manualcuts = ConfigText(default = "", fixed_size = False)
105                 self.input_manualcuts.setUseableChars(" 0123456789:.")
106
107                 self["actions"] = ActionMap(["SetupActions"],
108                 {
109                         "ok": self.keySelectOrGo,
110                         "save": self.keyGo,
111                         "cancel": self.keyCancel,
112                 }, -2)
113
114                 self.list = []
115                 ConfigListScreen.__init__(self, self.list)
116                 self.entry_replace = getConfigListEntry(_("Replace original:"), self.input_replace)
117                 self.entry_file = getConfigListEntry(_("New filename:"), self.input_file)
118                 self.entry_title = getConfigListEntry(_("New title:"), self.input_title)
119                 self.entry_descr = getConfigListEntry(_("New description:"), self.input_descr)
120                 self.entry_dir = getConfigListEntry(_("New location:"), self.input_dir)
121                 self.entry_manual = getConfigListEntry(_("Cut source:"), self.input_manual)
122                 self.entry_space = getConfigListEntry(_("Cuts (an IN OUT IN OUT ... sequence of hour:min:sec)"), self.input_space)
123                 self.entry_manualcuts = getConfigListEntry(":", self.input_manualcuts)
124                 self.createSetup(self["config"])
125
126                 self.onLayoutFinish.append(self.layoutFinished)
127
128         def layoutFinished(self):
129                 self.setTitle(_("Cut Parameter Input"))
130
131         def createSetup(self, configlist):
132                 list = [
133                         self.entry_replace
134                 ]
135                 if self.input_replace.value == "no":
136                         list.extend((
137                                 self.entry_file,
138                                 self.entry_dir,
139                         ))
140                 list.extend((
141                         self.entry_title,
142                         self.entry_descr,
143                         self.entry_manual,
144                 ))
145                 if self.input_manual.value == "yes":
146                         list.extend((
147                                 self.entry_space,
148                                 self.entry_manualcuts,
149                         ))
150                 self.list = list
151                 configlist.list = list
152                 configlist.l.setList(list)
153
154         def keyLeft(self):
155                 ConfigListScreen.keyLeft(self)
156                 cc = self["config"].getCurrent()
157                 if cc is self.entry_replace or cc is self.entry_manual:
158                         self.createSetup(self["config"])
159
160         def keyRight(self):
161                 ConfigListScreen.keyRight(self)
162                 cc = self["config"].getCurrent()
163                 if cc is self.entry_replace or cc is self.entry_manual:
164                         self.createSetup(self["config"])
165
166         def pathSelected(self, res):
167                 if res is not None:
168                         if config.movielist.videodirs.value != self.input_dir.choices:
169                                 self.input_dir.setChoices(config.movielist.videodirs.value, default=res)
170                         self.input_dir.value = res
171
172         def keySelectOrGo(self):
173                 if self["config"].getCurrent() == self.entry_dir:
174                         self.session.openWithCallback(
175                                 self.pathSelected,
176                                 MovieLocationBox,
177                                 _("Choose target folder"),
178                                 self.input_dir.value,
179                         )
180                 else:
181                         self.keyGo()
182
183         def keyGo(self):
184                 if self.input_replace.value == "yes":
185                         path = False
186                 else:
187                         path = self.rejoinName(self.input_dir.value, self.input_file.value)
188                 if self.input_manual.value == "no":
189                         cuts = False
190                 else:
191                         cuts = self.input_manualcuts.value.split(' ')
192                         while "" in cuts:
193                                 cuts.remove("")
194                 self.close((True, self.input_replace.value, path, self.input_title.value, self.input_descr.value, cuts))
195
196         def keyCancel(self):
197                 self.close((False,))
198
199         def baseName(self, str):
200                 name = str.split('/')[-1]
201                 if name.endswith(".ts") is True:
202                         return name[:-3]
203                 else:
204                         return name
205
206         def dirName(self, str):
207                 return '/'.join(str.split('/')[:-1]) + '/'
208
209         def rejoinName(self, dir, name):
210                 name = name.strip()
211                 if name.endswith(".ts") is True:
212                         return dir + name[:-3]
213                 else:
214                         return dir + name
215
216 class MovieCutQueue:
217         def __init__(self):
218                 self.container = eConsoleAppContainer()
219                 self.container.appClosed.append(self.runDone)
220                 self.queue = []
221                 self.running = False
222
223         def enqueue(self, cb, cmd):
224                 self.queue.append((cb, cmd))
225                 if not self.running:
226                         self.running = True
227                         self.runNext()
228                         return True
229                 else:
230                         return False
231
232         def runNext(self):
233                 if not self.queue:
234                         self.running = False
235                 else:
236                         self.container.execute(*self.queue[0][1])
237
238         def runDone(self, retval):
239                 cb = self.queue[0][0]
240                 self.queue = self.queue[1:]
241                 cb(retval)
242                 self.runNext()
243
244 global_mcut_errors = [_("The movie \"%s\" is successfully cut"),
245                       _("Cutting failed for movie \"%s\"")+":\n"+_("Bad arguments"),
246                       _("Cutting failed for movie \"%s\"")+":\n"+_("Couldn't open input .ts file"),
247                       _("Cutting failed for movie \"%s\"")+":\n"+_("Couldn't open input .cuts file"),
248                       _("Cutting failed for movie \"%s\"")+":\n"+_("Couldn't open input .ap file"),
249                       _("Cutting failed for movie \"%s\"")+":\n"+_("Couldn't open output .ts file"),
250                       _("Cutting failed for movie \"%s\"")+":\n"+_("Couldn't open output .cuts file"),
251                       _("Cutting failed for movie \"%s\"")+":\n"+_("Couldn't open output .ap file"),
252                       _("Cutting failed for movie \"%s\"")+":\n"+_("Empty .ap file"),
253                       _("Cutting failed for movie \"%s\"")+":\n"+_("No cuts specified"),
254                       _("Cutting failed for movie \"%s\"")+":\n"+_("Read/write error (disk full?)"),
255                       _("Cutting was aborted for movie \"%s\"")]
256
257 global_mcut_queue = MovieCutQueue()
258
259 global_mcut_block = False
260
261 class MovieCutSpawn:
262         def __init__(self, session, parent, clist, name):
263                 global global_mcut_queue
264                 global global_mcut_block
265                 self.session = session
266                 self.parent = parent
267                 self.name = name
268                 self.clist = [clist[0]] + clist
269                 self.mess = ""
270                 self.dialog = False
271                 self.waitTimer = eTimer()
272                 self.waitTimer.callback.append(self.doWaitAck)
273                 if global_mcut_queue.enqueue(self.doAck, self.clist):
274                         mess = _("The movie \"%s\" is cut in the background.") % (self.name)
275                 else:
276                         mess = _("Another movie is currently cut.\nThe movie \"%s\" will be cut in the background after it.") % (self.name)
277                 global_mcut_block = True
278                 self.dialog = self.session.openWithCallback(self.endc, MessageBox, mess, MessageBox.TYPE_INFO)
279
280         def doAck(self, retval):
281                 global global_mcut_errors
282 #               if WIFEXITED(retval):
283 #                       self.mess = global_mcut_errors[WEXITSTATUS(retval)] % (self.name)
284 #               else:
285 #                       self.mess = global_mcut_errors[-1] % (self.name)
286                 self.mess = global_mcut_errors[retval] % (self.name)
287                 self.doWaitAck()
288
289         def doWaitAck(self):
290                 global global_mcut_block
291                 if Screens.Standby.inStandby or not self.session.in_exec or (global_mcut_block and not self.dialog):
292                         self.waitTimer.start(2000, True)
293                 else:
294                         global_mcut_block = True
295                         self.session.openWithCallback(self.endw, MessageBox, self.mess, MessageBox.TYPE_INFO)
296
297         def endw(self, arg = 0):
298                 global global_mcut_block
299                 global_mcut_block = False
300                 if self.session.current_dialog == self.dialog:
301                         self.session.current_dialog.close(True)
302                         self.endc(arg)
303
304         def endc(self, arg = 0):
305                 global global_mcut_block
306                 global_mcut_block = False
307                 self.dialog = False
308                 self.parent.close()
309 #               self.session.current_dialog.close()