changes for alternative zapping:
authorStefan Pluecken <stefan.pluecken@multimedia-labs.de>
Sat, 4 Mar 2006 14:04:49 +0000 (14:04 +0000)
committerStefan Pluecken <stefan.pluecken@multimedia-labs.de>
Sat, 4 Mar 2006 14:04:49 +0000 (14:04 +0000)
- load/save in /etc/enigma2/alternatives.xml
- symmetric alternative relations

lib/python/Plugins/Extensions/ZappingAlternatives/plugin.py

index b018c25..53c9ab6 100644 (file)
@@ -13,11 +13,30 @@ import NavigationInstance
 from Screens.ChannelSelection import SimpleChannelSelection
 from ServiceReference import ServiceReference
 from Plugins.Plugin import PluginDescriptor
+from Tools.Directories import resolveFilename, SCOPE_CONFIG
+import xml.dom.minidom
+from Tools.XMLTools import elementsWithTag
 
 import os
 
 alternatives = {}
 
+def addAlternative(service1, service2):
+       if not alternatives.has_key(service1):
+               alternatives[service1] = []
+       alternatives[service1].append(service2)
+       if not alternatives.has_key(service2):
+               alternatives[service2] = []
+       alternatives[service2].append(service1)
+
+def removeAlternative(service1, service2):
+       alternatives[service1].remove(service2)
+       alternatives[service2].remove(service1)
+       if len(alternatives[service1]) == 0:
+               del alternatives[service1]
+       if len(alternatives[service2]) == 0:
+               del alternatives[service2]
+                       
 class AlternativeZapping(Screen):
        skin = """
                <screen position="100,100" size="560,400" title="Services alternatives setup..." >
@@ -31,6 +50,8 @@ class AlternativeZapping(Screen):
        def __init__(self, session):
                self.skin = AlternativeZapping.skin
                Screen.__init__(self, session)
+
+               self.filename = resolveFilename(SCOPE_CONFIG, "alternatives.xml")
                
                self.red = Label("")
                self["red"] = self.red
@@ -41,19 +62,22 @@ class AlternativeZapping(Screen):
                self.blue = Label("")
                self["blue"] = self.blue
                
+               self.alternatives = {}
+               
                self.serviceslist = []
-               self["serviceslist"] = MenuList(self.serviceslist)
-
                self.alternativeslist = []
-               self["alternativeslist"] = MenuList(self.alternativeslist)
                
+               self.loadAlternatives()
+               self["serviceslist"] = MenuList(self.serviceslist)
+               self["alternativeslist"] = MenuList(self.alternativeslist)
+
                self.onShown.append(self.updateServices)
                self.onShown.append(self.updateAlternatives)
 
                self["actions"] = ActionMap(["DirectionActions", "OkCancelActions", "ColorActions"],
                {
                        "ok": self.go,
-                       "cancel": self.close,
+                       "cancel": self.go,
                        "up": self.up,
                        "down": self.down,
                        "left": self.left,
@@ -64,8 +88,52 @@ class AlternativeZapping(Screen):
                        "blue": self.blueKey,
                }, -1)
                
+       def saveAlternatives(self):
+               doc = xml.dom.minidom.Document()
+               root_element = doc.createElement('alternatives')
+               doc.appendChild(root_element)
+               root_element.appendChild(doc.createTextNode("\n"))
+               
+               for alternative in self.alternatives.keys():
+                       t = doc.createTextNode("\t")
+                       root_element.appendChild(t)
+                       t = doc.createElement('service')
+                       t.setAttribute("ref", alternative)
+                       root_element.appendChild(t)
+                       t.appendChild(doc.createTextNode("\n"))
+                       for x in self.alternatives[alternative]:
+                               t.appendChild(doc.createTextNode("\t\t"))
+                               l = doc.createElement('alternative')
+                               l.setAttribute("ref", str(x))
+                               t.appendChild(l)
+                               t.appendChild(doc.createTextNode("\n"))
+                       t.appendChild(doc.createTextNode("\t"))
+                       root_element.appendChild(t)
+                       t = doc.createTextNode("\n")
+                       root_element.appendChild(t)
+               file = open(self.filename, "w")
+               doc.writexml(file)
+               file.write("\n")
+               file.close()
+       
+       def loadAlternatives(self):
+               self.alternatives = {}
+               alternatives = {}
+               doc = xml.dom.minidom.parse(self.filename)
+               
+               root = doc.childNodes[0]
+               for service in elementsWithTag(root.childNodes, 'service'):
+                       newService = str(service.getAttribute('ref'))
+                       if not self.alternatives.has_key(newService):
+                               self.alternatives[newService] = []
+                       for alternative in elementsWithTag(service.childNodes, 'alternative'):
+                               newAlternative = str(alternative.getAttribute('ref'))
+                               self.alternatives[newService].append(newAlternative)
+                               addAlternative(newService, newAlternative)
+               
        def go(self):
-               pass
+               self.saveAlternatives()
+               self.close()
        
        def up(self):
                self["serviceslist"].instance.moveSelection(self["serviceslist"].instance.moveUp)
@@ -82,19 +150,22 @@ class AlternativeZapping(Screen):
                pass
        
        def redKey(self):
+               for x in self.alternatives[self["serviceslist"].getCurrent()[1]]:
+                       removeAlternative(self["serviceslist"].getCurrent()[1], x)
                if len(self.serviceslist) > 0:
-                       del alternatives[self["serviceslist"].getCurrent()[1]]
+                       del self.alternatives[self["serviceslist"].getCurrent()[1]]
                self.updateServices()
                self.updateAlternatives()
        
        def finishedAlternativeSelection(self, args):
-               alternatives[self["serviceslist"].getCurrent()[1]].append(str(ServiceReference(args)))
+               self.alternatives[self["serviceslist"].getCurrent()[1]].append(str(ServiceReference(args)))
+               addAlternative(self["serviceslist"].getCurrent()[1], str(ServiceReference(args)))
                self.updateAlternatives()
        
        def updateServices(self):
                self.serviceslist = []
                
-               for x in alternatives.keys():
+               for x in self.alternatives.keys():
                        self.serviceslist.append((ServiceReference(x).getServiceName(), x))
                        
                self["serviceslist"].l.setList(self.serviceslist)
@@ -109,7 +180,7 @@ class AlternativeZapping(Screen):
                self.alternativeslist = []
        
                if len(self.serviceslist) > 0:
-                       alternativelist = alternatives[self["serviceslist"].getCurrent()[1]]
+                       alternativelist = self.alternatives[self["serviceslist"].getCurrent()[1]]
 
                        for x in alternativelist:
                                self.alternativeslist.append((ServiceReference(x).getServiceName(), x))
@@ -120,21 +191,12 @@ class AlternativeZapping(Screen):
                self.session.openWithCallback(self.finishedChannelSelection, SimpleChannelSelection, _("Select reference service"))
 
        def finishedChannelSelection(self, args):
-               if alternatives.has_key(str(ServiceReference(args))):
-                       pass
-               else:
-                       alternatives[str(ServiceReference(args))] = []
-               print alternatives
+               if not self.alternatives.has_key(str(ServiceReference(args))):
+                       self.alternatives[str(ServiceReference(args))] = []
+               print "alternatives:", self.alternatives
                self.updateServices()
-               #oldref = self.timer.service_ref
-               #try:
-                       #self.timer.service_ref = ServiceReference(args)
-                       #config.timerentry.service.vals = (str(self.timer.service_ref.getServiceName()),)
-                       #self["config"].invalidate(config.timerentry.service)
-               #except:
-               #       print "you pressed cancel"
-                       #self.timer.service_ref = oldref
-       
+               self.updateAlternatives()
+
        def yellowKey(self):
                if len(self.serviceslist) > 0:
                        self.session.openWithCallback(self.finishedAlternativeSelection, SimpleChannelSelection, _("Select alternative service"))