d83d3d7a265d4c509e85d13b97ff24bf3c8bc2ca
[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, eTimer
7 from Components.config import config, configElement, ConfigSubsection, configText
8
9 from Screens.FixedMenu import FixedMenu
10
11 import xml.dom.minidom
12
13 class ChannelContextMenu(FixedMenu):
14         def __init__(self, session, csel):
15                 self.csel = csel
16                 
17                 menu = [ ]
18                 
19                 if csel.mutableList is not None:
20                         if not csel.bouquet_mark_edit:
21                                 if csel.movemode:
22                                         menu.append(("disable move mode", self.toggleMoveMode))
23                                 else:
24                                         menu.append(("enable move mode", self.toggleMoveMode))
25
26                         if not csel.movemode:
27                                 if csel.bouquet_mark_edit:
28                                         menu.append(("end bouquet edit", self.bouquetMarkEnd))
29                                         menu.append(("abort bouquet edit", self.bouquetMarkAbort))
30                                 else:
31                                         menu.append(("edit bouquet...", self.bouquetMarkStart))
32
33                         if not csel.bouquet_mark_edit and not csel.movemode:
34                                 menu.append(("remove service", self.removeCurrentService))
35                         menu.append(("back", self.close))
36                 else:
37                         menu.append(("back", self.close))
38
39                 FixedMenu.__init__(self, session, "Channel Selection", menu)
40                 self.skinName = "Menu"
41
42         def removeCurrentService(self):
43                 self.close()
44                 self.csel.removeCurrentService()
45
46         def toggleMoveMode(self):
47                 self.csel.toggleMoveMode()
48                 self.close()
49
50         def bouquetMarkStart(self):
51                 self.close()
52                 self.csel.startMarkedEdit()
53
54         def bouquetMarkEnd(self):
55                 self.csel.endMarkedEdit(abort=False)
56                 self.close()
57
58         def bouquetMarkAbort(self):
59                 self.csel.endMarkedEdit(abort=True)
60                 self.close()
61  
62 class ChannelSelection(Screen):
63         def lastService(self):
64                 self.lastServiceTimer.stop()
65                 #zap to last running tv service
66                 config.tv = ConfigSubsection();
67                 config.tv.lastservice = configElement("config.tv.lastservice", configText, "", 0);
68                 self.session.nav.playService(eServiceReference(config.tv.lastservice.value))
69         
70         def __init__(self, session):
71                 Screen.__init__(self, session)
72                 
73                 self.entry_marked = False
74                 self.movemode = False
75                 self.bouquet_mark_edit = False
76                 
77                 ## FIXME
78                 self.__marked = [ ]
79                 
80                 self["key_red"] = Button("All")
81                 self["key_green"] = Button("Provider")
82                 self["key_yellow"] = Button("Satellite")
83                 self["key_blue"] = Button("Favourites")
84                 
85                 self["list"] = ServiceList()
86                 self.setRoot(eServiceReference("""1:0:1:0:0:0:0:0:0:0:(type == 1)"""))
87                 
88                 #self["okbutton"] = Button("ok", [self.channelSelected])
89
90                 self.lastServiceTimer = eTimer()
91                 self.lastServiceTimer.timeout.get().append(self.lastService)
92                 self.lastServiceTimer.start(100)
93
94                 class ChannelActionMap(ActionMap):
95                         def action(self, contexts, action):
96                                 if action[:7] == "bouquet":
97                                         l = self.csel
98                                         list = l["list"]
99                                         list.setMode(list.MODE_NORMAL)
100                                         l.setRoot(eServiceReference("1:7:1:0:0:0:0:0:0:0:" + action[8:]))
101                                 else:
102                                         if action == "cancel":
103                                                 l = self.csel
104                                                 if l.movemode: #movemode active?
105                                                         l.channelSelected() # unmark
106                                                         l.toggleMoveMode() # disable move mode
107                                                 elif l.bouquet_mark_edit:
108                                                         l.endMarkedEdit(True) # abort edit mode
109                                         ActionMap.action(self, contexts, action)
110
111                 self["actions"] = ChannelActionMap(["ChannelSelectActions", "OkCancelActions", "ContextMenuActions"], 
112                         {
113                                 "cancel": self.close,
114                                 "ok": self.channelSelected,
115                                 "mark": self.doMark,
116                                 "contextMenu": self.doContext,
117                                 "showFavourites": self.showFavourites,
118                     "showEPGList": self.showEPGList
119                         })
120                 self["actions"].csel = self
121
122         def showEPGList(self):
123                 ref=self["list"].getCurrent()
124                 ptr=eEPGCache.getInstance()
125                 if ptr.startTimeQuery(ref) != -1:
126                         self.session.open(EPGSelection, ref)
127                 else:
128                         print 'no epg for service', ref.toString()
129
130 #  multiple marked entry stuff ( edit mode, later multiepg selection )
131         def startMarkedEdit(self):
132                 l = self["list"]
133                 # add all services from the current list to internal marked set in listboxservicecontent
134                 if self.mutableList is not None:
135                         self.bouquetRoot = l.getRoot()
136                         self.clearMarks() # this clears the internal marked set in the listboxservicecontent
137                         self.bouquet_mark_edit = True
138                         self.__marked = l.getRootServices()
139                         for x in self.__marked:
140                                 l.addMarked(eServiceReference(x))
141
142         def removeCurrentService(self):
143                 l = self["list"]
144                 ref=l.getCurrent()
145                 if ref.valid() and self.mutableList is not None:
146                         self.mutableList.removeService(ref)
147                         self.mutableList.flushChanges() #FIXME dont flush on each single removed service
148                         self.setRoot(l.getRoot())
149
150         def endMarkedEdit(self, abort):
151                 l = self["list"]
152                 if not abort and self.mutableList is not None:
153                         new_marked = set(l.getMarked())
154                         old_marked = set(self.__marked)
155                         removed = old_marked - new_marked
156                         added = new_marked - old_marked
157                         changed = False
158                         for x in removed:
159                                 changed = True
160                                 self.mutableList.removeService(eServiceReference(x))
161                         for x in added:
162                                 changed = True
163                                 self.mutableList.addService(eServiceReference(x))
164                         if changed:
165                                 self.mutableList.flushChanges()
166                                 #self.setRoot(self.bouquetRoot)
167                                 self.showFavourites()
168                 self.__marked = []
169                 self.clearMarks()
170                 self.bouquet_mark_edit = False
171                 self.bouquetRoot = None
172
173         def setRoot(self, root):
174                 if not self.movemode:
175                         if not self.bouquet_mark_edit:
176                                 serviceHandler = eServiceCenter.getInstance()
177                                 list = serviceHandler.list(root)
178                                 if list is not None:
179                                         self.mutableList = list.startEdit()
180                                 else:
181                                         self.mutableList = None
182                         self["list"].setRoot(root)
183
184         def clearMarks(self):
185                 self["list"].clearMarks()
186
187         def doMark(self):
188                 if not self.bouquet_mark_edit:
189                         return
190                 ref = self["list"].getCurrent()
191                 if self["list"].isMarked(ref):
192                         self["list"].removeMarked(ref)
193                 else:
194                         self["list"].addMarked(ref)
195
196         def channelSelected(self):
197                 ref = self["list"].getCurrent()
198                 if self.movemode:
199                         if self.entry_marked:
200                                 self["list"].setCurrentMarked(False)
201                                 self.entry_marked = False
202                         else:
203                                 self["list"].setCurrentMarked(True)
204                                 self.entry_marked = True
205                 elif (ref.flags & 7) == 7:
206                         self.setRoot(ref)
207                 elif self.bouquet_mark_edit:
208                         self.doMark()
209                 else:
210                         self.session.nav.playService(ref)
211                         self.saveChannel()
212                         self.close()
213
214         #called from infoBar
215         def zap(self):
216                 self.session.nav.playService(self["list"].getCurrent())
217                 self.saveChannel()
218
219         def moveUp(self):
220                 self["list"].moveUp()
221
222         def moveDown(self):
223                 self["list"].moveDown()
224
225         def doContext(self):
226                 self.session.open(ChannelContextMenu, self)
227
228         def toggleMoveMode(self):
229                 if self.movemode:
230                         if self.entry_marked:
231                                 self.channelSelected() # unmark current entry
232                         self.movemode = False
233                         self.mutableList.flushChanges() # FIXME add check if changes was made
234                 else:
235                         self.movemode = True
236
237         def showFavourites(self):
238                 self.setRoot(eServiceReference('1:7:1:0:0:0:0:0:0:0:(type == 1) FROM BOUQUET "userbouquet.favourites.tv" ORDER BY bouquet'))
239                 list = self["list"]
240                 list.setMode(list.MODE_FAVOURITES)
241
242         def saveChannel(self):
243                 ref = self.session.nav.getCurrentlyPlayingServiceReference()
244                 if ref is not None:
245                         refstr = ref.toString()
246                 else:
247                         refstr = ""
248                 config.tv.lastservice.value = refstr
249                 config.tv.lastservice.save()