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-09-25 16:01:11 +02:00
|
|
|
import gobject
|
|
|
|
|
2007-02-23 13:09:33 +01:00
|
|
|
from sugar.graphics.xocolor import XoColor
|
2006-09-25 16:01:11 +02:00
|
|
|
from sugar.presence import PresenceService
|
2007-02-21 17:53:44 +01:00
|
|
|
from sugar.activity import bundleregistry
|
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
|
|
|
|
|
2006-09-25 16:01:11 +02:00
|
|
|
class ActivityModel:
|
2006-12-04 20:12:24 +01:00
|
|
|
def __init__(self, activity, bundle, service):
|
|
|
|
self._service = service
|
|
|
|
self._activity = activity
|
2006-12-20 13:58:29 +01:00
|
|
|
self._bundle = bundle
|
2006-12-04 20:12:24 +01:00
|
|
|
|
|
|
|
def get_id(self):
|
|
|
|
return self._activity.get_id()
|
|
|
|
|
|
|
|
def get_icon_name(self):
|
2006-12-20 13:58:29 +01:00
|
|
|
return self._bundle.get_icon()
|
2006-12-04 20:12:24 +01:00
|
|
|
|
|
|
|
def get_color(self):
|
2007-02-23 13:09:33 +01:00
|
|
|
return XoColor(self._activity.get_color())
|
2006-12-04 20:12:24 +01:00
|
|
|
|
|
|
|
def get_service(self):
|
|
|
|
return self._service
|
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,
|
|
|
|
gobject.TYPE_NONE, ([gobject.TYPE_PYOBJECT]))
|
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-02-21 17:53:44 +01:00
|
|
|
self._bundle_registry = bundleregistry.get_registry()
|
2006-12-04 20:12:24 +01:00
|
|
|
|
|
|
|
self._pservice = PresenceService.get_instance()
|
|
|
|
self._pservice.connect("service-appeared",
|
|
|
|
self._service_appeared_cb)
|
|
|
|
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
|
|
|
|
for buddy in self._pservice.get_buddies():
|
|
|
|
self._buddy_appeared_cb(self._pservice, buddy)
|
|
|
|
|
|
|
|
for service in self._pservice.get_services():
|
|
|
|
self._check_service(service)
|
|
|
|
|
2007-02-25 11:39:35 +01:00
|
|
|
network_manager = hardwaremanager.get_network_manager()
|
|
|
|
for nm_device in network_manager.get_devices():
|
|
|
|
self._add_network_device(nm_device)
|
2007-02-26 12:26:10 +01:00
|
|
|
network_manager.connect('device-added',
|
2007-02-25 11:39:35 +01:00
|
|
|
self._nm_device_activated_cb)
|
|
|
|
|
|
|
|
def _nm_device_activated_cb(self, manager, nm_device):
|
|
|
|
self._add_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):
|
|
|
|
self._remove_access_point(nm_device, nm_network)
|
2007-02-25 11:39:35 +01:00
|
|
|
|
|
|
|
def _add_network_device(self, nm_device):
|
2007-02-25 12:17:45 +01:00
|
|
|
for nm_network in nm_device.get_networks():
|
|
|
|
self._add_access_point(nm_device, nm_network)
|
2007-02-25 11:39:35 +01:00
|
|
|
nm_device.connect('network-appeared',
|
|
|
|
self._nm_network_appeared_cb)
|
|
|
|
nm_device.connect('network-disappeared',
|
|
|
|
self._nm_network_disappeared_cb)
|
|
|
|
|
2007-02-25 12:17:45 +01:00
|
|
|
def _add_access_point(self, nm_device, nm_network):
|
|
|
|
model = AccessPointModel(nm_device, nm_network)
|
2007-02-25 11:39:35 +01:00
|
|
|
self._access_points[nm_network.get_op()] = model
|
|
|
|
self.emit('access-point-added', model)
|
|
|
|
|
|
|
|
def _remove_access_point(self, nm_network):
|
|
|
|
self.emit('access-point-removed',
|
|
|
|
self._access_points[nm_network.get_op()])
|
|
|
|
del self._access_points[nm_network.get_op()]
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
def _buddy_activity_changed_cb(self, buddy, cur_activity):
|
|
|
|
if not self._buddies.has_key(buddy.get_name()):
|
|
|
|
return
|
|
|
|
buddy_model = self._buddies[buddy.get_name()]
|
|
|
|
if cur_activity == None:
|
|
|
|
self.emit('buddy-moved', buddy_model, None)
|
|
|
|
else:
|
|
|
|
self._notify_buddy_change(buddy_model, cur_activity)
|
|
|
|
|
|
|
|
def _notify_buddy_change(self, buddy_model, cur_activity):
|
|
|
|
if self._activities.has_key(cur_activity.get_id()):
|
|
|
|
activity_model = self._activities[cur_activity.get_id()]
|
|
|
|
self.emit('buddy-moved', buddy_model, activity_model)
|
|
|
|
|
|
|
|
def _buddy_appeared_cb(self, pservice, buddy):
|
|
|
|
model = BuddyModel(buddy=buddy)
|
|
|
|
if self._buddies.has_key(model.get_name()):
|
|
|
|
del model
|
|
|
|
return
|
|
|
|
|
|
|
|
model.connect('current-activity-changed',
|
|
|
|
self._buddy_activity_changed_cb)
|
|
|
|
self._buddies[model.get_name()] = model
|
|
|
|
self.emit('buddy-added', model)
|
|
|
|
|
|
|
|
cur_activity = buddy.get_current_activity()
|
|
|
|
if cur_activity:
|
|
|
|
self._notify_buddy_change(model, cur_activity)
|
|
|
|
|
|
|
|
def _buddy_disappeared_cb(self, pservice, buddy):
|
|
|
|
if not self._buddies.has_key(buddy.get_name()):
|
|
|
|
return
|
|
|
|
self.emit('buddy-removed', buddy)
|
|
|
|
del self._buddies[buddy.get_name()]
|
|
|
|
|
|
|
|
def _service_appeared_cb(self, pservice, service):
|
|
|
|
self._check_service(service)
|
|
|
|
|
|
|
|
def _check_service(self, service):
|
2006-12-20 13:58:29 +01:00
|
|
|
service_type = service.get_type()
|
|
|
|
bundle = self._bundle_registry.find_by_default_type(service_type)
|
|
|
|
if bundle != None:
|
2006-12-04 20:12:24 +01:00
|
|
|
activity_id = service.get_activity_id()
|
|
|
|
if not self.has_activity(activity_id):
|
|
|
|
activity = self._pservice.get_activity(activity_id)
|
|
|
|
if activity != None:
|
2006-12-20 13:58:29 +01:00
|
|
|
self.add_activity(bundle, activity, service)
|
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
|
|
|
|
|
2006-12-20 13:58:29 +01:00
|
|
|
def add_activity(self, bundle, activity, service):
|
2006-12-04 20:12:24 +01:00
|
|
|
model = ActivityModel(activity, bundle, service)
|
|
|
|
self._activities[model.get_id()] = model
|
|
|
|
self.emit('activity-added', model)
|
|
|
|
|
|
|
|
for buddy in self._pservice.get_buddies():
|
|
|
|
cur_activity = buddy.get_current_activity()
|
|
|
|
name = buddy.get_name()
|
|
|
|
if cur_activity == activity and self._buddies.has_key(name):
|
|
|
|
buddy_model = self._buddies[name]
|
|
|
|
self.emit('buddy-moved', buddy_model, model)
|
|
|
|
|
|
|
|
def _activity_disappeared_cb(self, pservice, activity):
|
|
|
|
if self._activities.has_key(activity.get_id()):
|
|
|
|
activity_model = self._activities[activity.get_id()]
|
|
|
|
self.emit('activity-removed', activity_model)
|
|
|
|
del self._activities[activity.get_id()]
|