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