Fixing typos
[enigma2-plugins.git] / reconstructapsc / 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 import Screens.Standby
6 from Components.ActionMap import ActionMap
7 from enigma import eTimer, eServiceCenter, iServiceInformation, eConsoleAppContainer
8 from os import access, chmod, X_OK
9
10 recons_path = "/usr/lib/enigma2/python/Plugins/Extensions/ReconstructApSc/bin/reconstruct_apsc"
11 # Hack to make sure it is executable
12 if not access(recons_path, X_OK):
13         chmod(recons_path, 493)
14
15 def main(session, service, **kwargs):
16         session.open(ReconstructApSc, service, **kwargs)
17
18 def Plugins(**kwargs):
19         return PluginDescriptor(name="ReconstructApSc", description=_("Reconstruct AP/SC ..."), where = PluginDescriptor.WHERE_MOVIELIST, fnc=main)
20
21
22 class ReconstructApSc(ChoiceBox):
23         def __init__(self, session, service):
24                 self.service = service
25                 serviceHandler = eServiceCenter.getInstance()
26                 path = self.service.getPath()
27                 info = serviceHandler.info(self.service)
28                 if not info:
29                         self.name = path
30                 else:
31                         self.name = info.getName(self.service)
32                 tlist = [
33                         (_("Don't reconstruct"), "CALLFUNC", self.confirmed0),
34                         (_("Reconstruct the .ap and .sc files of the selected movie"), "CALLFUNC", self.confirmed1),
35                         (_("Reconstruct all missing .ap and .sc files in this directory"), "CALLFUNC", self.confirmed2),
36                         (_("Check any running reconstruct process"), "CALLFUNC", self.confirmed3),
37                 ]
38                 ChoiceBox.__init__(self, session, _("What would you like to reconstruct?  (\"%s\")") % (self.name), list = tlist, selection = 0)
39                 self.skinName = "ChoiceBox"
40
41         def confirmed0(self, arg):
42                 self.close()
43
44         def confirmed1(self, arg):
45                 ReconstructApScSpawn(self.session, self, [recons_path, self.service.getPath()], self.name, _("movie"))
46
47         def confirmed2(self, arg):
48                 dir = self.dirName(self.service.getPath())
49                 ReconstructApScSpawn(self.session, self, [recons_path, "-d", dir], dir, _("directory"))
50
51         def confirmed3(self, arg):
52                 output = global_recons_queue.checkOutput()
53                 if output == False:
54                         mess = "There is no running reconstruction process"
55                 else:
56                         mess = "Current reconstruction process output:\n%s" % output
57                 self.session.openWithCallback(self.close, MessageBox, mess, MessageBox.TYPE_INFO)
58
59         def dirName(self, str):
60                 return '/'.join(str.split('/')[:-1]) + '/'
61
62
63 class ReconstructApScQueue:
64         def __init__(self):
65                 self.container = eConsoleAppContainer()
66                 self.container.appClosed.append(self.runDone)
67                 self.container.dataAvail.append(self.collOutput)
68                 self.queue = []
69                 self.output = ""
70                 self.running = False
71
72         def enqueue(self, cb, cmd):
73                 self.queue.append((cb, cmd))
74                 if not self.running:
75                         self.runNext()
76                         return True
77                 else:
78                         return False
79
80         def collOutput(self, data):
81                 self.output += data
82
83         def checkOutput(self):
84                 if not self.running:
85                         return False
86                 else:
87                         return self.output
88
89         def runNext(self):
90                 self.output = ""
91                 if not self.queue:
92                         self.running = False
93                 else:
94                         self.running = True
95                         self.container.execute(*self.queue[0][1])
96
97         def runDone(self, retval):
98                 cb = self.queue[0][0]
99                 self.queue = self.queue[1:]
100                 cb(retval, self.output)
101                 self.runNext()
102
103 global_recons_errors = [_("The %s \"%s\" is successfully processed:\n%s"),
104                       _("Processing failed for the %s \"%s\":\n%s")]
105
106 global_recons_queue = ReconstructApScQueue()
107
108 global_recons_block = False
109
110 class ReconstructApScSpawn:
111         def __init__(self, session, parent, clist, name, typename):
112                 global global_recons_queue
113                 global global_recons_block
114                 self.session = session
115                 self.parent = parent
116                 self.name = name
117                 self.typename = typename
118                 self.clist = [clist[0]] + clist
119                 self.mess = ""
120                 self.dialog = False
121                 self.waitTimer = eTimer()
122                 self.waitTimer.callback.append(self.doWaitAck)
123                 if global_recons_queue.enqueue(self.doAck, self.clist):
124                         mess = _("The %s \"%s\" is processed in the background.") % (self.typename, self.name)
125                 else:
126                         mess = _("Another movie or directory is currently processed.\nThe %s \"%s\" will be processed in the background after it.") % (self.typename, self.name)
127                 global_recons_block = True
128                 self.dialog = self.session.openWithCallback(self.endc, MessageBox, mess, MessageBox.TYPE_INFO)
129
130         def doAck(self, retval, output):
131                 global global_recons_errors
132                 self.mess = global_recons_errors[retval] % (self.typename, self.name, output)
133                 self.doWaitAck()
134
135         def doWaitAck(self):
136                 global global_recons_block
137                 if Screens.Standby.inStandby or not self.session.in_exec or (global_recons_block and not self.dialog):
138                         self.waitTimer.start(2000, True)
139                 else:
140                         global_recons_block = True
141                         self.session.openWithCallback(self.endw, MessageBox, self.mess, MessageBox.TYPE_INFO)
142
143         def endw(self, arg = 0):
144                 global global_recons_block
145                 global_recons_block = False
146                 if self.session.current_dialog == self.dialog:
147                         self.session.current_dialog.close(True)
148                         self.endc(arg)
149
150         def endc(self, arg = 0):
151                 global global_recons_block
152                 global_recons_block = False
153                 self.dialog = False
154                 self.parent.close()