possibility to sort epg list alphabetically (thanks to Moritz Venn)
[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, EPG_TYPE_SINGLE, EPG_TYPE_SIMILAR, EPG_TYPE_MULTI
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                         self.sort_type = 0
40                         self.setSortDescription()
41                 else:
42                         self.skinName = "EPGSelectionMulti"
43                         self.type = EPG_TYPE_MULTI
44                         self["key_yellow"] = Button(_("Prev"))
45                         self["key_blue"] = Button(_("Next"))
46                         self["now_button"] = Pixmap()
47                         self["next_button"] = Pixmap()
48                         self["more_button"] = Pixmap()
49                         self["now_button_sel"] = Pixmap()
50                         self["next_button_sel"] = Pixmap()
51                         self["more_button_sel"] = Pixmap()
52                         self["now_text"] = Label()
53                         self["next_text"] = Label()
54                         self["more_text"] = Label()
55                         self["date"] = Label()
56                         self.services = service
57                         self.zapFunc = zapFunc
58
59                 self["key_green"] = Button(_("Add timer"))
60                 self["list"] = EPGList(type = self.type, selChangedCB = self.onSelectionChanged, timer = self.session.nav.RecordTimer)
61
62                 self["actions"] = ActionMap(["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                                 "red": self.zapTo,
71                                 "input_date_time": self.enterDateTime,
72                                 "nextBouquet": self.nextBouquet,
73                                 "prevBouquet": self.prevBouquet
74                         })
75                 self["actions"].csel = self
76
77                 self.onLayoutFinish.append(self.onCreate)
78
79         def nextBouquet(self):
80                 if self.bouquetChangeCB:
81                         self.bouquetChangeCB(1, self)
82
83         def prevBouquet(self):
84                 if self.bouquetChangeCB:
85                         self.bouquetChangeCB(-1, self)
86
87         def enterDateTime(self):
88                 if self.type == EPG_TYPE_MULTI:
89                         global mepg_config_initialized
90                         if not mepg_config_initialized:
91                                 config.misc.prev_mepg_time=ConfigClock(default = time())
92                                 mepg_config_initialized = True
93                         self.session.openWithCallback(self.onDateTimeInputClosed, TimeDateInput, config.misc.prev_mepg_time )
94
95         def onDateTimeInputClosed(self, ret):
96                 if len(ret) > 1:
97                         if ret[0]:
98                                 self.ask_time=ret[1]
99                                 self["list"].fillMultiEPG(self.services, ret[1])
100
101         def closeScreen(self):
102                 self.close(self.closeRecursive)
103
104         def infoKeyPressed(self):
105                 cur = self["list"].getCurrent()
106                 event = cur[0]
107                 service = cur[1]
108                 if event is not None:
109                         if self.type != EPG_TYPE_SIMILAR:
110                                 self.session.open(EventViewSimple, event, service, self.eventViewCallback, self.openSimilarList)
111                         else:
112                                 self.session.open(EventViewSimple, event, service, self.eventViewCallback)
113
114         def openSimilarList(self, eventid, refstr):
115                 self.session.open(EPGSelection, refstr, None, eventid)
116
117         def setServices(self, services):
118                 self.services = services
119                 self.onCreate()
120
121         #just used in multipeg
122         def onCreate(self):
123                 l = self["list"]
124                 l.recalcEntrySize()
125                 if self.type == EPG_TYPE_MULTI:
126                         l.fillMultiEPG(self.services, self.ask_time)
127                 elif self.type == EPG_TYPE_SINGLE:
128                         l.fillSingleEPG(self.currentService)
129                 else:
130                         l.fillSimilarList(self.currentService, self.eventid)
131
132         def eventViewCallback(self, setEvent, setService, val):
133                 l = self["list"]
134                 old = l.getCurrent()
135                 if val == -1:
136                         self.moveUp()
137                 elif val == +1:
138                         self.moveDown()
139                 cur = l.getCurrent()
140                 if self.type == EPG_TYPE_MULTI and cur[0] is None and cur[1].ref != old[1].ref:
141                         self.eventViewCallback(setEvent, setService, val)
142                 else:
143                         setService(cur[1])
144                         setEvent(cur[0])
145
146         def zapTo(self): # just used in multiepg
147                 if self.zapFunc and self["key_red"].getText() == "Zap":
148                         lst = self["list"]
149                         count = lst.getCurrentChangeCount()
150                         if count == 0:
151                                 self.closeRecursive = True
152                                 ref = lst.getCurrent()[1]
153                                 self.zapFunc(ref.ref)
154
155         def eventSelected(self):
156                 self.infoKeyPressed()
157
158         def yellowButtonPressed(self):
159                 if self.type == EPG_TYPE_MULTI:
160                         self["list"].updateMultiEPG(-1)
161                 elif self.type == EPG_TYPE_SINGLE:
162                         if self.sort_type == 0:
163                                 self.sort_type = 1
164                         else:
165                                 self.sort_type = 0
166                         self["list"].sortSingleEPG(self.sort_type)
167                         self.setSortDescription()
168
169         def setSortDescription(self):
170                 if self.sort_type == 1:
171                         # TRANSLATORS: This must fit into the header button in the EPG-List
172                         self["key_yellow"].setText(_("Sort Time"))
173                 else:
174                         # TRANSLATORS: This must fit into the header button in the EPG-List
175                         self["key_yellow"].setText(_("Sort A-Z"))
176
177         def blueButtonPressed(self):
178                 if self.type == EPG_TYPE_MULTI:
179                         self["list"].updateMultiEPG(1)
180
181         def timerAdd(self):
182                 cur = self["list"].getCurrent()
183                 event = cur[0]
184                 serviceref = cur[1]
185                 if event is None:
186                         return
187                 newEntry = RecordTimerEntry(serviceref, checkOldTimers = True, *parseEvent(event))
188                 self.session.openWithCallback(self.timerEditFinished, TimerEntry, newEntry)
189
190         def timerEditFinished(self, answer):
191                 if answer[0]:
192                         self.session.nav.RecordTimer.record(answer[1])
193                 else:
194                         print "Timeredit aborted"       
195
196         def moveUp(self):
197                 self["list"].moveUp()
198
199         def moveDown(self):
200                 self["list"].moveDown()
201
202         def applyButtonState(self, state):
203                 if state == 0:
204                         self["now_button"].hide()
205                         self["now_button_sel"].hide()
206                         self["next_button"].hide()
207                         self["next_button_sel"].hide()
208                         self["more_button"].hide()
209                         self["more_button_sel"].hide()
210                         self["now_text"].hide()
211                         self["next_text"].hide()
212                         self["more_text"].hide()
213                         self["key_red"].setText("")
214                 else:
215                         if state == 1:
216                                 self["key_red"].setText("Zap")
217                                 self["now_button_sel"].show()
218                                 self["now_button"].hide()
219                         else:
220                                 self["now_button"].show()
221                                 self["now_button_sel"].hide()
222                                 self["key_red"].setText("")
223
224                         if state == 2:
225                                 self["next_button_sel"].show()
226                                 self["next_button"].hide()
227                         else:
228                                 self["next_button"].show()
229                                 self["next_button_sel"].hide()
230
231                         if state == 3:
232                                 self["more_button_sel"].show()
233                                 self["more_button"].hide()
234                         else:
235                                 self["more_button"].show()
236                                 self["more_button_sel"].hide()
237
238         def onSelectionChanged(self):
239                 if self.type == EPG_TYPE_MULTI:
240                         count = self["list"].getCurrentChangeCount()
241                         if self.ask_time != -1:
242                                 self.applyButtonState(0)
243                         elif count > 1:
244                                 self.applyButtonState(3)
245                         elif count > 0:
246                                 self.applyButtonState(2)
247                         else:
248                                 self.applyButtonState(1)
249                         days = [ _("Mon"), _("Tue"), _("Wed"), _("Thu"), _("Fri"), _("Sat"), _("Sun") ]
250                         datastr = ""
251                         event = self["list"].getCurrent()[0]
252                         if event is not None:
253                                 now = time()
254                                 beg = event.getBeginTime()
255                                 nowTime = localtime(now)
256                                 begTime = localtime(beg)
257                                 if nowTime[2] != begTime[2]:
258                                         datestr = '%s %d.%d.'%(days[begTime[6]], begTime[2], begTime[1])
259                                 else:
260                                         datestr = '%s %d.%d.'%(_("Today"), begTime[2], begTime[1])
261                         self["date"].setText(datestr)