Merge branch 'future'
[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(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"), ("gntp", "GNTP"), ("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="GrowleeConfiguration" position="center,center" size="560,400" title="Growlee 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.removeNotifier(self.setupList)
161                 cur = self.hostElement.value
162                 self.cur = cur
163                 cur.protocol.addNotifier(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                         if proto != "gntp":
178                                 l.append(getConfigListEntry(_("Receive Notifications?"), cur.enable_incoming))
179                         l.append(getConfigListEntry(_("Address"), cur.address))
180                         if proto == "growl" or proto == "gntp":
181                                 l.append(
182                                         getConfigListEntry(_("Password"), cur.password)
183                                 )
184
185                 self["config"].list = l
186
187         def getCurrentEntry(self):
188                 cur = self["config"].getCurrent()
189                 return cur and cur[0]
190
191         def getCurrentValue(self):
192                 cur = self["config"].getCurrent()
193                 return cur and str(cur[1].getText())
194
195         def createSummary(self):
196                 return SetupSummary
197
198         def keySave(self):
199                 config.plugins.growlee.save()
200                 if self["config"].isChanged():
201                         def doConnect(*args, **kwargs):
202                                 growleeConnection.listen()
203
204                         d = growleeConnection.stop()
205                         if d is not None:
206                                 d.addCallback(doConnect).addErrback(emergencyDisable)
207                         else:
208                                 maybeConnect()
209
210                 self.saveAll()
211                 self.close()
212
213         def close(self):
214                 if self.setupList in self.cur.protocol.notifiers:
215                         self.cur.protocol.removeNotifier(self.setupList)
216                 Screen.close(self)
217
218 def configuration(session, **kwargs):
219         session.open(GrowleeConfiguration)
220
221 def autostart(reason, **kwargs):
222         if reason == 0:
223                 # NOTE: we need to be the first one to be notified since other listeners
224                 # may remove the notifications from the list for good
225                 Notifications.notificationAdded.insert(0, gotNotification)
226
227                 growleeConnection.listen()
228
229 def Plugins(**kwargs):
230         return [
231                 PluginDescriptor(
232                         where=PluginDescriptor.WHERE_AUTOSTART,
233                         fnc=autostart,
234                         needsRestart = False,
235                 ),
236                 PluginDescriptor(
237                         name="Growlee",
238                         description=_("Configure Growlee"), 
239                         where=PluginDescriptor.WHERE_PLUGINMENU,
240                         fnc=configuration,
241                         needsRestart = False,
242                 ),
243         ]
244