i could swear that i tested this :)
[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
5 from . import NOTIFICATIONID
6
7 def emergencyDisable(*args, **kwargs):
8         global growleeConnection
9         if growleeConnection:
10                 growleeConnection.stop()
11
12         if gotNotification in Notifications.notificationAdded:
13                 Notifications.notificationAdded.remove(gotNotification)
14         Notifications.AddPopup(
15                 _("Network error.\nDisabling Growlee until next restart!"),
16                 MessageBox.TYPE_ERROR,
17                 10
18         )
19
20 def gotNotification():
21         notifications = Notifications.notifications
22         if notifications:
23                 _, screen, args, kwargs, id = notifications[-1]
24                 if screen is MessageBox and id != NOTIFICATIONID and id not in config.plugins.growlee.blacklist.value:
25
26                         # NOTE: priority is in [-2; 2] but type is [0; 3] so map it
27                         # XXX: maybe priority==type-2 would be more appropriate
28                         priority = kwargs.get("type", 0) - 1
29                         timeout = kwargs.get("timeout", -1)
30
31                         if "text" in kwargs:
32                                 description = kwargs["text"]
33                         else:
34                                 description = args[0]
35                         description = description.decode('utf-8')
36
37                         growleeConnection.sendNotification(title="Dreambox", description=description, priority=priority, timeout=timeout)
38
39 class GrowleeConnection:
40         connection = None
41
42         def sendNotification(self, title="Dreambox", description='', priority=-1, timeout=-1):
43                 try:
44                         level = int(config.plugins.growlee.level.value)
45                 except ValueError:
46                         level = -1
47
48                 if self.connection and not priority < level:
49                         self.connection.sendNotification(title=title, description=description, priority=priority, timeout=timeout)
50
51         def listen(self):
52                 if self.connection:
53                         return
54
55                 proto = config.plugins.growlee.protocol.value
56                 if proto == "prowl":
57                         from Prowl import ProwlAPI
58                         self.connection = ProwlAPI()
59                 elif proto == "growl":
60                         from GrowlTalk import GrowlTalkAbstraction
61                         self.connection = GrowlTalkAbstraction()
62                 else: # proto == "snarl":
63                         from SNP import SnarlNetworkProtocolAbstraction
64                         self.connection = SnarlNetworkProtocolAbstraction()
65
66         def stop(self):
67                 if self.connection:
68                         d = self.connection.stop()
69                         self.connection = None
70                         return d
71                 return None
72
73 growleeConnection = GrowleeConnection()
74