1453c05f338de2a28e142bbde4f7d80afd7738fb
[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                 ConfigListScreen.__init__(self, [])
17                 self.parent = parent
18                 self.job = job
19
20                 self["job_name"] = StaticText(job.name)
21                 self["job_progress"] = Progress()
22                 self["job_task"] = StaticText()
23                 self["summary_job_name"] = StaticText(job.name)
24                 self["summary_job_progress"] = Progress()
25                 self["summary_job_task"] = StaticText()
26                 self["job_status"] = StaticText()
27                 self["finished"] = Boolean()
28                 self["cancelable"] = Boolean(cancelable)
29                 self["backgroundable"] = Boolean(backgroundable)
30
31                 self["key_blue"] = StaticText(_("Background"))
32
33                 self.onShow.append(self.windowShow)
34                 self.onHide.append(self.windowHide)
35
36                 self["setupActions"] = ActionMap(["ColorActions", "SetupActions"],
37                 {
38                     "green": self.ok,
39                     "red": self.abort,
40                     "blue": self.background,
41                     "cancel": self.ok,
42                     "ok": self.ok,
43                 }, -2)
44
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["summary_job_progress"].range = j.end
73                 self["job_progress"].value = j.progress
74                 self["summary_job_progress"].value = j.progress
75                 #print "JobView::state_changed:", j.end, j.progress
76                 self["job_status"].text = j.getStatustext()
77                 if j.status == j.IN_PROGRESS:
78                         self["job_task"].text = j.tasks[j.current_task].name
79                         self["summary_job_task"].text = j.tasks[j.current_task].name
80                 else:
81                         self["job_task"].text = ""
82                         self["summary_job_task"].text = j.getStatustext()
83                 if j.status in (j.FINISHED, j.FAILED):
84                         self.performAfterEvent()
85                         self["backgroundable"].boolean = False
86                         if j.status == j.FINISHED:
87                                 self["finished"].boolean = True
88                                 self["cancelable"].boolean = False
89                         elif j.status == j.FAILED:
90                                 self["cancelable"].boolean = True
91
92         def background(self):
93                 if self["backgroundable"].boolean == True:
94                         self.close(True)
95
96         def ok(self):
97                 if self.job.status in (self.job.FINISHED, self.job.FAILED):
98                         self.close(False)
99
100         def abort(self):
101                 if self.job.status in (self.job.FINISHED, self.job.FAILED):
102                         self.close(False)
103                 if self["cancelable"].boolean == True:
104                         self.job.cancel()
105
106         def performAfterEvent(self):
107                 self["config"].hide()
108                 if self.settings.afterEvent.getValue() == "nothing":
109                         return
110                 elif self.settings.afterEvent.getValue() == "close":
111                         self.abort()
112                 from Screens.MessageBox import MessageBox
113                 if self.settings.afterEvent.getValue() == "deepstandby":
114                         if not Screens.Standby.inTryQuitMainloop:
115                                 Notifications.AddNotificationWithCallback(self.sendTryQuitMainloopNotification, MessageBox, _("A sleep timer wants to shut down\nyour Dreambox. Shutdown now?"), timeout = 20)
116                 elif self.settings.afterEvent.getValue() == "standby":
117                         if not Screens.Standby.inStandby:
118                                 Notifications.AddNotificationWithCallback(self.sendStandbyNotification, MessageBox, _("A sleep timer wants to set your\nDreambox to standby. Do that now?"), timeout = 20)
119
120         def sendStandbyNotification(self, answer):
121                 if answer:
122                         Notifications.AddNotification(Screens.Standby.Standby)
123
124         def sendTryQuitMainloopNotification(self, answer):
125                 if answer:
126                         Notifications.AddNotification(Screens.Standby.TryQuitMainloop, 1)