2007-06-24 14:45:05 +02:00
|
|
|
# Copyright (C) 2006-2007 Red Hat, Inc.
|
2006-10-15 01:24:45 +02:00
|
|
|
#
|
|
|
|
# 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-09-25 16:01:11 +02:00
|
|
|
import gobject
|
|
|
|
|
2007-02-23 13:09:33 +01:00
|
|
|
from sugar.graphics.xocolor import XoColor
|
2007-04-09 20:40:56 +02:00
|
|
|
from sugar.presence import presenceservice
|
2007-08-09 18:10:16 +02:00
|
|
|
from sugar import activity
|
2007-05-27 20:24:10 +02:00
|
|
|
|
2006-09-25 16:01:11 +02:00
|
|
|
from model.BuddyModel import BuddyModel
|
2007-02-26 12:23:00 +01:00
|
|
|
from model.accesspointmodel import AccessPointModel
|
2007-02-25 11:39:35 +01:00
|
|
|
from hardware import hardwaremanager
|
2007-03-29 16:10:34 +02:00
|
|
|
from hardware import nmclient
|
2007-02-25 11:39:35 +01:00
|
|
|
|
2006-09-25 16:01:11 +02:00
|
|
|
class ActivityModel:
|
2007-08-31 11:37:42 +02:00
|
|
|
def __init__(self, activity, bundle):
|
|
|
|
self.activity = activity
|
|
|
|
self.bundle = bundle
|
2006-12-04 20:12:24 +01:00
|
|
|
|
|
|
|
def get_id(self):
|
2007-08-31 11:37:42 +02:00
|
|
|
return self.activity.props.id
|
2006-12-04 20:12:24 +01:00
|
|
|
|
|
|
|
def get_icon_name(self):
|
2007-08-31 11:37:42 +02:00
|
|
|
return self.bundle.icon
|
2006-12-04 20:12:24 +01:00
|
|
|
|
|
|
|
def get_color(self):
|
2007-08-31 11:37:42 +02:00
|
|
|
return XoColor(self.activity.props.color)
|
2006-12-04 20:12:24 +01:00
|
|
|
|
2007-10-09 13:15:06 +02:00
|
|
|
def get_bundle_id(self):
|
|
|
|
return self.bundle.bundle_id
|
2007-05-15 22:21:40 +02:00
|
|
|
|
2006-09-25 16:01:11 +02:00
|
|
|
class MeshModel(gobject.GObject):
|
2006-12-04 20:12:24 +01:00
|
|
|
__gsignals__ = {
|
2007-02-25 11:39:35 +01:00
|
|
|
'activity-added': (gobject.SIGNAL_RUN_FIRST,
|
|
|
|
gobject.TYPE_NONE, ([gobject.TYPE_PYOBJECT])),
|
|
|
|
'activity-removed': (gobject.SIGNAL_RUN_FIRST,
|
|
|
|
gobject.TYPE_NONE, ([gobject.TYPE_PYOBJECT])),
|
|
|
|
'buddy-added': (gobject.SIGNAL_RUN_FIRST,
|
|
|
|
gobject.TYPE_NONE, ([gobject.TYPE_PYOBJECT])),
|
|
|
|
'buddy-moved': (gobject.SIGNAL_RUN_FIRST,
|
|
|
|
gobject.TYPE_NONE,
|
|
|
|
([gobject.TYPE_PYOBJECT,
|
|
|
|
gobject.TYPE_PYOBJECT])),
|
|
|
|
'buddy-removed': (gobject.SIGNAL_RUN_FIRST,
|
|
|
|
gobject.TYPE_NONE, ([gobject.TYPE_PYOBJECT])),
|
|
|
|
'access-point-added': (gobject.SIGNAL_RUN_FIRST,
|
|
|
|
gobject.TYPE_NONE, ([gobject.TYPE_PYOBJECT])),
|
|
|
|
'access-point-removed': (gobject.SIGNAL_RUN_FIRST,
|
2007-03-29 16:10:34 +02:00
|
|
|
gobject.TYPE_NONE, ([gobject.TYPE_PYOBJECT])),
|
|
|
|
'mesh-added': (gobject.SIGNAL_RUN_FIRST,
|
|
|
|
gobject.TYPE_NONE, ([gobject.TYPE_PYOBJECT])),
|
|
|
|
'mesh-removed': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, ([]))
|
2006-12-04 20:12:24 +01:00
|
|
|
}
|
|
|
|
|
2007-02-21 17:53:44 +01:00
|
|
|
def __init__(self):
|
2006-12-04 20:12:24 +01:00
|
|
|
gobject.GObject.__init__(self)
|
|
|
|
|
|
|
|
self._activities = {}
|
|
|
|
self._buddies = {}
|
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
|
|
|
|
2007-04-09 20:40:56 +02:00
|
|
|
self._pservice = presenceservice.get_instance()
|
2007-04-09 22:05:32 +02:00
|
|
|
self._pservice.connect("activity-appeared",
|
|
|
|
self._activity_appeared_cb)
|
2006-12-04 20:12:24 +01:00
|
|
|
self._pservice.connect('activity-disappeared',
|
|
|
|
self._activity_disappeared_cb)
|
|
|
|
self._pservice.connect("buddy-appeared",
|
|
|
|
self._buddy_appeared_cb)
|
|
|
|
self._pservice.connect("buddy-disappeared",
|
|
|
|
self._buddy_disappeared_cb)
|
|
|
|
|
|
|
|
# Add any buddies the PS knows about already
|
2007-08-27 21:47:58 +02:00
|
|
|
self._pservice.get_buddies_async(reply_handler=self._get_buddies_cb)
|
2006-12-04 20:12:24 +01:00
|
|
|
|
2007-08-27 21:47:58 +02:00
|
|
|
self._pservice.get_activities_async(reply_handler=self._get_activities_cb)
|
2006-12-04 20:12:24 +01:00
|
|
|
|
2007-02-25 11:39:35 +01:00
|
|
|
network_manager = hardwaremanager.get_network_manager()
|
2007-02-26 14:42:39 +01:00
|
|
|
if network_manager:
|
|
|
|
for nm_device in network_manager.get_devices():
|
|
|
|
self._add_network_device(nm_device)
|
|
|
|
network_manager.connect('device-added',
|
|
|
|
self._nm_device_added_cb)
|
2007-03-29 16:10:34 +02:00
|
|
|
network_manager.connect('device-removed',
|
|
|
|
self._nm_device_removed_cb)
|
2007-02-25 11:39:35 +01:00
|
|
|
|
2007-08-27 21:47:58 +02:00
|
|
|
def _get_buddies_cb(self, list):
|
|
|
|
for buddy in list:
|
|
|
|
self._buddy_appeared_cb(self._pservice, buddy)
|
|
|
|
|
|
|
|
def _get_activities_cb(self, list):
|
|
|
|
for activity in list:
|
|
|
|
self._check_activity(activity)
|
|
|
|
|
2007-02-26 13:20:36 +01:00
|
|
|
def _nm_device_added_cb(self, manager, nm_device):
|
2007-02-25 11:39:35 +01:00
|
|
|
self._add_network_device(nm_device)
|
|
|
|
|
2007-03-29 16:10:34 +02:00
|
|
|
def _nm_device_removed_cb(self, manager, nm_device):
|
|
|
|
self._remove_network_device(nm_device)
|
|
|
|
|
2007-02-25 12:17:45 +01:00
|
|
|
def _nm_network_appeared_cb(self, nm_device, nm_network):
|
|
|
|
self._add_access_point(nm_device, nm_network)
|
2007-02-25 11:39:35 +01:00
|
|
|
|
2007-02-25 12:17:45 +01:00
|
|
|
def _nm_network_disappeared_cb(self, nm_device, nm_network):
|
2007-04-05 05:48:09 +02:00
|
|
|
if self._access_points.has_key(nm_network.get_op()):
|
|
|
|
ap = self._access_points[nm_network.get_op()]
|
|
|
|
self._remove_access_point(ap)
|
2007-02-25 11:39:35 +01:00
|
|
|
|
|
|
|
def _add_network_device(self, nm_device):
|
2007-03-29 16:10:34 +02:00
|
|
|
dtype = nm_device.get_type()
|
|
|
|
if dtype == nmclient.DEVICE_TYPE_802_11_WIRELESS:
|
|
|
|
for nm_network in nm_device.get_networks():
|
|
|
|
self._add_access_point(nm_device, nm_network)
|
|
|
|
|
|
|
|
nm_device.connect('network-appeared',
|
|
|
|
self._nm_network_appeared_cb)
|
|
|
|
nm_device.connect('network-disappeared',
|
|
|
|
self._nm_network_disappeared_cb)
|
|
|
|
elif dtype == nmclient.DEVICE_TYPE_802_11_MESH_OLPC:
|
|
|
|
self._mesh = nm_device
|
|
|
|
self.emit('mesh-added', self._mesh)
|
|
|
|
|
|
|
|
def _remove_network_device(self, nm_device):
|
|
|
|
if nm_device == self._mesh:
|
|
|
|
self._mesh = None
|
|
|
|
self.emit('mesh-removed')
|
|
|
|
elif nm_device.get_type() == nmclient.DEVICE_TYPE_802_11_WIRELESS:
|
|
|
|
aplist = self._access_points.values()
|
|
|
|
for ap in aplist:
|
|
|
|
if ap.get_nm_device() == nm_device:
|
|
|
|
self._remove_access_point(ap)
|
2007-02-25 11:39:35 +01:00
|
|
|
|
2007-02-25 12:17:45 +01:00
|
|
|
def _add_access_point(self, nm_device, nm_network):
|
|
|
|
model = AccessPointModel(nm_device, nm_network)
|
2007-04-05 05:48:09 +02:00
|
|
|
self._access_points[model.get_id()] = model
|
2007-02-25 11:39:35 +01:00
|
|
|
self.emit('access-point-added', model)
|
|
|
|
|
2007-04-05 05:48:09 +02:00
|
|
|
def _remove_access_point(self, ap):
|
|
|
|
if not self._access_points.has_key(ap.get_id()):
|
|
|
|
return
|
|
|
|
self.emit('access-point-removed', ap)
|
|
|
|
del self._access_points[ap.get_id()]
|
2007-02-25 11:39:35 +01:00
|
|
|
|
2007-03-29 16:10:34 +02:00
|
|
|
def get_mesh(self):
|
|
|
|
return self._mesh
|
|
|
|
|
2007-02-25 11:39:35 +01:00
|
|
|
def get_access_points(self):
|
|
|
|
return self._access_points.values()
|
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
def get_activities(self):
|
|
|
|
return self._activities.values()
|
|
|
|
|
|
|
|
def get_buddies(self):
|
|
|
|
return self._buddies.values()
|
|
|
|
|
2007-04-20 19:56:46 +02:00
|
|
|
def _buddy_activity_changed_cb(self, model, cur_activity):
|
|
|
|
if not self._buddies.has_key(model.get_key()):
|
2006-12-04 20:12:24 +01:00
|
|
|
return
|
2007-04-27 18:56:13 +02:00
|
|
|
if cur_activity and self._activities.has_key(cur_activity.props.id):
|
|
|
|
activity_model = self._activities[cur_activity.props.id]
|
2007-04-20 19:56:46 +02:00
|
|
|
self.emit('buddy-moved', model, activity_model)
|
|
|
|
else:
|
|
|
|
self.emit('buddy-moved', model, None)
|
2006-12-04 20:12:24 +01:00
|
|
|
|
|
|
|
def _buddy_appeared_cb(self, pservice, buddy):
|
2007-04-10 21:55:55 +02:00
|
|
|
if self._buddies.has_key(buddy.props.key):
|
2006-12-04 20:12:24 +01:00
|
|
|
return
|
|
|
|
|
2007-04-10 21:55:55 +02:00
|
|
|
model = BuddyModel(buddy=buddy)
|
2006-12-04 20:12:24 +01:00
|
|
|
model.connect('current-activity-changed',
|
|
|
|
self._buddy_activity_changed_cb)
|
2007-04-10 21:55:55 +02:00
|
|
|
self._buddies[buddy.props.key] = model
|
2006-12-04 20:12:24 +01:00
|
|
|
self.emit('buddy-added', model)
|
|
|
|
|
2007-04-09 22:30:30 +02:00
|
|
|
cur_activity = buddy.props.current_activity
|
2006-12-04 20:12:24 +01:00
|
|
|
if cur_activity:
|
2007-04-20 19:56:46 +02:00
|
|
|
self._buddy_activity_changed_cb(model, cur_activity)
|
2006-12-04 20:12:24 +01:00
|
|
|
|
|
|
|
def _buddy_disappeared_cb(self, pservice, buddy):
|
2007-04-10 21:55:55 +02:00
|
|
|
if not self._buddies.has_key(buddy.props.key):
|
2006-12-04 20:12:24 +01:00
|
|
|
return
|
2007-04-10 21:55:55 +02:00
|
|
|
self.emit('buddy-removed', self._buddies[buddy.props.key])
|
|
|
|
del self._buddies[buddy.props.key]
|
2006-12-04 20:12:24 +01:00
|
|
|
|
2007-04-09 22:05:32 +02:00
|
|
|
def _activity_appeared_cb(self, pservice, activity):
|
|
|
|
self._check_activity(activity)
|
2006-12-04 20:12:24 +01:00
|
|
|
|
2007-08-09 18:10:16 +02:00
|
|
|
def _check_activity(self, presence_activity):
|
|
|
|
registry = activity.get_registry()
|
2007-08-31 11:37:42 +02:00
|
|
|
bundle = registry.get_activity(presence_activity.props.type)
|
|
|
|
if not bundle:
|
2007-04-09 22:05:32 +02:00
|
|
|
return
|
2007-08-09 18:10:16 +02:00
|
|
|
if self.has_activity(presence_activity.props.id):
|
2007-04-09 22:05:32 +02:00
|
|
|
return
|
2007-08-31 11:37:42 +02:00
|
|
|
self.add_activity(bundle, presence_activity)
|
2006-12-04 20:12:24 +01:00
|
|
|
|
|
|
|
def has_activity(self, activity_id):
|
|
|
|
return self._activities.has_key(activity_id)
|
|
|
|
|
|
|
|
def get_activity(self, activity_id):
|
|
|
|
if self.has_activity(activity_id):
|
|
|
|
return self._activities[activity_id]
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
2007-08-31 11:37:42 +02:00
|
|
|
def add_activity(self, bundle, activity):
|
|
|
|
model = ActivityModel(activity, bundle)
|
2006-12-04 20:12:24 +01:00
|
|
|
self._activities[model.get_id()] = model
|
|
|
|
self.emit('activity-added', model)
|
|
|
|
|
|
|
|
for buddy in self._pservice.get_buddies():
|
2007-04-09 22:05:32 +02:00
|
|
|
cur_activity = buddy.props.current_activity
|
2007-04-10 21:55:55 +02:00
|
|
|
key = buddy.props.key
|
|
|
|
if cur_activity == activity and self._buddies.has_key(key):
|
|
|
|
buddy_model = self._buddies[key]
|
2006-12-04 20:12:24 +01:00
|
|
|
self.emit('buddy-moved', buddy_model, model)
|
|
|
|
|
|
|
|
def _activity_disappeared_cb(self, pservice, activity):
|
2007-04-27 18:56:13 +02:00
|
|
|
if self._activities.has_key(activity.props.id):
|
|
|
|
activity_model = self._activities[activity.props.id]
|
2006-12-04 20:12:24 +01:00
|
|
|
self.emit('activity-removed', activity_model)
|
2007-04-27 18:56:13 +02:00
|
|
|
del self._activities[activity.props.id]
|