growlee: add syslog backend
[vuplus_dvbapp-plugin] / 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(name):
24         s = ConfigSubsection()
25         s.name = ConfigText(default=name, 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"), ("syslog", "Syslog UDP")])
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(str(i+1))
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(_("Converted connection"))
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         skin = """
83                 <screen name="RSSSetup" position="center,center" size="560,400" title="Simple RSS Reader Setup" >
84                         <ePixmap position="0,0" size="140,40" pixmap="skin_default/buttons/red.png" transparent="1" alphatest="on" />
85                         <ePixmap position="140,0" size="140,40" pixmap="skin_default/buttons/green.png" transparent="1" alphatest="on" />
86                         <ePixmap position="280,0" size="140,40" pixmap="skin_default/buttons/yellow.png" transparent="1" alphatest="on" />
87                         <ePixmap position="420,0" size="140,40" pixmap="skin_default/buttons/blue.png" transparent="1" alphatest="on" />
88                         <widget source="key_red" render="Label" position="0,0" zPosition="1" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
89                         <widget source="key_green" render="Label" position="140,0" zPosition="1" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
90                         <widget source="key_yellow" render="Label" position="280,0" zPosition="1" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
91                         <widget source="key_blue" render="Label" position="420,0" zPosition="1" size="140,40" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
92                         <widget name="config" position="5,45" size="550,350" scrollbarMode="showOnDemand" />
93                 </screen>"""
94
95         def __init__(self, session):
96                 Screen.__init__(self, session)
97
98                 # Buttons
99                 self["key_red"] = StaticText(_("Cancel"))
100                 self["key_green"] = StaticText(_("OK"))
101                 self["key_yellow"] = StaticText(_("New"))
102                 self["key_blue"] = StaticText(_("Delete"))
103
104                 # Summary
105                 self.setup_title = "Growlee Configuration"
106                 self.onChangedEntry = []
107
108                 # Define Actions
109                 self["setupActions"] = ActionMap(["SetupActions", "ColorActions"],
110                 {
111                         "blue": self.delete,
112                         "yellow": self.new,
113                         "cancel": self.keyCancel,
114                         "save": self.keySave,
115                 })
116
117                 self.hostElement = NoSave(ConfigSelection(choices=[(x, x.name.value) for x in config.plugins.growlee.hosts]))
118                 self.hostElement.addNotifier(self.setupList, initial_call=False)
119                 ConfigListScreen.__init__(
120                         self,
121                         [],
122                         session=session,
123                         on_change=self.changed
124                 )
125                 self.cur = self.hostElement.value
126
127                 # Trigger change
128                 self.setupList()
129                 self.changed()
130
131         def delete(self):
132                 from Screens.MessageBox import MessageBox
133
134                 self.session.openWithCallback(
135                         self.deleteConfirm,
136                         MessageBox,
137                         _("Really delete this entry?\nIt cannot be recovered!")
138                 )
139
140         def deleteConfirm(self, result):
141                 if result and config.plugins.growlee.hostcount.value > 0:
142                         config.plugins.growlee.hostcount.value -= 1
143                         config.plugins.growlee.hosts.remove(self.cur)
144                         self.hostElement.setChoices([(x, x.name.value) for x in config.plugins.growlee.hosts])
145                         self.cur = self.hostElement.value
146
147         def new(self):
148                 self.cur = addHost(_("New connection"))
149                 config.plugins.growlee.hostcount.value += 1
150                 self.hostElement.setChoices([(x, x.name.value) for x in config.plugins.growlee.hosts])
151                 self.hostElement.setValue(self.cur)
152
153         def changed(self):
154                 for x in self.onChangedEntry:
155                         x()
156
157         def setupList(self, *args):
158                 last = self.cur
159                 if self.setupList in last.protocol.notifiers:
160                         last.protocol.notifiers.remove(self.setupList)
161                 cur = self.hostElement.value
162                 self.cur = cur
163                 cur.protocol.notifiers.append(self.setupList)
164
165                 l = [
166                         getConfigListEntry(_("Host"), self.hostElement),
167                         getConfigListEntry(_("Name"), cur.name),
168                         getConfigListEntry(_("Type"), cur.protocol),
169                         getConfigListEntry(_("Minimum Priority"), cur.level),
170                         getConfigListEntry(_("Send Notifications?"), cur.enable_outgoing),
171                 ]
172
173                 proto = cur.protocol.value
174                 if proto ==  "prowl":
175                         l.append(getConfigListEntry(_("API Key"), cur.password))
176                 else:
177                         l.extend((
178                                 getConfigListEntry(_("Receive Notifications?"), cur.enable_incoming),
179                                 getConfigListEntry(_("Address"), cur.address),
180                         ))
181                         if proto == "growl":
182                                 l.append(
183                                         getConfigListEntry(_("Password"), cur.password)
184                                 )
185
186                 self["config"].list = l
187
188         def getCurrentEntry(self):
189                 cur = self["config"].getCurrent()
190                 return cur and cur[0]
191
192         def getCurrentValue(self):
193                 cur = self["config"].getCurrent()
194                 return cur and str(cur[1].getText())
195
196         def createSummary(self):
197                 return SetupSummary
198
199         def cancelConfirm(self):
200                 ConfigListScreen.cancelConfirm(self)
201                 config.plugins.growlee.cancel()
202
203         def keySave(self):
204                 config.plugins.growlee.save()
205                 if self["config"].isChanged():
206                         def doConnect(*args, **kwargs):
207                                 growleeConnection.listen()
208
209                         d = growleeConnection.stop()
210                         if d is not None:
211                                 d.addCallback(doConnect).addErrback(emergencyDisable)
212                         else:
213                                 maybeConnect()
214
215                 self.saveAll()
216                 self.close()
217
218         def close(self):
219                 if self.setupList in self.cur.protocol.notifiers:
220                         self.cur.protocol.notifiers.remove(self.setupList)
221                 Screen.close(self)
222
223 def configuration(session, **kwargs):
224         session.open(GrowleeConfiguration)
225
226 def autostart(**kwargs):
227         # NOTE: we need to be the first one to be notified since other listeners
228         # may remove the notifications from the list for good
229         Notifications.notificationAdded.insert(0, gotNotification)
230
231         growleeConnection.listen()
232
233 def Plugins(**kwargs):
234         return [
235                 PluginDescriptor(
236                         where=PluginDescriptor.WHERE_SESSIONSTART,
237                         fnc=autostart,
238                 ),
239                 PluginDescriptor(
240                         name="Growlee",
241                         description=_("Configure Growlee"), 
242                         where=PluginDescriptor.WHERE_PLUGINMENU,
243                         fnc=configuration,
244                 ),
245         ]
246