modifications for edit mode ( is working now )
[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 = [("back", self.close)]
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                 FixedMenu.__init__(self, session, "Channel Selection", menu)
33                 self.skinName = "Menu"
34
35         def toggleMoveMode(self):
36                 self.csel.toggleMoveMode()
37                 self.close()
38
39         def bouquetMarkStart(self):
40                 self.csel.startMarkedEdit()
41                 self.close()
42
43         def bouquetMarkEnd(self):
44                 self.csel.endMarkedEdit(abort=False)
45                 self.close()
46
47         def bouquetMarkAbort(self):
48                 self.csel.endMarkedEdit(abort=True)
49                 self.close()
50  
51 class ChannelSelection(Screen):
52         def __init__(self, session):
53                 Screen.__init__(self, session)
54                 
55                 self.entry_marked = False
56                 self.movemode = False
57                 self.bouquet_mark_edit = False
58                 
59                 ## FIXME
60                 self.__marked = [ ]
61                 
62                 self["key_red"] = Button("All")
63                 self["key_green"] = Button("Provider")
64                 self["key_yellow"] = Button("Satellite")
65                 self["key_blue"] = Button("Favourites")
66                 
67                 self["list"] = ServiceList()
68                 self.setRoot(eServiceReference("""1:0:1:0:0:0:0:0:0:0:(type == 1)"""))
69                 
70                 #self["okbutton"] = Button("ok", [self.channelSelected])
71                 
72                 class ChannelActionMap(ActionMap):
73                         def action(self, contexts, action):
74                                 if action[:7] == "bouquet":
75                                         l = self.csel
76                                         l.setRoot(eServiceReference("1:7:1:0:0:0:0:0:0:0:" + action[8:]))
77                                 else:
78                                         if action == "cancel":
79                                                 l = self.csel
80                                                 if l.movemode: #movemode active?
81                                                         l.channelSelected() # unmark
82                                                         l.toggleMoveMode() # disable move mode
83                                         ActionMap.action(self, contexts, action)
84
85                 self["actions"] = ChannelActionMap(["ChannelSelectActions", "OkCancelActions", "ContextMenuActions"], 
86                         {
87                                 "cancel": self.close,
88                                 "ok": self.channelSelected,
89                                 "mark": self.doMark,
90                                 "contextMenu": self.doContext,
91                             "showEPGList": self.showEPGList
92                         })
93                 self["actions"].csel = self
94
95         def showEPGList(self):
96                 ref=self["list"].getCurrent()
97                 ptr=eEPGCache.getInstance()
98                 if ptr.startTimeQuery(ref) != -1:
99                         self.session.open(EPGSelection, ref)
100                 else:
101                         print 'no epg for service', ref.toString()
102
103 #  multiple marked entry stuff ( edit mode, later multiepg selection )
104         def startMarkedEdit(self):
105                 l = self["list"]
106                 # add all services from the current list to internal marked set in listboxservicecontent
107                 if self.mutableList is not None:
108                         self.bouquetRoot = l.getRoot()
109                         self.clearMarks() # this clears the internal marked set in the listboxservicecontent
110                         self.bouquet_mark_edit = True
111                         self.__marked = l.getRootServices()
112                         for x in self.__marked:
113                                 l.addMarked(eServiceReference(x))
114
115         def endMarkedEdit(self, abort):
116                 l = self["list"]
117                 if not abort and self.mutableList is not None:
118                         new_marked = set(l.getMarked())
119                         old_marked = set(self.__marked)
120                         removed = old_marked - new_marked
121                         added = new_marked - old_marked
122                         changed = False
123                         for x in removed:
124                                 changed = True
125                                 self.mutableList.removeService(eServiceReference(x))
126                         for x in added:
127                                 changed = True
128                                 self.mutableList.addService(eServiceReference(x))
129                         if changed:
130                                 l.setRoot(self.bouquetRoot)
131                 self.__marked = []
132                 self.clearMarks()
133                 self.bouquet_mark_edit = False
134                 self.bouquetRoot = None
135
136         def setRoot(self, root):
137                 if not self.movemode:
138                         if not self.bouquet_mark_edit:
139                                 serviceHandler = eServiceCenter.getInstance()
140                                 list = serviceHandler.list(root)
141                                 if list is not None:
142                                         self.mutableList = list.startEdit()
143                                 else:
144                                         self.mutableList = None
145                         self["list"].setRoot(root)
146
147         def clearMarks(self):
148                 self["list"].clearMarks()
149
150         def doMark(self):
151                 if not self.bouquet_mark_edit:
152                         return
153                 ref = self["list"].getCurrent()
154                 if self["list"].isMarked(ref):
155                         self["list"].removeMarked(ref)
156                 else:
157                         self["list"].addMarked(ref)
158
159         def channelSelected(self):
160                 ref = self["list"].getCurrent()
161                 if self.movemode:
162                         if self.entry_marked:
163                                 self["list"].setCurrentMarked(False)
164                                 self.entry_marked = False
165                         else:
166                                 self["list"].setCurrentMarked(True)
167                                 self.entry_marked = True
168                 elif (ref.flags & 7) == 7:
169                         self.setRoot(ref)
170                 elif self.bouquet_mark_edit:
171                         self.doMark()
172                 else:
173                         self.session.nav.playService(ref)
174                         self.close()
175
176         #called from infoBar
177         def zap(self):
178                 self.session.nav.playService(self["list"].getCurrent())
179
180         def moveUp(self):
181                 self["list"].moveUp()
182
183         def moveDown(self):
184                 self["list"].moveDown()
185
186         def doContext(self):
187                 self.session.open(ChannelContextMenu, self)
188
189         def toggleMoveMode(self):
190                 if self.movemode:
191                         self.movemode = False
192                 else:
193                         self.movemode = True