fix no more working color buttons in channellist after disable movemode
[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                         print "MODE_TV"
420                         prev = self.servicePathTV.pop()
421                         print "prev", prev.toString()
422                         length = len(self.servicePathTV)
423                         print "length", length
424                         if length:
425                                 current = self.servicePathTV[length-1]
426                                 print "current", current.toString()
427                 else:
428                         prev = self.servicePathRadio.pop()
429                         length = len(self.servicePathRadio)
430                         if length:
431                                 current = self.servicePathRadio[length-1]
432                 self.setRoot(current, justSet)
433                 if not justSet:
434                         print "setCurrentSelection prev"
435                         self.setCurrentSelection(prev)
436                 return prev
437
438         def isBasePathEqual(self, ref):
439                 if self.mode == MODE_RADIO and len(self.servicePathRadio) > 1 and self.servicePathRadio[0] == ref:
440                         return True
441                 elif self.mode == MODE_TV and len(self.servicePathTV) > 1 and self.servicePathTV[0] == ref:
442                         return True
443                 return False
444
445         def isPrevPathEqual(self, ref):
446                 path = self.servicePathRadio
447                 if self.mode == MODE_TV:
448                         path = self.servicePathTV
449                 length = len(path)
450                 if length > 1 and path[length-2] == ref:
451                         return True
452                 return False
453
454         def preEnterPath(self, refstr):
455                 return False
456
457         def showAllServices(self):
458                 if not self.pathChangedDisabled:
459                         refstr = '%s ORDER BY name'%(self.service_types)
460                         if not self.preEnterPath(refstr):
461                                 ref = eServiceReference(refstr)
462                                 currentRoot = self.getRoot()
463                                 if currentRoot is None or currentRoot != ref:
464                                         self.clearPath()
465                                         self.enterPath(ref)
466
467         def showSatellites(self):
468                 if not self.pathChangedDisabled:
469                         refstr = '%s FROM SATELLITES ORDER BY satellitePosition'%(self.service_types)
470                         if not self.preEnterPath(refstr):
471                                 ref = eServiceReference(refstr)
472                                 justSet=False
473                                 prev = None
474
475                                 if self.isBasePathEqual(ref):
476                                         if self.isPrevPathEqual(ref):
477                                                 justSet=True
478                                         prev = self.pathUp(justSet)
479                                 else:
480                                         currentRoot = self.getRoot()
481                                         if currentRoot is None or currentRoot != ref:
482                                                 justSet=True
483                                                 self.clearPath()
484                                                 self.enterPath(ref, True)
485                                 if justSet:
486                                         serviceHandler = eServiceCenter.getInstance()
487                                         servicelist = serviceHandler.list(ref)
488                                         if not servicelist is None:
489                                                 while True:
490                                                         service = servicelist.getNext()
491                                                         if not service.valid(): #check if end of list
492                                                                 break
493                                                         orbpos = service.getData(4) >> 16
494                                                         if service.getPath().find("FROM PROVIDER") != -1:
495                                                                 service_name = _("Providers")
496                                                         else:
497                                                                 service_name = _("Services")
498                                                         try:
499                                                                 service_name += str(' - %s'%(nimmanager.getSatDescription(orbpos)))
500                                                                 service.setName(service_name) # why we need this cast?
501                                                         except:
502                                                                 if orbpos > 1800: # west
503                                                                         service.setName("%s (%3.1f" + _("W") + ")" %(str, (0 - (orbpos - 3600)) / 10.0))
504                                                                 else:
505                                                                         service.setName("%s (%3.1f" + _("E") + ")" % (str, orbpos / 10.0))
506                                                         self.servicelist.addService(service)
507                                                         self.servicelist.finishFill()
508                                                         if prev is not None:
509                                                                 self.setCurrentSelection(prev)
510
511         def showProviders(self):
512                 if not self.pathChangedDisabled:
513                         refstr = '%s FROM PROVIDERS ORDER BY name'%(self.service_types)
514                         if not self.preEnterPath(refstr):
515                                 ref = eServiceReference(refstr)
516                                 if self.isBasePathEqual(ref):
517                                         self.pathUp()
518                                 else:
519                                         currentRoot = self.getRoot()
520                                         if currentRoot is None or currentRoot != ref:
521                                                 self.clearPath()
522                                                 self.enterPath(ref)
523
524         def showFavourites(self):
525                 if not self.pathChangedDisabled:
526                         if not self.preEnterPath(self.bouquet_rootstr):
527                                 if self.isBasePathEqual(self.bouquet_root):
528                                         print "basepath equal"
529                                         self.pathUp()
530                                 else:
531                                         print "basepath not equal"
532                                         currentRoot = self.getRoot()
533                                         if currentRoot is None or currentRoot != self.bouquet_root:
534                                                 self.clearPath()
535                                                 self.enterPath(self.bouquet_root)
536
537         def keyNumberGlobal(self, number):
538                 char = self.numericalTextInput.getKey(number)
539                 print "You pressed number " + str(number)
540                 print "You would go to character " + str(char)
541                 self.servicelist.moveToChar(char)
542
543         def getRoot(self):
544                 return self.servicelist.getRoot()
545
546         def getCurrentSelection(self):
547                 return self.servicelist.getCurrent()
548
549         def setCurrentSelection(self, service):
550                 servicepath = service.getPath()
551                 pos = servicepath.find(" FROM BOUQUET")
552                 if pos != -1:
553                         if self.mode == MODE_TV:
554                                 servicepath = '(type == 1)' + servicepath[pos:]
555                         else:
556                                 servicepath = '(type == 2)' + servicepath[pos:]
557                         service.setPath(servicepath)
558                 self.servicelist.setCurrent(service)
559
560         def getBouquetList(self):
561                 serviceCount=0
562                 bouquets = [ ]
563                 serviceHandler = eServiceCenter.getInstance()
564                 list = serviceHandler.list(self.bouquet_root)
565                 if not list is None:
566                         while True:
567                                 s = list.getNext()
568                                 if not s.valid():
569                                         break
570                                 if ((s.flags & eServiceReference.flagDirectory) == eServiceReference.flagDirectory):
571                                         info = serviceHandler.info(s)
572                                         if not info is None:
573                                                 bouquets.append((info.getName(s), s))
574                                 else:
575                                         serviceCount += 1
576                         if len(bouquets) == 0 and serviceCount > 0:
577                                 info = serviceHandler.info(self.bouquet_root)
578                                 if not info is None:
579                                         bouquets.append((info.getName(self.bouquet_root), self.bouquet_root))
580                         return bouquets
581                 return None
582
583 class ChannelSelection(ChannelSelectionBase, ChannelSelectionEdit, ChannelSelectionEPG):
584         def __init__(self, session):
585                 ChannelSelectionBase.__init__(self,session)
586                 ChannelSelectionEdit.__init__(self)
587                 ChannelSelectionEPG.__init__(self)
588
589                 #config for lastservice
590                 config.tv = ConfigSubsection();
591                 config.tv.lastservice = configElement("config.tv.lastservice", configText, "", 0);
592                 config.tv.lastroot = configElement("config.tv.lastroot", configText, "", 0);
593                 config.tv.prevservice = configElement("config.tv.prevservice", configText, "", 0);
594                 config.tv.prevroot = configElement("config.tv.prevroot", configText, "", 0);
595
596                 self["actions"] = ActionMap(["OkCancelActions"],
597                         {
598                                 "cancel": self.cancel,
599                                 "ok": self.channelSelected,
600                         })
601                 self.onShown.append(self.onShow)
602
603                 self.lastChannelRootTimer = eTimer()
604                 self.lastChannelRootTimer.timeout.get().append(self.onCreate)
605                 self.lastChannelRootTimer.start(100,True)
606
607         def onCreate(self):
608                 self.setTvMode()
609                 self.servicePathTV = [ ]
610                 self.restoreRoot()
611                 lastservice=eServiceReference(config.tv.lastservice.value)
612                 if lastservice.valid():
613                         self.setCurrentSelection(lastservice)
614                         self.session.nav.playService(lastservice)
615
616         def onShow(self):
617                 self.recallBouquetMode()
618                 ref = self.session.nav.getCurrentlyPlayingServiceReference()
619                 if ref is not None and ref.valid() and ref.getPath() == "":
620                         self.servicelist.setPlayableIgnoreService(ref)
621                 else:
622                         self.servicelist.setPlayableIgnoreService(eServiceReference())
623
624         def channelSelected(self):
625                 ref = self.getCurrentSelection()
626                 if self.movemode:
627                         self.toggleMoveMarked()
628                 elif (ref.flags & 7) == 7:
629                         self.enterPath(ref)
630                 elif self.bouquet_mark_edit:
631                         self.doMark()
632                 else:
633                         self.zap()
634                         self.close(ref)
635
636         #called from infoBar and channelSelected
637         def zap(self):
638                 ref = self.session.nav.getCurrentlyPlayingServiceReference()
639                 if ref is None or ref != self.getCurrentSelection():
640                         self.session.nav.playService(self.getCurrentSelection())
641                 self.saveRoot()
642                 self.saveChannel()
643
644         def saveRoot(self):
645                 path = ''
646                 for i in self.servicePathTV:
647                         path += i.toString()
648                         path += ';'
649                 if config.tv.prevroot.value != config.tv.lastroot.value:
650                         config.tv.prevroot.value = config.tv.lastroot.value
651                         config.tv.prevroot.save()
652                 if len(path) and path != config.tv.lastroot.value:
653                         config.tv.lastroot.value = path
654                         config.tv.lastroot.save()
655
656         def restoreRoot(self):
657                 self.servicePathTV = [ ]
658                 re = compile('.+?;')
659                 tmp = re.findall(config.tv.lastroot.value)
660                 cnt = 0
661                 for i in tmp:
662                         self.servicePathTV.append(eServiceReference(i[:len(i)-1]))
663                         cnt += 1
664                 if cnt:
665                         path = self.servicePathTV.pop()
666                         self.enterPath(path)
667                 else:
668                         self.showFavourites()
669                         self.saveRoot()
670
671         def preEnterPath(self, refstr):
672                 if len(self.servicePathTV) and self.servicePathTV[0] != eServiceReference(refstr):
673                         pathstr = config.tv.lastroot.value
674                         if pathstr is not None and pathstr.find(refstr) == 0:
675                                 self.restoreRoot()
676                                 lastservice=eServiceReference(config.tv.lastservice.value)
677                                 if lastservice is not None:
678                                         self.setCurrentSelection(lastservice)
679                                 return True
680                 return False
681
682         def saveChannel(self):
683                 ref = self.session.nav.getCurrentlyPlayingServiceReference()
684                 if ref is not None:
685                         refstr = ref.toString()
686                 else:
687                         refstr = ""
688                 if refstr != config.tv.lastservice.value:
689                         config.tv.prevservice.value = config.tv.lastservice.value
690                         config.tv.prevservice.save()
691                         config.tv.lastservice.value = refstr
692                         config.tv.lastservice.save()
693
694         def recallPrevService(self):
695                 if len(config.tv.prevservice.value) and len(config.tv.prevroot.value):
696                         if config.tv.lastroot.value != config.tv.prevroot.value:
697                                 tmp = config.tv.lastroot.value
698                                 config.tv.lastroot.value = config.tv.prevroot.value
699                                 config.tv.lastroot.save()
700                                 config.tv.prevroot.value = tmp
701                                 config.tv.prevroot.save()
702                                 self.restoreRoot()
703                         if config.tv.lastservice.value != config.tv.prevservice.value:
704                                 tmp = config.tv.lastservice.value
705                                 config.tv.lastservice.value = config.tv.prevservice.value
706                                 config.tv.lastservice.save()
707                                 config.tv.prevservice.value = tmp
708                                 config.tv.prevservice.save()
709                                 lastservice=eServiceReference(config.tv.lastservice.value)
710                                 self.session.nav.playService(lastservice)
711                                 self.setCurrentSelection(lastservice)
712
713         def cancel(self):
714                 self.close(None)
715                 self.restoreRoot()
716                 lastservice=eServiceReference(config.tv.lastservice.value)
717                 if lastservice.valid() and self.getCurrentSelection() != lastservice:
718                         self.setCurrentSelection(lastservice)
719
720 from Screens.InfoBarGenerics import InfoBarEvent, InfoBarServiceName, InfoBarInstantRecord
721
722 class RadioInfoBar(Screen, InfoBarEvent, InfoBarServiceName, InfoBarInstantRecord):
723         def __init__(self, session):
724                 Screen.__init__(self, session)
725                 InfoBarEvent.__init__(self)
726                 InfoBarServiceName.__init__(self)
727                 InfoBarInstantRecord.__init__(self)
728                 self["Clock"] = Clock()
729
730 class ChannelSelectionRadio(ChannelSelectionBase, ChannelSelectionEdit, ChannelSelectionEPG):
731         def __init__(self, session):
732                 ChannelSelectionBase.__init__(self, session)
733                 ChannelSelectionEdit.__init__(self)
734                 ChannelSelectionEPG.__init__(self)
735
736                 config.radio = ConfigSubsection();
737                 config.radio.lastservice = configElement("config.radio.lastservice", configText, "", 0);
738                 config.radio.lastroot = configElement("config.radio.lastroot", configText, "", 0);
739                 self.onLayoutFinish.append(self.onCreate)
740
741                 self.info = session.instantiateDialog(RadioInfoBar)
742
743                 self["actions"] = ActionMap(["OkCancelActions", "TvRadioActions"],
744                         {
745                                 "keyTV": self.closeRadio,
746                                 "keyRadio": self.closeRadio,
747                                 "cancel": self.closeRadio,
748                                 "ok": self.channelSelected,
749                         })
750
751         def saveRoot(self):
752                 path = ''
753                 for i in self.servicePathRadio:
754                         path += i.toString()
755                         path += ';'
756                 if len(path) and path != config.radio.lastroot.value:
757                         config.radio.lastroot.value = path
758                         config.radio.lastroot.save()
759
760         def restoreRoot(self):
761                 self.servicePathRadio = [ ]
762                 re = compile('.+?;')
763                 tmp = re.findall(config.radio.lastroot.value)
764                 cnt = 0
765                 for i in tmp:
766                         self.servicePathRadio.append(eServiceReference(i[:len(i)-1]))
767                         cnt += 1
768                 if cnt:
769                         path = self.servicePathRadio.pop()
770                         self.enterPath(path)
771                 else:
772                         self.showFavourites()
773                         self.saveRoot()
774
775         def preEnterPath(self, refstr):
776                 if len(self.servicePathRadio) and self.servicePathRadio[0] != eServiceReference(refstr):
777                         pathstr = config.radio.lastroot.value
778                         if pathstr is not None and pathstr.find(refstr) == 0:
779                                 self.restoreRoot()
780                                 lastservice=eServiceReference(config.radio.lastservice.value)
781                                 if lastservice is not None:
782                                         self.setCurrentSelection(lastservice)
783                                 return True
784                 return False
785
786         def onCreate(self):
787                 self.setRadioMode()
788                 self.restoreRoot()
789                 lastservice=eServiceReference(config.radio.lastservice.value)
790                 if lastservice.valid():
791                         self.servicelist.setCurrent(lastservice)
792                         self.session.nav.playService(lastservice)
793                         self.servicelist.setPlayableIgnoreService(lastservice)
794                 self.info.instance.show()
795
796         def channelSelected(self): # just return selected service
797                 ref = self.getCurrentSelection()
798                 if self.movemode:
799                         self.toggleMoveMarked()
800                 elif (ref.flags & 7) == 7:
801                         self.enterPath(ref)
802                 elif self.bouquet_mark_edit:
803                         self.doMark()
804                 else:
805                         playingref = self.session.nav.getCurrentlyPlayingServiceReference()
806                         if playingref is None or playingref != ref:
807                                 self.session.nav.playService(ref)
808                                 self.servicelist.setPlayableIgnoreService(ref)
809                                 config.radio.lastservice.value = ref.toString()
810                                 config.radio.lastservice.save()
811                         self.saveRoot()
812
813         def closeRadio(self):
814                 self.info.instance.hide()
815                 #set previous tv service
816                 lastservice=eServiceReference(config.tv.lastservice.value)
817                 self.session.nav.playService(lastservice)
818                 self.close(None)
819
820 class SimpleChannelSelection(ChannelSelectionBase):
821         def __init__(self, session, title):
822                 ChannelSelectionBase.__init__(self, session)
823                 self.title = title
824                 self.onShown.append(self.onExecCallback)
825
826                 self["actions"] = ActionMap(["OkCancelActions", "TvRadioActions"],
827                         {
828                                 "cancel": self.cancel,
829                                 "ok": self.channelSelected,
830                                 "keyRadio": self.setModeRadio,
831                                 "keyTV": self.setModeTv,
832                         })
833
834         def onExecCallback(self):
835                 self.session.currentDialog.instance.setTitle(self.title)
836                 self.setModeTv()
837
838         def channelSelected(self): # just return selected service
839                 ref = self.getCurrentSelection()
840                 if (ref.flags & 7) == 7:
841                         self.enterPath(ref)
842                 else:
843                         ref = self.getCurrentSelection()
844                         self.close(ref)
845
846         def setModeTv(self):
847                 self.setTvMode()
848                 self.showFavourites()
849
850         def setModeRadio(self):
851                 self.setRadioMode()
852                 self.showFavourites()