From 4cd09ed5f83020a30cf75a570837d05b4c021adb Mon Sep 17 00:00:00 2001 From: Marco Pesenti Gritti Date: Sun, 12 Nov 2006 22:44:28 +0100 Subject: [PATCH] Add a simple dialog to enter the web hex key. Several typo fixed. Merged a patch from Dan. --- services/nm/nmclient.py | 15 +++++++++++++ services/nm/nminfo.py | 27 ++++++++++++++++-------- services/nm/wepkeydialog.py | 42 +++++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 9 deletions(-) create mode 100644 services/nm/wepkeydialog.py diff --git a/services/nm/nmclient.py b/services/nm/nmclient.py index d79919fe..ef71fd8e 100644 --- a/services/nm/nmclient.py +++ b/services/nm/nmclient.py @@ -30,6 +30,7 @@ from sugar.graphics.menu import Menu from sugar.graphics import style from sugar.graphics.iconcolor import IconColor from sugar.graphics.timeline import Timeline +from wepkeydialog import WEPKeyDialog from bubble import Bubble import nminfo @@ -784,6 +785,20 @@ class NMClientApp: # Auth algorithm should be a dropdown of: [Open System, Shared Key], # mapping to the values [IW_AUTH_ALG_OPEN_SYSTEM, IW_AUTH_ALG_SHARED_KEY] # above + + dialog = WEPKeyDialog() + response = dialog.run() + dialog.destroy() + + if response == gtk.RESPONSE_OK: + key = dialog.get_key() + self.nminfo.get_key_for_network_cb(key, wep_auth_alg, canceled=False) + else: + self.nminfo.get_key_for_network_cb(None, None, canceled=True) + + def cancel_get_key_for_network(self): + # Close the wireless key dialog and just have it return + # with the 'canceled' argument set to true pass def device_activation_stage_sig_handler(self, device, stage): diff --git a/services/nm/nminfo.py b/services/nm/nminfo.py index 3ba24906..a914a6cb 100644 --- a/services/nm/nminfo.py +++ b/services/nm/nminfo.py @@ -22,6 +22,8 @@ import time import os import binascii from ConfigParser import ConfigParser + +import nmclient try: from sugar import env except ImportError: @@ -371,18 +373,18 @@ class NMInfo(object): del net def get_key_for_network(self, dev_op, net_op, ssid, attempt, new_key, async_cb, async_err_cb): - if self._networks.has_key(ssid) and not new_key: + if self._allowed_networks.has_key(ssid) and not new_key: # We've got the info already - net = self._networks[ssid] + net = self._allowed_networks[ssid] async_cb(tuple(net.get_security())) return # Otherwise, ask the user for it net = None - dev = self._nm_client.get_dev(dev_op) + dev = self._nmclient.get_device(dev_op) if not dev: async_err_cb(NotFoundError("Device was unknown.")) - if dev.get_type() == DEVICE_TYPE_802_3_ETHERNET: + if dev.get_type() == nmclient.DEVICE_TYPE_802_3_ETHERNET: # We don't support wired 802.1x yet... async_err_cb(UnsupportedError("Device type is unsupported by NMI.")) @@ -390,10 +392,16 @@ class NMInfo(object): if not net: async_err_cb(NotFoundError("Network was unknown.")) - try: - (key, auth_alg) = self._nm_client.get_key_for_network() - except CanceledKeyRequestError, e: - # user canceled the key dialog; send the error back to NM + self._nmclient.get_key_for_network(net) + + def get_key_for_network_cb(self, key, auth_alg, canceled=False): + """ + Called by the NMClient when the Wireless Network Key dialog + is closed. + """ + if canceled: + e = CanceledKeyRequestError("Request was canceled.") + # key dialog dialog was canceled; send the error back to NM async_err_cb(e) return @@ -417,4 +425,5 @@ class NMInfo(object): async_cb(tuple(sec.get_properties())) def cancel_get_key_for_network(self): - pass + # Tell the NMClient to close the key request dialog + self._nmclient.cancel_get_key_for_network() diff --git a/services/nm/wepkeydialog.py b/services/nm/wepkeydialog.py new file mode 100644 index 00000000..dd61f1c6 --- /dev/null +++ b/services/nm/wepkeydialog.py @@ -0,0 +1,42 @@ +import gtk + +class WEPKeyDialog(gtk.Dialog): + def __init__(self): + gtk.Dialog.__init__(self) + + self.set_has_separator(False) + + self._entry = gtk.Entry() + self._entry.props.visibility = False + self._entry.connect('changed', self._entry_changed_cb) + self.vbox.pack_start(self._entry) + self._entry.show() + + self.add_buttons(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, + gtk.STOCK_OK, gtk.RESPONSE_OK) + + self.set_default_response(gtk.RESPONSE_OK) + self._update_response_sensitivity() + + def get_key(self): + return self._entry.get_text() + + def _entry_changed_cb(self, entry): + self._update_response_sensitivity() + + def _update_response_sensitivity(self): + key = self.get_key() + + is_hex = True + for c in key: + if not 'a' <= c <= 'f' and not '0' <= c <= '9': + is_hex = False + + valid_len = (len(key) == 10 or len(key) == 26) + self.set_response_sensitive(gtk.RESPONSE_OK, is_hex and valid_len) + +if __name__ == "__main__": + dialog = WEPKeyDialog() + dialog.run() + + print dialog.get_key()