patch by Pieter Grimmerink: add closeOnSuccess
[enigma2.git] / lib / python / Screens / Console.py
1 from enigma import eConsoleAppContainer
2 from Screens.Screen import Screen
3 from Components.ActionMap import ActionMap
4 from Components.ScrollLabel import ScrollLabel
5
6 class Console(Screen):
7         #TODO move this to skin.xml
8         skin = """
9                 <screen position="100,100" size="550,400" title="Command execution..." >
10                         <widget name="text" position="0,0" size="550,400" font="Regular;15" />
11                 </screen>"""
12                 
13         def __init__(self, session, title = "Console", cmdlist = None, finishedCallback = None, closeOnSuccess = False):
14                 self.skin = Console.skin
15                 Screen.__init__(self, session)
16
17                 self.finishedCallback = finishedCallback
18                 self.closeOnSuccess = closeOnSuccess
19
20                 self["text"] = ScrollLabel("")
21                 self["actions"] = ActionMap(["WizardActions", "DirectionActions"], 
22                 {
23                         "ok": self.cancel,
24                         "back": self.cancel,
25                         "up": self["text"].pageUp,
26                         "down": self["text"].pageDown
27                 }, -1)
28                 
29                 self.cmdlist = cmdlist
30                 self.newtitle = title
31                 
32                 self.onShown.append(self.updateTitle)
33                 
34                 self.container = eConsoleAppContainer()
35                 self.run = 0
36                 self.container.appClosed.get().append(self.runFinished)
37                 self.container.dataAvail.get().append(self.dataAvail)
38                 self.onLayoutFinish.append(self.startRun) # dont start before gui is finished
39
40         def updateTitle(self):
41                 self.setTitle(self.newtitle)
42
43         def startRun(self):
44                 self["text"].setText(_("Execution Progress:") + "\n\n")
45                 print "Console: executing in run", self.run, " the command:", self.cmdlist[self.run]
46                 self.container.execute(self.cmdlist[self.run])
47
48         def runFinished(self, retval):
49                 self.run += 1
50                 if self.run != len(self.cmdlist):
51                         self.container.execute(self.cmdlist[self.run])
52                 else:
53                         str = self["text"].getText()
54                         str += _("Execution finished!!");
55                         self["text"].setText(str)
56                         if self.finishedCallback is not None:
57                                 self.finishedCallback()
58                         if not retval and self.closeOnSuccess:
59                                 self.cancel()
60
61         def cancel(self):
62                 if self.run == len(self.cmdlist):
63                         self.close()
64
65         def dataAvail(self, str):
66                 self["text"].setText(self["text"].getText() + str)