27c4e209bdfa02363ed181e41076a0cb1c6f0d14
[enigma2.git] / lib / python / Screens / ChannelSelection.py
1 from Screen import Screen
2 from Components.Button import Button
3 from Components.ServiceList import ServiceList
4 from Components.ActionMap import NumberActionMap
5 from EpgSelection import EPGSelection
6 from enigma import eServiceReference, eEPGCache, eEPGCachePtr, eServiceCenter, eServiceCenterPtr, iMutableServiceListPtr, iStaticServiceInformationPtr, eTimer
7 from Components.config import config, configElement, ConfigSubsection, configText
8 from Screens.FixedMenu import FixedMenu
9 from Tools.NumericalTextInput import NumericalTextInput
10
11 import xml.dom.minidom
12
13 class BouquetSelector(FixedMenu):
14         def __init__(self, session, bouquets, parent):
15                 self.parent=parent
16                 entrys = [ ]
17                 for x in bouquets:
18                         entrys.append((x[0], self.bouquetSelected, x[1]))
19                 FixedMenu.__init__(self, session, "Bouquetlist", entrys)
20                 self.skinName = "Menu"
21
22         def bouquetSelected(self):
23                 self.parent.addCurrentServiceToBouquet(self["menu"].getCurrent()[2])
24                 self.close()
25
26 class ChannelContextMenu(FixedMenu):
27         def __init__(self, session, csel):
28                 self.csel = csel
29
30                 menu = [ ]
31
32                 inBouquetRootList = csel.servicelist.getRoot().toString().find('FROM BOUQUET "bouquets.') != -1 #FIXME HACK
33                 inBouquet = csel.getMutableList() is not None
34
35                 if not csel.bouquet_mark_edit and not csel.movemode and not inBouquetRootList:
36                         menu.append(("add service to bouquet", self.addServiceToBouquetSelected))
37                         if inBouquet:
38                                 menu.append(("remove service", self.removeCurrentService))
39
40                 if inBouquet: # current list is editable?
41                         if not csel.bouquet_mark_edit:
42                                 if not csel.movemode:
43                                         menu.append(("enable move mode", self.toggleMoveMode))
44                                         if not inBouquetRootList: 
45                                                 menu.append(("enable bouquet edit", self.bouquetMarkStart))
46                                 else:
47                                         menu.append(("disable move mode", self.toggleMoveMode))
48                         elif not inBouquetRootList: 
49                                 menu.append(("end bouquet edit", self.bouquetMarkEnd))
50                                 menu.append(("abort bouquet edit", self.bouquetMarkAbort))
51
52                 menu.append(("back", self.close))
53
54                 FixedMenu.__init__(self, session, "Channel Selection", menu)
55                 self.skinName = "Menu"
56
57         def addServiceToBouquetSelected(self):
58                 bouquets = [ ]
59                 serviceHandler = eServiceCenter.getInstance()
60                 list = serviceHandler.list(self.csel.bouquet_root)
61                 if not list is None:
62                         while True:
63                                 s = list.getNext()
64                                 if not s.valid():
65                                         break
66                                 if ((s.flags & eServiceReference.flagDirectory) == eServiceReference.flagDirectory):
67                                         info = serviceHandler.info(s)
68                                         if not info is None:
69                                                 bouquets.append((info.getName(s), s))
70                 cnt = len(bouquets)
71                 if cnt > 1: # show bouquet list
72                         self.session.open(BouquetSelector, bouquets, self)
73                 elif cnt == 1: # add to only one existing bouquet
74                         self.addCurrentServiceToBouquet(bouquet[0][1])
75                 else: #no bouquets in root.. so assume only one favourite list is used
76                         self.addCurrentServiceToBouquet(self.csel.bouquet_root)
77
78         def addCurrentServiceToBouquet(self, dest):
79                 self.csel.addCurrentServiceToBouquet(dest)
80                 self.close()
81
82         def removeCurrentService(self):
83                 self.csel.removeCurrentService()
84                 self.close()
85
86         def toggleMoveMode(self):
87                 self.csel.toggleMoveMode()
88                 self.close()
89
90         def bouquetMarkStart(self):
91                 self.csel.startMarkedEdit()
92                 self.close()
93
94         def bouquetMarkEnd(self):
95                 self.csel.endMarkedEdit(abort=False)
96                 self.close()
97
98         def bouquetMarkAbort(self):
99                 self.csel.endMarkedEdit(abort=True)
100                 self.close()
101
102 class ChannelSelectionEdit:
103         def __init__(self):
104                 self.entry_marked = False
105                 self.movemode = False
106                 self.bouquet_mark_edit = False
107                 self.mutableList = None
108                 self.__marked = [ ]
109
110         def getMutableList(self, root=eServiceReference()):
111                 if not self.mutableList is None:
112                         return self.mutableList
113                 serviceHandler = eServiceCenter.getInstance()
114                 if not root.valid():
115                         root=self.servicelist.getRoot()
116                 list = serviceHandler.list(root)
117                 if list is not None:
118                         return list.startEdit()
119                 return None
120
121 #  multiple marked entry stuff ( edit mode, later multiepg selection )
122         def startMarkedEdit(self):
123                 self.mutableList = self.getMutableList()
124                 # add all services from the current list to internal marked set in listboxservicecontent
125                 self.bouquetRoot = self.servicelist.getRoot()
126                 self.clearMarks() # this clears the internal marked set in the listboxservicecontent
127                 self.bouquet_mark_edit = True
128                 self.__marked = self.servicelist.getRootServices()
129                 for x in self.__marked:
130                         self.servicelist.addMarked(eServiceReference(x))
131
132         def endMarkedEdit(self, abort):
133                 if not abort and self.mutableList is not None:
134                         new_marked = set(self.servicelist.getMarked())
135                         old_marked = set(self.__marked)
136                         removed = old_marked - new_marked
137                         added = new_marked - old_marked
138                         changed = False
139                         for x in removed:
140                                 changed = True
141                                 self.mutableList.removeService(eServiceReference(x))
142                         for x in added:
143                                 changed = True
144                                 self.mutableList.addService(eServiceReference(x))
145                         if changed:
146                                 self.mutableList.flushChanges()
147                                 self.setRoot(self.bouquetRoot)
148                                 #self.showFavourites()
149                 self.__marked = []
150                 self.clearMarks()
151                 self.bouquet_mark_edit = False
152                 self.bouquetRoot = None
153                 self.mutableList = None
154
155         def clearMarks(self):
156                 self.servicelist.clearMarks()
157
158         def doMark(self):
159                 ref = self.servicelist.getCurrent()
160                 if self.servicelist.isMarked(ref):
161                         self.servicelist.removeMarked(ref)
162                 else:
163                         self.servicelist.addMarked(ref)
164
165         def removeCurrentService(self):
166                 ref = self.servicelist.getCurrent()
167                 mutableList = self.getMutableList()
168                 if ref.valid() and mutableList is not None:
169                         if not mutableList.removeService(ref):
170                                 mutableList.flushChanges() #FIXME dont flush on each single removed service
171                                 self.setRoot(self.servicelist.getRoot())
172
173         def addCurrentServiceToBouquet(self, dest):
174                 mutableList = self.getMutableList(dest)
175                 if not mutableList is None:
176                         if not mutableList.addService(self.servicelist.getCurrent()):
177                                 mutableList.flushChanges()
178                 self.close()
179
180         def toggleMoveMode(self):
181                 if self.movemode:
182                         if self.entry_marked:
183                                 self.toggleMoveMarked() # unmark current entry
184                         self.movemode = False
185                         self.mutableList.flushChanges() # FIXME add check if changes was made
186                         self.mutableList = None
187                 else:
188                         self.mutableList = self.getMutableList()
189                         self.movemode = True
190
191         def handleEditCancel(self):
192                 if self.movemode: #movemode active?
193                         self.channelSelected() # unmark
194                         self.toggleMoveMode() # disable move mode
195                 elif self.bouquet_mark_edit:
196                         self.endMarkedEdit(True) # abort edit mode
197
198         def toggleMoveMarked(self):
199                 if self.entry_marked:
200                         self.servicelist.setCurrentMarked(False)
201                         self.entry_marked = False
202                 else:
203                         self.servicelist.setCurrentMarked(True)
204                         self.entry_marked = True
205
206         def doContext(self):
207                 self.session.open(ChannelContextMenu, self)
208
209 class ChannelSelectionBase(Screen):
210         def __init__(self, session):
211                 Screen.__init__(self, session)
212
213                 #self.bouquet_root = eServiceReference('1:7:1:0:0:0:0:0:0:0:(type == 1) FROM BOUQUET "bouquets.tv" ORDER BY bouquet')
214                 self.bouquet_root = eServiceReference('1:7:1:0:0:0:0:0:0:0:(type == 1) FROM BOUQUET "userbouquet.favourites.tv" ORDER BY bouquet')
215
216                 self["key_red"] = Button("All")
217                 #self["key_green"] = Button("Provider")
218                 #self["key_yellow"] = Button("Satellite")
219                 self["key_green"] = Button("")
220                 self["key_yellow"] = Button("")
221                 self["key_blue"] = Button("Favourites")
222
223                 self["list"] = ServiceList()
224                 self.servicelist = self["list"]
225
226                 #self["okbutton"] = Button("ok", [self.channelSelected])
227                 
228                 self.numericalTextInput = NumericalTextInput()
229                 
230                 self.lastService = None
231
232                 self.lastServiceTimer = eTimer()
233                 self.lastServiceTimer.timeout.get().append(self.lastService)
234                 self.lastServiceTimer.start(100)
235
236         def getBouquetNumOffset(self, bouquet):
237                 if self.bouquet_root.toString().find('FROM BOUQUET "bouquets.') == -1: #FIXME HACK
238                         return 0
239                 offsetCount = 0
240                 serviceHandler = eServiceCenter.getInstance()
241                 bouquetlist = serviceHandler.list(self.bouquet_root)
242                 if not bouquetlist is None:
243                         while True:
244                                 bouquetIterator = bouquetlist.getNext()
245                                 if not bouquetIterator.valid() or bouquetIterator == bouquet: #end of list or bouquet found
246                                         break
247                                 if ((bouquetIterator.flags & eServiceReference.flagDirectory) != eServiceReference.flagDirectory):
248                                         continue
249                                 servicelist = serviceHandler.list(bouquetIterator)
250                                 if not servicelist is None:
251                                         while True:
252                                                 serviceIterator = servicelist.getNext()
253                                                 if not serviceIterator.valid(): #check if end of list
254                                                         break
255                                                 if serviceIterator.flags: #playable services have no flags
256                                                         continue
257                                                 offsetCount += 1
258                 return offsetCount
259
260         def setRootBase(self, root):
261                 inBouquetRootList = root.toString().find('FROM BOUQUET "bouquets.') != -1 #FIXME HACK
262                 if not inBouquetRootList and ((root.flags & eServiceReference.flagDirectory) == eServiceReference.flagDirectory):
263                         self.servicelist.setMode(ServiceList.MODE_FAVOURITES)
264                         self.servicelist.setNumberOffset(self.getBouquetNumOffset(root))
265                 else:
266                         self.servicelist.setMode(ServiceList.MODE_NORMAL)
267                 self.servicelist.setRoot(root)
268
269         def moveUp(self):
270                 self.servicelist.moveUp()
271
272         def moveDown(self):
273                 self.servicelist.moveDown()
274
275         def showFavourites(self):
276                 self.setRoot(self.bouquet_root)
277
278         def keyNumberGlobal(self, number):
279                 print "You pressed number " + str(number)
280                 print "You would go to character " + str(self.numericalTextInput.getKey(number))
281                 # TODO let the servicelist jump to the next entry strating with the chosen character
282
283         def enterBouquet(self, action):
284                 if action[:7] == "bouquet":
285                         if action.find("FROM BOUQUET") != -1:
286                                 self.setRoot(eServiceReference("1:7:1:0:0:0:0:0:0:0:" + action[8:]))
287                         else:
288                                 self.setRoot(eServiceReference("1:0:1:0:0:0:0:0:0:0:" + action[8:]))
289                         return True
290                 return False
291
292         def getRoot(self):
293                 return self.servicelist.getRoot()
294
295         def getCurrentSelection(self):
296                 return self.servicelist.getCurrent()
297
298         def setCurrentSelection(self, service):
299                 self.servicelist.setCurrent(service)
300
301         def cancel(self):
302                 self.close(None)
303
304 class ChannelSelection(ChannelSelectionBase, ChannelSelectionEdit):
305         def __init__(self, session):
306                 ChannelSelectionBase.__init__(self,session)
307                 ChannelSelectionEdit.__init__(self)
308
309                 #config for lastservice
310                 config.tv = ConfigSubsection();
311                 config.tv.lastservice = configElement("config.tv.lastservice", configText, "", 0);
312                 config.tv.lastroot = configElement("config.tv.lastroot", configText, "", 0);
313
314                 if config.tv.lastroot.value == "":
315                         self.servicelist.setRoot(eServiceReference("""1:0:1:0:0:0:0:0:0:0:(type == 1)"""))
316
317                 self.session.nav.playService(eServiceReference(config.tv.lastservice.value))
318
319                 class ChannelActionMap(NumberActionMap):
320                         def action(self, contexts, action):
321                                 if not self.csel.enterBouquet(action):
322                                         if action == "cancel":
323                                                 self.csel.handleEditCancel()
324                                         NumberActionMap.action(self, contexts, action)
325                 self["actions"] = ChannelActionMap(["ChannelSelectActions", "OkCancelActions", "ContextMenuActions"],
326                         {
327                                 "cancel": self.cancel,
328                                 "ok": self.channelSelected,
329                                 "mark": self.doMark,
330                                 "contextMenu": self.doContext,
331                                 "showFavourites": self.showFavourites,
332                                 "showEPGList": self.showEPGList,
333                                 "1": self.keyNumberGlobal,
334                                 "2": self.keyNumberGlobal,
335                                 "3": self.keyNumberGlobal,
336                                 "4": self.keyNumberGlobal,
337                                 "5": self.keyNumberGlobal,
338                                 "6": self.keyNumberGlobal,
339                                 "7": self.keyNumberGlobal,
340                                 "8": self.keyNumberGlobal,
341                                 "9": self.keyNumberGlobal,
342                                 "0": self.keyNumberGlobal
343                         })
344                 self["actions"].csel = self
345
346         def showEPGList(self):
347                 ref=self.servicelist.getCurrent()
348                 ptr=eEPGCache.getInstance()
349                 if ptr.startTimeQuery(ref) != -1:
350                         self.session.open(EPGSelection, ref)
351                 else:
352                         print 'no epg for service', ref.toString()
353
354         def channelSelected(self):
355                 ref = self.servicelist.getCurrent()
356                 if self.movemode:
357                         self.toggleMoveMarked()
358                 elif (ref.flags & 7) == 7:
359                         self.setRoot(ref)
360                 elif self.bouquet_mark_edit:
361                         self.doMark()
362                 else:
363                         self.zap()
364                         self.close(ref)
365
366         def setRoot(self, root):
367                 if not self.movemode:
368                         self.setRootBase(root)
369
370         #called from infoBar and channelSelected
371         def zap(self):
372                 self.session.nav.playService(self.servicelist.getCurrent())
373                 self.saveChannel()
374
375         def saveRoot(self, root):
376                 if root is not None:
377                         config.tv.lastroot.value = root.toString()
378                         config.tv.lastroot.save()
379
380         def saveChannel(self):
381                 ref = self.session.nav.getCurrentlyPlayingServiceReference()
382                 if ref is not None:
383                         refstr = ref.toString()
384                 else:
385                         refstr = ""
386                 config.tv.lastservice.value = refstr
387                 config.tv.lastservice.save()
388
389         def lastService(self):
390                 self.lastServiceTimer.stop()
391                 #zap to last running tv service
392                 self.setRoot(eServiceReference(config.tv.lastroot.value))
393                 self.session.nav.playService(eServiceReference(config.tv.lastservice.value))
394
395 class SimpleChannelSelection(ChannelSelectionBase):
396         def __init__(self, session, title):
397                 ChannelSelectionBase.__init__(self, session)
398                 self.title = title
399                 self.onShown.append(self.onExecCallback)
400
401                 class ChannelActionMap(ActionMap):
402                         def action(self, contexts, action):
403                                 if not self.csel.enterBouquet(action):
404                                         ActionMap.action(self, contexts, action)
405                 self["actions"] = ChannelActionMap(["ChannelSelectActions", "OkCancelActions", "ContextMenuActions"],
406                         {
407                                 "cancel": self.cancel,
408                                 "ok": self.channelSelected,
409                                 "showFavourites": self.showFavourites,
410                                 "1": self.keyNumberGlobal,
411                                 "2": self.keyNumberGlobal,
412                                 "3": self.keyNumberGlobal,
413                                 "4": self.keyNumberGlobal,
414                                 "5": self.keyNumberGlobal,
415                                 "6": self.keyNumberGlobal,
416                                 "7": self.keyNumberGlobal,
417                                 "8": self.keyNumberGlobal,
418                                 "9": self.keyNumberGlobal,
419                                 "0": self.keyNumberGlobal
420                         })
421                 self["actions"].csel = self
422
423         def onExecCallback(self):
424                 print "onExecCallback"
425                 self.session.currentDialog.instance.setTitle(self.title)
426
427         def channelSelected(self): # just return selected service
428                 ref = self.servicelist.getCurrent()
429                 self.close(ref)
430
431         def setRoot(self, root):
432                 self.setRootBase(root)
433