changing service in timer edit is now possible
[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 ChannelSelection(Screen):
102         def lastService(self):
103                 self.lastServiceTimer.stop()
104                 #zap to last running tv service
105                 self.setRoot(eServiceReference(config.tv.lastroot.value))
106                 self.session.nav.playService(eServiceReference(config.tv.lastservice.value))
107
108         def __init__(self, session):
109                 Screen.__init__(self, session)
110
111                 #config for lastservice
112                 config.tv = ConfigSubsection();
113                 config.tv.lastservice = configElement("config.tv.lastservice", configText, "", 0);
114                 config.tv.lastroot = configElement("config.tv.lastroot", configText, "", 0);
115
116                 self.entry_marked = False
117                 self.movemode = False
118                 self.bouquet_mark_edit = False
119                 #self.bouquet_root = eServiceReference('1:7:1:0:0:0:0:0:0:0:(type == 1) FROM BOUQUET "bouquets.tv" ORDER BY bouquet')
120                 self.bouquet_root = eServiceReference('1:7:1:0:0:0:0:0:0:0:(type == 1) FROM BOUQUET "userbouquet.favourites.tv" ORDER BY bouquet')
121                 self.mutableList = None
122
123                 self.__marked = [ ]
124
125                 self["key_red"] = Button("All")
126                 self["key_green"] = Button("Provider")
127                 self["key_yellow"] = Button("Satellite")
128                 self["key_blue"] = Button("Favourites")
129
130                 self["list"] = ServiceList()
131                 self.servicelist = self["list"]
132
133                 if config.tv.lastroot.value == "":
134                         self.servicelist.setRoot(eServiceReference("""1:0:1:0:0:0:0:0:0:0:(type == 1)"""))
135                 #self["okbutton"] = Button("ok", [self.channelSelected])
136
137                 self.lastServiceTimer = eTimer()
138                 self.lastServiceTimer.timeout.get().append(self.lastService)
139                 self.lastServiceTimer.start(100)
140
141                 class ChannelActionMap(ActionMap):
142                         def action(self, contexts, action):
143                                 if action[:7] == "bouquet":
144                                         l = self.csel
145                                         if action.find("FROM BOUQUET") != -1:
146                                                 l.setRoot(eServiceReference("1:7:1:0:0:0:0:0:0:0:" + action[8:]))
147                                         else:
148                                                 l.setRoot(eServiceReference("1:0:1:0:0:0:0:0:0:0:" + action[8:]))
149                                 else:
150                                         if action == "cancel":
151                                                 l = self.csel
152                                                 if l.movemode: #movemode active?
153                                                         l.channelSelected() # unmark
154                                                         l.toggleMoveMode() # disable move mode
155                                                 elif l.bouquet_mark_edit:
156                                                         l.endMarkedEdit(True) # abort edit mode
157                                         ActionMap.action(self, contexts, action)
158
159                 self["actions"] = ChannelActionMap(["ChannelSelectActions", "OkCancelActions", "ContextMenuActions"],
160                         {
161                                 "cancel": self.close,
162                                 "ok": self.channelSelected,
163                                 "mark": self.doMark,
164                                 "contextMenu": self.doContext,
165                                 "showFavourites": self.showFavourites,
166                                 "showEPGList": self.showEPGList
167                         })
168                 self["actions"].csel = self
169
170         def showEPGList(self):
171                 ref=self.servicelist.getCurrent()
172                 ptr=eEPGCache.getInstance()
173                 if ptr.startTimeQuery(ref) != -1:
174                         self.session.open(EPGSelection, ref)
175                 else:
176                         print 'no epg for service', ref.toString()
177
178         def getMutableList(self, root=eServiceReference()):
179                 if not self.mutableList is None:
180                         return self.mutableList
181                 serviceHandler = eServiceCenter.getInstance()
182                 if not root.valid():
183                         root=self.servicelist.getRoot()
184                 list = serviceHandler.list(root)
185                 if list is not None:
186                         return list.startEdit()
187                 return None
188
189 #  multiple marked entry stuff ( edit mode, later multiepg selection )
190         def startMarkedEdit(self):
191                 self.mutableList = self.getMutableList()
192                 # add all services from the current list to internal marked set in listboxservicecontent
193                 self.bouquetRoot = self.servicelist.getRoot()
194                 self.clearMarks() # this clears the internal marked set in the listboxservicecontent
195                 self.bouquet_mark_edit = True
196                 self.__marked = self.servicelist.getRootServices()
197                 for x in self.__marked:
198                         self.servicelist.addMarked(eServiceReference(x))
199
200         def endMarkedEdit(self, abort):
201                 if not abort and self.mutableList is not None:
202                         new_marked = set(self.servicelist.getMarked())
203                         old_marked = set(self.__marked)
204                         removed = old_marked - new_marked
205                         added = new_marked - old_marked
206                         changed = False
207                         for x in removed:
208                                 changed = True
209                                 self.mutableList.removeService(eServiceReference(x))
210                         for x in added:
211                                 changed = True
212                                 self.mutableList.addService(eServiceReference(x))
213                         if changed:
214                                 self.mutableList.flushChanges()
215                                 self.setRoot(self.bouquetRoot)
216                                 #self.showFavourites()
217                 self.__marked = []
218                 self.clearMarks()
219                 self.bouquet_mark_edit = False
220                 self.bouquetRoot = None
221                 self.mutableList = None
222
223         def clearMarks(self):
224                 self.servicelist.clearMarks()
225
226         def doMark(self):
227                 ref = self.servicelist.getCurrent()
228                 if self.servicelist.isMarked(ref):
229                         self.servicelist.removeMarked(ref)
230                 else:
231                         self.servicelist.addMarked(ref)
232
233         def removeCurrentService(self):
234                 ref = self.servicelist.getCurrent()
235                 mutableList = self.getMutableList()
236                 if ref.valid() and mutableList is not None:
237                         if not mutableList.removeService(ref):
238                                 mutableList.flushChanges() #FIXME dont flush on each single removed service
239                                 self.setRoot(self.servicelist.getRoot())
240
241         def addCurrentServiceToBouquet(self, dest):
242                 mutableList = self.getMutableList(dest)
243                 if not mutableList is None:
244                         if not mutableList.addService(self.servicelist.getCurrent()):
245                                 mutableList.flushChanges()
246                 self.close()
247
248         def searchNumberHelper(self, serviceHandler, num, bouquet):
249                 servicelist = serviceHandler.list(bouquet)
250                 if not servicelist is None:
251                         while num:
252                                 serviceIterator = servicelist.getNext()
253                                 if not serviceIterator.valid(): #check end of list
254                                         break
255                                 if serviceIterator.flags: #assume normal dvb service have no flags set
256                                         continue
257                                 num -= 1;
258                         if not num: #found service with searched number ?
259                                 return serviceIterator, 0
260                 return None, num
261
262         def zapToNumber(self, number):
263                 bouquet = self.bouquet_root
264                 service = None
265                 serviceHandler = eServiceCenter.getInstance()
266                 if bouquet.toString().find('FROM BOUQUET "bouquets.') == -1: #FIXME HACK
267                         service, number = self.searchNumberHelper(serviceHandler, number, bouquet)
268                 else:
269                         bouquetlist = serviceHandler.list(bouquet)
270                         if not bouquetlist is None:
271                                 while number:
272                                         bouquet = bouquetlist.getNext()
273                                         if not bouquet.valid(): #check end of list
274                                                 break
275                                         if ((bouquet.flags & eServiceReference.flagDirectory) != eServiceReference.flagDirectory):
276                                                 continue
277                                         service, number = self.searchNumberHelper(serviceHandler, number, bouquet)
278                 if not service is None:
279                         self.session.nav.playService(service) #play service
280                         if self.servicelist.getRoot() != bouquet: #already in correct bouquet?
281                                 self.setRoot(bouquet)
282                         self.servicelist.setCurrent(service) #select the service in servicelist
283
284         def getBouquetNumOffset(self, bouquet):
285                 if self.bouquet_root.toString().find('FROM BOUQUET "bouquets.') == -1: #FIXME HACK
286                         return 0
287                 offsetCount = 0
288                 serviceHandler = eServiceCenter.getInstance()
289                 bouquetlist = serviceHandler.list(self.bouquet_root)
290                 if not bouquetlist is None:
291                         while True:
292                                 bouquetIterator = bouquetlist.getNext()
293                                 if not bouquetIterator.valid() or bouquetIterator == bouquet: #end of list or bouquet found
294                                         break
295                                 if ((bouquetIterator.flags & eServiceReference.flagDirectory) != eServiceReference.flagDirectory):
296                                         continue
297                                 servicelist = serviceHandler.list(bouquetIterator)
298                                 if not servicelist is None:
299                                         while True:
300                                                 serviceIterator = servicelist.getNext()
301                                                 if not serviceIterator.valid(): #check if end of list
302                                                         break
303                                                 if serviceIterator.flags: #playable services have no flags
304                                                         continue
305                                                 offsetCount += 1
306                 return offsetCount
307
308         def setRoot(self, root):
309                 if not self.movemode: # dont change root when movemode is enabled
310                         inBouquetRootList = root.toString().find('FROM BOUQUET "bouquets.') != -1 #FIXME HACK
311                         if not inBouquetRootList and ((root.flags & eServiceReference.flagDirectory) == eServiceReference.flagDirectory):
312                                 self.servicelist.setMode(ServiceList.MODE_FAVOURITES)
313                                 self.servicelist.setNumberOffset(self.getBouquetNumOffset(root))
314                         else:
315                                 self.servicelist.setMode(ServiceList.MODE_NORMAL)
316                         self.servicelist.setRoot(root)
317
318         def channelSelected(self):
319                 ref = self.servicelist.getCurrent()
320                 if self.movemode:
321                         if self.entry_marked:
322                                 self.servicelist.setCurrentMarked(False)
323                                 self.entry_marked = False
324                         else:
325                                 self.servicelist.setCurrentMarked(True)
326                                 self.entry_marked = True
327                 elif (ref.flags & 7) == 7:
328                         self.setRoot(ref)
329                 elif self.bouquet_mark_edit:
330                         self.doMark()
331                 else:
332                         self.session.nav.playService(ref)
333                         self.saveChannel()
334                         self.close()
335
336         #called from infoBar
337         def zap(self):
338                 self.session.nav.playService(self.servicelist.getCurrent())
339                 self.saveChannel()
340
341         def moveUp(self):
342                 self.servicelist.moveUp()
343
344         def moveDown(self):
345                 self.servicelist.moveDown()
346
347         def doContext(self):
348                 self.session.open(ChannelContextMenu, self)
349
350         def toggleMoveMode(self):
351                 if self.movemode:
352                         if self.entry_marked:
353                                 self.channelSelected() # unmark current entry
354                         self.movemode = False
355                         self.mutableList.flushChanges() # FIXME add check if changes was made
356                         self.mutableList = None
357                 else:
358                         self.mutableList = self.getMutableList()
359                         self.movemode = True
360
361         def showFavourites(self):
362                 self.setRoot(self.bouquet_root)
363
364         def saveRoot(self, root):
365                 if root is not None:
366                         config.tv.lastroot.value = root.toString()
367                         config.tv.lastroot.save()
368
369         def saveChannel(self):
370                 ref = self.session.nav.getCurrentlyPlayingServiceReference()
371                 if ref is not None:
372                         refstr = ref.toString()
373                 else:
374                         refstr = ""
375                 config.tv.lastservice.value = refstr
376                 config.tv.lastservice.save()
377
378 class SimpleChannelSelection(ChannelSelection):
379         def __init__(self, session, title):
380                 ChannelSelection.__init__(self, session)
381                 self.title = title
382                 
383         def channelSelected(self): # just return selected service
384                 ref = self.servicelist.getCurrent()
385                 self.close(ref)
386                 
387         def doContext(self): # don't show context menu
388                 pass
389