changes for new config
[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 NumberActionMap
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"] = NumberActionMap(["SetupActions"],
44                 {
45                         "save": self.save,
46                         "cancel": self.cancel,
47                         "ok": self.save,
48                 }, -1)
49         def abort(self):
50                 print "aborting"
51
52         def save(self):
53                 for x in self["config"].list:
54                         x[1].save()
55                 if fritz_call is not None:
56                         fritz_call.connect()
57                 self.close()
58
59         def cancel(self):
60                 for x in self["config"].list:
61                         x[1].cancel()
62                 self.close()
63
64 class FritzProtocol(LineReceiver):
65         delimiter = "\r\n"
66         
67         def lineReceived(self, line):
68
69 #15.07.06 00:38:54;CALL;1;4;<provider>;<callee>;
70 #15.07.06 00:38:58;DISCONNECT;1;0;
71 #15.07.06 00:39:22;RING;0;<caller>;<outgoing msn>;
72 #15.07.06 00:39:27;DISCONNECT;0;0;
73
74                 a = line.split(';')
75                 (date, event) = a[0:2]
76                 
77                 if event == "RING":
78                         phone = a[4]
79                         number = a[3]
80                         text = _("incoming call!\n%s calls on %s!") % (number, phone)
81                         timeout = 10
82                 else:   
83                         return
84                 
85                 Notifications.AddNotification(MessageBox, text, type=MessageBox.TYPE_INFO, timeout=timeout)
86
87 class FritzClientFactory(ReconnectingClientFactory):
88
89         initialDelay = 20
90         maxDelay = 500
91         
92         def __init__(self):
93                 self.hangup_ok = False
94
95         def startedConnecting(self, connector):
96                 Notifications.AddNotification(MessageBox, _("Connecting to Fritz!Box..."), type=MessageBox.TYPE_INFO, timeout=2)
97         
98         def buildProtocol(self, addr):
99                 Notifications.AddNotification(MessageBox, _("Connected to Fritz!Box!"), type=MessageBox.TYPE_INFO, timeout=2)
100                 self.resetDelay()
101                 return FritzProtocol()
102         
103         def clientConnectionLost(self, connector, reason):
104                 if not self.hangup_ok:
105                         Notifications.AddNotification(MessageBox, _("Disconnected from\nFritz!Box! (%s)\nretrying...") % reason.getErrorMessage(), type=MessageBox.TYPE_INFO, timeout=4)
106                 ReconnectingClientFactory.clientConnectionLost(self, connector, reason)
107         
108         def clientConnectionFailed(self, connector, reason):
109                 Notifications.AddNotification(MessageBox, _("Connection to Fritz!Box\nfailed! (%s)\nretrying...") % reason.getErrorMessage(), type=MessageBox.TYPE_INFO, timeout=4)
110                 ReconnectingClientFactory.clientConnectionFailed(self, connector, reason)
111
112 class FritzCall:
113         def __init__(self):
114                 self.dialog = None
115                 self.d = None
116                 self.connect()
117                 
118         def connect(self):      
119                 self.abort()
120                 if config.FritzCall.enable.value:
121                         f = FritzClientFactory()
122                         self.d = (f, reactor.connectTCP("%d.%d.%d.%d" % tuple(config.FritzCall.hostname.value), 1012, f))
123
124         def shutdown(self):
125                 self.abort()
126
127         def abort(self):
128                 if self.d is not None:
129                         self.d[0].hangup_ok = True 
130                         self.d[0].stopTrying()
131                         self.d[1].disconnect()
132                         self.d = None
133
134 def main(session):
135         session.open(FritzCallSetup)
136
137 fritz_call = None
138
139 def autostart(reason, **kwargs):
140         global fritz_call
141         
142         # ouch, this is a hack  
143         if kwargs.has_key("session"):
144                 global my_global_session
145                 my_global_session = kwargs["session"]
146                 return
147         
148         print "autostart"
149         if reason == 0:
150                 fritz_call = FritzCall()
151         elif reason == 1:
152                 fritz_call.shutdown()
153                 fritz_call = None
154
155 def Plugins(**kwargs):
156         return [ PluginDescriptor(name="FritzCall", description="Display Fritzbox-Fon calls on screen", where = PluginDescriptor.WHERE_PLUGINMENU, fnc=main),
157                 PluginDescriptor(where = [PluginDescriptor.WHERE_SESSIONSTART, PluginDescriptor.WHERE_AUTOSTART], fnc = autostart) ]