remove some unneeded debug output
[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, ActionMap
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, currentConfigSelectionElement
8 from Screens.FixedMenu import FixedMenu
9 from Tools.NumericalTextInput import NumericalTextInput
10 from Components.NimManager import nimmanager
11 from Components.ServiceName import ServiceName
12 from Components.Clock import Clock
13 from Components.EventInfo import EventInfo
14 from re import *
15
16 import xml.dom.minidom
17
18 class BouquetSelector(FixedMenu):
19         def __init__(self, session, bouquets, selectedFunc):
20                 self.selectedFunc=selectedFunc
21                 entrys = [ ]
22                 for x in bouquets:
23                         entrys.append((x[0], self.bouquetSelected, x[1]))
24                 FixedMenu.__init__(self, session, "Bouquetlist", entrys)
25                 self.skinName = "Menu"
26
27         def bouquetSelected(self):
28                 self.selectedFunc(self["menu"].getCurrent()[2])
29
30 class ChannelContextMenu(FixedMenu):
31         def __init__(self, session, csel):
32                 self.csel = csel
33
34                 menu = [ ]
35
36                 inBouquetRootList = csel.getRoot().getPath().find('FROM BOUQUET "bouquets.') != -1 #FIXME HACK
37                 inBouquet = csel.getMutableList() is not None
38                 haveBouquets = csel.bouquet_root.getPath().find('FROM BOUQUET "bouquets.') != -1
39
40                 if not csel.bouquet_mark_edit and not csel.movemode and not inBouquetRootList:
41                         if (csel.getCurrentSelection().flags & eServiceReference.flagDirectory) != eServiceReference.flagDirectory:
42                                 if haveBouquets:
43                                         menu.append((_("add service to bouquet"), self.addServiceToBouquetSelected))
44                                 else:
45                                         menu.append((_("add service to favourites"), self.addServiceToBouquetSelected))
46                         if inBouquet:
47                                 menu.append((_("remove service"), self.removeCurrentService))
48
49                 if inBouquet: # current list is editable?
50                         if not csel.bouquet_mark_edit:
51                                 if not csel.movemode:
52                                         menu.append((_("enable move mode"), self.toggleMoveMode))
53                                         if not inBouquetRootList:
54                                                 if haveBouquets:
55                                                         menu.append((_("enable bouquet edit"), self.bouquetMarkStart))
56                                                 else:
57                                                         menu.append((_("enable favourite edit"), self.bouquetMarkStart))
58                                 else:
59                                         menu.append((_("disable move mode"), self.toggleMoveMode))
60                         elif not inBouquetRootList:
61                                 if haveBouquets:
62                                         menu.append((_("end bouquet edit"), self.bouquetMarkEnd))
63                                         menu.append((_("abort bouquet edit"), self.bouquetMarkAbort))
64                                 else:
65                                         menu.append((_("end favourites edit"), self.bouquetMarkEnd))
66                                         menu.append((_("abort favourites edit"), self.bouquetMarkAbort))
67
68                 menu.append((_("back"), self.close))
69
70                 FixedMenu.__init__(self, session, _("Channel Selection"), menu)
71                 self.skinName = "Menu"
72
73         def addServiceToBouquetSelected(self):
74                 bouquets = self.csel.getBouquetList()
75                 if bouquets is None:
76                         cnt = 0
77                 else:
78                         cnt = len(bouquets)
79                 if cnt > 1: # show bouquet list
80                         self.session.open(BouquetSelector, bouquets, self.addCurrentServiceToBouquet)
81                 elif cnt == 1: # add to only one existing bouquet
82                         self.addCurrentServiceToBouquet(bouquets[0][1])
83                 else: #no bouquets in root.. so assume only one favourite list is used
84                         self.addCurrentServiceToBouquet(self.csel.bouquet_root)
85
86         def addCurrentServiceToBouquet(self, dest):
87                 self.csel.addCurrentServiceToBouquet(dest)
88                 self.close()
89
90         def removeCurrentService(self):
91                 self.csel.removeCurrentService()
92                 self.close()
93
94         def toggleMoveMode(self):
95                 self.csel.toggleMoveMode()
96                 self.close()
97
98         def bouquetMarkStart(self):
99                 self.csel.startMarkedEdit()
100                 self.close()
101
102         def bouquetMarkEnd(self):
103                 self.csel.endMarkedEdit(abort=False)
104                 self.close()
105
106         def bouquetMarkAbort(self):
107                 self.csel.endMarkedEdit(abort=True)
108                 self.close()
109
110 class ChannelSelectionEPG:
111         def __init__(self):
112                 self["ChannelSelectEPGActions"] = ActionMap(["ChannelSelectEPGActions"],
113                         {
114                                 "showEPGList": self.showEPGList,
115                         })
116
117         def showEPGList(self):
118                 ref=self.getCurrentSelection()
119                 ptr=eEPGCache.getInstance()
120                 if ptr.startTimeQuery(ref) != -1:
121                         self.session.open(EPGSelection, ref)
122                 else:
123                         print 'no epg for service', ref.toString()
124
125 class ChannelSelectionEdit:
126         def __init__(self):
127                 self.entry_marked = False
128                 self.movemode = False
129                 self.bouquet_mark_edit = False
130                 self.mutableList = None
131                 self.__marked = [ ]
132                 self.saved_title = None
133                 self.saved_root = None
134
135                 class ChannelSelectionEditActionMap(ActionMap):
136                         def __init__(self, csel, contexts = [ ], actions = { }, prio=0):
137                                 ActionMap.__init__(self, contexts, actions, prio)
138                                 self.csel = csel
139                         def action(self, contexts, action):
140                                 if action == "cancel":
141                                         self.csel.handleEditCancel()
142                                 elif action == "ok":
143                                         pass # avoid typo warning...
144                                 else:
145                                         ActionMap.action(self, contexts, action)
146                 self["ChannelSelectEditActions"] = ChannelSelectionEditActionMap(self, ["ChannelSelectEditActions", "OkCancelActions"],
147                         {
148                                 "contextMenu": self.doContext,
149                         })
150
151         def getMutableList(self, root=eServiceReference()):
152                 if not self.mutableList is None:
153                         return self.mutableList
154                 serviceHandler = eServiceCenter.getInstance()
155                 if not root.valid():
156                         root=self.getRoot()
157                 list = serviceHandler.list(root)
158                 if list is not None:
159                         return list.startEdit()
160                 return None
161
162 #  multiple marked entry stuff ( edit mode, later multiepg selection )
163         def startMarkedEdit(self):
164                 self.mutableList = self.getMutableList()
165                 # add all services from the current list to internal marked set in listboxservicecontent
166                 self.bouquetRoot = self.getRoot()
167                 self.clearMarks() # this clears the internal marked set in the listboxservicecontent
168                 self.saved_title = self.instance.getTitle()
169                 new_title = self.saved_title
170                 if self.bouquet_root.getPath().find('FROM BOUQUET "bouquets.') != -1:
171                         new_title += ' ' + _("[bouquet edit]")
172                 else:
173                         new_title += ' ' + _("[favourite edit]")
174                 self.instance.setTitle(new_title)
175                 self.bouquet_mark_edit = True
176                 self.__marked = self.servicelist.getRootServices()
177                 for x in self.__marked:
178                         self.servicelist.addMarked(eServiceReference(x))
179                 self.saved_root = self.getRoot()
180                 self.showAllServices()
181
182         def endMarkedEdit(self, abort):
183                 if not abort and self.mutableList is not None:
184                         new_marked = set(self.servicelist.getMarked())
185                         old_marked = set(self.__marked)
186                         removed = old_marked - new_marked
187                         added = new_marked - old_marked
188                         changed = False
189                         for x in removed:
190                                 changed = True
191                                 self.mutableList.removeService(eServiceReference(x))
192                         for x in added:
193                                 changed = True
194                                 self.mutableList.addService(eServiceReference(x))
195                         if changed:
196                                 self.mutableList.flushChanges()
197                 self.__marked = []
198                 self.clearMarks()
199                 self.bouquet_mark_edit = False
200                 self.bouquetRoot = None
201                 self.mutableList = None
202                 self.instance.setTitle(self.saved_title)
203                 self.saved_title = None
204                 self.setRoot(self.saved_root)
205
206         def clearMarks(self):
207                 self.servicelist.clearMarks()
208
209         def doMark(self):
210                 ref = self.servicelist.getCurrent()
211                 if self.servicelist.isMarked(ref):
212                         self.servicelist.removeMarked(ref)
213                 else:
214                         self.servicelist.addMarked(ref)
215
216         def removeCurrentService(self):
217                 ref = self.servicelist.getCurrent()
218                 mutableList = self.getMutableList()
219                 if ref.valid() and mutableList is not None:
220                         if not mutableList.removeService(ref):
221                                 currentIndex = self.servicelist.getCurrentIndex()
222                                 self.servicelist.moveDown()
223                                 if self.servicelist.getCurrentIndex() == currentIndex:
224                                         currentIndex -= 1
225                                 mutableList.flushChanges() #FIXME dont flush on each single removed service
226                                 self.setRoot(self.getRoot())
227                                 self.servicelist.moveToIndex(currentIndex)
228
229         def addCurrentServiceToBouquet(self, dest):
230                 mutableList = self.getMutableList(dest)
231                 if not mutableList is None:
232                         if not mutableList.addService(self.servicelist.getCurrent()):
233                                 mutableList.flushChanges()
234                 self.close()
235
236         def toggleMoveMode(self):
237                 if self.movemode:
238                         if self.entry_marked:
239                                 self.toggleMoveMarked() # unmark current entry
240                         self.movemode = False
241                         self.pathChangedDisabled = False # re-enable path change
242                         self.mutableList.flushChanges() # FIXME add check if changes was made
243                         self.mutableList = None
244                         self.instance.setTitle(self.saved_title)
245                         self.saved_title = None
246                 else:
247                         self.mutableList = self.getMutableList()
248                         self.movemode = True
249                         self.pathChangedDisabled = True # no path change allowed in movemode
250                         self.saved_title = self.instance.getTitle()
251                         new_title = self.saved_title
252                         new_title += ' ' + _("[move mode]");
253                         self.instance.setTitle(new_title);
254
255         def handleEditCancel(self):
256                 if self.movemode: #movemode active?
257                         self.channelSelected() # unmark
258                         self.toggleMoveMode() # disable move mode
259                 elif self.bouquet_mark_edit:
260                         self.endMarkedEdit(True) # abort edit mode
261
262         def toggleMoveMarked(self):
263                 if self.entry_marked:
264                         self.servicelist.setCurrentMarked(False)
265                         self.entry_marked = False
266                 else:
267                         self.servicelist.setCurrentMarked(True)
268                         self.entry_marked = True
269
270         def doContext(self):
271                 self.session.open(ChannelContextMenu, self)
272
273 MODE_TV = 0
274 MODE_RADIO = 1
275
276 class ChannelSelectionBase(Screen):
277         def __init__(self, session):
278                 Screen.__init__(self, session)
279
280                 # this makes it much simple to implement a selectable radio or tv mode :)
281                 self.service_types_tv = '1:7:1:0:0:0:0:0:0:0:(type == 1) || (type == 17)'
282                 self.service_types_radio = '1:7:1:0:0:0:0:0:0:0:(type == 2)'
283
284                 self["key_red"] = Button(_("All"))
285                 self["key_green"] = Button(_("Satellites"))
286                 self["key_yellow"] = Button(_("Provider"))
287                 self["key_blue"] = Button(_("Favourites"))
288
289                 self["list"] = ServiceList()
290                 self.servicelist = self["list"]
291
292                 self.numericalTextInput = NumericalTextInput()
293
294                 self.servicePathTV = [ ]
295                 self.servicePathRadio = [ ]
296
297                 self.pathChangedDisabled = False
298
299                 self["ChannelSelectBaseActions"] = NumberActionMap(["ChannelSelectBaseActions", "NumberActions"],
300                         {
301                                 "showFavourites": self.showFavourites,
302                                 "showAllServices": self.showAllServices,
303                                 "showProviders": self.showProviders,
304                                 "showSatellites": self.showSatellites,
305                                 "1": self.keyNumberGlobal,
306                                 "2": self.keyNumberGlobal,
307                                 "3": self.keyNumberGlobal,
308                                 "4": self.keyNumberGlobal,
309                                 "5": self.keyNumberGlobal,
310                                 "6": self.keyNumberGlobal,
311                                 "7": self.keyNumberGlobal,
312                                 "8": self.keyNumberGlobal,
313                                 "9": self.keyNumberGlobal,
314                                 "0": self.keyNumberGlobal
315                         })
316
317         def appendDVBTypes(self, ref):
318                 path = ref.getPath()
319                 pos = path.find(' FROM BOUQUET')
320                 if pos != -1:
321                         return eServiceReference(self.service_types + path[pos:])
322                 return ref
323
324         def getBouquetNumOffset(self, bouquet):
325                 bouquet = self.appendDVBTypes(bouquet)
326                 if self.bouquet_root.getPath().find('FROM BOUQUET "bouquets.') == -1: #FIXME HACK
327                         return 0
328                 offsetCount = 0
329                 serviceHandler = eServiceCenter.getInstance()
330                 bouquetlist = serviceHandler.list(self.bouquet_root)
331                 if not bouquetlist is None:
332                         while True:
333                                 bouquetIterator = self.appendDVBTypes(bouquetlist.getNext())
334                                 if not bouquetIterator.valid() or bouquetIterator == bouquet: #end of list or bouquet found
335                                         break
336                                 if ((bouquetIterator.flags & eServiceReference.flagDirectory) != eServiceReference.flagDirectory):
337                                         continue
338                                 servicelist = serviceHandler.list(bouquetIterator)
339                                 if not servicelist is None:
340                                         while True:
341                                                 serviceIterator = servicelist.getNext()
342                                                 if not serviceIterator.valid(): #check if end of list
343                                                         break
344                                                 if serviceIterator.flags: #playable services have no flags
345                                                         continue
346                                                 offsetCount += 1
347                 return offsetCount
348
349         def recallBouquetMode(self):
350                 if self.mode == MODE_TV:
351                         self.service_types = self.service_types_tv
352                         if currentConfigSelectionElement(config.usage.multibouquet) == "yes":
353                                 self.bouquet_rootstr = '1:7:1:0:0:0:0:0:0:0:(type == 1) FROM BOUQUET "bouquets.tv" ORDER BY bouquet'
354                         else:
355                                 self.bouquet_rootstr = '%s FROM BOUQUET "userbouquet.favourites.tv" ORDER BY bouquet'%(self.service_types)
356                 else:
357                         self.service_types = self.service_types_radio
358                         if currentConfigSelectionElement(config.usage.multibouquet) == "yes":
359                                 self.bouquet_rootstr = '1:7:1:0:0:0:0:0:0:0:(type == 1) FROM BOUQUET "bouquets.radio" ORDER BY bouquet'
360                         else:
361                                 self.bouquet_rootstr = '%s FROM BOUQUET "userbouquet.favourites.radio" ORDER BY bouquet'%(self.service_types)
362                 self.bouquet_root = eServiceReference(self.bouquet_rootstr)
363
364         def setTvMode(self):
365                 title = self.instance.getTitle()
366                 pos = title.find(" (")
367                 if pos != -1:
368                         title = title[:pos]
369                 title += " (TV)"
370                 self.instance.setTitle(title)
371                 self.mode = MODE_TV
372                 self.recallBouquetMode()
373
374         def setRadioMode(self):
375                 title = self.instance.getTitle()
376                 pos = title.find(" (")
377                 if pos != -1:
378                         title = title[:pos]
379                 title += " (Radio)"
380                 self.instance.setTitle(title)
381                 self.mode = MODE_RADIO
382                 self.recallBouquetMode()
383
384         def setRoot(self, root, justSet=False):
385                 path = root.getPath()
386                 inBouquetRootList = path.find('FROM BOUQUET "bouquets.') != -1 #FIXME HACK
387                 pos = path.find(' FROM BOUQUET')
388                 isBouquet = pos != -1
389                 if not inBouquetRootList and isBouquet:
390                         self.servicelist.setMode(ServiceList.MODE_FAVOURITES)
391                         self.servicelist.setNumberOffset(self.getBouquetNumOffset(root))
392                         refstr = self.service_types + path[pos:]
393                         root = eServiceReference(refstr)
394                 else:
395                         self.servicelist.setMode(ServiceList.MODE_NORMAL)
396                 self.servicelist.setRoot(root, justSet)
397
398         def moveUp(self):
399                 self.servicelist.moveUp()
400
401         def moveDown(self):
402                 self.servicelist.moveDown()
403
404         def clearPath(self):
405                 if self.mode == MODE_RADIO:
406                         self.servicePathRadio = [ ]
407                 else:
408                         self.servicePathTV = [ ]
409
410         def enterPath(self, ref, justSet=False):
411                 if self.mode == MODE_RADIO:
412                         self.servicePathRadio.append(ref)
413                 else:
414                         self.servicePathTV.append(ref)
415                 self.setRoot(ref, justSet)
416
417         def pathUp(self, justSet=False):
418                 if self.mode == MODE_TV:
419                         prev = self.servicePathTV.pop()
420                         length = len(self.servicePathTV)
421                         if length:
422                                 current = self.servicePathTV[length-1]
423                 else:
424                         prev = self.servicePathRadio.pop()
425                         length = len(self.servicePathRadio)
426                         if length:
427                                 current = self.servicePathRadio[length-1]
428                 self.setRoot(current, justSet)
429                 if not justSet:
430                         self.setCurrentSelection(prev)
431                 return prev
432
433         def isBasePathEqual(self, ref):
434                 if self.mode == MODE_RADIO and len(self.servicePathRadio) > 1 and self.servicePathRadio[0] == ref:
435                         return True
436                 elif self.mode == MODE_TV and len(self.servicePathTV) > 1 and self.servicePathTV[0] == ref:
437                         return True
438                 return False
439
440         def isPrevPathEqual(self, ref):
441                 path = self.servicePathRadio
442                 if self.mode == MODE_TV:
443                         path = self.servicePathTV
444                 length = len(path)
445                 if length > 1 and path[length-2] == ref:
446                         return True
447                 return False
448
449         def preEnterPath(self, refstr):
450                 return False
451
452         def showAllServices(self):
453                 if not self.pathChangedDisabled:
454                         refstr = '%s ORDER BY name'%(self.service_types)
455                         if not self.preEnterPath(refstr):
456                                 ref = eServiceReference(refstr)
457                                 currentRoot = self.getRoot()
458                                 if currentRoot is None or currentRoot != ref:
459                                         self.clearPath()
460                                         self.enterPath(ref)
461
462         def showSatellites(self):
463                 if not self.pathChangedDisabled:
464                         refstr = '%s FROM SATELLITES ORDER BY satellitePosition'%(self.service_types)
465                         if not self.preEnterPath(refstr):
466                                 ref = eServiceReference(refstr)
467                                 justSet=False
468                                 prev = None
469
470                                 if self.isBasePathEqual(ref):
471                                         if self.isPrevPathEqual(ref):
472                                                 justSet=True
473                                         prev = self.pathUp(justSet)
474                                 else:
475                                         currentRoot = self.getRoot()
476                                         if currentRoot is None or currentRoot != ref:
477                                                 justSet=True
478                                                 self.clearPath()
479                                                 self.enterPath(ref, True)
480                                 if justSet:
481                                         serviceHandler = eServiceCenter.getInstance()
482                                         servicelist = serviceHandler.list(ref)
483                                         if not servicelist is None:
484                                                 while True:
485                                                         service = servicelist.getNext()
486                                                         if not service.valid(): #check if end of list
487                                                                 break
488                                                         orbpos = service.getData(4) >> 16
489                                                         if service.getPath().find("FROM PROVIDER") != -1:
490                                                                 service_name = _("Providers")
491                                                         else:
492                                                                 service_name = _("Services")
493                                                         try:
494                                                                 service_name += str(' - %s'%(nimmanager.getSatDescription(orbpos)))
495                                                                 service.setName(service_name) # why we need this cast?
496                                                         except:
497                                                                 if orbpos > 1800: # west
498                                                                         service.setName("%s (%3.1f" + _("W") + ")" %(str, (0 - (orbpos - 3600)) / 10.0))
499                                                                 else:
500                                                                         service.setName("%s (%3.1f" + _("E") + ")" % (str, orbpos / 10.0))
501                                                         self.servicelist.addService(service)
502                                                         self.servicelist.finishFill()
503                                                         if prev is not None:
504                                                                 self.setCurrentSelection(prev)
505
506         def showProviders(self):
507                 if not self.pathChangedDisabled:
508                         refstr = '%s FROM PROVIDERS ORDER BY name'%(self.service_types)
509                         if not self.preEnterPath(refstr):
510                                 ref = eServiceReference(refstr)
511                                 if self.isBasePathEqual(ref):
512                                         self.pathUp()
513                                 else:
514                                         currentRoot = self.getRoot()
515                                         if currentRoot is None or currentRoot != ref:
516                                                 self.clearPath()
517                                                 self.enterPath(ref)
518
519         def showFavourites(self):
520                 if not self.pathChangedDisabled:
521                         if not self.preEnterPath(self.bouquet_rootstr):
522                                 if self.isBasePathEqual(self.bouquet_root):
523                                         self.pathUp()
524                                 else:
525                                         currentRoot = self.getRoot()
526                                         if currentRoot is None or currentRoot != self.bouquet_root:
527                                                 self.clearPath()
528                                                 self.enterPath(self.bouquet_root)
529
530         def keyNumberGlobal(self, number):
531                 char = self.numericalTextInput.getKey(number)
532                 self.servicelist.moveToChar(char)
533
534         def getRoot(self):
535                 return self.servicelist.getRoot()
536
537         def getCurrentSelection(self):
538                 return self.servicelist.getCurrent()
539
540         def setCurrentSelection(self, service):
541                 servicepath = service.getPath()
542                 pos = servicepath.find(" FROM BOUQUET")
543                 if pos != -1:
544                         if self.mode == MODE_TV:
545                                 servicepath = '(type == 1)' + servicepath[pos:]
546                         else:
547                                 servicepath = '(type == 2)' + servicepath[pos:]
548                         service.setPath(servicepath)
549                 self.servicelist.setCurrent(service)
550
551         def getBouquetList(self):
552                 serviceCount=0
553                 bouquets = [ ]
554                 serviceHandler = eServiceCenter.getInstance()
555                 list = serviceHandler.list(self.bouquet_root)
556                 if not list is None:
557                         while True:
558                                 s = list.getNext()
559                                 if not s.valid():
560                                         break
561                                 if ((s.flags & eServiceReference.flagDirectory) == eServiceReference.flagDirectory):
562                                         info = serviceHandler.info(s)
563                                         if not info is None:
564                                                 bouquets.append((info.getName(s), s))
565                                 else:
566                                         serviceCount += 1
567                         if len(bouquets) == 0 and serviceCount > 0:
568                                 info = serviceHandler.info(self.bouquet_root)
569                                 if not info is None:
570                                         bouquets.append((info.getName(self.bouquet_root), self.bouquet_root))
571                         return bouquets
572                 return None
573
574 class ChannelSelection(ChannelSelectionBase, ChannelSelectionEdit, ChannelSelectionEPG):
575         def __init__(self, session):
576                 ChannelSelectionBase.__init__(self,session)
577                 ChannelSelectionEdit.__init__(self)
578                 ChannelSelectionEPG.__init__(self)
579
580                 #config for lastservice
581                 config.tv = ConfigSubsection();
582                 config.tv.lastservice = configElement("config.tv.lastservice", configText, "", 0);
583                 config.tv.lastroot = configElement("config.tv.lastroot", configText, "", 0);
584                 config.tv.prevservice = configElement("config.tv.prevservice", configText, "", 0);
585                 config.tv.prevroot = configElement("config.tv.prevroot", configText, "", 0);
586
587                 self["actions"] = ActionMap(["OkCancelActions"],
588                         {
589                                 "cancel": self.cancel,
590                                 "ok": self.channelSelected,
591                         })
592                 self.onShown.append(self.onShow)
593
594                 self.lastChannelRootTimer = eTimer()
595                 self.lastChannelRootTimer.timeout.get().append(self.onCreate)
596                 self.lastChannelRootTimer.start(100,True)
597
598         def onCreate(self):
599                 self.setTvMode()
600                 self.servicePathTV = [ ]
601                 self.restoreRoot()
602                 lastservice=eServiceReference(config.tv.lastservice.value)
603                 if lastservice.valid():
604                         self.setCurrentSelection(lastservice)
605                         self.session.nav.playService(lastservice)
606
607         def onShow(self):
608                 self.recallBouquetMode()
609                 ref = self.session.nav.getCurrentlyPlayingServiceReference()
610                 if ref is not None and ref.valid() and ref.getPath() == "":
611                         self.servicelist.setPlayableIgnoreService(ref)
612                 else:
613                         self.servicelist.setPlayableIgnoreService(eServiceReference())
614
615         def channelSelected(self):
616                 ref = self.getCurrentSelection()
617                 if self.movemode:
618                         self.toggleMoveMarked()
619                 elif (ref.flags & 7) == 7:
620                         self.enterPath(ref)
621                 elif self.bouquet_mark_edit:
622                         self.doMark()
623                 else:
624                         self.zap()
625                         self.close(ref)
626
627         #called from infoBar and channelSelected
628         def zap(self):
629                 ref = self.session.nav.getCurrentlyPlayingServiceReference()
630                 if ref is None or ref != self.getCurrentSelection():
631                         self.session.nav.playService(self.getCurrentSelection())
632                 self.saveRoot()
633                 self.saveChannel()
634
635         def saveRoot(self):
636                 path = ''
637                 for i in self.servicePathTV:
638                         path += i.toString()
639                         path += ';'
640                 if config.tv.prevroot.value != config.tv.lastroot.value:
641                         config.tv.prevroot.value = config.tv.lastroot.value
642                         config.tv.prevroot.save()
643                 if len(path) and path != config.tv.lastroot.value:
644                         config.tv.lastroot.value = path
645                         config.tv.lastroot.save()
646
647         def restoreRoot(self):
648                 self.servicePathTV = [ ]
649                 re = compile('.+?;')
650                 tmp = re.findall(config.tv.lastroot.value)
651                 cnt = 0
652                 for i in tmp:
653                         self.servicePathTV.append(eServiceReference(i[:len(i)-1]))
654                         cnt += 1
655                 if cnt:
656                         path = self.servicePathTV.pop()
657                         self.enterPath(path)
658                 else:
659                         self.showFavourites()
660                         self.saveRoot()
661
662         def preEnterPath(self, refstr):
663                 if len(self.servicePathTV) and self.servicePathTV[0] != eServiceReference(refstr):
664                         pathstr = config.tv.lastroot.value
665                         if pathstr is not None and pathstr.find(refstr) == 0:
666                                 self.restoreRoot()
667                                 lastservice=eServiceReference(config.tv.lastservice.value)
668                                 if lastservice is not None:
669                                         self.setCurrentSelection(lastservice)
670                                 return True
671                 return False
672
673         def saveChannel(self):
674                 ref = self.session.nav.getCurrentlyPlayingServiceReference()
675                 if ref is not None:
676                         refstr = ref.toString()
677                 else:
678                         refstr = ""
679                 if refstr != config.tv.lastservice.value:
680                         config.tv.prevservice.value = config.tv.lastservice.value
681                         config.tv.prevservice.save()
682                         config.tv.lastservice.value = refstr
683                         config.tv.lastservice.save()
684
685         def recallPrevService(self):
686                 if len(config.tv.prevservice.value) and len(config.tv.prevroot.value):
687                         if config.tv.lastroot.value != config.tv.prevroot.value:
688                                 tmp = config.tv.lastroot.value
689                                 config.tv.lastroot.value = config.tv.prevroot.value
690                                 config.tv.lastroot.save()
691                                 config.tv.prevroot.value = tmp
692                                 config.tv.prevroot.save()
693                                 self.restoreRoot()
694                         if config.tv.lastservice.value != config.tv.prevservice.value:
695                                 tmp = config.tv.lastservice.value
696                                 config.tv.lastservice.value = config.tv.prevservice.value
697                                 config.tv.lastservice.save()
698                                 config.tv.prevservice.value = tmp
699                                 config.tv.prevservice.save()
700                                 lastservice=eServiceReference(config.tv.lastservice.value)
701                                 self.session.nav.playService(lastservice)
702                                 self.setCurrentSelection(lastservice)
703
704         def cancel(self):
705                 self.close(None)
706                 self.restoreRoot()
707                 lastservice=eServiceReference(config.tv.lastservice.value)
708                 if lastservice.valid() and self.getCurrentSelection() != lastservice:
709                         self.setCurrentSelection(lastservice)
710
711 from Screens.InfoBarGenerics import InfoBarEvent, InfoBarServiceName, InfoBarInstantRecord
712
713 class RadioInfoBar(Screen, InfoBarEvent, InfoBarServiceName, InfoBarInstantRecord):
714         def __init__(self, session):
715                 Screen.__init__(self, session)
716                 InfoBarEvent.__init__(self)
717                 InfoBarServiceName.__init__(self)
718                 InfoBarInstantRecord.__init__(self)
719                 self["Clock"] = Clock()
720
721 class ChannelSelectionRadio(ChannelSelectionBase, ChannelSelectionEdit, ChannelSelectionEPG):
722         def __init__(self, session):
723                 ChannelSelectionBase.__init__(self, session)
724                 ChannelSelectionEdit.__init__(self)
725                 ChannelSelectionEPG.__init__(self)
726
727                 config.radio = ConfigSubsection();
728                 config.radio.lastservice = configElement("config.radio.lastservice", configText, "", 0);
729                 config.radio.lastroot = configElement("config.radio.lastroot", configText, "", 0);
730                 self.onLayoutFinish.append(self.onCreate)
731
732                 self.info = session.instantiateDialog(RadioInfoBar)
733
734                 self["actions"] = ActionMap(["OkCancelActions", "TvRadioActions"],
735                         {
736                                 "keyTV": self.closeRadio,
737                                 "keyRadio": self.closeRadio,
738                                 "cancel": self.closeRadio,
739                                 "ok": self.channelSelected,
740                         })
741
742         def saveRoot(self):
743                 path = ''
744                 for i in self.servicePathRadio:
745                         path += i.toString()
746                         path += ';'
747                 if len(path) and path != config.radio.lastroot.value:
748                         config.radio.lastroot.value = path
749                         config.radio.lastroot.save()
750
751         def restoreRoot(self):
752                 self.servicePathRadio = [ ]
753                 re = compile('.+?;')
754                 tmp = re.findall(config.radio.lastroot.value)
755                 cnt = 0
756                 for i in tmp:
757                         self.servicePathRadio.append(eServiceReference(i[:len(i)-1]))
758                         cnt += 1
759                 if cnt:
760                         path = self.servicePathRadio.pop()
761                         self.enterPath(path)
762                 else:
763                         self.showFavourites()
764                         self.saveRoot()
765
766         def preEnterPath(self, refstr):
767                 if len(self.servicePathRadio) and self.servicePathRadio[0] != eServiceReference(refstr):
768                         pathstr = config.radio.lastroot.value
769                         if pathstr is not None and pathstr.find(refstr) == 0:
770                                 self.restoreRoot()
771                                 lastservice=eServiceReference(config.radio.lastservice.value)
772                                 if lastservice is not None:
773                                         self.setCurrentSelection(lastservice)
774                                 return True
775                 return False
776
777         def onCreate(self):
778                 self.setRadioMode()
779                 self.restoreRoot()
780                 lastservice=eServiceReference(config.radio.lastservice.value)
781                 if lastservice.valid():
782                         self.servicelist.setCurrent(lastservice)
783                         self.session.nav.playService(lastservice)
784                         self.servicelist.setPlayableIgnoreService(lastservice)
785                 self.info.instance.show()
786
787         def channelSelected(self): # just return selected service
788                 ref = self.getCurrentSelection()
789                 if self.movemode:
790                         self.toggleMoveMarked()
791                 elif (ref.flags & 7) == 7:
792                         self.enterPath(ref)
793                 elif self.bouquet_mark_edit:
794                         self.doMark()
795                 else:
796                         playingref = self.session.nav.getCurrentlyPlayingServiceReference()
797                         if playingref is None or playingref != ref:
798                                 self.session.nav.playService(ref)
799                                 self.servicelist.setPlayableIgnoreService(ref)
800                                 config.radio.lastservice.value = ref.toString()
801                                 config.radio.lastservice.save()
802                         self.saveRoot()
803
804         def closeRadio(self):
805                 self.info.instance.hide()
806                 #set previous tv service
807                 lastservice=eServiceReference(config.tv.lastservice.value)
808                 self.session.nav.playService(lastservice)
809                 self.close(None)
810
811 class SimpleChannelSelection(ChannelSelectionBase):
812         def __init__(self, session, title):
813                 ChannelSelectionBase.__init__(self, session)
814                 self.title = title
815                 self.onShown.append(self.onExecCallback)
816
817                 self["actions"] = ActionMap(["OkCancelActions", "TvRadioActions"],
818                         {
819                                 "cancel": self.cancel,
820                                 "ok": self.channelSelected,
821                                 "keyRadio": self.setModeRadio,
822                                 "keyTV": self.setModeTv,
823                         })
824
825         def onExecCallback(self):
826                 self.session.currentDialog.instance.setTitle(self.title)
827                 self.setModeTv()
828
829         def channelSelected(self): # just return selected service
830                 ref = self.getCurrentSelection()
831                 if (ref.flags & 7) == 7:
832                         self.enterPath(ref)
833                 else:
834                         ref = self.getCurrentSelection()
835                         self.close(ref)
836
837         def setModeTv(self):
838                 self.setTvMode()
839                 self.showFavourites()
840
841         def setModeRadio(self):
842                 self.setRadioMode()
843                 self.showFavourites()