Ticket #1610: 0002-adhoc-master.patch
File 0002-adhoc-master.patch, 37.4 KB (added by erikos, 14 years ago) |
---|
-
extensions/deviceicon/network.py
diff --git a/extensions/deviceicon/network.py b/extensions/deviceicon/network.py index 94a4293..2e4d24c 100644
a b _GSM_STATE_CONNECTING = 2 65 65 _GSM_STATE_CONNECTED = 3 66 66 _GSM_STATE_NEED_AUTH = 4 67 67 68 def frequency_to_channel(frequency):69 ftoc = { 2412: 1, 2417: 2, 2422: 3, 2427: 4,70 2432: 5, 2437: 6, 2442: 7, 2447: 8,71 2452: 9, 2457: 10, 2462: 11, 2467: 12,72 2472: 13}73 return ftoc[frequency]74 68 75 69 class WirelessPalette(Palette): 76 70 __gtype_name__ = 'SugarWirelessPalette' 77 71 78 72 __gsignals__ = { 79 73 'deactivate-connection' : (gobject.SIGNAL_RUN_FIRST, 80 gobject.TYPE_NONE, ([])), 81 'create-connection' : (gobject.SIGNAL_RUN_FIRST, 82 gobject.TYPE_NONE, ([])), 74 gobject.TYPE_NONE, ([])) 83 75 } 84 76 85 def __init__(self, primary_text , can_create=True):77 def __init__(self, primary_text): 86 78 Palette.__init__(self, label=primary_text) 87 79 88 80 self._disconnect_item = None … … class WirelessPalette(Palette): 113 105 self._disconnect_item.connect('activate', self.__disconnect_activate_cb) 114 106 self.menu.append(self._disconnect_item) 115 107 116 if can_create:117 self._adhoc_item = gtk.MenuItem(_('Create new wireless network'))118 self._adhoc_item.connect('activate', self.__adhoc_activate_cb)119 self.menu.append(self._adhoc_item)120 self._adhoc_item.show()121 122 108 def set_connecting(self): 123 109 self.props.secondary_text = _('Connecting...') 124 110 … … class WirelessPalette(Palette): 145 131 def __disconnect_activate_cb(self, menuitem): 146 132 self.emit('deactivate-connection') 147 133 148 def __adhoc_activate_cb(self, menuitem):149 self.emit('create-connection')150 151 134 def _set_frequency(self, frequency): 152 135 try: 153 channel = frequency_to_channel(frequency)136 channel = network.frequency_to_channel(frequency) 154 137 except KeyError: 155 138 channel = 0 156 139 self._set_channel(channel) … … class GsmPalette(Palette): 314 297 315 298 class WirelessDeviceView(ToolButton): 316 299 317 _ICON_NAME = 'network-wireless'318 300 FRAME_POSITION_RELATIVE = 302 319 301 320 302 def __init__(self, device): … … class WirelessDeviceView(ToolButton): 333 315 self._active_ap_op = None 334 316 335 317 self._icon = PulsingIcon() 336 self._icon.props.icon_name = get_icon_state( self._ICON_NAME, 0)318 self._icon.props.icon_name = get_icon_state('network-wireless', 0) 337 319 self._inactive_color = xocolor.XoColor( \ 338 320 "%s,%s" % (style.COLOR_BUTTON_GREY.get_svg(), 339 321 style.COLOR_TRANSPARENT.get_svg())) … … class WirelessDeviceView(ToolButton): 347 329 self._palette = WirelessPalette(self._name) 348 330 self._palette.connect('deactivate-connection', 349 331 self.__deactivate_connection_cb) 350 self._palette.connect('create-connection',351 self.__create_connection_cb)352 332 self.set_palette(self._palette) 353 333 self._palette.set_group_id('frame') 354 334 … … class WirelessDeviceView(ToolButton): 418 398 def __ap_properties_changed_cb(self, properties): 419 399 self._update_properties(properties) 420 400 421 def _name_encodes_colors(self):422 """Match #XXXXXX,#YYYYYY at the end of the network name"""423 return self._name[-7] == '#' and self._name[-8] == ',' \424 and self._name[-15] == '#'425 426 401 def _update_properties(self, properties): 427 402 if 'Mode' in properties: 428 403 self._mode = properties['Mode'] … … class WirelessDeviceView(ToolButton): 438 413 self._frequency = properties['Frequency'] 439 414 440 415 if self._color == None: 441 if self._mode == network.NM_802_11_MODE_ADHOC \ 442 and self._name_encodes_colors(): 443 encoded_color = self._name.split("#", 1) 444 if len(encoded_color) == 2: 445 self._color = xocolor.XoColor('#' + encoded_color[1]) 416 if self._mode == network.NM_802_11_MODE_ADHOC and \ 417 self._name.startswith('Ad-hoc Network'): 418 self._color = profile.get_color() 446 419 else: 447 420 sha_hash = hashlib.sha1() 448 421 data = self._name + hex(self._flags) … … class WirelessDeviceView(ToolButton): 478 451 else: 479 452 state = network.DEVICE_STATE_UNKNOWN 480 453 481 if state == network.DEVICE_STATE_ACTIVATED: 482 icon_name = '%s-connected' % self._ICON_NAME 483 else: 484 icon_name = self._ICON_NAME 454 if self._mode != network.NM_802_11_MODE_ADHOC and \ 455 self._name.startswith('Ad-hoc Network') == False: 456 if state == network.DEVICE_STATE_ACTIVATED: 457 icon_name = '%s-connected' % 'network-wireless' 458 else: 459 icon_name = 'network-wireless' 485 460 486 icon_name = get_icon_state(icon_name, self._strength) 487 if icon_name: 488 self._icon.props.icon_name = icon_name 461 icon_name = get_icon_state(icon_name, self._strength) 462 if icon_name: 463 self._icon.props.icon_name = icon_name 464 else: 465 try: 466 channel = network.frequency_to_channel(self._frequency) 467 except KeyError: 468 channel = 1 469 if state == network.DEVICE_STATE_ACTIVATED: 470 self._icon.props.icon_name = 'network-adhoc-%s-connected' \ 471 % channel 472 else: 473 self._icon.props.icon_name = 'network-adhoc-%s' % channel 474 self._icon.props.base_color = profile.get_color() 489 475 490 476 if state == network.DEVICE_STATE_PREPARE or \ 491 477 state == network.DEVICE_STATE_CONFIG or \ … … class WirelessDeviceView(ToolButton): 524 510 netmgr.DeactivateConnection(conn_o) 525 511 break 526 512 527 def __create_connection_cb(self, palette, data=None):528 """Create an 802.11 IBSS network.529 530 The user's color is encoded at the end of the network name. The network531 name is truncated so that it does not exceed the 32 byte SSID limit.532 """533 client = gconf.client_get_default()534 nick = client.get_string('/desktop/sugar/user/nick').decode('utf-8')535 color = client.get_string('/desktop/sugar/user/color')536 color_suffix = ' %s' % color537 538 format = _('%s\'s network').encode('utf-8')539 extra_length = (len(format) - len('%s')) + len(color_suffix)540 name_limit = 32 - extra_length541 542 # truncate the nick and use a regex to drop any partial characters543 # at the end544 nick = nick.encode('utf-8')[:name_limit]545 pattern = "([\xf6-\xf7][\x80-\xbf]{0,2}|[\xe0-\xef][\x80-\xbf]{0,1}|[\xc0-\xdf])$"546 nick = re.sub(pattern, '', nick)547 548 connection_name = format % nick549 connection_name += color_suffix550 551 connection = network.find_connection_by_ssid(connection_name)552 if connection is None:553 settings = Settings()554 settings.connection.id = 'Auto ' + connection_name555 uuid = settings.connection.uuid = unique_id()556 settings.connection.type = '802-11-wireless'557 settings.wireless.ssid = dbus.ByteArray(connection_name)558 settings.wireless.band = 'bg'559 settings.wireless.mode = 'adhoc'560 settings.ip4_config = IP4Config()561 settings.ip4_config.method = 'link-local'562 563 connection = network.add_connection(uuid, settings)564 565 obj = self._bus.get_object(_NM_SERVICE, _NM_PATH)566 netmgr = dbus.Interface(obj, _NM_IFACE)567 568 netmgr.ActivateConnection(network.SETTINGS_SERVICE,569 connection.path,570 self._device.object_path,571 '/',572 reply_handler=self.__activate_reply_cb,573 error_handler=self.__activate_error_cb)574 575 513 def __activate_reply_cb(self, connection): 576 514 logging.debug('Network created: %s', connection) 577 515 -
src/jarabe/desktop/meshbox.py
diff --git a/src/jarabe/desktop/meshbox.py b/src/jarabe/desktop/meshbox.py index a04922b..e1c15df 100644
a b 1 1 # Copyright (C) 2006-2007 Red Hat, Inc. 2 2 # Copyright (C) 2009 Tomeu Vizoso, Simon Schampijer 3 # Copyright (C) 2009 One Laptop per Child3 # Copyright (C) 2009-2010 One Laptop per Child 4 4 # 5 5 # This program is free software; you can redistribute it and/or modify 6 6 # it under the terms of the GNU General Public License as published by … … from jarabe.model.network import IP4Config 53 53 from jarabe.model.network import WirelessSecurity 54 54 from jarabe.model.network import AccessPoint 55 55 from jarabe.model.olpcmesh import OlpcMeshManager 56 from jarabe.model.adhoc import AdHocManager 56 57 57 58 _NM_SERVICE = 'org.freedesktop.NetworkManager' 58 59 _NM_IFACE = 'org.freedesktop.NetworkManager' … … class WirelessNetworkView(CanvasPulsingIcon): 89 90 self._connection = None 90 91 self._color = None 91 92 92 if self._mode == network.NM_802_11_MODE_ADHOC \ 93 and self._name_encodes_colors(): 94 encoded_color = self._name.split("#", 1) 95 if len(encoded_color) == 2: 96 self._color = xocolor.XoColor('#' + encoded_color[1]) 93 if self._mode == network.NM_802_11_MODE_ADHOC and \ 94 self._name.startswith('Ad-hoc Network'): 95 self._color = profile.get_color() 97 96 else: 98 97 sha_hash = hashlib.sha1() 99 98 data = self._name + hex(self._flags) … … class WirelessNetworkView(CanvasPulsingIcon): 115 114 self.set_palette(self._palette) 116 115 self._palette_icon.props.xo_color = self._color 117 116 118 if network.find_connection_by_ssid(self._name) is not None: 119 self.props.badge_name = "emblem-favorite" 120 self._palette_icon.props.badge_name = "emblem-favorite" 121 elif initial_ap.flags == network.NM_802_11_AP_FLAGS_PRIVACY: 122 self.props.badge_name = "emblem-locked" 123 self._palette_icon.props.badge_name = "emblem-locked" 117 if self._mode != network.NM_802_11_MODE_ADHOC: 118 if network.find_connection_by_ssid(self._name) is not None: 119 self.props.badge_name = "emblem-favorite" 120 self._palette_icon.props.badge_name = "emblem-favorite" 121 elif self._flags == network.NM_802_11_AP_FLAGS_PRIVACY: 122 self.props.badge_name = "emblem-locked" 123 self._palette_icon.props.badge_name = "emblem-locked" 124 else: 125 self.props.badge_name = None 126 self._palette_icon.props.badge_name = None 124 127 else: 125 128 self.props.badge_name = None 126 129 self._palette_icon.props.badge_name = None … … class WirelessNetworkView(CanvasPulsingIcon): 146 149 path=self._device.object_path, 147 150 dbus_interface=_NM_WIRELESS_IFACE) 148 151 149 def _name_encodes_colors(self):150 """Match #XXXXXX,#YYYYYY at the end of the network name"""151 return self._name[-7] == '#' and self._name[-8] == ',' \152 and self._name[-15] == '#'153 154 152 def _create_palette(self): 155 153 icon_name = get_icon_state(_AP_ICON_NAME, self._strength) 156 154 self._palette_icon = Icon(icon_name=icon_name, … … class WirelessNetworkView(CanvasPulsingIcon): 221 219 else: 222 220 state = network.DEVICE_STATE_UNKNOWN 223 221 224 if state == network.DEVICE_STATE_ACTIVATED: 225 connection = network.find_connection_by_ssid(self._name) 226 if connection: 227 if self._mode == network.NM_802_11_MODE_INFRA: 228 connection.set_connected() 229 230 icon_name = '%s-connected' % _AP_ICON_NAME 231 else: 232 icon_name = _AP_ICON_NAME 233 234 icon_name = get_icon_state(icon_name, self._strength) 235 if icon_name: 222 if self._mode == network.NM_802_11_MODE_ADHOC and \ 223 self._name.startswith('Ad-hoc Network'): 224 channel = max([1] + [ap.channel for ap in 225 self._access_points.values()]) 226 if state == network.DEVICE_STATE_ACTIVATED: 227 icon_name = 'network-adhoc-%s-connected' % channel 228 else: 229 icon_name = 'network-adhoc-%s' % channel 236 230 self.props.icon_name = icon_name 237 231 icon = self._palette.props.icon 238 232 icon.props.icon_name = icon_name 233 else: 234 if state == network.DEVICE_STATE_ACTIVATED: 235 connection = network.find_connection_by_ssid(self._name) 236 if connection: 237 if self._mode == network.NM_802_11_MODE_INFRA: 238 connection.set_connected() 239 icon_name = '%s-connected' % _AP_ICON_NAME 240 else: 241 icon_name = _AP_ICON_NAME 242 243 icon_name = get_icon_state(icon_name, self._strength) 244 if icon_name: 245 self.props.icon_name = icon_name 246 icon = self._palette.props.icon 247 icon.props.icon_name = icon_name 239 248 240 249 if state == network.DEVICE_STATE_PREPARE or \ 241 250 state == network.DEVICE_STATE_CONFIG or \ … … class WirelessNetworkView(CanvasPulsingIcon): 444 453 dbus_interface=_NM_WIRELESS_IFACE) 445 454 446 455 456 class AdHocView(CanvasPulsingIcon): 457 def __init__(self, manager, channel): 458 CanvasPulsingIcon.__init__(self, 459 icon_name='network-adhoc-%s' % channel, 460 size=style.STANDARD_ICON_SIZE, cache=True) 461 self._bus = dbus.SystemBus() 462 self._manager = manager 463 self._device = manager.device 464 self._channel = channel 465 self._icon_name = 'network-adhoc-%s' % self._channel 466 self._disconnect_item = None 467 self._connect_item = None 468 self._palette_icon = None 469 self._greyed_out = False 470 self._name = "Ad-hoc Network %d" % channel 471 self._device_state = None 472 self._connection = None 473 self._active = False 474 475 self.connect('button-release-event', self.__button_release_event_cb) 476 477 interface_props = dbus.Interface(self._device, 478 'org.freedesktop.DBus.Properties') 479 interface_props.Get(_NM_DEVICE_IFACE, 'State', 480 reply_handler=self.__get_device_state_reply_cb, 481 error_handler=self.__get_device_state_error_cb) 482 483 self._bus.add_signal_receiver(self.__device_state_changed_cb, 484 signal_name='StateChanged', 485 path=self._device.object_path, 486 dbus_interface=_NM_DEVICE_IFACE) 487 self._bus.add_signal_receiver(self.__wireless_properties_changed_cb, 488 signal_name='PropertiesChanged', 489 path=self._device.object_path, 490 dbus_interface=_NM_WIRELESS_IFACE) 491 492 pulse_color = XoColor('%s,%s' % (style.COLOR_BUTTON_GREY.get_svg(), 493 style.COLOR_TRANSPARENT.get_svg())) 494 self.props.pulse_color = pulse_color 495 self._state_color = XoColor('%s,%s' % \ 496 (profile.get_color().get_stroke_color(), 497 style.COLOR_TRANSPARENT.get_svg())) 498 self.props.base_color = self._state_color 499 self._palette = self._create_palette() 500 self.set_palette(self._palette) 501 self._palette_icon.props.xo_color = self._state_color 502 503 def _create_palette(self): 504 self._palette_icon = Icon(icon_name=self._icon_name, 505 icon_size=style.STANDARD_ICON_SIZE) 506 507 _palette = palette.Palette(_("Ad-hoc Network %d") % self._channel, 508 icon=self._palette_icon) 509 510 self._connect_item = MenuItem(_('Connect'), 'dialog-ok') 511 self._connect_item.connect('activate', self.__connect_activate_cb) 512 _palette.menu.append(self._connect_item) 513 514 self._disconnect_item = MenuItem(_('Disconnect'), 'media-eject') 515 self._disconnect_item.connect('activate', 516 self.__disconnect_activate_cb) 517 _palette.menu.append(self._disconnect_item) 518 519 return _palette 520 521 def __button_release_event_cb(self, icon, event): 522 self._manager.activate_channel(self._channel) 523 524 def __connect_activate_cb(self, icon): 525 self._manager.activate_channel(self._channel) 526 527 def __disconnect_activate_cb(self, icon): 528 obj = self._bus.get_object(_NM_SERVICE, _NM_PATH) 529 netmgr = dbus.Interface(obj, _NM_IFACE) 530 531 netmgr_props = dbus.Interface(netmgr, dbus.PROPERTIES_IFACE) 532 active_connections_o = netmgr_props.Get(_NM_IFACE, 'ActiveConnections') 533 534 for conn_o in active_connections_o: 535 obj = self._bus.get_object(_NM_IFACE, conn_o) 536 props = dbus.Interface(obj, dbus.PROPERTIES_IFACE) 537 state = props.Get(_NM_ACTIVE_CONN_IFACE, 'State') 538 if state == network.NM_ACTIVE_CONNECTION_STATE_ACTIVATED: 539 ap_o = props.Get(_NM_ACTIVE_CONN_IFACE, 'SpecificObject') 540 if ap_o != '/': 541 netmgr.DeactivateConnection(conn_o) 542 543 def __get_device_state_reply_cb(self, state): 544 self._device_state = state 545 self._update() 546 547 def __get_device_state_error_cb(self, err): 548 logging.error('Error getting the device state: %s', err) 549 550 def __device_state_changed_cb(self, new_state, old_state, reason): 551 self._device_state = new_state 552 self._update() 553 554 def __wireless_properties_changed_cb(self, properties): 555 if 'ActiveAccessPoint' in properties and \ 556 properties['ActiveAccessPoint'] is not '/': 557 active_ap = self._bus.get_object(_NM_SERVICE, 558 properties['ActiveAccessPoint']) 559 props = dbus.Interface(active_ap, dbus.PROPERTIES_IFACE) 560 561 props.GetAll(_NM_ACCESSPOINT_IFACE, byte_arrays=True, 562 reply_handler=self.__get_all_ap_props_reply_cb, 563 error_handler=self.__get_all_ap_props_error_cb) 564 565 def __get_all_ap_props_reply_cb(self, properties): 566 if properties['Mode'] == network.NM_802_11_MODE_ADHOC: 567 channel = network.frequency_to_channel(properties['Frequency']) 568 if self._channel == channel: 569 self._active = True 570 else: 571 self._active = False 572 else: 573 self._active = False 574 self._update() 575 576 def __get_all_ap_props_error_cb(self, err): 577 logging.error('Error getting the access point properties: %s', err) 578 579 def _update(self): 580 if self._active: 581 state = self._device_state 582 else: 583 state = network.DEVICE_STATE_UNKNOWN 584 585 if state == network.DEVICE_STATE_ACTIVATED: 586 icon_name = '%s-connected' % self._icon_name 587 else: 588 icon_name = self._icon_name 589 590 self.props.base_color = self._state_color 591 self._palette_icon.props.xo_color = self._state_color 592 593 if icon_name is not None: 594 self.props.icon_name = icon_name 595 icon = self._palette.props.icon 596 icon.props.icon_name = icon_name 597 598 if state in [network.DEVICE_STATE_PREPARE, 599 network.DEVICE_STATE_CONFIG, 600 network.DEVICE_STATE_NEED_AUTH, 601 network.DEVICE_STATE_IP_CONFIG]: 602 if self._disconnect_item: 603 self._disconnect_item.show() 604 self._connect_item.hide() 605 self._palette.props.secondary_text = _('Connecting...') 606 self.props.pulsing = True 607 elif state == network.DEVICE_STATE_ACTIVATED: 608 if self._disconnect_item: 609 self._disconnect_item.show() 610 self._connect_item.hide() 611 self._palette.props.secondary_text = _('Connected') 612 self.props.pulsing = False 613 else: 614 if self._disconnect_item: 615 self._disconnect_item.hide() 616 self._connect_item.show() 617 self._palette.props.secondary_text = None 618 self.props.pulsing = False 619 620 def _update_color(self): 621 if self._greyed_out: 622 self.props.base_color = XoColor('#D5D5D5,#D5D5D5') 623 else: 624 self.props.base_color = self._state_color 625 626 def indicate_population(self, state): 627 if state == True: 628 self._state_color = profile.get_color() 629 self.props.base_color = self._state_color 630 self._palette_icon.props.xo_color = self._state_color 631 else: 632 color = '%s,%s' % (profile.get_color().get_stroke_color(), 633 style.COLOR_TRANSPARENT.get_svg()) 634 self._state_color = XoColor(color) 635 self.props.base_color = self._state_color 636 self._palette_icon.props.xo_color = self._state_color 637 638 def set_filter(self, query): 639 self._greyed_out = self._name.lower().find(query) == -1 640 self._update_color() 641 642 def disconnect(self): 643 self._bus.remove_signal_receiver(self.__device_state_changed_cb, 644 signal_name='StateChanged', 645 path=self._device.object_path, 646 dbus_interface=_NM_DEVICE_IFACE) 647 self._bus.remove_signal_receiver(self.__wireless_properties_changed_cb, 648 signal_name='PropertiesChanged', 649 path=self._device.object_path, 650 dbus_interface=_NM_WIRELESS_IFACE) 651 652 447 653 class OlpcMeshView(CanvasPulsingIcon): 448 654 def __init__(self, mesh_mgr, channel): 449 655 CanvasPulsingIcon.__init__(self, icon_name=_OLPC_MESH_ICON_NAME, … … class NetworkManagerObserver(object): 821 1027 self._bus = None 822 1028 self._devices = {} 823 1029 self._netmgr = None 1030 self._has_mesh_device = False 1031 self._check_mesh_source = 0 824 1032 825 1033 def listen(self): 826 1034 try: … … class NetworkManagerObserver(object): 840 1048 self._bus.add_signal_receiver(self.__device_removed_cb, 841 1049 signal_name='DeviceRemoved', 842 1050 dbus_interface=_NM_IFACE) 1051 self._bus.add_signal_receiver(self.__properties_changed_cb, 1052 signal_name='PropertiesChanged', 1053 dbus_interface=_NM_IFACE) 843 1054 844 1055 settings = network.get_settings() 845 1056 if settings is not None: … … class NetworkManagerObserver(object): 884 1095 device_type = props.Get(_NM_DEVICE_IFACE, 'DeviceType') 885 1096 if device_type == network.DEVICE_TYPE_802_11_WIRELESS: 886 1097 self._devices[device_o] = DeviceObserver(self._box, device) 1098 if self._check_mesh_source != 0: 1099 gobject.source_remove(self._check_mesh_source) 1100 self._check_mesh_source = gobject.timeout_add( \ 1101 5, self._add_adhoc_networks, device) 887 1102 elif device_type == network.DEVICE_TYPE_802_11_OLPC_MESH: 1103 self._has_mesh_device = True 888 1104 self._box.enable_olpc_mesh(device) 889 1105 890 1106 def _get_device_path_error_cb(self, err): … … class NetworkManagerObserver(object): 898 1114 observer = self._devices[device_o] 899 1115 observer.disconnect() 900 1116 del self._devices[device_o] 1117 if self._has_mesh_device == False: 1118 self._box.remove_adhoc_networks() 901 1119 return 902 1120 903 1121 device = self._bus.get_object(_NM_SERVICE, device_o) … … class NetworkManagerObserver(object): 906 1124 if device_type == network.DEVICE_TYPE_802_11_OLPC_MESH: 907 1125 self._box.disable_olpc_mesh(device) 908 1126 1127 def _add_adhoc_networks(self, device): 1128 """If we do not find mesh hardware we create Ad-hoc networks.""" 1129 if self._has_mesh_device == False: 1130 self._box.add_adhoc_networks(device) 1131 return False 1132 1133 def __properties_changed_cb(self, properties): 1134 if 'WirelessHardwareEnabled' in properties: 1135 if properties['WirelessHardwareEnabled'] == False: 1136 if self._has_mesh_device == False: 1137 self._box.remove_adhoc_networks() 1138 if properties['WirelessHardwareEnabled'] == True: 1139 for device in self._devices: 1140 if self._has_mesh_device == False: 1141 self._box.add_adhoc_networks(device) 1142 1143 909 1144 class MeshBox(gtk.VBox): 910 1145 __gtype_name__ = 'SugarMeshBox' 911 1146 … … class MeshBox(gtk.VBox): 915 1150 gobject.GObject.__init__(self) 916 1151 917 1152 self.wireless_networks = {} 1153 self._adhoc_manager = None 918 1154 919 1155 self._model = neighborhood.get_model() 920 1156 self._buddies = {} … … class MeshBox(gtk.VBox): 1072 1308 ap.disconnect() 1073 1309 return 1074 1310 1311 if self._adhoc_manager != None and \ 1312 ap.name.startswith('Ad-hoc Network') and \ 1313 ap.mode == network.NM_802_11_MODE_ADHOC: 1314 1315 if old_hash is None: # new Ad-hoc network finished initializing 1316 self._adhoc_manager.add_access_point(ap) 1317 # we are called as well in other cases but we do not need to 1318 # act here as we don't display signal strength for Ad-hoc networks 1319 return 1320 1075 1321 if old_hash is None: # new AP finished initializing 1076 1322 self._add_ap_to_network(ap) 1077 1323 return … … class MeshBox(gtk.VBox): 1094 1340 ap.initialize() 1095 1341 1096 1342 def remove_access_point(self, ap_o): 1343 if self._adhoc_manager is not None: 1344 if self._adhoc_manager.remove_access_point(ap_o) == True: 1345 return 1346 1097 1347 # we don't keep an index of ap object path to network, but since 1098 1348 # we'll only ever have a handful of networks, just try them all... 1099 1349 for net in self.wireless_networks.values(): … … class MeshBox(gtk.VBox): 1110 1360 # it (e.g. olpc-mesh adhoc network) 1111 1361 logging.debug('Can not remove access point %s' % ap_o) 1112 1362 1363 def add_adhoc_networks(self, device): 1364 if self._adhoc_manager is None: 1365 self._adhoc_manager = AdHocManager(device) 1366 for channel in self._adhoc_manager.channels: 1367 self._add_adhoc_network_icon(self._adhoc_manager, channel) 1368 self._adhoc_manager.autoconnect() 1369 1370 def remove_adhoc_networks(self): 1371 for channel in self._adhoc_manger.channels: 1372 icon = self._adhoc_manager.remove_network(channel) 1373 if icon is not None: 1374 icon.disconnect() 1375 self._layout.remove(icon) 1376 1377 def _add_adhoc_network_icon(self, adhoc_manager, channel): 1378 icon = AdHocView(adhoc_manager, channel) 1379 self._layout.add(icon) 1380 self._adhoc_manager.add_network(channel, icon) 1381 1113 1382 def _add_olpc_mesh_icon(self, mesh_mgr, channel): 1114 1383 icon = OlpcMeshView(mesh_mgr, channel) 1115 1384 self._layout.add(icon) -
src/jarabe/model/Makefile.am
diff --git a/src/jarabe/model/Makefile.am b/src/jarabe/model/Makefile.am index e9f0700..4650c3b 100644
a b 1 1 sugardir = $(pythondir)/jarabe/model 2 2 sugar_PYTHON = \ 3 adhoc.py \ 3 4 __init__.py \ 4 5 buddy.py \ 5 6 bundleregistry.py \ -
new file src/jarabe/model/adhoc.py
diff --git a/src/jarabe/model/adhoc.py b/src/jarabe/model/adhoc.py new file mode 100644 index 0000000..828451e
- + 1 # Copyright (C) 2010 One Laptop per Child 2 # 3 # This program is free software; you can redistribute it and/or modify 4 # it under the terms of the GNU General Public License as published by 5 # the Free Software Foundation; either version 2 of the License, or 6 # (at your option) any later version. 7 # 8 # This program is distributed in the hope that it will be useful, 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 # GNU General Public License for more details. 12 # 13 # You should have received a copy of the GNU General Public License 14 # along with this program; if not, write to the Free Software 15 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 16 17 import logging 18 19 import dbus 20 import gobject 21 22 from jarabe.model import network 23 from jarabe.model.network import Settings 24 from sugar.util import unique_id 25 from jarabe.model.network import IP4Config 26 27 _NM_SERVICE = 'org.freedesktop.NetworkManager' 28 _NM_IFACE = 'org.freedesktop.NetworkManager' 29 _NM_PATH = '/org/freedesktop/NetworkManager' 30 _NM_DEVICE_IFACE = 'org.freedesktop.NetworkManager.Device' 31 _NM_OLPC_MESH_IFACE = 'org.freedesktop.NetworkManager.Device.OlpcMesh' 32 33 34 class Network(object): 35 """Representation of an Ad-hoc network""" 36 37 def __init__(self, icon, access_point): 38 self.icon = icon 39 self.access_point = access_point 40 41 42 class AdHocManager(object): 43 """To mimic the mesh behavior on devices where mesh hardware is 44 not available we support the creation of an Ad-hoc network on 45 three channels 1, 6, 11. If Sugar sees no "known" network when it 46 starts, it does autoconnect to an Ad-hoc network. 47 48 """ 49 50 timeout = 30 51 52 def __init__(self, device): 53 self._bus = dbus.SystemBus() 54 55 self.device = device 56 self._idle_source = 0 57 self._device_state = network.DEVICE_STATE_UNKNOWN 58 59 self.channels = [1, 6, 11] 60 self._networks = {} 61 62 props = dbus.Interface(device, 63 'org.freedesktop.DBus.Properties') 64 props.Get(_NM_DEVICE_IFACE, 'State', 65 reply_handler=self.__get_device_state_reply_cb, 66 error_handler=self.__get_state_error_cb) 67 68 self._bus.add_signal_receiver(self.__device_state_changed_cb, 69 signal_name='StateChanged', 70 path=self.device.object_path, 71 dbus_interface=_NM_DEVICE_IFACE) 72 73 def _have_configured_connections(self): 74 return len(network.get_settings().connections) > 0 75 76 def autoconnect(self): 77 """Autoconnect to an Ad-hoc network""" 78 if self._have_configured_connections(): 79 self._autoconnect_adhoc_timer() 80 else: 81 self._autoconnect_adhoc() 82 83 def __get_state_error_cb(self, err): 84 logging.debug('Error getting the device state: %s', err) 85 86 def __get_device_state_reply_cb(self, state): 87 self._device_state = state 88 89 def __device_state_changed_cb(self, new_state, old_state, reason): 90 self._device_state = new_state 91 92 def _autoconnect_adhoc_timer(self): 93 """Start a timer which basically looks for 30 seconds of inactivity 94 on the device, then does autoconnect to an Ad-hoc network. 95 96 """ 97 if self._idle_source != 0: 98 gobject.source_remove(self._idle_source) 99 self._idle_source = gobject.timeout_add_seconds(self.timeout, 100 self._idle_check) 101 102 def _autoconnect_adhoc(self): 103 """First we try if there is an Ad-hoc network that is used by other 104 learners in the area, if not we default to channel 1. 105 106 """ 107 if self._networks[1].access_point is not None: 108 self._connect(1) 109 elif self._networks[6].access_point is not None: 110 self._connect(6) 111 elif self._networks[11].access_point is not None: 112 self._connect(11) 113 else: 114 self._connect(1) 115 116 def _idle_check(self): 117 if self._device_state == network.DEVICE_STATE_DISCONNECTED: 118 logging.debug("Connect to Ad-hoc network due to inactivity.") 119 self._autoconnect_adhoc() 120 return False 121 122 def activate_channel(self, channel): 123 self._connect(channel) 124 125 def _connect(self, channel): 126 name = "Ad-hoc Network %d" % channel 127 connection = network.find_connection_by_ssid(name) 128 if connection is None: 129 settings = Settings() 130 settings.connection.id = name 131 settings.connection.uuid = unique_id() 132 settings.connection.type = '802-11-wireless' 133 settings.wireless.ssid = dbus.ByteArray(name) 134 settings.wireless.band = 'bg' 135 settings.wireless.channel = channel 136 settings.wireless.mode = 'adhoc' 137 settings.ip4_config = IP4Config() 138 settings.ip4_config.method = 'link-local' 139 140 connection = network.add_connection(name, settings) 141 142 obj = self._bus.get_object(_NM_SERVICE, _NM_PATH) 143 netmgr = dbus.Interface(obj, _NM_IFACE) 144 145 netmgr.ActivateConnection(network.SETTINGS_SERVICE, 146 connection.path, 147 self.device.object_path, 148 '/', 149 reply_handler=self.__activate_reply_cb, 150 error_handler=self.__activate_error_cb) 151 152 def __activate_reply_cb(self, connection): 153 logging.debug('Ad-hoc network created: %s', connection) 154 155 def __activate_error_cb(self, err): 156 logging.debug('Failed to create Ad-hoc network: %s', err) 157 158 def add_network(self, channel, icon): 159 if channel not in self._networks: 160 self._networks[channel] = Network(icon, None) 161 162 def remove_network(self, channel): 163 if channel in self._networks: 164 net = self._networks.pop(channel) 165 return net.icon 166 return None 167 168 def add_access_point(self, access_point): 169 """If a new adhoc network is announcd that is named like an Ad-hoc 170 network we take this as an indication that other learners are 171 using the network an indicate this. 172 173 """ 174 if ' 1' == access_point.name[-2:]: 175 self._networks[1].icon.indicate_population(True) 176 self._networks[1].access_point = access_point 177 elif '6' == access_point.name[-1]: 178 self._networks[6].icon.indicate_population(True) 179 self._networks[6].access_point = access_point 180 elif '11' == access_point.name[-2:]: 181 self._networks[11].icon.indicate_population(True) 182 self._networks[11].access_point = access_point 183 184 def remove_access_point(self, ap_object_path): 185 """When the last user of the network goes away we remove 186 the access point and indicate this. 187 188 """ 189 for net in self._networks.values(): 190 if net.access_point is not None: 191 if net.access_point.model.object_path == ap_object_path: 192 net.icon.indicate_population(False) 193 net.access_point.disconnect() 194 net.access_point = None 195 return True 196 return False -
src/jarabe/model/network.py
diff --git a/src/jarabe/model/network.py b/src/jarabe/model/network.py index 3a949da..63a35e5 100644
a b 1 1 # Copyright (C) 2008 Red Hat, Inc. 2 2 # Copyright (C) 2009 Tomeu Vizoso, Simon Schampijer 3 # Copyright (C) 2009 One Laptop per Child3 # Copyright (C) 2009-2010 One Laptop per Child 4 4 # Copyright (C) 2009 Paraguay Educa, Martin Abente 5 5 # Copyright (C) 2010 Plan Ceibal, Daniel Castelo 6 6 # … … GSM_PUK_PATH = '/desktop/sugar/network/gsm/puk' 99 99 _nm_settings = None 100 100 _conn_counter = 0 101 101 102 103 def frequency_to_channel(frequency): 104 ftoc = {2412: 1, 2417: 2, 2422: 3, 2427: 4, 105 2432: 5, 2437: 6, 2442: 7, 2447: 8, 106 2452: 9, 2457: 10, 2462: 11, 2467: 12, 107 2472: 13} 108 return ftoc[frequency] 109 110 102 111 class WirelessSecurity(object): 103 112 def __init__(self): 104 113 self.key_mgmt = None 105 114 self.proto = None 106 115 self.group = None 107 116 self.pairwise = None 117 self.channel = None 108 118 109 119 def get_dict(self): 110 120 wireless_security = {} … … class WirelessSecurity(object): 116 126 wireless_security['pairwise'] = self.pairwise 117 127 if self.group is not None: 118 128 wireless_security['group'] = self.group 129 if self.channel: 130 wireless['channel'] = self.channel 119 131 return wireless_security 120 132 121 133 class Wireless(object): … … class AccessPoint(gobject.GObject): 475 487 self.wpa_flags = 0 476 488 self.rsn_flags = 0 477 489 self.mode = 0 490 self.channel = 1 478 491 479 492 def initialize(self): 480 493 model_props = dbus.Interface(self.model, … … class AccessPoint(gobject.GObject): 544 557 self.rsn_flags = properties['RsnFlags'] 545 558 if 'Mode' in properties: 546 559 self.mode = properties['Mode'] 560 if 'Frequency' in properties: 561 try: 562 self.channel = frequency_to_channel(properties['Frequency']) 563 except KeyError: 564 self.channel = 1 565 547 566 self._initialized = True 548 567 self.emit('props-changed', old_hash) 549 568