rework backends and split them up,
[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 from Components.ConfigList import ConfigListScreen
11 from Components.Sources.StaticText import StaticText
12
13 from GrowleeConnection import gotNotification, emergencyDisable, growleeConnection
14
15 from . import NOTIFICATIONID
16
17 config.plugins.growlee = ConfigSubsection()
18 config.plugins.growlee.enable_incoming = ConfigYesNo(default=False)
19 config.plugins.growlee.enable_outgoing = ConfigYesNo(default=False)
20 config.plugins.growlee.address = ConfigText(fixed_size=False)
21 config.plugins.growlee.password = ConfigPassword()
22 config.plugins.growlee.prowl_api_key = ConfigText(fixed_size=False)
23 config.plugins.growlee.protocol = ConfigSelection(default="growl", choices = [("growl", "Growl"), ("snarl", "Snarl"), ("prowl", "Prowl")])
24 config.plugins.growlee.level = ConfigSelection(default=-1, choices = [(-1, _("Low (Yes/No)")), (0, _("Normal (Information)")), (1, _("High (Warning)")), (2, _("Highest (Emergency)"))])
25 config.plugins.growlee.blacklist = ConfigSet(choices = [])
26
27 class GrowleeConfiguration(Screen, ConfigListScreen):
28         def __init__(self, session):
29                 Screen.__init__(self, session)
30                 self.skinName = [ "GrowleeConfiguration", "Setup" ]
31
32                 # Buttons
33                 self["key_red"] = StaticText(_("Cancel"))
34                 self["key_green"] = StaticText(_("OK"))
35
36                 # Summary
37                 self.setup_title = "Growlee Configuration"
38                 self.onChangedEntry = []
39
40                 # Define Actions
41                 self["actions"] = ActionMap(["SetupActions"],
42                         {
43                                 "cancel": self.keyCancel,
44                                 "save": self.keySave,
45                         }
46                 )
47
48                 config.plugins.growlee.protocol.addNotifier(self.setupList, initial_call=False)
49                 ConfigListScreen.__init__(
50                         self,
51                         [],
52                         session=session,
53                         on_change=self.changed
54                 )
55
56                 # Trigger change
57                 self.setupList()
58                 self.changed()
59
60         def changed(self):
61                 for x in self.onChangedEntry:
62                         x()
63
64         def setupList(self, *args):
65                 l = [
66                         getConfigListEntry(_("Type"), config.plugins.growlee.protocol),
67                         getConfigListEntry(_("Minimum Priority"), config.plugins.growlee.level),
68                         getConfigListEntry(_("Send Notifications?"), config.plugins.growlee.enable_outgoing),
69                 ]
70
71                 proto = config.plugins.growlee.protocol.value
72                 if proto ==  "prowl":
73                         l.append(getConfigListEntry(_("API Key"), config.plugins.growlee.prowl_api_key))
74                 else:
75                         l.extend((
76                                 getConfigListEntry(_("Receive Notifications?"), config.plugins.growlee.enable_incoming),
77                                 getConfigListEntry(_("Address"), config.plugins.growlee.address),
78                         ))
79                         if proto == "growl":
80                                 l.append(
81                                         getConfigListEntry(_("Password"), config.plugins.growlee.password)
82                                 )
83
84                 self["config"].list = l
85
86         def getCurrentEntry(self):
87                 cur = self["config"].getCurrent()
88                 return cur and cur[0]
89
90         def getCurrentValue(self):
91                 cur = self["config"].getCurrent()
92                 return cur and str(cur[1].getText())
93
94         def createSummary(self):
95                 return SetupSummary
96
97         def keySave(self):
98                 if self["config"].isChanged():
99                         def maybeConnect(*args, **kwargs):
100                                 if config.plugins.growlee.enable_incoming.value or config.plugins.growlee.enable_outgoing.value:
101                                         growleeConnection.listen()
102
103                         d = growleeConnection.stop()
104                         d.addCallback(maybeConnect).addErrback(emergencyDisable)
105
106                 self.saveAll()
107                 self.close()
108
109         def close(self):
110                 config.plugins.growlee.protocol.notifiers.remove(self.setupList)
111                 Screen.close(self)
112
113 def configuration(session, **kwargs):
114         session.open(GrowleeConfiguration)
115
116 def autostart(**kwargs):
117         # NOTE: we need to be the first one to be notified since other listeners
118         # may remove the notifications from the list for good
119         Notifications.notificationAdded.insert(0, gotNotification)
120
121         if config.plugins.growlee.enable_incoming.value or config.plugins.growlee.enable_outgoing.value:
122                 growleeConnection.listen()
123
124 def Plugins(**kwargs):
125         return [
126                 PluginDescriptor(
127                         where=PluginDescriptor.WHERE_SESSIONSTART,
128                         fnc=autostart,
129                 ),
130                 PluginDescriptor(
131                         name="Growlee",
132                         description=_("Configure Growlee"), 
133                         where=PluginDescriptor.WHERE_PLUGINMENU,
134                         fnc=configuration,
135                 ),
136         ]
137