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