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