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