changes for alternative zapping:
[enigma2.git] / lib / python / Plugins / Extensions / ZappingAlternatives / plugin.py
1 from enigma import *
2 from Screens.Screen import Screen
3 from Screens.MessageBox import MessageBox
4 from Components.ActionMap import ActionMap
5 from Components.Label import Label
6 from Components.Input import Input
7 from Components.GUIComponent import *
8 from Components.Pixmap import Pixmap
9 from Components.MenuList import MenuList
10 from Components.FileList import FileEntryComponent, FileList
11 from Navigation import Navigation
12 import NavigationInstance
13 from Screens.ChannelSelection import SimpleChannelSelection
14 from ServiceReference import ServiceReference
15 from Plugins.Plugin import PluginDescriptor
16 from Tools.Directories import resolveFilename, SCOPE_CONFIG
17 import xml.dom.minidom
18 from Tools.XMLTools import elementsWithTag
19
20 import os
21
22 alternatives = {}
23
24 def addAlternative(service1, service2):
25         if not alternatives.has_key(service1):
26                 alternatives[service1] = []
27         alternatives[service1].append(service2)
28         if not alternatives.has_key(service2):
29                 alternatives[service2] = []
30         alternatives[service2].append(service1)
31
32 def removeAlternative(service1, service2):
33         alternatives[service1].remove(service2)
34         alternatives[service2].remove(service1)
35         if len(alternatives[service1]) == 0:
36                 del alternatives[service1]
37         if len(alternatives[service2]) == 0:
38                 del alternatives[service2]
39                         
40 class AlternativeZapping(Screen):
41         skin = """
42                 <screen position="100,100" size="560,400" title="Services alternatives setup..." >
43                         <widget name="red" position="0,0" size="140,40" backgroundColor="red" halign="center" valign="center" font="Regular;21" />
44                         <widget name="green" position="140,0" size="140,40" backgroundColor="green" halign="center" valign="center" font="Regular;21" />
45                         <widget name="yellow" position="280,0" size="140,40" backgroundColor="yellow" halign="center" valign="center" font="Regular;21" />
46                         <widget name="blue" position="420,0" size="140,40" backgroundColor="blue" halign="center" valign="center" font="Regular;21" />
47                         <widget name="serviceslist" position="0,40" size="280,360" />
48                         <widget name="alternativeslist" position="280,40" size="280,360" selectionDisabled="1" />
49                 </screen>"""
50         def __init__(self, session):
51                 self.skin = AlternativeZapping.skin
52                 Screen.__init__(self, session)
53
54                 self.filename = resolveFilename(SCOPE_CONFIG, "alternatives.xml")
55                 
56                 self.red = Label("")
57                 self["red"] = self.red
58                 self.green = Label(_("Add service"))
59                 self["green"] = self.green
60                 self.yellow = Label("")
61                 self["yellow"] = self.yellow
62                 self.blue = Label("")
63                 self["blue"] = self.blue
64                 
65                 self.alternatives = {}
66                 
67                 self.serviceslist = []
68                 self.alternativeslist = []
69                 
70                 self.loadAlternatives()
71                 self["serviceslist"] = MenuList(self.serviceslist)
72                 self["alternativeslist"] = MenuList(self.alternativeslist)
73
74                 self.onShown.append(self.updateServices)
75                 self.onShown.append(self.updateAlternatives)
76
77                 self["actions"] = ActionMap(["DirectionActions", "OkCancelActions", "ColorActions"],
78                 {
79                         "ok": self.go,
80                         "cancel": self.go,
81                         "up": self.up,
82                         "down": self.down,
83                         "left": self.left,
84                         "right": self.right,
85                         "red": self.redKey,
86                         "green": self.greenKey,
87                         "yellow": self.yellowKey,
88                         "blue": self.blueKey,
89                 }, -1)
90                 
91         def saveAlternatives(self):
92                 doc = xml.dom.minidom.Document()
93                 root_element = doc.createElement('alternatives')
94                 doc.appendChild(root_element)
95                 root_element.appendChild(doc.createTextNode("\n"))
96                 
97                 for alternative in self.alternatives.keys():
98                         t = doc.createTextNode("\t")
99                         root_element.appendChild(t)
100                         t = doc.createElement('service')
101                         t.setAttribute("ref", alternative)
102                         root_element.appendChild(t)
103                         t.appendChild(doc.createTextNode("\n"))
104                         for x in self.alternatives[alternative]:
105                                 t.appendChild(doc.createTextNode("\t\t"))
106                                 l = doc.createElement('alternative')
107                                 l.setAttribute("ref", str(x))
108                                 t.appendChild(l)
109                                 t.appendChild(doc.createTextNode("\n"))
110                         t.appendChild(doc.createTextNode("\t"))
111                         root_element.appendChild(t)
112                         t = doc.createTextNode("\n")
113                         root_element.appendChild(t)
114                 file = open(self.filename, "w")
115                 doc.writexml(file)
116                 file.write("\n")
117                 file.close()
118         
119         def loadAlternatives(self):
120                 self.alternatives = {}
121                 alternatives = {}
122                 doc = xml.dom.minidom.parse(self.filename)
123                 
124                 root = doc.childNodes[0]
125                 for service in elementsWithTag(root.childNodes, 'service'):
126                         newService = str(service.getAttribute('ref'))
127                         if not self.alternatives.has_key(newService):
128                                 self.alternatives[newService] = []
129                         for alternative in elementsWithTag(service.childNodes, 'alternative'):
130                                 newAlternative = str(alternative.getAttribute('ref'))
131                                 self.alternatives[newService].append(newAlternative)
132                                 addAlternative(newService, newAlternative)
133                 
134         def go(self):
135                 self.saveAlternatives()
136                 self.close()
137         
138         def up(self):
139                 self["serviceslist"].instance.moveSelection(self["serviceslist"].instance.moveUp)
140                 self.updateAlternatives()
141                 
142         def down(self):
143                 self["serviceslist"].instance.moveSelection(self["serviceslist"].instance.moveDown)
144                 self.updateAlternatives()
145         
146         def left(self):
147                 pass
148         
149         def right(self):
150                 pass
151         
152         def redKey(self):
153                 for x in self.alternatives[self["serviceslist"].getCurrent()[1]]:
154                         removeAlternative(self["serviceslist"].getCurrent()[1], x)
155                 if len(self.serviceslist) > 0:
156                         del self.alternatives[self["serviceslist"].getCurrent()[1]]
157                 self.updateServices()
158                 self.updateAlternatives()
159         
160         def finishedAlternativeSelection(self, args):
161                 self.alternatives[self["serviceslist"].getCurrent()[1]].append(str(ServiceReference(args)))
162                 addAlternative(self["serviceslist"].getCurrent()[1], str(ServiceReference(args)))
163                 self.updateAlternatives()
164         
165         def updateServices(self):
166                 self.serviceslist = []
167                 
168                 for x in self.alternatives.keys():
169                         self.serviceslist.append((ServiceReference(x).getServiceName(), x))
170                         
171                 self["serviceslist"].l.setList(self.serviceslist)
172                 if len(self.serviceslist) > 0:
173                         self.yellow.setText(_("Add alternative"))
174                         self.red.setText(_("Remove service"))
175                 else:
176                         self.yellow.setText("")
177                         self.red.setText("")
178                         
179         def updateAlternatives(self):
180                 self.alternativeslist = []
181         
182                 if len(self.serviceslist) > 0:
183                         alternativelist = self.alternatives[self["serviceslist"].getCurrent()[1]]
184
185                         for x in alternativelist:
186                                 self.alternativeslist.append((ServiceReference(x).getServiceName(), x))
187                         
188                 self["alternativeslist"].l.setList(self.alternativeslist)
189                         
190         def greenKey(self):
191                 self.session.openWithCallback(self.finishedChannelSelection, SimpleChannelSelection, _("Select reference service"))
192
193         def finishedChannelSelection(self, args):
194                 if not self.alternatives.has_key(str(ServiceReference(args))):
195                         self.alternatives[str(ServiceReference(args))] = []
196                 print "alternatives:", self.alternatives
197                 self.updateServices()
198                 self.updateAlternatives()
199
200         def yellowKey(self):
201                 if len(self.serviceslist) > 0:
202                         self.session.openWithCallback(self.finishedAlternativeSelection, SimpleChannelSelection, _("Select alternative service"))
203         
204         def blueKey(self):
205                 pass
206
207
208 oldPlayService = NavigationInstance.instance.playService
209
210 def playService(self, ref):
211         if not oldPlayService(ref):
212                 if alternatives.has_key(str(ServiceReference(ref))):
213                         for x in alternatives[str(ServiceReference(ref))]:
214                                 if oldPlayService(ServiceReference(x).ref):
215                                         return 1
216                 return 0
217         return 1
218
219 def autostart(reason):
220         if reason == 0:
221                 NavigationInstance.instance.playService = type(NavigationInstance.instance.playService)(playService, NavigationInstance, Navigation)
222
223 def AlternativeZappingSetup(session):
224         session.open(AlternativeZapping)
225
226 def Plugins():
227         return [PluginDescriptor(where = PluginDescriptor.WHERE_AUTOSTART, fnc = autostart),
228                         PluginDescriptor(name="Alternative services setup" , description="Defines alternatives for services.", where = PluginDescriptor.WHERE_PLUGINMENU, fnc=AlternativeZappingSetup)]