fixes bug #593
[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 Components.SystemInfo import SystemInfo
5 from InfoBarGenerics import InfoBarNotifications
6 import Screens.Standby
7 from Tools import Notifications
8
9 class JobView(InfoBarNotifications, Screen, ConfigListScreen):
10         def __init__(self, session, job, parent=None, cancelable = True, backgroundable = True, afterEventChangeable = True):
11                 from Components.Sources.StaticText import StaticText
12                 from Components.Sources.Progress import Progress
13                 from Components.Sources.Boolean import Boolean
14                 from Components.ActionMap import ActionMap
15                 Screen.__init__(self, session, parent)
16                 InfoBarNotifications.__init__(self)
17                 ConfigListScreen.__init__(self, [])
18                 self.parent = parent
19                 self.job = job
20
21                 self["job_name"] = StaticText(job.name)
22                 self["job_progress"] = Progress()
23                 self["job_task"] = StaticText()
24                 self["summary_job_name"] = StaticText(job.name)
25                 self["summary_job_progress"] = Progress()
26                 self["summary_job_task"] = StaticText()
27                 self["job_status"] = StaticText()
28                 self["finished"] = Boolean()
29                 self["cancelable"] = Boolean(cancelable)
30                 self["backgroundable"] = Boolean(backgroundable)
31
32                 self["key_blue"] = StaticText(_("Background"))
33
34                 self.onShow.append(self.windowShow)
35                 self.onHide.append(self.windowHide)
36
37                 self["setupActions"] = ActionMap(["ColorActions", "SetupActions"],
38                 {
39                     "green": self.ok,
40                     "red": self.abort,
41                     "blue": self.background,
42                     "cancel": self.ok,
43                     "ok": self.ok,
44                 }, -2)
45
46                 self.settings = ConfigSubsection()
47                 if SystemInfo["DeepstandbySupport"]:
48                         shutdownString = _("go to deep standby")
49                 else:
50                         shutdownString = _("shut down")
51                 self.settings.afterEvent = ConfigSelection(choices = [("nothing", _("do nothing")), ("close", _("Close")), ("standby", _("go to standby")), ("deepstandby", shutdownString)], default = self.job.afterEvent or "nothing")
52                 self.job.afterEvent = self.settings.afterEvent.getValue()
53                 self.afterEventChangeable = afterEventChangeable
54                 self.setupList()
55                 self.state_changed()
56
57         def setupList(self):
58                 if self.afterEventChangeable:
59                         self["config"].setList( [ getConfigListEntry(_("After event"), self.settings.afterEvent) ])
60                 else:
61                         self["config"].hide()
62                 self.job.afterEvent = self.settings.afterEvent.getValue()
63
64         def keyLeft(self):
65                 ConfigListScreen.keyLeft(self)
66                 self.setupList()
67
68         def keyRight(self):
69                 ConfigListScreen.keyRight(self)
70                 self.setupList()
71         
72         def windowShow(self):
73                 self.job.state_changed.append(self.state_changed)
74
75         def windowHide(self):
76                 if len(self.job.state_changed) > 0:
77                     self.job.state_changed.remove(self.state_changed)
78
79         def state_changed(self):
80                 j = self.job
81                 self["job_progress"].range = j.end
82                 self["summary_job_progress"].range = j.end
83                 self["job_progress"].value = j.progress
84                 self["summary_job_progress"].value = j.progress
85                 #print "JobView::state_changed:", j.end, j.progress
86                 self["job_status"].text = j.getStatustext()
87                 if j.status == j.IN_PROGRESS:
88                         self["job_task"].text = j.tasks[j.current_task].name
89                         self["summary_job_task"].text = j.tasks[j.current_task].name
90                 else:
91                         self["job_task"].text = ""
92                         self["summary_job_task"].text = j.getStatustext()
93                 if j.status in (j.FINISHED, j.FAILED):
94                         self.performAfterEvent()
95                         self["backgroundable"].boolean = False
96                         if j.status == j.FINISHED:
97                                 self["finished"].boolean = True
98                                 self["cancelable"].boolean = False
99                         elif j.status == j.FAILED:
100                                 self["cancelable"].boolean = True
101
102         def background(self):
103                 if self["backgroundable"].boolean == True:
104                         self.close(True)
105
106         def ok(self):
107                 if self.job.status in (self.job.FINISHED, self.job.FAILED):
108                         self.close(False)
109
110         def abort(self):
111                 if self.job.status in (self.job.FINISHED, self.job.FAILED):
112                         self.close(False)
113                 if self["cancelable"].boolean == True:
114                         self.job.cancel()
115
116         def performAfterEvent(self):
117                 self["config"].hide()
118                 if self.settings.afterEvent.getValue() == "nothing":
119                         return
120                 elif self.settings.afterEvent.getValue() == "close" and self.job.status == self.job.FINISHED:
121                         self.close(False)
122                 from Screens.MessageBox import MessageBox
123                 if self.settings.afterEvent.getValue() == "deepstandby":
124                         if not Screens.Standby.inTryQuitMainloop:
125                                 Notifications.AddNotificationWithCallback(self.sendTryQuitMainloopNotification, MessageBox, _("A sleep timer wants to shut down\nyour Dreambox. Shutdown now?"), timeout = 20)
126                 elif self.settings.afterEvent.getValue() == "standby":
127                         if not Screens.Standby.inStandby:
128                                 Notifications.AddNotificationWithCallback(self.sendStandbyNotification, MessageBox, _("A sleep timer wants to set your\nDreambox to standby. Do that now?"), timeout = 20)
129
130         def checkNotifications(self):
131                 InfoBarNotifications.checkNotifications(self)
132                 if Notifications.notifications == []:
133                         if self.settings.afterEvent.getValue() == "close" and self.job.status == self.job.FAILED:
134                                 self.close(False)
135                 
136         def sendStandbyNotification(self, answer):
137                 if answer:
138                         Notifications.AddNotification(Screens.Standby.Standby)
139
140         def sendTryQuitMainloopNotification(self, answer):
141                 if answer:
142                         Notifications.AddNotification(Screens.Standby.TryQuitMainloop, 1)