fix priorities
[enigma2.git] / lib / python / Plugins / Extensions / FritzCall / plugin.py
1 from Screens.Screen import Screen
2 from Screens.MessageBox import MessageBox
3 from Components.ActionMap import ActionMap
4 from Components.Label import Label
5 from Plugins.Plugin import PluginDescriptor
6 from Tools import Notifications
7
8 from twisted.internet import reactor
9 from twisted.internet.protocol import ReconnectingClientFactory
10 from twisted.protocols.basic import LineReceiver
11
12 from enigma import eTimer
13
14 my_global_session = None
15
16 from Components.config import config, ConfigSubsection, ConfigIP, ConfigEnableDisable, getConfigListEntry
17 from Components.ConfigList import ConfigList, ConfigListScreen
18
19 config.FritzCall = ConfigSubsection()
20 config.FritzCall.hostname = ConfigIP(default = [192,168,178,254])
21 config.FritzCall.enable = ConfigEnableDisable(default = False)
22
23 class FritzCallSetup(ConfigListScreen, Screen):
24         skin = """
25                 <screen position="100,100" size="550,400" title="FritzCall Setup" >
26                 <widget name="config" position="20,10" size="460,350" scrollbarMode="showOnDemand" />
27                 </screen>"""
28
29
30         def __init__(self, session, args = None):
31                 from Tools.BoundFunction import boundFunction
32                 
33                 Screen.__init__(self, session)
34                 self.onClose.append(self.abort)
35                 
36                 # nun erzeugen wir eine liste von elementen fuer die menu liste.
37                 self.list = [ ]
38                 self.list.append(getConfigListEntry(_("Call monitoring"), config.FritzCall.enable))
39                 self.list.append(getConfigListEntry(_("Fritz!Box FON IP address"), config.FritzCall.hostname))
40                 ConfigListScreen.__init__(self, self.list)
41
42                 # DO NOT ASK.
43                 self["setupActions"] = ActionMap(["SetupActions"],
44                 {
45                         "save": self.save,
46                         "cancel": self.cancel,
47                         "ok": self.save,
48                 }, -2)
49
50         def abort(self):
51                 print "aborting"
52
53         def save(self):
54                 for x in self["config"].list:
55                         x[1].save()
56                 if fritz_call is not None:
57                         fritz_call.connect()
58                 self.close()
59
60         def cancel(self):
61                 for x in self["config"].list:
62                         x[1].cancel()
63                 self.close()
64
65 class FritzProtocol(LineReceiver):
66         delimiter = "\r\n"
67         
68         def lineReceived(self, line):
69
70 #15.07.06 00:38:54;CALL;1;4;<provider>;<callee>;
71 #15.07.06 00:38:58;DISCONNECT;1;0;
72 #15.07.06 00:39:22;RING;0;<caller>;<outgoing msn>;
73 #15.07.06 00:39:27;DISCONNECT;0;0;
74
75                 a = line.split(';')
76                 (date, event) = a[0:2]
77                 
78                 if event == "RING":
79                         phone = a[4]
80                         number = a[3]
81                         text = _("incoming call!\n%s calls on %s!") % (number, phone)
82                         timeout = 10
83                 else:   
84                         return
85                 
86                 Notifications.AddNotification(MessageBox, text, type=MessageBox.TYPE_INFO, timeout=timeout)
87
88 class FritzClientFactory(ReconnectingClientFactory):
89
90         initialDelay = 20
91         maxDelay = 500
92         
93         def __init__(self):
94                 self.hangup_ok = False
95
96         def startedConnecting(self, connector):
97                 Notifications.AddNotification(MessageBox, _("Connecting to Fritz!Box..."), type=MessageBox.TYPE_INFO, timeout=2)
98         
99         def buildProtocol(self, addr):
100                 Notifications.AddNotification(MessageBox, _("Connected to Fritz!Box!"), type=MessageBox.TYPE_INFO, timeout=2)
101                 self.resetDelay()
102                 return FritzProtocol()
103         
104         def clientConnectionLost(self, connector, reason):
105                 if not self.hangup_ok:
106                         Notifications.AddNotification(MessageBox, _("Disconnected from\nFritz!Box! (%s)\nretrying...") % reason.getErrorMessage(), type=MessageBox.TYPE_INFO, timeout=4)
107                 ReconnectingClientFactory.clientConnectionLost(self, connector, reason)
108         
109         def clientConnectionFailed(self, connector, reason):
110                 Notifications.AddNotification(MessageBox, _("Connection to Fritz!Box\nfailed! (%s)\nretrying...") % reason.getErrorMessage(), type=MessageBox.TYPE_INFO, timeout=4)
111                 ReconnectingClientFactory.clientConnectionFailed(self, connector, reason)
112
113 class FritzCall:
114         def __init__(self):
115                 self.dialog = None
116                 self.d = None
117                 self.connect()
118                 
119         def connect(self):      
120                 self.abort()
121                 if config.FritzCall.enable.value:
122                         f = FritzClientFactory()
123                         self.d = (f, reactor.connectTCP("%d.%d.%d.%d" % tuple(config.FritzCall.hostname.value), 1012, f))
124
125         def shutdown(self):
126                 self.abort()
127
128         def abort(self):
129                 if self.d is not None:
130                         self.d[0].hangup_ok = True 
131                         self.d[0].stopTrying()
132                         self.d[1].disconnect()
133                         self.d = None
134
135 def main(session):
136         session.open(FritzCallSetup)
137
138 fritz_call = None
139
140 def autostart(reason, **kwargs):
141         global fritz_call
142         
143         # ouch, this is a hack  
144         if kwargs.has_key("session"):
145                 global my_global_session
146                 my_global_session = kwargs["session"]
147                 return
148         
149         print "autostart"
150         if reason == 0:
151                 fritz_call = FritzCall()
152         elif reason == 1:
153                 fritz_call.shutdown()
154                 fritz_call = None
155
156 def Plugins(**kwargs):
157         return [ PluginDescriptor(name="FritzCall", description="Display Fritzbox-Fon calls on screen", where = PluginDescriptor.WHERE_PLUGINMENU, fnc=main),
158                 PluginDescriptor(where = [PluginDescriptor.WHERE_SESSIONSTART, PluginDescriptor.WHERE_AUTOSTART], fnc = autostart) ]