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