Allow going into standby/shutting down after completing jobs (dvd burning)
[enigma2.git] / lib / python / Screens / TaskView.py
1 from Screen import Screen
2 from Components.ConfigList import ConfigListScreen
3 from Components.config import config, ConfigSubsection, ConfigSelection, getConfigListEntry
4 from InfoBarGenerics import InfoBarNotifications
5 import Screens.Standby
6 from Tools import Notifications
7
8 class JobView(InfoBarNotifications, Screen, ConfigListScreen):
9         def __init__(self, session, job, parent=None, cancelable = True, backgroundable = True, afterEvent = 0):
10                 from Components.Sources.StaticText import StaticText
11                 from Components.Sources.Progress import Progress
12                 from Components.Sources.Boolean import Boolean
13                 from Components.ActionMap import ActionMap
14                 Screen.__init__(self, session, parent)
15                 InfoBarNotifications.__init__(self)
16                 self.parent = parent
17                 self.job = job
18                 self.job.taskview = self
19
20                 self["job_name"] = StaticText(job.name)
21                 self["job_progress"] = Progress()
22                 self["job_status"] = StaticText()
23                 self["job_task"] = StaticText()
24                 self["finished"] = Boolean()
25                 self["cancelable"] = Boolean(cancelable)
26                 self["backgroundable"] = Boolean(backgroundable)
27
28                 self["key_blue"] = StaticText(_("Background"))
29
30                 self.onShow.append(self.windowShow)
31                 self.onHide.append(self.windowHide)
32
33                 self["actions"] = ActionMap(["OkCancelActions"], 
34                         {
35                                 "ok": self.ok,
36                                 "cancel": self.ok
37                         })
38                 self["ColorActions"] = ActionMap(["ColorActions"],
39                         {
40                                 "red": self.abort,
41                                 "green": self.ok,
42                                 "blue": self.background,
43                         })
44                 ConfigListScreen.__init__(self, [])
45                 self.afterevents = [ "nothing", "standby", "deepstandby", "close" ]
46                 self.settings = ConfigSubsection()
47                 self.settings.afterEvent = ConfigSelection(choices = [("nothing", _("do nothing")), ("close", _("Close")), ("standby", _("go to standby")), ("deepstandby", _("go to deep standby"))], default = self.afterevents[afterEvent])
48                 self.setupList()
49                 self.state_changed()
50
51         def setupList(self):
52                 self["config"].setList( [ getConfigListEntry(_("After event"), self.settings.afterEvent) ])
53                 
54         def keyLeft(self):
55                 ConfigListScreen.keyLeft(self)
56                 self.setupList()
57
58         def keyRight(self):
59                 ConfigListScreen.keyRight(self)
60                 self.setupList()
61         
62         def windowShow(self):
63                 self.job.state_changed.append(self.state_changed)
64
65         def windowHide(self):
66                 if len(self.job.state_changed) > 0:
67                     self.job.state_changed.remove(self.state_changed)
68
69         def state_changed(self):
70                 j = self.job
71                 self["job_progress"].range = j.end
72                 self["job_progress"].value = j.progress
73                 #print "JobView::state_changed:", j.end, j.progress
74                 self["job_status"].text = {j.NOT_STARTED: _("Waiting"), j.IN_PROGRESS: _("In Progress"), j.FINISHED: _("Finished"), j.FAILED: _("Failed")}[j.status]
75                 if j.status == j.IN_PROGRESS:
76                         self["job_task"].text = j.tasks[j.current_task].name
77                 else:
78                         self["job_task"].text = ""
79                 if j.status in [j.FINISHED, j.FAILED]:
80                         self.performAfterEvent()
81                         self["backgroundable"].boolean = False
82                         if j.status == j.FINISHED:
83                                 self["finished"].boolean = True
84                                 self["cancelable"].boolean = False
85                         elif j.status == j.FAILED:
86                                 self["cancelable"].boolean = True
87
88         def background(self):
89                 if self["backgroundable"].boolean == True:
90                         self.close(True)
91
92         def ok(self):
93                 if self.job.status in [self.job.FINISHED, self.job.FAILED]:
94                         self.close(False)
95
96         def abort(self):
97                 if self.job.status in [self.job.FINISHED, self.job.FAILED]:
98                         self.close(False)
99                 if self["cancelable"].boolean == True:
100                         self.job.cancel()
101
102         def performAfterEvent(self):
103                 if self.settings.afterEvent.getValue() == "nothing":
104                         return
105                 elif self.settings.afterEvent.getValue() == "close":
106                         self.abort()
107                 from Screens.MessageBox import MessageBox
108                 if self.settings.afterEvent.getValue() == "deepstandby":
109                         if not Screens.Standby.inTryQuitMainloop:
110                                 Notifications.AddNotificationWithCallback(self.sendTryQuitMainloopNotification, MessageBox, _("A sleep timer wants to shut down\nyour Dreambox. Shutdown now?"), timeout = 20)
111                 elif self.settings.afterEvent.getValue() == "standby":
112                         if not Screens.Standby.inStandby:
113                                 Notifications.AddNotificationWithCallback(self.sendStandbyNotification, MessageBox, _("A sleep timer wants to set your\nDreambox to standby. Do that now?"), timeout = 20)
114
115         def sendStandbyNotification(self, answer):
116                 if answer:
117                         Notifications.AddNotification(Screens.Standby.Standby)
118
119         def sendTryQuitMainloopNotification(self, answer):
120                 if answer:
121                         Notifications.AddNotification(Screens.Standby.TryQuitMainloop, 1)