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