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