add two native python epg views
[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 from Components.NimManager import nimmanager
11
12 import xml.dom.minidom
13
14 class BouquetSelector(FixedMenu):
15         def __init__(self, session, bouquets, selectedFunc):
16                 self.selectedFunc=selectedFunc
17                 entrys = [ ]
18                 for x in bouquets:
19                         entrys.append((x[0], self.bouquetSelected, x[1]))
20                 FixedMenu.__init__(self, session, "Bouquetlist", entrys)
21                 self.skinName = "Menu"
22
23         def bouquetSelected(self):
24                 self.selectedFunc(self["menu"].getCurrent()[2])
25
26 class ChannelContextMenu(FixedMenu):
27         def __init__(self, session, csel):
28                 self.csel = csel
29
30                 menu = [ ]
31
32                 inBouquetRootList = csel.servicelist.getRoot().getPath().find('FROM BOUQUET "bouquets.') != -1 #FIXME HACK
33                 inBouquet = csel.getMutableList() is not None
34                 haveBouquets = csel.bouquet_root.getPath().find('FROM BOUQUET "bouquets.') != -1
35
36                 if not csel.bouquet_mark_edit and not csel.movemode and not inBouquetRootList:
37                         if (csel.getCurrentSelection().flags & eServiceReference.flagDirectory) != eServiceReference.flagDirectory:
38                                 if haveBouquets:
39                                         menu.append((_("add service to bouquet"), self.addServiceToBouquetSelected))
40                                 else:
41                                         menu.append((_("add service to favourites"), self.addServiceToBouquetSelected))
42                         if inBouquet:
43                                 menu.append((_("remove service"), self.removeCurrentService))
44
45                 if inBouquet: # current list is editable?
46                         if not csel.bouquet_mark_edit:
47                                 if not csel.movemode:
48                                         menu.append((_("enable move mode"), self.toggleMoveMode))
49                                         if not inBouquetRootList:
50                                                 if haveBouquets:
51                                                         menu.append((_("enable bouquet edit"), self.bouquetMarkStart))
52                                                 else:
53                                                         menu.append((_("enable favourite edit"), self.bouquetMarkStart))
54                                 else:
55                                         menu.append((_("disable move mode"), self.toggleMoveMode))
56                         elif not inBouquetRootList:
57                                 if haveBouquets:
58                                         menu.append((_("end bouquet edit"), self.bouquetMarkEnd))
59                                         menu.append((_("abort bouquet edit"), self.bouquetMarkAbort))
60                                 else:
61                                         menu.append((_("end favourites edit"), self.bouquetMarkEnd))
62                                         menu.append((_("abort favourites edit"), self.bouquetMarkAbort))
63
64                 menu.append((_("back"), self.close))
65
66                 FixedMenu.__init__(self, session, _("Channel Selection"), menu)
67                 self.skinName = "Menu"
68
69         def addServiceToBouquetSelected(self):
70                 bouquets = self.csel.getBouquetList()
71                 if bouquets is None:
72                         cnt = 0
73                 else:
74                         cnt = len(bouquets)
75                 if cnt > 1: # show bouquet list
76                         self.session.open(BouquetSelector, bouquets, self.addCurrentServiceToBouquet)
77                 elif cnt == 1: # add to only one existing bouquet
78                         self.addCurrentServiceToBouquet(bouquet[0][1])
79                 else: #no bouquets in root.. so assume only one favourite list is used
80                         self.addCurrentServiceToBouquet(self.csel.bouquet_root)
81
82         def addCurrentServiceToBouquet(self, dest):
83                 self.csel.addCurrentServiceToBouquet(dest)
84                 self.close()
85
86         def removeCurrentService(self):
87                 self.csel.removeCurrentService()
88                 self.close()
89
90         def toggleMoveMode(self):
91                 self.csel.toggleMoveMode()
92                 self.close()
93
94         def bouquetMarkStart(self):
95                 self.csel.startMarkedEdit()
96                 self.close()
97
98         def bouquetMarkEnd(self):
99                 self.csel.endMarkedEdit(abort=False)
100                 self.close()
101
102         def bouquetMarkAbort(self):
103                 self.csel.endMarkedEdit(abort=True)
104                 self.close()
105
106 class ChannelSelectionEdit:
107         def __init__(self):
108                 self.entry_marked = False
109                 self.movemode = False
110                 self.bouquet_mark_edit = False
111                 self.mutableList = None
112                 self.__marked = [ ]
113                 self.saved_title = None
114                 self.saved_root = None
115
116         def getMutableList(self, root=eServiceReference()):
117                 if not self.mutableList is None:
118                         return self.mutableList
119                 serviceHandler = eServiceCenter.getInstance()
120                 if not root.valid():
121                         root=self.servicelist.getRoot()
122                 list = serviceHandler.list(root)
123                 if list is not None:
124                         return list.startEdit()
125                 return None
126
127 #  multiple marked entry stuff ( edit mode, later multiepg selection )
128         def startMarkedEdit(self):
129                 self.mutableList = self.getMutableList()
130                 # add all services from the current list to internal marked set in listboxservicecontent
131                 self.bouquetRoot = self.servicelist.getRoot()
132                 self.clearMarks() # this clears the internal marked set in the listboxservicecontent
133                 self.saved_title = self.instance.getTitle()
134                 new_title = self.saved_title
135                 if self.bouquet_root.getPath().find('FROM BOUQUET "bouquets.') != -1:
136                         new_title += ' ' + _("[bouquet edit]")
137                 else:
138                         new_title += ' ' + _("[favourite edit]")
139                 self.instance.setTitle(new_title)
140                 self.bouquet_mark_edit = True
141                 self.__marked = self.servicelist.getRootServices()
142                 for x in self.__marked:
143                         self.servicelist.addMarked(eServiceReference(x))
144                 self.saved_root = self.getRoot()
145                 self.showAllServices()
146
147         def endMarkedEdit(self, abort):
148                 if not abort and self.mutableList is not None:
149                         new_marked = set(self.servicelist.getMarked())
150                         old_marked = set(self.__marked)
151                         removed = old_marked - new_marked
152                         added = new_marked - old_marked
153                         changed = False
154                         for x in removed:
155                                 changed = True
156                                 self.mutableList.removeService(eServiceReference(x))
157                         for x in added:
158                                 changed = True
159                                 self.mutableList.addService(eServiceReference(x))
160                         if changed:
161                                 self.mutableList.flushChanges()
162                 self.__marked = []
163                 self.clearMarks()
164                 self.bouquet_mark_edit = False
165                 self.bouquetRoot = None
166                 self.mutableList = None
167                 self.instance.setTitle(self.saved_title)
168                 self.saved_title = None
169                 self.setRoot(self.saved_root)
170
171         def clearMarks(self):
172                 self.servicelist.clearMarks()
173
174         def doMark(self):
175                 ref = self.servicelist.getCurrent()
176                 if self.servicelist.isMarked(ref):
177                         self.servicelist.removeMarked(ref)
178                 else:
179                         self.servicelist.addMarked(ref)
180
181         def removeCurrentService(self):
182                 ref = self.servicelist.getCurrent()
183                 mutableList = self.getMutableList()
184                 if ref.valid() and mutableList is not None:
185                         if not mutableList.removeService(ref):
186                                 currentIndex = self.servicelist.getCurrentIndex()
187                                 self.servicelist.moveDown()
188                                 if self.servicelist.getCurrentIndex() == currentIndex:
189                                         currentIndex -= 1
190                                 mutableList.flushChanges() #FIXME dont flush on each single removed service
191                                 self.setRoot(self.servicelist.getRoot())
192                                 self.servicelist.moveToIndex(currentIndex)
193
194         def addCurrentServiceToBouquet(self, dest):
195                 mutableList = self.getMutableList(dest)
196                 if not mutableList is None:
197                         if not mutableList.addService(self.servicelist.getCurrent()):
198                                 mutableList.flushChanges()
199                 self.close()
200
201         def toggleMoveMode(self):
202                 if self.movemode:
203                         if self.entry_marked:
204                                 self.toggleMoveMarked() # unmark current entry
205                         self.movemode = False
206                         self.mutableList.flushChanges() # FIXME add check if changes was made
207                         self.mutableList = None
208                         self.instance.setTitle(self.saved_title)
209                         self.saved_title = None
210                 else:
211                         self.mutableList = self.getMutableList()
212                         self.movemode = True
213                         self.saved_title = self.instance.getTitle()
214                         new_title = self.saved_title
215                         new_title += ' ' + _("[move mode]");
216                         self.instance.setTitle(new_title);
217
218         def handleEditCancel(self):
219                 if self.movemode: #movemode active?
220                         self.channelSelected() # unmark
221                         self.toggleMoveMode() # disable move mode
222                 elif self.bouquet_mark_edit:
223                         self.endMarkedEdit(True) # abort edit mode
224
225         def toggleMoveMarked(self):
226                 if self.entry_marked:
227                         self.servicelist.setCurrentMarked(False)
228                         self.entry_marked = False
229                 else:
230                         self.servicelist.setCurrentMarked(True)
231                         self.entry_marked = True
232
233         def doContext(self):
234                 self.session.open(ChannelContextMenu, self)
235
236 class ChannelSelectionBase(Screen):
237         def __init__(self, session):
238                 Screen.__init__(self, session)
239
240                 # this makes it much simple to implement a selectable radio or tv mode :)
241                 self.service_types_tv = '1:7:1:0:0:0:0:0:0:0:(type == 1) || (type == 17)'
242                 self.service_types_radio = '1:7:1:0:0:0:0:0:0:0:(type == 2)'
243
244                 self.service_types = self.service_types_tv
245
246                 #self.bouquet_root = eServiceReference('1:7:1:0:0:0:0:0:0:0:(type == 1) FROM BOUQUET "bouquets.tv" ORDER BY bouquet')
247                 self.bouquet_root = eServiceReference('%s FROM BOUQUET "userbouquet.favourites.tv" ORDER BY bouquet'%(self.service_types))
248
249                 self["key_red"] = Button(_("All"))
250                 self["key_green"] = Button(_("Satellites"))
251                 self["key_yellow"] = Button(_("Provider"))
252                 self["key_blue"] = Button(_("Favourites"))
253
254                 self["list"] = ServiceList()
255                 self.servicelist = self["list"]
256
257                 #self["okbutton"] = Button("ok", [self.channelSelected])
258
259                 self.numericalTextInput = NumericalTextInput()
260
261         def getBouquetNumOffset(self, bouquet):
262                 if self.bouquet_root.getPath().find('FROM BOUQUET "bouquets.') == -1: #FIXME HACK
263                         return 0
264                 offsetCount = 0
265                 serviceHandler = eServiceCenter.getInstance()
266                 bouquetlist = serviceHandler.list(self.bouquet_root)
267                 if not bouquetlist is None:
268                         while True:
269                                 bouquetIterator = bouquetlist.getNext()
270                                 if not bouquetIterator.valid() or bouquetIterator == bouquet: #end of list or bouquet found
271                                         break
272                                 if ((bouquetIterator.flags & eServiceReference.flagDirectory) != eServiceReference.flagDirectory):
273                                         continue
274                                 servicelist = serviceHandler.list(bouquetIterator)
275                                 if not servicelist is None:
276                                         while True:
277                                                 serviceIterator = servicelist.getNext()
278                                                 if not serviceIterator.valid(): #check if end of list
279                                                         break
280                                                 if serviceIterator.flags: #playable services have no flags
281                                                         continue
282                                                 offsetCount += 1
283                 return offsetCount
284
285         def setRootBase(self, root, justSet=False):
286                 inBouquetRootList = root.getPath().find('FROM BOUQUET "bouquets.') != -1 #FIXME HACK
287                 if not inBouquetRootList and (root.getPath().find('FROM BOUQUET') != -1):
288                         self.servicelist.setMode(ServiceList.MODE_FAVOURITES)
289                         self.servicelist.setNumberOffset(self.getBouquetNumOffset(root))
290                 else:
291                         self.servicelist.setMode(ServiceList.MODE_NORMAL)
292                 self.servicelist.setRoot(root, justSet)
293
294         def moveUp(self):
295                 self.servicelist.moveUp()
296
297         def moveDown(self):
298                 self.servicelist.moveDown()
299
300         def showAllServices(self):
301                 ref = eServiceReference('%s ORDER BY name'%(self.service_types))
302                 self.setRoot(ref)
303
304         def showSatellites(self):
305                 ref = eServiceReference('%s FROM SATELLITES ORDER BY satellitePosition'%(self.service_types))
306                 self.setRoot(ref, True)
307                 serviceHandler = eServiceCenter.getInstance()
308                 servicelist = serviceHandler.list(ref)
309                 if not servicelist is None:
310                         while True:
311                                 service = servicelist.getNext()
312                                 if not service.valid(): #check if end of list
313                                         break
314                                 orbpos = service.getData(4) >> 16
315                                 if service.getPath().find("FROM PROVIDER") != -1:
316                                         service_name = _("Providers")
317                                 else:
318                                         service_name = _("Services")
319                                 try:
320                                         service_name += str(' - %s'%(nimmanager.getSatDescription(orbpos)))
321                                         service.setName(service_name) # why we need this cast?
322                                 except:
323                                         if orbpos > 1800: # west
324                                                 service.setName("%s (%3.1f" + _("W") + ")" %(str, (0 - (orbpos - 3600)) / 10.0))
325                                         else:
326                                                 service.setName("%s (%3.1f" + _("E") + ")" % (str, orbpos / 10.0))
327                                 self.servicelist.addService(service)
328                 self.servicelist.finishFill()
329
330         def showProviders(self):
331                 ref = eServiceReference('%s FROM PROVIDERS ORDER BY name'%(self.service_types))
332                 self.setRoot(ref)
333
334         def showFavourites(self):
335                 self.setRoot(self.bouquet_root)
336
337         def keyNumberGlobal(self, number):
338                 char = self.numericalTextInput.getKey(number)
339                 print "You pressed number " + str(number)
340                 print "You would go to character " + str(char)
341                 self.servicelist.moveToChar(char)
342
343         def enterBouquet(self, action):
344                 if action[:7] == "bouquet":
345                         if action.find("FROM BOUQUET") != -1:
346                                 self.setRoot(eServiceReference("1:7:1:0:0:0:0:0:0:0:" + action[8:]))
347                         else:
348                                 self.setRoot(eServiceReference("1:0:1:0:0:0:0:0:0:0:" + action[8:]))
349                         return True
350                 return False
351
352         def getRoot(self):
353                 return self.servicelist.getRoot()
354
355         def getCurrentSelection(self):
356                 return self.servicelist.getCurrent()
357
358         def setCurrentSelection(self, service):
359                 self.servicelist.setCurrent(service)
360
361         def cancel(self):
362                 self.close(None)
363
364         def getBouquetList(self):
365                 serviceCount=0
366                 bouquets = [ ]
367                 serviceHandler = eServiceCenter.getInstance()
368                 list = serviceHandler.list(self.bouquet_root)
369                 if not list is None:
370                         while True:
371                                 s = list.getNext()
372                                 if not s.valid():
373                                         break
374                                 if ((s.flags & eServiceReference.flagDirectory) == eServiceReference.flagDirectory):
375                                         info = serviceHandler.info(s)
376                                         if not info is None:
377                                                 bouquets.append((info.getName(s), s))
378                                 else:
379                                         serviceCount += 1
380                         if len(bouquets) == 0 and serviceCount > 0:
381                                 info = serviceHandler.info(self.bouquet_root)
382                                 if not info is None:
383                                         bouquets.append((info.getName(self.bouquet_root), self.bouquet_root))
384                         return bouquets
385                 return None
386
387 class ChannelSelection(ChannelSelectionBase, ChannelSelectionEdit):
388         def __init__(self, session):
389                 ChannelSelectionBase.__init__(self,session)
390                 ChannelSelectionEdit.__init__(self)
391
392                 #config for lastservice
393                 config.tv = ConfigSubsection();
394                 config.tv.lastservice = configElement("config.tv.lastservice", configText, "", 0);
395                 config.tv.lastroot = configElement("config.tv.lastroot", configText, "", 0);
396
397                 class ChannelActionMap(NumberActionMap):
398                         def action(self, contexts, action):
399                                 if not self.csel.enterBouquet(action):
400                                         if action == "cancel":
401                                                 self.csel.handleEditCancel()
402                                         NumberActionMap.action(self, contexts, action)
403                 self["actions"] = ChannelActionMap(["ChannelSelectActions", "OkCancelActions", "ContextMenuActions"],
404                         {
405                                 "cancel": self.cancel,
406                                 "ok": self.channelSelected,
407                                 "mark": self.doMark,
408                                 "contextMenu": self.doContext,
409                                 "showFavourites": self.showFavourites,
410                                 "showAllServices": self.showAllServices,
411                                 "showProviders": self.showProviders,
412                                 "showSatellites": self.showSatellites,
413                                 "showEPGList": self.showEPGList,
414                                 "1": self.keyNumberGlobal,
415                                 "2": self.keyNumberGlobal,
416                                 "3": self.keyNumberGlobal,
417                                 "4": self.keyNumberGlobal,
418                                 "5": self.keyNumberGlobal,
419                                 "6": self.keyNumberGlobal,
420                                 "7": self.keyNumberGlobal,
421                                 "8": self.keyNumberGlobal,
422                                 "9": self.keyNumberGlobal,
423                                 "0": self.keyNumberGlobal
424                         })
425                 self["actions"].csel = self
426                 self.onShown.append(self.onShow)
427
428 #               self.onLayoutFinish.append(self.onCreate)
429                 self.lastChannelRootTimer = eTimer()
430                 self.lastChannelRootTimer.timeout.get().append(self.onCreate)
431                 self.lastChannelRootTimer.start(100,True)
432
433         def onCreate(self):
434                 lastroot=eServiceReference(config.tv.lastroot.value)
435                 if lastroot.valid():
436                         self.setRoot(lastroot)
437                 else:
438                         self.showFavourites()
439                         self.saveRoot(self.getRoot())
440                 lastservice=eServiceReference(config.tv.lastservice.value)
441                 if lastservice.valid():
442                         self.session.nav.playService(lastservice)
443                         self.servicelist.setCurrent(lastservice)
444
445         def onShow(self):
446                 ref = self.session.nav.getCurrentlyPlayingServiceReference()
447                 if ref is not None and ref.valid() and ref.getPath() == "":
448                         self.servicelist.setPlayableIgnoreService(ref)
449                 else:
450                         self.servicelist.setPlayableIgnoreService(eServiceReference())
451
452         def showEPGList(self):
453                 ref=self.getCurrentSelection()
454                 ptr=eEPGCache.getInstance()
455                 if ptr.startTimeQuery(ref) != -1:
456                         self.session.open(EPGSelection, ref)
457                 else:
458                         print 'no epg for service', ref.toString()
459
460         def channelSelected(self):
461                 ref = self.getCurrentSelection()
462                 if self.movemode:
463                         self.toggleMoveMarked()
464                 elif (ref.flags & 7) == 7:
465                         self.setRoot(ref)
466                 elif self.bouquet_mark_edit:
467                         self.doMark()
468                 else:
469                         self.zap()
470                         self.close(ref)
471
472         def setRoot(self, root, justSet=False):
473                 if not self.movemode:
474                         self.setRootBase(root, justSet)
475
476         #called from infoBar and channelSelected
477         def zap(self):
478                 self.session.nav.playService(self.getCurrentSelection())
479                 self.saveRoot(self.getRoot())
480                 self.saveChannel()
481
482         def saveRoot(self, root):
483                 if root is not None:
484                         config.tv.lastroot.value = root.toString()
485                         config.tv.lastroot.save()
486
487         def saveChannel(self):
488                 ref = self.session.nav.getCurrentlyPlayingServiceReference()
489                 if ref is not None:
490                         refstr = ref.toString()
491                 else:
492                         refstr = ""
493                 config.tv.lastservice.value = refstr
494                 config.tv.lastservice.save()
495
496         def cancel(self):
497                 self.close(None)
498                 lastroot=eServiceReference(config.tv.lastroot.value)
499                 lastservice=eServiceReference(config.tv.lastservice.value)
500                 if lastroot.valid() and self.getRoot() != lastroot:
501                         self.setRoot(lastroot)
502                 if lastservice.valid() and self.getCurrentSelection() != lastservice:
503                         self.servicelist.setCurrent(lastservice)
504
505 class SimpleChannelSelection(ChannelSelectionBase):
506         def __init__(self, session, title):
507                 ChannelSelectionBase.__init__(self, session)
508                 self.title = title
509                 self.onShown.append(self.onExecCallback)
510
511                 class ChannelActionMap(NumberActionMap):
512                         def action(self, contexts, action):
513                                 if not self.csel.enterBouquet(action):
514                                         NumberActionMap.action(self, contexts, action)
515                 self["actions"] = ChannelActionMap(["ChannelSelectActions", "OkCancelActions", "ContextMenuActions"],
516                         {
517                                 "cancel": self.cancel,
518                                 "ok": self.channelSelected,
519                                 "showFavourites": self.showFavourites,
520                                 "showAllServices": self.showAllServices,
521                                 "showProviders": self.showProviders,
522                                 "showSatellites": self.showSatellites,
523                                 "1": self.keyNumberGlobal,
524                                 "2": self.keyNumberGlobal,
525                                 "3": self.keyNumberGlobal,
526                                 "4": self.keyNumberGlobal,
527                                 "5": self.keyNumberGlobal,
528                                 "6": self.keyNumberGlobal,
529                                 "7": self.keyNumberGlobal,
530                                 "8": self.keyNumberGlobal,
531                                 "9": self.keyNumberGlobal,
532                                 "0": self.keyNumberGlobal
533                         })
534                 self["actions"].csel = self
535
536         def onExecCallback(self):
537                 print "onExecCallback"
538                 self.showFavourites()
539                 self.session.currentDialog.instance.setTitle(self.title)
540
541         def channelSelected(self): # just return selected service
542                 ref = self.getCurrentSelection()
543                 self.close(ref)
544
545         def setRoot(self, root):
546                 self.setRootBase(root)
547