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