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