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