small fix
[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(bouquets[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 USE_MULTIBOUQUETS = False
237
238 class ChannelSelectionBase(Screen):
239         def __init__(self, session):
240                 Screen.__init__(self, session)
241
242                 # this makes it much simple to implement a selectable radio or tv mode :)
243                 self.service_types_tv = '1:7:1:0:0:0:0:0:0:0:(type == 1) || (type == 17)'
244 #               self.service_types_radio = '1:7:1:0:0:0:0:0:0:0:(type == 2)'
245
246                 self.service_types = self.service_types_tv
247                 if USE_MULTIBOUQUETS:
248                         self.bouquet_root = eServiceReference('1:7:1:0:0:0:0:0:0:0:(type == 1) FROM BOUQUET "bouquets.tv" ORDER BY bouquet')
249                 else:
250                         self.bouquet_root = eServiceReference('%s FROM BOUQUET "userbouquet.favourites.tv" ORDER BY bouquet'%(self.service_types))
251
252                 self["key_red"] = Button(_("All"))
253                 self["key_green"] = Button(_("Satellites"))
254                 self["key_yellow"] = Button(_("Provider"))
255                 self["key_blue"] = Button(_("Favourites"))
256
257                 self["list"] = ServiceList()
258                 self.servicelist = self["list"]
259
260                 #self["okbutton"] = Button("ok", [self.channelSelected])
261
262                 self.numericalTextInput = NumericalTextInput()
263
264         def appendDVBTypes(self, ref):
265                 path = ref.getPath()
266                 pos = path.find(' FROM BOUQUET')
267                 if pos != -1:
268                         return eServiceReference(self.service_types + path[pos:])
269                 return ref
270
271         def getBouquetNumOffset(self, bouquet):
272                 bouquet = self.appendDVBTypes(bouquet)
273                 if self.bouquet_root.getPath().find('FROM BOUQUET "bouquets.') == -1: #FIXME HACK
274                         return 0
275                 offsetCount = 0
276                 serviceHandler = eServiceCenter.getInstance()
277                 bouquetlist = serviceHandler.list(self.bouquet_root)
278                 if not bouquetlist is None:
279                         while True:
280                                 bouquetIterator = self.appendDVBTypes(bouquetlist.getNext())
281                                 if not bouquetIterator.valid() or bouquetIterator == bouquet: #end of list or bouquet found
282                                         break
283                                 if ((bouquetIterator.flags & eServiceReference.flagDirectory) != eServiceReference.flagDirectory):
284                                         continue
285                                 servicelist = serviceHandler.list(bouquetIterator)
286                                 if not servicelist is None:
287                                         while True:
288                                                 serviceIterator = servicelist.getNext()
289                                                 if not serviceIterator.valid(): #check if end of list
290                                                         break
291                                                 if serviceIterator.flags: #playable services have no flags
292                                                         continue
293                                                 offsetCount += 1
294                 return offsetCount
295
296         def setRootBase(self, root, justSet=False):
297                 path = root.getPath()
298                 inBouquetRootList = path.find('FROM BOUQUET "bouquets.') != -1 #FIXME HACK
299                 pos = path.find(' FROM BOUQUET')
300                 isBouquet = pos != -1
301                 if not inBouquetRootList and isBouquet:
302                         self.servicelist.setMode(ServiceList.MODE_FAVOURITES)
303                         self.servicelist.setNumberOffset(self.getBouquetNumOffset(root))
304                         refstr = self.service_types + path[pos:]
305                         root = eServiceReference(refstr)
306                 else:
307                         self.servicelist.setMode(ServiceList.MODE_NORMAL)
308                 self.servicelist.setRoot(root, justSet)
309
310         def moveUp(self):
311                 self.servicelist.moveUp()
312
313         def moveDown(self):
314                 self.servicelist.moveDown()
315
316         def showAllServices(self):
317                 ref = eServiceReference('%s ORDER BY name'%(self.service_types))
318                 self.setRoot(ref)
319
320         def showSatellites(self):
321                 ref = eServiceReference('%s FROM SATELLITES ORDER BY satellitePosition'%(self.service_types))
322                 self.setRoot(ref, True)
323                 serviceHandler = eServiceCenter.getInstance()
324                 servicelist = serviceHandler.list(ref)
325                 if not servicelist is None:
326                         while True:
327                                 service = servicelist.getNext()
328                                 if not service.valid(): #check if end of list
329                                         break
330                                 orbpos = service.getData(4) >> 16
331                                 if service.getPath().find("FROM PROVIDER") != -1:
332                                         service_name = _("Providers")
333                                 else:
334                                         service_name = _("Services")
335                                 try:
336                                         service_name += str(' - %s'%(nimmanager.getSatDescription(orbpos)))
337                                         service.setName(service_name) # why we need this cast?
338                                 except:
339                                         if orbpos > 1800: # west
340                                                 service.setName("%s (%3.1f" + _("W") + ")" %(str, (0 - (orbpos - 3600)) / 10.0))
341                                         else:
342                                                 service.setName("%s (%3.1f" + _("E") + ")" % (str, orbpos / 10.0))
343                                 self.servicelist.addService(service)
344                 self.servicelist.finishFill()
345
346         def showProviders(self):
347                 ref = eServiceReference('%s FROM PROVIDERS ORDER BY name'%(self.service_types))
348                 self.setRoot(ref)
349
350         def showFavourites(self):
351                 self.setRoot(self.bouquet_root)
352
353         def keyNumberGlobal(self, number):
354                 char = self.numericalTextInput.getKey(number)
355                 print "You pressed number " + str(number)
356                 print "You would go to character " + str(char)
357                 self.servicelist.moveToChar(char)
358
359         def enterBouquet(self, action):
360                 if action[:7] == "bouquet":
361                         if action.find("FROM BOUQUET") != -1:
362                                 self.setRoot(eServiceReference("1:7:1:0:0:0:0:0:0:0:" + action[8:]))
363                         else:
364                                 self.setRoot(eServiceReference("1:0:1:0:0:0:0:0:0:0:" + action[8:]))
365                         return True
366                 return False
367
368         def getRoot(self):
369                 return self.servicelist.getRoot()
370
371         def getCurrentSelection(self):
372                 return self.servicelist.getCurrent()
373
374         def setCurrentSelection(self, service):
375                 self.servicelist.setCurrent(service)
376
377         def cancel(self):
378                 self.close(None)
379
380         def getBouquetList(self):
381                 serviceCount=0
382                 bouquets = [ ]
383                 serviceHandler = eServiceCenter.getInstance()
384                 list = serviceHandler.list(self.bouquet_root)
385                 if not list is None:
386                         while True:
387                                 s = list.getNext()
388                                 if not s.valid():
389                                         break
390                                 if ((s.flags & eServiceReference.flagDirectory) == eServiceReference.flagDirectory):
391                                         info = serviceHandler.info(s)
392                                         if not info is None:
393                                                 bouquets.append((info.getName(s), s))
394                                 else:
395                                         serviceCount += 1
396                         if len(bouquets) == 0 and serviceCount > 0:
397                                 info = serviceHandler.info(self.bouquet_root)
398                                 if not info is None:
399                                         bouquets.append((info.getName(self.bouquet_root), self.bouquet_root))
400                         return bouquets
401                 return None
402
403 class ChannelSelection(ChannelSelectionBase, ChannelSelectionEdit):
404         def __init__(self, session):
405                 ChannelSelectionBase.__init__(self,session)
406                 ChannelSelectionEdit.__init__(self)
407
408                 #config for lastservice
409                 config.tv = ConfigSubsection();
410                 config.tv.lastservice = configElement("config.tv.lastservice", configText, "", 0);
411                 config.tv.lastroot = configElement("config.tv.lastroot", configText, "", 0);
412
413                 class ChannelActionMap(NumberActionMap):
414                         def action(self, contexts, action):
415                                 if not self.csel.enterBouquet(action):
416                                         if action == "cancel":
417                                                 self.csel.handleEditCancel()
418                                         NumberActionMap.action(self, contexts, action)
419                 self["actions"] = ChannelActionMap(["ChannelSelectActions", "OkCancelActions", "ContextMenuActions"],
420                         {
421                                 "cancel": self.cancel,
422                                 "ok": self.channelSelected,
423                                 "mark": self.doMark,
424                                 "contextMenu": self.doContext,
425                                 "showFavourites": self.showFavourites,
426                                 "showAllServices": self.showAllServices,
427                                 "showProviders": self.showProviders,
428                                 "showSatellites": self.showSatellites,
429                                 "showEPGList": self.showEPGList,
430                                 "1": self.keyNumberGlobal,
431                                 "2": self.keyNumberGlobal,
432                                 "3": self.keyNumberGlobal,
433                                 "4": self.keyNumberGlobal,
434                                 "5": self.keyNumberGlobal,
435                                 "6": self.keyNumberGlobal,
436                                 "7": self.keyNumberGlobal,
437                                 "8": self.keyNumberGlobal,
438                                 "9": self.keyNumberGlobal,
439                                 "0": self.keyNumberGlobal
440                         })
441                 self["actions"].csel = self
442                 self.onShown.append(self.onShow)
443
444 #               self.onLayoutFinish.append(self.onCreate)
445                 self.lastChannelRootTimer = eTimer()
446                 self.lastChannelRootTimer.timeout.get().append(self.onCreate)
447                 self.lastChannelRootTimer.start(100,True)
448
449         def onCreate(self):
450                 lastroot=eServiceReference(config.tv.lastroot.value)
451                 if lastroot.valid():
452                         self.setRoot(lastroot)
453                 else:
454                         self.showFavourites()
455                         self.saveRoot(self.getRoot())
456                 lastservice=eServiceReference(config.tv.lastservice.value)
457                 if lastservice.valid():
458                         self.session.nav.playService(lastservice)
459                         self.servicelist.setCurrent(lastservice)
460
461         def onShow(self):
462                 ref = self.session.nav.getCurrentlyPlayingServiceReference()
463                 if ref is not None and ref.valid() and ref.getPath() == "":
464                         self.servicelist.setPlayableIgnoreService(ref)
465                 else:
466                         self.servicelist.setPlayableIgnoreService(eServiceReference())
467
468         def showEPGList(self):
469                 ref=self.getCurrentSelection()
470                 ptr=eEPGCache.getInstance()
471                 if ptr.startTimeQuery(ref) != -1:
472                         self.session.open(EPGSelection, ref)
473                 else:
474                         print 'no epg for service', ref.toString()
475
476         def channelSelected(self):
477                 ref = self.getCurrentSelection()
478                 if self.movemode:
479                         self.toggleMoveMarked()
480                 elif (ref.flags & 7) == 7:
481                         self.setRoot(ref)
482                 elif self.bouquet_mark_edit:
483                         self.doMark()
484                 else:
485                         self.zap()
486                         self.close(ref)
487
488         def setRoot(self, root, justSet=False):
489                 if not self.movemode:
490                         self.setRootBase(root, justSet)
491
492         #called from infoBar and channelSelected
493         def zap(self):
494                 self.session.nav.playService(self.getCurrentSelection())
495                 self.saveRoot(self.getRoot())
496                 self.saveChannel()
497
498         def saveRoot(self, root):
499                 if root is not None:
500                         config.tv.lastroot.value = root.toString()
501                         config.tv.lastroot.save()
502
503         def saveChannel(self):
504                 ref = self.session.nav.getCurrentlyPlayingServiceReference()
505                 if ref is not None:
506                         refstr = ref.toString()
507                 else:
508                         refstr = ""
509                 config.tv.lastservice.value = refstr
510                 config.tv.lastservice.save()
511
512         def cancel(self):
513                 self.close(None)
514                 lastroot=eServiceReference(config.tv.lastroot.value)
515                 lastservice=eServiceReference(config.tv.lastservice.value)
516                 if lastroot.valid() and self.getRoot() != lastroot:
517                         self.setRoot(lastroot)
518                 if lastservice.valid() and self.getCurrentSelection() != lastservice:
519                         self.servicelist.setCurrent(lastservice)
520
521 class SimpleChannelSelection(ChannelSelectionBase):
522         def __init__(self, session, title):
523                 ChannelSelectionBase.__init__(self, session)
524                 self.title = title
525                 self.onShown.append(self.onExecCallback)
526
527                 class ChannelActionMap(NumberActionMap):
528                         def action(self, contexts, action):
529                                 if not self.csel.enterBouquet(action):
530                                         NumberActionMap.action(self, contexts, action)
531                 self["actions"] = ChannelActionMap(["ChannelSelectActions", "OkCancelActions", "ContextMenuActions"],
532                         {
533                                 "cancel": self.cancel,
534                                 "ok": self.channelSelected,
535                                 "showFavourites": self.showFavourites,
536                                 "showAllServices": self.showAllServices,
537                                 "showProviders": self.showProviders,
538                                 "showSatellites": self.showSatellites,
539                                 "1": self.keyNumberGlobal,
540                                 "2": self.keyNumberGlobal,
541                                 "3": self.keyNumberGlobal,
542                                 "4": self.keyNumberGlobal,
543                                 "5": self.keyNumberGlobal,
544                                 "6": self.keyNumberGlobal,
545                                 "7": self.keyNumberGlobal,
546                                 "8": self.keyNumberGlobal,
547                                 "9": self.keyNumberGlobal,
548                                 "0": self.keyNumberGlobal
549                         })
550                 self["actions"].csel = self
551
552         def onExecCallback(self):
553                 print "onExecCallback"
554                 self.showFavourites()
555                 self.session.currentDialog.instance.setTitle(self.title)
556
557         def channelSelected(self): # just return selected service
558                 ref = self.getCurrentSelection()
559                 self.close(ref)
560
561         def setRoot(self, root):
562                 self.setRootBase(root)
563