4.2.5r2
[enigma2.git] / usr / lib / enigma2 / python / Plugins / SystemPlugins / NetworkManager / plugin.py
1 # -*- coding: utf-8 -*-
2
3 from enigma import eNetworkManager, StringMap
4 from Components.config import config, ConfigBoolean
5 from Plugins.Plugin import PluginDescriptor
6 from Screens.MessageBox import MessageBox
7 from Tools.Log import Log
8
9 from MultiInputBox import MultiInputBox
10 from NetworkConfig import NetworkServiceConfig
11
12 from NetworkWizard import NetworkWizardNew
13
14 class NetworkAgent(object):
15         def __init__(self, session):
16                 self._nm = eNetworkManager.getInstance()
17                 self.session = session
18
19                 self._userInputField = None
20                 self._connected_signals = []
21                 self._userInputScreen = None
22                 ap = self._connected_signals.append
23                 ap( self._nm.userInputRequested.connect(self._userInputRequested) )
24                 ap( self._nm.userInputCanceled.connect(self._userInputCanceled) )
25                 ap( self._nm.errorReported.connect(self._errorReported) )
26
27         def _errorReported(self, svcpath, error):
28                 Log.w("Network service %s report an error: %s" %(svcpath, error))
29                 service = self._nm.getService(svcpath)
30                 svcname = svcpath
31                 if service:
32                         svcname = service.name()
33                 title = _("Network error on %s" %svcname)
34                 self.session.open(MessageBox, error, type=MessageBox.TYPE_ERROR, title=title)
35
36         def _userInputRequested(self, svcpath):
37                 Log.i(svcpath)
38                 dialog_values = self._nm.getUserInputRequestFields()
39                 for key,value in dialog_values.iteritems():
40                         Log.i("%s => %s" %(key, value))
41
42                 windowTitle = _("Network")
43                 svc = self._nm.getService(svcpath)
44                 if svc:
45                         windowTitle = svc.name()
46
47                 prev = dialog_values.get("PreviousPassphrase", None)
48                 if prev:
49                         del dialog_values["PreviousPassphrase"]
50                 #filter WPS until it's fixed
51                 wps = dialog_values.get("WPS", None)
52                 if wps:
53                         del dialog_values["WPS"]
54
55                 input_config = []
56                 if len(dialog_values) > 0:
57                         for key, value in dialog_values.iteritems():
58                                 input_config.append( self._createInputConfig(key, value, prev) ),
59                         self._userInputScreen = self.session.openWithCallback(self._onUserMultiInput, MultiInputBox, title=_("Input required"), windowTitle=windowTitle, config=input_config)
60                 else:
61                         self._nm.sendUserReply(StringMap()) #Cancel
62
63         def _createInputConfig(self, key, request_part, previousPassphrase):
64                         requirement = request_part["Requirement"] == "mandatory"
65                         val_type = MultiInputBox.TYPE_TEXT
66                         if request_part["Type"] == "wpspin":
67                                 val_type = MultiInputBox.TYPE_PIN
68                         if request_part["Type"] in ('psk', 'wep', 'passphrase'):
69                                 val_type = MultiInputBox.TYPE_PASSWORD
70
71                         value = ""
72                         if previousPassphrase and request_part["Type"] == previousPassphrase["Type"]:
73                                 value = str(previousPassphrase["value"])
74
75                         alternatives = request_part.get("Alternates", [])
76                         return {
77                                 "key" : key,
78                                 "value" : value,
79                                 "title" : key,
80                                 "required" : requirement,
81                                 "type" : val_type,
82                                 "alternatives" : alternatives,
83                         }
84
85         def _userInputCanceled(self):
86                 if self._userInputScreen:
87                         self._userInputScreen.close()
88                         self._userInputScreen = None
89                 self.session.open(MessageBox, _("There was no input for too long!"), type=MessageBox.TYPE_ERROR, title=_("Timeout!"))
90
91         def _onUserMultiInput(self, values):
92                 self._userInputScreen = None
93                 if values:
94                         self._nm.sendUserReply(StringMap(values))
95                 else:
96                         self._nm.sendUserReply(StringMap())
97
98         def shutdown(self):
99                 Log.i("cancelling any pending request")
100                 if self._userInputScreen:
101                         self._nm.sendUserReply(StringMap()) #cancel request
102
103 global networkagent
104 networkagent = None
105 def main(reason, **kwargs):
106         global networkagent
107         if reason == 0:
108                 session = kwargs.get("session", None)
109                 if session:
110                         networkagent = NetworkAgent(session)
111         elif reason == 1 and networkagent:
112                 networkagent.shutdown()
113
114 def nw_setup(session, **kwargs):
115         session.open(NetworkServiceConfig)
116
117 def nw_menu(menuid, **kwargs):
118         if menuid == "network":
119                 return [(_("Network Setup"), nw_setup, "nw_setup", 1)]
120         else:
121                 return []
122
123 config.misc.firstrun = ConfigBoolean(default = True)
124 def runNetworkWizard(*args, **kwargs):
125         return NetworkWizardNew(*args, **kwargs)
126
127 def Plugins(**kwargs):
128         lst = [
129                 PluginDescriptor(name="Network Agent", where=[PluginDescriptor.WHERE_SESSIONSTART,PluginDescriptor.WHERE_AUTOSTART], needsRestart=False, fnc=main),
130                 PluginDescriptor(name=_("Network"), description=_("Set up your Network connections"), where = PluginDescriptor.WHERE_MENU, needsRestart = True, fnc=nw_menu)
131         ]
132         if config.misc.firstrun.value:
133                 NetworkWizardNew.firstRun = True
134                 NetworkWizardNew.checkNetwork = True
135         return lst