fix false assumption when updating syntax
[enigma2-plugins.git] / growlee / src / GrowleeConnection.py
1 from Components.config import config
2 from Tools import Notifications
3 from Screens.MessageBox import MessageBox
4 from twisted.internet.defer import Deferred
5 from twisted.internet import reactor
6
7 from . import NOTIFICATIONID
8
9 def emergencyDisable(*args, **kwargs):
10         if args:
11                 try: args[0].printTraceback()
12                 except Exception: pass
13
14         global growleeConnection
15         if growleeConnection:
16                 growleeConnection.stop()
17
18         if gotNotification in Notifications.notificationAdded:
19                 Notifications.notificationAdded.remove(gotNotification)
20         Notifications.AddPopup(
21                 _("Network error.\nDisabling Growlee until next restart!"),
22                 MessageBox.TYPE_ERROR,
23                 10
24         )
25
26 def gotNotification():
27         notifications = Notifications.notifications
28         if notifications:
29                 _, screen, args, kwargs, id = notifications[-1]
30                 if screen is MessageBox and id != NOTIFICATIONID:
31
32                         # NOTE: priority is in [-2; 2] but type is [0; 3] so map it
33                         # XXX: maybe priority==type-2 would be more appropriate
34                         priority = kwargs.get("type", 0) - 1
35                         timeout = kwargs.get("timeout", -1)
36
37                         if "text" in kwargs:
38                                 description = kwargs["text"]
39                         else:
40                                 description = args[0]
41                         description = description
42
43                         growleeConnection.sendNotification(title="Dreambox", description=description, priority=priority, timeout=timeout, id=id)
44
45 class GrowleeConnection:
46         connections = []
47         pending = 0
48
49         def sendNotification(self, title="Dreambox", description='', priority=-1, timeout=-1, id=""):
50                 for connection, host in self.connections:
51                         try:
52                                 level = int(host.level.value)
53                         except ValueError:
54                                 level = -1
55
56                         if connection and id not in host.blacklist.value and not priority < level:
57                                 connection.sendNotification(title=title, description=description, priority=priority, timeout=timeout)
58
59         def listen(self):
60                 if self.connections:
61                         return
62
63                 for host in config.plugins.growlee.hosts:
64                         if not (host.enable_outgoing.value or host.enable_incoming.value):
65                                 continue
66
67                         proto = host.protocol.value
68                         if proto == "prowl":
69                                 from Prowl import ProwlAPI
70                                 connection = ProwlAPI(host)
71                         elif proto == "growl":
72                                 from GrowlTalk import GrowlTalkAbstraction
73                                 connection = GrowlTalkAbstraction(host)
74                         elif proto == "snarl":
75                                 from SNP import SnarlNetworkProtocolAbstraction
76                                 connection = SnarlNetworkProtocolAbstraction(host)
77                         else: # proto == "syslog":
78                                 from Syslog import SyslogAbstraction
79                                 connection = SyslogAbstraction(host)
80
81                         self.connections.append((connection, host))
82
83         def maybeClose(self, resOrFail, defer = None):
84                 self.pending -= 1
85                 if self.pending == 0:
86                         if defer: defer.callback(True)
87
88         def stop(self):
89                 defer = Deferred()
90                 self.pending = 0
91                 for connection, host in self.connections:
92                         d = connection.stop()
93                         if d is not None:
94                                 self.pending += 1
95                                 d.addBoth(self.maybeClose, defer = defer)
96                 del self.connections[:]
97
98                 if self.pending == 0:
99                         reactor.callLater(1, defer, True)
100                 return defer
101
102 growleeConnection = GrowleeConnection()
103