call closed without parameters is also valid
[enigma2.git] / lib / python / Screens / EpgSelection.py
1 from Screen import Screen
2 from Components.Button import Button
3 from Components.Pixmap import Pixmap
4 from Components.Label import Label
5 from Components.EpgList import *
6 from Components.ActionMap import ActionMap
7 from Components.ScrollLabel import ScrollLabel
8 from Screens.EventView import EventViewSimple
9 from enigma import eServiceReference, eServiceEventPtr
10 from Screens.FixedMenu import FixedMenu
11 from RecordTimer import RecordTimerEntry, parseEvent
12 from TimerEdit import TimerEditList
13 from TimerEntry import TimerEntry
14 from ServiceReference import ServiceReference
15 from Components.config import config, currentConfigSelectionElement
16 from time import localtime
17
18 import xml.dom.minidom
19
20 class EPGSelection(Screen):
21         def __init__(self, session, service, zapFunc=None, eventid=None):
22                 Screen.__init__(self, session)
23                 self["key_red"] = Button("")
24                 self.closeRecursive = False
25                 if isinstance(service, str) and eventid != None:
26                         self.type = EPG_TYPE_SIMILAR
27                         self["key_yellow"] = Button()
28                         self["key_blue"] = Button()
29                         self["key_red"] = Button()
30                         self.currentService=service
31                         self.eventid = eventid
32                 elif isinstance(service, eServiceReference) or isinstance(service, str):
33                         self.type = EPG_TYPE_SINGLE
34                         self["key_yellow"] = Button()
35                         self["key_blue"] = Button()
36                         self.currentService=ServiceReference(service)
37                 else:
38                         self.skinName = "EPGSelectionMulti"
39                         self.type = EPG_TYPE_MULTI
40                         self["key_yellow"] = Button(_("Prev"))
41                         self["key_blue"] = Button(_("Next"))
42                         self["now_button"] = Pixmap()
43                         self["next_button"] = Pixmap()
44                         self["more_button"] = Pixmap()
45                         self["now_button_sel"] = Pixmap()
46                         self["next_button_sel"] = Pixmap()
47                         self["more_button_sel"] = Pixmap()
48                         self["now_text"] = Label()
49                         self["next_text"] = Label()
50                         self["more_text"] = Label()
51                         self["date"] = Label()
52                         self.services = service
53                         self.zapFunc = zapFunc
54
55                 self["key_green"] = Button(_("Add timer"))
56                 self["list"] = EPGList(type = self.type, selChangedCB = self.onSelectionChanged, timer = self.session.nav.RecordTimer)
57
58                 class ChannelActionMap(ActionMap):
59                         def action(self, contexts, action):
60                                 return ActionMap.action(self, contexts, action)
61
62                 self["actions"] = ChannelActionMap(["EPGSelectActions", "OkCancelActions"],
63                         {
64                                 "cancel": self.closeScreen,
65                                 "ok": self.eventSelected,
66                                 "timerAdd": self.timerAdd,
67                                 "yellow": self.yellowButtonPressed,
68                                 "blue": self.blueButtonPressed,
69                                 "info": self.infoKeyPressed,
70                                 "zapTo": self.zapTo
71                         })
72                 self["actions"].csel = self
73
74                 self.onLayoutFinish.append(self.onCreate)
75
76         def closeScreen(self):
77                 self.close(self.closeRecursive)
78
79         def infoKeyPressed(self):
80                 if self.type == EPG_TYPE_MULTI or self.type == EPG_TYPE_SIMILAR:
81                         cur = self["list"].getCurrent()
82                         event = cur[0]
83                         service = cur[1]
84                 else:
85                         event = self["list"].getCurrent()
86                         service = self.currentService
87                 if event is not None:
88                         if self.type != EPG_TYPE_SIMILAR:
89                                 self.session.open(EventViewSimple, event, service, self.eventViewCallback, self.openSimilarList)
90                         else:
91                                 self.session.open(EventViewSimple, event, service, self.eventViewCallback)
92
93         def openSimilarList(self, eventid, refstr):
94                 self.session.open(EPGSelection, refstr, None, eventid)
95
96         #just used in multipeg
97         def onCreate(self):
98                 l = self["list"]
99                 if self.type == EPG_TYPE_MULTI:
100                         l.recalcEntrySize()
101                         l.fillMultiEPG(self.services)
102                 elif self.type == EPG_TYPE_SINGLE:
103                         if SINGLE_CPP == 0:
104                                 l.recalcEntrySize()
105                         l.fillSingleEPG(self.currentService)
106                 else:
107                         l.recalcEntrySize()
108                         l.fillSimilarList(self.currentService, self.eventid)
109
110         def eventViewCallback(self, setEvent, setService, val):
111                 l = self["list"]
112                 old = l.getCurrent()
113                 if val == -1:
114                         self.moveUp()
115                 elif val == +1:
116                         self.moveDown()
117                 cur = l.getCurrent()
118                 if self.type == EPG_TYPE_SINGLE:
119                         setEvent(cur)
120                 else:
121                         if self.type == EPG_TYPE_MULTI and cur[0] is None and cur[1].ref != old[1].ref:
122                                 self.eventViewCallback(setEvent, setService, val)
123                         else:
124                                 setService(cur[1])
125                                 setEvent(cur[0])
126
127         def zapTo(self): # just used in multiepg
128                 if self.zapFunc != None:
129                         self.closeRecursive = True
130                         ref = self["list"].getCurrent()[1]
131                         self.zapFunc(ref.ref)
132
133         def eventSelected(self):
134                 if self.type == EPG_TYPE_MULTI:
135                         self.zapTo()
136                 else:
137                         self.infoKeyPressed()
138
139         def yellowButtonPressed(self):
140                 if self.type == EPG_TYPE_MULTI:
141                         self["list"].updateMultiEPG(-1)
142
143         def blueButtonPressed(self):
144                 if self.type == EPG_TYPE_MULTI:
145                         self["list"].updateMultiEPG(1)
146
147         def timerAdd(self):
148                 if self.type == EPG_TYPE_SINGLE:
149                         event = self["list"].getCurrent()
150                         serviceref = self.currentService
151                 else:
152                         cur = self["list"].getCurrent()
153                         event = cur[0]
154                         serviceref = cur[1]
155                 if event is None:
156                         return
157                 newEntry = RecordTimerEntry(serviceref, *parseEvent(event))
158                 self.session.openWithCallback(self.timerEditFinished, TimerEntry, newEntry)
159
160         def timerEditFinished(self, answer):
161                 if answer[0]:
162                         self.session.nav.RecordTimer.record(answer[1])
163                 else:
164                         print "Timeredit aborted"       
165
166         def moveUp(self):
167                 self["list"].moveUp()
168
169         def moveDown(self):
170                 self["list"].moveDown()
171
172         def applyButtonState(self, state):
173                 if state == 1:
174                         self["now_button_sel"].show()
175                         self["now_button"].hide()
176                 else:
177                         self["now_button"].show()
178                         self["now_button_sel"].hide()
179
180                 if state == 2:
181                         self["next_button_sel"].show()
182                         self["next_button"].hide()
183                 else:
184                         self["next_button"].show()
185                         self["next_button_sel"].hide()
186
187                 if state == 3:
188                         self["more_button_sel"].show()
189                         self["more_button"].hide()
190                 else:
191                         self["more_button"].show()
192                         self["more_button_sel"].hide()
193
194         def onSelectionChanged(self):
195                 if self.type == EPG_TYPE_MULTI:
196                         count = self["list"].getCurrentChangeCount()
197                         if count > 1:
198                                 self.applyButtonState(3)
199                         elif count > 0:
200                                 self.applyButtonState(2)
201                         else:
202                                 self.applyButtonState(1)
203                         days = [ _("Mon"), _("Tue"), _("Wed"), _("Thu"), _("Fri"), _("Sat"), _("Sun") ]
204                         datastr = ""
205                         event = self["list"].getCurrent()[0]
206                         if event is not None:
207                                 now = time()
208                                 beg = event.getBeginTime()
209                                 nowTime = localtime(now)
210                                 begTime = localtime(beg)
211                                 if nowTime[2] != begTime[2]:
212                                         datestr = '%s %d.%d.'%(days[begTime[6]], begTime[2], begTime[1])
213                                 else:
214                                         datestr = '%s %d.%d.'%(_("Today"), begTime[2], begTime[1])
215                         self["date"].setText(datestr)