oe2.2 only: add new plugin copy epg via ftp
[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 hasattr(Notifications, 'notificationQueue'):
19                 addedList = Notifications.notificationQueue.addedCB
20         else:
21                 addedList = Notifications.notificationAdded
22         if gotNotification in addedList:
23                 addedList.remove(gotNotification)
24         Notifications.AddPopup(
25                 _("Network error.\nDisabling Growlee until next restart!"),
26                 MessageBox.TYPE_ERROR,
27                 10
28         )
29
30 def gotNotification():
31         if hasattr(Notifications, 'notificationQueue'):
32                 notifications = Notifications.notificationQueue.queue
33                 def handler(note):
34                         return note.fnc, note.screen, note.args, note.kwargs, note.id
35         else:
36                 notifications = Notifications.notifications
37                 handler = lambda note: note
38         if notifications:
39                 _, screen, args, kwargs, id = handler(notifications[-1])
40                 if screen is MessageBox and id != NOTIFICATIONID:
41
42                         # NOTE: priority is in [-2; 2] but type is [0; 3] so map it
43                         # XXX: maybe priority==type-2 would be more appropriate
44                         priority = kwargs.get("type", 0) - 1
45                         timeout = kwargs.get("timeout", -1)
46
47                         if "text" in kwargs:
48                                 description = kwargs["text"]
49                         else:
50                                 description = args[0]
51                         description = description
52
53                         growleeConnection.sendNotification(title="Dreambox", description=description, priority=priority, timeout=timeout, id=id)
54
55 class GrowleeConnection:
56         connections = []
57         pending = 0
58
59         def sendNotification(self, title="Dreambox", description='', priority=-1, timeout=-1, id=""):
60                 for connection, host in self.connections:
61                         try:
62                                 level = int(host.level.value)
63                         except ValueError:
64                                 level = -1
65
66                         if connection and id not in host.blacklist.value and not priority < level:
67                                 connection.sendNotification(title=title, description=description, priority=priority, timeout=timeout)
68
69         def listen(self):
70                 if self.connections:
71                         return
72
73                 for host in config.plugins.growlee.hosts:
74                         if not (host.enable_outgoing.value or host.enable_incoming.value):
75                                 continue
76
77                         proto = host.protocol.value
78                         if proto == "prowl":
79                                 from Prowl import ProwlAPI
80                                 connection = ProwlAPI(host)
81                         elif proto == "growl":
82                                 from GrowlTalk import GrowlTalkAbstraction
83                                 connection = GrowlTalkAbstraction(host)
84                         elif proto == "gntp":
85                                 from GNTP import GNTPAbstraction
86                                 connection = GNTPAbstraction(host)
87                         elif proto == "snarl":
88                                 from SNP import SnarlNetworkProtocolAbstraction
89                                 connection = SnarlNetworkProtocolAbstraction(host)
90                         else: # proto == "syslog":
91                                 from Syslog import SyslogAbstraction
92                                 connection = SyslogAbstraction(host)
93
94                         self.connections.append((connection, host))
95
96         def maybeClose(self, resOrFail, defer = None):
97                 self.pending -= 1
98                 if self.pending == 0:
99                         if defer: defer.callback(True)
100
101         def stop(self):
102                 defer = Deferred()
103                 self.pending = 0
104                 for connection, host in self.connections:
105                         d = connection.stop()
106                         if d is not None:
107                                 self.pending += 1
108                                 d.addBoth(self.maybeClose, defer = defer)
109                 del self.connections[:]
110
111                 if self.pending == 0:
112                         reactor.callLater(1, defer, True)
113                 return defer
114
115 growleeConnection = GrowleeConnection()
116