2006-10-15 01:24:45 +02:00
|
|
|
# Copyright (C) 2006, Red Hat, Inc.
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
|
2006-08-29 11:48:20 +02:00
|
|
|
import random
|
|
|
|
|
2006-10-03 16:31:32 +02:00
|
|
|
import hippo
|
2006-10-06 11:11:38 +02:00
|
|
|
import gobject
|
2007-03-29 16:10:34 +02:00
|
|
|
from gettext import gettext as _
|
2006-08-29 11:48:20 +02:00
|
|
|
|
2006-10-06 11:11:38 +02:00
|
|
|
from sugar.graphics.spreadbox import SpreadBox
|
|
|
|
from sugar.graphics.snowflakebox import SnowflakeBox
|
2006-10-03 16:31:32 +02:00
|
|
|
from sugar.graphics.canvasicon import CanvasIcon
|
2007-02-28 15:42:41 +01:00
|
|
|
from sugar.graphics import color
|
2007-03-16 03:03:42 +01:00
|
|
|
from sugar.graphics import xocolor
|
2007-02-28 16:19:01 +01:00
|
|
|
from sugar.graphics import canvasicon
|
2007-03-29 16:10:34 +02:00
|
|
|
from sugar.graphics import units
|
2007-02-26 12:23:00 +01:00
|
|
|
from model import accesspointmodel
|
2007-03-29 16:10:34 +02:00
|
|
|
from model.devices.network import mesh
|
2007-02-25 12:17:45 +01:00
|
|
|
from hardware import hardwaremanager
|
2007-03-29 16:10:34 +02:00
|
|
|
from hardware import nmclient
|
2006-09-25 16:01:11 +02:00
|
|
|
from view.BuddyIcon import BuddyIcon
|
2007-02-28 16:19:01 +01:00
|
|
|
from view.pulsingicon import PulsingIcon
|
2007-03-29 16:10:34 +02:00
|
|
|
from sugar import profile
|
2006-09-25 16:40:15 +02:00
|
|
|
|
2007-02-28 15:42:41 +01:00
|
|
|
_ICON_NAME = 'device-network-wireless'
|
2007-02-26 12:23:00 +01:00
|
|
|
|
2007-02-28 16:19:01 +01:00
|
|
|
class AccessPointView(PulsingIcon):
|
2007-02-25 11:39:35 +01:00
|
|
|
def __init__(self, model):
|
2007-02-28 16:19:01 +01:00
|
|
|
PulsingIcon.__init__(self)
|
2007-02-25 12:17:45 +01:00
|
|
|
self._model = model
|
|
|
|
|
|
|
|
self.connect('activated', self._activate_cb)
|
2007-02-25 15:05:56 +01:00
|
|
|
|
|
|
|
model.connect('notify::strength', self._strength_changed_cb)
|
2007-02-25 17:34:15 +01:00
|
|
|
model.connect('notify::name', self._name_changed_cb)
|
2007-02-26 13:20:36 +01:00
|
|
|
model.connect('notify::state', self._state_changed_cb)
|
2007-02-25 15:05:56 +01:00
|
|
|
|
2007-03-16 03:03:42 +01:00
|
|
|
import sha
|
|
|
|
sh = sha.new()
|
|
|
|
data = self._model.props.name + hex(self._model.props.capabilities) + \
|
|
|
|
hex(self._model.props.mode)
|
|
|
|
sh.update(data)
|
|
|
|
h = hash(sh.digest())
|
|
|
|
idx = h % len(xocolor._colors)
|
2007-03-29 17:25:47 +02:00
|
|
|
self._device_stroke = xocolor._colors[idx][0]
|
|
|
|
self._device_fill = xocolor._colors[idx][1]
|
2007-02-28 16:19:01 +01:00
|
|
|
|
2007-02-25 15:05:56 +01:00
|
|
|
self._update_icon()
|
2007-02-25 17:34:15 +01:00
|
|
|
self._update_name()
|
2007-02-26 12:23:00 +01:00
|
|
|
self._update_state()
|
2007-02-25 15:05:56 +01:00
|
|
|
|
|
|
|
def _strength_changed_cb(self, model, pspec):
|
2007-02-25 11:39:35 +01:00
|
|
|
self._update_icon()
|
|
|
|
|
2007-02-25 17:34:15 +01:00
|
|
|
def _name_changed_cb(self, model, pspec):
|
|
|
|
self._update_name()
|
|
|
|
|
2007-02-26 12:23:00 +01:00
|
|
|
def _state_changed_cb(self, model, pspec):
|
|
|
|
self._update_state()
|
|
|
|
|
2007-02-25 12:17:45 +01:00
|
|
|
def _activate_cb(self, icon):
|
|
|
|
network_manager = hardwaremanager.get_network_manager()
|
2007-02-26 14:42:39 +01:00
|
|
|
if network_manager:
|
|
|
|
device = self._model.get_nm_device()
|
|
|
|
network = self._model.get_nm_network()
|
|
|
|
network_manager.set_active_device(device, network)
|
2007-02-25 12:17:45 +01:00
|
|
|
|
2007-02-25 17:34:15 +01:00
|
|
|
def _update_name(self):
|
|
|
|
self.props.tooltip = self._model.props.name
|
|
|
|
|
2007-02-25 11:39:35 +01:00
|
|
|
def _update_icon(self):
|
2007-02-28 15:42:41 +01:00
|
|
|
icon_name = canvasicon.get_icon_state(
|
|
|
|
_ICON_NAME, self._model.props.strength)
|
|
|
|
if icon_name:
|
|
|
|
self.props.icon_name = icon_name
|
2007-02-26 12:23:00 +01:00
|
|
|
|
|
|
|
def _update_state(self):
|
|
|
|
if self._model.props.state == accesspointmodel.STATE_CONNECTING:
|
2007-03-30 01:40:05 +02:00
|
|
|
self.props.pulse_time = 1.0
|
2007-03-29 17:25:47 +02:00
|
|
|
self.props.colors = [
|
|
|
|
[ color.HTMLColor(self._device_stroke),
|
|
|
|
color.HTMLColor(self._device_fill) ],
|
|
|
|
[ color.HTMLColor(self._device_stroke),
|
|
|
|
color.HTMLColor('#e2e2e2') ]
|
|
|
|
]
|
2007-02-26 12:23:00 +01:00
|
|
|
elif self._model.props.state == accesspointmodel.STATE_CONNECTED:
|
2007-03-30 01:40:05 +02:00
|
|
|
self.props.pulse_time = 2.0
|
2007-03-29 17:25:47 +02:00
|
|
|
self.props.colors = [
|
|
|
|
[ color.HTMLColor(self._device_stroke),
|
|
|
|
color.HTMLColor(self._device_fill) ],
|
|
|
|
[ color.HTMLColor('#ffffff'),
|
|
|
|
color.HTMLColor(self._device_fill) ]
|
|
|
|
]
|
2007-02-26 12:23:00 +01:00
|
|
|
elif self._model.props.state == accesspointmodel.STATE_NOTCONNECTED:
|
2007-03-29 17:25:47 +02:00
|
|
|
self.props.pulse_time = 0.0
|
|
|
|
self.props.colors = [
|
|
|
|
[ color.HTMLColor(self._device_stroke),
|
|
|
|
color.HTMLColor(self._device_fill) ]
|
|
|
|
]
|
2007-02-25 11:39:35 +01:00
|
|
|
|
2007-03-29 21:00:27 +02:00
|
|
|
|
|
|
|
_MESH_ICON_NAME = 'theme:device-network-mesh'
|
|
|
|
|
|
|
|
class MeshDeviceView(PulsingIcon):
|
2007-03-29 16:10:34 +02:00
|
|
|
def __init__(self, nm_device):
|
2007-03-29 21:00:27 +02:00
|
|
|
PulsingIcon.__init__(self, scale=units.MEDIUM_ICON_SCALE,
|
|
|
|
icon_name=_MESH_ICON_NAME)
|
2007-03-29 16:10:34 +02:00
|
|
|
self._nm_device = nm_device
|
|
|
|
self.props.tooltip = _("Mesh Network")
|
|
|
|
|
2007-03-29 21:00:27 +02:00
|
|
|
mycolor = profile.get_color()
|
|
|
|
self._device_fill = mycolor.get_fill_color()
|
|
|
|
self._device_stroke = mycolor.get_stroke_color()
|
|
|
|
|
2007-03-29 16:10:34 +02:00
|
|
|
self.connect('activated', self._activate_cb)
|
|
|
|
|
|
|
|
self._nm_device.connect('state-changed', self._state_changed_cb)
|
|
|
|
self._update_state()
|
|
|
|
|
|
|
|
def _activate_cb(self, icon):
|
|
|
|
network_manager = hardwaremanager.get_network_manager()
|
|
|
|
network_manager.set_active_device(self._nm_device)
|
|
|
|
|
|
|
|
def _state_changed_cb(self, model):
|
|
|
|
self._update_state()
|
|
|
|
|
|
|
|
def _update_state(self):
|
|
|
|
state = self._nm_device.get_state()
|
|
|
|
if state == nmclient.DEVICE_STATE_ACTIVATING:
|
2007-03-29 21:00:27 +02:00
|
|
|
self.props.pulse_time = 0.75
|
|
|
|
self.props.colors = [
|
|
|
|
[ color.HTMLColor(self._device_stroke),
|
|
|
|
color.HTMLColor(self._device_fill) ],
|
|
|
|
[ color.HTMLColor(self._device_stroke),
|
|
|
|
color.HTMLColor('#e2e2e2') ]
|
|
|
|
]
|
2007-03-29 16:10:34 +02:00
|
|
|
elif state == nmclient.DEVICE_STATE_ACTIVATED:
|
2007-03-29 21:00:27 +02:00
|
|
|
self.props.pulse_time = 1.5
|
|
|
|
self.props.colors = [
|
|
|
|
[ color.HTMLColor(self._device_stroke),
|
|
|
|
color.HTMLColor(self._device_fill) ],
|
|
|
|
[ color.HTMLColor('#ffffff'),
|
|
|
|
color.HTMLColor(self._device_fill) ]
|
|
|
|
]
|
2007-03-29 16:10:34 +02:00
|
|
|
elif state == nmclient.DEVICE_STATE_INACTIVE:
|
2007-03-29 21:00:27 +02:00
|
|
|
self.props.pulse_time = 0.0
|
|
|
|
self.props.colors = [
|
|
|
|
[ color.HTMLColor(self._device_stroke),
|
|
|
|
color.HTMLColor(self._device_fill) ]
|
|
|
|
]
|
2007-03-29 16:10:34 +02:00
|
|
|
|
2006-10-06 11:11:38 +02:00
|
|
|
class ActivityView(SnowflakeBox):
|
2006-12-04 20:12:24 +01:00
|
|
|
def __init__(self, shell, menu_shell, model):
|
|
|
|
SnowflakeBox.__init__(self)
|
2006-09-25 16:40:15 +02:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
self._shell = shell
|
|
|
|
self._model = model
|
|
|
|
self._icons = {}
|
2006-09-25 16:40:15 +02:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
icon = CanvasIcon(icon_name=model.get_icon_name(),
|
2007-02-23 17:08:37 +01:00
|
|
|
xo_color=model.get_color(), box_width=80)
|
2006-12-04 20:12:24 +01:00
|
|
|
icon.connect('activated', self._clicked_cb)
|
|
|
|
self.append(icon, hippo.PACK_FIXED)
|
|
|
|
self.set_root(icon)
|
2006-09-25 16:40:15 +02:00
|
|
|
|
2007-04-10 21:55:55 +02:00
|
|
|
def has_buddy_icon(self, key):
|
|
|
|
return self._icons.has_key(key)
|
2006-09-25 18:38:01 +02:00
|
|
|
|
2007-04-10 21:55:55 +02:00
|
|
|
def add_buddy_icon(self, key, icon):
|
|
|
|
self._icons[key] = icon
|
2006-12-04 20:12:24 +01:00
|
|
|
self.append(icon, hippo.PACK_FIXED)
|
2006-09-25 18:38:01 +02:00
|
|
|
|
2007-04-10 21:55:55 +02:00
|
|
|
def remove_buddy_icon(self, key):
|
|
|
|
icon = self._icons[key]
|
2006-12-04 20:12:24 +01:00
|
|
|
self.remove(icon)
|
2007-04-10 21:55:55 +02:00
|
|
|
del self._icons[key]
|
2006-09-25 18:38:01 +02:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
def _clicked_cb(self, item):
|
2007-04-11 21:08:40 +02:00
|
|
|
bundle_id = self._model.get_service().get_service_name()
|
2006-12-04 20:12:24 +01:00
|
|
|
self._shell.join_activity(bundle_id, self._model.get_id())
|
2006-09-25 18:38:01 +02:00
|
|
|
|
2006-10-06 11:11:38 +02:00
|
|
|
class MeshBox(SpreadBox):
|
2006-12-04 20:12:24 +01:00
|
|
|
def __init__(self, shell, menu_shell):
|
|
|
|
SpreadBox.__init__(self, background_color=0xe2e2e2ff)
|
2006-09-15 15:28:18 +02:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
self._shell = shell
|
|
|
|
self._menu_shell = menu_shell
|
|
|
|
self._model = shell.get_model().get_mesh()
|
|
|
|
self._buddies = {}
|
|
|
|
self._activities = {}
|
2007-02-25 11:39:35 +01:00
|
|
|
self._access_points = {}
|
2007-03-29 16:10:34 +02:00
|
|
|
self._mesh = None
|
2006-12-04 20:12:24 +01:00
|
|
|
self._buddy_to_activity = {}
|
2007-03-30 01:08:44 +02:00
|
|
|
self._suspended = True
|
2006-09-15 15:28:18 +02:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
for buddy_model in self._model.get_buddies():
|
|
|
|
self._add_alone_buddy(buddy_model)
|
2006-09-12 12:19:20 +02:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
self._model.connect('buddy-added', self._buddy_added_cb)
|
|
|
|
self._model.connect('buddy-removed', self._buddy_removed_cb)
|
|
|
|
self._model.connect('buddy-moved', self._buddy_moved_cb)
|
2006-09-25 16:40:15 +02:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
for activity_model in self._model.get_activities():
|
|
|
|
self._add_activity(activity_model)
|
2006-09-25 16:40:15 +02:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
self._model.connect('activity-added', self._activity_added_cb)
|
|
|
|
self._model.connect('activity-removed', self._activity_removed_cb)
|
2006-09-04 17:00:45 +02:00
|
|
|
|
2007-02-25 11:39:35 +01:00
|
|
|
for ap_model in self._model.get_access_points():
|
|
|
|
self._add_access_point(ap_model)
|
|
|
|
|
|
|
|
self._model.connect('access-point-added',
|
|
|
|
self._access_point_added_cb)
|
|
|
|
self._model.connect('access-point-removed',
|
|
|
|
self._access_point_removed_cb)
|
|
|
|
|
2007-03-29 16:10:34 +02:00
|
|
|
if self._model.get_mesh():
|
|
|
|
self._add_mesh_icon(self._model.get_mesh())
|
|
|
|
|
|
|
|
self._model.connect('mesh-added',
|
|
|
|
self._mesh_added_cb)
|
|
|
|
self._model.connect('mesh-removed',
|
|
|
|
self._mesh_removed_cb)
|
|
|
|
|
|
|
|
def _mesh_added_cb(self, model, mesh):
|
|
|
|
self._add_mesh_icon(mesh)
|
|
|
|
|
|
|
|
def _mesh_removed_cb(self, model):
|
2007-04-05 06:22:13 +02:00
|
|
|
self._remove_mesh()
|
2007-03-29 16:10:34 +02:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
def _buddy_added_cb(self, model, buddy_model):
|
|
|
|
self._add_alone_buddy(buddy_model)
|
2006-09-25 16:40:15 +02:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
def _buddy_removed_cb(self, model, buddy_model):
|
|
|
|
self._remove_buddy(buddy_model)
|
2006-09-25 16:40:15 +02:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
def _buddy_moved_cb(self, model, buddy_model, activity_model):
|
|
|
|
self._move_buddy(buddy_model, activity_model)
|
2006-09-25 18:38:01 +02:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
def _activity_added_cb(self, model, activity_model):
|
|
|
|
self._add_activity(activity_model)
|
2006-09-25 16:40:15 +02:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
def _activity_removed_cb(self, model, activity_model):
|
|
|
|
self._remove_activity(activity_model)
|
2006-09-04 17:00:45 +02:00
|
|
|
|
2007-02-25 11:39:35 +01:00
|
|
|
def _access_point_added_cb(self, model, ap_model):
|
|
|
|
self._add_access_point(ap_model)
|
|
|
|
|
|
|
|
def _access_point_removed_cb(self, model, ap_model):
|
2007-03-07 14:23:44 +01:00
|
|
|
self._remove_access_point(ap_model)
|
2007-02-25 11:39:35 +01:00
|
|
|
|
2007-03-29 16:10:34 +02:00
|
|
|
def _add_mesh_icon(self, mesh):
|
|
|
|
if self._mesh:
|
|
|
|
self._remove_mesh()
|
|
|
|
if not mesh:
|
|
|
|
return
|
|
|
|
self._mesh = MeshDeviceView(mesh)
|
|
|
|
self.add_item(self._mesh)
|
|
|
|
|
2007-04-05 06:22:13 +02:00
|
|
|
def _remove_mesh(self):
|
2007-03-29 16:10:34 +02:00
|
|
|
if not self._mesh:
|
|
|
|
return
|
|
|
|
self.remove_item(self._mesh)
|
|
|
|
self._mesh = None
|
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
def _add_alone_buddy(self, buddy_model):
|
|
|
|
icon = BuddyIcon(self._shell, self._menu_shell, buddy_model)
|
|
|
|
self.add_item(icon)
|
2006-09-25 16:40:15 +02:00
|
|
|
|
2007-04-10 21:55:55 +02:00
|
|
|
self._buddies[buddy_model.get_key()] = icon
|
2006-09-25 16:40:15 +02:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
def _remove_alone_buddy(self, buddy_model):
|
2007-04-10 21:55:55 +02:00
|
|
|
icon = self._buddies[buddy_model.get_key()]
|
2006-12-04 20:12:24 +01:00
|
|
|
self.remove_item(icon)
|
2007-04-10 21:55:55 +02:00
|
|
|
del self._buddies[buddy_model.get_key()]
|
2006-09-25 16:40:15 +02:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
def _remove_buddy(self, buddy_model):
|
2007-04-10 21:55:55 +02:00
|
|
|
key = buddy_model.get_key()
|
|
|
|
if self._buddies.has_key(key):
|
2006-12-04 20:12:24 +01:00
|
|
|
self._remove_alone_buddy(buddy_model)
|
|
|
|
else:
|
|
|
|
for activity in self._activities.values():
|
2007-04-10 21:55:55 +02:00
|
|
|
if activity.has_buddy_icon(key):
|
|
|
|
activity.remove_buddy_icon(key)
|
2006-09-25 18:55:35 +02:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
def _move_buddy(self, buddy_model, activity_model):
|
2007-04-10 21:55:55 +02:00
|
|
|
key = buddy_model.get_key()
|
2006-09-25 18:38:01 +02:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
self._remove_buddy(buddy_model)
|
2006-09-25 18:38:01 +02:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
if activity_model == None:
|
|
|
|
self._add_alone_buddy(buddy_model)
|
|
|
|
else:
|
|
|
|
activity = self._activities[activity_model.get_id()]
|
2006-09-25 18:38:01 +02:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
icon = BuddyIcon(self._shell, self._menu_shell, buddy_model)
|
2007-04-10 21:55:55 +02:00
|
|
|
activity.add_buddy_icon(buddy_model.get_key(), icon)
|
2006-09-25 18:38:01 +02:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
def _add_activity(self, activity_model):
|
|
|
|
icon = ActivityView(self._shell, self._menu_shell, activity_model)
|
|
|
|
self.add_item(icon)
|
2006-09-25 16:40:15 +02:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
self._activities[activity_model.get_id()] = icon
|
2006-09-25 16:40:15 +02:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
def _remove_activity(self, activity_model):
|
|
|
|
icon = self._activities[activity_model.get_id()]
|
|
|
|
self.remove_item(icon)
|
|
|
|
del self._activities[activity_model.get_id()]
|
2007-02-25 11:39:35 +01:00
|
|
|
|
|
|
|
def _add_access_point(self, ap_model):
|
|
|
|
icon = AccessPointView(ap_model)
|
|
|
|
self.add_item(icon)
|
|
|
|
|
|
|
|
self._access_points[ap_model.get_id()] = icon
|
|
|
|
|
|
|
|
def _remove_access_point(self, ap_model):
|
|
|
|
icon = self._access_points[ap_model.get_id()]
|
|
|
|
self.remove_item(icon)
|
|
|
|
del self._access_points[ap_model.get_id()]
|
2007-03-30 01:08:44 +02:00
|
|
|
|
|
|
|
def suspend(self):
|
|
|
|
if not self._suspended:
|
|
|
|
self._suspended = True
|
|
|
|
for ap in self._access_points.values():
|
|
|
|
ap.props.paused = True
|
|
|
|
|
|
|
|
def resume(self):
|
|
|
|
if self._suspended:
|
|
|
|
self._suspended = False
|
|
|
|
for ap in self._access_points.values():
|
|
|
|
ap.props.paused = False
|