growlee/src/plugin.py: first steps of multiconnect
authorMoritz Venn <moritz.venn@freaque.net>
Wed, 8 Dec 2010 20:07:11 +0000 (21:07 +0100)
committerMoritz Venn <moritz.venn@freaque.net>
Mon, 31 Jan 2011 15:51:50 +0000 (16:51 +0100)
basic work on connecting to multiple servers. had this lying around for
a while, not really sure about its state.

growlee/src/plugin.py

index 7deeb7e..6b78b39 100644 (file)
@@ -6,7 +6,8 @@ from Screens.Setup import SetupSummary
 from Screens.Screen import Screen
 from Components.ActionMap import ActionMap
 from Components.config import config, getConfigListEntry, ConfigSubsection, \
-               ConfigText, ConfigPassword, ConfigYesNo, ConfigSelection, ConfigSet
+               ConfigText, ConfigPassword, ConfigYesNo, ConfigSelection, ConfigSet, \
+               ConfigSubList, NoSave
 from Components.ConfigList import ConfigListScreen
 from Components.Sources.StaticText import StaticText
 
@@ -14,15 +15,74 @@ from GrowleeConnection import gotNotification, emergencyDisable, growleeConnecti
 
 from . import NOTIFICATIONID
 
-config.plugins.growlee = ConfigSubsection()
-config.plugins.growlee.enable_incoming = ConfigYesNo(default=False)
-config.plugins.growlee.enable_outgoing = ConfigYesNo(default=False)
-config.plugins.growlee.address = ConfigText(fixed_size=False)
-config.plugins.growlee.password = ConfigPassword()
-config.plugins.growlee.prowl_api_key = ConfigText(fixed_size=False)
-config.plugins.growlee.protocol = ConfigSelection(default="growl", choices = [("growl", "Growl"), ("snarl", "Snarl"), ("prowl", "Prowl")])
-config.plugins.growlee.level = ConfigSelection(default="-1", choices = [("-1", _("Low (Yes/No)")), ("0", _("Normal (Information)")), ("1", _("High (Warning)")), ("2", _("Highest (Emergency)"))])
-config.plugins.growlee.blacklist = ConfigSet(choices = [])
+growlee = ConfigSubsection()
+config.plugins.growlee = growlee
+growlee.hostcount = ConfigNumber(default=0)
+growlee.hosts = ConfigSubList()
+
+i = 0
+while i < growlee.hostcount.value:
+       s = ConfigSubsection()
+       s.name = ConfigText(default=str(i+1), fixed_size=False)
+       s.enable_incoming = ConfigYesNo(default=False)
+       s.enable_outgoing = ConfigYesNo(default=False)
+       s.address = ConfigText(fixed_size=False)
+       s.password = ConfigPassword()
+       s.protocol = ConfigSelection(default="growl", choices=[("growl", "Growl"), ("snarl", "Snarl"), ("prowl", "Prowl")])
+       s.level = ConfigSelection(default="-1", choices=[("-1", _("Low (Yes/No)")), ("0", _("Normal (Information)")), ("1", _("High (Warning)")), ("2", _("Highest (Emergency)"))])
+       s.blacklist = ConfigSet(choices=[])
+       growlee.hosts.append(s)
+       i += 1
+       del s
+
+# XXX: change to new config format
+growlee.enable_outgoing = ConfigYesNo(default=False)
+if growlee.hostcount.value == 0 and growlee.enable_outgoing.value:
+       growlee.enable_incoming = ConfigYesNo(default=False)
+       growlee.address = ConfigText(fixed_size=False)
+       growlee.password = ConfigPassword()
+       password = growlee.password.value
+       growlee.prowl_api_key = ConfigText()
+       growlee.protocol = ConfigSelection(default="growl", choices=[("growl", "Growl"), ("snarl", "Snarl"), ("prowl", "Prowl")])
+       growlee.level = ConfigSelection(default="-1", choices=[("-1", _("Low (Yes/No)")), ("0", _("Normal (Information)")), ("1", _("High (Warning)")), ("2", _("Highest (Emergency)"))])
+       growlee.blacklist = ConfigSet(choices=[])
+       if growlee.protocol.value == "prowl":
+               password = growlee.prowl_api_key.value
+
+       s = ConfigSubsection()
+       s.name = ConfigText(default="1", fixed_size=False)
+       s.enable_incoming = ConfigYesNo(default=False)
+       s.enable_outgoing = ConfigYesNo(default=False)
+       s.address = ConfigText(fixed_size=False)
+       s.password = ConfigPassword()
+       s.protocol = ConfigSelection(default="growl", choices=[("growl", "Growl"), ("snarl", "Snarl"), ("prowl", "Prowl")])
+       s.level = ConfigSelection(default="-1", choices=[("-1", _("Low (Yes/No)")), ("0", _("Normal (Information)")), ("1", _("High (Warning)")), ("2", _("Highest (Emergency)"))])
+       s.blacklist = ConfigSet(choices=[])
+
+       s.enable_incoming.value = growlee.enable_incoming.value
+       s.enable_outgoing.value = growlee.enable_outgoing.value
+       s.address.value = growlee.address.value
+       s.password.value = password
+       s.protocol.value = growlee.protocol.value
+       s.level.value = growlee.level.value
+       s.blacklist.value = growlee.blacklist.value
+
+       growlee.enable_incoming.value = False
+       growlee.enable_outgoing.value = False
+       growlee.address.value = ""
+       growlee.password.value = ""
+       growlee.prowl_api_key.value = ""
+       growlee.protocol.value = "growl"
+       growlee.level.value = "-1"
+       growlee.blacklist.value = []
+
+       growlee.hostcount.value += 1
+       growlee.hosts.append(s)
+
+       growlee.save()
+       del s
+
+del i, growlee
 
 class GrowleeConfiguration(Screen, ConfigListScreen):
        def __init__(self, session):
@@ -45,7 +105,8 @@ class GrowleeConfiguration(Screen, ConfigListScreen):
                        }
                )
 
-               config.plugins.growlee.protocol.addNotifier(self.setupList, initial_call=False)
+               self.hostElement = NoSave(ConfigSelection(choices=[(x, x.name.value) for x in config.plugins.growlee.hosts]))
+               self.hostElement.addNotifier(self.setupList, initial_call=False)
                ConfigListScreen.__init__(
                        self,
                        [],
@@ -62,23 +123,31 @@ class GrowleeConfiguration(Screen, ConfigListScreen):
                        x()
 
        def setupList(self, *args):
+               last = self.cur
+               if self.setupList in last.protocol.notifiers:
+                       last.protocol.notifiers.remove(self.setupList)
+               cur = self.hostElement.value
+               cur.protocol.notifiers.append(self.setupList)
+
                l = [
-                       getConfigListEntry(_("Type"), config.plugins.growlee.protocol),
-                       getConfigListEntry(_("Minimum Priority"), config.plugins.growlee.level),
-                       getConfigListEntry(_("Send Notifications?"), config.plugins.growlee.enable_outgoing),
+                       getConfigListEntry(_("Host"), self.hostElement),
+                       getConfigListEntry(_("Name"), cur.name),
+                       getConfigListEntry(_("Type"), cur.protocol),
+                       getConfigListEntry(_("Minimum Priority"), cur.level),
+                       getConfigListEntry(_("Send Notifications?"), cur.enable_outgoing),
                ]
 
-               proto = config.plugins.growlee.protocol.value
+               proto = cur.protocol.value
                if proto ==  "prowl":
-                       l.append(getConfigListEntry(_("API Key"), config.plugins.growlee.prowl_api_key))
+                       l.append(getConfigListEntry(_("API Key"), cur.password))
                else:
                        l.extend((
-                               getConfigListEntry(_("Receive Notifications?"), config.plugins.growlee.enable_incoming),
-                               getConfigListEntry(_("Address"), config.plugins.growlee.address),
+                               getConfigListEntry(_("Receive Notifications?"), cur.enable_incoming),
+                               getConfigListEntry(_("Address"), cur.address),
                        ))
                        if proto == "growl":
                                l.append(
-                                       getConfigListEntry(_("Password"), config.plugins.growlee.password)
+                                       getConfigListEntry(_("Password"), cur.password)
                                )
 
                self["config"].list = l