growlee: more work on multiconnect
[enigma2-plugins.git] / growlee / src / plugin.py
1 from Plugins.Plugin import PluginDescriptor
2
3 from Tools import Notifications
4
5 from Screens.Setup import SetupSummary
6 from Screens.Screen import Screen
7 from Components.ActionMap import ActionMap
8 from Components.config import config, getConfigListEntry, ConfigSubsection, \
9                 ConfigText, ConfigPassword, ConfigYesNo, ConfigSelection, ConfigSet, \
10                 ConfigSubList, ConfigNumber, NoSave
11 from Components.ConfigList import ConfigListScreen
12 from Components.Sources.StaticText import StaticText
13
14 from GrowleeConnection import gotNotification, emergencyDisable, growleeConnection
15
16 from . import NOTIFICATIONID
17
18 growlee = ConfigSubsection()
19 config.plugins.growlee = growlee
20 growlee.hostcount = ConfigNumber(default=0)
21 growlee.hosts = ConfigSubList()
22
23 def addHost(idx):
24         s = ConfigSubsection()
25         s.name = ConfigText(default=str(idx+1), fixed_size=False)
26         s.enable_incoming = ConfigYesNo(default=False)
27         s.enable_outgoing = ConfigYesNo(default=False)
28         s.address = ConfigText(fixed_size=False)
29         s.password = ConfigPassword()
30         s.protocol = ConfigSelection(default="growl", choices=[("growl", "Growl"), ("snarl", "Snarl"), ("prowl", "Prowl")])
31         s.level = ConfigSelection(default="-1", choices=[("-1", _("Low (Yes/No)")), ("0", _("Normal (Information)")), ("1", _("High (Warning)")), ("2", _("Highest (Emergency)"))])
32         s.blacklist = ConfigSet(choices=[])
33         config.plugins.growlee.hosts.append(s)
34         return s
35
36 i = 0
37 while i < growlee.hostcount.value:
38         addHost(i)
39         i += 1
40
41 # XXX: change to new config format
42 # NOTE: after some time, remove this and hardcode default length to 1
43 # since internally we assume to have at least 1 host configured
44 if growlee.hostcount.value == 0:
45         growlee.enable_outgoing = ConfigYesNo(default=False)
46         growlee.enable_incoming = ConfigYesNo(default=False)
47         growlee.address = ConfigText(fixed_size=False)
48         growlee.password = ConfigPassword()
49         password = growlee.password.value
50         growlee.prowl_api_key = ConfigText()
51         growlee.protocol = ConfigSelection(default="growl", choices=[("growl", "Growl"), ("snarl", "Snarl"), ("prowl", "Prowl")])
52         growlee.level = ConfigSelection(default="-1", choices=[("-1", _("Low (Yes/No)")), ("0", _("Normal (Information)")), ("1", _("High (Warning)")), ("2", _("Highest (Emergency)"))])
53         growlee.blacklist = ConfigSet(choices=[])
54         if growlee.protocol.value == "prowl":
55                 password = growlee.prowl_api_key.value
56
57         s = addHost(0)
58         s.enable_incoming.value = growlee.enable_incoming.value
59         s.enable_outgoing.value = growlee.enable_outgoing.value
60         s.address.value = growlee.address.value
61         s.password.value = password
62         s.protocol.value = growlee.protocol.value
63         s.level.value = growlee.level.value
64         s.blacklist.value = growlee.blacklist.value
65
66         growlee.enable_incoming.value = False
67         growlee.enable_outgoing.value = False
68         growlee.address.value = ""
69         growlee.password.value = ""
70         growlee.prowl_api_key.value = ""
71         growlee.protocol.value = "growl"
72         growlee.level.value = "-1"
73         growlee.blacklist.value = []
74
75         growlee.hostcount.value += 1
76         growlee.save()
77         del s
78
79 del i, growlee
80
81 class GrowleeConfiguration(Screen, ConfigListScreen):
82         def __init__(self, session):
83                 Screen.__init__(self, session)
84                 self.skinName = [ "GrowleeConfiguration", "Setup" ]
85
86                 # Buttons
87                 self["key_red"] = StaticText(_("Cancel"))
88                 self["key_green"] = StaticText(_("OK"))
89
90                 # Summary
91                 self.setup_title = "Growlee Configuration"
92                 self.onChangedEntry = []
93
94                 # Define Actions
95                 self["actions"] = ActionMap(["SetupActions"],
96                         {
97                                 "cancel": self.keyCancel,
98                                 "save": self.keySave,
99                         }
100                 )
101
102                 self.hostElement = NoSave(ConfigSelection(choices=[(x, x.name.value) for x in config.plugins.growlee.hosts]))
103                 self.hostElement.addNotifier(self.setupList, initial_call=False)
104                 ConfigListScreen.__init__(
105                         self,
106                         [],
107                         session=session,
108                         on_change=self.changed
109                 )
110                 self.cur = self.hostElement.value
111
112                 # Trigger change
113                 self.setupList()
114                 self.changed()
115
116         def changed(self):
117                 for x in self.onChangedEntry:
118                         x()
119
120         def setupList(self, *args):
121                 last = self.cur
122                 if self.setupList in last.protocol.notifiers:
123                         last.protocol.notifiers.remove(self.setupList)
124                 cur = self.hostElement.value
125                 self.cur = cur
126                 cur.protocol.notifiers.append(self.setupList)
127
128                 l = [
129                         getConfigListEntry(_("Host"), self.hostElement),
130                         getConfigListEntry(_("Name"), cur.name),
131                         getConfigListEntry(_("Type"), cur.protocol),
132                         getConfigListEntry(_("Minimum Priority"), cur.level),
133                         getConfigListEntry(_("Send Notifications?"), cur.enable_outgoing),
134                 ]
135
136                 proto = cur.protocol.value
137                 if proto ==  "prowl":
138                         l.append(getConfigListEntry(_("API Key"), cur.password))
139                 else:
140                         l.extend((
141                                 getConfigListEntry(_("Receive Notifications?"), cur.enable_incoming),
142                                 getConfigListEntry(_("Address"), cur.address),
143                         ))
144                         if proto == "growl":
145                                 l.append(
146                                         getConfigListEntry(_("Password"), cur.password)
147                                 )
148
149                 self["config"].list = l
150
151         def getCurrentEntry(self):
152                 cur = self["config"].getCurrent()
153                 return cur and cur[0]
154
155         def getCurrentValue(self):
156                 cur = self["config"].getCurrent()
157                 return cur and str(cur[1].getText())
158
159         def createSummary(self):
160                 return SetupSummary
161
162         def keySave(self):
163                 if self["config"].isChanged():
164                         def doConnect(*args, **kwargs):
165                                 growleeConnection.listen()
166
167                         d = growleeConnection.stop()
168                         if d is not None:
169                                 d.addCallback(doConnect).addErrback(emergencyDisable)
170                         else:
171                                 maybeConnect()
172
173                 self.saveAll()
174                 self.close()
175
176         def close(self):
177                 if self.setupList in self.cur.protocol.notifiers:
178                         self.cur.protocol.notifiers.remove(self.setupList)
179                 Screen.close(self)
180
181 def configuration(session, **kwargs):
182         session.open(GrowleeConfiguration)
183
184 def autostart(**kwargs):
185         # NOTE: we need to be the first one to be notified since other listeners
186         # may remove the notifications from the list for good
187         Notifications.notificationAdded.insert(0, gotNotification)
188
189         growleeConnection.listen()
190
191 def Plugins(**kwargs):
192         return [
193                 PluginDescriptor(
194                         where=PluginDescriptor.WHERE_SESSIONSTART,
195                         fnc=autostart,
196                 ),
197                 PluginDescriptor(
198                         name="Growlee",
199                         description=_("Configure Growlee"), 
200                         where=PluginDescriptor.WHERE_PLUGINMENU,
201                         fnc=configuration,
202                 ),
203         ]
204